uart.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * uart.c
  3. *
  4. * Created on: 2019. 5. 27.
  5. * Author: parkyj
  6. */
  7. #include "uart.h"
  8. UARTQUEUE TerminalQueue;
  9. void InitUartQueue(pUARTQUEUE pQueue)
  10. {
  11. pQueue->data = pQueue->head = pQueue->tail = 0;
  12. if (HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1) != HAL_OK)
  13. {
  14. // _Error_Handler(__FILE__, __LINE__);
  15. }
  16. //HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1);
  17. //HAL_UART_Receive_IT(hTerminal, pQueue->Buffer + pQueue->head, 1);
  18. }
  19. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  20. {
  21. pUARTQUEUE pQueue;
  22. UartTimerCnt = 0;
  23. pQueue = &TerminalQueue;
  24. pQueue->head++;
  25. if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  26. pQueue->data++;
  27. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  28. GetDataFromUartQueue(huart);
  29. HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 1);
  30. // Set_UartRcv(true);
  31. }
  32. void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
  33. {
  34. pUARTQUEUE pQueue = &TerminalQueue;
  35. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  36. GetDataFromUartQueue(huart);
  37. pQueue->Buffer[pQueue->head++] = data;
  38. if (pQueue->head == QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  39. pQueue->data++;
  40. // HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 10);
  41. }
  42. void GetDataFromUartQueue(UART_HandleTypeDef *huart)
  43. {
  44. volatile static uint8_t update_data_buf[1024];
  45. volatile static int cnt;
  46. uint8_t temp_buf[11];
  47. // UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
  48. UART_HandleTypeDef *dst = &hTerminal;
  49. pUARTQUEUE pQueue = &TerminalQueue;
  50. // if (HAL_UART_Transmit(dst, pQueue->Buffer + pQueue->tail, 1, 3000) != HAL_OK)
  51. // {
  52. // _Error_Handler(__FILE__, __LINE__);
  53. // }
  54. update_data_buf[cnt++] = *(pQueue->Buffer + pQueue->tail);
  55. pQueue->tail++;
  56. if (pQueue->tail >= QUEUE_BUFFER_LENGTH) pQueue->tail = 0;
  57. pQueue->data--;
  58. if(pQueue->data == 0){
  59. #if 0 // PYJ.2019.07.15_BEGIN --
  60. #endif // PYJ.2019.07.15_END --
  61. cnt = 0;
  62. FirmwareUpdateStart(&update_data_buf[0]);
  63. for(int i = 0; i < 1024; i++)
  64. update_data_buf[i] = 0;
  65. FirmwareTimerCnt = 0;
  66. // HAL_Delay(1);
  67. }
  68. }
  69. void Uart1_Data_Send(uint8_t* data,uint8_t size){
  70. HAL_UART_Transmit_DMA(&huart1, data,size);
  71. }