#include "stm32f10x.h" #include #include #include #include #include "AT.h" #include "sys.h" #include "ec800m.h" uint8_t DEBUG = 1; // Debug开关 // 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指令的函数,配置串口 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; } // 串口打印日志 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.status == AT_Status_None && AT.result_length > 0) { // 发送日志 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; extern struct EC800M ec800m; void my_timer() { Client5_timer_ms++; Business_timer_ms++; ec800m.timer_ms++; timer_ms++; } 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; } }