Add to 11U68 11E68
Dependencies: DirectoryList MODSERIAL mbed
Fork of ika_shouyu_poppoyaki by
main.cpp@28:689c3880e0e4, 2013-09-20 (annotated)
- Committer:
- okano
- Date:
- Fri Sep 20 00:40:08 2013 +0000
- Revision:
- 28:689c3880e0e4
- Parent:
- 27:2b5c1eb39bb5
- Child:
- 29:96e28bc1bd99
made function returns error. LED1 and LED2 assigned to toggle by TX and RX
Who changed what in which revision?
User | Revision | Line number | New 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 | 26:a63e73885b21 | 5 | * @version 0.9 |
okano | 18:b401da200216 | 6 | * @date Sep-2013 |
okano | 26:a63e73885b21 | 7 | * |
okano | 26:a63e73885b21 | 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 | 26:a63e73885b21 | 11 | * |
okano | 26:a63e73885b21 | 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 | 26:a63e73885b21 | 14 | * the data with the ISP protocol. |
okano | 26:a63e73885b21 | 15 | * This program does same process of that. The mbed perform the function like |
okano | 14:a7b9f74fb856 | 16 | * "FlashMagic" and "lpc21isp". |
okano | 26:a63e73885b21 | 17 | * (This program not just copies the binary but also insert 4 byte checksum at |
okano | 14:a7b9f74fb856 | 18 | * address 0x1C.) |
okano | 26:a63e73885b21 | 19 | * |
okano | 26:a63e73885b21 | 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 | 28:689c3880e0e4 | 34 | #include "error_code.h" |
okano | 21:e149d0bdbf4a | 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 | 8:b220fadbb3d8 | 40 | int error_state = 0; |
okano | 7:815366f003ee | 41 | |
okano | 28:689c3880e0e4 | 42 | int isp_flash_write( char *file_name ); |
okano | 7:815366f003ee | 43 | int file_size( FILE *fp ); |
okano | 7:815366f003ee | 44 | char read_byte( void ); |
okano | 16:cac2348cfcfb | 45 | void success_indicator(); |
okano | 7:815366f003ee | 46 | |
okano | 19:7a7381e78025 | 47 | |
okano | 7:815366f003ee | 48 | int main() |
okano | 7:815366f003ee | 49 | { |
okano | 28:689c3880e0e4 | 50 | int err; |
okano | 26:a63e73885b21 | 51 | |
okano | 28:689c3880e0e4 | 52 | err = isp_flash_write( SOURCE_FILE ); |
okano | 28:689c3880e0e4 | 53 | |
okano | 8:b220fadbb3d8 | 54 | printf( "\r\n %s\r\n\r\n", |
okano | 28:689c3880e0e4 | 55 | err ? |
okano | 8:b220fadbb3d8 | 56 | "** The data could not be written :(" : |
okano | 8:b220fadbb3d8 | 57 | "** The data has been written successflly :)" |
okano | 8:b220fadbb3d8 | 58 | ); |
okano | 26:a63e73885b21 | 59 | |
okano | 28:689c3880e0e4 | 60 | if ( err ) |
okano | 19:7a7381e78025 | 61 | error( " ** ISP failed\r\n" ); |
okano | 26:a63e73885b21 | 62 | |
okano | 14:a7b9f74fb856 | 63 | #ifdef AUTO_PROGRAM_START |
okano | 21:e149d0bdbf4a | 64 | set_target_baud_rate( TARGET_OPERATION_BAUD_RATE ); |
okano | 16:cac2348cfcfb | 65 | |
okano | 14:a7b9f74fb856 | 66 | reset_target( NO_ISP_MODE ); |
okano | 16:cac2348cfcfb | 67 | printf( " ** The program in flash has been started!!\r\n" ); |
okano | 14:a7b9f74fb856 | 68 | #endif |
okano | 14:a7b9f74fb856 | 69 | |
okano | 16:cac2348cfcfb | 70 | printf( " (now the mbed is working in \"serial through mode\")\r\n\r\n" ); |
okano | 16:cac2348cfcfb | 71 | |
okano | 16:cac2348cfcfb | 72 | success.attach( &success_indicator, 0.1 ); |
okano | 16:cac2348cfcfb | 73 | |
okano | 21:e149d0bdbf4a | 74 | usb_serial_bridge_operation(); // doesn't return. infinite loop in this function |
okano | 20:98d7b5878e3e | 75 | } |
okano | 16:cac2348cfcfb | 76 | |
okano | 20:98d7b5878e3e | 77 | |
okano | 28:689c3880e0e4 | 78 | int isp_flash_write( char *file_name ) |
okano | 22:bd98a782fba6 | 79 | { |
okano | 28:689c3880e0e4 | 80 | FILE *fp; |
okano | 28:689c3880e0e4 | 81 | target_param *tpp; |
okano | 28:689c3880e0e4 | 82 | int data_size; |
okano | 28:689c3880e0e4 | 83 | int last_sector; |
okano | 28:689c3880e0e4 | 84 | int err; |
okano | 28:689c3880e0e4 | 85 | |
okano | 28:689c3880e0e4 | 86 | if ( NULL == (tpp = open_target( ISP_BAUD_RATE )) ) { |
okano | 28:689c3880e0e4 | 87 | return ( ERROR_AT_TARGET_OPEN ); |
okano | 28:689c3880e0e4 | 88 | } |
okano | 28:689c3880e0e4 | 89 | |
okano | 28:689c3880e0e4 | 90 | printf( " target device found : type = \"%s\"\r\n", tpp->type_name ); |
okano | 28:689c3880e0e4 | 91 | printf( " ID = 0x%08X\r\n", tpp->id ); |
okano | 28:689c3880e0e4 | 92 | printf( " RAM size = %10d bytes\r\n", tpp->ram_size ); |
okano | 28:689c3880e0e4 | 93 | printf( " flash size = %10d bytes\r\n", tpp->flash_size ); |
okano | 28:689c3880e0e4 | 94 | |
okano | 28:689c3880e0e4 | 95 | printf( " opening file: \"%s\"\r\n", file_name ); |
okano | 28:689c3880e0e4 | 96 | |
okano | 28:689c3880e0e4 | 97 | if ( NULL == (fp = fopen( file_name, "rb" )) ) { |
okano | 28:689c3880e0e4 | 98 | return ( ERROR_AT_FILE_OPEN ); |
okano | 28:689c3880e0e4 | 99 | } |
okano | 28:689c3880e0e4 | 100 | |
okano | 28:689c3880e0e4 | 101 | data_size = file_size( fp ); |
okano | 28:689c3880e0e4 | 102 | last_sector = data_size / tpp->sector_size; |
okano | 28:689c3880e0e4 | 103 | |
okano | 28:689c3880e0e4 | 104 | printf( " data size = %d bytes, it takes %d secotrs in flash area\r\n", data_size, last_sector + 1 ); |
okano | 28:689c3880e0e4 | 105 | printf( " resetting target\r\n" ); |
okano | 28:689c3880e0e4 | 106 | |
okano | 28:689c3880e0e4 | 107 | if ( erase_sectors( last_sector ) ) |
okano | 28:689c3880e0e4 | 108 | return ( ERROR_AT_SECTOR_ERASE ); |
okano | 28:689c3880e0e4 | 109 | |
okano | 28:689c3880e0e4 | 110 | printf( "\r\n ==== flash writing ====\r\n" ); |
okano | 28:689c3880e0e4 | 111 | |
okano | 28:689c3880e0e4 | 112 | if ( err = write_flash( fp, tpp ) ) |
okano | 28:689c3880e0e4 | 113 | return ( err ); |
okano | 28:689c3880e0e4 | 114 | |
okano | 28:689c3880e0e4 | 115 | printf( "\r\n ==== flash reading and verifying ====\r\n" ); |
okano | 28:689c3880e0e4 | 116 | |
okano | 28:689c3880e0e4 | 117 | if ( err = verify_flash( fp, tpp ) ) |
okano | 28:689c3880e0e4 | 118 | return ( err ); |
okano | 28:689c3880e0e4 | 119 | |
okano | 28:689c3880e0e4 | 120 | fclose( fp ); |
okano | 28:689c3880e0e4 | 121 | |
okano | 28:689c3880e0e4 | 122 | post_writing_process( tpp ); |
okano | 28:689c3880e0e4 | 123 | |
okano | 28:689c3880e0e4 | 124 | return ( 0 ); |
okano | 22:bd98a782fba6 | 125 | } |
okano | 22:bd98a782fba6 | 126 | |
okano | 22:bd98a782fba6 | 127 | |
okano | 28:689c3880e0e4 | 128 | |
okano | 22:bd98a782fba6 | 129 | int file_size( FILE *fp ) |
okano | 22:bd98a782fba6 | 130 | { |
okano | 22:bd98a782fba6 | 131 | int size; |
okano | 22:bd98a782fba6 | 132 | |
okano | 22:bd98a782fba6 | 133 | fseek( fp, 0, SEEK_END ); // seek to end of file |
okano | 22:bd98a782fba6 | 134 | size = ftell( fp ); // get current file pointer |
okano | 22:bd98a782fba6 | 135 | fseek( fp, 0, SEEK_SET ); // seek back to beginning of file |
okano | 22:bd98a782fba6 | 136 | |
okano | 22:bd98a782fba6 | 137 | return size; |
okano | 22:bd98a782fba6 | 138 | } |
okano | 22:bd98a782fba6 | 139 | |
okano | 22:bd98a782fba6 | 140 | |
okano | 16:cac2348cfcfb | 141 | void success_indicator() |
okano | 16:cac2348cfcfb | 142 | { |
okano | 16:cac2348cfcfb | 143 | static int i = 0; |
okano | 26:a63e73885b21 | 144 | |
okano | 16:cac2348cfcfb | 145 | leds = 0x1 << (i++ & 0x3); |
okano | 16:cac2348cfcfb | 146 | } |
okano | 21:e149d0bdbf4a | 147 | |
okano | 21:e149d0bdbf4a | 148 | |
okano | 28:689c3880e0e4 | 149 | void toggle_led( char v ) |
okano | 21:e149d0bdbf4a | 150 | { |
okano | 28:689c3880e0e4 | 151 | leds = leds ^ (0x1 << v); |
okano | 21:e149d0bdbf4a | 152 | } |
okano | 26:a63e73885b21 | 153 | |
okano | 26:a63e73885b21 | 154 |