diff --git a/.vscode/settings.json b/.vscode/settings.json index 13695b9..a8d666d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -79,6 +79,8 @@ "netdev_ipaddr.h": "c", "stdint.h": "c", "stdarg.h": "c", - "math.h": "c" + "math.h": "c", + "ctime": "c", + "bsp_wdg.h": "c" } } diff --git a/applications/main.c b/applications/main.c index 36cefa0..71c77e9 100644 --- a/applications/main.c +++ b/applications/main.c @@ -2,7 +2,7 @@ * @Author: mbw * @Date: 2024-10-23 17:14:16 * @LastEditors: mbw && 1600520629@qq.com - * @LastEditTime: 2025-06-16 16:46:25 + * @LastEditTime: 2025-06-16 17:55:45 * @FilePath: \ble_bjq_ch303rct6_ml307\applications\main.c * @Descrt_thread_ * @@ -313,6 +313,25 @@ int main(void) LOG_D("工作时长:%d", work_duration); // 读取历史记录总数 g_Calibration_status = Flash_Get_Calibration_State(); + + //这种方法,当纽扣电池没电,直接完蛋,但是5年应该能撑住 + uint16_t dr3 = (BKP_ReadBackupRegister(BKP_DR3) & 0X0F); + if (dr3 != 0)//说明已经标定过 ,并且在掉电时被擦出了,重新写入标定数据 + { + uint16_t dr2 = (BKP_ReadBackupRegister(BKP_DR2) & 0xFFFF); + uint8_t calibration_buf[2] = {0}; + + // 写入标定值 + calibration_buf[0] = dr2 & 0xFF; // 低字节 + calibration_buf[1] = (dr2 >> 8) & 0xFF; // 高字节 + + LOG_D("calibration_buf[0] = %X calibration_buf[1] = %X", calibration_buf[0], calibration_buf[1]); + Flash_Sys_Cfg(kAlarmLValueId, calibration_buf, 2); + Flash_Set_Calibration_State(kSysGasCalibStatus); + BKP_WriteBackupRegister(BKP_DR3, 0);// 清除标志位 + + } + rt_thread_mdelay(10); } else @@ -564,7 +583,13 @@ int main(void) // 写入掉电记录 Flash_Write_Record(kRecordPowerDown); rt_thread_delay(100); - Flash_Set_WorkDuration(work_duration); // 写入工作时长 + if(Flash_Set_WorkDuration(work_duration) != RT_EOK) // 写入工作时长 + { + LOG_E("写入工作时长失败");//到这里如果失败了,则必须将其写道后备区中,一般情况下这个函数进不来 + BKP_WriteBackupRegister(BKP_DR2, Sensor_device.alarm_value); + BKP_WriteBackupRegister(BKP_DR3, 1);//失败标志量 + } + if (Flash_Get_Valve_Num()) { rt_uint8_t mac_addr[6]; diff --git a/applications/user_sys.h b/applications/user_sys.h index 40bf216..81eeb4a 100644 --- a/applications/user_sys.h +++ b/applications/user_sys.h @@ -37,6 +37,7 @@ // #define SYS_IOT_URL ("8.130.105.38") // #define SYS_IOT_PORT ("7133") + #define K (8 * 1000) // 定义宏来打印编译时间和日期 diff --git a/bsp/src/bsp_flash.c b/bsp/src/bsp_flash.c index f3cc160..15f9e48 100644 --- a/bsp/src/bsp_flash.c +++ b/bsp/src/bsp_flash.c @@ -256,7 +256,6 @@ static rt_size_t Flash_Write(rt_uint32_t addr, rt_uint8_t *buf, rt_size_t len) } BSP_Flash_Lock(); - return i; } @@ -286,7 +285,20 @@ int Flash_Set_WorkDuration(rt_uint16_t value) Flash_ErasePage_ReadConfigInfo(page_buf); rt_memcpy(page_buf + in_page_offset, &value, FLASH_WORK_TIME_LEN); - return Flash_Write_ConfigInfo(page_buf); + for (size_t i = 0; i < 3; i++) + { + if(Flash_Write_ConfigInfo(page_buf) != 0)//当写错时,重新写入 + { + return RT_EOK; + } + else + { + Flash_ErasePage_ConfigInfo(); + rt_thread_delay(10); + } + } + + return RT_ERROR; } int Flash_Get_Sever_Data(flash_sever_info *sever_info) @@ -893,6 +905,7 @@ int BSP_Flash_Init (void) Flash_Write (FLASH_INIT_FLAG_ADDR, flash_init_flag, sizeof (flash_init_flag)); Flash_Set_WorkDuration (work_duration); + LOG_D ("work_duration:%d", Flash_Get_WorkDuration()); Convert_To_Hex (&sever_info, sever_data);