uart.c 5.1 KB

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