Ika Shouyu Poppoyaki - LPC82x supported

Dependencies:   MODSERIAL mbed

Fork of ika_shouyu_poppoyaki by Tedd OKANO

Committer:
okano
Date:
Fri Sep 13 03:49:11 2013 +0000
Revision:
24:9830b4f1207b
Parent:
23:017f306cf3ca
Child:
25:33cb5ad8ae24
deviding code into modules

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 11:8dfc3217d1ca 1 /**
okano 11:8dfc3217d1ca 2 * Sample of ISP operation for NXP MCUs
okano 11:8dfc3217d1ca 3 *
okano 14:a7b9f74fb856 4 * @author Tedd OKANO
okano 19:7a7381e78025 5 * @version 0.8
okano 18:b401da200216 6 * @date Sep-2013
okano 14:a7b9f74fb856 7 *
okano 14:a7b9f74fb856 8 * This program programs MCU flash memory through UART. It uses
okano 14:a7b9f74fb856 9 * "In-System Programming (ISP)" interface in target MCU (NXP LPC micro-
okano 14:a7b9f74fb856 10 * controllers).
okano 14:a7b9f74fb856 11 *
okano 14:a7b9f74fb856 12 * The ISP is done by PC and serial cable normally. The ISP protocol is
okano 14:a7b9f74fb856 13 * executed software on a PC. The software reads a data file and transfers
okano 14:a7b9f74fb856 14 * the data with the ISP protocol.
okano 14:a7b9f74fb856 15 * This program does same process of that. The mbed perform the function like
okano 14:a7b9f74fb856 16 * "FlashMagic" and "lpc21isp".
okano 14:a7b9f74fb856 17 * (This program not just copies the binary but also insert 4 byte checksum at
okano 14:a7b9f74fb856 18 * address 0x1C.)
okano 14:a7b9f74fb856 19 *
okano 14:a7b9f74fb856 20 * This program currently supports LPC1114(LPC1114FN28/102 - DIP28-ARM) and
okano 14:a7b9f74fb856 21 * LPC810(LPC810M021FN8 - DIP8-ARM).
okano 11:8dfc3217d1ca 22 */
okano 11:8dfc3217d1ca 23
okano 5:ff30f5b58617 24 #include "mbed.h"
okano 5:ff30f5b58617 25 #include "target_table.h"
okano 22:bd98a782fba6 26 #include "serial_utilities.h"
okano 22:bd98a782fba6 27 #include "command_interface.h"
okano 22:bd98a782fba6 28 #include "writing.h"
okano 22:bd98a782fba6 29 #include "uu_coding.h"
okano 23:017f306cf3ca 30 #include "target_handling.h"
okano 24:9830b4f1207b 31 #include "verification.h"
okano 21:e149d0bdbf4a 32 #include "ika.h"
okano 21:e149d0bdbf4a 33
okano 0:6baefda2e511 34
okano 2:8d75eb0ecd20 35 BusOut leds( LED4, LED3, LED2, LED1 );
okano 2:8d75eb0ecd20 36 LocalFileSystem local( "local" );
okano 16:cac2348cfcfb 37 Ticker success;
okano 0:6baefda2e511 38
okano 19:7a7381e78025 39
okano 19:7a7381e78025 40
okano 19:7a7381e78025 41
okano 19:7a7381e78025 42
okano 1:54e619428ae6 43 #define SOURCE_FILE "/local/bin"
okano 18:b401da200216 44
okano 18:b401da200216 45 // "ISP_BAUD_RATE" is baud rate for ISP operation
okano 18:b401da200216 46
okano 16:cac2348cfcfb 47 #define ISP_BAUD_RATE 115200
okano 16:cac2348cfcfb 48 //#define ISP_BAUD_RATE 57600
okano 16:cac2348cfcfb 49 //#define ISP_BAUD_RATE 9600
okano 16:cac2348cfcfb 50
okano 18:b401da200216 51 // "TARGET_OPERATION_BAUD_RATE" is baud rate for USB-serial bridge operation after
okano 18:b401da200216 52 // ISP completion.
okano 18:b401da200216 53 // if the target application uses serial(UART) and you use the bridge feature,
okano 18:b401da200216 54 // please set this value correctly.
okano 18:b401da200216 55
okano 16:cac2348cfcfb 56 #define TARGET_OPERATION_BAUD_RATE 9600
okano 4:55f1977bd11a 57
okano 8:b220fadbb3d8 58 int error_state = 0;
okano 7:815366f003ee 59
okano 20:98d7b5878e3e 60 int post_writing_process( target_param *tpp );
okano 20:98d7b5878e3e 61
okano 7:815366f003ee 62 int file_size( FILE *fp );
okano 7:815366f003ee 63 char read_byte( void );
okano 7:815366f003ee 64 void erase_sectors( int last_sector );
okano 21:e149d0bdbf4a 65
okano 16:cac2348cfcfb 66 void success_indicator();
okano 7:815366f003ee 67
okano 19:7a7381e78025 68
okano 12:5a33b5d39792 69 #pragma diag_suppress 1293 // surpressing a warning message of "assignment in condition" ;)
okano 12:5a33b5d39792 70
okano 7:815366f003ee 71 int main()
okano 7:815366f003ee 72 {
okano 7:815366f003ee 73 FILE *fp;
okano 20:98d7b5878e3e 74 target_param *tpp;
okano 7:815366f003ee 75 int data_size;
okano 7:815366f003ee 76 int last_sector;
okano 8:b220fadbb3d8 77
okano 7:815366f003ee 78 printf( "\r\n\r\n\r\nmbed ISP program : programming LPC device from mbed\r\n" );
okano 7:815366f003ee 79
okano 20:98d7b5878e3e 80 if ( NULL == (tpp = open_target( ISP_BAUD_RATE )) )
okano 20:98d7b5878e3e 81 {
okano 20:98d7b5878e3e 82 error( "couldn't open the taget" );
okano 20:98d7b5878e3e 83 return ( 1 );
okano 20:98d7b5878e3e 84 }
okano 8:b220fadbb3d8 85
okano 8:b220fadbb3d8 86 printf( " target device found : type = \"%s\"\r\n", tpp->type_name );
okano 8:b220fadbb3d8 87 printf( " ID = 0x%08X\r\n", tpp->id );
okano 8:b220fadbb3d8 88 printf( " RAM size = %10d bytes\r\n", tpp->ram_size );
okano 8:b220fadbb3d8 89 printf( " flash size = %10d bytes\r\n", tpp->flash_size );
okano 8:b220fadbb3d8 90
okano 12:5a33b5d39792 91 printf( " opening file: \"%s\"\r\n", SOURCE_FILE );
okano 12:5a33b5d39792 92
okano 12:5a33b5d39792 93 if ( NULL == (fp = fopen( SOURCE_FILE, "rb" )) ) {
okano 12:5a33b5d39792 94 error( "couldn't open source file" );
okano 12:5a33b5d39792 95 return ( 1 );
okano 12:5a33b5d39792 96 }
okano 12:5a33b5d39792 97
okano 12:5a33b5d39792 98 data_size = file_size( fp );
okano 12:5a33b5d39792 99 last_sector = data_size / tpp->sector_size;
okano 12:5a33b5d39792 100
okano 12:5a33b5d39792 101 printf( " data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 );
okano 12:5a33b5d39792 102 printf( " resetting target\r\n" );
okano 12:5a33b5d39792 103
okano 7:815366f003ee 104 erase_sectors( last_sector );
okano 12:5a33b5d39792 105
okano 20:98d7b5878e3e 106 write_flash( fp, tpp );
okano 20:98d7b5878e3e 107 verify_flash( fp, tpp );
okano 12:5a33b5d39792 108
okano 7:815366f003ee 109 fclose( fp );
okano 8:b220fadbb3d8 110
okano 8:b220fadbb3d8 111 printf( "\r\n %s\r\n\r\n",
okano 8:b220fadbb3d8 112 error_state ?
okano 8:b220fadbb3d8 113 "** The data could not be written :(" :
okano 8:b220fadbb3d8 114 "** The data has been written successflly :)"
okano 8:b220fadbb3d8 115 );
okano 19:7a7381e78025 116
okano 19:7a7381e78025 117 if ( error_state )
okano 19:7a7381e78025 118 error( " ** ISP failed\r\n" );
okano 20:98d7b5878e3e 119
okano 20:98d7b5878e3e 120 post_writing_process( tpp );
okano 20:98d7b5878e3e 121
okano 8:b220fadbb3d8 122
okano 16:cac2348cfcfb 123 #define AUTO_PROGRAM_START
okano 14:a7b9f74fb856 124 #ifdef AUTO_PROGRAM_START
okano 21:e149d0bdbf4a 125 set_target_baud_rate( TARGET_OPERATION_BAUD_RATE );
okano 16:cac2348cfcfb 126
okano 14:a7b9f74fb856 127 reset_target( NO_ISP_MODE );
okano 16:cac2348cfcfb 128 printf( " ** The program in flash has been started!!\r\n" );
okano 14:a7b9f74fb856 129 #endif
okano 14:a7b9f74fb856 130
okano 16:cac2348cfcfb 131 printf( " (now the mbed is working in \"serial through mode\")\r\n\r\n" );
okano 16:cac2348cfcfb 132
okano 16:cac2348cfcfb 133 success.attach( &success_indicator, 0.1 );
okano 16:cac2348cfcfb 134
okano 21:e149d0bdbf4a 135 usb_serial_bridge_operation(); // doesn't return. infinite loop in this function
okano 20:98d7b5878e3e 136 }
okano 16:cac2348cfcfb 137
okano 20:98d7b5878e3e 138
okano 22:bd98a782fba6 139 int post_writing_process( target_param *tpp )
okano 22:bd98a782fba6 140 {
okano 22:bd98a782fba6 141 if ( tpp->write_type == UUENCODE )
okano 22:bd98a782fba6 142 try_and_check( "G 0 T\r\n", "0", 0 );
okano 22:bd98a782fba6 143
okano 22:bd98a782fba6 144 }
okano 22:bd98a782fba6 145
okano 22:bd98a782fba6 146
okano 22:bd98a782fba6 147 int file_size( FILE *fp )
okano 22:bd98a782fba6 148 {
okano 22:bd98a782fba6 149 int size;
okano 22:bd98a782fba6 150
okano 22:bd98a782fba6 151 fseek( fp, 0, SEEK_END ); // seek to end of file
okano 22:bd98a782fba6 152 size = ftell( fp ); // get current file pointer
okano 22:bd98a782fba6 153 fseek( fp, 0, SEEK_SET ); // seek back to beginning of file
okano 22:bd98a782fba6 154
okano 22:bd98a782fba6 155 return size;
okano 22:bd98a782fba6 156 }
okano 22:bd98a782fba6 157
okano 22:bd98a782fba6 158
okano 7:815366f003ee 159
okano 7:815366f003ee 160
okano 7:815366f003ee 161 void erase_sectors( int last_sector )
okano 7:815366f003ee 162 {
okano 7:815366f003ee 163 char command_str[ STR_BUFF_SIZE ];
okano 8:b220fadbb3d8 164
okano 7:815366f003ee 165 sprintf( command_str, "P 0 %d\r\n", last_sector );
okano 7:815366f003ee 166 try_and_check( command_str, "0", 0 );
okano 8:b220fadbb3d8 167
okano 7:815366f003ee 168 *(command_str) = 'E';
okano 7:815366f003ee 169 try_and_check( command_str, "0", 0 );
okano 7:815366f003ee 170 }
okano 7:815366f003ee 171
okano 19:7a7381e78025 172
okano 20:98d7b5878e3e 173
okano 20:98d7b5878e3e 174
okano 4:55f1977bd11a 175
okano 0:6baefda2e511 176
okano 19:7a7381e78025 177
okano 16:cac2348cfcfb 178 void success_indicator()
okano 16:cac2348cfcfb 179 {
okano 16:cac2348cfcfb 180 static int i = 0;
okano 16:cac2348cfcfb 181
okano 16:cac2348cfcfb 182 leds = 0x1 << (i++ & 0x3);
okano 16:cac2348cfcfb 183 }
okano 21:e149d0bdbf4a 184
okano 21:e149d0bdbf4a 185
okano 21:e149d0bdbf4a 186 void set_leds( char v )
okano 21:e149d0bdbf4a 187 {
okano 21:e149d0bdbf4a 188 leds = v;
okano 21:e149d0bdbf4a 189 }