#include "hal_flash.h" //flash 1.0V /********************************************************************* * LOCAL VARIABLES */ Flash_t mFlash; FlashBackup_t mBackup; /********************************************************************* * PUBLIC FUNCTIONS */ /****************************************************接口****************************************************/ void Flash_Initialize(void) { uint8_t ret = 0; uint32_t* addr_R = (uint32_t*)START_FSTORAGE_ADDR; Fstorage_FlashInit(); SEGGER_RTT_printf(0,"Flash head read(%04X).\n",*addr_R); if((*addr_R)!=FLASH_FLAG){ SEGGER_RTT_printf(0,"Flash first init(%04X),write infomation to flash.\n",*addr_R); memset((uint8_t*)(&mFlash),0,sizeof(Flash_t)); mFlash.flag = FLASH_FLAG; //重置之后将备份区域的地址拷贝 // Read_N_Byte_flash(FLASH_ADDR_BACKUP,(uint32_t*)(&mBackup),sizeof(FlashBackup_t)); Flash_GetBackup(&mBackup); for(int i=0;i<6;i++){ mFlash.mClient.macAddr[i] = mBackup.macAddr_R[i]; mFlash.macHost[i] = mBackup.macAddr_L[i]; } mFlash.mClient.isConfig = mBackup.isConfig; mFlash.mClient.hardVersion = mBackup.hardVersion; mFlash.mClient.sotfVersion = mBackup.sotfVersion; SEGGER_RTT_printf(0,"backup config:%d,hardVersion:%d,sotfVersion:%d\n",mBackup.isConfig,mBackup.hardVersion,mBackup.sotfVersion); Flash_SaveInfomation(); SEGGER_RTT_printf(0,"Flash head second read(%04X).\n",*addr_R); if((*addr_R)!=FLASH_FLAG){ SEGGER_RTT_printf(0,"Flash write head fail.\n"); app_err_Set(ERR_NUM_FLASH,1); ret = 1; // nrf_delay_ms(30000); // NVIC_SystemReset(); return; } SEGGER_RTT_printf(0,"System reset...\n",*addr_R); nrf_delay_ms(3000); NVIC_SystemReset(); // for(uint32_t i=0;i>8 | step<<8)); //else flash_data &= ((step>>8 | step<<8)<<16 | 0x0000FFFF); flash_data = ((step<<24 & 0xff000000) | (step<<8 & 0x00ff0000) | (step>>8 & 0x0000ff00) | (step>>24 & 0x000000ff)); //写回内容 err_code = Only_Write_N_4Byte_flash(addr , &flash_data, sizeof(flash_data),false); if(err_code != FLASH_OP_SUCCESS)return err_code; mFlash.mStep.num++; } return FLASH_OP_SUCCESS; } /** @brief 删除所有步数 @param 无 @return 错误代码 */ uint32_t Flash_DeleteAllStep(void) { uint32_t err_code; uint16_t PageNum; //获取页号 err_code = GetPageNum(FLASH_ADDR_STEP, &PageNum); if(err_code != FLASH_OP_SUCCESS)return err_code; //擦除一页 return Erase_OnePage(PageNum); } /** @brief 存储基本信息 @param 无 @return 错误代码 */ uint32_t Flash_SaveInfomation(void) { uint32_t err_code; uint16_t PageNum; uint32_t write_addr,len; //获取页号 err_code = GetPageNum(FLASH_ADDR_INFO, &PageNum); if(err_code != FLASH_OP_SUCCESS)return err_code; //查找空闲空间 Find_FreeSpace_In_Page(PageNum, &write_addr, &len); //更新保存基本信息 if(len < sizeof(mFlash)) { Erase_OnePage(PageNum);//擦除一页 write_addr = FLASH_ADDR_INFO; } return Only_Write_N_4Byte_flash(write_addr, (uint32_t*)&mFlash, sizeof(mFlash), false);//直接写 } /** @brief 存储备份信息 @param 无 @return 错误代码 */ uint32_t Flash_SaveBackup(void) { uint32_t err_code; uint16_t PageNum; uint32_t write_addr,len; //获取页号 err_code = GetPageNum(FLASH_ADDR_BACKUP, &PageNum); if(err_code != FLASH_OP_SUCCESS)return err_code; //查找空闲空间 Find_FreeSpace_In_Page(PageNum, &write_addr, &len); //更新保存备份信息 if(len < sizeof(mBackup)) { Erase_OnePage(PageNum);//擦除一页 write_addr = FLASH_ADDR_BACKUP; } return Only_Write_N_4Byte_flash(write_addr, (uint32_t*)&mBackup, sizeof(mBackup), false);//直接写 } /** @brief 获取基本信息 @param 无 @return 错误代码 */ uint32_t Flash_GetInfomation(Flash_t *pflash) { uint32_t err_code; uint16_t PageNum; uint32_t read_addr,len; //获取页号 err_code = GetPageNum(FLASH_ADDR_INFO, &PageNum); if(err_code != FLASH_OP_SUCCESS)return err_code; //查找空闲空间 Find_FreeSpace_In_Page(PageNum, &read_addr, &len); //获取最新的基本信息的地址 read_addr = (read_addr != FLASH_ADDR_INFO)?read_addr - sizeof(Flash_t):FLASH_ADDR_INFO;//read_addr = FLASH_ADDR_INFO的情况属于第一次烧写 return Read_N_Byte_flash(read_addr , (uint32_t*)pflash, sizeof(Flash_t)); } /** @brief 获取备份信息 @param 无 @return 错误代码 */ uint32_t Flash_GetBackup(FlashBackup_t *pbackup) { uint32_t err_code; uint16_t PageNum; uint32_t read_addr,len; //获取页号 err_code = GetPageNum(FLASH_ADDR_BACKUP, &PageNum); if(err_code != FLASH_OP_SUCCESS)return err_code; //查找空闲空间 Find_FreeSpace_In_Page(PageNum, &read_addr, &len); //获取最新的基本信息的地址 read_addr = (read_addr != FLASH_ADDR_BACKUP)?read_addr - sizeof(FlashBackup_t):FLASH_ADDR_BACKUP;//read_addr = FLASH_ADDR_INFO的情况属于第一次烧写 return Read_N_Byte_flash(read_addr , (uint32_t*)pbackup, sizeof(FlashBackup_t)); } /** @brief 保存日志信息 @param[in] id Fault identifier. See @ref NRF_FAULT_IDS. @param[in] pc The program counter of the instruction that triggered the fault, or 0 if unavailable. @param[in] info Optional additional information regarding the fault. The value of the @p id parameter dictates how to interpret this parameter. Refer to the documentation for each fault identifier (@ref NRF_FAULT_IDS and @ref APP_ERROR_FAULT_IDS) for details about interpreting @p info. @return 错误代码 */ uint32_t Flash_SaveLog(uint32_t id, uint32_t pc, uint32_t info) { memset((uint8_t*)(&mFlash.mFlashLog),0,sizeof(FlashLog)); mFlash.mFlashLog.Errorflag =1; switch (id) { #if defined(SOFTDEVICE_PRESENT) && SOFTDEVICE_PRESENT case NRF_FAULT_ID_SD_ASSERT: memcpy(mFlash.mFlashLog.logData,"SD: ASSERTION FAILED",MaxLength(sizeof("SD: ASSERTION FAILED"),sizeof(mFlash.mFlashLog.logData))); break; case NRF_FAULT_ID_APP_MEMACC: memcpy(mFlash.mFlashLog.logData,"SD: INVALID MEMORY ACCESS",MaxLength(sizeof("SD: INVALID MEMORY ACCESS"),sizeof(mFlash.mFlashLog.logData))); break; #endif case NRF_FAULT_ID_SDK_ASSERT: { assert_info_t * p_info = (assert_info_t *)info; sprintf((char *)mFlash.mFlashLog.logData,"ASSERTION FAILED %s:%u", p_info->p_file_name, p_info->line_num); break; } case NRF_FAULT_ID_SDK_ERROR: { error_info_t * p_info = (error_info_t *)info; sprintf((char *)mFlash.mFlashLog.logData,"error:%u,%s:%u", p_info->err_code, p_info->p_file_name, p_info->line_num); SEGGER_RTT_printf(0,">>>>>err code :%d,%s",p_info->err_code,mFlash.mFlashLog.logData); break; } default: sprintf((char *)mFlash.mFlashLog.logData,"UNKNOWN FAULT 0x%08X\n", pc); break; } return Flash_SaveInfomation(); } /** @brief 返回主机标志位 @param 无 @return 主机标志位 */ uint8_t Get_isHost(void) { return mFlash.isHost; } /** @brief 测试halflash接口 @param 无 @return 无 */ void TestHalFlashInterface(void) { //uint32_t Flash_SaveStep(void); ////存储基本信息 //uint32_t Flash_SaveInfomation(void); ////存储备份信息 //uint32_t Flash_SaveBackup(void); ////获取基本信息 //uint32_t Flash_GetInfomation(Flash_t *pflash); ////获取备份信息 //uint32_t Flash_GetBackup(FlashBackup_t *pbackup); // uint32_t i; // Flash_t m_testflash; // //// Fstorage_FlashInit(); // // //测试步数信息的写入和读取 // for(i=0;i<1500;i++) // { // mFlash.mStep.num = i; // mFlash.mStep.stepCur[0] = 0; // mFlash.mStep.stepCur[1] = i; // // SEGGER_RTT_printf(0,"Flash_SaveStep[%d]:%d \n\n",i,Flash_SaveStep()); // } // uint8_t checkflash[4] = {0}; // for(i=0;i<1500;i++) // { // Read_N_Byte_flash(FLASH_ADDR_STEP+i*4, (uint32_t*)&checkflash[0], 4); // SEGGER_RTT_printf(0,"checkflash[%d]:%02x,%02x,%02x,%02x ",i,checkflash[0],checkflash[1],checkflash[2],checkflash[3]); // if(i%5 == 0)SEGGER_RTT_printf(0,"\n"); // nrf_delay_ms(5); // } }