uart.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. uart_hal_tx_type uart_hal_tx;
  11. void InitUartQueue(pUARTQUEUE pQueue)
  12. {
  13. setbuf(stdout, NULL);
  14. pQueue->data = pQueue->head = pQueue->tail = 0;
  15. uart_hal_tx.output_p = uart_hal_tx.input_p = 0;
  16. if (HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.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;
  26. // printf("Function : %s : \r\n",__func__);
  27. AdcTimerCnt = UartRxTimerCnt = 0;
  28. pQueue = &TerminalQueue;
  29. pQueue->head++;
  30. if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  31. pQueue->data++;
  32. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  33. GetDataFromUartQueue(huart);
  34. HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 1);
  35. // Set_UartRcv(true);
  36. }
  37. void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
  38. {
  39. pUARTQUEUE pQueue = &TerminalQueue;
  40. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  41. GetDataFromUartQueue(huart);
  42. pQueue->Buffer[pQueue->head++] = data;
  43. if (pQueue->head == QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  44. pQueue->data++;
  45. // HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 10);
  46. }
  47. uint8_t uart_buf[QUEUE_BUFFER_LENGTH];
  48. void GetDataFromUartQueue(UART_HandleTypeDef *huart)
  49. {
  50. volatile static int cnt;
  51. // UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
  52. UART_HandleTypeDef *dst = &hTerminal;
  53. pUARTQUEUE pQueue = &TerminalQueue;
  54. // if (HAL_UART_Transmit(dst, pQueue->Buffer + pQueue->tail, 1, 3000) != HAL_OK)
  55. // {
  56. // _Error_Handler(__FILE__, __LINE__);
  57. // }
  58. uart_buf[cnt++] = *(pQueue->Buffer + pQueue->tail);
  59. #ifdef DEBUG_PRINT
  60. printf("%02x ",*(pQueue->Buffer + pQueue->tail)) ;
  61. #endif /* DEBUG_PRINT */
  62. pQueue->tail++;
  63. if (pQueue->tail >= QUEUE_BUFFER_LENGTH) pQueue->tail = 0;
  64. pQueue->data--;
  65. if(pQueue->data == 0){
  66. // printf("data cnt zero !!! \r\n");
  67. RF_Ctrl_Main(&uart_buf[Header]);
  68. // HAL_UART_Transmit(dst, &temp_buf[BLUECELL_HEADER00], 11, 3000);
  69. #if 0 // PYJ.2019.07.15_BEGIN --
  70. for(int i = 0; i < cnt; i++){
  71. printf("%02x ",uart_buf[i]);
  72. }
  73. #endif // PYJ.2019.07.15_END --
  74. memset(uart_buf,0x00,cnt);
  75. // for(int i = 0; i < cnt; i++)
  76. // uart_buf[i] = 0;
  77. cnt = 0;
  78. HAL_Delay(1);
  79. }
  80. }
  81. void Uart1_Data_Send(uint8_t* data,uint8_t size){
  82. HAL_UART_Transmit(&huart1, data,size, 10);
  83. }