uart.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * uart.c
  3. *
  4. * Created on: 2019. 5. 27.
  5. * Author: parkyj
  6. */
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include "main.h"
  10. #include "uart.h"
  11. #include "string.h"
  12. #include "Bluecell_operate.h"
  13. UARTQUEUE TerminalQueue;
  14. UARTQUEUE WifiQueue;
  15. uart_hal_tx_type uart_hal_tx;
  16. extern volatile uint32_t UartRxTimerCnt;
  17. extern bool Bluecell_Operate(uint8_t* data);
  18. void InitUartQueue(pUARTQUEUE pQueue)
  19. {
  20. pQueue->data = pQueue->head = pQueue->tail = 0;
  21. uart_hal_tx.output_p = uart_hal_tx.input_p = 0;
  22. // HAL_UART_Receive_IT(&huart2,rxBuf,5);
  23. if (HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1) != HAL_OK)
  24. //if (HAL_UART_Receive_IT(&hTerminal, TerminalQueue.Buffer, 1) != HAL_OK)
  25. {
  26. // _Error_Handler(__FILE__, __LINE__);
  27. }
  28. if (HAL_UART_Receive_DMA(&hTest, TerminalQueue.Buffer, 1) != HAL_OK)
  29. {
  30. // _Error_Handler(__FILE__, __LINE__);
  31. }
  32. //HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1);
  33. //HAL_UART_Receive_IT(hTerminal, pQueue->Buffer + pQueue->head, 1);
  34. }
  35. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  36. {
  37. // UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hTest:&hTerminal);
  38. pUARTQUEUE pQueue;
  39. // printf("Function : %s : \r\n",__func__);
  40. //printf("%02x ",uart_buf[i]);
  41. UartRxTimerCnt = 0;
  42. pQueue = &TerminalQueue;
  43. pQueue->head++;
  44. if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  45. pQueue->data++;
  46. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  47. GetDataFromUartQueue(huart);
  48. HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 1);
  49. HAL_UART_Receive_DMA(&hTest, pQueue->Buffer + pQueue->head, 1);
  50. // Set_UartRcv(true);
  51. }
  52. void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
  53. {
  54. pUARTQUEUE pQueue = &TerminalQueue;
  55. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  56. GetDataFromUartQueue(huart);
  57. pQueue->Buffer[pQueue->head++] = data;
  58. if (pQueue->head == QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  59. pQueue->data++;
  60. // HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 10);
  61. }
  62. volatile uint8_t uart_buf[QUEUE_BUFFER_LENGTH];
  63. void GetDataFromUartQueue(UART_HandleTypeDef *huart)
  64. {
  65. volatile static int cnt;
  66. bool chksumret = 0;
  67. uint16_t Length = 0;
  68. uint16_t CrcChk = 0;
  69. UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hTest:&hTerminal);
  70. // UART_HandleTypeDef *dst = &hTerminal;
  71. pUARTQUEUE pQueue = &TerminalQueue;
  72. // if (HAL_UART_Transmit(dst, pQueue->Buffer + pQueue->tail, 1, 3000) != HAL_OK)
  73. // {
  74. // _Error_Handler(__FILE__, __LINE__);
  75. // }
  76. uart_buf[cnt++] = *(pQueue->Buffer + pQueue->tail);
  77. //#ifdef DEBUG_PRINT
  78. // printf("%02x ",*(pQueue->Buffer + pQueue->tail)) ;
  79. //#endif /* DEBUG_PRINT */
  80. pQueue->tail++;
  81. if (pQueue->tail >= QUEUE_BUFFER_LENGTH) pQueue->tail = 0;
  82. pQueue->data--;
  83. if(pQueue->data == 0){
  84. // printf("data cnt zero !!! \r\n");
  85. //RF_Ctrl_Main(&uart_buf[Header]);
  86. // HAL_UART_Transmit(dst, &temp_buf[BLUECELL_HEADER00], 11, 3000);
  87. #if 1// PYJ.2019.07.15_BEGIN --
  88. printf("\r\n[RX]");
  89. for(int i = 0; i < cnt; i++){
  90. printf("%02x ",uart_buf[i]);
  91. }
  92. printf(" CNT : %d \r\n",cnt);
  93. #endif // PYJ.2019.07.15_END --
  94. if(uart_buf[0] == 0xbe){
  95. Length = uart_buf[BLUECELL_LENGTH_H] << 8 | uart_buf[BLUECELL_LENGTH_L] ;
  96. CrcChk = uart_buf[Length + 1] << 8 | uart_buf[Length + 2] ;
  97. if(CRC16_Check(&uart_buf[BLUECELL_TYPE], Length,CrcChk))
  98. Bluecell_Operate(uart_buf);
  99. }else if(uart_buf[0] == MBIC_PREAMBLE0
  100. &&uart_buf[1] == MBIC_PREAMBLE1
  101. &&uart_buf[2] == MBIC_PREAMBLE2
  102. &&uart_buf[3] == MBIC_PREAMBLE3)
  103. {
  104. if(Chksum_Check(uart_buf,MBIC_HEADER_SIZE - 4,uart_buf[MBIC_CHECKSHUM_INDEX]))
  105. {
  106. Length = ((uart_buf[MBIC_LENGTH_0] << 8) | uart_buf[MBIC_LENGTH_1]);
  107. CrcChk = ((uart_buf[MBIC_PAYLOADSTART + Length + 1] << 8) | (uart_buf[MBIC_PAYLOADSTART + Length + 2]));
  108. if(CRC16_Check(&uart_buf[MBIC_PAYLOADSTART], Length,CrcChk))
  109. MBIC_Operate(uart_buf);
  110. else
  111. printf("CRC ERROR \r\n");
  112. }
  113. else
  114. {
  115. printf("CHECK SUM ERR \r\n");
  116. }
  117. }
  118. memset(uart_buf,0x00,cnt);
  119. // for(int i = 0; i < cnt; i++)
  120. // uart_buf[i] = 0;
  121. cnt = 0;
  122. // HAL_Delay(1);
  123. }
  124. }
  125. void Uart_Check(void){
  126. while (TerminalQueue.data > 0 && UartRxTimerCnt > 50) GetDataFromUartQueue(&hTerminal);
  127. }
  128. void Uart1_Data_Send(uint8_t* data,uint16_t size){
  129. // HAL_UART_Transmit(&hTerminal, &data[0],size,0xFFFF);
  130. // HAL_UART_Transmit(&hTest, &data[0],size,0xFFFF);
  131. HAL_UART_Transmit_DMA(&hTerminal, &data[0],size);
  132. //HAL_UART_Transmit_IT(&hTerminal, &data[0],size);
  133. // printf("data[278] : %x \r\n",data[278]);
  134. //// HAL_Delay(1);
  135. // printf("\r\n [TX] : {");
  136. // for(int i = 0; i< size; i++)
  137. // printf(",0x%02x ",data[i]);
  138. // printf("};\r\n\tCOUNT : %d \r\n",size);
  139. // printf("\r\n");
  140. }