uart.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * uart.c
  3. *
  4. * Created on: 2019. 5. 27.
  5. * Author: parkyj
  6. */
  7. #include <stdio.h>
  8. #include "main.h"
  9. #include "uart.h"
  10. #include "string.h"
  11. UARTQUEUE TerminalQueue;
  12. UARTQUEUE WifiQueue;
  13. uart_hal_tx_type uart_hal_tx;
  14. extern volatile uint32_t UartRxTimerCnt;
  15. void InitUartQueue(pUARTQUEUE pQueue)
  16. {
  17. setbuf(stdout, NULL);
  18. pQueue->data = pQueue->head = pQueue->tail = 0;
  19. uart_hal_tx.output_p = uart_hal_tx.input_p = 0;
  20. if (HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1) != HAL_OK)
  21. {
  22. //_Error_Handler(__FILE__, __LINE__);
  23. }
  24. //HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1);
  25. //HAL_UART_Receive_IT(hTerminal, pQueue->Buffer + pQueue->head, 1);
  26. }
  27. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  28. {
  29. pUARTQUEUE pQueue;
  30. printf("Function : %s : \r\n",__func__);
  31. // AdcTimerCnt = UartRxTimerCnt = 0;
  32. pQueue = &TerminalQueue;
  33. pQueue->head++;
  34. if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  35. pQueue->data++;
  36. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  37. GetDataFromUartQueue(huart);
  38. HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 1);
  39. // Set_UartRcv(true);
  40. }
  41. void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
  42. {
  43. pUARTQUEUE pQueue = &TerminalQueue;
  44. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  45. GetDataFromUartQueue(huart);
  46. pQueue->Buffer[pQueue->head++] = data;
  47. if (pQueue->head == QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  48. pQueue->data++;
  49. // HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 10);
  50. }
  51. volatile uint8_t uart_buf[QUEUE_BUFFER_LENGTH];
  52. void GetDataFromUartQueue(UART_HandleTypeDef *huart)
  53. {
  54. volatile static int cnt;
  55. // UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
  56. // UART_HandleTypeDef *dst = &hTerminal;
  57. pUARTQUEUE pQueue = &TerminalQueue;
  58. // if (HAL_UART_Transmit(dst, pQueue->Buffer + pQueue->tail, 1, 3000) != HAL_OK)
  59. // {
  60. // _Error_Handler(__FILE__, __LINE__);
  61. // }
  62. uart_buf[cnt++] = *(pQueue->Buffer + pQueue->tail);
  63. #ifdef DEBUG_PRINT
  64. printf("%02x ",*(pQueue->Buffer + pQueue->tail)) ;
  65. #endif /* DEBUG_PRINT */
  66. pQueue->tail++;
  67. if (pQueue->tail >= QUEUE_BUFFER_LENGTH) pQueue->tail = 0;
  68. pQueue->data--;
  69. if(pQueue->data == 0){
  70. // printf("data cnt zero !!! \r\n");
  71. //RF_Ctrl_Main(&uart_buf[Header]);
  72. // HAL_UART_Transmit(dst, &temp_buf[BLUECELL_HEADER00], 11, 3000);
  73. #if 1 // PYJ.2019.07.15_BEGIN --
  74. for(int i = 0; i < cnt; i++){
  75. printf("%02x ",uart_buf[i]);
  76. }
  77. #endif // PYJ.2019.07.15_END --
  78. memset(uart_buf,0x00,cnt);
  79. // for(int i = 0; i < cnt; i++)
  80. // uart_buf[i] = 0;
  81. cnt = 0;
  82. HAL_Delay(1);
  83. }
  84. }
  85. void Uart_Check(void){
  86. while (TerminalQueue.data > 0 && UartRxTimerCnt > 100) GetDataFromUartQueue(&hTerminal);
  87. }
  88. void Uart1_Data_Send(uint8_t* data,uint8_t size){
  89. HAL_UART_Transmit_DMA(&huart1, data,size);
  90. }