uint8_t Flash_Read_Bytes(unsigned int num_bytes, uint8_t byte1, uint8_t byte2, uint8_t byte3){ uint8_t buf[8]; uint8_t index; while(!(SPSR & 0x80)); slave_down(); _delay_ms(1); Flash_Write(0x03); Flash_Write(byte1); Flash_Write(byte2); Flash_Write(byte3); int count = 0; uint8_t letters[num_bytes]; while(count != (num_bytes + 1)){ _delay_ms(1); Flash_Write(0x00); letters[count] = SPDR; //UART1_TX(letters[count]); count++; } slave_up(); _delay_ms(1); uint8_t *letts = letters; //UART1_Print(letts); for(index=0; index<5; index++){ UART1_Print(itoa(*(letts+index), buf, 16)); } UART1_Flush(); return 0; }
void If_WriteFirmware(void) { Flash_Init(); if (1) { UINT32 ReadAddrOffset, AddrBase, DataSize, i; PBYTE pOriginalData; UINT32 usLength; if (1) { // Write data to flash AddrBase = WIFI_FW_HELPER_ADDR; Flash_Write (AddrBase, (UINT8*)helperimage, sizeof (helperimage)); // Read back data for checking... ReadAddrOffset = 0; pOriginalData = (PBYTE)helperimage; DataSize = sizeof (helperimage); while (ReadAddrOffset < DataSize) { if (ReadAddrOffset + FLASH_PAGE_SIZE < DataSize) usLength = FLASH_PAGE_SIZE; else usLength = DataSize - ReadAddrOffset; Flash_Read(AddrBase + ReadAddrOffset, ucBuffer, usLength); for (i = 0; i < usLength; i ++) { if (ucBuffer[i] != pOriginalData[ReadAddrOffset + i]) break; } if (i != usLength) TRACE("Data Verify failed1!\r\n"); ReadAddrOffset += usLength; } } if (1) { // Write data to flash AddrBase = WIFI_FW_FW_ADDR; Flash_Write (AddrBase, (UINT8*)fmimage, sizeof (fmimage)); // Read back data for checking... ReadAddrOffset = 0; pOriginalData = (PBYTE)fmimage; DataSize = sizeof (fmimage); while (ReadAddrOffset < DataSize) { if (ReadAddrOffset + FLASH_PAGE_SIZE < DataSize) usLength = FLASH_PAGE_SIZE; else usLength = DataSize - ReadAddrOffset; Flash_Read(AddrBase + ReadAddrOffset, ucBuffer, usLength); for (i = 0; i < usLength; i ++) { if (ucBuffer[i] != pOriginalData[ReadAddrOffset + i]) break; } if (i != usLength) TRACE("Data Verify failed1!2\r\n"); ReadAddrOffset += usLength; } } } while (1); }
/******************************************************************************* 函 数 名: Program_Write_Ext 功能说明: 带扩展的写入编程 参 数: addr: 写入数据的首地址 size: 要写入数据的大小 data: 写入数据内容 返 回 值: 写入结果TRUE/FALSE *******************************************************************************/ u8 Program_Write_Ext(u32 addr, u32 len, u8 *data) /**> 增加flash编程扩展接口,Jay Add 2015.11.13 **/ { u8 flag; u32 secpos; /**> 扇区地址 **/ u16 secoff; /**> 扇区内偏移地址 **/ u16 secremain; /**> 扇区内剩余地址 **/ u16 i; u32 offaddr; /**> 去掉0X08000000后的地址 **/ u8 STMFLASH_BUF[STM_SECTOR_SIZE]; if (len & 0x01) /**> 内部为半字节写入 **/ { len++; } Flash_Unlock(); if((addr < STM32_FLASH_BASE)|| /**> 若地址越界,直接返回错误 **/ (addr >= (STM32_FLASH_BASE+1024*STM_SECTOR_SIZE))) return 0; offaddr = addr-STM32_FLASH_BASE; /**> 实际偏移地址 **/ secpos = offaddr/STM_SECTOR_SIZE; /**> 扇区地址 0~127 for STM32F103cBT6 **/ secoff = offaddr%STM_SECTOR_SIZE; /**> 在扇区内的偏移 **/ secremain = STM_SECTOR_SIZE-secoff; /**> 扇区剩余空间大小 **/ if(len <= secremain) secremain = len; /**> 不大于该扇区范围 **/ while(1) { Program_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE, /**> 读出整个扇区的内容 **/ STM_SECTOR_SIZE, STMFLASH_BUF); for(i=0;i<secremain;i++) /**> 校验数据 **/ { if(0xff != STMFLASH_BUF[secoff+i]) break; } if(i<secremain) /**> 需要擦除 **/ { flag=FLASH_ErasePage(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE); for(i=0;i<secremain;i++) { STMFLASH_BUF[i+secoff] = data[i]; } flag=Flash_Write(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE, STM_SECTOR_SIZE, STMFLASH_BUF); }else flag=Flash_Write(addr,secremain,data); /**> 写已经擦除了的,直接写入扇区剩余区间**/ if(len==secremain)break; /**> 写入结束了 **/ else /**> 写入未结束 **/ { secpos++; /**> 扇区地址增1 **/ secoff=0; /**> 偏移位置为0 **/ data += secremain; /**> 指针偏移 **/ addr += secremain; /**> 写地址偏移 **/ len -= secremain; /**> 字节数递减 **/ if(len>(STM_SECTOR_SIZE)) secremain=STM_SECTOR_SIZE;/**> 下一个扇区还是写不完 **/ else secremain = len; /**> 下一个扇区可以写完了 **/ } }; Flash_lock(); return flag; }
int Flash_Test_Write(void){ slave_down(); Flash_Write(0x06); slave_up(); _delay_ms(1); slave_down(); _delay_ms(1); Flash_Write(0x02); Flash_Write(0x07); Flash_Write(0x00); Flash_Write(0x00); Flash_String_Push("I Drank What? -Socrates"); slave_up(); return 0; }
BYTE UpLoad2Flash( BYTE * buffer, BYTE len ){ DWORD dwActualBootLoaderUpLoadAddr; //! last uploaded address into the firmware //! calcula o CRC32 do buffer recebido if ( e2prom_read( (BYTE*)&dwCRC32, BOOT_LDR_CRC32_ADDR, sizeof( DWORD )) == FALSE ){ return BOOT_LOADER_E2PROM_ERR; } dwCRC32 = crc32c( dwCRC32, buffer, len ); if ( e2prom_write( (BYTE*)&dwCRC32, BOOT_LDR_CRC32_ADDR, sizeof( DWORD )) == FALSE ){ return BOOT_LOADER_E2PROM_ERR; } if ( e2prom_read( (BYTE*)&dwActualBootLoaderUpLoadAddr, BOOT_LDR_PCK_CNTRL_ADDR, sizeof( DWORD )) == FALSE ){ return BOOT_LOADER_E2PROM_ERR; } // salva o buffer recebido na Flash EEPROM if ( Flash_Write( buffer, FEE_BOOTLOADER + dwActualBootLoaderUpLoadAddr, len ) != len ){ return BOOT_LOADER_FLASH_ERR; } dwActualBootLoaderUpLoadAddr += len; if ( e2prom_write( (BYTE*)&dwActualBootLoaderUpLoadAddr, BOOT_LDR_PCK_CNTRL_ADDR, sizeof( DWORD )) == FALSE ){ return BOOT_LOADER_E2PROM_ERR; } return TRUE; }
/** * @brief main function of flash demo * @param None. * @retval result to return to system. */ int main(void) { #define ERROR_DELAY 1000000U #define SUCCESS_DELAY 4000000U Result tmp = ERROR; uint8_t Flag_1st = 0U; LED_Init(); SW_Init(); while (1U) { if(Flag_1st == 0U && SW_Get(SW0)) { /* if SW0 is turned on 1st time*/ Flag_1st = 1U; Copy_Routine(FLASH_API_RAM, FLASH_API_ROM, SIZE_FLASH_API); tmp = Flash_Write(); } else { /* Do nothing */ } if(tmp== SUCCESS) { LED_On(LED1); delay(SUCCESS_DELAY); LED_Off(LED1); delay(SUCCESS_DELAY); } else { LED_On(LED1); delay(ERROR_DELAY); LED_Off(LED1); delay(ERROR_DELAY); } } }
void Schedule_Init(void) { Flash_Init(CLOCK_FREQ_MHZ); ScheduleRegistry = (unsigned long *) SCHEDULE_REGISTRY_ADDR; ScheduleRegistrySize = 0; // look for current schedule (if one exists!) while((ScheduleRegistry[ScheduleRegistrySize] != EMPTY_SCHEDULE_ENTRY) && (ScheduleRegistrySize < 256)) { ScheduleRegistrySize++; } // check if first time loading a schedule if((ScheduleRegistrySize % SCHEDULE_REGISTRY_SIZE) == 0) { Flash_Erase(SCHEDULE_REGISTRY_ADDR); Flash_Erase(SCHEDULE_BUFFER_A_ADDR); Flash_Write(SCHEDULE_REGISTRY_ADDR, SCHEDULE_BUFFER_A_ADDR); Schedule = (schedule_entry_t *) SCHEDULE_BUFFER_A_ADDR; TempSchedule = (schedule_entry_t *) SCHEDULE_BUFFER_B_ADDR; ScheduleSize = 0; TempScheduleSize = 0; ScheduleRegistrySize = 1; } else { Schedule = (schedule_entry_t *) ScheduleRegistry[ScheduleRegistrySize-1]; ScheduleSize = TempScheduleSize; TempScheduleSize = 0; if(Schedule == (schedule_entry_t *) SCHEDULE_BUFFER_A_ADDR) { TempSchedule = (schedule_entry_t *) SCHEDULE_BUFFER_B_ADDR; } else { TempSchedule = (schedule_entry_t *) SCHEDULE_BUFFER_A_ADDR; } } }
void Flash_Data_Reset(void){ U16 i; for(i=0;i<DATA_NUM;i++){ flashData[i] = data_initial[i]; } Flash_Write(0); }
void Flash_String_Push(uint8_t *s){ UART1_Flush(); while (*s) { //point to the string array //UART1_TX(*s); Flash_Write(*s); //transmit current element s++; //increment element, rinse, repeat until dry. } }
/* ** Receive data from the Host and save to flash */ void OnFlashImgDowloadFrame( Dci_Context* pctxt) { byte buff[MAX_MSG_SIZE]; byte *pdata; int lenMsg, i, nErr, ec = 0; byte idStatus; long addr; ushort ctPad = 0; Dci_BinaryImageTransferFrame* pBitf = (Dci_BinaryImageTransferFrame*) pctxt->pMsg; // check transfer id if (!IsTransferring( pBitf->idTransfer ) ) return; // Check frame id and set up status. idStatus = (pBitf->idFrame != s_idFrame) ? BSE_FrameError : ( s_idFrame < (s_flashHdr.bitParams.ctFrames-1)) ? BSE_ReadyNextFrame : BSE_TransferComplete; /*---- write frame to EEPROM ----*/ addr = FLASH_IMAGE_OFFSET + (pBitf->idFrame * s_flashHdr.bitParams.sizeFrame); pdata = Dci_BinaryImageTransferFrame_GetData(pBitf); //Pad the frame with zero so we have writing on even boundaries. if( pBitf->ctBytes < s_flashHdr.bitParams.sizeFrame) { //Calculate the number of bytes to pad out to boundary. ctPad = FLASH_FRAME_SIZE - (pBitf->ctBytes & (FLASH_FRAME_SIZE-1)); memset( pdata + pBitf->ctBytes, 0, ctPad); //zero out remaining elements. } //Write the data and/or any padded bytes to the flash. ec = Flash_Write( addr, pBitf->ctBytes + ctPad, pdata); if( ec ) { idStatus = BSE_WriteError; ResetTransfer(); } //Send the current status so process can continue. lenMsg = Dci_BinaryImageTransferStatus_Init( buff, WCACOMP_FLASH, pBitf->idTransfer, s_idFrame, pBitf->ctBytes, idStatus); Dciproc_SendMessage1(buff, lenMsg, false, pctxt); if( !ec ) ++s_idFrame; // If transfer complete finish writing whole block and header. if( idStatus == BSE_TransferComplete ) { //Write Flash Header with calculated check sum value. s_flashHdr.crc = 0x0; //TODO Calculate Checksum Flash_Hdr_Write(&s_flashHdr); } pctxt->bHandled = true; }
uint8_t Flashmem_ID(void){ uint8_t dev_ID = 0; //uint8_t man_ID = 0; slave_down(); _delay_ms(1); Flash_Write(0x90); Flash_Write(0x00); Flash_Write(0x00); Flash_Write(0x01); Flash_Write(0x00); dev_ID = SPDR; //man_ID = SPDR; slave_up(); return dev_ID; //return man_ID; }
//------------Scoreboard_Record------------ // Record a score in the scoreboard, regardless of whether or // not the numerical score is high enough to be shown at the top. // Input: first first initial // middle middle initial // last last initial // score numerical score earned in the game // Output: none void Scoreboard_Record(char first, char middle, char last, uint32_t score){ int i, j; // compare the score with the RAM scoreboard so far for(i=0; i<SCOREBOARDSIZE; i=i+1){ if(score > RAMScoreboard[i].score){ // found a score better than one in RAM // shift all lower scores down for(j=(SCOREBOARDSIZE-1); j>i; j=j-1){ RAMScoreboard[j].first = RAMScoreboard[j-1].first; RAMScoreboard[j].middle = RAMScoreboard[j-1].middle; RAMScoreboard[j].last = RAMScoreboard[j-1].last; RAMScoreboard[j].score = RAMScoreboard[j-1].score; } // insert the new score in the RAM scoreboard RAMScoreboard[i].first = first; RAMScoreboard[i].middle = middle; RAMScoreboard[i].last = last; RAMScoreboard[i].score = score; // do not over-write all lower scores break; } } if((boardptr <= (uint32_t *)(scoreblock + 0x3F8))){ // there is still room in the block to hold more scores // append the new score to the end of the scores in flash Flash_Write((uint32_t)boardptr, (first<<24)|(middle<<16)|(last<<8)); Flash_Write((uint32_t)(boardptr + 1), score); boardptr = boardptr + 2; } else{ // there is no more room in the block to hold more scores // clear the block Flash_Erase(scoreblock); boardptr = (uint32_t *)scoreblock; // store the top 'SCOREBOARDSIZE' scores from the RAM buffer for(i=0; i<SCOREBOARDSIZE; i=i+1){ Flash_Write((uint32_t)boardptr, (RAMScoreboard[i].first<<24)|(RAMScoreboard[i].middle<<16)|(RAMScoreboard[i].last<<8)); Flash_Write((uint32_t)(boardptr + 1), RAMScoreboard[i].score); boardptr = boardptr + 2; } } }
/*! ****************************************************************************** ** \brief set flash back to read or reset mode ** ** \param pResetSecAddr ** \arg operate address ** ** \return None ** ****************************************************************************** */ void WFlash_ReadResetCmd(uint16_t* pResetSecAddr) { WFlash_SetRunMode(WFLASH_PGM_MODE); WFlashDummy(WFlash_GetRunMode()); /* issue read/reset command */ Flash_Write(pResetSecAddr, FLASH_READ_RESET) ; WFlash_SetRunMode(WFLASH_ROM_MODE); WFlashDummy(WFlash_GetRunMode()); WFlashDummy(Flash_Read(pResetSecAddr)); }
uint8_t Flash_Test_Read(void){ uint8_t num_bytes = 4; while(!(SPSR & 0x80)); slave_down(); Flash_Write(0x03); Flash_Write(0x04); Flash_Write(0x00); Flash_Write(0x00); int count = 0; uint8_t letters[num_bytes]; while(count != (num_bytes+1)){ Flash_Write(0x00); letters[count] = SPDR; count++; } slave_up(); _delay_ms(1); uint8_t *letts = letters; UART1_Print(letts); return SPDR; // may not make sense to return anything here. Change this. }
/******************************************************************************* 函 数 名: Program_Write 功能说明: 写入编程 参 数: addr: 写入数据的首地址 size: 要写入数据的大小 data: 写入数据内容 返 回 值: 写入结果TRUE/FALSE *******************************************************************************/ u8 Program_Write(u32 addr, u32 len, u8 *data) { u8 flag; if (len & 0x01) //内部为半字节写入 { len++; } Flash_Unlock(); flag = Flash_Write(addr, len, data); Flash_lock(); return flag; }
/******************************************************************************* 函 数 名: Program_Write 功能说明: 写入编程 参 数: addr: 写入数据的首地址 size: 要写入数据的大小 data: 写入数据内容 返 回 值: 写入结果TRUE/FALSE *******************************************************************************/ BF_BOOLEAN Program_Write(BF_INT32U addr, BF_INT32U len, BF_INT08U *data) { BF_INT08U flag; if (len & 0x01) //内部为半字节写入 { len++; } Flash_Unlock(); flag = Flash_Write(addr, len, data); Flash_lock(); return flag; }
void Schedule_Refresh(void) { if(ScheduleRegistrySize == 256) { Flash_Erase(SCHEDULE_REGISTRY_ADDR); ScheduleRegistrySize = 0; } Flash_Write((unsigned long) &ScheduleRegistry[ScheduleRegistrySize], (unsigned long) TempSchedule); TempSchedule = Schedule; Schedule = (schedule_entry_t *) ScheduleRegistry[ScheduleRegistrySize]; ScheduleRegistrySize++; ScheduleSize = TempScheduleSize; TempScheduleSize = 0; }
FLASH_STATUS Index_Save(void) { lock_infor_t temp; temp = (*(lock_infor_t*)INDEX_ADDR_START); if(temp.flag!=0xffff) { FLASH_Unlock(); FLASH_ErasePage(INDEX_ADDR_START); FLASH_Lock(); } lock_infor.flag =0x01; return Flash_Write(INDEX_ADDR_START, (uint32_t*)&lock_infor, sizeof(lock_infor_t)); }
void TaskProtocol(void *p_arg) { MSG_t msg; INT8U err , i; IDPROTOCOL_t *pProto; // INT8U *p; // SendString("TaskProtocol \n"); for (;;) { msg = (MSG_t)OSQPend(ProtocolMBox, 0, &err); // OSTimeDly(3000); //SendString("taskprotocol"); switch (msg) { // 串口1接收到协议数据 case UART1_BUFF0_RECV: case UART1_BUFF1_RECV: pProto = &gU1RecvBuff[msg - UART1_BUFF0_RECV]; if ((pProto->direction & ID_DESTMASK) == ID_RFIRBOARD) { if (pProto->command == CMD_SETID) { Flash_Write(RFIR_ID_ADDR,pProto->data,4);//读取buf存入FLASH SendString("\n\rID setok:"); Flash_Read(RFIR_ID_ADDR, MY_ID, 4); //读取FLASH ID存入BUF for(i=0;i<4;i++)//sizeof(ID) { SendString(";"); Outint(MY_ID[i]); } } } // 查看是否需要应答 if (IS_PROTO_NEED_ACK(pProto)) { // 返回应答数据 SendIDAck(USART1, pProto); } break; default: break; } } }
void WriteLog2Flash( struct LogBuffer * logPtr ){ BYTE bLen; DWORD dwAddr; logPtr->wStamp = wLogStamp++; e2prom_write( (BYTE*)&wLogStamp, LOG_STAMP_ADDR, sizeof( WORD )); bLen = logPtr->bLen; bLen += (sizeof( logPtr->bCode ) + sizeof( logPtr->wStamp ) + sizeof( logPtr->bLen )); dwAddr = logPtrEnd * sizeof ( struct LogBuffer ); dwAddr += FEE_LOG_ADDR; Flash_Write( (BYTE*)logPtr, dwAddr , bLen ); IncLogPtrEnd( ); return; }
int Flash_Byte_Write(uint8_t *bytes, uint8_t addr){ uint8_t index; uint8_t buf[8]; for(index=0;index<5; index++){ UART1_Print(itoa(*(bytes+index), buf, 16)); } slave_down(); Flash_Write(0x06); slave_up(); _delay_ms(1); slave_down(); _delay_ms(1); Flash_Write(0x02); Flash_Write(addr); Flash_Write(0x00); Flash_Write(0x00); for(index=0; index<5; index++){ Flash_Write(*(bytes+index)); } slave_up(); _delay_ms(1); return 0; }
// bQuick=TRUE: just check first and last block bool FLASH_Verify(char *pFlashName, alt_u8 InitValue, bool bShowMessage, bool bQuickVerify){ bool bPass = TRUE; int i, k, BlockNum; FLASH_HANDLE hFlash; alt_u32 Offset, Size; alt_u8 *pBuf, Cnt; const int nBufSize = 8*1024; // 16K int nWriteSizeSum, nWriteSize; int nReadSizeSum, nReadSize; hFlash = Flash_Open(pFlashName); if (!hFlash){ if (bShowMessage) printf("Failed to open flash.\r\n"); return FALSE; } BlockNum = Flash_GetBlockCount(hFlash); //===== alloc buffer pBuf = (alt_u8 *)malloc(nBufSize); if (!pBuf){ if (bShowMessage) printf("[Error] Failed to alloc memory.\r\n"); return FALSE; } //===== erase for(i=0;i<BlockNum && bPass;i++){ if (bQuickVerify && (i !=0 )&& (i != (BlockNum-1))) continue; bPass = Flash_Erase(hFlash, i); if (bShowMessage){ if (!bPass) printf("[Error] Failed to erase flash block %d/%d\r\n.\r\n", i, BlockNum); else printf("Erase Block %d/%d\r\n", i, BlockNum); } } //===== write if (bPass){ Cnt = InitValue; for(i=0;i<BlockNum && bPass;i++){ if (bQuickVerify && (i !=0 )&& (i != (BlockNum-1))) continue; bPass = Flash_GetBlockInfo(hFlash, i, &Offset, &Size); if (!bPass) continue; if (bShowMessage) printf("Write Block[%d/%d], size=%d\r\n", i, BlockNum, (int)Size); nWriteSizeSum = 0; while(nWriteSizeSum < Size && bPass){ // cal write size nWriteSize = nBufSize; if (nWriteSize > (Size - nWriteSizeSum)) nWriteSize = Size - nWriteSizeSum; // fill data for(k=0;k<nWriteSize;k++){ *(pBuf+k) = Cnt++; } // write data block bPass = Flash_Write(hFlash, Offset+nWriteSizeSum, pBuf, nWriteSize); if (bShowMessage && !bPass) printf("[Error] Write Block[%d/%d] NG\r\n", i, BlockNum); // usleep(20*1000); // nWriteSizeSum += nWriteSize; } } } if (bPass){ if (bShowMessage) printf("alt_dcache_flush_all\r\n"); alt_dcache_flush_all(); } //===== read & verify if (bPass){ Cnt = InitValue; for(i=0;i<BlockNum && bPass;i++){ if (bQuickVerify && (i !=0 )&& (i != (BlockNum-1))) continue; bPass = Flash_GetBlockInfo(hFlash, i, &Offset, &Size); if (!bPass){ if (bShowMessage) printf("[Error] Flash_GetBlockInfo at block %d\r\n", i); continue; } if (bShowMessage) printf("Read Block[%d/%d], size=%d\r\n", i, BlockNum, (int)Size); nReadSizeSum = 0; while(nReadSizeSum < Size && bPass){ // cal write size nReadSize = nBufSize; if (nReadSize > (Size - nReadSizeSum)) nReadSize = Size - nReadSizeSum; // read data block bPass = Flash_Read(hFlash, Offset+nReadSizeSum, pBuf, nReadSize); if (!bPass){ if (bShowMessage) printf("[Error] Flash_Read fail at block-offset %d-%d\r\n", i, (int)Offset+nReadSizeSum); }else{ // verify // verify data for(k=0;k<nReadSize && bPass;k++){ if (*(pBuf+k) != Cnt){ if (bShowMessage) printf("[Error] Verify fail, block:%d, index:%d, read=%Xh, expected=%Xh\r\n", i, nReadSizeSum+k, *(pBuf+k), Cnt); bPass = FALSE; }else{ Cnt++; } } } // nReadSizeSum += nReadSize; } } } if (pBuf) free(pBuf); if (hFlash) Flash_Close(hFlash); // return bPass; }
FLASH_STATUS id_infor_Save(uint8_t id, id_infor_t id_struct) { id_infor_t id_data, *p; uint32_t sector_addr,addr; uint16_t i; if((id>=1)&&(id<=32)) { sector_addr = USER_ID_PAGE0_ADDR_START; addr = USER_ID_PAGE0_ADDR_START + (id-1) * sizeof(id_infor_t); } else if((id>=33)&&(id<=64)) { sector_addr = USER_ID_PAGE1_ADDR_START; addr = USER_ID_PAGE0_ADDR_START + (id-1) * sizeof(id_infor_t); } else if((id>=65)&&(id<=95)) { sector_addr = USER_ID_PAGE2_ADDR_START; addr = USER_ID_PAGE0_ADDR_START + (id-1) * sizeof(id_infor_t); } else if((id>=96)&&(id<=99)) { sector_addr = ADMIN_ID_PAGE4_ADDR_START; addr = ADMIN_ID_PAGE4_ADDR_START + (id-96)* sizeof(id_infor_t); } else return SAVE_FAIL; id_data = *(id_infor_t*)addr; if(id_data.flag==0xffff) { id_struct.flag = 0x01; // FLASH_ProgramWord(addr, addr); id_infor_Write(addr, id_struct); return SAVE_OK; } else { p = (id_infor_t*)sector_addr; if((id>=1)&&(id<=95)) { for(i=0; i<32; i++) { Sector_data[i] = *p; p++; } FLASH_Unlock(); FLASH_ErasePage(sector_addr); FLASH_Lock(); Sector_data[id-1] = id_struct; Sector_data[id-1].flag = 0x01; return Flash_Write(sector_addr, (uint32_t*)Sector_data ,sizeof(id_infor_t)*32); } else { for(i=0; i<4; i++) { Sector_data[i] = *p; p++; } FLASH_Unlock(); FLASH_ErasePage(sector_addr); FLASH_Lock(); Sector_data[id-96] = id_struct; Sector_data[id-96].flag = 0x01; return Flash_Write(sector_addr, (uint32_t*)Sector_data ,sizeof(id_infor_t)*4); } // return SAVE_OK; } }
static FLASH_STATUS id_infor_Write(uint32_t addr, id_infor_t id_data) { return Flash_Write( addr, (uint32_t*)&id_data, sizeof(id_infor_t)); }
/** * Move_Image_To_Boot_Loc * * Move the image at imgAddr to the boot location. This * function holds a copy of the vector table. If incase there is * a failure then it will try to replace the vector table to * save the boot loader. * * @param imgAddr Starting address of the image to be moved. * @param imgSiz Size of the image at imgAddr. * * @return SUCCESS | FAIL */ result_t Move_Image_To_Boot_Loc (uint32_t imgAddr, uint32_t imgSiz) { result_t ret; uint32_t ImgFlashAddr = imgAddr + 32; /*Drop the vec table*/ uint32_t CSize= 0x8000; uint32_t RSize = 0; uint32_t CurSize = 0x20; uint32_t BootAddr = 0x0; uint32_t AddVec = 0x0; Flash_Read (0x0, 32, vecbuffer); /* Keep this for recovery purpose*/ if (Flash_Param_Partition_Erase (BootAddr) == SUCCESS) { if (Flash_Write (BootAddr, vecbuffer, 0x20) == FAIL) { memcpy (cpybuffer, vecbuffer, 0x20); AddVec = 0x20; } else BootAddr = 0x20; } ret = __Binary_Erase (CurrImageAddr, CurrImageSize, vecbuffer); if (Flash_Read (ImgFlashAddr, (CSize - CurSize), (cpybuffer + AddVec)) == SUCCESS) { if (Flash_Write (BootAddr, cpybuffer, (0x7FE0 + AddVec)) == SUCCESS) { CurSize += (0x8000 - 0x20); ImgFlashAddr += (0x8000 - 0x20); BootAddr += (0x8000 - 0x20); } else { if (Flash_Write (BootAddr, vecbuffer, 0x20) == FAIL) { TOGGLE_LED (RED); while (1); } else return FAIL; } } while (CurSize < imgSiz) { RSize = ((imgSiz - CurSize) > CSize)? CSize : (imgSiz - CurSize); if (Flash_Read (ImgFlashAddr, RSize, cpybuffer) == SUCCESS) { if (Flash_Write (BootAddr, cpybuffer, 0x8000) == SUCCESS) { CurSize += RSize; ImgFlashAddr += RSize; BootAddr += RSize; } else return FAIL; } } return SUCCESS; }
void clearAppVersion() { memset(Bp.appVersion,0,16); Flash_Write(BootPrarmeterAddr,(u8*)&Bp,sizeof(Bp)); }
/** * Write Header to Flash Page-0. **/ int Flash_Hdr_Write(Flash_Hdr *pHdr ) { return Flash_Write(0x0, sizeof( Flash_Hdr), (byte*)pHdr); }
/******************************************************************************** * FunctionName: Tasktest * * Description : 协议发送任务 * * Parameters : * * Returns : None. *******************************************************************************/ void TaskTest(void *p_arg) { INT8U err ; // u16 i; // unsigned char buf[]="0123456789abcdefghijklmnopqrstuvwxyz"; INT32U msg; u8 send_sig=0; //发送信号 标志位 1:学习成功 2:学习失败 3:正在学习中 p_arg = p_arg; for (;;) { /********发射4432数据********/ msg = (INT32U)OSQPend(TestMBox, 0, &err); send_sig= msg; if(send_sig!=0) { WatchDog_Feed(); //喂狗 if(send_sig==1) { if (Learn_State==1)//空调学习成功 { WatchDog_Feed(); //喂狗 Send_LenStop(USART2);//停止学习 WatchDog_Feed(); //喂狗 Flash_Write(ARC_ADDR,u2p->data,208);//读取buf存入FLASH 存入固定器件地址的键值上ARC1_ADDR+learn_DeviceName*2080+learn_KeyCode*208 learn_cmd=0;//清空开始学习标志? Learn_State=0; // SendString(" 空调学习成功!"); } else if(Learn_State==2)//电视学习成功 { WatchDog_Feed(); //喂狗 Send_LenStop(USART2);//停止学习 WatchDog_Feed(); //喂狗 Flash_Write(URC_ADDR,u2p->data,208);//读取buf存入FLASH learn_cmd=0;//清空开始学习标志? Learn_State=0; // SendString(" 电视学习成功!"); } Send_4432ID0 = MY_ID[0];// my id Send_4432ID1 = MY_ID[1]; Send_4432ID2 = MY_ID[2]; Send_4432ID3 = MY_ID[3]; Send_4432DIR = 0x82 ;//方向 Send_4432CMD = 0x34 ;//反馈指令 Send_4432LENGTH= 3; //数据长度 Send_4432DATA0 = send_sig;//学习成功 Send_4432DATA1 = learn_DeviceName;// Send_4432DATA2 = learn_KeyCode;// Data_Encrypt(si4432cmd_senddata,18); // WatchDog_Feed(); //喂狗 sending(send_data,Send_Legth); WatchDog_Feed(); //喂狗 OSTimeDly(8); WatchDog_Feed(); //喂狗 sending(send_data,Send_Legth); WatchDog_Feed(); //喂狗 } else if(send_sig==2) { learn_cmd=0;//清空开始学习标志? SendString(" 学习失败!"); Send_4432ID0 = MY_ID[0];// my id Send_4432ID1 = MY_ID[1]; Send_4432ID2 = MY_ID[2]; Send_4432ID3 = MY_ID[3]; Send_4432DIR = 0x82 ;//方向 Send_4432CMD = 0x34 ;//反馈指令 Send_4432LENGTH= 3; //数据长度 Send_4432DATA0 = send_sig;//学习成功 Send_4432DATA1 = learn_DeviceName;// Send_4432DATA2 = learn_KeyCode;// Data_Encrypt(si4432cmd_senddata,18); // WatchDog_Feed(); //喂狗 sending(send_data,Send_Legth); WatchDog_Feed(); //喂狗 OSTimeDly(5); WatchDog_Feed(); //喂狗 sending(send_data,Send_Legth); WatchDog_Feed(); //喂狗 } else if(send_sig==3) SendString(" 正在学习!"); else { learn_cmd=0;//清空开始学习标志? SendString(" 其他错误= "); Send_4432ID0 = MY_ID[0];// my id Send_4432ID1 = MY_ID[1]; Send_4432ID2 = MY_ID[2]; Send_4432ID3 = MY_ID[3]; Send_4432DIR = 0x82 ;//方向 Send_4432CMD = 0x34 ;//反馈指令 Send_4432LENGTH= 3; //数据长度 Send_4432DATA0 = send_sig;//学习成功 Send_4432DATA1 = learn_DeviceName;// Send_4432DATA2 = learn_KeyCode;// Data_Encrypt(si4432cmd_senddata,18); // WatchDog_Feed(); //喂狗 sending(send_data,Send_Legth); WatchDog_Feed(); //喂狗 OSTimeDly(5); WatchDog_Feed(); //喂狗 sending(send_data,Send_Legth); WatchDog_Feed(); //喂狗 // Outint(send_sig); } //if((send_sig==1)||(send_sig==2))//如果学习成功或失败 清空设备号和 键值 发送学习停止信号 if (send_sig!=3) { //Send_LenStop(USART2); learn_DeviceName = 0; learn_KeyCode = 0; learn_cmd=0;//清空开始学习标志位 } send_sig=0; } //else //接收4432信号send_sig==0 } }
/******************************************************************************** * FunctionName: TaskSet * * Description : 设置任务,设置完删除任务 * * Parameters : None. * * Returns : None. *******************************************************************************/ void TaskSet(void *p_arg) { unsigned char k, data_flash_temp[208]={0xAE, 0x35, 0x24, 0x70, 0x80, 0xB7, 0x80, 0xBA, 0x1F, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x80, 0x9B, 0x1D, 0x5C, 0x1B, 0x5E, 0x02, 0x77, 0x02, 0xED, 0xEF, 0x80, 0xB8, 0x80, 0xB9, 0x1D, 0x5C, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1F, 0x80, 0x9B, 0x1D, 0x5C, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x80, 0x9B, 0x1D, 0x80, 0x9A, 0x1E, 0x80, 0xF5, 0x1E, 0x8F, 0x70, 0x02, 0xEA, 0x8C, 0x80, 0xB8, 0x80, 0xB9, 0x1D, 0x5C, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x80, 0x9A, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x80, 0x9B, 0x1D, 0x80, 0x9A, 0x1E, 0x80, 0xF5, 0x1E, 0x85, 0x88, 0x80, 0xB8, 0x80, 0xB8, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x5B, 0x1E, 0x80, 0x9A, 0x1E, 0x5C, 0x1E, 0x5B, 0x1F, 0x5B, 0x1E, 0x5B, 0x1F, 0x5B, 0x1F, 0x5B, 0x1E, 0x5B, 0x1E, 0x80, 0x9B, 0x15, 0x80, 0xA4, 0x02, 0x86, 0xB7, 0x04, 0x81, 0x6D, 0x02, 0x77, 0x02, 0x77, 0x02, 0x77, 0x02, 0x77, 0x02, 0x77, 0x02, 0x77, 0x02, 0xAF, 0xFB, 0x02, 0x77, 0x1F, 0x5B, 0x1F, 0x5B, 0x1E, 0x5B, 0x1E, 0x80, 0x9B, 0x1D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD7}; // INT8U i; u32 seed; u8 random_data,i; //INT32U msg = 0; //INT8U err = 0; OS_CPU_SR cpu_sr = 0; p_arg = p_arg; DelayMs(30); OS_ENTER_CRITICAL(); BSP_Init(); Flash_Read(RFIR_ID_ADDR, MY_ID, 4); //读取FLASH ID存入BUF #if 0 SendString("\n\rMY ID:"); for(i=0;i<4;i++)//sizeof(ID) { SendString(";"); Outint(MY_ID[i]); } #endif /******************************************************************************** * Description : 任务建立 共9个任务 *******************************************************************************/ CT361SndErrSemp = OSSemCreate(0);//creat sem //创建协议解析任务 OSTaskCreate(TaskProtocol, (void*)0, &TaskProtocolStk[TASK_PROTOCOL_STKSIZE-1], TASK_PROTOCOL_PRIO); //创建串口发送任务 OSTaskCreate(TaskProtoSend, (void*)0, &TaskProtoSendStk[TASK_PROTO_SEND_STKSIZE-1], TASK_PROTO_SEND_PRIO ); //创建量产测试任务 OSTaskCreate(TaskTest, (void*)0, &TaskTestStk[TASK_TEST_STKSIZE-1], TASK_TEST_PRIO ); //创建4432接收任务 OSTaskCreate(TaskSi4432Read, (void*)0, &TaskSi4432ReadStk[TASK_SET_STKSIZE-1], TASK_Si4432_READ_PRIO ); //创建返回码接收任务 OSTaskCreate(TaskCT361SndErr,\ (void*)0,\ &TaskCT361SndErrStk[TASK_CT361_SND_ERR_STKSIZE-1],\ TASK_CT361_SND_ERR_PRIO); /******************************************************************************** * Description : 创建消息队列 *******************************************************************************/ // SetMBox = OSQCreate(&SetQBuff[0], TASK_SET_QSIZE); ProtocolMBox = OSQCreate(&ProtocolQBuff[0], TASK_PROTOCOL_QSIZE); ProtoSendMBox = OSQCreate(&ProtoSendQBuff[0], TASK_PROTOSEND_QSIZE); TestMBox = OSQCreate(&TestQBuff[0], TASK_TEST_QSIZE); Si4432ReadMBox = OSQCreate(&Si4432ReadQBuff[0], TASK_Si4432READ_QSIZE); OS_EXIT_CRITICAL(); SysTickInit(); //自我删除 // OSTaskDel(OS_PRIO_SELF); /*自我删除函数会自动进行任务的调度,因此此处不需要添加延时*/ Flash_Write(0x800D678,data_flash_temp,208); for (;;) { /**********学习状态指示灯********/ if(learn_cmd==1) //学习开始标志位 { LED_Send(0);// 绿灯关闭 LED_Learn(1);//红灯闪烁 WatchDog_Feed(); //喂狗 OSTimeDly(4); WatchDog_Feed(); //喂狗 LED_Learn(0); WatchDog_Feed(); //喂狗 OSTimeDly(4); WatchDog_Feed(); //喂狗 Timout_Count--; if(Timout_Count==0) { Timout_Count=TIMOUT_20s; OSQPost(TestMBox, (void*)Stu_Fail); //学习失败信号 learn_cmd=0; //清空学习成功标志位 } } if(learn_cmd==0) //学习成功标志位 { LED_Learn(0);//红灯关闭 LED_Send(1); //绿灯开启 } /**********生成随机滚动序列*********/ seed+=23; if(seed>65000) seed=0; srand(seed); random_data=rand(); test_roll_list=random_data^0x29; if(learn_cmd==0) { Timout_Count2--; if((Timout_Count2>=40)&&(Timout_Count2<=200)) { LED_Send(0); } else if((Timout_Count2<40)&&(Timout_Count2>=1)) { LED_Send(1); // Timout_Count2=200; } else if(Timout_Count2<=0) Timout_Count2=200; } WatchDog_Feed(); //喂狗 OSTimeDly(1); } }
/******************************************************************************** * FunctionName: Message_Flash_Write * * Description : * * Parameters : * * Returns : *******************************************************************************/ void Message_Flash_Write(int flag_save,uint8_t *message) { if(flag_save == 1) { Flash_Write(Message_Save1,message,Message_Size); } else if(flag_save == 2) { Flash_Write(Message_Save2, message, Message_Size); } else if(flag_save == 3) { Flash_Write(Message_Save3, message, Message_Size); } else if(flag_save == 4) { Flash_Write(Message_Save4, message, Message_Size); } else if(flag_save == 5) { Flash_Write(Message_Save5, message, Message_Size); } else if(flag_save == 6) { Flash_Write(Message_Save6, message, Message_Size); } else if(flag_save == 7) { Flash_Write(Message_Save7, message, Message_Size); } else if(flag_save == 8) { Flash_Write(Message_Save8, message, Message_Size); } else if(flag_save == 9) { Flash_Write(Message_Save9, message, Message_Size); } else if(flag_save == 10) { Flash_Write(Message_Save10, message, Message_Size); } else if(flag_save == 11) { Flash_Write(Message_Save11, message, Message_Size); } else if(flag_save == 12) { Flash_Write(Message_Save12, message, Message_Size); } else if(flag_save == 13) { Flash_Write(Message_Save13, message, Message_Size); } else if(flag_save == 14) { Flash_Write(Message_Save14,message,Message_Size); } else if(flag_save == 15) { Flash_Write(Message_Save15, message, Message_Size); } else if(flag_save == 16) { Flash_Write(Message_Save16, message, Message_Size); } else if(flag_save == 17) { Flash_Write(Message_Save17, message, Message_Size); } else if(flag_save == 18) { Flash_Write(Message_Save18, message, Message_Size); } else if(flag_save == 19) { Flash_Write(Message_Save19, message, Message_Size); } else if(flag_save == 20) { Flash_Write(Message_Save20, message, Message_Size); } #if 0 switch(flag_save) { case 1: Flash_Write(Message_Save1,message,Message_Size); break; case 2: Flash_Write(Message_Save2,message,Message_Size); break; case 3: Flash_Write(Message_Save3,message,Message_Size); break; case 4: Flash_Write(Message_Save4,message,Message_Size); break; case 5: Flash_Write(Message_Save5,message,Message_Size); break; case 6: Flash_Write(Message_Save6,message,Message_Size); break; case 7: Flash_Write(Message_Save7,message,Message_Size); break; case 8: Flash_Write(Message_Save8,message,Message_Size); break; case 9: Flash_Write(Message_Save9,message,Message_Size); break; case 10: Flash_Write(Message_Save10,message,Message_Size); break; case 11: Flash_Write(Message_Save11,message,Message_Size); break; case 12: Flash_Write(Message_Save12,message,Message_Size); break; case 13: Flash_Write(Message_Save13,message,Message_Size); break; case 14: Flash_Write(Message_Save14,message,Message_Size); break; case 15: Flash_Write(Message_Save15,message,Message_Size); break; case 16: Flash_Write(Message_Save16,message,Message_Size); break; case 17: Flash_Write(Message_Save17,message,Message_Size); break; case 18: Flash_Write(Message_Save18,message,Message_Size); break; case 19: Flash_Write(Message_Save19,message,Message_Size); break; case 20: Flash_Write(Message_Save20,message,Message_Size); break; default: break; } #endif }