#include "stm32f10x.h" #include #include #include #include #include "AT.h" #include "sys.h" #include "ec800m.h" #include "LowPower.h" #include "bc260.h" uint8_t DEBUG = 1; // Debug开关 // 函数声明 uint32_t get_time(void); uint32_t get_time_diff(uint32_t time); // 未处理数据用到的一些变量 static uint16_t at_last_length = 0; struct TIMER_Struct AT_Timer = { .time = 0, .flag = 0 }; // AT指令 struct AT_Struct AT = { .status = AT_Status_None, .result_length = 0 }; // 获取AT指令返回的长度 uint16_t AT_result_length(void) { return AT.result_length; } // 获取AT指令返回的内容 uint8_t * AT_result(void) { return AT.result; } // 获取AT返回数据后的等待时间 uint32_t AT_wait_time(void) { uint16_t length = AT_result_length(); if(length == 0) { time_clear(&AT_Timer); return 0; } // 没有数据 if(length != at_last_length) { at_last_length = length; time_clear(&AT_Timer); return 0; } return time_get(&AT_Timer); } // 发送AT指令的函数,配置串口 static void USART_SendByte(uint8_t Byte) { USART_SendData(USART2, Byte); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); } // 发送字节数组 static void USART_Send_Bytes(uint8_t * bytes, uint16_t Length) { uint16_t i; for(i = 0; i < Length; i ++) { USART_SendByte(bytes[i]); } } // 串口中断函数 void USART2_IRQHandler(void) { if(USART_GetFlagStatus(USART2, USART_IT_RXNE) == SET) { uint8_t RxData = USART_ReceiveData(USART2); // 写入缓存 AT.result[AT.result_length++] = RxData; // 串口的一些设置 USART_ClearITPendingBit(USART2, USART_IT_RXNE); } } // 发送AT指令,二进制 void AT_Send_Bytes(uint8_t * CMD, uint16_t length) { // 状态 AT.status = AT_Status_Using; // 发送AT指令 USART_Send_Bytes(CMD, length); } // 发送AT指令,字符串 void AT_Send_String(char * CMD) { AT_Send_Bytes((uint8_t * )CMD, strlen(CMD)); } // 清空AT指令 void AT_Clear() { AT.status = AT_Status_None; // 清除AT指令返回 memset(AT.result, 0, sizeof(AT.result)); AT.result_length = 0; } // 清空AT指令返回的结果 void AT_Clear_Result() { // 清除AT指令返回 memset(AT.result, 0, sizeof(AT.result)); AT.result_length = 0; } // 设置AT指令的状态 void AT_Set_Status(enum AT_Status status) { AT.status = status; } // 获取AT指令的状态 enum AT_Status AT_Get_Status(void) { return AT.status; } // 串口打印日志 void Log_SendByte(uint8_t Byte) { USART_SendData(USART1, Byte); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } void Log_SendString(char *String) { uint16_t i; for(i = 0; String[i] != '\0'; i ++) // for(i = 0; i<5; i ++) { Log_SendByte(String[i]); } } // 打印字节 void Log_SendArray(uint8_t * arr, uint16_t len) { uint16_t i; for(i = 0; i < len; i ++) { Log_SendByte(arr[i]); } } char Log_String[512]; void Log_Printf(char *format, ...) { memset(Log_String, 0, sizeof(Log_String)); va_list arg; va_start(arg, format); vsprintf(Log_String, format, arg); va_end(arg); Log_SendString(Log_String); } // 打印字节 void Log_SendArray_Debug(uint8_t * arr, uint16_t len) { if(DEBUG == 0) return; // 调试开关 Log_SendArray(arr, len); } // 打印的缓存 void Log_Printf_Debug(char *format, ...) { if(DEBUG == 0) return; // 调试开关 memset(Log_String, 0, sizeof(Log_String)); va_list arg; va_start(arg, format); vsprintf(Log_String, format, arg); va_end(arg); Log_SendString(Log_String); } // AT监听 void AT_Handle() { if(AT_Get_Status() == AT_Status_None) { // 没有数据 if(AT_wait_time() > 20) // 过20ms之后再取数据,避免过早取数据影响其他的业务逻辑。 { // 发送日志 Log_Printf_Debug("未处理数据:\r\n"); Log_SendArray_Debug(AT_result(), AT_result_length()); AT_Clear(); } } } // 定时器 uint32_t timer_ms = 0; extern uint32_t Client5_timer_ms; extern uint32_t Business_timer_ms; void my_timer() { Client5_timer_ms++; Business_timer_ms++; Power_timer_ms++; timer_ms++; #if NBFLAG bc260y.timer_ms++; #endif #if _4GFLAG ec800m.timer_ms++; Restart_time_ms++; #endif } uint32_t get_time(void) { return timer_ms; } uint32_t get_time_diff(uint32_t time) { if(timer_ms >= time) { return timer_ms - time; } else { return 0xFFFFFFFF - time + timer_ms; } } uint32_t time_get(struct TIMER_Struct * timer) { if(timer->flag == 0) { timer->flag = 1; timer->time = get_time(); return 0; } else if(timer->flag == 1) { return get_time_diff(timer->time); } else { return 0; } } void time_clear(struct TIMER_Struct * timer) { timer->flag = 0; timer->time = 0; }