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. // 未处理数据用到的一些变量
  10. static uint16_t at_last_length = 0;
  11. static struct TIMER_Struct timer = {
  12. .time = 0,
  13. .flag = 0
  14. };
  15. // AT指令
  16. struct AT_Struct
  17. {
  18. enum AT_Status status;
  19. uint8_t result[1024];
  20. uint16_t result_length;
  21. };
  22. static struct AT_Struct at_Struct = {
  23. .status = AT_Status_None,
  24. .result_length = 0
  25. };
  26. // 获取AT指令返回的长度
  27. uint16_t AT_result_length(void)
  28. {
  29. return at_Struct.result_length;
  30. }
  31. // 获取AT指令返回的内容
  32. uint8_t * AT_result(void)
  33. {
  34. return at_Struct.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(&timer);
  43. return 0;
  44. }
  45. // 没有数据
  46. if(length != at_last_length)
  47. {
  48. at_last_length = length;
  49. time_clear(&timer);
  50. return 0;
  51. }
  52. return time_get_delay(&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_Struct.result[at_Struct.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_Struct.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_Struct.status = AT_Status_None;
  98. // 清除AT指令返回
  99. memset(at_Struct.result, 0, sizeof(at_Struct.result));
  100. at_Struct.result_length = 0;
  101. }
  102. // 清空AT指令返回的结果
  103. void AT_Clear_Result()
  104. {
  105. // 清除AT指令返回
  106. memset(at_Struct.result, 0, sizeof(at_Struct.result));
  107. at_Struct.result_length = 0;
  108. }
  109. // 设置AT指令的状态
  110. void AT_Set_Status(enum AT_Status status)
  111. {
  112. at_Struct.status = status;
  113. }
  114. // 获取AT指令的状态
  115. enum AT_Status AT_Get_Status(void)
  116. {
  117. return at_Struct.status;
  118. }
  119. // AT监听
  120. void AT_Handle()
  121. {
  122. if(AT_Get_Status() == AT_Status_None)
  123. {
  124. // 没有数据
  125. if(AT_wait_time() > 20) // 过20ms之后再取数据,避免过早取数据影响其他的业务逻辑。
  126. {
  127. // 发送日志
  128. Log_Printf_Debug("未处理数据:\r\n");
  129. Log_SendArray_Debug(AT_result(), AT_result_length());
  130. AT_Clear();
  131. }
  132. }
  133. }