Ika Shouyu Poppoyaki - LPC82x supported

Dependencies:   MODSERIAL mbed

Fork of ika_shouyu_poppoyaki by Tedd OKANO

Committer:
okano
Date:
Fri Sep 13 03:51:14 2013 +0000
Revision:
25:33cb5ad8ae24
Parent:
24:9830b4f1207b
Child:
26:a63e73885b21
dividing 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 25:33cb5ad8ae24 32 #include "_user_settings.h"
okano 21:e149d0bdbf4a 33 #include "ika.h"
okano 21:e149d0bdbf4a 34
okano 0:6baefda2e511 35
okano 2:8d75eb0ecd20 36 BusOut leds( LED4, LED3, LED2, LED1 );
okano 2:8d75eb0ecd20 37 LocalFileSystem local( "local" );
okano 16:cac2348cfcfb 38 Ticker success;
okano 0:6baefda2e511 39
okano 19:7a7381e78025 40
okano 19:7a7381e78025 41
okano 8:b220fadbb3d8 42 int error_state = 0;
okano 7:815366f003ee 43
okano 20:98d7b5878e3e 44 int post_writing_process( target_param *tpp );
okano 20:98d7b5878e3e 45
okano 7:815366f003ee 46 int file_size( FILE *fp );
okano 7:815366f003ee 47 char read_byte( void );
okano 7:815366f003ee 48 void erase_sectors( int last_sector );
okano 21:e149d0bdbf4a 49
okano 16:cac2348cfcfb 50 void success_indicator();
okano 7:815366f003ee 51
okano 19:7a7381e78025 52
okano 12:5a33b5d39792 53 #pragma diag_suppress 1293 // surpressing a warning message of "assignment in condition" ;)
okano 12:5a33b5d39792 54
okano 7:815366f003ee 55 int main()
okano 7:815366f003ee 56 {
okano 7:815366f003ee 57 FILE *fp;
okano 20:98d7b5878e3e 58 target_param *tpp;
okano 7:815366f003ee 59 int data_size;
okano 7:815366f003ee 60 int last_sector;
okano 8:b220fadbb3d8 61
okano 7:815366f003ee 62 printf( "\r\n\r\n\r\nmbed ISP program : programming LPC device from mbed\r\n" );
okano 7:815366f003ee 63
okano 20:98d7b5878e3e 64 if ( NULL == (tpp = open_target( ISP_BAUD_RATE )) )
okano 20:98d7b5878e3e 65 {
okano 20:98d7b5878e3e 66 error( "couldn't open the taget" );
okano 20:98d7b5878e3e 67 return ( 1 );
okano 20:98d7b5878e3e 68 }
okano 8:b220fadbb3d8 69
okano 8:b220fadbb3d8 70 printf( " target device found : type = \"%s\"\r\n", tpp->type_name );
okano 8:b220fadbb3d8 71 printf( " ID = 0x%08X\r\n", tpp->id );
okano 8:b220fadbb3d8 72 printf( " RAM size = %10d bytes\r\n", tpp->ram_size );
okano 8:b220fadbb3d8 73 printf( " flash size = %10d bytes\r\n", tpp->flash_size );
okano 8:b220fadbb3d8 74
okano 12:5a33b5d39792 75 printf( " opening file: \"%s\"\r\n", SOURCE_FILE );
okano 12:5a33b5d39792 76
okano 12:5a33b5d39792 77 if ( NULL == (fp = fopen( SOURCE_FILE, "rb" )) ) {
okano 12:5a33b5d39792 78 error( "couldn't open source file" );
okano 12:5a33b5d39792 79 return ( 1 );
okano 12:5a33b5d39792 80 }
okano 12:5a33b5d39792 81
okano 12:5a33b5d39792 82 data_size = file_size( fp );
okano 12:5a33b5d39792 83 last_sector = data_size / tpp->sector_size;
okano 12:5a33b5d39792 84
okano 12:5a33b5d39792 85 printf( " data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 );
okano 12:5a33b5d39792 86 printf( " resetting target\r\n" );
okano 12:5a33b5d39792 87
okano 7:815366f003ee 88 erase_sectors( last_sector );
okano 12:5a33b5d39792 89
okano 20:98d7b5878e3e 90 write_flash( fp, tpp );
okano 20:98d7b5878e3e 91 verify_flash( fp, tpp );
okano 12:5a33b5d39792 92
okano 7:815366f003ee 93 fclose( fp );
okano 8:b220fadbb3d8 94
okano 8:b220fadbb3d8 95 printf( "\r\n %s\r\n\r\n",
okano 8:b220fadbb3d8 96 error_state ?
okano 8:b220fadbb3d8 97 "** The data could not be written :(" :
okano 8:b220fadbb3d8 98 "** The data has been written successflly :)"
okano 8:b220fadbb3d8 99 );
okano 19:7a7381e78025 100
okano 19:7a7381e78025 101 if ( error_state )
okano 19:7a7381e78025 102 error( " ** ISP failed\r\n" );
okano 20:98d7b5878e3e 103
okano 20:98d7b5878e3e 104 post_writing_process( tpp );
okano 20:98d7b5878e3e 105
okano 8:b220fadbb3d8 106
okano 16:cac2348cfcfb 107 #define AUTO_PROGRAM_START
okano 14:a7b9f74fb856 108 #ifdef AUTO_PROGRAM_START
okano 21:e149d0bdbf4a 109 set_target_baud_rate( TARGET_OPERATION_BAUD_RATE );
okano 16:cac2348cfcfb 110
okano 14:a7b9f74fb856 111 reset_target( NO_ISP_MODE );
okano 16:cac2348cfcfb 112 printf( " ** The program in flash has been started!!\r\n" );
okano 14:a7b9f74fb856 113 #endif
okano 14:a7b9f74fb856 114
okano 16:cac2348cfcfb 115 printf( " (now the mbed is working in \"serial through mode\")\r\n\r\n" );
okano 16:cac2348cfcfb 116
okano 16:cac2348cfcfb 117 success.attach( &success_indicator, 0.1 );
okano 16:cac2348cfcfb 118
okano 21:e149d0bdbf4a 119 usb_serial_bridge_operation(); // doesn't return. infinite loop in this function
okano 20:98d7b5878e3e 120 }
okano 16:cac2348cfcfb 121
okano 20:98d7b5878e3e 122
okano 22:bd98a782fba6 123 int post_writing_process( target_param *tpp )
okano 22:bd98a782fba6 124 {
okano 22:bd98a782fba6 125 if ( tpp->write_type == UUENCODE )
okano 22:bd98a782fba6 126 try_and_check( "G 0 T\r\n", "0", 0 );
okano 22:bd98a782fba6 127
okano 22:bd98a782fba6 128 }
okano 22:bd98a782fba6 129
okano 22:bd98a782fba6 130
okano 22:bd98a782fba6 131 int file_size( FILE *fp )
okano 22:bd98a782fba6 132 {
okano 22:bd98a782fba6 133 int size;
okano 22:bd98a782fba6 134
okano 22:bd98a782fba6 135 fseek( fp, 0, SEEK_END ); // seek to end of file
okano 22:bd98a782fba6 136 size = ftell( fp ); // get current file pointer
okano 22:bd98a782fba6 137 fseek( fp, 0, SEEK_SET ); // seek back to beginning of file
okano 22:bd98a782fba6 138
okano 22:bd98a782fba6 139 return size;
okano 22:bd98a782fba6 140 }
okano 22:bd98a782fba6 141
okano 22:bd98a782fba6 142
okano 7:815366f003ee 143
okano 7:815366f003ee 144
okano 7:815366f003ee 145 void erase_sectors( int last_sector )
okano 7:815366f003ee 146 {
okano 7:815366f003ee 147 char command_str[ STR_BUFF_SIZE ];
okano 8:b220fadbb3d8 148
okano 7:815366f003ee 149 sprintf( command_str, "P 0 %d\r\n", last_sector );
okano 7:815366f003ee 150 try_and_check( command_str, "0", 0 );
okano 8:b220fadbb3d8 151
okano 7:815366f003ee 152 *(command_str) = 'E';
okano 7:815366f003ee 153 try_and_check( command_str, "0", 0 );
okano 7:815366f003ee 154 }
okano 7:815366f003ee 155
okano 19:7a7381e78025 156
okano 20:98d7b5878e3e 157
okano 20:98d7b5878e3e 158
okano 4:55f1977bd11a 159
okano 0:6baefda2e511 160
okano 19:7a7381e78025 161
okano 16:cac2348cfcfb 162 void success_indicator()
okano 16:cac2348cfcfb 163 {
okano 16:cac2348cfcfb 164 static int i = 0;
okano 16:cac2348cfcfb 165
okano 16:cac2348cfcfb 166 leds = 0x1 << (i++ & 0x3);
okano 16:cac2348cfcfb 167 }
okano 21:e149d0bdbf4a 168
okano 21:e149d0bdbf4a 169
okano 21:e149d0bdbf4a 170 void set_leds( char v )
okano 21:e149d0bdbf4a 171 {
okano 21:e149d0bdbf4a 172 leds = v;
okano 21:e149d0bdbf4a 173 }