uart.c 5.9 KB

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