Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MODSERIAL DirectoryList
main.cpp
- Committer:
- okano
- Date:
- 2013-09-13
- Revision:
- 24:9830b4f1207b
- Parent:
- 23:017f306cf3ca
- Child:
- 25:33cb5ad8ae24
File content as of revision 24:9830b4f1207b:
/**
* Sample of ISP operation for NXP MCUs
*
* @author Tedd OKANO
* @version 0.8
* @date Sep-2013
*
* This program programs MCU flash memory through UART. It uses
* "In-System Programming (ISP)" interface in target MCU (NXP LPC micro-
* controllers).
*
* The ISP is done by PC and serial cable normally. The ISP protocol is
* executed software on a PC. The software reads a data file and transfers
* the data with the ISP protocol.
* This program does same process of that. The mbed perform the function like
* "FlashMagic" and "lpc21isp".
* (This program not just copies the binary but also insert 4 byte checksum at
* address 0x1C.)
*
* This program currently supports LPC1114(LPC1114FN28/102 - DIP28-ARM) and
* LPC810(LPC810M021FN8 - DIP8-ARM).
*/
#include "mbed.h"
#include "target_table.h"
#include "serial_utilities.h"
#include "command_interface.h"
#include "writing.h"
#include "uu_coding.h"
#include "target_handling.h"
#include "verification.h"
#include "ika.h"
BusOut leds( LED4, LED3, LED2, LED1 );
LocalFileSystem local( "local" );
Ticker success;
#define SOURCE_FILE "/local/bin"
// "ISP_BAUD_RATE" is baud rate for ISP operation
#define ISP_BAUD_RATE 115200
//#define ISP_BAUD_RATE 57600
//#define ISP_BAUD_RATE 9600
// "TARGET_OPERATION_BAUD_RATE" is baud rate for USB-serial bridge operation after
// ISP completion.
// if the target application uses serial(UART) and you use the bridge feature,
// please set this value correctly.
#define TARGET_OPERATION_BAUD_RATE 9600
int error_state = 0;
int post_writing_process( target_param *tpp );
int file_size( FILE *fp );
char read_byte( void );
void erase_sectors( int last_sector );
void success_indicator();
#pragma diag_suppress 1293 // surpressing a warning message of "assignment in condition" ;)
int main()
{
FILE *fp;
target_param *tpp;
int data_size;
int last_sector;
printf( "\r\n\r\n\r\nmbed ISP program : programming LPC device from mbed\r\n" );
if ( NULL == (tpp = open_target( ISP_BAUD_RATE )) )
{
error( "couldn't open the taget" );
return ( 1 );
}
printf( " target device found : type = \"%s\"\r\n", tpp->type_name );
printf( " ID = 0x%08X\r\n", tpp->id );
printf( " RAM size = %10d bytes\r\n", tpp->ram_size );
printf( " flash size = %10d bytes\r\n", tpp->flash_size );
printf( " opening file: \"%s\"\r\n", SOURCE_FILE );
if ( NULL == (fp = fopen( SOURCE_FILE, "rb" )) ) {
error( "couldn't open source file" );
return ( 1 );
}
data_size = file_size( fp );
last_sector = data_size / tpp->sector_size;
printf( " data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 );
printf( " resetting target\r\n" );
erase_sectors( last_sector );
write_flash( fp, tpp );
verify_flash( fp, tpp );
fclose( fp );
printf( "\r\n %s\r\n\r\n",
error_state ?
"** The data could not be written :(" :
"** The data has been written successflly :)"
);
if ( error_state )
error( " ** ISP failed\r\n" );
post_writing_process( tpp );
#define AUTO_PROGRAM_START
#ifdef AUTO_PROGRAM_START
set_target_baud_rate( TARGET_OPERATION_BAUD_RATE );
reset_target( NO_ISP_MODE );
printf( " ** The program in flash has been started!!\r\n" );
#endif
printf( " (now the mbed is working in \"serial through mode\")\r\n\r\n" );
success.attach( &success_indicator, 0.1 );
usb_serial_bridge_operation(); // doesn't return. infinite loop in this function
}
int post_writing_process( target_param *tpp )
{
if ( tpp->write_type == UUENCODE )
try_and_check( "G 0 T\r\n", "0", 0 );
}
int file_size( FILE *fp )
{
int size;
fseek( fp, 0, SEEK_END ); // seek to end of file
size = ftell( fp ); // get current file pointer
fseek( fp, 0, SEEK_SET ); // seek back to beginning of file
return size;
}
void erase_sectors( int last_sector )
{
char command_str[ STR_BUFF_SIZE ];
sprintf( command_str, "P 0 %d\r\n", last_sector );
try_and_check( command_str, "0", 0 );
*(command_str) = 'E';
try_and_check( command_str, "0", 0 );
}
void success_indicator()
{
static int i = 0;
leds = 0x1 << (i++ & 0x3);
}
void set_leds( char v )
{
leds = v;
}