| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #include "stm32f10x.h"
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include "At_Module.h"
- #include "Timer_Module.h"
- #include "Log_Module.h"
- #include "CONFIG.h"
- // AT指令
- struct AT_Struct
- {
- enum AT_Status status;
- uint8_t result[1024];
- uint16_t result_length;
- };
- static struct AT_Struct at_Struct = {
- .status = AT_Status_None,
- .result_length = 0
- };
- // 串口中断函数
- void USART2_IRQHandler(void)
- {
- if(USART_GetFlagStatus(USART2, USART_IT_RXNE) == SET)
- {
- uint8_t RxData = USART_ReceiveData(USART2);
- // 写入缓存
- at_Struct.result[at_Struct.result_length++] = RxData;
-
- // 串口的一些设置
- USART_ClearITPendingBit(USART2, USART_IT_RXNE);
- }
- }
- // 发送AT指令的函数,配置串口
- static void USART_SendByte(uint8_t Byte)
- {
- USART_SendData(USART2, Byte);
- while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
- }
- // 发送字节数组
- static void USART_SendBytes(uint8_t * bytes, uint16_t Length)
- {
- uint16_t i;
- for(i = 0; i < Length; i ++)
- {
- USART_SendByte(bytes[i]);
- }
- }
- // 获取AT指令返回的长度
- uint16_t AT_result_length(void)
- {
- return at_Struct.result_length;
- }
- // 获取AT指令返回的内容
- uint8_t * AT_result(void)
- {
- return at_Struct.result;
- }
- // 发送AT指令,二进制
- void AT_Send_Bytes(uint8_t * CMD, uint16_t length)
- {
- // 状态
- AT_Set_Status(AT_Status_Using);
- // 发送AT指令
- USART_SendBytes(CMD, length);
- }
- // 发送AT指令,字符串
- void AT_Send_String(char * CMD)
- {
- AT_Send_Bytes((uint8_t * )CMD, strlen(CMD));
- }
- // 清空AT指令
- void AT_Clear()
- {
- AT_Set_Status(AT_Status_None);
- AT_Clear_Result();
- }
- // 清空AT指令返回的结果
- void AT_Clear_Result()
- {
- // 清除AT指令返回
- memset(at_Struct.result, 0, sizeof(at_Struct.result));
- at_Struct.result_length = 0;
- }
- // 设置AT指令的状态
- void AT_Set_Status(enum AT_Status status)
- {
- at_Struct.status = status;
- }
- // 获取AT指令的状态
- enum AT_Status AT_Get_Status(void)
- {
- return at_Struct.status;
- }
- // 未处理数据用到的一些变量
- static uint16_t at_last_length = 0;
- static struct TIMER_Struct timer = {
- .time = 0,
- .flag = 0
- };
- // 获取AT返回数据后的等待时间
- uint32_t AT_wait_time(void)
- {
- uint16_t length = AT_result_length();
- if(length == 0)
- {
- time_clear(&timer);
- return 0;
- }
- // 没有数据
- if(length != at_last_length)
- {
- at_last_length = length;
- time_clear(&timer);
- return 0;
- }
- return time_get_delay(&timer);
- }
- // AT监听
- void AT_ResidueHandle(void)
- {
- 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();
- }
- }
- }
|