diff --git a/applications/main.c b/applications/main.c index 59d99c6..5267037 100644 --- a/applications/main.c +++ b/applications/main.c @@ -35,7 +35,7 @@ static rt_uint32_t event_flags[kMaxEventcnt] = {0}; struct rt_event alarm_event; -rt_bool_t is_event_initialized = RT_FALSE; // 是否初始化完成 +rt_bool_t is_event_initialized = RT_FALSE; // 是否初始化完成 TsSysControl SysControl; @@ -43,9 +43,9 @@ struct rt_timer work_cnt_timer; rt_timer_t led_state_timer; -void Send_Laser_Alarm_Event(AlarmEvent event_type); +void Send_Laser_Alarm_Event (AlarmEvent event_type); -static void Set_Event(AlarmEvent event_type) +static void Set_Event (AlarmEvent event_type) { if (event_type < kMaxEventcnt) { @@ -53,28 +53,28 @@ static void Set_Event(AlarmEvent event_type) } } -void SYS_EventInit(void) +void SYS_EventInit (void) { if (!is_event_initialized) { - if (rt_event_init(&alarm_event, "alarm_event", RT_IPC_FLAG_PRIO) == RT_EOK) + if (rt_event_init (&alarm_event, "alarm_event", RT_IPC_FLAG_PRIO) == RT_EOK) { for (AlarmEvent event = kPowerOnEvent; event < kMaxEventcnt; event++) { - Set_Event(event); + Set_Event (event); } is_event_initialized = RT_TRUE; } else { is_event_initialized = RT_FALSE; - LOG_E("Alarm event init failed!"); + LOG_E ("Alarm event init failed!"); } } } -rt_uint32_t Get_Sys_Event_Flag(AlarmEvent event_type) +rt_uint32_t Get_Sys_Event_Flag (AlarmEvent event_type) { if (is_event_initialized == RT_TRUE) { @@ -86,103 +86,105 @@ rt_uint32_t Get_Sys_Event_Flag(AlarmEvent event_type) return kMaxEventcnt; } -void Send_Laser_Alarm_Event(AlarmEvent event_type) +void Send_Laser_Alarm_Event (AlarmEvent event_type) { if (is_event_initialized == RT_TRUE) { if (SysControl.status == kPowerDownEvent) { - LOG_D("掉电模式下不处理其他事件"); + LOG_D ("掉电模式下不处理其他事件"); } else { - LOG_I("Send_Laser_Alarm_Event = %d", event_type); - rt_event_send(&alarm_event, Get_Sys_Event_Flag(event_type)); + LOG_I ("Send_Laser_Alarm_Event = %d", event_type); + rt_event_send (&alarm_event, Get_Sys_Event_Flag (event_type)); } } else { is_event_initialized = RT_FALSE; - LOG_E("Alarm event no init !"); + LOG_E ("Alarm event no init !"); } } -int _Self_Check_Mode(void) +int _Self_Check_Mode (void) { - LED_STOP(r); - LED_STOP(g); - LED_STOP(y); - rt_thread_mdelay(500); - LED_CTRL(g, "500,2500", 1); - LED_START(g); - rt_thread_mdelay(1000); - LED_CTRL(y, "500,2500", 1); - LED_START(y); - rt_thread_mdelay(1000); - LED_CTRL(r, "500,2500", 1); - LED_START(r); - rt_thread_mdelay(1000); - LED_STOP(r); - LED_STOP(g); - LED_STOP(y); + LED_STOP (r); + LED_STOP (g); + LED_STOP (y); + rt_thread_mdelay (500); + LED_CTRL (g, "500,2500", 1); + LED_START (g); + rt_thread_mdelay (1000); + LED_CTRL (y, "500,2500", 1); + LED_START (y); + rt_thread_mdelay (1000); + LED_CTRL (r, "500,2500", 1); + LED_START (r); + rt_thread_mdelay (1000); + LED_STOP (r); + LED_STOP (g); + LED_STOP (y); BEEP_SELF_CHECK; return RT_EOK; } -void Preheat_Sensor(rt_uint32_t timeout_counter) +void Preheat_Sensor (rt_uint32_t timeout_counter) { // 定义超时时间,单位为毫秒 #define TIMEOUT_MS 15000 - timeout_counter += 10; // 每次递归增加10毫秒 + timeout_counter += 10; // 每次递归增加10毫秒 if (H308.Data.checksum != 0) { BEEP_PREAT_OK; - LED_STOP(r); - LED_STOP(g); - LED_STOP(y); - LOG_D("预热完成"); + LED_STOP (r); + LED_STOP (g); + LED_STOP (y); + LOG_D ("预热完成"); BEEP_PREAT_OK; - Send_Laser_Alarm_Event(kNormalDetectionEvents); + Send_Laser_Alarm_Event (kNormalDetectionEvents); } - else if (IS_H308_EndOfLife()) // 如果寿命到期,预热完成后直接到寿命过期状态 + else if (IS_H308_EndOfLife()) // 如果寿命到期,预热完成后直接到寿命过期状态 { - LED_STOP(r); - LED_STOP(g); - LED_STOP(y); + LED_STOP (r); + LED_STOP (g); + LED_STOP (y); BEEP_PREAT_OK; - Send_Laser_Alarm_Event(kSensorFailureEvent); + Send_Laser_Alarm_Event (kSensorFailureEvent); } else if ((timeout_counter >= TIMEOUT_MS) && (H308.Data.checksum == 0)) { - LED_STOP(r); - LED_STOP(g); - LED_STOP(y); + LED_STOP (r); + LED_STOP (g); + LED_STOP (y); BEEP_PREAT_OK; - LOG_D("预热完成,传感器故障"); - Send_Laser_Alarm_Event(kFaultEvent); // 传感器故障 + LOG_D ("预热完成,传感器故障"); + Send_Laser_Alarm_Event (kFaultEvent); // 传感器故障 } else { - rt_thread_mdelay(10); - Preheat_Sensor(timeout_counter); + rt_thread_mdelay (10); + Preheat_Sensor (timeout_counter); } } -void Work_Cnt_Timer_Callback(void *parameter) + +void Work_Cnt_Timer_Callback (void *parameter) { work_duration++; - if (IS_H308_EndOfLife()) // 每天检测一下是否过期 + if (IS_H308_EndOfLife()) // 每天检测一下是否过期 { - if (Flash_GetNum_Records(kRecordSensoEndOfLife) == 0) + if (Flash_GetNum_Records (kRecordSensoEndOfLife) == 0) { - Flash_Write_Record(kRecordSensoEndOfLife); + Flash_Write_Record (kRecordSensoEndOfLife); } - Send_Laser_Alarm_Event(kSensorFailureEvent); + Send_Laser_Alarm_Event (kSensorFailureEvent); } } -int main(void) + +int main (void) { #define WORK_TIMER_CNT (1000 * 60 * 60 * 24) @@ -197,244 +199,269 @@ int main(void) SYS_EventInit(); BSP_SYS_Init(); // 读取历史记录总数 - Flash_GetTotalRecord(&TotalRecords); + Flash_GetTotalRecord (&TotalRecords); work_duration = Flash_Get_WorkDuration(); - LOG_D("工作天数:%d", work_duration); - rt_timer_init(&work_cnt_timer, - "work_cnt_timer", - Work_Cnt_Timer_Callback, - RT_NULL, - WORK_TIMER_CNT, - RT_TIMER_FLAG_PERIODIC); - rt_timer_start(&work_cnt_timer); + LOG_D ("工作天数:%d", work_duration); + rt_timer_init (&work_cnt_timer, + "work_cnt_timer", + Work_Cnt_Timer_Callback, + RT_NULL, + WORK_TIMER_CNT, + RT_TIMER_FLAG_PERIODIC); + rt_timer_start (&work_cnt_timer); + TuFlashProductTimeLimitFrame LimitTime; + if (Flash_GetProductTimeLimit (&LimitTime, kExpirationTimeId) == READY) + { + // 计算出 到期时间对应的RTC秒数 + H308.expiration_seconds = DateTime2Seconds (LimitTime.Struct.year, + LimitTime.Struct.month, LimitTime.Struct.day, LimitTime.Struct.hour, + LimitTime.Struct.minute, LimitTime.Struct.second); + LOG_D ("h308_expiration_time:%04d-%02d-%02d,%02d:%02d", + LimitTime.Struct.year, LimitTime.Struct.month, LimitTime.Struct.day, + LimitTime.Struct.hour, LimitTime.Struct.minute, LimitTime.Struct.second); + } + if (Flash_GetNum_Records (kRecordSensoEndOfLife) == 1) + { + LOG_W ("寿命已到期"); + H308.end_of_life = 1; + } + // 寿命到期检测 + if (IS_H308_EndOfLife()) + { + if (Flash_GetNum_Records (kRecordSensoEndOfLife) == 0) + { + Flash_Write_Record (kRecordSensoEndOfLife); + } + Send_Laser_Alarm_Event (kSensorFailureEvent); + } // 检测上电/掉电复位标志 - if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) // TODO:这块超级电容会造成掉电重启,并且标志是上电复位,只能通过其超级电容造成复位时事件创建失败进行判断,不治本 + if (RCC_GetFlagStatus (RCC_FLAG_PORRST) != RESET) // TODO:这块超级电容会造成掉电重启,并且标志是上电复位,只能通过其超级电容造成复位时事件创建失败进行判断,不治本 { RCC_ClearFlag(); - Send_Laser_Alarm_Event(kPowerOnEvent); + Send_Laser_Alarm_Event (kPowerOnEvent); } else { - Send_Laser_Alarm_Event(kPreheatingEvent); // 这一句的作用是设备不是掉电重启的情况,直接进入预热模式 + Send_Laser_Alarm_Event (kPreheatingEvent); // 这一句的作用是设备不是掉电重启的情况,直接进入预热模式 } } else { - LOG_D("欠压复位\r\n"); + LOG_D ("欠压复位\r\n"); RCC_ClearFlag(); - NVIC_SystemReset(); // 直接重启系统 + NVIC_SystemReset(); // 直接重启系统 } while (1) { if (is_event_initialized) { - result = rt_event_recv(&alarm_event, - Get_Sys_Event_Flag(kPowerOnEvent) | // 上电 - Get_Sys_Event_Flag(kPowerDownEvent) | // 掉电 - Get_Sys_Event_Flag(kPreheatingEvent) | // 预热 - Get_Sys_Event_Flag(kNormalDetectionEvents) | // 正常检测 - Get_Sys_Event_Flag(kAlarmExceptionEvent) | // 报警异常 - Get_Sys_Event_Flag(kAlarmEvent) | // 报警 - Get_Sys_Event_Flag(kAlarmRcyEvent) | // 报警恢复 - Get_Sys_Event_Flag(kSelfCheckEvent) | // 自检 - Get_Sys_Event_Flag(kFaultEvent) | // 故障模式 - Get_Sys_Event_Flag(kFaultRcyEvent) | // 故障恢复 - Get_Sys_Event_Flag(KMuteEvent) | // 消音 - Get_Sys_Event_Flag(kSensorFailureEvent), // 传感器寿命到期 - RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, - RT_WAITING_FOREVER, &received_event); + result = rt_event_recv (&alarm_event, + Get_Sys_Event_Flag (kPowerOnEvent) | // 上电 + Get_Sys_Event_Flag (kPowerDownEvent) | // 掉电 + Get_Sys_Event_Flag (kPreheatingEvent) | // 预热 + Get_Sys_Event_Flag (kNormalDetectionEvents) | // 正常检测 + Get_Sys_Event_Flag (kAlarmExceptionEvent) | // 报警异常 + Get_Sys_Event_Flag (kAlarmEvent) | // 报警 + Get_Sys_Event_Flag (kAlarmRcyEvent) | // 报警恢复 + Get_Sys_Event_Flag (kSelfCheckEvent) | // 自检 + Get_Sys_Event_Flag (kFaultEvent) | // 故障模式 + Get_Sys_Event_Flag (kFaultRcyEvent) | // 故障恢复 + Get_Sys_Event_Flag (KMuteEvent) | // 消音 + Get_Sys_Event_Flag (kSensorFailureEvent), // 传感器寿命到期 + RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &received_event); if (result == RT_EOK) { if (is_event_initialized == RT_TRUE) { - if (received_event & Get_Sys_Event_Flag(kPowerOnEvent)) // 上电 + if (received_event & Get_Sys_Event_Flag (kPowerOnEvent)) // 上电 { - LOG_D("上电模式"); - LOG_D("is_event_initialized :%d", is_event_initialized); + LOG_D ("上电模式"); + LOG_D ("is_event_initialized :%d", is_event_initialized); SysControl.status = kPowerOnEvent; - Flash_Write_Record(kRecordPowerOn); - Send_Laser_Alarm_Event(kPreheatingEvent); + Flash_Write_Record (kRecordPowerOn); + Send_Laser_Alarm_Event (kPreheatingEvent); } - else if (received_event & Get_Sys_Event_Flag(kPreheatingEvent)) // 预热 + else if (received_event & Get_Sys_Event_Flag (kPreheatingEvent)) // 预热 { - LOG_D("预热模式"); + LOG_D ("预热模式"); SysControl.last_status = SysControl.status; - SysControl.status = kPreheatingEvent; + SysControl.status = kPreheatingEvent; LED_G_PREAT; BEEP_PREAT_OK; - Send_Laser_Alarm_Event(kNormalDetectionEvents); + Send_Laser_Alarm_Event (kNormalDetectionEvents); } - else if (received_event & Get_Sys_Event_Flag(kNormalDetectionEvents)) // 正常检测 + else if (received_event & Get_Sys_Event_Flag (kNormalDetectionEvents)) // 正常检测 { SysControl.last_status = SysControl.status; - SysControl.status = kNormalDetectionEvents; + SysControl.status = kNormalDetectionEvents; if (SysControl.last_status == kSelfCheckEvent) { - rt_thread_mdelay(5000); - LOG_I("自检电磁阀动作"); + rt_thread_mdelay (5000); + LOG_I ("自检电磁阀动作"); EMV_CLOSE_VALVE; - LOG_I("自检风机动作"); + LOG_I ("自检风机动作"); relay_state_flag = 1; - rt_thread_mdelay(500); + rt_thread_mdelay (500); relay_state_flag = 0; } if (H308.detection_flag == kH308Alarm) { LED_G_NORMAL; - rt_thread_mdelay(500); - Send_Laser_Alarm_Event(kAlarmEvent); + rt_thread_mdelay (500); + Send_Laser_Alarm_Event (kAlarmEvent); } else if (H308.detection_flag == kH308Fault) { LED_G_NORMAL; - rt_thread_mdelay(500); - Send_Laser_Alarm_Event(kFaultEvent); + rt_thread_mdelay (500); + Send_Laser_Alarm_Event (kFaultEvent); } else { - LOG_D("正常检测模式"); + LOG_D ("正常检测模式"); } } - else if (received_event & Get_Sys_Event_Flag(kAlarmExceptionEvent)) // 浓度异常 + else if (received_event & Get_Sys_Event_Flag (kAlarmExceptionEvent)) // 浓度异常 { - LOG_D("浓度异常模式"); + LOG_D ("浓度异常模式"); SysControl.last_status = SysControl.status; - SysControl.status = kAlarmExceptionEvent; + SysControl.status = kAlarmExceptionEvent; LED_R_ALARM; - LOG_D("LED_R_ALARM"); + LOG_D ("LED_R_ALARM"); } - else if (received_event & Get_Sys_Event_Flag(kAlarmEvent)) // 报警 + else if (received_event & Get_Sys_Event_Flag (kAlarmEvent)) // 报警 { - LOG_D("报警模式"); + LOG_D ("报警模式"); SysControl.last_status = SysControl.status; - SysControl.status = kAlarmEvent; + SysControl.status = kAlarmEvent; - Flash_Write_Record(kRecordAlarm); // 写入flash报警信息 + Flash_Write_Record (kRecordAlarm); // 写入flash报警信息 EMV_CLOSE_VALVE; relay_state_flag = 1; LED_R_ALARM; BEEP_ALARM; #if (IOT_MODULE_SWITCH == 1) - Nt26k_Send_Event(kNt26kAlarmEvent); + Nt26k_Send_Event (kNt26kAlarmEvent); #endif } - else if (received_event & Get_Sys_Event_Flag(kAlarmRcyEvent)) // 报警恢复 + else if (received_event & Get_Sys_Event_Flag (kAlarmRcyEvent)) // 报警恢复 { - LOG_D("报警恢复模式"); + LOG_D ("报警恢复模式"); SysControl.last_status = SysControl.status; - SysControl.status = kAlarmRcyEvent; + SysControl.status = kAlarmRcyEvent; - Flash_Write_Record(kRecordAlarmRcy); + Flash_Write_Record (kRecordAlarmRcy); relay_state_flag = 0; BEEP_STOP; #if (IOT_MODULE_SWITCH == 1) - Nt26k_Send_Event(kNt26kAlarmRcyEvent); + Nt26k_Send_Event (kNt26kAlarmRcyEvent); #endif - Send_Laser_Alarm_Event(kNormalDetectionEvents); + Send_Laser_Alarm_Event (kNormalDetectionEvents); } - else if (received_event & Get_Sys_Event_Flag(kFaultEvent)) // 故障 + else if (received_event & Get_Sys_Event_Flag (kFaultEvent)) // 故障 { - LOG_D("故障模式"); + LOG_D ("故障模式"); - if (Flash_GetNum_Records(kRecordSensoEndOfLife) != 1) // 当设备失效时,只上报设备失效的故障,其他故障不上报 + if (Flash_GetNum_Records (kRecordSensoEndOfLife) != 1) // 当设备失效时,只上报设备失效的故障,其他故障不上报 { SysControl.last_status = SysControl.status; - SysControl.status = kFaultEvent; + SysControl.status = kFaultEvent; - Flash_Write_Record(kRecordFault); + Flash_Write_Record (kRecordFault); LED_Y_FAULT; #if (IOT_MODULE_SWITCH == 1) - Nt26k_Send_Event(kNt26kFaultEvent); + Nt26k_Send_Event (kNt26kFaultEvent); #endif } else { - Send_Laser_Alarm_Event(kSensorFailureEvent); + Send_Laser_Alarm_Event (kSensorFailureEvent); } } - else if (received_event & Get_Sys_Event_Flag(kFaultRcyEvent)) // 故障恢复 + else if (received_event & Get_Sys_Event_Flag (kFaultRcyEvent)) // 故障恢复 { - LOG_D("故障恢复模式"); + LOG_D ("故障恢复模式"); SysControl.last_status = SysControl.status; - SysControl.status = kFaultRcyEvent; + SysControl.status = kFaultRcyEvent; - Flash_Write_Record(kRecordFaultRcy); + Flash_Write_Record (kRecordFaultRcy); - Send_Laser_Alarm_Event(kNormalDetectionEvents); + Send_Laser_Alarm_Event (kNormalDetectionEvents); #if (IOT_MODULE_SWITCH == 1) - Nt26k_Send_Event(kNt26kFaultRcyEvent); + Nt26k_Send_Event (kNt26kFaultRcyEvent); #endif } - else if (received_event & Get_Sys_Event_Flag(KMuteEvent)) // 消音 + else if (received_event & Get_Sys_Event_Flag (KMuteEvent)) // 消音 { - LOG_D("消音模式"); + LOG_D ("消音模式"); SysControl.last_status = SysControl.status; - SysControl.status = KMuteEvent; + SysControl.status = KMuteEvent; BEEP_STOP; #if (IOT_MODULE_SWITCH == 1) - Nt26k_Send_Event(kNt26kSilenceEvent); + Nt26k_Send_Event (kNt26kSilenceEvent); #endif } - else if (received_event & Get_Sys_Event_Flag(kPowerDownEvent)) // 掉电 + else if (received_event & Get_Sys_Event_Flag (kPowerDownEvent)) // 掉电 { - LOG_D("掉电模式"); + LOG_D ("掉电模式"); H308_PWR_OFF; SysControl.last_status = SysControl.status; - SysControl.status = kPowerDownEvent; + SysControl.status = kPowerDownEvent; // 写入掉电记录 - Flash_Write_Record(kRecordPowerDown); + Flash_Write_Record (kRecordPowerDown); - Flash_Set_WorkDuration(work_duration); // 写入工作时长 + Flash_Set_WorkDuration (work_duration); // 写入工作时长 #if (IOT_MODULE_SWITCH == 1) - Nt26k_Send_Event(kNt26kPowerDownEvent); + Nt26k_Send_Event (kNt26kPowerDownEvent); #endif // 等待20s,要是还没断电就重启 for (uint8_t i = 0; i < 20; i++) { - rt_thread_mdelay(1000); - LOG_D(" %d s", i); + rt_thread_mdelay (1000); + LOG_D (" %d s", i); } - NVIC_SystemReset(); // 直接重启系统 + NVIC_SystemReset(); // 直接重启系统 } - else if (received_event & Get_Sys_Event_Flag(kSelfCheckEvent)) // 自检 + else if (received_event & Get_Sys_Event_Flag (kSelfCheckEvent)) // 自检 { - LOG_D("自检模式"); + LOG_D ("自检模式"); SysControl.last_status = SysControl.status; - SysControl.status = kSelfCheckEvent; + SysControl.status = kSelfCheckEvent; _Self_Check_Mode(); #if (IOT_MODULE_SWITCH == 1) - Nt26k_Send_Event(kNt26kSelfCheckEvent); + Nt26k_Send_Event (kNt26kSelfCheckEvent); #endif - Send_Laser_Alarm_Event(kNormalDetectionEvents); // 这里先返回检测模式,然后当事件触发时进入事件模式 + Send_Laser_Alarm_Event (kNormalDetectionEvents); // 这里先返回检测模式,然后当事件触发时进入事件模式 } - else if (received_event & Get_Sys_Event_Flag(kSensorFailureEvent)) // 失效 + else if (received_event & Get_Sys_Event_Flag (kSensorFailureEvent)) // 失效 { SysControl.last_status = SysControl.status; - SysControl.status = kSensorFailureEvent; - LOG_D("传感器失效模式"); + SysControl.status = kSensorFailureEvent; + LOG_D ("传感器失效模式"); - if (Flash_GetNum_Records(kRecordSensoEndOfLife) == 0) + if (Flash_GetNum_Records (kRecordSensoEndOfLife) == 0) { - Flash_Write_Record(kRecordSensoEndOfLife); + Flash_Write_Record (kRecordSensoEndOfLife); } LED_Y_END_OF_LIFE; } @@ -443,43 +470,45 @@ int main(void) } else { - rt_thread_mdelay(1000); + rt_thread_mdelay (1000); } } } #ifdef TEST_ENABLE -static void SYS_Set_RtcProductTime(int argc, char **argv) +static void SYS_Set_RtcProductTime (int argc, char **argv) { if (argc == 7) { - int year = atoi(argv[1]); - int mon = atoi(argv[2]); - int day = atoi(argv[3]); - int hour = atoi(argv[4]); - int min = atoi(argv[5]); - int second = atoi(argv[6]); - RTC_SetTime(2000 + year, mon, day, hour, min, second); + int year = atoi (argv[1]); + int mon = atoi (argv[2]); + int day = atoi (argv[3]); + int hour = atoi (argv[4]); + int min = atoi (argv[5]); + int second = atoi (argv[6]); + RTC_SetTime (2000 + year, mon, day, hour, min, second); RTC_GetTime(); - LOG_D("RTC: %4d-%02d-%02d, %02d:%02d:%02d", RtcDateTime.year, RtcDateTime.month, RtcDateTime.day, - RtcDateTime.hour, RtcDateTime.minute, RtcDateTime.second); + LOG_D ("RTC: %4d-%02d-%02d, %02d:%02d:%02d", RtcDateTime.year, RtcDateTime.month, RtcDateTime.day, + RtcDateTime.hour, RtcDateTime.minute, RtcDateTime.second); // 设置出厂时间 - Flash_SetProductTimeLimit(2000 + year, mon, day, hour, min, second, kFactoryTimeId); + Flash_SetProductTimeLimit (2000 + year, mon, day, hour, min, second, kFactoryTimeId); // 到期时间自动 + 5年30天 - Set_ExpirationTime(MAX_EXPIRATION_DAYS); + Set_ExpirationTime (MAX_EXPIRATION_DAYS); } else { - LOG_E("SYS_Set_RtcProductTime --use _cmd_ [2000 + y] [m] [d] [h] [m] [s]"); + LOG_E ("SYS_Set_RtcProductTime --use _cmd_ [2000 + y] [m] [d] [h] [m] [s]"); } } -MSH_CMD_EXPORT(SYS_Set_RtcProductTime, "SYS_Set_RtcProductTime"); -static void SYS_SW_Version(void) +MSH_CMD_EXPORT (SYS_Set_RtcProductTime, "SYS_Set_RtcProductTime"); + +static void SYS_SW_Version (void) { - rt_kprintf("JT-DT-YD4N02A Software version: V%02X\r\n", (uint8_t)Flash_Get_SysCfg(kSwVerId)); + rt_kprintf ("JT-DT-YD4N02A Software version: V%02X\r\n", (uint8_t)Flash_Get_SysCfg (kSwVerId)); } -MSH_CMD_EXPORT(SYS_SW_Version, "软件版本"); + +MSH_CMD_EXPORT (SYS_SW_Version, "软件版本"); #endif diff --git a/applications/user_sys.c b/applications/user_sys.c index d94d00e..7deb5db 100644 --- a/applications/user_sys.c +++ b/applications/user_sys.c @@ -253,7 +253,6 @@ rt_uint32_t IMEI_Delay(void) int BSP_SYS_Init(void) { - BSP_Flash_Init(); BSP_RTC_Init(); BSP_Rng_Init(); BSP_WDG_Init(); @@ -262,10 +261,7 @@ int BSP_SYS_Init(void) BSP_BEEP_Init(); BSP_EMV_Init(); BSP_BUTTON_Init(); - BSP_H308_Init(); BSP_HR_Init(); - nt26k_device_class_register(); - nt26k_device_register(); BSP_Nt26k_Thread_Init(); return 0; } diff --git a/bsp/inc/at_device_nt26k.h b/bsp/inc/at_device_nt26k.h index 15329b1..696b654 100644 --- a/bsp/inc/at_device_nt26k.h +++ b/bsp/inc/at_device_nt26k.h @@ -1,24 +1,3 @@ -/*** - * @Author: mbw - * @Date: 2024-10-09 08:42:14 - * @LastEditors: mbw && 1600520629@qq.com - * @LastEditTime: 2024-12-30 16:30:57 - * @FilePath: \JT-DT-YD4N02A_RTT_MRS-4G\bsp\inc\at_device_nt26k.h - * @Description: - * @ - * @Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. - */ -/*** - * @Author: mbw - * @Date: 2024-10-09 08:42:14 - * @LastEditors: mbw && 1600520629@qq.com - * @LastEditTime: 2024-10-14 14:25:58 - * @FilePath: \JT-DT-YD4N02A_RTT_MRS-4G\bsp\inc\at_device_nt26k.h - * @Description: - * @ - * @Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. - */ - #ifndef __AT_DEVICE_NT26K_H__ #define __AT_DEVICE_NT26K_H__ @@ -86,7 +65,6 @@ typedef struct __attribute__((packed)) extern nt26k_sys_info nt26k; extern rt_uint8_t nt26k_init_complate_flag; extern struct rt_completion nt26k_init_complate; -int nt26k_device_class_register(void); int at_send_data(struct at_device *device, const char *data, rt_size_t size); rt_err_t Nt26k_Reset(struct at_device *device); int Time_Calibration(struct at_device *device); diff --git a/bsp/inc/bsp_nt26k.h b/bsp/inc/bsp_nt26k.h index 9a37901..3038183 100644 --- a/bsp/inc/bsp_nt26k.h +++ b/bsp/inc/bsp_nt26k.h @@ -207,6 +207,5 @@ extern rt_sem_t nt26k_disconnect_sem; extern rt_uint8_t nt26k_conncet_tcp_flag; extern rt_uint8_t nt26k_disconnect_pdp_flag; extern rt_uint8_t power_on_send_flag; -int nt26k_device_register(void); int BSP_Nt26k_Thread_Init(void); void Nt26k_Send_Event(Nt26kEvent event_type); diff --git a/bsp/src/at_device_nt26k.c b/bsp/src/at_device_nt26k.c index 1ab275c..4853c2e 100644 --- a/bsp/src/at_device_nt26k.c +++ b/bsp/src/at_device_nt26k.c @@ -2,7 +2,7 @@ * @Author : stark1898y 1658608470@qq.com * @Date : 2024-09-04 13:33:49 * @LastEditors: mbw && 1600520629@qq.com - * @LastEditTime: 2025-01-13 09:46:29 + * @LastEditTime: 2025-01-15 09:27:15 * @FilePath: \JT-DT-YD4N02A_RTT_MRS-NT26K\bsp\src\at_device_nt26k.c * @Description : * @@ -33,16 +33,16 @@ #if IOT_MODULE_SWITCH == 1 -#define NT26K_POWER_OFF RT_FALSE -#define NT26K_POWER_ON RT_TRUE -#define NT26K_POWER_ON_TIME 3 -#define NT26K_POWER_OFF_TIME 4 +#define NT26K_POWER_OFF RT_FALSE +#define NT26K_POWER_ON RT_TRUE +#define NT26K_POWER_ON_TIME 3 +#define NT26K_POWER_OFF_TIME 4 #define AT_CLIENT_RECV_BUFF_LEN 512 -#define AT_DEFAULT_TIMEOUT 5000 +#define AT_DEFAULT_TIMEOUT 5000 #ifdef AT_DEVICE_USING_NT26K #define NT26K_WAIT_CONNECT_TIME 5000 #define NT26K_THREAD_STACK_SIZE 4096 -#define NT26K_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2) +#define NT26K_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2) char data_buf[AT_CLIENT_RECV_BUFF_LEN] = {0}; @@ -51,12 +51,12 @@ struct rt_completion nt26k_init_complate; volatile rt_uint8_t socket_id = 0; // 将本地时间转换为对应时区时间 -void Time_Zone_Conversion(TsRtcDateTime *timeInfo) +void Time_Zone_Conversion (TsRtcDateTime *timeInfo) { // 根据时区差异进行转换,这里时区差为8小时,没有分钟差异。 - timeInfo->hour += RT_LIBC_DEFAULT_TIMEZONE; // 小时部分加8小时 + timeInfo->hour += RT_LIBC_DEFAULT_TIMEZONE; // 小时部分加8小时 if (timeInfo->hour >= 24) - { // 如果超过24小时,则天数加1,小时数减24。 + { // 如果超过24小时,则天数加1,小时数减24。 timeInfo->day++; timeInfo->hour -= 24; if (timeInfo->day > 365) @@ -66,24 +66,24 @@ void Time_Zone_Conversion(TsRtcDateTime *timeInfo) } } -rt_err_t Nt26k_Reset(struct at_device *device) +rt_err_t Nt26k_Reset (struct at_device *device) { - LOG_I("NT26K Reset"); - rt_pin_mode(NT26K_RST_PIN, PIN_MODE_OUTPUT); - rt_pin_write(NT26K_RST_PIN, PIN_LOW); - rt_thread_mdelay(10); - rt_pin_write(NT26K_RST_PIN, PIN_HIGH); - rt_thread_mdelay(50); - rt_pin_write(NT26K_RST_PIN, PIN_LOW); + LOG_I ("NT26K Reset"); + rt_pin_mode (NT26K_RST_PIN, PIN_MODE_OUTPUT); + rt_pin_write (NT26K_RST_PIN, PIN_LOW); + rt_thread_mdelay (10); + rt_pin_write (NT26K_RST_PIN, PIN_HIGH); + rt_thread_mdelay (50); + rt_pin_write (NT26K_RST_PIN, PIN_LOW); return RT_EOK; } -static int nt26k_power_on(struct at_device *device) +static int nt26k_power_on (struct at_device *device) { struct at_device_nt26k *nt26k = RT_NULL; - nt26k = (struct at_device_nt26k *)device->user_data; + nt26k = (struct at_device_nt26k *)device->user_data; nt26k->power_status = RT_TRUE; /* not nead to set pin configuration for nt26k device power on */ @@ -92,21 +92,21 @@ static int nt26k_power_on(struct at_device *device) return (RT_EOK); } - rt_pin_write(nt26k->pwr_en_pin, PIN_LOW); - rt_thread_mdelay(10); - rt_pin_write(nt26k->pwr_en_pin, PIN_HIGH); + rt_pin_write (nt26k->pwr_en_pin, PIN_LOW); + rt_thread_mdelay (10); + rt_pin_write (nt26k->pwr_en_pin, PIN_HIGH); return (RT_EOK); } -static rt_err_t nt26k_power_off(struct at_device *device) +static rt_err_t nt26k_power_off (struct at_device *device) { struct at_device_nt26k *nt26k = RT_NULL; nt26k = (struct at_device_nt26k *)device->user_data; if (nt26k->power_status != RT_FALSE) { - rt_pin_mode(nt26k->pwr_en_pin, PIN_MODE_OUTPUT); - rt_pin_write(nt26k->pwr_en_pin, PIN_LOW); + rt_pin_mode (nt26k->pwr_en_pin, PIN_MODE_OUTPUT); + rt_pin_write (nt26k->pwr_en_pin, PIN_LOW); } return RT_EOK; @@ -115,223 +115,223 @@ static rt_err_t nt26k_power_off(struct at_device *device) #ifdef TEST_ENABLE void TEST_Nt26k_Reset() { - Nt26k_Reset(RT_NULL); + Nt26k_Reset (RT_NULL); } -MSH_CMD_EXPORT(TEST_Nt26k_Reset, "test_nt26k_reset"); +MSH_CMD_EXPORT (TEST_Nt26k_Reset, "test_nt26k_reset"); #endif -static int nt26k_sleep(struct at_device *device) +static int nt26k_sleep (struct at_device *device) { - at_response_t resp = RT_NULL; + at_response_t resp = RT_NULL; struct at_device_nt26k *nt26k = RT_NULL; nt26k = (struct at_device_nt26k *)device->user_data; - if (!nt26k->power_status) // power off + if (!nt26k->power_status) // power off { return (RT_EOK); } - if (nt26k->sleep_status) // is sleep status + if (nt26k->sleep_status) // is sleep status { return (RT_EOK); } - resp = at_create_resp(64, 0, rt_tick_from_millisecond(300)); + resp = at_create_resp (64, 0, rt_tick_from_millisecond (300)); if (resp == RT_NULL) { - LOG_D("no memory for resp create."); - at_delete_resp(resp); + LOG_D ("no memory for resp create."); + at_delete_resp (resp); return (-RT_ERROR); } /* enable sleep mode */ // Sleep1 - if (at_obj_exec_cmd(device->client, resp, "AT+ECPMUCFG=1,2") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+ECPMUCFG=1,2") != RT_EOK) { - LOG_D("enable sleep fail.\"AT+ECPMUCFG=1,2\" execute fail."); - at_delete_resp(resp); + LOG_D ("enable sleep fail.\"AT+ECPMUCFG=1,2\" execute fail."); + at_delete_resp (resp); return (-RT_ERROR); } /* enable PSM mode */ - if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=1,,,,\"00100010\"") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CPSMS=1,,,,\"00100010\"") != RT_EOK) { - LOG_D("enable sleep fail.\"AT+CPSMS=1,,,,\"00100010\" execute fail."); - at_delete_resp(resp); + LOG_D ("enable sleep fail.\"AT+CPSMS=1,,,,\"00100010\" execute fail."); + at_delete_resp (resp); return (-RT_ERROR); } nt26k->sleep_status = RT_TRUE; - at_delete_resp(resp); + at_delete_resp (resp); return (RT_EOK); } -static int nt26k_wakeup(struct at_device *device) +static int nt26k_wakeup (struct at_device *device) { struct at_device_nt26k *nt26k = RT_NULL; nt26k = (struct at_device_nt26k *)device->user_data; if (nt26k->pwr_key_pin != -1) { - rt_pin_write(nt26k->pwr_key_pin, PIN_LOW); - rt_thread_mdelay(100); - rt_pin_write(nt26k->pwr_key_pin, PIN_HIGH); + rt_pin_write (nt26k->pwr_key_pin, PIN_LOW); + rt_thread_mdelay (100); + rt_pin_write (nt26k->pwr_key_pin, PIN_HIGH); nt26k->sleep_status = RT_TRUE; } return (RT_EOK); } -static void urc_tcp_recv(struct at_client *client, const char *data, rt_size_t size) +static void urc_tcp_recv (struct at_client *client, const char *data, rt_size_t size) { - int len = 0; - rt_uint16_t crc16 = 0; - rt_uint16_t rec_crc16 = 0; - char crc16_buf[6] = {0}; + int len = 0; + rt_uint16_t crc16 = 0; + rt_uint16_t rec_crc16 = 0; + char crc16_buf[6] = {0}; rt_uint8_t recv_byte_buf[AT_CLIENT_RECV_BUFF_LEN] = {0}; - rt_memset(data_buf, 0, sizeof(data_buf)); + rt_memset (data_buf, 0, sizeof (data_buf)); - if (sscanf(data, "+QIURC: \"recv\",%*d,%d,%s", &len, data_buf) == 2) + if (sscanf (data, "+QIURC: \"recv\",%*d,%d,%s", &len, data_buf) == 2) { - HexStrToBytes(data_buf, recv_byte_buf, len * 2); - LOG_D("nt26k recv data: %s", data_buf); - crc16 = crc1021(data_buf, len * 2 - 10); // 去掉帧尾三个字节和2个字节的校验值,字符长度为10 + HexStrToBytes (data_buf, recv_byte_buf, len * 2); + LOG_D ("nt26k recv data: %s", data_buf); + crc16 = crc1021 (data_buf, len * 2 - 10); // 去掉帧尾三个字节和2个字节的校验值,字符长度为10 - rt_strncpy(crc16_buf, data_buf + len * 2 - 10, 4); - LOG_D("nt26k crc16: %04s", crc16_buf); - rec_crc16 = strtol(crc16_buf, NULL, 16); - if (crc16 != rec_crc16) // 看下数据接收的是否正确 + rt_strncpy (crc16_buf, data_buf + len * 2 - 10, 4); + LOG_D ("nt26k crc16: %04s", crc16_buf); + rec_crc16 = strtol (crc16_buf, NULL, 16); + if (crc16 != rec_crc16) // 看下数据接收的是否正确 { - LOG_E("nt26k recv data error {crc16 [%x]!= rec_crc16[%x]}", crc16, rec_crc16); + LOG_E ("nt26k recv data error {crc16 [%x]!= rec_crc16[%x]}", crc16, rec_crc16); } else { /*比较数组的长度和结构体的长度是否一致,如果不一致则数据解析错误,如果一致复制数组值到结构体中*/ - if (len == sizeof(struct Nt26kRecvData)) + if (len == sizeof (struct Nt26kRecvData)) { - rt_memset(nt26k_ops.recv, 0, sizeof(struct Nt26kRecvData)); // 清空结构体 - rt_memcpy(nt26k_ops.recv, recv_byte_buf, sizeof(struct Nt26kRecvData)); - if (nt26k_ops.recv->recv_data.event_type != INSTRUCTION_HEART_BEAT) // 加这个的原因就是如果有指令下发,会出现两个信号量,指令会执行两次,所以排除心跳包才释放信号量 + rt_memset (nt26k_ops.recv, 0, sizeof (struct Nt26kRecvData)); // 清空结构体 + rt_memcpy (nt26k_ops.recv, recv_byte_buf, sizeof (struct Nt26kRecvData)); + if (nt26k_ops.recv->recv_data.event_type != INSTRUCTION_HEART_BEAT) // 加这个的原因就是如果有指令下发,会出现两个信号量,指令会执行两次,所以排除心跳包才释放信号量 { - rt_sem_release(nt26k_recv_sem); + rt_sem_release (nt26k_recv_sem); } else { // 接收到的是心跳包 - rt_sem_release(nt26k_recv_heart_sem); + rt_sem_release (nt26k_recv_heart_sem); } } else { - LOG_E("nt26k recv data error {len [%d]!= sizeof(struct Nt26kRecvData)[%d]}", len, sizeof(struct Nt26kRecvData)); + LOG_E ("nt26k recv data error {len [%d]!= sizeof(struct Nt26kRecvData)[%d]}", len, sizeof (struct Nt26kRecvData)); } } } - rt_memset(recv_byte_buf, 0, sizeof(recv_byte_buf)); + rt_memset (recv_byte_buf, 0, sizeof (recv_byte_buf)); } -static void urc_device_reset(struct at_client *client, const char *data, rt_size_t size) +static void urc_device_reset (struct at_client *client, const char *data, rt_size_t size) { - LOG_D("device reset"); + LOG_D ("device reset"); nt26k_conncet_tcp_flag = 0; } -static void urc_tcp_disconnect(struct at_client *client, const char *data, rt_size_t size) +static void urc_tcp_disconnect (struct at_client *client, const char *data, rt_size_t size) { size_t id; - if (sscanf(data, "+QIURC: \"closed\",%d", &id) == 1) + if (sscanf (data, "+QIURC: \"closed\",%d", &id) == 1) { if (id == socket_id) { - LOG_W("socket %d disconnect", id); + LOG_W ("socket %d disconnect", id); nt26k_conncet_tcp_flag = 0; - rt_sem_release(nt26k_disconnect_sem); + rt_sem_release (nt26k_disconnect_sem); } } } static const struct at_urc urc_table[] = { - {"+QIURC: \"recv\"", "\r\n", urc_tcp_recv}, - {"boot.rom", "\r\n", urc_device_reset}, // 这个是软件复位时, - {"Lierda", "\r\n", urc_device_reset}, // 这个时硬件复位时回复 + {"+QIURC: \"recv\"", "\r\n", urc_tcp_recv }, + {"boot.rom", "\r\n", urc_device_reset }, // 这个是软件复位时, + {"Lierda", "\r\n", urc_device_reset }, // 这个时硬件复位时回复 {"+QIURC: \"closed\"", "\r\n", urc_tcp_disconnect}, // tcp连接断开 }; -void show_resp_info(at_response_t resp) +void show_resp_info (at_response_t resp) { - RT_ASSERT(resp); + RT_ASSERT (resp); /* Print response line buffer */ const char *line_buffer = RT_NULL; for (rt_size_t line_num = 1; line_num <= resp->line_counts; line_num++) { - if ((line_buffer = at_resp_get_line(resp, line_num)) != RT_NULL) - LOG_I("line %d buffer : %s", line_num, line_buffer); + if ((line_buffer = at_resp_get_line (resp, line_num)) != RT_NULL) + LOG_I ("line %d buffer : %s", line_num, line_buffer); else - LOG_I("Parse line buffer error!"); + LOG_I ("Parse line buffer error!"); } return; } -int at_device_nt26k_disconnect_tcp(struct at_device *device) +int at_device_nt26k_disconnect_tcp (struct at_device *device) { #define NT26K_CLOSE_REP_TIME (5 * AT_DEFAULT_TIMEOUT) - at_response_t resp = at_create_resp(AT_CLIENT_RECV_BUFF_LEN, 0, NT26K_CLOSE_REP_TIME); + at_response_t resp = at_create_resp (AT_CLIENT_RECV_BUFF_LEN, 0, NT26K_CLOSE_REP_TIME); if (resp == RT_NULL) { - LOG_E("No memory for response structure!"); - at_delete_resp(resp); + LOG_E ("No memory for response structure!"); + at_delete_resp (resp); return -RT_ENOMEM; } - if (at_obj_exec_cmd(device->client, resp, TCP_CLOSE_SOCKET, socket_id) != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, TCP_CLOSE_SOCKET, socket_id) != RT_EOK) { - at_delete_resp(resp); + at_delete_resp (resp); return -RT_ERROR; } nt26k_conncet_tcp_flag = 0; - at_delete_resp(resp); + at_delete_resp (resp); return RT_EOK; } -static int at_device_nt26k_connect_tcp(struct at_device *device) +static int at_device_nt26k_connect_tcp (struct at_device *device) { if (nt26k_conncet_tcp_flag) return RT_EOK; - ASSERT(device); - int err_code = 0; - at_response_t resp = at_create_resp(64, 4, AT_DEFAULT_TIMEOUT); + ASSERT (device); + int err_code = 0; + at_response_t resp = at_create_resp (64, 4, AT_DEFAULT_TIMEOUT); if (resp == RT_NULL) { - LOG_E("No memory for response structure!"); - at_delete_resp(resp); + LOG_E ("No memory for response structure!"); + at_delete_resp (resp); return -RT_ENOMEM; } - if (Flash_Get_Sever_Data(&sever_info) != RT_EOK) + if (Flash_Get_Sever_Data (&sever_info) != RT_EOK) { - LOG_E("Get Sever Data Failed"); - at_delete_resp(resp); + LOG_E ("Get Sever Data Failed"); + at_delete_resp (resp); return RT_ERROR; } - if (at_obj_exec_cmd(device->client, resp, TCP_CONNECT_CMD, sever_info.server_url, sever_info.server_port) == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, TCP_CONNECT_CMD, sever_info.server_url, sever_info.server_port) == RT_EOK) { - if (at_resp_parse_line_args_by_kw(resp, "+QIOPEN:", "+QIOPEN:%d,%d", &socket_id, &err_code) > 0) + if (at_resp_parse_line_args_by_kw (resp, "+QIOPEN:", "+QIOPEN:%d,%d", &socket_id, &err_code) > 0) { if (err_code == 0) { - LOG_D("nt26k connect to tcp server success"); + LOG_D ("nt26k connect to tcp server success"); nt26k_conncet_tcp_flag = 1; - at_delete_resp(resp); + at_delete_resp (resp); return RT_EOK; } else { - LOG_E("nt26k connect to tcp server failed. error code: %d", err_code); + LOG_E ("nt26k connect to tcp server failed. error code: %d", err_code); if (err_code == 570) { nt26k_disconnect_pdp_flag = 1; @@ -339,65 +339,65 @@ static int at_device_nt26k_connect_tcp(struct at_device *device) } } } - at_delete_resp(resp); + at_delete_resp (resp); return RT_ERROR; } -int at_send_data(struct at_device *device, const char *data, rt_size_t size) +int at_send_data (struct at_device *device, const char *data, rt_size_t size) { #define NT26K_SEND_RESP_TIME (3000) - const char *line_buffer = RT_NULL; - rt_uint8_t retry = 0; - at_response_t resp = at_create_resp(AT_CLIENT_RECV_BUFF_LEN / 4, 2, NT26K_SEND_RESP_TIME); - rt_uint8_t max_retry_count = (rt_uint8_t)Flash_Get_SysCfg(kIotRetryId); + const char *line_buffer = RT_NULL; + rt_uint8_t retry = 0; + at_response_t resp = at_create_resp (AT_CLIENT_RECV_BUFF_LEN / 4, 2, NT26K_SEND_RESP_TIME); + rt_uint8_t max_retry_count = (rt_uint8_t)Flash_Get_SysCfg (kIotRetryId); if (resp == RT_NULL) { - LOG_E("no memory for nt26k device(%s) response structure.", device->name); - at_delete_resp(resp); + LOG_E ("no memory for nt26k device(%s) response structure.", device->name); + at_delete_resp (resp); return -RT_ERROR; } while (retry < max_retry_count) { - rt_thread_mdelay(IMEI_Delay()); // 获取一个随机延时 - if (at_obj_exec_cmd(device->client, resp, TCP_SEND_DATA, socket_id, data) == RT_EOK) + rt_thread_mdelay (IMEI_Delay()); // 获取一个随机延时 + if (at_obj_exec_cmd (device->client, resp, TCP_SEND_DATA, socket_id, data) == RT_EOK) { - if ((line_buffer = at_resp_get_line(resp, 2)) != RT_NULL) + if ((line_buffer = at_resp_get_line (resp, 2)) != RT_NULL) { - if (rt_strstr(line_buffer, "SEND OK") != RT_NULL) + if (rt_strstr (line_buffer, "SEND OK") != RT_NULL) { - LOG_D("send data success, socket_id: %d", socket_id); - at_delete_resp(resp); + LOG_D ("send data success, socket_id: %d", socket_id); + at_delete_resp (resp); return RT_EOK; } } } retry++; - rt_thread_mdelay(1000); + rt_thread_mdelay (1000); } - at_delete_resp(resp); + at_delete_resp (resp); return -RT_ERROR; } -int nt26k_check_link_status(struct at_device *device) +int nt26k_check_link_status (struct at_device *device) { - at_response_t resp = RT_NULL; - int result = -RT_ERROR; + at_response_t resp = RT_NULL; + int result = -RT_ERROR; - resp = at_create_resp(64, 0, rt_tick_from_millisecond(300)); + resp = at_create_resp (64, 0, rt_tick_from_millisecond (300)); if (resp == RT_NULL) { - LOG_D("no memory for resp create."); - at_delete_resp(resp); + LOG_D ("no memory for resp create."); + at_delete_resp (resp); return (-RT_ERROR); } result = -RT_ERROR; - if (at_obj_exec_cmd(device->client, resp, "AT+CEREG?") == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CEREG?") == RT_EOK) { int link_stat = 0; - if (at_resp_parse_line_args_by_kw(resp, "+CEREG:", "+CEREG: %*d,%d", &link_stat) > 0) + if (at_resp_parse_line_args_by_kw (resp, "+CEREG:", "+CEREG: %*d,%d", &link_stat) > 0) { if (link_stat == 1 || link_stat == 5) { @@ -405,53 +405,53 @@ int nt26k_check_link_status(struct at_device *device) } } } - if (at_obj_exec_cmd(device->client, resp, "AT+QICFG=\"tcp/keepalive\",1,600,100,6") != RT_EOK)//保活期为最大30min,故30min重设一下 + if (at_obj_exec_cmd (device->client, resp, "AT+QICFG=\"tcp/keepalive\",1,600,100,6") != RT_EOK) // 保活期为最大30min,故30min重设一下 { result = -RT_ERROR; } - at_delete_resp(resp); + at_delete_resp (resp); return (result); } /* ============================= nt26k network interface operations ============================= */ /* set nt26k network interface device status and address information */ -int nt26k_netdev_set_info(struct netdev *netdev) +int nt26k_netdev_set_info (struct netdev *netdev) { #define NT26K_INFO_RESP_SIZE 128 -#define NT26K_INFO_RESP_TIMO rt_tick_from_millisecond(1000) +#define NT26K_INFO_RESP_TIMO rt_tick_from_millisecond (1000) - int result = RT_EOK; - at_response_t resp = RT_NULL; + int result = RT_EOK; + at_response_t resp = RT_NULL; struct at_device *device = RT_NULL; - RT_ASSERT(netdev); + RT_ASSERT (netdev); - device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + device = at_device_get_by_name (AT_DEVICE_NAMETYPE_NETDEV, netdev->name); if (device == RT_NULL) { - LOG_E("get device(%s) failed.", netdev->name); + LOG_E ("get device(%s) failed.", netdev->name); return -RT_ERROR; } /* set network interface device status */ - netdev_low_level_set_status(netdev, RT_TRUE); - netdev_low_level_set_link_status(netdev, RT_TRUE); - netdev_low_level_set_dhcp_status(netdev, RT_TRUE); + netdev_low_level_set_status (netdev, RT_TRUE); + netdev_low_level_set_link_status (netdev, RT_TRUE); + netdev_low_level_set_dhcp_status (netdev, RT_TRUE); - resp = at_create_resp(NT26K_INFO_RESP_SIZE, 0, NT26K_INFO_RESP_TIMO); + resp = at_create_resp (NT26K_INFO_RESP_SIZE, 0, NT26K_INFO_RESP_TIMO); if (resp == RT_NULL) { - LOG_E("no memory for resp create."); - at_delete_resp(resp); + LOG_E ("no memory for resp create."); + at_delete_resp (resp); result = -RT_ENOMEM; goto __exit; } - device->class->device_ops->control(device, AT_DEVICE_CTRL_NET_CONN, RT_NULL); // 打开连接 + device->class->device_ops->control (device, AT_DEVICE_CTRL_NET_CONN, RT_NULL); // 打开连接 __exit: if (resp) { - at_delete_resp(resp); + at_delete_resp (resp); } return result; @@ -462,215 +462,215 @@ __exit: * * @param netdev: nt26k network interface device */ -static void nt26k_check_link_status_entry(void *parameter) +static void nt26k_check_link_status_entry (void *parameter) { #define NT26K_LINK_DELAY_TIME (10 * 60 * RT_TICK_PER_SECOND) -#define NT26K_LINK_STATUS_OK 1 +#define NT26K_LINK_STATUS_OK 1 int link_status; struct at_device *device = RT_NULL; - struct netdev *netdev = (struct netdev *)parameter; + struct netdev *netdev = (struct netdev *)parameter; - device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + device = at_device_get_by_name (AT_DEVICE_NAMETYPE_NETDEV, netdev->name); if (device == RT_NULL) { - LOG_E("get device(%s) failed.", netdev->name); + LOG_E ("get device(%s) failed.", netdev->name); return; } while (1) { - link_status = nt26k_check_link_status(device); + link_status = nt26k_check_link_status (device); if (link_status < 0) { - rt_thread_mdelay(NT26K_LINK_DELAY_TIME); + rt_thread_mdelay (NT26K_LINK_DELAY_TIME); continue; } /* check the network interface device link status */ - if ((NT26K_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev)) + if ((NT26K_LINK_STATUS_OK == link_status) != netdev_is_link_up (netdev)) { - netdev_low_level_set_link_status(netdev, (NT26K_LINK_STATUS_OK == link_status)); + netdev_low_level_set_link_status (netdev, (NT26K_LINK_STATUS_OK == link_status)); } - rt_thread_delay(NT26K_LINK_DELAY_TIME); + rt_thread_delay (NT26K_LINK_DELAY_TIME); } } -int nt26k_netdev_check_link_status(struct netdev *netdev) +int nt26k_netdev_check_link_status (struct netdev *netdev) { -#define NT26K_LINK_THREAD_TICK 20 +#define NT26K_LINK_THREAD_TICK 20 #define NT26K_LINK_THREAD_STACK_SIZE (1024 + 512) -#define NT26K_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2) +#define NT26K_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2) rt_thread_t tid; char tname[RT_NAME_MAX] = {0}; - RT_ASSERT(netdev); + RT_ASSERT (netdev); - rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name); + rt_snprintf (tname, RT_NAME_MAX, "%s", netdev->name); /* create nt26k link status polling thread */ - tid = rt_thread_create(tname, nt26k_check_link_status_entry, (void *)netdev, - NT26K_LINK_THREAD_STACK_SIZE, NT26K_LINK_THREAD_PRIORITY, NT26K_LINK_THREAD_TICK); + tid = rt_thread_create (tname, nt26k_check_link_status_entry, (void *)netdev, + NT26K_LINK_THREAD_STACK_SIZE, NT26K_LINK_THREAD_PRIORITY, NT26K_LINK_THREAD_TICK); if (tid != RT_NULL) { - rt_thread_startup(tid); + rt_thread_startup (tid); } return RT_EOK; } -static int nt26k_net_init(struct at_device *device); +static int nt26k_net_init (struct at_device *device); -static int nt26k_netdev_set_up(struct netdev *netdev) +static int nt26k_netdev_set_up (struct netdev *netdev) { struct at_device *device = RT_NULL; - device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + device = at_device_get_by_name (AT_DEVICE_NAMETYPE_NETDEV, netdev->name); if (device == RT_NULL) { - LOG_E("get device(%s) failed.", netdev->name); + LOG_E ("get device(%s) failed.", netdev->name); return -RT_ERROR; } if (device->is_init == RT_FALSE) { - nt26k_net_init(device); + nt26k_net_init (device); device->is_init = RT_TRUE; - netdev_low_level_set_status(netdev, RT_TRUE); - LOG_D("network interface device(%s) set up status.", netdev->name); + netdev_low_level_set_status (netdev, RT_TRUE); + LOG_D ("network interface device(%s) set up status.", netdev->name); } return RT_EOK; } -static int nt26k_netdev_set_down(struct netdev *netdev) +static int nt26k_netdev_set_down (struct netdev *netdev) { struct at_device *device = RT_NULL; - device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + device = at_device_get_by_name (AT_DEVICE_NAMETYPE_NETDEV, netdev->name); if (device == RT_NULL) { - LOG_E("get device(%s) failed.", netdev->name); + LOG_E ("get device(%s) failed.", netdev->name); return -RT_ERROR; } if (device->is_init == RT_TRUE) { - nt26k_power_off(device); + nt26k_power_off (device); device->is_init = RT_FALSE; - netdev_low_level_set_status(netdev, RT_FALSE); - LOG_D("network interface device(%s) set down status.", netdev->name); + netdev_low_level_set_status (netdev, RT_FALSE); + LOG_D ("network interface device(%s) set down status.", netdev->name); } return RT_EOK; } -static int nt26k_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server) +static int nt26k_netdev_set_dns_server (struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server) { -#define NT26K_DNS_RESP_LEN 64 -#define NT26K_DNS_RESP_TIMEO rt_tick_from_millisecond(300) +#define NT26K_DNS_RESP_LEN 64 +#define NT26K_DNS_RESP_TIMEO rt_tick_from_millisecond (300) - int result = RT_EOK; - at_response_t resp = RT_NULL; + int result = RT_EOK; + at_response_t resp = RT_NULL; struct at_device *device = RT_NULL; - RT_ASSERT(netdev); - RT_ASSERT(dns_server); + RT_ASSERT (netdev); + RT_ASSERT (dns_server); - device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + device = at_device_get_by_name (AT_DEVICE_NAMETYPE_NETDEV, netdev->name); if (device == RT_NULL) { - LOG_E("get device(%s) failed.", netdev->name); + LOG_E ("get device(%s) failed.", netdev->name); return -RT_ERROR; } - resp = at_create_resp(NT26K_DNS_RESP_LEN, 0, NT26K_DNS_RESP_TIMEO); + resp = at_create_resp (NT26K_DNS_RESP_LEN, 0, NT26K_DNS_RESP_TIMEO); if (resp == RT_NULL) { - LOG_D("no memory for resp create."); - at_delete_resp(resp); + LOG_D ("no memory for resp create."); + at_delete_resp (resp); result = -RT_ENOMEM; goto __exit; } /* send "AT+QIDNSCFG=[,]" commond to set dns servers */ - if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG=%d,%s", - dns_num, inet_ntoa(*dns_server)) != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+QIDNSCFG=%d,%s", + dns_num, inet_ntoa (*dns_server)) != RT_EOK) { result = -RT_ERROR; goto __exit; } - netdev_low_level_set_dns_server(netdev, dns_num, dns_server); + netdev_low_level_set_dns_server (netdev, dns_num, dns_server); __exit: if (resp) { - at_delete_resp(resp); + at_delete_resp (resp); } return result; } #ifdef NETDEV_USING_PING -static int nt26k_netdev_ping(struct netdev *netdev, const char *host, size_t data_len, - uint32_t timeout, struct netdev_ping_resp *ping_resp +static int nt26k_netdev_ping (struct netdev *netdev, const char *host, size_t data_len, + uint32_t timeout, struct netdev_ping_resp *ping_resp #if RT_VER_NUM >= 0x50100 - , - rt_bool_t is_bind + , + rt_bool_t is_bind #endif ) { #define NT26K_PING_RESP_SIZE 128 -#define NT26K_PING_IP_SIZE 16 -#define NT26K_PING_TIMEO (NT26K_CON_REP_TIME) +#define NT26K_PING_IP_SIZE 16 +#define NT26K_PING_TIMEO (NT26K_CON_REP_TIME) - rt_err_t result = RT_EOK; - int response = -1, recv_data_len, ping_time, ttl; + rt_err_t result = RT_EOK; + int response = -1, recv_data_len, ping_time, ttl; char ip_addr[NT26K_PING_IP_SIZE] = {0}; - at_response_t resp = RT_NULL; - struct at_device *device = RT_NULL; + at_response_t resp = RT_NULL; + struct at_device *device = RT_NULL; #if RT_VER_NUM >= 0x50100 - RT_UNUSED(is_bind); + RT_UNUSED (is_bind); #endif - RT_ASSERT(netdev); - RT_ASSERT(host); - RT_ASSERT(ping_resp); + RT_ASSERT (netdev); + RT_ASSERT (host); + RT_ASSERT (ping_resp); - device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + device = at_device_get_by_name (AT_DEVICE_NAMETYPE_NETDEV, netdev->name); if (device == RT_NULL) { - LOG_E("get device(%s) failed.", netdev->name); + LOG_E ("get device(%s) failed.", netdev->name); return -RT_ERROR; } - resp = at_create_resp(NT26K_PING_RESP_SIZE, 4, NT26K_PING_TIMEO); + resp = at_create_resp (NT26K_PING_RESP_SIZE, 4, NT26K_PING_TIMEO); if (resp == RT_NULL) { - LOG_E("no memory for resp create"); - at_delete_resp(resp); + LOG_E ("no memory for resp create"); + at_delete_resp (resp); return -RT_ENOMEM; } /* send "AT+QPING=""[,[][,]]" commond to send ping request */ - if (at_obj_exec_cmd(device->client, resp, "AT+QPING=1,\"%s\",%d,1", host, timeout / RT_TICK_PER_SECOND) < 0) + if (at_obj_exec_cmd (device->client, resp, "AT+QPING=1,\"%s\",%d,1", host, timeout / RT_TICK_PER_SECOND) < 0) { result = -RT_ERROR; goto __exit; } - at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d", &response); + at_resp_parse_line_args_by_kw (resp, "+QPING:", "+QPING:%d", &response); /* Received the ping response from the server */ if (response == 0) { - if (at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d,\"%[^\"]\",%d,%d,%d", - &response, ip_addr, &recv_data_len, &ping_time, &ttl) <= 0) + if (at_resp_parse_line_args_by_kw (resp, "+QPING:", "+QPING:%d,\"%[^\"]\",%d,%d,%d", + &response, ip_addr, &recv_data_len, &ping_time, &ttl) <= 0) { result = -RT_ERROR; goto __exit; @@ -680,25 +680,25 @@ static int nt26k_netdev_ping(struct netdev *netdev, const char *host, size_t dat /* prase response number */ switch (response) { - case 0: - inet_aton(ip_addr, &(ping_resp->ip_addr)); - ping_resp->data_len = recv_data_len; - ping_resp->ticks = ping_time; - ping_resp->ttl = ttl; - result = RT_EOK; - break; - case 569: - result = -RT_ETIMEOUT; - break; - default: - result = -RT_ERROR; - break; + case 0: + inet_aton (ip_addr, &(ping_resp->ip_addr)); + ping_resp->data_len = recv_data_len; + ping_resp->ticks = ping_time; + ping_resp->ttl = ttl; + result = RT_EOK; + break; + case 569: + result = -RT_ETIMEOUT; + break; + default: + result = -RT_ERROR; + break; } __exit: if (resp) { - at_delete_resp(resp); + at_delete_resp (resp); } return result; @@ -720,36 +720,36 @@ const struct netdev_ops nt26k_netdev_ops = RT_NULL, }; -static struct netdev *nt26k_netdev_add(const char *netdev_name) +static struct netdev *nt26k_netdev_add (const char *netdev_name) { #define ETHERNET_MTU 1380 -#define HWADDR_LEN 8 +#define HWADDR_LEN 8 struct netdev *netdev = RT_NULL; - netdev = netdev_get_by_name(netdev_name); + netdev = netdev_get_by_name (netdev_name); if (netdev != RT_NULL) { return (netdev); } - netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev)); + netdev = (struct netdev *)rt_calloc (1, sizeof (struct netdev)); if (netdev == RT_NULL) { - LOG_E("no memory for netdev create."); + LOG_E ("no memory for netdev create."); return RT_NULL; } - netdev->mtu = ETHERNET_MTU; - netdev->ops = &nt26k_netdev_ops; + netdev->mtu = ETHERNET_MTU; + netdev->ops = &nt26k_netdev_ops; netdev->hwaddr_len = HWADDR_LEN; #ifdef SAL_USING_AT - extern int sal_at_netdev_set_pf_info(struct netdev * netdev); + extern int sal_at_netdev_set_pf_info (struct netdev * netdev); /* set the network interface socket/netdb operations */ - sal_at_netdev_set_pf_info(netdev); + sal_at_netdev_set_pf_info (netdev); #endif - netdev_register(netdev, netdev_name, RT_NULL); + netdev_register (netdev, netdev_name, RT_NULL); return netdev; } @@ -757,163 +757,140 @@ static struct netdev *nt26k_netdev_add(const char *netdev_name) /* ============================= nt26k device operations ============================= */ /* initialize for nt26k */ -static void nt26k_init_thread_entry(void *parameter) +static void nt26k_init_thread_entry (void *parameter) { -#define INIT_RETRY 5 -#define CPIN_RETRY 5 -#define CSQ_RETRY 20 -#define CEREG_RETRY 50 +#define INIT_RETRY 5 +#define CPIN_RETRY 10 +#define CSQ_RETRY 20 +#define CEREG_RETRY 50 #define IPADDR_RETRY 10 int i; - rt_err_t result = RT_EOK; - at_response_t resp = RT_NULL; + rt_err_t result = RT_EOK; + at_response_t resp = RT_NULL; struct at_device *device = (struct at_device *)parameter; struct at_client *client = device->client; - resp = at_create_resp(128, 0, rt_tick_from_millisecond(300)); + resp = at_create_resp (128, 0, rt_tick_from_millisecond (300)); if (resp == RT_NULL) { - LOG_E("no memory for resp create."); - at_delete_resp(resp); + LOG_E ("no memory for resp create."); + at_delete_resp (resp); return; } - LOG_D("start init %s device.", device->name); + LOG_D ("start init %s device.", device->name); while (1) { /* power on the nt26k device */ - nt26k_power_off(device); - rt_thread_mdelay(100); - nt26k_power_on(device); - rt_thread_mdelay(1000); - + nt26k_power_off (device); + rt_thread_mdelay (100); + nt26k_power_on (device); + rt_thread_mdelay (100); /* wait nt26k startup finish, send AT every 500ms, if receive OK, SYNC success*/ - if (at_client_obj_wait_connect(client, NT26K_WAIT_CONNECT_TIME)) + if (at_client_obj_wait_connect (client, NT26K_WAIT_CONNECT_TIME)) { result = -RT_ETIMEOUT; goto __exit; } /* disable echo */ - if (at_obj_exec_cmd(device->client, resp, "ATE0") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "ATE0") != RT_EOK) { result = -RT_ERROR; goto __exit; } /* get module version */ - if (at_obj_exec_cmd(device->client, resp, "AT+CGMR") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CGMR") != RT_EOK) { result = -RT_ERROR; goto __exit; } for (i = 0; i < (int)resp->line_counts - 1; i++) { - LOG_D("%s", at_resp_get_line(resp, i + 1)); + LOG_D ("%s", at_resp_get_line (resp, i + 1)); } - // // AT+QSIMDET=0,1 - // if (at_obj_exec_cmd(device->client, resp, "AT+QSIMDET=0,1") != RT_EOK) - // { - // result = -RT_ERROR; - // goto __exit; - // } /* disable sleep mode */ - if (at_obj_exec_cmd(device->client, resp, "AT+ECPMUCFG=1,0") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+ECPMUCFG=1,0") != RT_EOK) { result = -RT_ERROR; goto __exit; } /* disable PSM mode */ - if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=0") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CPSMS=0") != RT_EOK) { result = -RT_ERROR; goto __exit; } /*AT+QICFG="dataformat" 设置收发模式*/ - if (at_obj_exec_cmd(device->client, resp, "AT+QICFG=\"dataformat\",%d,%d", AT_NSONMI_MODE_DEFAULT, AT_NSONMI_MODE_DEFAULT) != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+QICFG=\"dataformat\",%d,%d", AT_NSONMI_MODE_DEFAULT, AT_NSONMI_MODE_DEFAULT) != RT_EOK) { result = -RT_ERROR; goto __exit; } /*设置显示模式*/ - if (at_obj_exec_cmd(device->client, resp, "AT+QICFG=\"viewmode\",1") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+QICFG=\"viewmode\",1") != RT_EOK) { result = -RT_ERROR; goto __exit; } /*设置保活信息*/ - if (at_obj_exec_cmd(device->client, resp, "AT+QICFG=\"tcp/keepalive\",1,1800,50,5") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+QICFG=\"tcp/keepalive\",1,1800,50,5") != RT_EOK) { result = -RT_ERROR; goto __exit; } - if (at_obj_exec_cmd(device->client, resp, "AT+QICFG=\"passiveclosed\",1") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+QICFG=\"passiveclosed\",1") != RT_EOK) { result = -RT_ERROR; goto __exit; } - /* set network interface device hardware address(IMEI) */ - { -#define NT26K_NETDEV_HWADDR_LEN 8 -#define NT26K_IMEI_LEN 15 - - char imei[NT26K_IMEI_LEN] = {0}; - /* send "AT+CGSN" commond to get device IMEI */ - if (at_obj_exec_cmd(device->client, resp, "AT+CGSN") != RT_EOK) - { - result = -RT_ERROR; - goto __exit; - } - if (at_resp_parse_line_args_by_kw(resp, "86", "%s", imei) <= 0) - { - LOG_E("%s device prase \"AT+CGSN=1\" cmd error.", device->name); - result = -RT_ERROR; - goto __exit; - } - - LOG_D("%s device IMEI number: %s", device->name, imei); - - rt_memcpy(nt26k.imei, imei, NT26K_IMEI_LEN); - - } /* check SIM card */ for (i = 0; i < CPIN_RETRY; i++) { - if (at_obj_exec_cmd(device->client, resp, "AT+CPIN?") == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CPIN?") == RT_EOK) { - if (at_resp_get_line_by_kw(resp, "+CPIN: READY") != RT_NULL) + if (at_resp_get_line_by_kw (resp, "+CPIN: READY") != RT_NULL) break; } - rt_thread_mdelay(2000); + rt_thread_mdelay (1000); } if (i == CPIN_RETRY) { - LOG_E("%s device SIM card detection failed.", device->name); + LOG_E ("%s device SIM card detection failed.", device->name); result = -RT_ERROR; goto __exit; } - /*网络注册 */ - for (i = 0; i < CSQ_RETRY; i++) - { - if (at_obj_exec_cmd(device->client, resp, "AT+CREG=2") == RT_EOK) - { - if (at_resp_get_line_by_kw(resp, "OK") > 0) - { - LOG_D("%s device network registration success.", device->name); - break; - } - } - rt_thread_mdelay(1000); - } - if (i == CSQ_RETRY) + /* set network interface device hardware address(IMEI) */ + if ((*(rt_uint16_t *)FLASH_NB_IMEI_ADDR == 0xE339) || (*(rt_uint16_t *)FLASH_NB_IMEI_ADDR == 0x39E3)) // 未写入 { - LOG_D("%s device network registration failed.", device->name); - result = -RT_ERROR; - goto __exit; +#define NT26K_NETDEV_HWADDR_LEN 8 +#define NT26K_IMEI_LEN 15 + + char imei[NT26K_IMEI_LEN] = {0}; + /* send "AT+CGSN" commond to get device IMEI */ + if (at_obj_exec_cmd (device->client, resp, "AT+CGSN") != RT_EOK) + { + result = -RT_ERROR; + goto __exit; + } + if (at_resp_parse_line_args_by_kw (resp, "86", "%s", imei) <= 0) + { + LOG_E ("%s device prase \"AT+CGSN=1\" cmd error.", device->name); + result = -RT_ERROR; + goto __exit; + } + + LOG_D ("%s device IMEI number: %s", device->name, imei); + + rt_memcpy (nt26k.imei, imei, NT26K_IMEI_LEN); + Flash_Sys_Cfg (kIotImeiId, nt26k.imei, NT26K_IMEI_LEN); } + rt_thread_mdelay (1); /* set network interface device hardware iccid */ + if ((*(rt_uint16_t *)FLASH_NB_ICCID_ADDR == 0xE339) || (*(rt_uint16_t *)FLASH_NB_ICCID_ADDR == 0x39E3)) // 未写入 { #define NT26K_ICCID_LEN 20 @@ -921,25 +898,26 @@ static void nt26k_init_thread_entry(void *parameter) char iccid[NT26K_ICCID_LEN] = {0}; /* send "AT+ECICCID" commond to get device iccid */ - if (at_obj_exec_cmd(device->client, resp, "AT+QCCID") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+QCCID") != RT_EOK) { result = -RT_ERROR; goto __exit; } - if (at_resp_parse_line_args_by_kw(resp, "+QCCID:", "+QCCID:%s", iccid) <= 0) + if (at_resp_parse_line_args_by_kw (resp, "+QCCID:", "+QCCID:%s", iccid) <= 0) { - LOG_E("%s device prase \"AT+ECICCID\" cmd error.", device->name); + LOG_E ("%s device prase \"AT+ECICCID\" cmd error.", device->name); result = -RT_ERROR; goto __exit; } - LOG_D("%s device iccid number: %s", device->name, iccid); + LOG_D ("%s device iccid number: %s", device->name, iccid); - rt_memcpy(nt26k.iccid, iccid, NT26K_ICCID_LEN); - + rt_memcpy (nt26k.iccid, iccid, NT26K_ICCID_LEN); + Flash_Sys_Cfg (kIotIccidId, nt26k.iccid, NT26K_ICCID_LEN); } - + rt_thread_mdelay (1); /* set network interface device hardware imsi */ + if ((*(rt_uint16_t *)FLASH_NB_IMSI_ADDR == 0xE339) || (*(rt_uint16_t *)FLASH_NB_IMSI_ADDR == 0x39E3)) // 未写入 { #define NT26K_IMSI_LEN 15 @@ -947,79 +925,100 @@ static void nt26k_init_thread_entry(void *parameter) char imsi[NT26K_IMSI_LEN] = {0}; /* send "AT+CIMI" commond to get device imsi */ - if (at_obj_exec_cmd(device->client, resp, "AT+CIMI") != RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CIMI") != RT_EOK) { result = -RT_ERROR; goto __exit; } - if (at_resp_parse_line_args_by_kw(resp, "46", "%s", imsi) <= 0) + if (at_resp_parse_line_args_by_kw (resp, "46", "%s", imsi) <= 0) { - LOG_E("%s device prase \"AT+CIMI\" cmd error.", device->name); + LOG_E ("%s device prase \"AT+CIMI\" cmd error.", device->name); result = -RT_ERROR; goto __exit; } - LOG_D("%s device imsi number: %s", device->name, imsi); + LOG_D ("%s device imsi number: %s", device->name, imsi); - rt_memcpy(nt26k.imsi, imsi, NT26K_IMSI_LEN); - + rt_memcpy (nt26k.imsi, imsi, NT26K_IMSI_LEN); + Flash_Sys_Cfg (kIotImsiId, nt26k.imsi, NT26K_IMSI_LEN); } - - /* check signal strength */ + rt_thread_mdelay (1); + /*网络注册 */ for (i = 0; i < CSQ_RETRY; i++) { - if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CREG=2") == RT_EOK) { - int signal_strength = 0, err_rate = 0; - - if (at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d,%d", &signal_strength, &err_rate) > 0) + if (at_resp_get_line_by_kw (resp, "OK") > 0) { - if ((signal_strength != 99) && (signal_strength != 0)) - { - LOG_D("%s device signal strength: %d, channel bit err_rate: %d", - device->name, signal_strength, err_rate); - nt26k.rssi = signal_strength; - break; - } + LOG_D ("%s device network registration success.", device->name); + break; } } - rt_thread_mdelay(1000); + rt_thread_mdelay (1000); } if (i == CSQ_RETRY) { - LOG_E("%s device signal strength check failed", device->name); + LOG_D ("%s device network registration failed.", device->name); result = -RT_ERROR; goto __exit; } + // /* check signal strength */ + // for (i = 0; i < CSQ_RETRY; i++) + // { + + // if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == RT_EOK) + // { + // int signal_strength = 0, err_rate = 0; + + // if (at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d,%d", &signal_strength, &err_rate) > 0) + // { + // if ((signal_strength != 99) && (signal_strength != 0)) + // { + // LOG_D("%s device signal strength: %d, channel bit err_rate: %d", + // device->name, signal_strength, err_rate); + // nt26k.rssi = signal_strength; + // break; + // } + // } + // } + // rt_thread_mdelay(1000); + // } + // if (i == CSQ_RETRY) + // { + // LOG_E("%s device signal strength check failed", device->name); + // result = -RT_ERROR; + // goto __exit; + // } + /* check the GPRS network is registered */ for (i = 0; i < CEREG_RETRY; i++) { - if (at_obj_exec_cmd(device->client, resp, "AT+CEREG?") == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CEREG?") == RT_EOK) { int link_stat = 0; - if (at_resp_parse_line_args_by_kw(resp, "+CEREG:", "+CEREG: %*d,%d", &link_stat) > 0) + if (at_resp_parse_line_args_by_kw (resp, "+CEREG:", "+CEREG: %*d,%d", &link_stat) > 0) { if ((link_stat == 1) || (link_stat == 5)) { - LOG_D("%s device GPRS is registered", device->name); + LOG_D ("%s device GPRS is registered", device->name); break; } } } - rt_thread_mdelay(1000); + rt_thread_mdelay (1000); } for (i = 0; i < CEREG_RETRY; i++) { - if (at_obj_exec_cmd(device->client, resp, "AT+QICSGP=1,1,\"cmnet\",\"\",\"\",1") == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+QICSGP=1,1,\"cmnet\",\"\",\"\",1") == RT_EOK) { break; } - rt_thread_mdelay(20000); + rt_thread_mdelay (20000); } if (i == CEREG_RETRY) { @@ -1028,113 +1027,110 @@ static void nt26k_init_thread_entry(void *parameter) } for (i = 0; i < CEREG_RETRY; i++) { - if (at_obj_exec_cmd(device->client, resp, "AT+CGACT=1,1") == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CGACT=1,1") == RT_EOK) { - LOG_D("pdp激活成功"); + LOG_D ("pdp激活成功"); break; } - rt_thread_mdelay(20000); + rt_thread_mdelay (20000); } if (i == CEREG_RETRY) { result = -RT_ERROR; goto __exit; } - if (at_obj_exec_cmd(device->client, resp, "AT+CCLK?") == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CCLK?") == RT_EOK) { TsRtcDateTime rtc_dt; int year, mounth, days, hous, min, sec; /*+CCLK:24/11/12,06:08:19+32*/ - if (at_resp_parse_line_args_by_kw(resp, "+CCLK:", "+CCLK: \"%d/%d/%d,%d:%d:%d+32\"", &year, &mounth, &days, &hous, &min, &sec) > 0) + if (at_resp_parse_line_args_by_kw (resp, "+CCLK:", "+CCLK: \"%d/%d/%d,%d:%d:%d+32\"", &year, &mounth, &days, &hous, &min, &sec) > 0) { - rtc_dt.year = (2000 + year); - rtc_dt.month = mounth; - rtc_dt.day = days; - rtc_dt.hour = hous; // 此时为零区时间,需要转化为东八区 + rtc_dt.year = (2000 + year); + rtc_dt.month = mounth; + rtc_dt.day = days; + rtc_dt.hour = hous; // 此时为零区时间,需要转化为东八区 rtc_dt.minute = min; rtc_dt.second = sec; - Time_Zone_Conversion(&rtc_dt); // 时区设置 - rtc_dt.week = RTC_GetWeek(rtc_dt.year, rtc_dt.month, rtc_dt.day); - RTC_SetTime(rtc_dt.year, rtc_dt.month, rtc_dt.day, - rtc_dt.hour, rtc_dt.minute, rtc_dt.second); // 设置时间 - LOG_I("RTC时间: %04d-%02d-%02d %02d:%02d:%02d \n", - rtc_dt.year, rtc_dt.month, rtc_dt.day, rtc_dt.hour, rtc_dt.minute, rtc_dt.second); + Time_Zone_Conversion (&rtc_dt); // 时区设置 + rtc_dt.week = RTC_GetWeek (rtc_dt.year, rtc_dt.month, rtc_dt.day); + RTC_SetTime (rtc_dt.year, rtc_dt.month, rtc_dt.day, + rtc_dt.hour, rtc_dt.minute, rtc_dt.second); // 设置时间 + LOG_I ("RTC时间: %04d-%02d-%02d %02d:%02d:%02d \n", + rtc_dt.year, rtc_dt.month, rtc_dt.day, rtc_dt.hour, rtc_dt.minute, rtc_dt.second); result = RT_EOK; } } - Flash_Sys_Cfg(kIotImsiId, nt26k.imsi, NT26K_IMSI_LEN); - Flash_Sys_Cfg(kIotImeiId, nt26k.imei, NT26K_IMEI_LEN); - Flash_Sys_Cfg(kIotIccidId, nt26k.iccid, NT26K_ICCID_LEN); /* initialize successfully */ result = RT_EOK; break; - __exit: +__exit: if (result != RT_EOK) { /* power off the nt26k device */ // nt26k_power_off(device); - rt_thread_mdelay(1000); + rt_thread_mdelay (1000); - LOG_I("%s device initialize retry...", device->name); + LOG_I ("%s device initialize retry...", device->name); } } if (resp) { - at_delete_resp(resp); + at_delete_resp (resp); } if (result == RT_EOK) { /* set network interface device status and address information */ - nt26k_netdev_set_info(device->netdev); + nt26k_netdev_set_info (device->netdev); /* check and create link staus sync thread */ - nt26k_netdev_check_link_status(device->netdev); - LOG_I("%s device network initialize success.", device->name); - rt_completion_done(&nt26k_init_complate); // 通知初始化完成 + nt26k_netdev_check_link_status (device->netdev); + LOG_I ("%s device network initialize success.", device->name); + rt_completion_done (&nt26k_init_complate); // 通知初始化完成 } else { - LOG_E("%s device network initialize failed(%d).", device->name, result); + LOG_E ("%s device network initialize failed(%d).", device->name, result); } } /* nt26k device network initialize */ -static int nt26k_net_init(struct at_device *device) +static int nt26k_net_init (struct at_device *device) { #ifdef AT_DEVICE_NT26K_INIT_ASYN rt_thread_t tid; /* 初始化完成量对象 */ - rt_completion_init(&nt26k_init_complate); + rt_completion_init (&nt26k_init_complate); - tid = rt_thread_create("nt26k_net", nt26k_init_thread_entry, (void *)device, - NT26K_THREAD_STACK_SIZE, NT26K_THREAD_PRIORITY, 20); + tid = rt_thread_create ("nt26k_net", nt26k_init_thread_entry, (void *)device, + NT26K_THREAD_STACK_SIZE, NT26K_THREAD_PRIORITY, 20); if (tid) { - rt_thread_startup(tid); + rt_thread_startup (tid); } else { - LOG_E("create %s device init thread failed.", device->name); + LOG_E ("create %s device init thread failed.", device->name); return -RT_ERROR; } #else - nt26k_init_thread_entry(device); + nt26k_init_thread_entry (device); #endif /* AT_DEVICE_NT26K_INIT_ASYN */ return RT_EOK; } // 去除字符串中的 \r 和 \n -void remove_crlf(char *str) +void remove_crlf (char *str) { char *p = str; while (*p) { if (*p == '\r' || *p == '\n') { - memmove(p, p + 1, strlen(p)); + memmove (p, p + 1, strlen (p)); } else { @@ -1143,46 +1139,46 @@ void remove_crlf(char *str) } } -rt_err_t Nt26k_Get_Signal_Info(struct at_device *device) +rt_err_t Nt26k_Get_Signal_Info (struct at_device *device) { #define RETRY 20 - rt_err_t result = RT_EOK; + rt_err_t result = RT_EOK; at_response_t resp = RT_NULL; rt_uint8_t i; - RT_ASSERT(device); - resp = at_create_resp(1024, 4, rt_tick_from_millisecond(2000)); + RT_ASSERT (device); + resp = at_create_resp (1024, 4, rt_tick_from_millisecond (2000)); if (resp == RT_NULL) { - LOG_E("no memory for resp create."); - at_delete_resp(resp); + LOG_E ("no memory for resp create."); + at_delete_resp (resp); return RT_ERROR; } /* check signal strength */ for (i = 0; i < RETRY; i++) { - if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CSQ") == RT_EOK) { int signal_strength = 0, err_rate = 0; - if (at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d,%d", &signal_strength, &err_rate) > 0) + if (at_resp_parse_line_args_by_kw (resp, "+CSQ:", "+CSQ: %d,%d", &signal_strength, &err_rate) > 0) { if ((signal_strength != 99) && (signal_strength != 0)) { - LOG_D("%s device signal strength: %d, channel bit err_rate: %d", - device->name, signal_strength, err_rate); + LOG_D ("%s device signal strength: %d, channel bit err_rate: %d", + device->name, signal_strength, err_rate); nt26k.rssi = signal_strength; break; } } } - rt_thread_mdelay(1000); + rt_thread_mdelay (1000); } if (i == RETRY) { - LOG_E("%s device signal strength check failed", device->name); + LOG_E ("%s device signal strength check failed", device->name); result = -RT_ERROR; } /* check signal strength */ @@ -1191,27 +1187,27 @@ rt_err_t Nt26k_Get_Signal_Info(struct at_device *device) for (i = 0; i < RETRY; i++) { - if (at_obj_exec_cmd(device->client, resp, "AT+CESQ") == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CESQ") == RT_EOK) { rt_uint8_t rsrp = 0, rsrq = 0; - if (at_resp_parse_line_args_by_kw(resp, "+CESQ:", "+CESQ:%*[^,],%*[^,],%*[^,],%*[^,],%d,%d", &rsrq, &rsrp) > 0) + if (at_resp_parse_line_args_by_kw (resp, "+CESQ:", "+CESQ:%*[^,],%*[^,],%*[^,],%*[^,],%d,%d", &rsrq, &rsrp) > 0) { if ((rsrp <= 97) && (rsrp > 0) && ((rsrq <= 34) && (rsrq > 0))) { - LOG_D("%s device signal rsrp: %d, rsrq: %d", - device->name, rsrp, rsrq); + LOG_D ("%s device signal rsrp: %d, rsrq: %d", + device->name, rsrp, rsrq); nt26k.rsrp = rsrp; nt26k.rsrq = rsrq; break; } } } - rt_thread_mdelay(1000); + rt_thread_mdelay (1000); } if (i == RETRY) { - LOG_E("%s device signal data failed", device->name); + LOG_E ("%s device signal data failed", device->name); result = -RT_ERROR; } // /* get signal snr, pci */ @@ -1261,159 +1257,159 @@ rt_err_t Nt26k_Get_Signal_Info(struct at_device *device) // result = -RT_ERROR; // } - at_delete_resp(resp); + at_delete_resp (resp); return result; } /* nt26k device network interface attach * - set network interface device link status */ -static int nt26k_init(struct at_device *device) +static int nt26k_init (struct at_device *device) { struct at_device_nt26k *nt26k = RT_NULL; - RT_ASSERT(device); + RT_ASSERT (device); - nt26k = (struct at_device_nt26k *)device->user_data; - nt26k->power_status = RT_FALSE; // default power is off. - nt26k->sleep_status = RT_FALSE; // default sleep is disabled. + nt26k = (struct at_device_nt26k *)device->user_data; + nt26k->power_status = RT_FALSE; // default power is off. + nt26k->sleep_status = RT_FALSE; // default sleep is disabled. /* initialize AT client */ #if RT_VER_NUM >= 0x50100 - at_client_init(nt26k->client_name, nt26k->recv_line_num, nt26k->recv_line_num); + at_client_init (nt26k->client_name, nt26k->recv_line_num, nt26k->recv_line_num); #else - at_client_init(nt26k->client_name, nt26k->recv_line_num); + at_client_init (nt26k->client_name, nt26k->recv_line_num); #endif - device->client = at_client_get(nt26k->client_name); + device->client = at_client_get (nt26k->client_name); if (device->client == RT_NULL) { - LOG_E("get AT client(%s) failed.", nt26k->client_name); + LOG_E ("get AT client(%s) failed.", nt26k->client_name); return -RT_ERROR; } /* register URC data execution function */ - at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0])); + at_obj_set_urc_table (device->client, urc_table, sizeof (urc_table) / sizeof (urc_table[0])); #ifdef AT_USING_SOCKET - nt26k_socket_init(device); + nt26k_socket_init (device); #endif /* add nt26k device to the netdev list */ - device->netdev = nt26k_netdev_add(nt26k->device_name); + device->netdev = nt26k_netdev_add (nt26k->device_name); if (device->netdev == RT_NULL) { - LOG_E("add netdev(%s) failed.", nt26k->device_name); + LOG_E ("add netdev(%s) failed.", nt26k->device_name); return -RT_ERROR; } /* initialize nt26k pin configuration */ if (nt26k->pwr_en_pin != -1) { - rt_pin_mode(nt26k->pwr_en_pin, PIN_MODE_OUTPUT); + rt_pin_mode (nt26k->pwr_en_pin, PIN_MODE_OUTPUT); } if (nt26k->rst_pin != -1) { - rt_pin_mode(nt26k->rst_pin, PIN_MODE_OUTPUT); + rt_pin_mode (nt26k->rst_pin, PIN_MODE_OUTPUT); } if (nt26k->pwr_key_pin != -1) { - rt_pin_mode(nt26k->pwr_key_pin, PIN_MODE_OUTPUT); - device->class->device_ops->control(device, AT_DEVICE_CTRL_WAKEUP, RT_NULL); + rt_pin_mode (nt26k->pwr_key_pin, PIN_MODE_OUTPUT); + device->class->device_ops->control (device, AT_DEVICE_CTRL_WAKEUP, RT_NULL); } /* initialize nt26k device network */ - return nt26k_netdev_set_up(device->netdev); + return nt26k_netdev_set_up (device->netdev); } -static int nt26k_deinit(struct at_device *device) +static int nt26k_deinit (struct at_device *device) { - RT_ASSERT(device); + RT_ASSERT (device); - return nt26k_netdev_set_down(device->netdev); + return nt26k_netdev_set_down (device->netdev); } -static int nt26k_control(struct at_device *device, int cmd, void *arg) +static int nt26k_control (struct at_device *device, int cmd, void *arg) { int result = -RT_ERROR; - RT_ASSERT(device); + RT_ASSERT (device); switch (cmd) { - case AT_DEVICE_CTRL_SLEEP: - result = nt26k_sleep(device); - break; - case AT_DEVICE_CTRL_WAKEUP: - result = nt26k_wakeup(device); - break; - case AT_DEVICE_CTRL_NET_CONN: - result = at_device_nt26k_connect_tcp(device); - break; - case AT_DEVICE_CTRL_NET_DISCONN: - result = at_device_nt26k_disconnect_tcp(device); // 直接删除Socket - break; - case AT_DEVICE_CTRL_GET_SIGNAL: - result = Nt26k_Get_Signal_Info(device); - break; - case AT_DEVICE_CTRL_RESET: - result = Nt26k_Reset(device); - break; - case AT_DEVICE_CTRL_POWER_ON: - result = nt26k_power_on(device); - break; - case AT_DEVICE_CTRL_POWER_OFF: - result = nt26k_power_off(device); - break; - case AT_DEVICE_CTRL_LOW_POWER: - case AT_DEVICE_CTRL_SET_WIFI_INFO: - case AT_DEVICE_CTRL_GET_GPS: - case AT_DEVICE_CTRL_GET_VER: - LOG_W("not support the control command(%d).", cmd); - break; - default: - LOG_E("input error control command(%d).", cmd); - break; + case AT_DEVICE_CTRL_SLEEP: + result = nt26k_sleep (device); + break; + case AT_DEVICE_CTRL_WAKEUP: + result = nt26k_wakeup (device); + break; + case AT_DEVICE_CTRL_NET_CONN: + result = at_device_nt26k_connect_tcp (device); + break; + case AT_DEVICE_CTRL_NET_DISCONN: + result = at_device_nt26k_disconnect_tcp (device); // 直接删除Socket + break; + case AT_DEVICE_CTRL_GET_SIGNAL: + result = Nt26k_Get_Signal_Info (device); + break; + case AT_DEVICE_CTRL_RESET: + result = Nt26k_Reset (device); + break; + case AT_DEVICE_CTRL_POWER_ON: + result = nt26k_power_on (device); + break; + case AT_DEVICE_CTRL_POWER_OFF: + result = nt26k_power_off (device); + break; + case AT_DEVICE_CTRL_LOW_POWER: + case AT_DEVICE_CTRL_SET_WIFI_INFO: + case AT_DEVICE_CTRL_GET_GPS: + case AT_DEVICE_CTRL_GET_VER: + LOG_W ("not support the control command(%d).", cmd); + break; + default: + LOG_E ("input error control command(%d).", cmd); + break; } return result; } -int Time_Calibration(struct at_device *device) +int Time_Calibration (struct at_device *device) { at_response_t resp = RT_NULL; - RT_ASSERT(device); - resp = at_create_resp(64, 2, rt_tick_from_millisecond(2000)); + RT_ASSERT (device); + resp = at_create_resp (64, 2, rt_tick_from_millisecond (2000)); if (resp == RT_NULL) { - LOG_E("no memory for resp create."); - at_delete_resp(resp); + LOG_E ("no memory for resp create."); + at_delete_resp (resp); return RT_ERROR; } - if (at_obj_exec_cmd(device->client, resp, "AT+CCLK?") == RT_EOK) + if (at_obj_exec_cmd (device->client, resp, "AT+CCLK?") == RT_EOK) { TsRtcDateTime rtc_dt; - ; // 网络时间 + ; // 网络时间 int year, mounth, days, hous, min, sec; /*+CCLK:24/11/12,06:08:19+32*/ - if (at_resp_parse_line_args_by_kw(resp, "+CCLK:", "+CCLK: \"%d/%d/%d,%d:%d:%d+32\"", &year, &mounth, &days, &hous, &min, &sec) > 0) + if (at_resp_parse_line_args_by_kw (resp, "+CCLK:", "+CCLK: \"%d/%d/%d,%d:%d:%d+32\"", &year, &mounth, &days, &hous, &min, &sec) > 0) { - rtc_dt.year = (2000 + year); - rtc_dt.month = mounth; - rtc_dt.day = days; - rtc_dt.hour = hous; // 此时为零区时间,需要转化为东八区 + rtc_dt.year = (2000 + year); + rtc_dt.month = mounth; + rtc_dt.day = days; + rtc_dt.hour = hous; // 此时为零区时间,需要转化为东八区 rtc_dt.minute = min; rtc_dt.second = sec; - Time_Zone_Conversion(&rtc_dt); // 时区设置 - rtc_dt.week = RTC_GetWeek(rtc_dt.year, rtc_dt.month, rtc_dt.day); - RTC_SetTime(rtc_dt.year, rtc_dt.month, rtc_dt.day, - rtc_dt.hour, rtc_dt.minute, rtc_dt.second); // 设置时间 - LOG_I("RTC时间: %04d-%02d-%02d %02d:%02d:%02d \n", - rtc_dt.year, rtc_dt.month, rtc_dt.day, rtc_dt.hour, rtc_dt.minute, rtc_dt.second); - at_delete_resp(resp); + Time_Zone_Conversion (&rtc_dt); // 时区设置 + rtc_dt.week = RTC_GetWeek (rtc_dt.year, rtc_dt.month, rtc_dt.day); + RTC_SetTime (rtc_dt.year, rtc_dt.month, rtc_dt.day, + rtc_dt.hour, rtc_dt.minute, rtc_dt.second); // 设置时间 + LOG_I ("RTC时间: %04d-%02d-%02d %02d:%02d:%02d \n", + rtc_dt.year, rtc_dt.month, rtc_dt.day, rtc_dt.hour, rtc_dt.minute, rtc_dt.second); + at_delete_resp (resp); return RT_EOK; } } - at_delete_resp(resp); + at_delete_resp (resp); return RT_ERROR; } @@ -1424,23 +1420,23 @@ const struct at_device_ops nt26k_device_ops = nt26k_control, }; -int nt26k_device_class_register(void) +static int nt26k_device_class_register (void) { struct at_device_class *class = RT_NULL; - class = (struct at_device_class *)rt_calloc(1, sizeof(struct at_device_class)); + class = (struct at_device_class *)rt_calloc (1, sizeof (struct at_device_class)); if (class == RT_NULL) { - LOG_E("no memory for device class create."); - rt_free(class); + LOG_E ("no memory for device class create."); + rt_free (class); return -RT_ENOMEM; } class->device_ops = &nt26k_device_ops; - return at_device_class_register(class, AT_DEVICE_CLASS_NT26K); + return at_device_class_register (class, AT_DEVICE_CLASS_NT26K); } -// INIT_DEVICE_EXPORT(nt26k_device_class_register); -#endif //! IOT_MODULE_SWITCH +INIT_DEVICE_EXPORT(nt26k_device_class_register); +#endif //! IOT_MODULE_SWITCH -#endif /* AT_DEVICE_USING_NT26K */ +#endif /* AT_DEVICE_USING_NT26K */ diff --git a/bsp/src/bsp_flash.c b/bsp/src/bsp_flash.c index c3f3cc3..ce2e9b8 100644 --- a/bsp/src/bsp_flash.c +++ b/bsp/src/bsp_flash.c @@ -82,11 +82,11 @@ static rt_base_t interrupt_value; /*默认的系统配置*/ struct flash_sever_info sever_info = { - .server_url = SYS_IOT_URL, + .server_url = SYS_IOT_URL, .server_port = SYS_IOT_PORT, }; -static void BSP_Flash_UnLock(void) +static void BSP_Flash_UnLock (void) { #if (SystemCoreClock > SYSCLK_FREQ_96MHz_HSE) RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV2; @@ -96,17 +96,17 @@ static void BSP_Flash_UnLock(void) FLASH_Unlock(); } -static void BSP_Flash_Lock(void) +static void BSP_Flash_Lock (void) { FLASH_Lock(); #if (SystemCoreClock > SYSCLK_FREQ_96MHz_HSE) RCC->CFGR0 &= ~(uint32_t)RCC_HPRE_DIV2; #endif - rt_hw_interrupt_enable(interrupt_value); + rt_hw_interrupt_enable (interrupt_value); } -static void BSP_Flash_FastUnLock(void) +static void BSP_Flash_FastUnLock (void) { #if (SystemCoreClock > SYSCLK_FREQ_96MHz_HSE) RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV2; @@ -116,17 +116,17 @@ static void BSP_Flash_FastUnLock(void) FLASH_Unlock_Fast(); } -static void BSP_Flash_FastLock(void) +static void BSP_Flash_FastLock (void) { FLASH_Lock_Fast(); #if (SystemCoreClock > SYSCLK_FREQ_96MHz_HSE) RCC->CFGR0 &= ~(uint32_t)RCC_HPRE_DIV2; #endif - rt_hw_interrupt_enable(interrupt_value); + rt_hw_interrupt_enable (interrupt_value); } -static rt_size_t Flash_Read(rt_uint32_t addr, rt_uint8_t *buf, rt_size_t len) +static rt_size_t Flash_Read (rt_uint32_t addr, rt_uint8_t *buf, rt_size_t len) { rt_size_t read_len = 0; @@ -138,13 +138,13 @@ static rt_size_t Flash_Read(rt_uint32_t addr, rt_uint8_t *buf, rt_size_t len) return read_len; } -int Get_IotImei(char *buf, rt_size_t len) +int Get_IotImei (char *buf, rt_size_t len) { if ((*(rt_uint8_t *)FLASH_NB_IMEI_ADDR != 0xE3) && (*(rt_uint8_t *)FLASH_NB_IMEI_ADDR != 0x39)) { - rt_memcpy(buf, (char *)FLASH_NB_IMEI_ADDR, len); + rt_memcpy (buf, (char *)FLASH_NB_IMEI_ADDR, len); - LOG_D("NB IMEI: %s", buf); + LOG_D ("NB IMEI: %s", buf); return 0; } else @@ -154,18 +154,18 @@ int Get_IotImei(char *buf, rt_size_t len) { buf[i] = '0'; } - LOG_D("NB IMSI: %s", buf); + LOG_D ("NB IMSI: %s", buf); return -1; } } -int Get_IotImsi(char *buf, rt_size_t len) +int Get_IotImsi (char *buf, rt_size_t len) { if ((*(rt_uint8_t *)FLASH_NB_IMSI_ADDR != 0xE3) && (*(rt_uint8_t *)FLASH_NB_IMSI_ADDR != 0x39)) { - rt_memcpy(buf, (char *)FLASH_NB_IMSI_ADDR, len); + rt_memcpy (buf, (char *)FLASH_NB_IMSI_ADDR, len); - LOG_D("NB IMSI: %s", buf); + LOG_D ("NB IMSI: %s", buf); return 0; } else @@ -175,18 +175,18 @@ int Get_IotImsi(char *buf, rt_size_t len) { buf[i] = '0'; } - LOG_D("NB IMSI: %s", buf); + LOG_D ("NB IMSI: %s", buf); return -1; } } -int Get_IotIccid(char *buf, rt_size_t len) +int Get_IotIccid (char *buf, rt_size_t len) { if ((*(rt_uint8_t *)FLASH_NB_ICCID_ADDR != 0xE3) && (*(rt_uint8_t *)FLASH_NB_ICCID_ADDR != 0x39)) { - rt_memcpy(buf, (char *)FLASH_NB_ICCID_ADDR, len); + rt_memcpy (buf, (char *)FLASH_NB_ICCID_ADDR, len); - LOG_D("NB ICCID: %s", buf); + LOG_D ("NB ICCID: %s", buf); return 0; } else @@ -196,7 +196,7 @@ int Get_IotIccid(char *buf, rt_size_t len) { buf[i] = '0'; } - LOG_D("NB ICCID: %s", buf); + LOG_D ("NB ICCID: %s", buf); return -1; } } @@ -208,28 +208,28 @@ int Get_IotIccid(char *buf, rt_size_t len) * @param {size_t} len * @return {*} */ -static rt_size_t Flash_Write(rt_uint32_t addr, rt_uint8_t *buf, rt_size_t len) +static rt_size_t Flash_Write (rt_uint32_t addr, rt_uint8_t *buf, rt_size_t len) { rt_size_t i = 0; - __attribute__((aligned(2))) rt_uint16_t write_data; + __attribute__ ((aligned (2))) rt_uint16_t write_data; if (addr % 2 != 0) return 0; - FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_WRPRTERR); + FLASH_ClearFlag (FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_WRPRTERR); BSP_Flash_UnLock(); for (i = 0; i < len; i += 2, buf += 2, addr += 2) { - rt_memcpy(&write_data, buf, 2); - FLASH_ProgramHalfWord(addr, write_data); + rt_memcpy (&write_data, buf, 2); + FLASH_ProgramHalfWord (addr, write_data); /* You can add your code under here. */ if (*(rt_uint16_t *)addr != write_data) { BSP_Flash_Lock(); - LOG_E("Flash_Write Error"); + LOG_E ("Flash_Write Error"); return 0; } } @@ -239,70 +239,70 @@ static rt_size_t Flash_Write(rt_uint32_t addr, rt_uint8_t *buf, rt_size_t len) return i; } -int BSP_Flash_Write_Info(rt_uint8_t *buf, rt_size_t len) +int BSP_Flash_Write_Info (rt_uint8_t *buf, rt_size_t len) { rt_uint8_t page_buf[FLASH_PAGE_SIZE] = {0}; - rt_uint8_t in_page_offset = (FLASH_HW_VER_ADDR - FLASH_CONFIG_INFO_START_ADDR); + rt_uint8_t in_page_offset = (FLASH_HW_VER_ADDR - FLASH_CONFIG_INFO_START_ADDR); - Flash_ErasePage_ReadConfigInfo(page_buf); + Flash_ErasePage_ReadConfigInfo (page_buf); for (rt_uint8_t i = 0; i < len; i++) { page_buf[in_page_offset + i] = buf[i]; } - return Flash_Write_ConfigInfo(page_buf); + return Flash_Write_ConfigInfo (page_buf); } -rt_uint16_t Flash_Get_WorkDuration(void) +rt_uint16_t Flash_Get_WorkDuration (void) { return *(rt_uint16_t *)(FLASH_WORK_TIME_ADDR); } -int Flash_Set_WorkDuration(rt_uint16_t value) +int Flash_Set_WorkDuration (rt_uint16_t value) { rt_uint8_t page_buf[FLASH_PAGE_SIZE] = {0}; - rt_uint8_t in_page_offset = (FLASH_WORK_TIME_ADDR - FLASH_CONFIG_INFO_START_ADDR); + rt_uint8_t in_page_offset = (FLASH_WORK_TIME_ADDR - FLASH_CONFIG_INFO_START_ADDR); - Flash_ErasePage_ReadConfigInfo(page_buf); + Flash_ErasePage_ReadConfigInfo (page_buf); - rt_memcpy(page_buf + in_page_offset, &value, FLASH_WORK_TIME_LEN); - return Flash_Write_ConfigInfo(page_buf); + rt_memcpy (page_buf + in_page_offset, &value, FLASH_WORK_TIME_LEN); + return Flash_Write_ConfigInfo (page_buf); } -int Flash_Get_Sever_Data(struct flash_sever_info *sever_info) +int Flash_Get_Sever_Data (struct flash_sever_info *sever_info) { rt_uint8_t data[FLASH_SERVER_LEN] = {0}; - rt_memcpy(data, (rt_uint8_t *)FLASH_SERVER_ADDR_ADDR, FLASH_SERVER_LEN); - LOG_D("data[%d] = %x data[%d] = %x data[%d] = %x data[%d] = %x data[%d] = %x data[%d] = %x", 0, data[0], 1, data[1], 2, data[2], 3, data[3], 4, data[4], 5, data[5]); - rt_snprintf(sever_info->server_url, sizeof(sever_info->server_url), "%d.%d.%d.%d", - data[3], data[2], data[1], data[0]); + rt_memcpy (data, (rt_uint8_t *)FLASH_SERVER_ADDR_ADDR, FLASH_SERVER_LEN); + LOG_D ("data[%d] = %x data[%d] = %x data[%d] = %x data[%d] = %x data[%d] = %x data[%d] = %x", 0, data[0], 1, data[1], 2, data[2], 3, data[3], 4, data[4], 5, data[5]); + rt_snprintf (sever_info->server_url, sizeof (sever_info->server_url), "%d.%d.%d.%d", + data[3], data[2], data[1], data[0]); rt_uint16_t port_num = ((data[5] << 8) | data[4]); - LOG_D("port_num = %d", port_num); - rt_snprintf(sever_info->server_port, sizeof(sever_info->server_port), "%d", port_num); + LOG_D ("port_num = %d", port_num); + rt_snprintf (sever_info->server_port, sizeof (sever_info->server_port), "%d", port_num); - LOG_D("server_url:%s, server_port:%s", sever_info->server_url, sever_info->server_port); + LOG_D ("server_url:%s, server_port:%s", sever_info->server_url, sever_info->server_port); return 0; } -int Flash_Set_Sever_Data(rt_uint8_t *data) +int Flash_Set_Sever_Data (rt_uint8_t *data) { rt_uint8_t page_buf[FLASH_PAGE_SIZE] = {0}; - rt_uint8_t in_page_offset = (FLASH_SERVER_ADDR_ADDR - FLASH_CONFIG_INFO_START_ADDR); + rt_uint8_t in_page_offset = (FLASH_SERVER_ADDR_ADDR - FLASH_CONFIG_INFO_START_ADDR); if (data == RT_NULL) { return -RT_ERROR; } - Flash_ErasePage_ReadConfigInfo(page_buf); + Flash_ErasePage_ReadConfigInfo (page_buf); //*将传入的数据写到flash地址中 for (size_t i = 0; i < FLASH_SERVER_LEN; i++) { page_buf[in_page_offset++] = data[i]; } - return Flash_Write_ConfigInfo(page_buf); + return Flash_Write_ConfigInfo (page_buf); } /** @@ -311,8 +311,8 @@ int Flash_Set_Sever_Data(rt_uint8_t *data) * @param {uint8_t} page * @return {*} */ -static ErrorStatus Flash_ErasePage_Records(TeRecord record, - rt_uint8_t page_offset) +static ErrorStatus Flash_ErasePage_Records (TeRecord record, + rt_uint8_t page_offset) { ErrorStatus flag = READY; @@ -328,7 +328,7 @@ static ErrorStatus Flash_ErasePage_Records(TeRecord record, } BSP_Flash_FastUnLock(); - FLASH_ErasePage_Fast(hr_start_addr[record] + FLASH_PAGE_SIZE * erase_page); + FLASH_ErasePage_Fast (hr_start_addr[record] + FLASH_PAGE_SIZE * erase_page); BSP_Flash_FastLock(); for (rt_uint8_t i = 0; i < (FLASH_PAGE_SIZE / 2); i++) @@ -339,19 +339,19 @@ static ErrorStatus Flash_ErasePage_Records(TeRecord record, } } - LOG_D("/**Flash_Erase(%d)Page(%d)=%d**/", record, erase_page, flag); + LOG_D ("/**Flash_Erase(%d)Page(%d)=%d**/", record, erase_page, flag); return flag; } -void Flash_Erase_Records(TeRecord record) +void Flash_Erase_Records (TeRecord record) { for (rt_uint8_t erase_page = 0; erase_page < hr_record_pages[record]; erase_page++) { - Flash_ErasePage_Records(record, erase_page); + Flash_ErasePage_Records (record, erase_page); } - LOG_D("Flash_Erase_Records(%d)", record); + LOG_D ("Flash_Erase_Records(%d)", record); } /* @@ -359,7 +359,7 @@ void Flash_Erase_Records(TeRecord record) * @param {TeRecord} record * @return {*} */ -static rt_uint16_t Flash_GetMaxIndex_Records(TeRecord record) +static rt_uint16_t Flash_GetMaxIndex_Records (TeRecord record) { rt_uint16_t index_max = 0; @@ -370,13 +370,13 @@ static rt_uint16_t Flash_GetMaxIndex_Records(TeRecord record) if (*(rt_uint16_t *)(hr_start_addr[record] + FLASH_PAGE_SIZE * page + FLASH_HR_ONE_RECORD_SIZE * i) == HR_RECORD_FRAME_HEADER) { index_max = - LWUTIL_MAX(index_max, - *(rt_uint16_t *)(hr_start_addr[record] + FLASH_PAGE_SIZE * page + FLASH_HR_ONE_RECORD_SIZE * i + 2)); + LWUTIL_MAX (index_max, + *(rt_uint16_t *)(hr_start_addr[record] + FLASH_PAGE_SIZE * page + FLASH_HR_ONE_RECORD_SIZE * i + 2)); } } } - LOG_D("Flash_Get_Records(%d)MaxNum = %d", record, index_max); + LOG_D ("Flash_Get_Records(%d)MaxNum = %d", record, index_max); return index_max; } @@ -386,10 +386,10 @@ static rt_uint16_t Flash_GetMaxIndex_Records(TeRecord record) * @param {TeRecord} record * @return {*} */ -rt_uint8_t Flash_GetNum_Records(TeRecord record) +rt_uint8_t Flash_GetNum_Records (TeRecord record) { - rt_uint8_t num = 0; - rt_uint16_t num_max = Flash_GetMaxIndex_Records(record); + rt_uint8_t num = 0; + rt_uint16_t num_max = Flash_GetMaxIndex_Records (record); if (num_max <= hr_record_max_num[record]) { @@ -400,7 +400,7 @@ rt_uint8_t Flash_GetNum_Records(TeRecord record) num = hr_record_max_num[record]; } - LOG_D("Flash_GetNum_(%d)Records = %d", record, num); + LOG_D ("Flash_GetNum_(%d)Records = %d", record, num); return num; } @@ -412,13 +412,13 @@ rt_uint8_t Flash_GetNum_Records(TeRecord record) * @param {uint8_t} index * @return {*} */ -void Flash_Read_Record(TuFlashHrRecordFrame *pHrRecord, TeRecord record, - rt_uint8_t index) +void Flash_Read_Record (TuFlashHrRecordFrame *pHrRecord, TeRecord record, + rt_uint8_t index) { - LOG_D("/*********Flash_Read_(%d)Record(%d)***************/", record, index); - uint16_t index_max = Flash_GetMaxIndex_Records(record); + LOG_D ("/*********Flash_Read_(%d)Record(%d)***************/", record, index); + uint16_t index_max = Flash_GetMaxIndex_Records (record); - rt_uint8_t page_offset = 0; + rt_uint8_t page_offset = 0; rt_uint8_t in_page_offset = 0; if (index <= index_max) @@ -429,39 +429,39 @@ void Flash_Read_Record(TuFlashHrRecordFrame *pHrRecord, TeRecord record, { // index_redirect = index_max - (hr_record_max_num[record] - index); uint16_t index_redirect = index_max - (50 - index); - LOG_D("index_redirect = %d", index_redirect); - page_offset = ((index_redirect - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record]; + LOG_D ("index_redirect = %d", index_redirect); + page_offset = ((index_redirect - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record]; in_page_offset = ((index_redirect - 1) % FLASH_PAGE_HR_RECORD_NUM) * FLASH_HR_ONE_RECORD_SIZE; } else { - page_offset = 0; + page_offset = 0; in_page_offset = ((index_max - 1) % FLASH_PAGE_HR_RECORD_NUM) * FLASH_HR_ONE_RECORD_SIZE; } } else { - page_offset = ((index - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record]; + page_offset = ((index - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record]; in_page_offset = ((index - 1) % FLASH_PAGE_HR_RECORD_NUM) * FLASH_HR_ONE_RECORD_SIZE; } - LOG_D("page_offset = %d", page_offset); - LOG_D("in_page_offset = %d", in_page_offset); + LOG_D ("page_offset = %d", page_offset); + LOG_D ("in_page_offset = %d", in_page_offset); uint32_t addr = (hr_start_addr[record] + FLASH_PAGE_SIZE * page_offset + in_page_offset); - LOG_D("addr = %04X", addr); + LOG_D ("addr = %04X", addr); - Flash_Read(addr, pHrRecord->buf, HR_RECORD_FRAME_LEN); + Flash_Read (addr, pHrRecord->buf, HR_RECORD_FRAME_LEN); - LOG_D("Flash_Read_(%d)Record(%d):%04d-%02d-%02d,%02d:%02d", record, - index, pHrRecord->Struct.year, pHrRecord->Struct.month, - pHrRecord->Struct.day, pHrRecord->Struct.hour, - pHrRecord->Struct.minute); + LOG_D ("Flash_Read_(%d)Record(%d):%04d-%02d-%02d,%02d:%02d", record, + index, pHrRecord->Struct.year, pHrRecord->Struct.month, + pHrRecord->Struct.day, pHrRecord->Struct.hour, + pHrRecord->Struct.minute); } else { - rt_memset(pHrRecord->buf, 0, HR_RECORD_FRAME_LEN); - LOG_E("Flash_GetMaxNum_(%d)Records Error!: (index_max)%d < %d", record, - index_max, index); + rt_memset (pHrRecord->buf, 0, HR_RECORD_FRAME_LEN); + LOG_E ("Flash_GetMaxNum_(%d)Records Error!: (index_max)%d < %d", record, + index_max, index); } } @@ -471,37 +471,37 @@ void Flash_Read_Record(TuFlashHrRecordFrame *pHrRecord, TeRecord record, * @param {uint16_t} index * @return {*} */ -void Flash_Write_RecordIndex(TuFlashHrRecordFrame *pHrRecord, TeRecord record, - rt_uint16_t index) +void Flash_Write_RecordIndex (TuFlashHrRecordFrame *pHrRecord, TeRecord record, + rt_uint16_t index) { - LOG_D("/*********Flash_Write_RecordIndex***************/"); + LOG_D ("/*********Flash_Write_RecordIndex***************/"); rt_uint8_t page_offset = ((index - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record]; - LOG_D("page_offset = %d", page_offset); + LOG_D ("page_offset = %d", page_offset); rt_uint8_t in_page_offset = ((index - 1) % FLASH_PAGE_HR_RECORD_NUM) * FLASH_HR_ONE_RECORD_SIZE; - LOG_D("in_page_offset = %d", in_page_offset); + LOG_D ("in_page_offset = %d", in_page_offset); rt_uint32_t addr = (hr_start_addr[record] + FLASH_PAGE_SIZE * page_offset + in_page_offset); - LOG_D("addr = %04X", addr); - Flash_Write(addr, pHrRecord->buf, HR_RECORD_FRAME_LEN); + LOG_D ("addr = %04X", addr); + Flash_Write (addr, pHrRecord->buf, HR_RECORD_FRAME_LEN); TuFlashHrRecordFrame pReadRecord; - Flash_Read(addr, pReadRecord.buf, HR_RECORD_FRAME_LEN); + Flash_Read (addr, pReadRecord.buf, HR_RECORD_FRAME_LEN); - LOG_D("(%04X)Flash_Write_(%d)RecordIndex(%d):%04d-%02d-%02d,%02d:%02d", - pReadRecord.Struct.header, record, pReadRecord.Struct.index, - pReadRecord.Struct.year, pReadRecord.Struct.month, - pReadRecord.Struct.day, pReadRecord.Struct.hour, - pReadRecord.Struct.minute); + LOG_D ("(%04X)Flash_Write_(%d)RecordIndex(%d):%04d-%02d-%02d,%02d:%02d", + pReadRecord.Struct.header, record, pReadRecord.Struct.index, + pReadRecord.Struct.year, pReadRecord.Struct.month, + pReadRecord.Struct.day, pReadRecord.Struct.hour, + pReadRecord.Struct.minute); } -void Flash_Write_Record(TeRecord record) +void Flash_Write_Record (TeRecord record) { - LOG_D("/*********Flash_Write_Record***************/"); + LOG_D ("/*********Flash_Write_Record***************/"); - rt_uint16_t index_max = Flash_GetMaxIndex_Records(record); + rt_uint16_t index_max = Flash_GetMaxIndex_Records (record); rt_uint16_t index_new = index_max + 1; - LOG_D("index_new = %d", index_new); + LOG_D ("index_new = %d", index_new); rt_uint16_t index_start; rt_uint8_t index_start_page_offset; @@ -511,69 +511,69 @@ void Flash_Write_Record(TeRecord record) if (index_max <= hr_record_max_num[record]) { - index_start = 0; + index_start = 0; index_start_page_offset = 0; - index_max_page_offset = ((index_max - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record]; - LOG_D("index_max_page_offset = %d", index_max_page_offset); + index_max_page_offset = ((index_max - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record]; + LOG_D ("index_max_page_offset = %d", index_max_page_offset); } else { index_start = index_max - (hr_record_max_num[record] - 1); - LOG_D("index_start = %d", index_start); + LOG_D ("index_start = %d", index_start); index_start_page_offset = ((index_start - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record]; - LOG_D("index_start_page_offset = %d", index_start_page_offset); + LOG_D ("index_start_page_offset = %d", index_start_page_offset); index_max_page_offset = ((index_max - 1) / FLASH_PAGE_HR_RECORD_NUM) % hr_record_pages[record]; } index_max_in_page_offset = (index_max - 1) % FLASH_PAGE_HR_RECORD_NUM; - LOG_D("index_max_in_page_offset = %d", index_max_in_page_offset); + LOG_D ("index_max_in_page_offset = %d", index_max_in_page_offset); if ((index_max_in_page_offset + 1) == (FLASH_PAGE_HR_RECORD_NUM - 1) && index_max_page_offset == (hr_record_pages[record] - 1)) { - Flash_ErasePage_Records(record, 0); + Flash_ErasePage_Records (record, 0); } else if ((index_max_in_page_offset + 1) == (FLASH_PAGE_HR_RECORD_NUM - 1) && index_max_page_offset == index_start_page_offset - 1) { - Flash_ErasePage_Records(record, index_max_page_offset + 1); + Flash_ErasePage_Records (record, index_max_page_offset + 1); } RTC_GetTime(); TuFlashHrRecordFrame HrRecord; HrRecord.Struct.header = HR_RECORD_FRAME_HEADER; - HrRecord.Struct.index = index_new; - HrRecord.Struct.year = RtcDateTime.year; - HrRecord.Struct.month = RtcDateTime.month; - HrRecord.Struct.day = RtcDateTime.day; - HrRecord.Struct.hour = RtcDateTime.hour; + HrRecord.Struct.index = index_new; + HrRecord.Struct.year = RtcDateTime.year; + HrRecord.Struct.month = RtcDateTime.month; + HrRecord.Struct.day = RtcDateTime.day; + HrRecord.Struct.hour = RtcDateTime.hour; HrRecord.Struct.minute = RtcDateTime.minute; - Flash_Write_RecordIndex(&HrRecord, record, index_new); + Flash_Write_RecordIndex (&HrRecord, record, index_new); } -void Flash_ErasePage_ConfigInfo(void) +void Flash_ErasePage_ConfigInfo (void) { BSP_Flash_FastUnLock(); - FLASH_ErasePage_Fast(FLASH_CONFIG_INFO_START_ADDR); + FLASH_ErasePage_Fast (FLASH_CONFIG_INFO_START_ADDR); BSP_Flash_FastLock(); } -void Flash_ErasePage_ReadConfigInfo(rt_uint8_t *page_buf) +void Flash_ErasePage_ReadConfigInfo (rt_uint8_t *page_buf) { - Flash_Read(FLASH_CONFIG_INFO_START_ADDR, page_buf, FLASH_PAGE_SIZE); - rt_thread_mdelay(10); + Flash_Read (FLASH_CONFIG_INFO_START_ADDR, page_buf, FLASH_PAGE_SIZE); + rt_thread_mdelay (10); Flash_ErasePage_ConfigInfo(); - rt_thread_mdelay(10); + rt_thread_mdelay (10); } -int Flash_Write_ConfigInfo(rt_uint8_t *page_buf) +int Flash_Write_ConfigInfo (rt_uint8_t *page_buf) { - return Flash_Write(FLASH_CONFIG_INFO_START_ADDR, page_buf, FLASH_PAGE_SIZE); + return Flash_Write (FLASH_CONFIG_INFO_START_ADDR, page_buf, FLASH_PAGE_SIZE); } -ErrorStatus Flash_GetProductTimeLimit(TuFlashProductTimeLimitFrame *pLimitTime, - TeFlashProductTimeLimitId id) +ErrorStatus Flash_GetProductTimeLimit (TuFlashProductTimeLimitFrame *pLimitTime, + TeFlashProductTimeLimitId id) { ErrorStatus flag = NoREADY; rt_uint32_t addr = 0; @@ -588,39 +588,39 @@ ErrorStatus Flash_GetProductTimeLimit(TuFlashProductTimeLimitFrame *pLimitTime, } if (addr != 0) { - Flash_Read(addr, pLimitTime->buf, sizeof(TuFlashProductTimeLimitFrame)); + Flash_Read (addr, pLimitTime->buf, sizeof (TuFlashProductTimeLimitFrame)); if (pLimitTime->Struct.header == FLASH_PRODUCT_TIME_FRAME_HEADER) { flag = READY; - LOG_D("Flash_GetProductTimeLimit(%d):%04d-%02d-%02d,%02d:%02d:%02d", - id, pLimitTime->Struct.year, pLimitTime->Struct.month, - pLimitTime->Struct.day, pLimitTime->Struct.hour, - pLimitTime->Struct.minute, pLimitTime->Struct.second); + LOG_D ("Flash_GetProductTimeLimit(%d):%04d-%02d-%02d,%02d:%02d:%02d", + id, pLimitTime->Struct.year, pLimitTime->Struct.month, + pLimitTime->Struct.day, pLimitTime->Struct.hour, + pLimitTime->Struct.minute, pLimitTime->Struct.second); } else { - LOG_E("Flash_GetProductTimeLimit Error!"); + LOG_E ("Flash_GetProductTimeLimit Error!"); } } return flag; } -void Flash_SetProductTimeLimit(rt_uint16_t year, rt_uint8_t mon, rt_uint8_t day, - rt_uint8_t hour, rt_uint8_t min, rt_uint8_t second, - TeFlashProductTimeLimitId id) +void Flash_SetProductTimeLimit (rt_uint16_t year, rt_uint8_t mon, rt_uint8_t day, + rt_uint8_t hour, rt_uint8_t min, rt_uint8_t second, + TeFlashProductTimeLimitId id) { TuFlashProductTimeLimitFrame LimitTime; rt_uint8_t page_buf[FLASH_PAGE_SIZE]; - Flash_ErasePage_ReadConfigInfo(page_buf); + Flash_ErasePage_ReadConfigInfo (page_buf); LimitTime.Struct.header = FLASH_PRODUCT_TIME_FRAME_HEADER; - LimitTime.Struct.year = year; - LimitTime.Struct.month = mon; - LimitTime.Struct.day = day; - LimitTime.Struct.hour = hour; + LimitTime.Struct.year = year; + LimitTime.Struct.month = mon; + LimitTime.Struct.day = day; + LimitTime.Struct.hour = hour; LimitTime.Struct.minute = min; LimitTime.Struct.second = second; @@ -634,58 +634,47 @@ void Flash_SetProductTimeLimit(rt_uint16_t year, rt_uint8_t mon, rt_uint8_t day, in_page_offset = (FLASH_EXPIRATION_TIME_START_ADDR - FLASH_CONFIG_INFO_START_ADDR); } - for (rt_uint8_t i = 0; i < sizeof(TuFlashProductTimeLimitFrame); i++) + for (rt_uint8_t i = 0; i < sizeof (TuFlashProductTimeLimitFrame); i++) { page_buf[in_page_offset + i] = LimitTime.buf[i]; } - Flash_Write_ConfigInfo(page_buf); - - TuFlashProductTimeLimitFrame ReadLimitTime; - Flash_GetProductTimeLimit(&ReadLimitTime, id); + Flash_Write_ConfigInfo (page_buf); } -void Set_ExpirationTime(rt_uint16_t days) +void Set_ExpirationTime (rt_uint16_t days) { uint32_t factory_seconds, expiration_seconds; TuFlashProductTimeLimitFrame LimitTime; - if (Flash_GetProductTimeLimit(&LimitTime, kFactoryTimeId) == READY) + if (Flash_GetProductTimeLimit (&LimitTime, kFactoryTimeId) == READY) { - factory_seconds = DateTime2Seconds(LimitTime.Struct.year, - LimitTime.Struct.month, LimitTime.Struct.day, - LimitTime.Struct.hour, LimitTime.Struct.minute, - LimitTime.Struct.second); + factory_seconds = DateTime2Seconds (LimitTime.Struct.year, + LimitTime.Struct.month, LimitTime.Struct.day, + LimitTime.Struct.hour, LimitTime.Struct.minute, + LimitTime.Struct.second); expiration_seconds = factory_seconds + days * 24 * 3600; TsRtcDateTime DateTime; - Seconds2DateTime(expiration_seconds, &DateTime); - Flash_SetProductTimeLimit(DateTime.year, DateTime.month, DateTime.day, - DateTime.hour, DateTime.minute, DateTime.second, - kExpirationTimeId); - - if (Flash_GetProductTimeLimit(&LimitTime, kExpirationTimeId) == READY) - { - LOG_D("到期时间:%04d-%02d-%02d,%02d:%02d:%02d", - LimitTime.Struct.year, LimitTime.Struct.month, - LimitTime.Struct.day, LimitTime.Struct.hour, - LimitTime.Struct.minute, LimitTime.Struct.second); - } + Seconds2DateTime (expiration_seconds, &DateTime); + Flash_SetProductTimeLimit (DateTime.year, DateTime.month, DateTime.day, + DateTime.hour, DateTime.minute, DateTime.second, + kExpirationTimeId); } } -rt_uint8_t Flash_Sys_Cfg(TeFlashCfgInfoId id, rt_uint8_t *buf, rt_size_t len) +rt_uint8_t Flash_Sys_Cfg (TeFlashCfgInfoId id, rt_uint8_t *buf, rt_size_t len) { rt_uint8_t page_buf[FLASH_PAGE_SIZE] = {0}; - Flash_ErasePage_ReadConfigInfo(page_buf); + Flash_ErasePage_ReadConfigInfo (page_buf); rt_uint8_t in_page_offset = (hr_sys_cfg_info_addr[id] - FLASH_CONFIG_INFO_START_ADDR); if (len > hr_sys_cfg_info_len[id]) { - LOG_E("Flash_Sys_Cfg: id value too long ->%d", id); - LOG_D("buf len %d > id len[%d]", len, hr_sys_cfg_info_len[id]); + LOG_E ("Flash_Sys_Cfg: id value too long ->%d", id); + LOG_D ("buf len %d > id len[%d]", len, hr_sys_cfg_info_len[id]); return -RT_ERROR; } @@ -694,11 +683,11 @@ rt_uint8_t Flash_Sys_Cfg(TeFlashCfgInfoId id, rt_uint8_t *buf, rt_size_t len) page_buf[in_page_offset + i] = buf[i]; } - return Flash_Write_ConfigInfo(page_buf); + return Flash_Write_ConfigInfo (page_buf); } /*这个返回一两个字节的数,方便提取*/ -size_t Flash_Get_SysCfg(TeFlashCfgInfoId id) +size_t Flash_Get_SysCfg (TeFlashCfgInfoId id) { rt_uint8_t iot_upload_cycle[2] = {0}; rt_uint16_t iot_upload; @@ -708,51 +697,51 @@ size_t Flash_Get_SysCfg(TeFlashCfgInfoId id) } else { - rt_memcpy(&iot_upload_cycle[0], (rt_uint8_t *)hr_sys_cfg_info_addr[id], 2); + rt_memcpy (&iot_upload_cycle[0], (rt_uint8_t *)hr_sys_cfg_info_addr[id], 2); iot_upload = ((iot_upload_cycle[0] << 8) + iot_upload_cycle[1]); - LOG_D("iot_upload: %d", iot_upload); + LOG_D ("iot_upload: %d", iot_upload); return iot_upload; } } -ErrorStatus Flash_GetTotalRecord(TsTotalRecords *pTotalRecords) +ErrorStatus Flash_GetTotalRecord (TsTotalRecords *pTotalRecords) { ErrorStatus flag = NoREADY; - pTotalRecords->alarm = Flash_GetNum_Records(kRecordAlarm); - pTotalRecords->alarm_rcy = Flash_GetNum_Records(kRecordAlarmRcy); - pTotalRecords->fault = Flash_GetNum_Records(kRecordFault); - pTotalRecords->fault_rcy = Flash_GetNum_Records(kRecordFaultRcy); - pTotalRecords->power_failure = Flash_GetNum_Records(kRecordPowerDown); - pTotalRecords->power_on = Flash_GetNum_Records(kRecordPowerOn); - pTotalRecords->sensor_failure = Flash_GetNum_Records(kRecordSensoEndOfLife); + pTotalRecords->alarm = Flash_GetNum_Records (kRecordAlarm); + pTotalRecords->alarm_rcy = Flash_GetNum_Records (kRecordAlarmRcy); + pTotalRecords->fault = Flash_GetNum_Records (kRecordFault); + pTotalRecords->fault_rcy = Flash_GetNum_Records (kRecordFaultRcy); + pTotalRecords->power_failure = Flash_GetNum_Records (kRecordPowerDown); + pTotalRecords->power_on = Flash_GetNum_Records (kRecordPowerOn); + pTotalRecords->sensor_failure = Flash_GetNum_Records (kRecordSensoEndOfLife); flag = READY; return flag; } -ErrorStatus Flash_GetRecord(TeFrameC2 record_type, rt_uint8_t index, - TsRecordsTime *pRecordsTime) +ErrorStatus Flash_GetRecord (TeFrameC2 record_type, rt_uint8_t index, + TsRecordsTime *pRecordsTime) { ErrorStatus flag = NoREADY; if (kNumOfRecords < record_type && record_type < kGetCurrentTime) { TuFlashHrRecordFrame pHrReadRecord; - Flash_Read_Record(&pHrReadRecord, record_type - 1, index); + Flash_Read_Record (&pHrReadRecord, record_type - 1, index); - LOG_D("(%d)Flash_GetRecord(%d):%04d-%02d-%02d,%02d:%02d", - record_type - 1, pHrReadRecord.Struct.index, - pHrReadRecord.Struct.year, pHrReadRecord.Struct.month, - pHrReadRecord.Struct.day, pHrReadRecord.Struct.hour, - pHrReadRecord.Struct.minute); + LOG_D ("(%d)Flash_GetRecord(%d):%04d-%02d-%02d,%02d:%02d", + record_type - 1, pHrReadRecord.Struct.index, + pHrReadRecord.Struct.year, pHrReadRecord.Struct.month, + pHrReadRecord.Struct.day, pHrReadRecord.Struct.hour, + pHrReadRecord.Struct.minute); pRecordsTime->year_h = pHrReadRecord.Struct.year / 256; pRecordsTime->year_l = pHrReadRecord.Struct.year % 256; - pRecordsTime->month = pHrReadRecord.Struct.month; - pRecordsTime->day = pHrReadRecord.Struct.day; - pRecordsTime->hour = pHrReadRecord.Struct.hour; + pRecordsTime->month = pHrReadRecord.Struct.month; + pRecordsTime->day = pHrReadRecord.Struct.day; + pRecordsTime->hour = pHrReadRecord.Struct.hour; pRecordsTime->minute = pHrReadRecord.Struct.minute; flag = READY; @@ -765,24 +754,23 @@ ErrorStatus Flash_GetRecord(TeFrameC2 record_type, rt_uint8_t index, * @description: * @return {*} */ -void BSP_Flash_EraseRecodrs(void) +void BSP_Flash_EraseRecodrs (void) { for (rt_uint8_t record = 0; record <= kRecordSensoEndOfLife; record++) { for (rt_uint8_t i = 0; i < hr_record_pages[record]; i++) { - Flash_ErasePage_Records(record, i); + Flash_ErasePage_Records (record, i); } - // LOG_D("/**BSP_Flash_First_Init (%d)**/", record); } Flash_ErasePage_ConfigInfo(); } -int BSP_Flash_Init(void) +int BSP_Flash_Init (void) { if (*(rt_uint16_t *)FLASH_INIT_FLAG_ADDR != FLASH_FIRST_INIT_VALUE) { - LOG_D("BSP_Flash_EraseRecodrs!"); + LOG_D ("BSP_Flash_EraseRecodrs!"); rt_uint8_t flash_init_flag[2]; sys_config_info sci; rt_uint8_t sever_data[6] = {0}; @@ -792,196 +780,200 @@ int BSP_Flash_Init(void) BSP_Flash_EraseRecodrs(); - Flash_Write(FLASH_INIT_FLAG_ADDR, flash_init_flag, - sizeof(flash_init_flag)); + 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()); - Flash_SetProductTimeLimit(2025, 1, 10, 13, 50, 20, kFactoryTimeId); - Set_ExpirationTime(MAX_EXPIRATION_DAYS); + Convert_To_Hex (&sever_info, sever_data); - sci.hw_ver = SYS_HW_VERSION; - sci.sw_ver = SYS_SW_VERSION; - sci.alarm_l_value = SYS_ALARM_VALVE; - sci.alarm_h_value = SYS_ALARM_VALVE_MAX; - sci.iot_upload_cycle = SYS_IOT_UPLOAD_CYCLE_MIN; - sci.iot_retry = SYS_IOT_RETRY; + if (Flash_Set_Sever_Data (sever_data) <= 0) + { + LOG_D ("Flash_Set_Sever_Data error!"); + } + + Flash_SetProductTimeLimit (2025, 1, 10, 13, 50, 20, kFactoryTimeId); + Set_ExpirationTime (MAX_EXPIRATION_DAYS); + + sci.hw_ver = SYS_HW_VERSION; + sci.sw_ver = SYS_SW_VERSION; + sci.alarm_l_value = SYS_ALARM_VALVE; + sci.alarm_h_value = SYS_ALARM_VALVE_MAX; + sci.iot_upload_cycle = SYS_IOT_UPLOAD_CYCLE_MIN; + sci.iot_retry = SYS_IOT_RETRY; sci.temp_alarm_threshold = SYS_TEMP_ALARM_THRESHOLD; - sci.emagnetic_switch = SYS_EMV_SWITCH; - sci.relay_switch = SYS_RELAY_SWITCH; + sci.emagnetic_switch = SYS_EMV_SWITCH; + sci.relay_switch = SYS_RELAY_SWITCH; - Flash_Set_WorkDuration(0); - LOG_D("work_duration:%d", Flash_Get_WorkDuration()); - - Convert_To_Hex(&sever_info, sever_data); - for (size_t i = 0; i < sizeof(sever_data); i++) - { - LOG_D("%02x", sever_data[i]); - } - - if (Flash_Set_Sever_Data(sever_data) <= 0) - { - LOG_D("Flash_Set_Sever_Data error!"); - } - - Flash_Write(FLASH_HW_VER_ADDR, (rt_uint8_t *)&sci, - (sizeof(sys_config_info) - 50)); + Flash_Write (FLASH_HW_VER_ADDR, (rt_uint8_t *)&sci, + (sizeof (sys_config_info) - 50)); } return 0; } #ifdef RT_USING_COMPONENTS_INIT -// INIT_BOARD_EXPORT(BSP_Flash_Init); +INIT_PREV_EXPORT (BSP_Flash_Init); #endif #ifdef TEST_ENABLE -static void TEST_Flash_GetMaxIndex_Records(int argc, char **argv) +static void TEST_Flash_GetMaxIndex_Records (int argc, char **argv) { if (argc == 2) { - rt_uint8_t record = atoi(argv[1]); - LOG_D("Flash_GetMaxIndex_Records(%d) = %d", record, - Flash_GetMaxIndex_Records(record)); + rt_uint8_t record = atoi (argv[1]); + LOG_D ("Flash_GetMaxIndex_Records(%d) = %d", record, + Flash_GetMaxIndex_Records (record)); } else { - LOG_E("TEST_Flash_GetMaxIndex_Records --use _cmd_ [record](0~6)"); + LOG_E ("TEST_Flash_GetMaxIndex_Records --use _cmd_ [record](0~6)"); } } -MSH_CMD_EXPORT(TEST_Flash_GetMaxIndex_Records, - "TEST_Flash_GetMaxIndex_Records"); -static void TEST_Flash_Erase_Records(int argc, char **argv) +MSH_CMD_EXPORT (TEST_Flash_GetMaxIndex_Records, + "TEST_Flash_GetMaxIndex_Records"); + +static void TEST_Flash_Erase_Records (int argc, char **argv) { if (argc == 2) { - int record = atoi(argv[1]); - Flash_Erase_Records(record); + int record = atoi (argv[1]); + Flash_Erase_Records (record); } else { - LOG_E("TEST_Flash_Erase_Records --use _cmd_ [record](0~6)"); + LOG_E ("TEST_Flash_Erase_Records --use _cmd_ [record](0~6)"); } } -MSH_CMD_EXPORT(TEST_Flash_Erase_Records, "TEST_Flash_Erase_Records"); -static void TEST_Flash_Write_Record(int argc, char **argv) +MSH_CMD_EXPORT (TEST_Flash_Erase_Records, "TEST_Flash_Erase_Records"); + +static void TEST_Flash_Write_Record (int argc, char **argv) { if (argc == 3) { - int record = atoi(argv[1]); - int num = atoi(argv[2]); + int record = atoi (argv[1]); + int num = atoi (argv[2]); for (rt_uint8_t i = 0; i < num; i++) { - Flash_Write_Record(record); - rt_thread_mdelay(2); + Flash_Write_Record (record); + rt_thread_mdelay (2); // BSP_WDG_Loop(); } } else { - LOG_E("TEST_Flash_Write_Record --use _cmd_ [record(0~6)] [num]"); + LOG_E ("TEST_Flash_Write_Record --use _cmd_ [record(0~6)] [num]"); } } -MSH_CMD_EXPORT(TEST_Flash_Write_Record, "TEST_Flash_Write_Record"); -static void TEST_Flash_GetNum_Records(int argc, char **argv) +MSH_CMD_EXPORT (TEST_Flash_Write_Record, "TEST_Flash_Write_Record"); + +static void TEST_Flash_GetNum_Records (int argc, char **argv) { if (argc == 2) { - int record = atoi(argv[1]); + int record = atoi (argv[1]); if (0 <= record && record <= 6) { - LOG_D("TEST_Flash_GetNum_Records(%d) = %d", record, - Flash_GetNum_Records(record)); + LOG_D ("TEST_Flash_GetNum_Records(%d) = %d", record, + Flash_GetNum_Records (record)); } else { - LOG_E("TEST_Flash_GetNum_Records --use _cmd_ [record](0~6)"); + LOG_E ("TEST_Flash_GetNum_Records --use _cmd_ [record](0~6)"); } } else { - LOG_E("TEST_Flash_GetNum_Records --use _cmd_ [record](0~6)"); + LOG_E ("TEST_Flash_GetNum_Records --use _cmd_ [record](0~6)"); } } -MSH_CMD_EXPORT(TEST_Flash_GetNum_Records, "TEST_Flash_GetNum_Records"); -static void TEST_Flash_GetProductTimeLimit(int argc, char **argv) +MSH_CMD_EXPORT (TEST_Flash_GetNum_Records, "TEST_Flash_GetNum_Records"); + +static void TEST_Flash_GetProductTimeLimit (int argc, char **argv) { if (argc == 2) { - int id = atoi(argv[1]); + int id = atoi (argv[1]); TuFlashProductTimeLimitFrame LimitTime; - Flash_GetProductTimeLimit(&LimitTime, id); + Flash_GetProductTimeLimit (&LimitTime, id); } else { - LOG_E( + LOG_E ( "TEST_Flash_GetProductTimeLimit --use _cmd_ [id(0:FACTORY; 1:EXPIRATION)]"); } } -MSH_CMD_EXPORT(TEST_Flash_GetProductTimeLimit, - "TEST_Flash_GetProductTimeLimit"); -static void TEST_Flash_SetProductTimeLimit(int argc, char **argv) +MSH_CMD_EXPORT (TEST_Flash_GetProductTimeLimit, + "TEST_Flash_GetProductTimeLimit"); + +static void TEST_Flash_SetProductTimeLimit (int argc, char **argv) { if (argc == 8) { - int year = atoi(argv[1]); - int mon = atoi(argv[2]); - int day = atoi(argv[3]); - int hour = atoi(argv[4]); - int min = atoi(argv[5]); - int second = atoi(argv[6]); - int id = atoi(argv[7]); - Flash_SetProductTimeLimit(year, mon, day, hour, min, second, id); + int year = atoi (argv[1]); + int mon = atoi (argv[2]); + int day = atoi (argv[3]); + int hour = atoi (argv[4]); + int min = atoi (argv[5]); + int second = atoi (argv[6]); + int id = atoi (argv[7]); + Flash_SetProductTimeLimit (year, mon, day, hour, min, second, id); } else { - LOG_E( + LOG_E ( "TEST_Flash_SetProductTimeLimit --use _cmd_ [y] [m] [d] [h] [m] [s] [id(0:FACTORY; 1:EXPIRATION)]"); } } -MSH_CMD_EXPORT(TEST_Flash_SetProductTimeLimit, - "TEST_Flash_SetProductTimeLimit"); -void Set_FactoryRtcTime(void) +MSH_CMD_EXPORT (TEST_Flash_SetProductTimeLimit, + "TEST_Flash_SetProductTimeLimit"); + +void Set_FactoryRtcTime (void) { RTC_GetTime(); - LOG_D("%4d-%02d-%02d, %02d:%02d:%02d", RtcDateTime.year, RtcDateTime.month, - RtcDateTime.day, RtcDateTime.hour, RtcDateTime.minute, - RtcDateTime.second); + LOG_D ("%4d-%02d-%02d, %02d:%02d:%02d", RtcDateTime.year, RtcDateTime.month, + RtcDateTime.day, RtcDateTime.hour, RtcDateTime.minute, + RtcDateTime.second); - Flash_SetProductTimeLimit(RtcDateTime.year, RtcDateTime.month, - RtcDateTime.day, RtcDateTime.hour, RtcDateTime.minute, - RtcDateTime.second, kFactoryTimeId); + Flash_SetProductTimeLimit (RtcDateTime.year, RtcDateTime.month, + RtcDateTime.day, RtcDateTime.hour, RtcDateTime.minute, + RtcDateTime.second, kFactoryTimeId); } -MSH_CMD_EXPORT(Set_FactoryRtcTime, "Use RTC time Set_FactoryRtcTime"); -static void TEST_Flash_Set_ExpirationTime(int argc, char **argv) +MSH_CMD_EXPORT (Set_FactoryRtcTime, "Use RTC time Set_FactoryRtcTime"); + +static void TEST_Flash_Set_ExpirationTime (int argc, char **argv) { if (argc == 2) { - int days = atoi(argv[1]); - Set_ExpirationTime(days); + int days = atoi (argv[1]); + Set_ExpirationTime (days); } else { - LOG_E("TEST_Flash_Set_ExpirationTime --use _cmd_ [days]"); + LOG_E ("TEST_Flash_Set_ExpirationTime --use _cmd_ [days]"); } } -MSH_CMD_EXPORT(TEST_Flash_Set_ExpirationTime, "TEST_Flash_Set_ExpirationTime"); -static void TEST_Flash_Get_Sys_Info(int argc, char **argv) +MSH_CMD_EXPORT (TEST_Flash_Set_ExpirationTime, "TEST_Flash_Set_ExpirationTime"); + +static void TEST_Flash_Get_Sys_Info (int argc, char **argv) { if (argc == 2) { - int id = atoi(argv[1]); - Flash_Get_SysCfg(id); + int id = atoi (argv[1]); + Flash_Get_SysCfg (id); } else { - LOG_E("TEST_Flash_Get_Sys_Info "); + LOG_E ("TEST_Flash_Get_Sys_Info "); } } -MSH_CMD_EXPORT(TEST_Flash_Get_Sys_Info, "TEST_Flash_Get_Sys_Info"); + +MSH_CMD_EXPORT (TEST_Flash_Get_Sys_Info, "TEST_Flash_Get_Sys_Info"); #endif diff --git a/bsp/src/bsp_h308.c b/bsp/src/bsp_h308.c index 8c2d0c4..5fbc217 100644 --- a/bsp/src/bsp_h308.c +++ b/bsp/src/bsp_h308.c @@ -229,51 +229,61 @@ uint8_t IS_H308_EndOfLife(void) return H308.end_of_life; } + + +void UART4_Init(void) +{ + GPIO_InitTypeDef GPIO_InitStructure = {0}; + USART_InitTypeDef USART_InitStructure = {0}; + NVIC_InitTypeDef NVIC_InitStructure = {0}; + + lwrb_init(&uart4_rx_rb, uart4_rx_rb_data, sizeof(uart4_rx_rb_data)); + + USART_DeInit(UART4); // 寄存器恢复默认值 + + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_Init(GPIOC, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_Init(GPIOC, &GPIO_InitStructure); + + USART_InitStructure.USART_BaudRate = 9600; + USART_InitStructure.USART_WordLength = USART_WordLength_8b; + USART_InitStructure.USART_StopBits = USART_StopBits_1; + USART_InitStructure.USART_Parity = USART_Parity_No; + + USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; + USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; + USART_Init(UART4, &USART_InitStructure); + + USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); + + NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); + + USART_Cmd(UART4, ENABLE); + LOG_I("UART4 Init"); +} + static void h308_thread_entry(void *param) { - // strstr要用,静态的才能算正确偏移 + rt_err_t ret; static uint8_t str[UART4_RX_RB_LENGTH] = {0}; static uint8_t h308_rx_timout_cnt = 0; static uint8_t h308_err_cnt = 0; - - rt_err_t ret; - - H308.alarm_value = Flash_Get_SysCfg(kAlarmLValueId); // 获取系统报警阈值 - LOG_D("报警阈值:%d", H308.alarm_value); - LOG_D("h308_thread_entry"); - - H308_PWR_OFF; - rt_thread_mdelay(1000); - H308_PWR_ON; - TuFlashProductTimeLimitFrame LimitTime; - if (Flash_GetProductTimeLimit(&LimitTime, kExpirationTimeId) == READY) - { - // 计算出 到期时间对应的RTC秒数 - H308.expiration_seconds = DateTime2Seconds(LimitTime.Struct.year, - LimitTime.Struct.month, LimitTime.Struct.day, LimitTime.Struct.hour, - LimitTime.Struct.minute, LimitTime.Struct.second); - LOG_D("h308_expiration_time:%04d-%02d-%02d,%02d:%02d", - LimitTime.Struct.year, LimitTime.Struct.month, LimitTime.Struct.day, - LimitTime.Struct.hour, LimitTime.Struct.minute, LimitTime.Struct.second); - } + H308.alarm_value = Flash_Get_SysCfg(kAlarmLValueId); // 获取系统报警阈值 + LOG_D("报警阈值:%d%LEL", H308.alarm_value); - if (Flash_GetNum_Records(kRecordSensoEndOfLife) == 1) - { - LOG_W("寿命已到期"); - H308.end_of_life = 1; - } - //这里应该等下iot模组更新网络,不然可能不准确,但如果从未更新时,截至日期小于1天才会有影响,所以可不等待 - rt_thread_mdelay(5000);//这里等的原因是,等待4G更新网络,其二是传感器需要10s的启动时间 - // 寿命到期检测 - if (IS_H308_EndOfLife()) - { - if (Flash_GetNum_Records(kRecordSensoEndOfLife) == 0) - { - Flash_Write_Record(kRecordSensoEndOfLife); - } - Send_Laser_Alarm_Event(kSensorFailureEvent); - } while (1) { ret = rt_sem_take(uart4_rx_ok_sem, 3000); @@ -285,7 +295,7 @@ static void h308_thread_entry(void *param) if (len >= 44) { - ret = H308_GetFrameData(str, len, &H308.Data); + ret = H308_GetFrameData((const char *)str, len, &H308.Data); LOG_I("str:[%s]", str); if (ret == 0) { @@ -335,57 +345,12 @@ static void h308_thread_entry(void *param) } } -void UART4_Init(void) -{ - GPIO_InitTypeDef GPIO_InitStructure = {0}; - USART_InitTypeDef USART_InitStructure = {0}; - NVIC_InitTypeDef NVIC_InitStructure = {0}; - - lwrb_init(&uart4_rx_rb, uart4_rx_rb_data, sizeof(uart4_rx_rb_data)); - - USART_DeInit(UART4); // 寄存器恢复默认值 - - RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); - RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); - - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_Init(GPIOC, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - GPIO_Init(GPIOC, &GPIO_InitStructure); - - USART_InitStructure.USART_BaudRate = 9600; - USART_InitStructure.USART_WordLength = USART_WordLength_8b; - USART_InitStructure.USART_StopBits = USART_StopBits_1; - USART_InitStructure.USART_Parity = USART_Parity_No; - - USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; - USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; - USART_Init(UART4, &USART_InitStructure); - - USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); - - NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; - NVIC_Init(&NVIC_InitStructure); - - USART_Cmd(UART4, ENABLE); - LOG_I("UART4 Init"); -} - int BSP_H308_Init(void) { - UART4_Init(); - rt_pin_mode(H308_PWR_PIN, PIN_MODE_OUTPUT); rt_pin_mode(H308_RST_PIN, PIN_MODE_OUTPUT); - H308_PWR_OFF; H308_RST_OFF; + H308_PWR_ON; uart4_rx_ok_sem = rt_sem_create("uart4_rx", 0, RT_IPC_FLAG_FIFO); if (uart4_rx_ok_sem == RT_NULL) @@ -398,7 +363,7 @@ int BSP_H308_Init(void) { LOG_E("uart4_rx_timer create failed"); } - + UART4_Init(); rt_err_t ret = rt_thread_init(&h308_thread, "h308_thread", h308_thread_entry, @@ -419,7 +384,7 @@ int BSP_H308_Init(void) return ret; } -// INIT_PREV_EXPORT(BSP_H308_Init); +INIT_PREV_EXPORT(BSP_H308_Init); void UART4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); void UART4_IRQHandler(void) diff --git a/bsp/src/bsp_hr.c b/bsp/src/bsp_hr.c index 6cc63df..742b2d1 100644 --- a/bsp/src/bsp_hr.c +++ b/bsp/src/bsp_hr.c @@ -2,7 +2,7 @@ * @Author : stark1898y 1658608470@qq.com * @Date : 2024-09-04 17:33:57 * @LastEditors: mbw && 1600520629@qq.com - * @LastEditTime: 2025-01-13 09:30:42 + * @LastEditTime: 2025-01-15 08:53:20 * @FilePath: \JT-DT-YD4N02A_RTT_MRS-NT26K\bsp\src\bsp_hr.c * @Description : * @@ -36,7 +36,6 @@ static lwrb_t uart5_tx_rb; static rt_uint8_t uart5_tx_rb_data[UART5_TX_RB_LENGTH]; static rt_sem_t uart5_rx_ok_sem; -static rt_sem_t uart5_rx_parity_err_sem; static rt_timer_t uart5_timer; uint8_t hr_rx_flag = 0; // 接收缓冲区中,已经收到的数据包数量 @@ -492,22 +491,16 @@ int Process_Factor_Cmd(rt_uint8_t cmd) static void hr_thread_entry(void *param) { LOG_D("hr_thread_entry"); - uint8_t rx_buff[64] = {0}; + char rx_buff[64] = {0}; lwrb_reset(&uart5_rx_rb); lwrb_reset(&uart5_tx_rb); while (1) { rt_sem_take(uart5_rx_ok_sem, RT_WAITING_FOREVER); - if (rt_sem_trytake(uart5_rx_parity_err_sem) == 0) - { - LOG_D("uart5_rx_parity_err_sem"); - lwrb_free(&uart5_rx_rb); - continue; - } uint8_t len = lwrb_get_full(&uart5_rx_rb); lwrb_read(&uart5_rx_rb, rx_buff, len); - LOG_HEX("hr_rx_data:", 16, &rx_buff[0], len); + if (rt_strstr(rx_buff, "[getParameter]") != RT_NULL) { @@ -522,7 +515,7 @@ static void hr_thread_entry(void *param) { if (len >= HOST_FRAME_MIN_LEN) { - TsFrameData *HostFrameData = HR_GetFrameData(rx_buff, len); + TsFrameData *HostFrameData = HR_GetFrameData((rt_uint8_t *)rx_buff, len); if (HostFrameData != RT_NULL) { LOG_HEX("HostFrameData", 16, &HostFrameData->data[0], HostFrameData->len); // 数据段 @@ -534,6 +527,7 @@ static void hr_thread_entry(void *param) } } lwrb_reset(&uart5_rx_rb); + LOG_HEX("hr_rx_data:", 16, (rt_uint8_t *)&rx_buff[0], len); rt_memset(rx_buff, 0, len); } } @@ -594,12 +588,6 @@ int BSP_HR_Init(void) { LOG_E("uart5_rx_ok_sem create failed"); } - uart5_rx_parity_err_sem = rt_sem_create("uart5_rx_pe", 0, RT_IPC_FLAG_PRIO); - if (uart5_rx_parity_err_sem == RT_NULL) - { - LOG_E("uart5_rx_parity_err_sem create failed"); - } - uart5_timer = rt_timer_create("uart5_timeout", uart5_timeout, RT_NULL, 50, RT_TIMER_FLAG_PERIODIC); if (uart5_timer == RT_NULL) { @@ -634,10 +622,7 @@ void UART5_IRQHandler(void) GET_INT_SP(); rt_interrupt_enter(); unsigned char data; - if (USART_GetITStatus(UART5, USART_IT_PE) != RESET) - { - rt_sem_release(uart5_rx_parity_err_sem); - } + if (USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) { data = USART_ReceiveData(UART5); diff --git a/bsp/src/bsp_nt26k.c b/bsp/src/bsp_nt26k.c index 4a48daf..cf2129c 100644 --- a/bsp/src/bsp_nt26k.c +++ b/bsp/src/bsp_nt26k.c @@ -1574,7 +1574,7 @@ int BSP_Nt26k_Thread_Init (void) // INIT_APP_EXPORT(BSP_Nt26k_Thread_Init); -int nt26k_device_register (void) +static int nt26k_device_register (void) { struct at_device_nt26k *nt26k = &_dev; @@ -1585,6 +1585,6 @@ int nt26k_device_register (void) (void *)nt26k); } -// INIT_COMPONENT_EXPORT(nt26k_device_register); +INIT_COMPONENT_EXPORT(nt26k_device_register); #endif // IOT_MODULE_SWITCH