uart.c 5.4 KB

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