uart.c 2.8 KB

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