uart(6947).c 2.9 KB

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