AT.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "stm32f10x.h"
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "AT.h"
  7. #include "sys.h"
  8. #include "ec800m.h"
  9. #include "LowPower.h"
  10. #include "bc260.h"
  11. uint8_t DEBUG = 1; // Debug开关
  12. // 函数声明
  13. uint32_t get_time(void);
  14. uint32_t get_time_diff(uint32_t time);
  15. // 未处理数据用到的一些变量
  16. static uint16_t at_last_length = 0;
  17. struct TIMER_Struct AT_Timer = {
  18. .time = 0,
  19. .flag = 0
  20. };
  21. // AT指令
  22. struct AT_Struct AT = {
  23. .status = AT_Status_None,
  24. .result_length = 0
  25. };
  26. // 获取AT指令返回的长度
  27. uint16_t AT_result_length(void)
  28. {
  29. return AT.result_length;
  30. }
  31. // 获取AT指令返回的内容
  32. uint8_t * AT_result(void)
  33. {
  34. return AT.result;
  35. }
  36. // 获取AT返回数据后的等待时间
  37. uint32_t AT_wait_time(void)
  38. {
  39. uint16_t length = AT_result_length();
  40. if(length == 0)
  41. {
  42. time_clear(&AT_Timer);
  43. return 0;
  44. }
  45. // 没有数据
  46. if(length != at_last_length)
  47. {
  48. at_last_length = length;
  49. time_clear(&AT_Timer);
  50. return 0;
  51. }
  52. return time_get(&AT_Timer);
  53. }
  54. // 发送AT指令的函数,配置串口
  55. static void USART_SendByte(uint8_t Byte)
  56. {
  57. USART_SendData(USART2, Byte);
  58. while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
  59. }
  60. // 发送字节数组
  61. static void USART_Send_Bytes(uint8_t * bytes, uint16_t Length)
  62. {
  63. uint16_t i;
  64. for(i = 0; i < Length; i ++)
  65. {
  66. USART_SendByte(bytes[i]);
  67. }
  68. }
  69. // 串口中断函数
  70. void USART2_IRQHandler(void)
  71. {
  72. if(USART_GetFlagStatus(USART2, USART_IT_RXNE) == SET)
  73. {
  74. uint8_t RxData = USART_ReceiveData(USART2);
  75. // 写入缓存
  76. AT.result[AT.result_length++] = RxData;
  77. // 串口的一些设置
  78. USART_ClearITPendingBit(USART2, USART_IT_RXNE);
  79. }
  80. }
  81. // 发送AT指令,二进制
  82. void AT_Send_Bytes(uint8_t * CMD, uint16_t length)
  83. {
  84. // 状态
  85. AT.status = AT_Status_Using;
  86. // 发送AT指令
  87. USART_Send_Bytes(CMD, length);
  88. }
  89. // 发送AT指令,字符串
  90. void AT_Send_String(char * CMD)
  91. {
  92. AT_Send_Bytes((uint8_t * )CMD, strlen(CMD));
  93. }
  94. // 清空AT指令
  95. void AT_Clear()
  96. {
  97. AT.status = AT_Status_None;
  98. // 清除AT指令返回
  99. memset(AT.result, 0, sizeof(AT.result));
  100. AT.result_length = 0;
  101. }
  102. // 清空AT指令返回的结果
  103. void AT_Clear_Result()
  104. {
  105. // 清除AT指令返回
  106. memset(AT.result, 0, sizeof(AT.result));
  107. AT.result_length = 0;
  108. }
  109. // 设置AT指令的状态
  110. void AT_Set_Status(enum AT_Status status)
  111. {
  112. AT.status = status;
  113. }
  114. // 获取AT指令的状态
  115. enum AT_Status AT_Get_Status(void)
  116. {
  117. return AT.status;
  118. }
  119. // 串口打印日志
  120. void Log_SendByte(uint8_t Byte)
  121. {
  122. USART_SendData(USART1, Byte);
  123. while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  124. }
  125. void Log_SendString(char *String)
  126. {
  127. uint16_t i;
  128. for(i = 0; String[i] != '\0'; i ++)
  129. // for(i = 0; i<5; i ++)
  130. {
  131. Log_SendByte(String[i]);
  132. }
  133. }
  134. // 打印字节
  135. void Log_SendArray(uint8_t * arr, uint16_t len)
  136. {
  137. uint16_t i;
  138. for(i = 0; i < len; i ++)
  139. {
  140. Log_SendByte(arr[i]);
  141. }
  142. }
  143. char Log_String[512];
  144. void Log_Printf(char *format, ...)
  145. {
  146. memset(Log_String, 0, sizeof(Log_String));
  147. va_list arg;
  148. va_start(arg, format);
  149. vsprintf(Log_String, format, arg);
  150. va_end(arg);
  151. Log_SendString(Log_String);
  152. }
  153. // 打印字节
  154. void Log_SendArray_Debug(uint8_t * arr, uint16_t len)
  155. {
  156. if(DEBUG == 0) return; // 调试开关
  157. Log_SendArray(arr, len);
  158. }
  159. // 打印的缓存
  160. void Log_Printf_Debug(char *format, ...)
  161. {
  162. if(DEBUG == 0) return; // 调试开关
  163. memset(Log_String, 0, sizeof(Log_String));
  164. va_list arg;
  165. va_start(arg, format);
  166. vsprintf(Log_String, format, arg);
  167. va_end(arg);
  168. Log_SendString(Log_String);
  169. }
  170. // AT监听
  171. void AT_Handle()
  172. {
  173. if(AT_Get_Status() == AT_Status_None)
  174. {
  175. // 没有数据
  176. if(AT_wait_time() > 20) // 过20ms之后再取数据,避免过早取数据影响其他的业务逻辑。
  177. {
  178. // 发送日志
  179. Log_Printf_Debug("未处理数据:\r\n");
  180. Log_SendArray_Debug(AT_result(), AT_result_length());
  181. AT_Clear();
  182. }
  183. }
  184. }
  185. // 定时器
  186. uint32_t timer_ms = 0;
  187. extern uint32_t Client5_timer_ms;
  188. extern uint32_t Business_timer_ms;
  189. void my_timer()
  190. {
  191. Client5_timer_ms++;
  192. Business_timer_ms++;
  193. Power_timer_ms++;
  194. timer_ms++;
  195. #if NBFLAG
  196. bc260y.timer_ms++;
  197. #endif
  198. #if _4GFLAG
  199. ec800m.timer_ms++;
  200. Restart_time_ms++;
  201. #endif
  202. }
  203. uint32_t get_time(void)
  204. {
  205. return timer_ms;
  206. }
  207. uint32_t get_time_diff(uint32_t time)
  208. {
  209. if(timer_ms >= time)
  210. {
  211. return timer_ms - time;
  212. }
  213. else
  214. {
  215. return 0xFFFFFFFF - time + timer_ms;
  216. }
  217. }
  218. uint32_t time_get(struct TIMER_Struct * timer)
  219. {
  220. if(timer->flag == 0)
  221. {
  222. timer->flag = 1;
  223. timer->time = get_time();
  224. return 0;
  225. }
  226. else if(timer->flag == 1)
  227. {
  228. return get_time_diff(timer->time);
  229. }
  230. else
  231. {
  232. return 0;
  233. }
  234. }
  235. void time_clear(struct TIMER_Struct * timer)
  236. {
  237. timer->flag = 0;
  238. timer->time = 0;
  239. }