X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=Bootloader%2Fmain.c;h=aabaeebe502842c77922d49ded00763311bb6238;hb=581b464a13969e0a7f195879513adece5eea2b53;hp=639e8b2a8344df54c8cfdef9c821a6294aafc2ba;hpb=1acbc97e98d8388332c51260017ab23227a91339;p=kiibohd-controller.git diff --git a/Bootloader/main.c b/Bootloader/main.c index 639e8b2..aabaeeb 100644 --- a/Bootloader/main.c +++ b/Bootloader/main.c @@ -30,18 +30,26 @@ /** * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here. */ -static char staging[ FLASH_SECTOR_SIZE ]; +static char staging[ USB_DFU_TRANSFER_SIZE ]; // ----- Functions ----- -void sector_print( void* buf, size_t sector, size_t chunks ) +int sector_print( void* buf, size_t sector, size_t chunks ) { uint8_t* start = (uint8_t*)buf + sector * USB_DFU_TRANSFER_SIZE; uint8_t* end = (uint8_t*)buf + (sector + 1) * USB_DFU_TRANSFER_SIZE; uint8_t* pos = start; + // Verify if sector erased + FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION; + FTFL.fccob.read_1s_section.addr = (uintptr_t)start; + FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL; + FTFL.fccob.read_1s_section.num_words = 250; // 2000 kB / 64 bits + int retval = ftfl_submit_cmd(); + +#ifdef FLASH_DEBUG print( NL ); print("Block "); printHex( sector ); @@ -49,14 +57,17 @@ void sector_print( void* buf, size_t sector, size_t chunks ) printHex( (size_t)start ); print(" -> "); printHex( (size_t)end ); + print(" Erased: "); + printHex( retval ); print( NL ); +#endif // Display sector for ( size_t line = 0; pos < end - 24; line++ ) { // Each Line printHex_op( (size_t)pos, 4 ); - print(" "); + print(": "); // Each 2 byte chunk for ( size_t chunk = 0; chunk < chunks; chunk++ ) @@ -70,6 +81,8 @@ void sector_print( void* buf, size_t sector, size_t chunks ) print( NL ); } + + return retval; } static enum dfu_status setup_read( size_t off, size_t *len, void **buf ) @@ -78,12 +91,16 @@ static enum dfu_status setup_read( size_t off, size_t *len, void **buf ) *buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off; // Calculate length of transfer + /* *len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE ? 0 : USB_DFU_TRANSFER_SIZE; + */ // Check for error + /* if ( *buf > (void*)&_app_rom_end ) return (DFU_STATUS_errADDRESS); + */ return (DFU_STATUS_OK); } @@ -92,6 +109,17 @@ static enum dfu_status setup_write( size_t off, size_t len, void **buf ) { static int last = 0; +#ifdef FLASH_DEBUG + // Debug + print("Setup Write: offset("); + printHex( off ); + print(") len("); + printHex( len ); + print(") last("); + printHex( last ); + printNL(")"); +#endif + if ( len > sizeof(staging) ) return (DFU_STATUS_errADDRESS); @@ -100,7 +128,7 @@ static enum dfu_status setup_write( size_t off, size_t len, void **buf ) last = 0; if ( last && len != 0 ) return (DFU_STATUS_errADDRESS); - if ( len != FLASH_SECTOR_SIZE ) + if ( len != USB_DFU_TRANSFER_SIZE ) { last = 1; memset( staging, 0xff, sizeof(staging) ); @@ -116,16 +144,13 @@ static enum dfu_status finish_write( void *buf, size_t off, size_t len ) if ( len == 0 ) return (DFU_STATUS_OK); - target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE); + target = flash_get_staging_area( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE ); if ( !target ) return (DFU_STATUS_errADDRESS); memcpy( target, buf, len ); - print("BUF: "); - printHex( off ); - sector_print( target, 0, 16 ); // Depending on the error return a different status - switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) ) + switch ( flash_program_sector( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE ) ) { /* case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error @@ -167,6 +192,12 @@ void main() // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); GPIOA_PSOR |= (1<<5); + + // TODO Add CMake configuration for disabling + // Set LCD backlight on ICED to Red + GPIOC_PDDR |= (1<<1); + PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); + GPIOC_PCOR |= (1<<1); #else #error "Incompatible chip for bootloader" #endif @@ -174,43 +205,25 @@ void main() uart_serial_setup(); printNL( NL "Bootloader DFU-Mode" ); - // TODO REMOVEME + // Bootloader Enter Reasons + print(" RCM_SRS0 - "); + printHex( RCM_SRS0 & 0x60 ); + print( NL " RCM_SRS1 - "); + printHex( RCM_SRS1 & 0x02 ); + print( NL " _app_rom - "); + printHex( (uint32_t)_app_rom ); + print( NL " Soft Rst - " ); + printHex( memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 ); + print( NL ); + +#ifdef FLASH_DEBUG for ( uint8_t sector = 0; sector < 3; sector++ ) sector_print( &_app_rom, sector, 16 ); print( NL ); - - // XXX REMOVEME - /* - GPIOB_PDDR |= (1<<16); - PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); - GPIOB_PSOR |= (1<<16); - - // RST - GPIOC_PDDR |= (1<<8); - PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); - GPIOC_PSOR |= (1<<8); - - // CS1B - GPIOC_PDDR |= (1<<4); - PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); - GPIOC_PCOR |= (1<<4); - */ - // Backlight - GPIOC_PDDR |= (1<<1); - PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); - GPIOC_PCOR |= (1<<1); - GPIOC_PDDR |= (1<<2); - PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); - GPIOC_PCOR |= (1<<2); - GPIOC_PDDR |= (1<<3); - PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); - GPIOC_PCOR |= (1<<3); +#endif flash_prepare_flashing(); - - //uint32_t *position = &_app_rom; usb_init( &dfu_device ); - for (;;) { usb_poll();