uart.c 3.1 KB

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