uart.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * uart.c
  3. *
  4. * Created on: 2019. 5. 27.
  5. * Author: parkyj
  6. */
  7. #include "main.h"
  8. UARTQUEUE TerminalQueue;
  9. UARTQUEUE UbxQueue;
  10. void InitUartQueue(pUARTQUEUE pQueue)
  11. {
  12. pQueue->data = pQueue->head = pQueue->tail = 0;
  13. }
  14. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  15. {
  16. pUARTQUEUE pQueue;
  17. pQueue = (huart->Instance == USART1 ? &TerminalQueue:&UbxQueue);
  18. pQueue->head++;
  19. if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  20. pQueue->data++;
  21. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  22. GetDataFromUartQueue(huart);
  23. HAL_UART_Receive_IT(huart, pQueue->Buffer + pQueue->head, 1);
  24. }
  25. void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
  26. {
  27. pUARTQUEUE pQueue = (huart->Instance == USART1 ? &TerminalQueue:&UbxQueue);
  28. if (pQueue->data >= QUEUE_BUFFER_LENGTH)
  29. GetDataFromUartQueue(huart);
  30. pQueue->Buffer[pQueue->head++] = data;
  31. if (pQueue->head == QUEUE_BUFFER_LENGTH) pQueue->head = 0;
  32. pQueue->data++;
  33. }
  34. void GetDataFromUartQueue(UART_HandleTypeDef *huart)
  35. {
  36. // UART_HandleTypeDef *dst = (huart->Instance == USART1 ? &hTpb22:&hTerminal);
  37. // pUARTQUEUE pQueue = (huart->Instance == USART6 ? &TerminalQueue:&UbxQueue);
  38. pUARTQUEUE pQueue = (huart->Instance == USART1 ? &TerminalQueue:&UbxQueue);
  39. if(huart->Instance == USART1)
  40. {
  41. printf("%c",*(pQueue->Buffer + pQueue->tail));
  42. // printf("%c",*(pQueue->Buffer + pQueue->tail));
  43. } else{
  44. // for(uint8_t i = 0; i < 4; i++)
  45. // HAL_UART_Transmit(&hTerminal, buf[i], 1, 3000);
  46. // HAL_UART_Transmit(&hTerminal, pQueue->Buffer + pQueue->tail, 1, 3000);
  47. // printf("%c",*(pQueue->Buffer + pQueue->tail));
  48. // printf("%c",*(pQueue->Buffer + pQueue->tail));
  49. }
  50. pQueue->tail++;
  51. if (pQueue->tail >= QUEUE_BUFFER_LENGTH) pQueue->tail = 0;
  52. pQueue->data--;
  53. // if(pQueue->data == 0){
  54. // if(huart->Instance == USART1)
  55. // printf("Usart1\r\n");
  56. // else
  57. // printf("Usart6\r\n");
  58. // }
  59. HAL_Delay(1);
  60. }