Skip to content

Commit

Permalink
Ok! Fix Link-E
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlohr committed May 5, 2023
1 parent 29f1e5d commit 6f896b6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
4 changes: 1 addition & 3 deletions minichlink/microgdbstub.h
Expand Up @@ -24,8 +24,6 @@
void RVNetPoll(void * dev );
int RVSendGDBHaltReason( void * dev );
void RVNetConnect( void * dev );
int RVSendGDBHaltReason( void * dev );
void RVNetPoll(void * dev );
int RVReadCPURegister( void * dev, int regno, uint32_t * regret );
void RVDebugExec( void * dev, int halt_reset_or_resume );
int RVReadMem( void * dev, uint32_t memaddy, uint8_t * payload, int len );
Expand Down Expand Up @@ -274,7 +272,7 @@ void HandleGDBPacket( void * dev, char * data, int len )
{
// Request a list of actions supported by the ‘vCont’ packet.
// We don't support vCont
SendReplyFull( "vCont;s;c;t;" ); //no ;s maybe?
SendReplyFull( "vCont;;c;;" ); //no ;s or ;t because we don't implement them.
}
else
{
Expand Down
41 changes: 23 additions & 18 deletions minichlink/minichlink.c
Expand Up @@ -589,7 +589,7 @@ static int DefaultWaitForFlash( void * dev )
return 0;
}

static int DefaultWaitForDoneOp( void * dev )
static int DefaultWaitForDoneOp( void * dev, int ignore )
{
int r;
uint32_t rrv;
Expand All @@ -601,7 +601,7 @@ static int DefaultWaitForDoneOp( void * dev )
while( rrv & (1<<12) );
if( (rrv >> 8 ) & 7 )
{
fprintf( stderr, "Fault writing memory (DMABSTRACTS = %08x)\n", rrv );
if( !ignore ) fprintf( stderr, "Fault writing memory (DMABSTRACTS = %08x)\n", rrv );
MCF.WriteReg32( dev, DMABSTRACTCS, 0x00000700 );
return -9;
}
Expand Down Expand Up @@ -697,7 +697,7 @@ static int DefaultWriteHalfWord( void * dev, uint32_t address_to_write, uint16_t
MCF.WriteReg32( dev, DMDATA0, data );
MCF.WriteReg32( dev, DMCOMMAND, 0x00271008 ); // Copy data to x8, and execute program.

ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
iss->currentstateval = -1;


Expand All @@ -723,7 +723,7 @@ static int DefaultReadHalfWord( void * dev, uint32_t address_to_write, uint16_t
MCF.WriteReg32( dev, DMCOMMAND, 0x00241000 ); // Only execute.
MCF.WriteReg32( dev, DMCOMMAND, 0x00221008 ); // Read x8 into DATA0.

ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
iss->currentstateval = -1;

uint32_t rr;
Expand Down Expand Up @@ -753,7 +753,7 @@ static int DefaultWriteByte( void * dev, uint32_t address_to_write, uint8_t data
MCF.WriteReg32( dev, DMDATA0, data );
MCF.WriteReg32( dev, DMCOMMAND, 0x00271008 ); // Copy data to x8, and execute program.

ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
iss->currentstateval = -1;
return ret;
}
Expand All @@ -777,7 +777,7 @@ static int DefaultReadByte( void * dev, uint32_t address_to_write, uint8_t * dat
MCF.WriteReg32( dev, DMCOMMAND, 0x00241000 ); // Only execute.
MCF.WriteReg32( dev, DMCOMMAND, 0x00221008 ); // Read x8 into DATA0.

ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
iss->currentstateval = -1;

uint32_t rr;
Expand Down Expand Up @@ -850,7 +850,7 @@ static int DefaultWriteWord( void * dev, uint32_t address_to_write, uint32_t dat
iss->currentstateval = address_to_write;

if( is_flash )
ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
}
else
{
Expand All @@ -865,11 +865,11 @@ static int DefaultWriteWord( void * dev, uint32_t address_to_write, uint32_t dat
{
// XXX TODO: This likely can be a very short delay.
// XXX POSSIBLE OPTIMIZATION REINVESTIGATE.
ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
}
else
{
ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
}
}

Expand Down Expand Up @@ -1101,13 +1101,15 @@ static int DefaultReadWord( void * dev, uint32_t address_to_read, uint32_t * dat
iss->statetag = STTAG( "RDSQ" );
iss->currentstateval = address_to_read;

r |= MCF.WaitForDoneOp( dev );
r |= MCF.WaitForDoneOp( dev, 0 );
}

if( iss->autoincrement )
iss->currentstateval += 4;

r |= MCF.ReadReg32( dev, DMDATA0, data );
if( iss->currentstateval == iss->ram_base + iss->ram_size )
MCF.WaitForDoneOp( dev, 1 ); // Ignore any post-errors.
return r;
}

Expand Down Expand Up @@ -1234,6 +1236,7 @@ int DefaultReadBinaryBlob( void * dev, uint32_t address_to_read_from, uint32_t r
}
}
}
MCF.WaitForDoneOp( dev, 0 );
return 0;
}

Expand Down Expand Up @@ -1514,22 +1517,22 @@ int DefaultConfigureNRSTAsGPIO( void * dev, int one_if_yes_gpio )
}
ctlr |= CR_OPTER_Set | CR_STRT_Set; // OBER
MCF.WriteWord( dev, 0x40022010, ctlr ); // FLASH->CTLR = 0x40022010
ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
ret |= MCF.WaitForFlash( dev );

MCF.WriteHalfWord( dev, (intptr_t)&OB->RDPR, RDP_Key );

ctlr &=~CR_OPTER_Reset;
MCF.WriteWord( dev, 0x40022010, ctlr ); // FLASH->CTLR = 0x40022010
ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
ret |= MCF.WaitForFlash( dev );
ctlr |= CR_OPTPG_Set;
MCF.WriteWord( dev, 0x40022010, ctlr ); // FLASH->CTLR = 0x40022010
ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
ret |= MCF.WaitForFlash( dev );
ctlr &=~CR_OPTPG_Reset;
MCF.WriteWord( dev, 0x40022010, ctlr ); // FLASH->CTLR = 0x40022010
ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
ret |= MCF.WaitForFlash( dev );


Expand All @@ -1541,14 +1544,14 @@ int DefaultConfigureNRSTAsGPIO( void * dev, int one_if_yes_gpio )
}
ctlr |= CR_OPTPG_Set; //OBPG
MCF.WriteWord( dev, 0x40022010, ctlr ); // FLASH->CTLR = 0x40022010
ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
ret |= MCF.WaitForFlash( dev );

uint32_t config = OB_IWDG_HW | OB_STOP_NoRST | OB_STDBY_NoRST | (one_if_yes_gpio?OB_RST_NoEN:OB_RST_EN_DT1ms) | (uint16_t)0xE0;
printf( "Config (%08x): %08x\n", (intptr_t)&OB->USER, config );
MCF.WriteHalfWord( dev, (intptr_t)&OB->USER, config );

ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
ret |= MCF.WaitForFlash( dev );

ctlr &= CR_OPTPG_Reset;
Expand Down Expand Up @@ -1579,7 +1582,7 @@ int DefaultConfigureNRSTAsGPIO( void * dev, int one_if_yes_gpio )
csw |= 1<<5;//OBER;
MCF.WriteWord( dev, 0x40022010, csw ); // FLASH->CTLR = 0x40022010
MCF.WriteHalfWord( dev, 0x1FFFF802, 0xffff );
ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
ret |= MCF.WaitForFlash( dev );

MCF.ReadWord( dev, 0x40022010, &csw ); // FLASH->CTLR = 0x40022010
Expand All @@ -1604,7 +1607,7 @@ int DefaultConfigureNRSTAsGPIO( void * dev, int one_if_yes_gpio )
csw |= (one_if_yes_gpio?0b00:0b11)<<(3+8);
printf( "CSW writing: %08x\n", csw );
MCF.WriteHalfWord( dev, 0x1FFFF802, csw );
ret |= MCF.WaitForDoneOp( dev );
ret |= MCF.WaitForDoneOp( dev, 0 );
ret |= MCF.WaitForFlash( dev );
}

Expand Down Expand Up @@ -1710,6 +1713,8 @@ int SetupAutomaticHighLevelFunctions( void * dev )
MCF.VoidHighLevelState = DefaultVoidHighLevelState;

struct InternalState * iss = calloc( 1, sizeof( struct InternalState ) );
iss->ram_base = 0x20000000;
iss->ram_size = 2048;

((struct ProgrammerStructBase*)dev)->internal = iss;
return 0;
Expand Down
Binary file modified minichlink/minichlink.exe
Binary file not shown.
4 changes: 3 additions & 1 deletion minichlink/minichlink.h
Expand Up @@ -50,7 +50,7 @@ struct MiniChlinkFunctions
int (*SetEnableBreakpoints)( void * dev, int halt_on_break, int single_step );

int (*WaitForFlash)( void * dev );
int (*WaitForDoneOp)( void * dev );
int (*WaitForDoneOp)( void * dev, int ignore );

int (*PrintChipInfo)( void * dev );

Expand Down Expand Up @@ -104,6 +104,8 @@ struct InternalState
int lastwriteflags;
int processor_in_mode;
int autoincrement;
uint32_t ram_base;
uint32_t ram_size;
};


Expand Down
2 changes: 1 addition & 1 deletion minichlink/pgm-esp32s2-ch32xx.c
Expand Up @@ -206,7 +206,7 @@ static int ESPWaitForFlash( void * dev )
return 0;
}

static int ESPWaitForDoneOp( void * dev )
static int ESPWaitForDoneOp( void * dev, int ignore )
{
struct ESP32ProgrammerStruct * eps = (struct ESP32ProgrammerStruct *)dev;
if( SRemain( eps ) < 2 )
Expand Down
18 changes: 10 additions & 8 deletions minichlink/pgm-wch-linke.c
Expand Up @@ -159,6 +159,16 @@ static int LESetupInterface( void * d )
// This puts the processor on hold to allow the debugger to run.
wch_link_command( dev, "\x81\x0d\x01\x02", 4, 0, 0, 0 ); // Reply: Ignored, 820d050900300500

// For some reason, if we don't do this sometimes the programmer starts in a hosey mode.
MCF.WriteReg32( d, DMCONTROL, 0x80000001 ); // Make the debug module work properly.
MCF.WriteReg32( d, DMCONTROL, 0x80000001 ); // Initiate a halt request.
MCF.WriteReg32( d, DMCONTROL, 0x80000001 ); // No, really make sure.
MCF.WriteReg32( d, DMABSTRACTCS, 0x00000700 ); // Ignore any pending errors.
MCF.WriteReg32( d, DMPROGBUF0, 0x00100073 );
MCF.WriteReg32( d, DMABSTRACTAUTO, 0 );
MCF.WriteReg32( d, DMCOMMAND, 0x00261000 ); // Read x0

// This puts the processor on hold to allow the debugger to run.
wch_link_command( dev, "\x81\x11\x01\x09", 4, (int*)&transferred, rbuff, 1024 ); // Reply: Chip ID + Other data (see below)
if( transferred != 20 )
{
Expand All @@ -170,14 +180,6 @@ static int LESetupInterface( void * d )
fprintf( stderr, "PFlags : %02x-%02x-%02x-%02x\n", rbuff[12], rbuff[13], rbuff[14], rbuff[15] );
fprintf( stderr, "Part Type (B): %02x-%02x-%02x-%02x\n", rbuff[16], rbuff[17], rbuff[18], rbuff[19] );

int r = MCF.WaitForDoneOp( d );
if( r )
{
fprintf( stderr, "Error: WaitForDoneOp(...) failed\n" );
}

//Default behavior for other programmers is to be/stay in halt mode as much as possible.
MCF.HaltMode( d, 2 );
return 0;
}

Expand Down

6 comments on commit 6f896b6

@bmx
Copy link
Contributor

@bmx bmx commented on 6f896b6 May 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or not

this commit broke my wchlink

before:

./minichlink -r + 0x200 0x20
Found WCH LinkE
Part Type (A): 0x0506 (This is the capacity code, in KB)
Part UUID    : 30-70-05-18-00-ba-27-51
PFlags       : 23-48-76-26
Part Type (B): 64-59-18-e9
Interface Setup
00000200: 39 71 01 43 6e c6 19 a0 39 71 41 53 6a c8 66 ca 
00000210: 62 cc 5e ce 19 a0 39 71 01 53 5a d0 56 d2 52 d4 

after:

./minichlink -r + 0x200 0x20
Found WCH LinkE
Part Type (A): 0x0617 (This is the capacity code, in KB)
Part UUID    : 00-26-10-00-00-ba-27-51
PFlags       : 23-48-76-26
Part Type (B): 64-59-18-e9
Interface Setup
00000200: 00 00 05 00 00 00 05 00 00 00 05 00 00 00 05 00 
00000210: 00 00 05 00 00 00 05 00 00 00 05 00 00 00 05 00 

target: ch32v307v-r1-1v0

@cnlohr
Copy link
Owner Author

@cnlohr cnlohr commented on 6f896b6 May 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooohhhhhhhhh cruudddd

@cnlohr
Copy link
Owner Author

@cnlohr cnlohr commented on 6f896b6 May 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmx are you on Discord?

@cnlohr
Copy link
Owner Author

@cnlohr cnlohr commented on 6f896b6 May 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to interactively debug this.

@cnlohr
Copy link
Owner Author

@cnlohr cnlohr commented on 6f896b6 May 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracking #72 I think I fixed it in 249f5d2

@bmx
Copy link
Contributor

@bmx bmx commented on 6f896b6 May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found the issue.
ReadBinaryBlob is not meant to read the proprietary blob but read many bytes on the target.
In this commit, ReadBinaryBlob has been put into graveyard, and then using the DefaultRead, which is failing (and not efficient)

Please sign in to comment.