| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #include "stm32f10x.h"
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <stdlib.h>
- #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;
- }
|