uart(2701).c 2.7 KB

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