uart.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * uart.c
  3. *
  4. * Created on: 2019. 5. 27.
  5. * Author: parkyj
  6. */
  7. #include "uart.h"
  8. #include <stdbool.h>
  9. UARTQUEUE TerminalQueue;
  10. UARTQUEUE WifiQueue;
  11. bool UartDataisReved;
  12. void InitUartQueue(UART_HandleTypeDef *huart,pUARTQUEUE pQueue)
  13. {
  14. UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
  15. pQueue->data = pQueue->head = pQueue->tail = 0;
  16. if (HAL_UART_Receive_DMA(dst, pQueue->Buffer, 1) != HAL_OK)
  17. {
  18. // _Error_Handler(__FILE__, __LINE__);
  19. }
  20. //HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1);
  21. //HAL_UART_Receive_IT(hTerminal, pQueue->Buffer + pQueue->head, 1);
  22. }
  23. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  24. {
  25. pUARTQUEUE pQueue = (huart->Instance == USART2 ? &WifiQueue:&TerminalQueue);
  26. UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
  27. pQueue->head++;
  28. if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  29. pQueue->data++;
  30. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  31. GetDataFromUartQueue(huart);
  32. HAL_UART_Receive_DMA(dst, pQueue->Buffer + pQueue->head, 1);
  33. UartDataRecvSet(true);
  34. // Set_UartRcv(true);
  35. }
  36. void UartDataRecvSet(bool val){
  37. UartDataisReved = val;
  38. }
  39. bool UartDataRecvGet(void){
  40. return UartDataisReved;
  41. }
  42. void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
  43. {
  44. pUARTQUEUE pQueue = (huart->Instance == USART2 ? &WifiQueue:&TerminalQueue);
  45. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  46. GetDataFromUartQueue(huart);
  47. pQueue->Buffer[pQueue->head++] = data;
  48. if (pQueue->head == QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  49. pQueue->data++;
  50. // HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 10);
  51. }
  52. typedef enum{
  53. Header = 0,
  54. Type,
  55. Length,
  56. Crcindex,
  57. }Bluecell_Prot_p;
  58. uint8_t uart_buf[QUEUE_BUFFER_LENGTH];
  59. bool GoodDataStatus = false;
  60. void GetDataFromUartQueue(UART_HandleTypeDef *huart)
  61. {
  62. UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
  63. pUARTQUEUE pQueue = (huart->Instance == USART2 ? &WifiQueue:&TerminalQueue);
  64. volatile static int cnt;
  65. #if 0 // PYJ.2019.12.13_BEGIN --
  66. if(huart->Instance == USART1){
  67. HAL_UART_Transmit_DMA(&hWifi, pQueue->Buffer + pQueue->tail, 1);
  68. }else{
  69. printf("%c",*(pQueue->Buffer + pQueue->tail));
  70. }
  71. #endif // PYJ.2019.12.13_END --
  72. printf("%c",*(pQueue->Buffer + pQueue->tail));
  73. uart_buf[cnt++] = *(pQueue->Buffer + pQueue->tail);
  74. if(*(pQueue->Buffer + pQueue->tail) == 0xbe)
  75. GoodDataStatus = true;
  76. // printf("Function : %s : ",__func__);
  77. // if (HAL_UART_Transmit_DMA(dst, pQueue->Buffer + pQueue->tail, 1) != HAL_OK)
  78. //if (HAL_UART_Transmit_DMA(&hTerminal, pQueue->Buffer + pQueue->tail, 1) != HAL_OK)
  79. //{
  80. // _Error_Handler(__FILE__, __LINE__);
  81. //}
  82. // printf("\r\n");
  83. pQueue->tail++;
  84. if (pQueue->tail >= QUEUE_BUFFER_LENGTH) pQueue->tail = 0;
  85. pQueue->data--;
  86. if(pQueue->data == 0 && GoodDataStatus == true){
  87. ESP8266_StrFilter(&uart_buf[Header]);
  88. memset(uart_buf,NULL,cnt);
  89. cnt = 0;
  90. GoodDataStatus = false;
  91. }
  92. }
  93. void Uart1_Data_Send(uint8_t* data,uint8_t size){
  94. HAL_UART_Transmit_DMA(&huart1, data,size);
  95. }
  96. void Uart2_Data_Send(uint8_t* data,uint8_t size){
  97. HAL_UART_Transmit_DMA(&huart2, data,size);
  98. }