At_Module.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "stm32f10x.h"
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include "At_Module.h"
  6. #include "Timer_Module.h"
  7. #include "Log_Module.h"
  8. #include "CONFIG.h"
  9. #define LENGTH 1024
  10. // AT指令
  11. struct AT_Struct
  12. {
  13. enum AT_Status status;
  14. uint8_t result[LENGTH];
  15. uint16_t result_length;
  16. };
  17. static struct AT_Struct at_Struct = {
  18. .status = AT_Status_None,
  19. .result_length = 0
  20. };
  21. // 串口中断函数
  22. void USART2_IRQHandler(void)
  23. {
  24. if(USART_GetFlagStatus(USART2, USART_IT_RXNE) == SET)
  25. {
  26. uint8_t RxData = USART_ReceiveData(USART2);
  27. // 判断数组写入长度,防止过界
  28. if(at_Struct.result_length >= (LENGTH-1))
  29. {
  30. at_Struct.result_length = 0;
  31. }
  32. // 写入缓存
  33. at_Struct.result[at_Struct.result_length++] = RxData;
  34. // 串口的一些设置
  35. USART_ClearITPendingBit(USART2, USART_IT_RXNE);
  36. }
  37. }
  38. // 发送AT指令的函数,配置串口
  39. static void USART_SendByte(uint8_t Byte)
  40. {
  41. USART_SendData(USART2, Byte);
  42. while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
  43. }
  44. // 发送字节数组
  45. static void USART_SendBytes(uint8_t * bytes, uint16_t Length)
  46. {
  47. uint16_t i;
  48. for(i = 0; i < Length; i ++)
  49. {
  50. USART_SendByte(bytes[i]);
  51. }
  52. }
  53. // 获取AT指令返回的长度
  54. uint16_t AT_result_length(void)
  55. {
  56. return at_Struct.result_length;
  57. }
  58. // 获取AT指令返回的内容
  59. uint8_t * AT_result(void)
  60. {
  61. return at_Struct.result;
  62. }
  63. // 发送AT指令,二进制
  64. void AT_Send_Bytes(uint8_t * CMD, uint16_t length)
  65. {
  66. // 状态
  67. AT_Set_Status(AT_Status_Using);
  68. // 发送AT指令
  69. USART_SendBytes(CMD, length);
  70. }
  71. // 发送AT指令,字符串
  72. void AT_Send_String(char * CMD)
  73. {
  74. AT_Send_Bytes((uint8_t * )CMD, strlen(CMD));
  75. }
  76. // 清空AT指令
  77. void AT_Clear()
  78. {
  79. AT_Set_Status(AT_Status_None);
  80. AT_Clear_Result();
  81. }
  82. // 清空AT指令返回的结果
  83. void AT_Clear_Result()
  84. {
  85. // 清除AT指令返回
  86. memset(at_Struct.result, 0, sizeof(at_Struct.result));
  87. at_Struct.result_length = 0;
  88. }
  89. // 设置AT指令的状态
  90. void AT_Set_Status(enum AT_Status status)
  91. {
  92. at_Struct.status = status;
  93. }
  94. // 获取AT指令的状态
  95. enum AT_Status AT_Get_Status(void)
  96. {
  97. return at_Struct.status;
  98. }
  99. // 未处理数据用到的一些变量
  100. static uint16_t at_last_length = 0;
  101. static struct TIMER_Struct timer = {
  102. .time = 0,
  103. .flag = 0
  104. };
  105. // 获取AT返回数据后的等待时间
  106. uint32_t AT_wait_time(void)
  107. {
  108. uint16_t length = AT_result_length();
  109. if(length == 0)
  110. {
  111. time_clear(&timer);
  112. return 0;
  113. }
  114. // 没有数据
  115. if(length != at_last_length)
  116. {
  117. at_last_length = length;
  118. time_clear(&timer);
  119. return 0;
  120. }
  121. return time_get_delay(&timer);
  122. }
  123. // AT监听
  124. void AT_ResidueHandle(void)
  125. {
  126. if(AT_Get_Status() == AT_Status_None)
  127. {
  128. // 没有数据
  129. if(AT_wait_time() > 20) // 过20ms之后再取数据,避免过早取数据影响其他的业务逻辑。
  130. {
  131. // 发送日志
  132. Log_Printf_Debug("未处理数据:\r\n");
  133. Log_SendArray_Debug(AT_result(), AT_result_length());
  134. AT_Clear();
  135. }
  136. }
  137. }