At_Module.c 2.6 KB

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