diff --git a/barebones/barebones.c b/barebones/barebones.c index d031670d0c97dd82cff52da7e83e05a7e85d5760..3beba546698bbd3feb9341cbb03f20274388f1aa 100644 --- a/barebones/barebones.c +++ b/barebones/barebones.c @@ -19,6 +19,8 @@ void SystemInit(void) while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08); // Wait till PLL is used as system clock source } +static const uint8_t array[384] = { 0xff }; + int main() { // Enable GPIOD. @@ -27,6 +29,9 @@ int main() // Push-Pull, 50MHz Output GPIOD->CFGLR = (GPIOD->CFGLR & 0xfffffff0) | 3; + + GPIOD->BSHR = array[GPIOD->BSHR]; + while(1) { GPIOD->BSHR = 1; // Turn on GPIOD0 diff --git a/minichlink/wch_link_base.h b/minichlink/wch_link_base.h index c35cdfb7dda8333508c39dcc97eea39a360d94bc..45c288dfbe1b4d765567846e4646344bc6c64042 100644 --- a/minichlink/wch_link_base.h +++ b/minichlink/wch_link_base.h @@ -5,7 +5,7 @@ #include "libusb.h" -#define WCHTIMEOUT 1000000 +#define WCHTIMEOUT 1000 #define WCHCHECK(x) if( status = x ) { fprintf( stderr, "Bad USB Operation on line %d (%d)\n", __LINE__, status ); exit( status ); } static inline libusb_device_handle * wch_link_base_setup() diff --git a/minichlink/wch_power.c b/minichlink/wch_power.c new file mode 100644 index 0000000000000000000000000000000000000000..5cc3d9e5bb74cfb5d42dd66f6c3b3d39564ba1ff --- /dev/null +++ b/minichlink/wch_power.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include "wch_link_base.h" + +int main() +{ + // Note this is for CH32V003, MCU UID 20-9e-ab-cd-88-b3-bc-84 + +return 0; + + libusb_device_handle * devh = wch_link_base_setup(); + int transferred; + int status; +"\x81\x0d\x01\x01" +"\x81\x0c\x02\x09\x01" + +// Disable 3.3: +"\x81\x0d\x01\x0a" +// Enable 3.3: +"\x81\x0d\x01\x09" +// Disable 5v: +"\x81\x0d\x01\x0c" +// Enable 5v: +"\x81\x0d\x01\x0b" + +// Or so the software says... In reality it doesn't follow those rules. + +// also for disabling read protection: +"\x81\x0c\x02\x09\x01" +"\x81\x0d\x01\x02" +"\x81\x06\x08\x02\xf7\xff\xff\xff\xff\xff\xff" +"\x81\x0b\x01\x01" +"\x81\x0d\x01\xff" +"\x81\x0d\x01\x01" +"\x81\x0c\x02\x09\x01" +"\x81\x0d\x01\x02" +"\x81\x06\x01\x01" +"\x81\x0d\x01\xff" + +// to enable read protection +"\x81\x0d\x01\x01" +"\x81\x0c\x02\x09\x01" +"\x81\x0d\x01\x02" +"\x81\x06\x08\x03\xf7\xff\xff\xff\xff\xff\xff" +"\x81\x0b\x01\x01" +"\x81\x0d\x01\xff" +"\x81\x0d\x01\x01" +"\x81\x0c\x02\x09\x01" +"\x81\x0d\x01\x02" +"\x81\x06\x01\x01" +"\x81\x0d\x01\xff" + +} diff --git a/minichlink/wch_query.c b/minichlink/wch_query.c new file mode 100644 index 0000000000000000000000000000000000000000..fdb9e53e9fb9c322f603dad64cfc3692d2fbd283 --- /dev/null +++ b/minichlink/wch_query.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include "wch_link_base.h" + +// TODO Make me actually query data! + +int main() +{ + + libusb_device_handle * devh = wch_link_base_setup(); + int transferred; + int status; + char rbuff[1024]; + libusb_bulk_transfer( devh, 0x81, rbuff, 1024, &transferred, 1 ); // Clear out any pending transfers. + + WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x0d\x01\x01", 4, &transferred, WCHTIMEOUT ) ); // Checkme with BULK_IN + WCHCHECK( libusb_bulk_transfer( devh, 0x81, rbuff, 1024, &transferred, 500 ) ); // Ignore respone. +// "\x1b\x00\x20\xda\x62\x67\x86\xe0\xff\xff\x00\x00\x00\x00\x09\x00" \ +"\x01\x02\x00\x2f\x00\x81\x03\x07\x00\x00\x00\x82\x0d\x04\x02\x08" \ +"\x02\x00" << Back in + + WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x0c\x02\x09\x01", 5, &transferred, WCHTIMEOUT ) ); // Checkme with BULK_IN + WCHCHECK( libusb_bulk_transfer( devh, 0x81, rbuff, 1024, &transferred, 500 ) ); // Ignore respone. +//"\x82\x0c\x01\x01" << Back In + + + WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x0d\x01\x02", 4, &transferred, WCHTIMEOUT) ); // Checkme with BULK_IN + WCHCHECK( libusb_bulk_transfer( devh, 0x81, rbuff, 1024, &transferred, 500 ) ); // Ignore respone. +//"\x82\x0d\x05\x09\x00\x30\x05\x00" << back in. + + + WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x06\x01\x01", 4, &transferred, WCHTIMEOUT) ); + WCHCHECK( libusb_bulk_transfer( devh, 0x81, rbuff, 1024, &transferred, 500 ) ); // Ignore respone. +//"\x82\x06\x01\x02" << back in + + WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x11\x01\x09", 4, &transferred, WCHTIMEOUT) ); // Checkme with BULK_IN + WCHCHECK( libusb_bulk_transfer( devh, 0x81, rbuff, 1024, &transferred, WCHTIMEOUT) ); // Checkme with BULK_IN + printf( "Query data: %d\n", transferred ); + int i; + for( i = 0; i < transferred; i++ ) + { + printf( "%02x ", (unsigned char)rbuff[i] ); + } + printf( "\n" ); + +// Note this is for CH32V003, MCU UID 20-9e-ab-cd-88-b3-bc-84 +//Respone is: +//"\xff\xff\x00\x10\x20\x9e\xab\xcd\x88\xb3\xbc\x48\xff\xff\xff\xff" \ + +//"\x00\x30\x05\x00" << Back in. <<<< QUERY DATA. + WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x0d\x01\x04", 4, &transferred, WCHTIMEOUT) ); // Odd that it did it twice - maybe something took too long on the proc. + WCHCHECK( libusb_bulk_transfer( devh, 0x81, rbuff, 1024, &transferred, 500 ) ); // Ignore respone. +//"\x82\x0d\x01\x03" <<< Streamed back in. + WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x0d\x01\xff", 4, &transferred, WCHTIMEOUT) ); // Odd that it did it twice - maybe something took too long on the proc. + WCHCHECK( libusb_bulk_transfer( devh, 0x81, rbuff, 1024, &transferred, 500 ) ); // Ignore respone. +//"\x82\x0d\x01\xff" <<< Streamed back in. +} diff --git a/minichlink/wch_write_demo.c b/minichlink/wch_write_demo.c index 12ee42190ff9c236cb7b02f2e75de52be8e0028d..e93c266c428ac3f57f57c73199c79aa1f28f9f87 100644 --- a/minichlink/wch_write_demo.c +++ b/minichlink/wch_write_demo.c @@ -15,10 +15,13 @@ int main() WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x11\x01\x09", 4, &transferred, WCHTIMEOUT) ); // Checkme with BULK_IN WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x06\x01\x01", 4, &transferred, WCHTIMEOUT) ); WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x06\x01\x01", 4, &transferred, WCHTIMEOUT) ); // Odd that it did it twice - maybe something took too long on the proc. + + // This contains the write data quantity, in bytes. (The last 2 octets) + // Then it just rollllls on in. WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x01\x08\x08\x00\x00\x00\x00\x00\x02\x0c", 11, &transferred, WCHTIMEOUT) ); // Still check me. WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x81\x02\x01\x05", 4, &transferred, WCHTIMEOUT) ); // Last checkme til data. - // then just stream this. + // then just stream this. This is some sort of propreitary image. WCHCHECK( libusb_bulk_transfer( devh, 0x01, "\x21\x11\x22\xca\x26\xc8\x93\x77\x15\x00\x99\xcf\xb7\x06\x67\x45" \ "\xb7\x27\x02\x40\x93\x86\x36\x12\x37\x97\xef\xcd\xd4\xc3\x13\x07" \