uart.c 3.6 KB

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