OSStatus MicoFlashErase(mico_partition_t partition, uint32_t off_set, uint32_t size) { OSStatus err = kNoErr; uint32_t start_addr = mico_partitions[ partition ].partition_start_addr + off_set; uint32_t end_addr = mico_partitions[ partition ].partition_start_addr + off_set + size - 1; if (size == 0) goto exit; require_action_quiet( partition > MICO_PARTITION_ERROR, exit, err = kParamErr ); require_action_quiet( partition < MICO_PARTITION_MAX, exit, err = kParamErr ); require_action_quiet( mico_partitions[ partition ].partition_owner != MICO_FLASH_NONE, exit, err = kNotFoundErr ); #ifndef BOOTLOADER require_action_quiet( ( mico_partitions[ partition ].partition_options & PAR_OPT_WRITE_MASK ) == PAR_OPT_WRITE_EN, exit, err = kPermissionErr ); #endif require_action_quiet( start_addr >= mico_partitions[ partition ].partition_start_addr, exit, err = kParamErr ); require_action_quiet( end_addr < mico_partitions[ partition ].partition_start_addr + mico_partitions[ partition ].partition_length, exit, err = kParamErr ); if( platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].initialized == false ) { err = MicoFlashInitialize( partition ); require_noerr_quiet( err, exit ); } mico_rtos_lock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); err = platform_flash_erase( &platform_flash_peripherals[ mico_partitions[ partition ].partition_owner ], start_addr, end_addr ); mico_rtos_unlock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex ); exit: return err; }
OSStatus MicoFlashErase( mico_flash_t flash, uint32_t StartAddress, uint32_t EndAddress ) { OSStatus err = kNoErr; if( platform_flash_drivers[flash].initialized == false ) { err = MicoFlashInitialize( flash ); require_noerr( err, exit ); } mico_rtos_lock_mutex( &platform_flash_drivers[flash].flash_mutex ); err = platform_flash_erase( &platform_flash_drivers[flash], StartAddress, EndAddress ); mico_rtos_unlock_mutex( &platform_flash_drivers[flash].flash_mutex ); exit: return err; }
/** * @brief Display the Main Menu on HyperTerminal * @param None * @retval None */ void Main_Menu(void) { char cmdbuf [CMD_STRING_SIZE] = {0}, cmdname[15] = {0}; /* command input buffer */ int i, j; /* index for command buffer */ char startAddressStr[10], endAddressStr[10], flash_dev_str[4]; int32_t startAddress, endAddress; bool inputFlashArea = false; mico_logic_partition_t *partition; mico_flash_t flash_dev; OSStatus err = kNoErr; while (1) { /* loop forever */ printf ("\n\rMXCHIP> "); getline (&cmdbuf[0], sizeof (cmdbuf)); /* input command line */ for (i = 0; cmdbuf[i] == ' '; i++); /* skip blanks on head */ for (; cmdbuf[i] != 0; i++) { /* convert to upper characters */ cmdbuf[i] = toupper(cmdbuf[i]); } for (i = 0; cmdbuf[i] == ' '; i++); /* skip blanks on head */ for(j=0; cmdbuf[i] != ' '&&cmdbuf[i] != 0; i++,j++) { /* find command name */ cmdname[j] = cmdbuf[i]; } cmdname[j] = '\0'; /***************** Command "0" or "BOOTUPDATE": Update the application *************************/ if(strcmp(cmdname, "BOOTUPDATE") == 0 || strcmp(cmdname, "0") == 0) { partition = MicoFlashGetInfo( MICO_PARTITION_BOOTLOADER ); if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead Bootloader...\n\r"); SerialUpload( partition->partition_owner, partition->partition_start_addr, "BootLoaderImage.bin", partition->partition_length ); continue; } printf ("\n\rUpdating Bootloader...\n\r"); err = MicoFlashDisableSecurity( MICO_PARTITION_BOOTLOADER, 0x0, partition->partition_length ); require_noerr( err, exit); SerialDownload( partition->partition_owner, partition->partition_start_addr, partition->partition_length ); } /***************** Command "1" or "FWUPDATE": Update the MICO application *************************/ else if(strcmp(cmdname, "FWUPDATE") == 0 || strcmp(cmdname, "1") == 0) { partition = MicoFlashGetInfo( MICO_PARTITION_APPLICATION ); if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead application...\n\r"); SerialUpload( partition->partition_owner, partition->partition_start_addr, "ApplicationImage.bin", partition->partition_length ); continue; } printf ("\n\rUpdating application...\n\r"); err = MicoFlashDisableSecurity( MICO_PARTITION_APPLICATION, 0x0, partition->partition_length ); require_noerr( err, exit); SerialDownload( partition->partition_owner, partition->partition_start_addr, partition->partition_length ); } /***************** Command "2" or "DRIVERUPDATE": Update the RF driver *************************/ else if(strcmp(cmdname, "DRIVERUPDATE") == 0 || strcmp(cmdname, "2") == 0) { partition = MicoFlashGetInfo( MICO_PARTITION_RF_FIRMWARE ); if( partition == NULL ){ printf ("\n\rNo flash memory for RF firmware, exiting...\n\r"); continue; } if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead RF firmware...\n\r"); SerialUpload( partition->partition_owner, partition->partition_start_addr, "DriverImage.bin", partition->partition_length ); continue; } printf ("\n\rUpdating RF driver...\n\r"); err = MicoFlashDisableSecurity( MICO_PARTITION_RF_FIRMWARE, 0x0, partition->partition_length ); require_noerr( err, exit); SerialDownload( partition->partition_owner, partition->partition_start_addr, partition->partition_length ); } /***************** Command "3" or "PARAUPDATE": Update the application *************************/ else if(strcmp(cmdname, "PARAUPDATE") == 0 || strcmp(cmdname, "3") == 0) { partition = MicoFlashGetInfo( MICO_PARTITION_PARAMETER_1 ); if (findCommandPara(cmdbuf, "e", NULL, 0) != -1){ printf ("\n\rErasing settings...\n\r"); err = MicoFlashDisableSecurity( MICO_PARTITION_PARAMETER_1, 0x0, partition->partition_length ); require_noerr( err, exit); MicoFlashErase( MICO_PARTITION_PARAMETER_1, 0x0, partition->partition_length ); continue; } if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead settings...\n\r"); SerialUpload( partition->partition_owner, partition->partition_start_addr, "DriverImage.bin", partition->partition_length ); continue; } printf ("\n\rUpdating settings...\n\r"); err = MicoFlashDisableSecurity( MICO_PARTITION_PARAMETER_1, 0x0, partition->partition_length ); require_noerr( err, exit); SerialDownload( partition->partition_owner, partition->partition_start_addr, partition->partition_length ); } /***************** Command "4" or "FLASHUPDATE": : Update the Flash *************************/ else if(strcmp(cmdname, "FLASHUPDATE") == 0 || strcmp(cmdname, "4") == 0) { if (findCommandPara(cmdbuf, "dev", flash_dev_str, 1) == -1 ){ printf ("\n\rUnkown target type! Exiting...\n\r"); continue; } if(Str2Int((uint8_t *)flash_dev_str, (int32_t *)&flash_dev)==0){ printf ("\n\rDevice Number Err! Exiting...\n\r"); continue; } if( flash_dev >= MICO_FLASH_MAX ){ printf ("\n\rDevice Err! Exiting...\n\r"); continue; } inputFlashArea = false; if (findCommandPara(cmdbuf, "start", startAddressStr, 10) != -1){ if(Str2Int((uint8_t *)startAddressStr, &startAddress)==0){ //Found Flash start address printf ("\n\rIllegal start address.\n\r"); continue; }else{ if (findCommandPara(cmdbuf, "end", endAddressStr, 10) != -1){ //Found Flash end address if(Str2Int((uint8_t *)endAddressStr, &endAddress)==0){ printf ("\n\rIllegal end address.\n\r"); continue; }else{ inputFlashArea = true; } }else{ printf ("\n\rFlash end address not found.\n\r"); continue; } } } if(endAddress<startAddress && inputFlashArea == true) { printf ("\n\rIllegal address.\n\r"); continue; } if(inputFlashArea != true){ startAddress = platform_flash_peripherals[ flash_dev ].flash_start_addr ; endAddress = platform_flash_peripherals[ flash_dev ].flash_start_addr + platform_flash_peripherals[ flash_dev ].flash_length - 1; } if (findCommandPara(cmdbuf, "e", NULL, 0) != -1){ printf ("\n\rErasing dev%d content From 0x%x to 0x%x\n\r", flash_dev, startAddress, endAddress); platform_flash_init( &platform_flash_peripherals[ flash_dev ] ); platform_flash_disable_protect( &platform_flash_peripherals[ flash_dev ], startAddress, endAddress ); platform_flash_erase( &platform_flash_peripherals[ flash_dev ], startAddress, endAddress ); continue; } if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead dev%d content From 0x%x to 0x%x\n\r", flash_dev, startAddress, endAddress); SerialUpload(flash_dev, startAddress, "FlashImage.bin", endAddress-startAddress+1); continue; } printf ("\n\rUpdating dev%d content From 0x%x to 0x%x\n\r", flash_dev, startAddress, endAddress); platform_flash_disable_protect( &platform_flash_peripherals[ flash_dev ], startAddress, endAddress ); SerialDownload(flash_dev, startAddress, endAddress-startAddress+1); } /***************** Command: MEMORYMAP *************************/ else if(strcmp(cmdname, "MEMORYMAP") == 0 || strcmp(cmdname, "5") == 0) { printf("\r"); for( i = 0; i <= MICO_PARTITION_PARAMETER_2; i++ ){ partition = MicoFlashGetInfo( (mico_partition_t)i ); if (partition->partition_owner == MICO_FLASH_NONE) continue; printf( "| %11s | Dev:%d | 0x%08x | 0x%08x |\r\n", partition->partition_description, partition->partition_owner, partition->partition_start_addr, partition->partition_length); } } /***************** Command: Excute the application *************************/ else if(strcmp(cmdname, "BOOT") == 0 || strcmp(cmdname, "6") == 0) { printf ("\n\rBooting.......\n\r"); partition = MicoFlashGetInfo( MICO_PARTITION_APPLICATION ); bootloader_start_app( partition->partition_start_addr ); } /***************** Command: Reboot *************************/ else if(strcmp(cmdname, "REBOOT") == 0 || strcmp(cmdname, "7") == 0) { printf ("\n\rReBooting.......\n\r"); MicoSystemReboot(); break; } else if(strcmp(cmdname, "HELP") == 0 || strcmp(cmdname, "?") == 0) { printf ( menu, MODEL, Bootloader_REVISION, HARDWARE_REVISION ); /* display command menu */ break; } else if(strcmp(cmdname, "") == 0 ) { break; } else{ printf (ERROR_STR, "UNKNOWN COMMAND"); break; } exit: continue; } }
void init_platform_bootloader( void ) { CRC8_Context crc; OSStatus err = kNoErr; mico_logic_partition_t *rf_partition = MicoFlashGetInfo( MICO_PARTITION_RF_FIRMWARE ); MicoGpioInitialize( (mico_gpio_t)MICO_SYS_LED, OUTPUT_PUSH_PULL ); MicoGpioOutputLow( (mico_gpio_t)MICO_SYS_LED ); MicoGpioInitialize( (mico_gpio_t)MICO_RF_LED, OUTPUT_OPEN_DRAIN_NO_PULL ); MicoGpioOutputHigh( (mico_gpio_t)MICO_RF_LED ); MicoGpioInitialize((mico_gpio_t)BOOT_SEL, INPUT_PULL_UP); MicoGpioInitialize((mico_gpio_t)MFG_SEL, INPUT_PULL_UP); #ifdef USE_MiCOKit_EXT dc_motor_init( ); dc_motor_set( 0 ); rgb_led_init(); rgb_led_open(0, 0, 0); #endif /* Specific operations used in EMW3165 production */ #define NEED_RF_DRIVER_COPY_BASE ((uint32_t)0x08008000) #define TEMP_RF_DRIVER_BASE ((uint32_t)0x08040000) #define TEMP_RF_DRIVER_END ((uint32_t)0x0807FFFF) const uint8_t isDriverNeedCopy = *(uint8_t *)(NEED_RF_DRIVER_COPY_BASE); const uint32_t totalLength = rf_partition->partition_length; const uint8_t crcResult = *(uint8_t *)(TEMP_RF_DRIVER_END); uint8_t targetCrcResult = 0; uint32_t copyLength; uint32_t destStartAddress_tmp = rf_partition->partition_start_addr; uint32_t sourceStartAddress_tmp = TEMP_RF_DRIVER_BASE; uint32_t i; if ( isDriverNeedCopy != 0x0 ) return; platform_log( "Bootloader start to copy RF driver..." ); /* Copy RF driver to SPI flash */ err = platform_flash_init( &platform_flash_peripherals[ MICO_FLASH_SPI ] ); require_noerr(err, exit); err = platform_flash_init( &platform_flash_peripherals[ MICO_FLASH_EMBEDDED ] ); require_noerr(err, exit); err = platform_flash_erase( &platform_flash_peripherals[ MICO_FLASH_SPI ], rf_partition->partition_start_addr, rf_partition->partition_start_addr + rf_partition->partition_length - 1 ); require_noerr(err, exit); platform_log( "Time: %d", mico_get_time_no_os() ); for(i = 0; i <= totalLength/SizePerRW; i++){ if( i == totalLength/SizePerRW ){ if(totalLength%SizePerRW) copyLength = totalLength%SizePerRW; else break; }else{ copyLength = SizePerRW; } printf("."); err = platform_flash_read( &platform_flash_peripherals[ MICO_FLASH_EMBEDDED ], &sourceStartAddress_tmp, data , copyLength ); require_noerr( err, exit ); err = platform_flash_write( &platform_flash_peripherals[ MICO_FLASH_SPI ], &destStartAddress_tmp, data, copyLength ); require_noerr(err, exit); } printf("\r\n"); /* Check CRC-8 check-sum */ platform_log( "Bootloader start to verify RF driver..." ); sourceStartAddress_tmp = TEMP_RF_DRIVER_BASE; destStartAddress_tmp = rf_partition->partition_start_addr; CRC8_Init( &crc ); for(i = 0; i <= totalLength/SizePerRW; i++){ if( i == totalLength/SizePerRW ){ if(totalLength%SizePerRW) copyLength = totalLength%SizePerRW; else break; }else{ copyLength = SizePerRW; } printf("."); err = platform_flash_read( &platform_flash_peripherals[ MICO_FLASH_SPI ], &destStartAddress_tmp, data, copyLength ); require_noerr( err, exit ); CRC8_Update( &crc, data, copyLength); } CRC8_Final( &crc, &targetCrcResult ); printf("\r\n"); //require_string( crcResult == targetCrcResult, exit, "Check-sum error" ); if( crcResult != targetCrcResult ){ platform_log("Check-sum error"); while(1); } /* Clear RF driver from temperary storage */ platform_log("Bootloader start to clear RF driver temporary storage..."); /* Clear copy tag */ err = platform_flash_erase( &platform_flash_peripherals[ MICO_FLASH_EMBEDDED ], NEED_RF_DRIVER_COPY_BASE, NEED_RF_DRIVER_COPY_BASE); require_noerr(err, exit); exit: return; }