uart.c 3.1 KB

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