Skip to content

Commit

Permalink
Super unbrick mode now unlocked. It just continuously tries to unbrick.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlohr committed Nov 16, 2024
1 parent 5ffe1fa commit 497b7fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
43 changes: 23 additions & 20 deletions minichlink/minichlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -2297,11 +2297,11 @@ int DefaultUnbrick( void * dev )
MCF.DelayUS( dev, 60000 );
MCF.DelayUS( dev, 60000 );
MCF.Control3v3( dev, 1 );
MCF.DelayUS( dev, 100 );
MCF.FlushLLCommands( dev );
printf( "Connection starting\n" );
MCF.FlushLLCommands( dev );

int timeout = 0;
int max_timeout = 500;
int max_timeout = 50000; // An absurdly long time.
uint32_t ds = 0;
for( timeout = 0; timeout < max_timeout; timeout++ )
{
Expand All @@ -2323,22 +2323,30 @@ int DefaultUnbrick( void * dev )
if( ds != 0xffffffff && ds != 0x00000000 ) break;
}

// Make sure we are in halt.
MCF.WriteReg32( dev, DMCONTROL, 0x80000001 ); // Make the debug module work properly.
MCF.WriteReg32( dev, DMCONTROL, 0x80000001 ); // Initiate a halt request.
MCF.WriteReg32( dev, DMCONTROL, 0x80000001 ); // No, really make sure.
MCF.WriteReg32( dev, DMCONTROL, 0x80000001 );
if( timeout == max_timeout )
{
fprintf( stderr, "Timed out trying to unbrick\n" );
return -5;
}

int i;
for( i = 0; i < 10; i++ )
{
// Make sure we are in halt.
MCF.WriteReg32( dev, DMCONTROL, 0x80000001 ); // Make the debug module work properly.
MCF.WriteReg32( dev, DMCONTROL, 0x80000001 ); // Initiate a halt request.
MCF.WriteReg32( dev, DMCONTROL, 0x80000001 ); // No, really make sure.
MCF.WriteReg32( dev, DMCONTROL, 0x80000001 );

// After more experimentation, it appaers to work best by not clearing the halt request.
MCF.FlushLLCommands( dev );
}

MCF.WriteReg32( dev, DMABSTRACTCS, 0x00000700 ); // Clear out possible abstractcs errors.

int r = MCF.ReadReg32( dev, DMSTATUS, &ds );
printf( "DMStatus After Halt: /%d/%08x\n", r, ds );

// Many times we would clear the halt request, but in this case, we want to just leave it here, to prevent it from booting.
// TODO: Experiment and see if this is needed/wanted in cases. NOTE: If you don't clear halt request, progarmmers can get stuck.
// MCF.WriteReg32( dev, DMCONTROL, 0x00000001 ); // Clear Halt Request.

// After more experimentation, it appaers to work best by not clearing the halt request.
MCF.FlushLLCommands( dev );

// Override all option bytes and reset to factory settings, unlocking all flash sections.
uint8_t option_data[] = { 0xa5, 0x5a, 0x97, 0x68, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00 };
if( MCF.WriteBinaryBlob != DefaultWriteBinaryBlob )
Expand All @@ -2349,11 +2357,6 @@ int DefaultUnbrick( void * dev )

MCF.DelayUS( dev, 20000 );

if( timeout == max_timeout )
{
fprintf( stderr, "Timed out trying to unbrick\n" );
return -5;
}
MCF.Erase( dev, 0, 0, 1);
MCF.FlushLLCommands( dev );
return -5;
Expand Down
Binary file modified minichlink/minichlink.exe
Binary file not shown.
6 changes: 4 additions & 2 deletions minichlink/pgm-wch-linke.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,8 @@ static int LEControl5v( void * d, int bOn )
return 0;
}

// Official unbrick unreliable on x-series devices.
/*
static int LEUnbrick( void * d )
{
printf( "Sending unbrick\n" );
Expand All @@ -526,7 +528,7 @@ static int LEUnbrick( void * d )
printf( "Done unbrick\n" );
return 0;
}

*/

static int LEConfigureNRSTAsGPIO( void * d, int one_if_yes_gpio )
{
Expand Down Expand Up @@ -584,7 +586,7 @@ void * TryInit_WCHLinkE()
MCF.SetupInterface = LESetupInterface;
MCF.Control3v3 = LEControl3v3;
MCF.Control5v = LEControl5v;
MCF.Unbrick = LEUnbrick;
//MCF.Unbrick = LEUnbrick; //
MCF.ConfigureNRSTAsGPIO = LEConfigureNRSTAsGPIO;
MCF.ConfigureReadProtection = LEConfigureReadProtection;

Expand Down

0 comments on commit 497b7fc

Please sign in to comment.