Skip to content

Commit

Permalink
Added "go after flash" command
Browse files Browse the repository at this point in the history
Authos: anonymous contributor.
  • Loading branch information
bogdanm committed May 26, 2013
1 parent db70666 commit f653a38
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.spec
*.o
*.d
stm32ld

7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ STM32 Firmware Loader

These C sources can be used to burn .bin (no hex support) firmware images to STM32 microcontrollers using the built-in serial bootloader.

> Usage: stm32ld <port> <baud> <binary image name>
> Usage: stm32ld <port> <baud> <binary image name|0 not to flash> [<0|1 to send Go command to new flashed app>]

Building
--------

If lake is installed:
> lake
Otherwise:

Linux/Mac OS X:
> gcc -o stm32ld main.c stm32ld.c serial_posix.c
Expand Down
94 changes: 64 additions & 30 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "stm32ld.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

Expand Down Expand Up @@ -47,14 +48,17 @@ static void writeh_progress( u32 wrote )

int main( int argc, const char **argv )
{
u8 not_flashing=0;
u8 send_go_command=0;
u8 minor, major;
u16 version;
long baud;

// Argument validation
if( argc != 4 )
if( argc < 4 )
{
fprintf( stderr, "Usage: stm32ld <port> <baud> <binary image name>\n" );
fprintf( stderr, "Usage: stm32ld <port> <baud> <binary image name|0 to not flash> [<0|1 to send Go command to new flashed app>]\n" );
fprintf( stderr, "Note: Thanks to Go command you don't need to change status of BOOT0 after flashing,\n\t\ttake care after power cycle ...\n\n\n" );
exit( 1 );
}
errno = 0;
Expand All @@ -64,16 +68,29 @@ int main( int argc, const char **argv )
fprintf( stderr, "Invalid baud '%s'\n", argv[ 2 ] );
exit( 1 );
}
if( ( fp = fopen( argv[ 3 ], "rb" ) ) == NULL )

if( argc >= 5 && strlen(argv[ 4 ])==1 && strncmp(argv[ 4 ], "1", 1)==0 )
{
fprintf( stderr, "Unable to open %s\n", argv[ 3 ] );
exit( 1 );
send_go_command=1;
}

if( strlen(argv[ 3 ])==1 && strncmp(argv[ 3 ], "0", 1)==0 )
{
not_flashing=1;
}
else
{
fseek( fp, 0, SEEK_END );
fpsize = ftell( fp );
fseek( fp, 0, SEEK_SET );
if( ( fp = fopen( argv[ 3 ], "rb" ) ) == NULL )
{
fprintf( stderr, "Unable to open %s\n", argv[ 3 ] );
exit( 1 );
}
else
{
fseek( fp, 0, SEEK_END );
fpsize = ftell( fp );
fseek( fp, 0, SEEK_SET );
}
}

// Connect to bootloader
Expand Down Expand Up @@ -115,36 +132,53 @@ int main( int argc, const char **argv )
}
}

// Write unprotect
if( stm32_write_unprotect() != STM32_OK )
if( not_flashing == 0 )
{
fprintf( stderr, "Unable to execute write unprotect\n" );
exit( 1 );
}
else
printf( "Cleared write protection.\n" );
// Write unprotect
if( stm32_write_unprotect() != STM32_OK )
{
fprintf( stderr, "Unable to execute write unprotect\n" );
exit( 1 );
}
else
printf( "Cleared write protection.\n" );

// Erase flash
if( stm32_erase_flash() != STM32_OK )
{
fprintf( stderr, "Unable to erase chip\n" );
exit( 1 );
// Erase flash
if( stm32_erase_flash() != STM32_OK )
{
fprintf( stderr, "Unable to erase chip\n" );
exit( 1 );
}
else
printf( "Erased FLASH memory.\n" );

// Program flash
setbuf( stdout, NULL );
printf( "Programming flash ... ");
if( stm32_write_flash( writeh_read_data, writeh_progress ) != STM32_OK )
{
fprintf( stderr, "Unable to program FLASH memory.\n" );
exit( 1 );
}
else
printf( "\nDone.\n" );

fclose( fp );
}
else
printf( "Erased FLASH memory.\n" );
printf( "Skipping flashing ... \n" );

// Program flash
setbuf( stdout, NULL );
printf( "Programming flash ... ");
if( stm32_write_flash( writeh_read_data, writeh_progress ) != STM32_OK )
if( send_go_command == 1 )
{
fprintf( stderr, "Uanble to program FLASH memory.\n" );
exit( 1 );
// Run GO
printf( "Sending Go command ... \n" );
if( stm32_go_command( ) != STM32_OK )
{
fprintf( stderr, "Unable to run Go command.\n" );
exit( 1 );
}
}
else
printf( "\nDone.\n" );

fclose( fp );
return 0;
}

23 changes: 23 additions & 0 deletions stm32ld.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,26 @@ int stm32_write_flash( p_read_data read_data_func, p_progress progress_func )
return STM32_OK;
}

// "Go" command - Description in: AN3155 Application note
int stm32_go_command( void )
{
u32 address = STM32_FLASH_START_ADDRESS; // TODO: get address as optional parameter from command line

STM32_CHECK_INIT;

// Send "Go" request
stm32h_send_command( STM32_CMD_GO );
STM32_EXPECT( STM32_COMM_ACK );

// Send address to Jump & Run
stm32h_send_address( address );
STM32_EXPECT( STM32_COMM_ACK );

// Expect second ACK as acknowledge of address validation & checksum
// NOTE: second ACK should come but in real it's not coming :-( ... not required ...
//STM32_EXPECT( STM32_COMM_ACK );

return STM32_OK;
}


4 changes: 3 additions & 1 deletion stm32ld.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ enum
STM32_CMD_GET_ID = 0x02,
STM32_CMD_WRITE_FLASH = 0x31,
STM32_CMD_WRITE_UNPROTECT = 0x73,
STM32_CMD_READ_FLASH = 0x11
STM32_CMD_READ_FLASH = 0x11,
STM32_CMD_GO = 0x21
};

// Function types for stm32_write_flash
Expand All @@ -44,6 +45,7 @@ int stm32_get_chip_id( u16 *version );
int stm32_write_unprotect();
int stm32_erase_flash();
int stm32_write_flash( p_read_data read_data_func, p_progress progress_func );
int stm32_go_command( void );

#endif

0 comments on commit f653a38

Please sign in to comment.