AT.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. uint8_t DEBUG = 1; // Debug开关
  10. // AT指令
  11. struct AT_Struct AT = {
  12. .status = AT_Status_None,
  13. .result_length = 0
  14. };
  15. // 获取AT指令返回的长度
  16. uint16_t AT_result_length(void)
  17. {
  18. return AT.result_length;
  19. }
  20. // 获取AT指令返回的内容
  21. uint8_t * AT_result(void)
  22. {
  23. return AT.result;
  24. }
  25. // 发送AT指令的函数,配置串口
  26. static void USART_SendByte(uint8_t Byte)
  27. {
  28. USART_SendData(USART2, Byte);
  29. while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
  30. }
  31. // 发送字节数组
  32. static void USART_Send_Bytes(uint8_t * bytes, uint16_t Length)
  33. {
  34. uint16_t i;
  35. for(i = 0; i < Length; i ++)
  36. {
  37. USART_SendByte(bytes[i]);
  38. }
  39. }
  40. // 串口中断函数
  41. void USART2_IRQHandler(void)
  42. {
  43. if(USART_GetFlagStatus(USART2, USART_IT_RXNE) == SET)
  44. {
  45. uint8_t RxData = USART_ReceiveData(USART2);
  46. // 写入缓存
  47. AT.result[AT.result_length++] = RxData;
  48. // 串口的一些设置
  49. USART_ClearITPendingBit(USART2, USART_IT_RXNE);
  50. }
  51. }
  52. // 发送AT指令,二进制
  53. void AT_Send_Bytes(uint8_t * CMD, uint16_t length)
  54. {
  55. // 状态
  56. AT.status = AT_Status_Using;
  57. // 发送AT指令
  58. USART_Send_Bytes(CMD, length);
  59. }
  60. // 发送AT指令,字符串
  61. void AT_Send_String(char * CMD)
  62. {
  63. AT_Send_Bytes((uint8_t * )CMD, strlen(CMD));
  64. }
  65. // 清空AT指令
  66. void AT_Clear()
  67. {
  68. AT.status = AT_Status_None;
  69. // 清除AT指令返回
  70. memset(AT.result, 0, sizeof(AT.result));
  71. AT.result_length = 0;
  72. }
  73. // 清空AT指令返回的结果
  74. void AT_Clear_Result()
  75. {
  76. // 清除AT指令返回
  77. memset(AT.result, 0, sizeof(AT.result));
  78. AT.result_length = 0;
  79. }
  80. // 设置AT指令的状态
  81. void AT_Set_Status(enum AT_Status status)
  82. {
  83. AT.status = status;
  84. }
  85. // 串口打印日志
  86. void Log_SendByte(uint8_t Byte)
  87. {
  88. USART_SendData(USART1, Byte);
  89. while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  90. }
  91. void Log_SendString(char *String)
  92. {
  93. uint16_t i;
  94. for(i = 0; String[i] != '\0'; i ++)
  95. // for(i = 0; i<5; i ++)
  96. {
  97. Log_SendByte(String[i]);
  98. }
  99. }
  100. // 打印字节
  101. void Log_SendArray(uint8_t * arr, uint16_t len)
  102. {
  103. uint16_t i;
  104. for(i = 0; i < len; i ++)
  105. {
  106. Log_SendByte(arr[i]);
  107. }
  108. }
  109. char Log_String[512];
  110. void Log_Printf(char *format, ...)
  111. {
  112. memset(Log_String, 0, sizeof(Log_String));
  113. va_list arg;
  114. va_start(arg, format);
  115. vsprintf(Log_String, format, arg);
  116. va_end(arg);
  117. Log_SendString(Log_String);
  118. }
  119. // 打印字节
  120. void Log_SendArray_Debug(uint8_t * arr, uint16_t len)
  121. {
  122. if(DEBUG == 0) return; // 调试开关
  123. Log_SendArray(arr, len);
  124. }
  125. // 打印的缓存
  126. void Log_Printf_Debug(char *format, ...)
  127. {
  128. if(DEBUG == 0) return; // 调试开关
  129. memset(Log_String, 0, sizeof(Log_String));
  130. va_list arg;
  131. va_start(arg, format);
  132. vsprintf(Log_String, format, arg);
  133. va_end(arg);
  134. Log_SendString(Log_String);
  135. }
  136. // AT监听
  137. void AT_Handle()
  138. {
  139. if(AT.status == AT_Status_None && AT.result_length > 0)
  140. {
  141. // 发送日志
  142. Log_Printf_Debug("未处理数据:\r\n");
  143. Log_SendArray_Debug(AT.result, AT.result_length);
  144. AT_Clear();
  145. }
  146. }
  147. // 定时器
  148. uint32_t timer_ms = 0;
  149. extern uint32_t Client5_timer_ms;
  150. extern uint32_t Business_timer_ms;
  151. extern struct EC800M ec800m;
  152. void my_timer()
  153. {
  154. Client5_timer_ms++;
  155. Business_timer_ms++;
  156. ec800m.timer_ms++;
  157. timer_ms++;
  158. }
  159. uint32_t get_time(void)
  160. {
  161. return timer_ms;
  162. }
  163. uint32_t get_time_diff(uint32_t time)
  164. {
  165. if(timer_ms >= time)
  166. {
  167. return timer_ms - time;
  168. }
  169. else
  170. {
  171. return 0xFFFFFFFF - time + timer_ms;
  172. }
  173. }