uart.c 4.4 KB

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