AT.c 4.3 KB

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