uart_interface.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "uart_interface.h"
  2. #include <uart4.h>
  3. uart_type uart_msg = {
  4. .rx_count_u8 = 0,
  5. .tx_count_u8 = 0,
  6. .rx_finished_flg = 0,
  7. .disconnect_flg = 0,
  8. .rx_over_time = 5,
  9. .rx_over_time_count = 0xFF,
  10. .disconnect_count = 0,
  11. };
  12. void uart_rx_ticks(void)
  13. {
  14. if (uart_msg.rx_over_time_count != 0xFF)
  15. {
  16. if (++uart_msg.rx_over_time_count >= uart_msg.rx_over_time)
  17. {
  18. uart_msg.rx_over_time_count = 0xFF;
  19. uart_msg.rx_finished_flg = 1;
  20. }
  21. }
  22. if (uart_msg.disconnect_count != 0xFFFF)
  23. {
  24. if (++uart_msg.disconnect_count >= 2500) // 5000ms
  25. {
  26. uart_msg.disconnect_count = 0xFFFF;
  27. uart_msg.disconnect_flg = 1;
  28. }
  29. }
  30. }
  31. void uart_start_send(uart_type *p_msg)
  32. {
  33. p_msg->tx_count_u8 = 0;
  34. UART_RXNE_IT_DISABLE();
  35. USART_SendData(UART4, p_msg->tx[p_msg->tx_count_u8++]);
  36. UART_TXE_IT_ENABLE();
  37. }
  38. void uart4_it(void)
  39. {
  40. // 接收中断处理
  41. if (RESET != USART_GetITStatus(UART4, USART_IT_RXNE))
  42. {
  43. USART_ClearITPendingBit(UART4, USART_IT_RXNE);
  44. uart_msg.rx_over_time_count = 0;
  45. uart_msg.disconnect_count = 0;
  46. uart_msg.disconnect_flg = 0;
  47. if (uart_msg.rx_count_u8 >= RX_TX_BUF_LEN)
  48. {
  49. uart_msg.rx_count_u8 = 0;
  50. }
  51. uart_msg.rx[uart_msg.rx_count_u8++] = USART_ReceiveData(UART4);
  52. }
  53. // 发送中断处理
  54. if (RESET != USART_GetITStatus(UART4, USART_IT_TXE))
  55. {
  56. USART_ClearITPendingBit(UART4, USART_IT_TXE);
  57. USART_SendData(UART4, uart_msg.tx[uart_msg.tx_count_u8++]);
  58. if (uart_msg.tx_count_u8 >= uart_msg.tx_len)
  59. {
  60. USART_ITConfig(UART4, USART_IT_TXE, DISABLE);
  61. USART_ITConfig(UART4, USART_IT_TC, ENABLE);
  62. }
  63. }
  64. // 发送完成中断
  65. if (RESET != USART_GetITStatus(UART4, USART_IT_TC))
  66. {
  67. USART_ClearITPendingBit(UART4, USART_IT_TC);
  68. USART_ITConfig(UART4, USART_IT_TC, DISABLE);
  69. USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
  70. }
  71. // 发送错误
  72. if (RESET != USART_GetITStatus(UART4, USART_IT_ORE))
  73. USART_ClearITPendingBit(UART4, USART_IT_ORE);
  74. }