comparison Bootloader/main.c @ 337:932ef3bbd4d7

Working mk20dx256vlh7 usb flash support
author Jacob Alexander <haata@kiibohd.com>
date Sat, 30 May 2015 00:02:22 -0700
parents 7be3475fc9fa
children 136e47478441
comparison
equal deleted inserted replaced
336:7be3475fc9fa 337:932ef3bbd4d7
47 FTFL.fccob.read_1s_section.addr = (uintptr_t)start; 47 FTFL.fccob.read_1s_section.addr = (uintptr_t)start;
48 FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL; 48 FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
49 FTFL.fccob.read_1s_section.num_words = 250; // 2000 kB / 64 bits 49 FTFL.fccob.read_1s_section.num_words = 250; // 2000 kB / 64 bits
50 int retval = ftfl_submit_cmd(); 50 int retval = ftfl_submit_cmd();
51 51
52 #ifdef FLASH_DEBUG
52 print( NL ); 53 print( NL );
53 print("Block "); 54 print("Block ");
54 printHex( sector ); 55 printHex( sector );
55 print(" "); 56 print(" ");
56 printHex( (size_t)start ); 57 printHex( (size_t)start );
57 print(" -> "); 58 print(" -> ");
58 printHex( (size_t)end ); 59 printHex( (size_t)end );
59 print(" Erased: "); 60 print(" Erased: ");
60 printHex( retval ); 61 printHex( retval );
61 print( NL ); 62 print( NL );
63 #endif
62 64
63 // Display sector 65 // Display sector
64 for ( size_t line = 0; pos < end - 24; line++ ) 66 for ( size_t line = 0; pos < end - 24; line++ )
65 { 67 {
66 // Each Line 68 // Each Line
105 107
106 static enum dfu_status setup_write( size_t off, size_t len, void **buf ) 108 static enum dfu_status setup_write( size_t off, size_t len, void **buf )
107 { 109 {
108 static int last = 0; 110 static int last = 0;
109 111
112 #ifdef FLASH_DEBUG
113 // Debug
114 print("Setup Write: offset(");
115 printHex( off );
116 print(") len(");
117 printHex( len );
118 print(") last(");
119 printHex( last );
120 printNL(")");
121 #endif
122
110 if ( len > sizeof(staging) ) 123 if ( len > sizeof(staging) )
111 return (DFU_STATUS_errADDRESS); 124 return (DFU_STATUS_errADDRESS);
112 125
113 // We only allow the last write to be less than one sector size. 126 // We only allow the last write to be less than one sector size.
114 if ( off == 0 ) 127 if ( off == 0 )
115 last = 0; 128 last = 0;
116 if ( last && len != 0 ) 129 if ( last && len != 0 )
117 return (DFU_STATUS_errADDRESS); 130 return (DFU_STATUS_errADDRESS);
118 if ( len != FLASH_SECTOR_SIZE ) 131 if ( len != USB_DFU_TRANSFER_SIZE )
119 { 132 {
120 last = 1; 133 last = 1;
121 memset( staging, 0xff, sizeof(staging) ); 134 memset( staging, 0xff, sizeof(staging) );
122 } 135 }
123 136
129 { 142 {
130 void *target; 143 void *target;
131 if ( len == 0 ) 144 if ( len == 0 )
132 return (DFU_STATUS_OK); 145 return (DFU_STATUS_OK);
133 146
134 target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE); 147 target = flash_get_staging_area( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE );
135 if ( !target ) 148 if ( !target )
136 return (DFU_STATUS_errADDRESS); 149 return (DFU_STATUS_errADDRESS);
137 memcpy( target, buf, len ); 150 memcpy( target, buf, len );
138 151
139 // Depending on the error return a different status 152 // Depending on the error return a different status
140 switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) ) 153 switch ( flash_program_sector( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE ) )
141 { 154 {
142 /* 155 /*
143 case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error 156 case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
144 case FTFL_FSTAT_ACCERR: // Flash Access Error 157 case FTFL_FSTAT_ACCERR: // Flash Access Error
145 case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error 158 case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error
183 #error "Incompatible chip for bootloader" 196 #error "Incompatible chip for bootloader"
184 #endif 197 #endif
185 198
186 uart_serial_setup(); 199 uart_serial_setup();
187 printNL( NL "Bootloader DFU-Mode" ); 200 printNL( NL "Bootloader DFU-Mode" );
201
202 // Bootloader Enter Reasons
203 print(" RCM_SRS0 - ");
204 printHex( RCM_SRS0 & 0x60 );
205 print( NL " RCM_SRS1 - ");
206 printHex( RCM_SRS1 & 0x02 );
207 print( NL " _app_rom - ");
208 printHex( (uint32_t)_app_rom );
209 print( NL " Soft Rst - " );
210 printHex( memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 );
211 print( NL );
188 212
189 // XXX REMOVEME 213 // XXX REMOVEME
190 /* 214 /*
191 GPIOB_PDDR |= (1<<16); 215 GPIOB_PDDR |= (1<<16);
192 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); 216 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
213 GPIOC_PDDR |= (1<<3); 237 GPIOC_PDDR |= (1<<3);
214 PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); 238 PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
215 GPIOC_PCOR |= (1<<3); 239 GPIOC_PCOR |= (1<<3);
216 */ 240 */
217 241
218 /* 242 #ifdef FLASH_DEBUG
219 // Read Firmware 1 Status 243 for ( uint8_t sector = 0; sector < 3; sector++ )
220 FTFL.fccob.read_1s_block.fcmd = FTFL_FCMD_READ_1s_BLOCK;
221 FTFL.fccob.read_1s_block.addr = (uintptr_t)&_app_rom;
222 FTFL.fccob.read_1s_block.margin = FTFL_MARGIN_NORMAL;
223
224 int retval = ftfl_submit_cmd();
225 print("Firmware Erase Status: ");
226 printHex( retval );
227 print( NL );
228
229
230 // Read Bootloader 1 Status
231 FTFL.fccob.read_1s_block.fcmd = FTFL_FCMD_READ_1s_BLOCK;
232 FTFL.fccob.read_1s_block.addr = (uintptr_t)&_bootloader;
233 FTFL.fccob.read_1s_block.margin = FTFL_MARGIN_NORMAL;
234
235 retval = ftfl_submit_cmd();
236 print("Bootloader Erase Status: ");
237 printHex( retval );
238 print( NL );
239 */
240
241 /*
242 // Program First Longword of firmware
243 FTFL.fccob.program_longword.fcmd = FTFL_FCMD_PROGRAM_LONGWORD;
244 FTFL.fccob.program_longword.addr = (uintptr_t)&_app_rom;
245 FTFL.fccob.program_longword.data_be[0] = 0x1;
246 FTFL.fccob.program_longword.data_be[1] = 0x2;
247 FTFL.fccob.program_longword.data_be[2] = 0x4;
248 FTFL.fccob.program_longword.data_be[3] = 0x8;
249 int retval = ftfl_submit_cmd();
250 print("Write Longword Status: ");
251 printHex( retval );
252 print( NL );
253 */
254
255 /*
256 // Erase Sector
257 FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
258 FTFL.fccob.erase.addr = (uintptr_t)&_app_rom;
259 int retval = ftfl_submit_cmd();
260 print("Erase Status: ");
261 printHex( retval );
262 print( NL );
263
264 // Prepare FlexRAM
265 FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
266 FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
267 retval = ftfl_submit_cmd();
268 print("Set FlexRAM Status: ");
269 printHex( retval );
270 print( NL );
271
272 // Write to FlexRAM
273 memset( FlexRAM, 0xB4, 1000 );
274 memset( &FlexRAM[1000], 0xE3, 1000 );
275
276 // Program Sector
277 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
278 FTFL.fccob.program_section.addr = (uintptr_t)&_app_rom;
279 FTFL.fccob.program_section.num_words = 128;
280 //FTFL.fccob.program_section.num_words = 250; // 2000 kb / 64 bits
281 retval = ftfl_submit_cmd();
282 print("Program Sector1 Status: ");
283 printHex( retval );
284 print( NL );
285
286 FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
287 FTFL.fccob.program_section.addr = (uintptr_t)&_app_rom + 0x400;
288 FTFL.fccob.program_section.num_words = 128;
289 //FTFL.fccob.program_section.num_words = 250; // 2000 kb / 64 bits
290 retval = ftfl_submit_cmd();
291 print("Program Sector2 Status: ");
292 printHex( retval );
293 print( NL );
294
295 for ( uint8_t sector = 0; sector < 1; sector++ )
296 //sector_print( &_bootloader, sector, 16 );
297 sector_print( &_app_rom, sector, 16 ); 244 sector_print( &_app_rom, sector, 16 );
298 print( NL ); 245 print( NL );
299 */ 246 #endif
300 247
301 flash_prepare_flashing(); 248 flash_prepare_flashing();
302
303 //uint32_t *position = &_app_rom;
304 usb_init( &dfu_device ); 249 usb_init( &dfu_device );
305
306 for (;;) 250 for (;;)
307 { 251 {
308 usb_poll(); 252 usb_poll();
309 } 253 }
310 } 254 }