static int uploadApp(void) { int retval = 0; uint8_t recvEndAddr; uint32_t flashMemory = APP_ADDR_START; uint32_t blockNum = 0; uint32_t recvCnt = 0; uint8_t* pAppBuffer = (uint8_t *)FLASH_BUFFER_ADDR; __disable_irq(); /* Erase the last sector which includes the application id */ retval = EraseSector(LAST_SECTOR_ADDR); if (0 != retval) return retval; /* receive user application firmware data */ retval = receiveAppData(&recvCnt, (uint8_t *)&recvEndAddr); if (0 != retval) return retval; /* receive firmware data */ while(1) { pAppBuffer = (uint8_t *)FLASH_BUFFER_ADDR + (blockNum << 10); if (*pAppBuffer > recvEndAddr) // reach at receive address tail { EraseSector(recvEndAddr); break; } /* Write flash */ flashMemory = APP_ADDR_START + (blockNum << 10); if (blockNum%4 == 0) { initAppBufData((uint32_t *)pAppBuffer); retval = EraseSector(flashMemory); } /* Write App data to falsh */ if (0 == retval) retval = ProgramPage(flashMemory, APP_DATA_BLOCK_SIZE, pAppBuffer); if (0 == retval) { blockNum++; continue; } else break; } __enable_irq(); return retval; }
// //====================================================================== // 写入 //====================================================================== void c_eep::wirte(uint8 *source, uint32 tarAddr, uint16 len) { // //------------------------------------ // 0. 关闭系统中断 nvic.globalDisable(); // //------------------------------------ // 1. 开启内部高速时钟 RCC->CR |= ((uint32_t)RCC_CR_HSION); while((RCC->CR & RCC_CR_HSIRDY) == 0); // //------------------------------------ // 2. 解锁 Init(); // //------------------------------------ // 3. 擦除并写入数据 EraseSector(tarAddr); ProgramPage(tarAddr, len, source); // //------------------------------------ // 4. 锁定 UnInit(); // //------------------------------------ // 5. 关闭内部高速时钟 RCC->CR &= ~((uint32_t)RCC_CR_HSION); while(RCC->CR & RCC_CR_HSIRDY); // //------------------------------------ // 6. 开启中断 nvic.globalEnable(); }
int SSD_Write(void*p, int size) { int r; EraseSector(FILE_ADDR_BEIGN); r = ProgramPage(FILE_ADDR_BEIGN, size, p); return r; }
/********************************************************************************************************* **函数名称: WriteFlash **函数功能: 向FLASH中写入数据 **入口参数: dst 目标地址,即FLASH起始地址,地址必须以256字节对齐,即地址的低8位必须为0 ** src 源地址,即RAM地址,地址必须字对齐,即地址的低两位必须为0 ** no 写入字节数,必须为256/512/1024/4096 **出口参数: Compare的返回值,操作成功正确返回0 ********************************************************************************************************/ INT8U WriteFlash(INT32U dst, INT32U src, INT32U no) { SelSector((dst/0x1000),(dst+no)/0x1000); // 选择扇区 EraseSector((dst/0x1000),(dst+no)/0x1000); // 擦除扇区 BlankCHK((dst/0x1000),(dst+no)/0x1000); // 查空扇区 SelSector((dst/0x1000),(dst+no)/0x1000); // 选择扇区 RamToFlash(dst,src,no); // 写数据到FLASH return(Compare(dst,src,no)); // 比较数据 }
void PROVISION_Overwrite(void) { //#define OVERWRITE_MANUFACTURING_INFO // if this line is uncommented will overwrite the provisioned info #ifdef OVERWRITE_MANUFACTURING_INFO // manufacturing info const struct { uint16 m_unFormatVersion; uint8 m_aMAC[8]; uint16 m_unVRef; uint8 m_ucMaxPA; uint8 m_ucCristal; uint8 m_aProvKEY[16]; uint8 m_aProvSecManager[8]; uint8 m_aDeviceTag[16]; uint8 m_aPAProfile0[17]; uint8 m_aPAProfile1[17]; uint8 m_aPAProfile2[17]; uint8 m_aPAProfile3[17]; uint16 m_unCountryCode[16]; } c_stManufacturing = {1, #if defined( PROVISIONING_DEVICE ) { 0xFD, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, _ISA100_DEVICE_ID }, // dev addr #elif defined( BACKBONE_SUPPORT ) { 0xFB, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, _ISA100_DEVICE_ID }, // dev addr #elif defined( ROUTING_SUPPORT ) { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x0B, _ISA100_DEVICE_ID }, // dev addr #else { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, _ISA100_DEVICE_ID }, // dev addr #endif 0xC409, 0xE7, 0x00, { 0x00,0x49,0x00,0x53,0x00,0x41,0x00,0x20,0x00,0x31,0x00,0x30,0x00,0x30,0x00,0x00 }, // m_aProvKEY { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF }, // m_aProvSecManager { 'I', 'N', 'T', 'E', 'G', 'R', 'A', 'T', 'I', 'O', 'N', ' ', 'K', 'I', 'T', ' ' }, // m_aDeviceTag { 0x00,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7 }, // m_aPAProfile0 { 0x00,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7 }, // m_aPAProfile1 { 0x00,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7 }, // m_aPAProfile2 { 0x00,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7,0xE7 }, // m_aPAProfile3 { CODE_USA, CODE_CAN, CODE_MEX, CODE_JPN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; EraseSector( MANUFACTURING_SECTOR_NO ); WritePersistentData( (uint8*)&c_stManufacturing, MANUFACTURING_START_ADDR, sizeof(c_stManufacturing) ); #endif // OVERWRITE_MANUFACTURING_INFO }
/* returns the # of failures, 0 on success */ static unsigned ProgramImageFile(char* filename, UINT8* pos, tImageHeader* pImageHeader, int start, int size) { int i; int fd; int read; /* how many for this sector */ unsigned failures = 0; fd = rb->open(filename, O_RDONLY); if (fd < 0) return false; /* no error checking necessary here, we checked for minimum size already */ rb->lseek(fd, start, SEEK_SET); /* go to start position */ *(tImageHeader*)sector = *pImageHeader; /* copy header into sector buffer */ read = rb->read(fd, sector + sizeof(tImageHeader), SECTORSIZE - sizeof(tImageHeader)); /* payload behind */ size -= read; read += sizeof(tImageHeader); /* to be programmed, but not part of the file */ do { if (!EraseSector(pos)) { /* nothing we can do, let the programming count the errors */ } for (i=0; i<read; i++) { if (!ProgramByte(pos + i, sector[i])) { failures++; } } pos += SECTORSIZE; read = rb->read(fd, sector, (size > SECTORSIZE) ? SECTORSIZE : size); /* payload for next sector */ size -= read; } while (read > 0); rb->close(fd); return failures; }
/* ******************************************************************************************************* ** 函数名称:read_userinfo_flash() ** 函数功能:清除冷热启动信息 ** 入口参数:无 ** 出口参数:无 ** ******************************************************************************************************* */ void clean_userinfo_flash(void) { //User_Info_Flash *readflash; User_Info_Flash *writeflash = (User_Info_Flash *)flashdata; writeflash->poweron_cnt = 1; writeflash->powerup_cnt = 0; writeflash->is_fistpoweron = 0x12801280; writeflash->err_type = 0; /*将更新好的数据写入到flash*/ SelSector(14,14); // 选择扇区14 EraseSector(14,14); // 擦除扇区14 BlankCHK(14,14) ; // 查空扇区14 SelSector(14,14); // 选择扇区14 RamToFlash(USER_FLASH_ADDR,(uint32)flashdata, 512); }
/* ******************************************************************************************************* ** 函数名称:powerup_process() ** 函数功能:冷热启动处理 ** 入口参数:无 ** 出口参数:无 ** ******************************************************************************************************* */ void startup_process(void) { User_Info_Flash *readflash; User_Info_Flash *writeflash; readflash = (User_Info_Flash *)USER_FLASH_ADDR; //flash地址 writeflash = (User_Info_Flash *)flashdata; //暂存flash数据数组 /*更新User_Info_Flash 结构体的各个字段*/ if(readflash->is_fistpoweron != 0x12801280){ //判断是否为首次上电 IS_HOTSTART1 = 0x128A128A; IS_HOTSTART2 = 0x128B128B; RESET_TYPE = 0; writeflash->is_fistpoweron = 0x12801280; //初始化各个字段 writeflash->poweron_cnt = 1; writeflash->powerup_cnt = 0; writeflash->err_type = 0; }else{ if((IS_HOTSTART1 == 0x128A128A)&&(IS_HOTSTART2 == 0x128B128B)){ //复位启动 writeflash->is_fistpoweron = 0x12801280; writeflash->poweron_cnt = readflash->poweron_cnt; writeflash->powerup_cnt = (readflash->powerup_cnt+1); writeflash->err_type = RESET_TYPE; }else{ IS_HOTSTART1 = 0x128A128A; IS_HOTSTART2 = 0x128B128B; RESET_TYPE = 0; //上电启动 writeflash->is_fistpoweron = 0x12801280; writeflash->poweron_cnt = (readflash->poweron_cnt + 1); writeflash->powerup_cnt = readflash->powerup_cnt; writeflash->err_type = readflash->err_type; } } /*将更新好的数据写入到flash*/ SelSector(14,14); // 选择扇区14 EraseSector(14,14); // 擦除扇区14 BlankCHK(14,14) ; // 查空扇区14 SelSector(14,14); // 选择扇区14 RamToFlash(USER_FLASH_ADDR,(uint32)flashdata, 512); }
/* returns the # of failures, 0 on success */ unsigned ProgramFirmwareFile(char* filename, int chipsize) { int i, j; int fd; int read = SEC_SIZE; /* how many for this sector */ UINT16 keep = *(UINT16*)(FB + KEEP); /* we must keep this! */ unsigned failures = 0; fd = rb->open(filename, O_RDONLY); if (fd < 0) return false; for (i=0; i<chipsize; i+=SEC_SIZE) { if (!EraseSector(FB + i)) { /* nothing we can do, let the programming count the errors */ } if (read == SEC_SIZE) /* not EOF yet */ { read = rb->read(fd, sector, SEC_SIZE); if (i==0) { /* put original value back in */ *(UINT16*)(sector + KEEP) = keep; } for (j=0; j<read; j++) { if (!ProgramByte(FB + i + j, sector[j])) { failures++; } } } } rb->close(fd); return failures; }
bool PersistentStore::access_finish() { if (eeprom_dirty) { IAP_STATUS_CODE status; if (--current_slot < 0) { // all slots have been used, erase everything and start again __disable_irq(); status = EraseSector(EEPROM_SECTOR, EEPROM_SECTOR); __enable_irq(); current_slot = EEPROM_SLOTS - 1; } __disable_irq(); status = CopyRAM2Flash(SLOT_ADDRESS(EEPROM_SECTOR, current_slot), ram_eeprom, IAP_WRITE_4096); __enable_irq(); if (status != CMD_SUCCESS) return false; eeprom_dirty = false; } return true; }
// Reprogram the new settings if flash writing is allowed static void program_cfg(cfg_setting_t *new_cfg) { uint32_t status; uint32_t addr; cortex_int_state_t state; addr = (uint32_t)&config_rom; state = cortex_int_get_and_disable(); status = EraseSector(addr); cortex_int_restore(state); if (status != 0) { return; } memset(write_buffer, 0xFF, sizeof(write_buffer)); memcpy(write_buffer, new_cfg, sizeof(cfg_setting_t)); state = cortex_int_get_and_disable(); status = ProgramPage(addr, sizeof(write_buffer), write_buffer); cortex_int_restore(state); if (0 != status) { return; } }
/* Function to do the read/write to USB Disk */ bool USB_ReadWriteFile(void) { FRESULT rc; /* Result code */ UINT br; uint32_t *address = USER_FLASH_START; uint16_t i = 0; if(f_mount(&fatFS, "0:", 1) != FR_OK) { PRINTDBG("\nUnable to mount pendrive."); /* First... Mounting USB Unit. */ return(false); } if (f_open(&fileObj, "0:firmware.bin", FA_READ) != FR_OK) { PRINTDBG("\nUnable to open firmware.bin from USB."); return(false); } /* LED's ON. */ LEDPORT->FIOPIN &= ~(1 << LEDPORTPIN); PRINTDBG("\nReading firmware.bin from USB."); for (;;) { /* Clear Buffer. */ for(i = 0; i < _MIN_SS; i++) buffer[i] = 0; /* Read a chunk of file */ rc = f_read(&fileObj, &buffer, sizeof(buffer), &br); if (rc || !br) { break; /* Error or end of file */ } if(address == (uint32_t *)USER_FLASH_START) { uint32_t checksum = 0; uint32_t *tmpptr; tmpptr = (uint32_t *)&buffer[0]; for (i = 0; i < 8; i++) { checksum += *tmpptr; tmpptr++; } if (checksum != 0) { rc = 1; PRINTDBG("\nChecksum Error."); return(false); } PRINTDBG("\nChecksum Valid."); } /* */ __disable_irq(); for(i = USER_START_SECTOR; i <= MAX_USER_SECTOR; i++) { if(address < sector_end_map[i]) { if(address == sector_start_map[i]) { EraseSector(i,i); } break; } } // if(CopyRAM2Flash((uint8_t *)address, &buffer[0], FLASH_BUF_SIZE) != CMD_SUCCESS) { return(false); /* No way to recover. Just let Windows report a write failure */ } // #if DEBUG printf("\n%d bytes writed in %#08x.", br, address); #endif address += (FLASH_BUF_SIZE / sizeof(uint32_t)); __enable_irq(); } if (f_close(&fileObj) != FR_OK) { PRINTDBG("\nError to close file."); return(false); } PRINTDBG("\nClose the file."); USB_Host_SetDeviceConfiguration(FlashDisk_MS_Interface.Config.PortNumber, 0); PRINTDBG("\nFlash userdata updated."); return(true); }
///////////////////////////////////////////////////////////////////// // 函数名称:void File_Table_Write(BYTE page,BYTE *buf) // 函数功能: 配置表文件写 // 入口参数: // // // 出口参数:无 //////////////////////////////////////////////////////////////////// void File_Table_Write(uint16 page, BYTE *buf, uint8 *des_ip) { uint8 str[128], err, *pw,i=0,n=1; uint32 resault, tmp, resault1; struct CLASS_INFO *pinfo; #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr; /* Prevent compiler warning */ #endif IP_printf("文件写:%d\n\n\n", page); //设备表擦除 if (page == 0) { // gDeviceTable.File.FileDebug=gDeviceTable.Debug; // gDeviceTable.Debug&=~DEV_DEBUG_MSG; OS_ENTER_CRITICAL(); InitIAP(); resault = SelSector(ARM_TABLE_SECTOR_START, ARM_TABLE_SECTOR_END); resault = EraseSector(ARM_TABLE_SECTOR_START, ARM_TABLE_SECTOR_END); resault = BlankCHK(ARM_TABLE_SECTOR_START, ARM_TABLE_SECTOR_END); OS_EXIT_CRITICAL(); IP_printf("配置表被擦除!"); if (resault != CMD_SUCCESS) { File_Req(0, 0xffff, des_ip); return; } } if (page < 256) { if ((page % 10) == 0) IP_printf("文件写:%d\n\n\n", page); InitIAP(); //p=(BYTE *)(buf+sizeof (struct BASE_MSG)+sizeof(struct SFILE_TRANSLATE_TYPE )); pw = (BYTE *)User_mem_allco(256); if (pw == NULL) { File_Req(0, page, des_ip); return; } memcpy(pw, buf, 256); if (page == 0) { pinfo = (struct CLASS_INFO *)pw; User_ID_Read(str, 1); memcpy((BYTE *)pinfo->class_ID, str, 8); //IP_printf_256(pw); } if (page == 0xfa) { err = 0; memset(str, 0, 128); while(AT24C16_Write(str, DEVICE_USE_STAT_TIME, 128) == FALSE) { if (err++ > 10 ) break; }; } if ((page > 2) && (page < 10)) //课程表 { err = 0; //while(AT24C16_Write(pw,CLASS_TABLE_ADDRESS+(page-3)*256,256)==FALSE) //{ if (err++>10) break; }; } else { tmp = ARM_TABLE_ADDRESS + page * 256; OS_ENTER_CRITICAL(); resault = SelSector(ARM_TABLE_SECTOR_START, ARM_TABLE_SECTOR_END); resault = RamToFlash(tmp, (uint32)pw, 256); OS_EXIT_CRITICAL(); } User_mem_free(pw); //申请下一包 if ((page + 1) < 256) { File_Req(0, page + 1, des_ip); } else { File_Req(0, 0xffff, des_ip); Device_Rec_Scan(); } } else { File_Req(0, 0xffff, des_ip); Device_Rec_Scan(); } }
/*********************************************************************//** * @brief The entry of the program * * @param[in]None * * @return None. * **********************************************************************/ void c_entry (void) { uint32_t result[4]; uint8_t ver_major, ver_minor; uint32_t i; uint8_t *ptr; uint32_t flash_prog_area_sec_start; uint32_t flash_prog_area_sec_end; IAP_STATUS_CODE status; // Initialize debug_frmwrk_init(); for (i = 0;i < sizeof(buffer);i++) { buffer[i] = (uint8_t)i; } flash_prog_area_sec_start = GetSecNum(FLASH_PROG_AREA_START); flash_prog_area_sec_end = GetSecNum(FLASH_PROG_AREA_START + FLASH_PROG_AREA_SIZE); _DBG_(menu); status = ReadPartID(result); if(status != CMD_SUCCESS) { _DBG("Read Part ID failed with code is ");_DBD(status);_DBG_(""); while(1); } _DBG("PartID: ");_DBH32(result[0]);_DBG_(""); status = ReadBootCodeVer(&ver_major, &ver_minor); if(status != CMD_SUCCESS) { _DBG("Read Boot Code Version failed with code is ");_DBD(status);_DBG_(""); while(1); } _DBG("Boot Code Version: ");_DBD(ver_major);_DBG(".");_DBD(ver_minor);_DBG_(""); status = ReadDeviceSerialNum(result); if(status != CMD_SUCCESS) { _DBG("Read UID failed with code is ");_DBD(status);_DBG_(""); while(1); } _DBG("UID: "); for(i = 0; i < 4; i++) { _DBD32(result[i]); if(i<3) _DBG("-"); } _DBG_(""); status = EraseSector(flash_prog_area_sec_start, flash_prog_area_sec_end); if(status != CMD_SUCCESS) { _DBG("Erase chip failed with code is ");_DBD(status);_DBG_(""); while(1); } status = BlankCheckSector(flash_prog_area_sec_start, flash_prog_area_sec_end, &result[0], &result[1]); if(status != CMD_SUCCESS) { _DBG("Blank Check failed with code is ");_DBD(status);_DBG_(""); if(status == SECTOR_NOT_BLANK) { _DBG(">>>>The first non-blank sector is sector "); _DBD(flash_prog_area_sec_start + result[0]); _DBG_(""); } while(1); } _DBG_("Erase chip: Success"); /* Be aware that Program and ErasePage take long time to complete!!! If bigger RAM is present, allocate big buffer and reduce the number of Program blocks. */ /* Program flash block by block until the end of the flash. */ for ( i = 0; i < FLASH_PROG_AREA_SIZE/BUFF_SIZE; i++ ) { ptr = (uint8_t*)(FLASH_PROG_AREA_START + i*BUFF_SIZE); status = CopyRAM2Flash(ptr, buffer,IAP_WRITE_1024); if(status != CMD_SUCCESS) { _DBG("Program chip failed with code is ");_DBD(status);_DBG_(""); while(1); } } // Compare for ( i = 0; i < FLASH_PROG_AREA_SIZE/BUFF_SIZE; i++ ) { ptr = (uint8_t*)(FLASH_PROG_AREA_START + i*BUFF_SIZE); status = Compare(ptr, buffer,BUFF_SIZE); if(status != CMD_SUCCESS) { _DBG("Compare memory failed with code is ");_DBD(status);_DBG_(""); while(1); } } _DBG_("Program chip: Success"); _DBG_("Demo termination"); while (1); }