123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- #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<FLASH_STEP_LEN;i++){ mFlash.mStep.stepPerHour[i] = i; } //测试
- // Flash_WriteHalfWordBuff(FLASH_ADDR_STEP,&mFlash.mStep.stepPerHour[0],FLASH_STEP_LEN);
- // while (nrf_fstorage_is_busy(&fstorage));
- // Flash_ReadHalfWordBuff(FLASH_ADDR_STEP,mFlash.mStep.stepPerHour,FLASH_STEP_LEN);
- }
- if(ret==0){
- // Read_N_Byte_flash(FLASH_ADDR_INFO,(uint32_t*)(&mFlash),sizeof(Flash_t));
- // Read_N_Byte_flash(FLASH_ADDR_BACKUP,(uint32_t*)(&mBackup),sizeof(FlashBackup_t));
- Flash_GetInfomation(&mFlash);
- Flash_GetBackup(&mBackup);
- }
- if(mFlash.basePress==0) mFlash.basePress = 200;
- SEGGER_RTT_printf(0,"mFlash.mStep.num=%d\n",mFlash.mStep.num);
- SEGGER_RTT_printf(0,"mFlash.mStep.stepCur[0]=%d,mFlash.mStep.stepCur[1]=%d\n",mFlash.mStep.stepCur[0],mFlash.mStep.stepCur[1]);
- SEGGER_RTT_printf(0,"mFlash.basePress=%d\n",mFlash.basePress);
- SEGGER_RTT_printf(0,"err code :%s\n",mFlash.mFlashLog.logData);
-
- SEGGER_RTT_printf(0,"Flash init ok.\n");
-
- mFlash.isHost = _IS_HOST;
-
- #ifdef PIN_SEL
- nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_PULLUP);
- nrf_delay_ms(100);
- mFlash.isHost = (uint8_t)nrf_gpio_pin_read(PIN_SEL);
- nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_NOPULL);
- #endif
- if(mFlash.isHost){
- SEGGER_RTT_printf(0,"======= Left shooe ======= \n");
- }else{
- SEGGER_RTT_printf(0,"======= Right shooe ======= \n");
- }
-
-
- TestHalFlashInterface();
- }
-
- /**
- @brief 存储步数
- @param 无
- @return 错误代码
- */
- uint32_t Flash_SaveStep(void)
- {
- uint32_t err_code;
- uint32_t flash_data;
- uint32_t addr;
-
- if(mFlash.mStep.num<1500)
- {
- //SEGGER_RTT_printf(0,"app_step_GetStep_L()=%d,app_step_GetStep_R()=%d\n",app_step_GetStep_L(),app_step_GetStep_R());
- uint32_t step = app_step_GetStep_L() + app_step_GetStep_R(); //获取左右鞋步数
-
- //获取地址
- //if(mFlash.mStep.num%2 == 0)addr = FLASH_ADDR_STEP+mFlash.mStep.num*2;
- //else addr = FLASH_ADDR_STEP+(mFlash.mStep.num-1)*2;
- addr = FLASH_ADDR_STEP+mFlash.mStep.num*4;
-
- //读取内容
- err_code = Read_N_Byte_flash(addr , &flash_data, sizeof(flash_data));
- if(err_code != FLASH_OP_SUCCESS)return err_code;
- //修改内容
- //if(mFlash.mStep.num%2 == 0)flash_data &= (0xFFFF0000 | (step>>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);
- // }
- }
|