uart.c 2.7 KB

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