Skip to content

Commit

Permalink
Add the ability to use a ch32v003-based programmer
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlohr committed Nov 11, 2024
1 parent 1f19b78 commit df6a5fc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
6 changes: 6 additions & 0 deletions minichlink/99-minichlink.rules
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ SUBSYSTEM=="usb", ATTRS{idVendor}=="4348", ATTRS{idProduct}=="55e0", GROUP="plug

SUBSYSTEM=="usb", ATTRS{idVendor}=="303a", ATTRS{idProduct}=="4004", GROUP="plugdev", MODE="0660"
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="303a", ATTRS{idProduct}=="4004", GROUP="plugdev", MODE="0660"

# rv003usb bootloader
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="1209", ATTRS{idProduct}=="b003", GROUP="plugdev", MODE="0660"
#KERNEL=="hiddev*", SUBSYSTEM=="usbmisc", ATTRS{idVendor}=="1209", ATTRS{idProduct}=="b003", GROUP="plugdev", MODE="0660"

# ch32v003 programming rvswdio dongle
SUBSYSTEM=="usb", ATTRS{idVendor}=="1206", ATTRS{idProduct}=="5d10", GROUP="plugdev", MODE="0660"
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="1206", ATTRS{idProduct}=="5d10", GROUP="plugdev", MODE="0660"

2 changes: 1 addition & 1 deletion minichlink/minichlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void * MiniCHLinkInitAsDLL( struct MiniChlinkFunctions ** MCFO, const init_hints
}
else if( (dev = TryInit_ESP32S2CHFUN()) )
{
fprintf( stderr, "Found ESP32S2 Programmer\n" );
fprintf( stderr, "Found ESP32S2-Style Programmer\n" );
}
else if ((dev = TryInit_NHCLink042()))
{
Expand Down
50 changes: 35 additions & 15 deletions minichlink/pgm-esp32s2-ch32xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
#include "hidapi.c"
#include "minichlink.h"

#define PROGRAMMER_TYPE_ESP32S2 0
#define PROGRAMMER_TYPE_CH32V003 1

struct ESP32ProgrammerStruct
{
void * internal;

hid_device * hd;
uint32_t state;
uint8_t commandbuffer[256];
int commandbuffersize;
int programmer_type;
int commandplace;
uint8_t reply[256];
int replysize;
int replylen;

int dev_version;
Expand All @@ -20,7 +26,7 @@ int ESPFlushLLCommands( void * dev );

static inline int SRemain( struct ESP32ProgrammerStruct * e )
{
return sizeof( e->commandbuffer ) - e->commandplace - 2; //Need room for EOF.
return e->commandbuffersize - e->commandplace; // We will need room for EOF, but we can use this to detect overflows.
}

static inline void Write4LE( struct ESP32ProgrammerStruct * e, uint32_t val )
Expand Down Expand Up @@ -55,8 +61,6 @@ static inline void Write1( struct ESP32ProgrammerStruct * e, uint8_t val )
static int ESPWriteReg32( void * dev, uint8_t reg_7_bit, uint32_t value )
{
struct ESP32ProgrammerStruct * eps = (struct ESP32ProgrammerStruct *)dev;
// printf( "WriteReg: %02x -> %08x\n", reg_7_bit, value );


if( SRemain( eps ) < 5 ) ESPFlushLLCommands( eps );

Expand All @@ -74,6 +78,7 @@ int ESPReadReg32( void * dev, uint8_t reg_7_bit, uint32_t * commandresp )
ESPFlushLLCommands( eps );

// printf( "ReadReg: %02x -> %d\n", reg_7_bit,eps->replylen );
//printf( "REG: %d O: %02x %02x %02x %02x %02x %02x\n", reg_7_bit, eps->reply[0], eps->reply[1], eps->reply[2], eps->reply[3], eps->reply[4], eps->reply[5]);

if( eps->replylen < 6 )
{
Expand All @@ -90,7 +95,7 @@ int ESPFlushLLCommands( void * dev )
{
struct ESP32ProgrammerStruct * eps = (struct ESP32ProgrammerStruct *)dev;

if( eps->commandplace >= sizeof( eps->commandbuffer ) )
if( eps->commandplace >= eps->commandbuffersize - 1 )
{
fprintf( stderr, "Error: Command buffer overflow\n" );
return -5;
Expand All @@ -113,7 +118,7 @@ int ESPFlushLLCommands( void * dev )
printf("\n" );
#endif

r = hid_send_feature_report( eps->hd, eps->commandbuffer, 255 );
r = hid_send_feature_report( eps->hd, eps->commandbuffer, eps->commandbuffersize );
eps->commandplace = 1;
if( r < 0 )
{
Expand All @@ -122,17 +127,17 @@ int ESPFlushLLCommands( void * dev )
}
retry:
eps->reply[0] = 0xad; // Key report ID
r = hid_get_feature_report( eps->hd, eps->reply, sizeof( eps->reply ) );
/*
int i;
printf( "RESP: %d\n",eps->reply[0] );
r = hid_get_feature_report( eps->hd, eps->reply, eps->replysize );
#if 0
printf( "RESP: %d %d\n", r,eps->reply[0] );

for( i = 0; i < eps->reply[0]; i++ )
for( int i = 0; i < eps->reply[0]; i++ )
{
printf( "%02x ", eps->reply[i+1] );
if( (i % 16) == 15 ) printf( "\n" );
}
printf( "\n" );*/
printf( "\n" );
#endif

if( eps->reply[0] == 0xff ) goto retry;
//printf( ">:::%d: %02x %02x %02x %02x %02x %02x\n", eps->replylen, eps->reply[0], eps->reply[1], eps->reply[2], eps->reply[3], eps->reply[4], eps->reply[5] );
Expand Down Expand Up @@ -397,14 +402,29 @@ int ESPPollTerminal( void * dev, uint8_t * buffer, int maxlen, uint32_t leavefla

void * TryInit_ESP32S2CHFUN()
{
#define VID 0x303a
#define PID 0x4004
hid_init();
hid_device * hd = hid_open( VID, PID, L"s2-ch32xx-pgm-v0"); // third parameter is "serial"
if( !hd ) return 0;

struct ESP32ProgrammerStruct * eps = malloc( sizeof( struct ESP32ProgrammerStruct ) );
memset( eps, 0, sizeof( *eps ) );
hid_device * hd = hid_open( 0x303a, 0x4004, L"s2-ch32xx-pgm-v0"); // third parameter is "serial"
if( hd )
{
eps->commandbuffersize = 255;
eps->replysize = 255;
eps->programmer_type = PROGRAMMER_TYPE_ESP32S2;
}
else if( !!( hd = hid_open( 0x1206, 0x5D10, L"RVSWDIO003-01") ) )
{
eps->commandbuffersize = 78;
eps->replysize = 78;
eps->programmer_type = PROGRAMMER_TYPE_CH32V003;
}
else
{
free( eps );
return 0;
}

eps->hd = hd;
eps->commandplace = 1;
eps->dev_version = 0;
Expand Down

0 comments on commit df6a5fc

Please sign in to comment.