/********************************************************************* * * 文件名: User_flash.c * 功能描述:flash功能 * 作者: 陈俊超 * 时间:2020-12-12 * ********************************************************************/ #include "User_flash.h" #include "User_task.h" #include "app_timer.h" /********************** 变量区 **************************************/ static bool volatile m_fds_initialized =false; static step_flash m_step_config = {0,{0}}; extern TranforStep_type m_tranforStep; static const step_flash m_dummy_cfg ={0,{0}};/* 出工厂参数 */ static fds_record_t const m_dummy_record = { .file_id = sportdatakey_file_id, .key = sportdatakey_rec_key, .data.p_data = &m_dummy_cfg, .data.length_words = (sizeof(m_dummy_cfg) + 2) / sizeof(uint32_t),//记录长度需要四字节对齐 }; #if DEBUG_EN const char *fds_err_str(ret_code_t ret) { /* Array to map FDS return values to strings. */ static char const * err_str[] = { "FDS_ERR_OPERATION_TIMEOUT", "FDS_ERR_NOT_INITIALIZED", "FDS_ERR_UNALIGNED_ADDR", "FDS_ERR_INVALID_ARG", "FDS_ERR_NULL_ARG", "FDS_ERR_NO_OPEN_RECORDS", "FDS_ERR_NO_SPACE_IN_FLASH", "FDS_ERR_NO_SPACE_IN_QUEUES", "FDS_ERR_RECORD_TOO_LARGE", "FDS_ERR_NOT_FOUND", "FDS_ERR_NO_PAGES", "FDS_ERR_USER_LIMIT_REACHED", "FDS_ERR_CRC_CHECK_FAILED", "FDS_ERR_BUSY", "FDS_ERR_INTERNAL", }; return err_str[ret - NRF_ERROR_FDS_ERR_BASE]; } /* Array to map FDS events to strings. */ static char const * fds_evt_str[] = { "FDS_EVT_INIT", "FDS_EVT_WRITE", "FDS_EVT_UPDATE", "FDS_EVT_DEL_RECORD", "FDS_EVT_DEL_FILE", "FDS_EVT_GC", }; #endif /********************** 函数区 **************************************/ /********************************************************** * 函数名字:fds_evt_handler * 函数作用:fds文件管理库事件回调 * 函数参数:p_evt:回调参数 * 函数返回值:无 ***********************************************************/ bool flashbusyFlag=false; static void fds_evt_handler(fds_evt_t const * p_evt) { if (p_evt->result == NRF_SUCCESS){ flashbusyFlag=false; #if DEBUG_EN SEGGER_RTT_printf(0,"Event: %s received (NRF_SUCCESS)\n",fds_evt_str[p_evt->id]); #endif } else{ flashbusyFlag=false; #if DEBUG_EN SEGGER_RTT_printf(0,"Event: %s received (%s)\n",fds_evt_str[p_evt->id],fds_err_str(p_evt->result)); #endif } switch (p_evt->id) { case FDS_EVT_INIT: if (p_evt->result == NRF_SUCCESS){ m_fds_initialized = true; } break; case FDS_EVT_WRITE:{ flashbusyFlag=false; if (p_evt->result == NRF_SUCCESS) { #if DEBUG_EN SEGGER_RTT_printf(0,"Record ID:\t0x%04x\n", p_evt->write.record_id); SEGGER_RTT_printf(0,"File ID:\t0x%04x\n", p_evt->write.file_id); SEGGER_RTT_printf(0,"Record key:\t0x%04x\n", p_evt->write.record_key); #endif } } break; case FDS_EVT_DEL_RECORD:{ flashbusyFlag=false; if (p_evt->result == NRF_SUCCESS){ #if DEBUG_EN SEGGER_RTT_printf(0,"Record ID:\t0x%04x\n", p_evt->del.record_id); SEGGER_RTT_printf(0,"File ID:\t0x%04x\n", p_evt->del.file_id); SEGGER_RTT_printf(0,"Record key:\t0x%04x\n", p_evt->del.record_key); #endif } } break; default: break; } } /********************************************************** * 函数名字:record_delete_next * 函数作用:删除所有的记录 * 函数参数:无 * 函数返回值:true:删除成功 * false:删除失败 ***********************************************************/ bool record_delete_next(void) { ret_code_t rc; fds_find_token_t tok = {0}; fds_record_desc_t desc = {0}; rc = fds_record_find(sportdatakey_file_id, sportdatakey_rec_key, &desc, &tok);//查找配置文件记录 if (rc == NRF_SUCCESS){ rc = fds_record_delete(&desc); if (rc != NRF_SUCCESS)return false; } else{ return false; } return true; } /********************************************************** * 函数名字:Add_StepToflash * 函数作用:步数+1 * 函数参数:无 * 函数返回值:无 ***********************************************************/ void Add_Step(void) { m_step_config.step_number[m_step_config.step_journey_cun*2]+=1; m_step_config.step_number[m_step_config.step_journey_cun*2+1]= (uint16_t)(m_step_config.step_number[m_step_config.step_journey_cun*2]*OneStepLong); #if DEBUG_EN SEGGER_RTT_printf(0,"AAAAA_step:%d\n",m_step_config.step_number[m_step_config.step_journey_cun*2]); #endif } /********************************************************** * 函数名字:Update_StepToflash * 函数作用:更新步数到flash * 函数参数:无 * 函数返回值:true:更新成功 * false:更新失败 ***********************************************************/ bool Update_StepToflash(void) { ret_code_t rc; fds_record_desc_t desc = {0};//用来操作记录的描述符结构清零 fds_find_token_t tok = {0};//保存秘钥的令牌清零 bool Result =false; static step_flash m_step_config_1 = {0,{0}}; if(flashbusyFlag){ nrf_delay_ms(2); if(flashbusyFlag)return false; } rc = fds_record_find(sportdatakey_file_id, sportdatakey_rec_key, &desc, &tok); if(NRF_SUCCESS == rc){//对应KEY记录查找数据 memcpy(&m_step_config_1, &m_step_config, sizeof(step_flash)); fds_record_t const record = { .file_id = sportdatakey_file_id, .key = sportdatakey_rec_key, .data.p_data = &m_step_config_1, .data.length_words = (sizeof(step_flash) + 2) / sizeof(uint32_t) }; // flashbusyFlag=true; rc = fds_record_update(&desc, &record);//重新写记录 if(NRF_SUCCESS == rc){ Result =true; } else if(FDS_ERR_NO_SPACE_IN_FLASH == rc) { fds_gc();//垃圾回收 } #if DEBUG_EN SEGGER_RTT_printf(0,"Update_StepToflash:%02x,%d\n",rc,record.data.length_words); #endif } return Result; } /********************************************************** * 函数名字:Update_flash_read * 函数作用:向flash读取数据参数 * 函数参数:无 * 函数返回值:无 ***********************************************************/ void Update_flash_read(void) { ret_code_t rc; fds_record_desc_t desc = {0};//用来操作记录的描述符结构清零 fds_find_token_t tok = {0};//保存秘钥的令牌清零 rc = fds_record_find(sportdatakey_file_id, sportdatakey_rec_key, &desc, &tok);//对应KEY记录查找数据 APP_ERROR_CHECK(rc); if (rc == NRF_SUCCESS){//如果查找成功 fds_flash_record_t config = {0};// rc = fds_record_open(&desc, &config);//打开记录读取数据 APP_ERROR_CHECK(rc); memcpy(&m_step_config,config.p_data,sizeof(step_flash)); rc = fds_record_close(&desc);//关闭记录 APP_ERROR_CHECK(rc); #if DEBUG_EN SEGGER_RTT_printf(0,"m_step_config.step_number:%d,%d\n",m_step_config.step_number[0],m_step_config.step_number[1]); #endif } } /********************************************************** * 函数名字:User_flash_init * 函数作用:flash功能初始化,定时管理任务 * 函数参数:无 * 函数返回值:无 ***********************************************************/ void User_flash_init(void) { ret_code_t rc; (void) fds_register(fds_evt_handler); rc = fds_init(); APP_ERROR_CHECK(rc); while (!m_fds_initialized){}//等待初始化完成 fds_stat_t stat = {0}; rc = fds_stat(&stat); APP_ERROR_CHECK(rc); fds_record_desc_t desc = {0}; fds_find_token_t tok = {0}; rc = fds_record_find(sportdatakey_file_id, sportdatakey_rec_key, &desc, &tok); if (rc == NRF_SUCCESS){//如果查找成功 fds_flash_record_t config = {0}; rc = fds_record_open(&desc, &config);//打开记录读取数据 APP_ERROR_CHECK(rc); rc = fds_record_close(&desc);//关闭记录 APP_ERROR_CHECK(rc); #if DEBUG_EN SEGGER_RTT_printf(0,"fds_record_find,length:%d\n",config.p_header->length_words); #endif if(config.p_header->length_words != (sizeof(step_flash)+2)/4){//判断一下读出来的参数结构体长度是否正确 record_delete_next();//把所有记录清零 rc = fds_record_write(&desc, &m_dummy_record);//重新写记录 APP_ERROR_CHECK(rc); #if DEBUG_EN SEGGER_RTT_printf(0,"Update config file..done.\n"); #endif memcpy(&m_step_config, &m_dummy_record,sizeof(step_flash));//以出厂设置作为当前运行参数 } else{ memcpy(&m_step_config, config.p_data,sizeof(step_flash)); #if DEBUG_EN #if STEP_TEST SEGGER_RTT_printf(0,"flash data:%02x,%02x,%02x,%02x,%02x,%02x\n", m_step_config.step_number[0],m_step_config.step_number[1], m_step_config.step_number[500],m_step_config.step_number[501],m_step_config.step_number[1438],m_step_config.step_number[1439]); #endif #endif } } else{ #if DEBUG_EN SEGGER_RTT_printf(0,"Writing config file...\n"); #endif rc = fds_record_write(&desc, &m_dummy_record);//重新写记录 APP_ERROR_CHECK(rc); } ////////////////测试 #if STEP_TEST m_step_config.step_journey_cun =1440; for(uint16_t i=0;i< m_step_config.step_journey_cun*2;i++){ m_step_config.step_number[i]=(0x0000+i); } #if DEBUG_EN SEGGER_RTT_printf(0,"STEP_TEST\n"); #endif Update_StepToflash(); #endif } void Clear_Stepflash(void){ memset(&m_step_config,0,sizeof(step_flash)); } /********************************************************** * 函数名字:Rsponse_ReqStepCmd * 函数作用:回复脚步查询指令 * 函数参数:Packet_number:需要发送的包的数量 * 函数返回值:无 ***********************************************************/ void Rsponse_ReqStepCmd(uint8_t Packet_number) { uint8_t buf[40]; uint8_t Length=0; uint8_t temp=0; buf[Length++] = 0x02; buf[Length++] = 0x00; memcpy(buf+Length, &m_tranforStep.systemtime[0],8); Length+=8; buf[Length++] = Packet_number; for(temp=0;temp<3;temp++){ send_protocol(DEX_NUM,0xA1,buf,Length); nrf_delay_ms(1); } #if DEBUG_EN SEGGER_RTT_printf(0,"Rsponse Packet length:\n",Packet_number); #endif } /********************************************************** * 函数名字:SendStep_Packet * 函数作用:回复脚步查询指令 * 函数参数:发送单个步数帧到ESB当中 * 函数返回值:无 ***********************************************************/ void SendStep_Packet_ToEsb(void) { uint8_t buf[230]; uint8_t temp=0; uint8_t Length=0; uint8_t StepDateLength=0; buf[Length++] = 0x02; buf[Length++] = m_tranforStep.CurrentPacket; if(1 == m_tranforStep.Packetlength)//单包 StepDateLength =4; else if(m_tranforStep.BeSentDataLength >= ESB_MAX_SEND_DATA) StepDateLength =ESB_MAX_SEND_DATA; else StepDateLength =m_tranforStep.BeSentDataLength;//判断是否有最后一个包 for(temp=0;temp < StepDateLength;temp +=2){ buf[Length++] = m_step_config.step_number[temp] >>8; buf[Length++] = m_step_config.step_number[temp] & 0x00ff; } for(temp=0;temp<1;temp++){ send_protocol(DEX_NUM,0xA1,buf,Length); nrf_delay_ms(1); } #if DEBUG_EN SEGGER_RTT_printf(0,"Step no send ==%d,%02x,%02x,%02x\n",m_tranforStep.BeSentDataLength,buf[0],buf[1],buf[2]); #endif } /********************************************************** * 函数名字:Calculate_Send_Packet * 函数作用:计算需要发送的脚步 * 函数参数:无 * 函数返回值:无 ***********************************************************/ void Calculate_Send_Packet(void) { //计算需要发送的数据包数量 if(m_step_config.step_journey_cun ==0 && (0 == m_step_config.step_number[0])){ m_tranforStep.Packetlength =0; } else if((m_step_config.step_journey_cun*4) <=ESB_MAX_SEND_DATA){ m_tranforStep.Packetlength =1; m_tranforStep.BeSentDataLength = 4; } else{ m_tranforStep.BeSentDataLength = (m_step_config.step_journey_cun+1)*4; m_tranforStep.Packetlength =(m_tranforStep.BeSentDataLength/ESB_MAX_SEND_DATA); if(m_tranforStep.BeSentDataLength%ESB_MAX_SEND_DATA != 0) { m_tranforStep.Packetlength +=1; } } #if DEBUG_EN SEGGER_RTT_printf(0,"step_journey_cun:%d,%d,Packetlength ==%d\n",m_step_config.step_journey_cun,m_tranforStep.BeSentDataLength,m_tranforStep.Packetlength); #endif } /********************************************************** * 函数名字:OneHour_StepF * 函数作用:计算需要发送的脚步 * 函数参数:无 * 函数返回值:无 ***********************************************************/ void OneHour_Step(void) { m_step_config.step_journey_cun++; if(m_step_config.step_journey_cun >= step_number_length){ record_delete_next(); } }