hal_uart4.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "hal_uart4.h"
  2. #include "queue.h"
  3. #include "stm32f4xx.h"
  4. #include "stm32f4xx_gpio.h"
  5. #include "stm32f4xx_rcc.h"
  6. #include "stm32f4xx_usart.h"
  7. uart_type uart_msg = {
  8. .rx_count_u8 = 0,
  9. .tx_count_u8 = 0,
  10. .rx_finished_flg = 0,
  11. .disconnect_flg = 0,
  12. .rx_over_time = 5,
  13. .rx_over_time_count = 0xFF,
  14. .disconnect_count = 0,
  15. };
  16. void hal_uart4_init(void)
  17. {
  18. GPIO_InitTypeDef GPIO_StructInit;
  19. NVIC_InitTypeDef NVIC_InitStructure;
  20. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  21. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
  22. GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  23. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  24. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  25. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  26. GPIO_StructInit.GPIO_Pin = GPIO_Pin_10;
  27. GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
  28. GPIO_Init(GPIOC, &GPIO_StructInit);
  29. GPIO_StructInit.GPIO_Pin = GPIO_Pin_11;
  30. // GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  31. // GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  32. // GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  33. // GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
  34. GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
  35. GPIO_Init(GPIOC, &GPIO_StructInit);
  36. NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; // 串口1中断通道
  37. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
  38. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
  39. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
  40. NVIC_Init(&NVIC_InitStructure);
  41. USART_InitTypeDef USART_InitStructure;
  42. USART_InitStructure.USART_BaudRate = 115200;
  43. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  44. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  45. USART_InitStructure.USART_Parity = USART_Parity_No;
  46. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  47. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  48. USART_Init(UART4, &USART_InitStructure); // 初始化串口
  49. USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
  50. USART_Cmd(UART4, ENABLE); // 使能串口
  51. }
  52. void UART4_IRQHandler(void)
  53. {
  54. // 接收中断处理
  55. if (RESET != USART_GetITStatus(UART4, USART_IT_RXNE))
  56. {
  57. USART_ClearITPendingBit(UART4, USART_IT_RXNE);
  58. uart_msg.rx_over_time_count = 0;
  59. uart_msg.disconnect_count = 0;
  60. uart_msg.disconnect_flg = 0;
  61. if (uart_msg.rx_count_u8 >= RX_TX_BUF_LEN)
  62. {
  63. uart_msg.rx_count_u8 = 0;
  64. }
  65. uart_msg.rx[uart_msg.rx_count_u8++] = USART_ReceiveData(UART4);
  66. }
  67. // 发送中断处理
  68. if (RESET != USART_GetITStatus(UART4, USART_IT_TXE))
  69. {
  70. USART_ClearITPendingBit(UART4, USART_IT_TXE);
  71. USART_SendData(UART4, uart_msg.tx[uart_msg.tx_count_u8++]);
  72. if (uart_msg.tx_count_u8 >= uart_msg.tx_len)
  73. {
  74. USART_ITConfig(UART4, USART_IT_TXE, DISABLE);
  75. USART_ITConfig(UART4, USART_IT_TC, ENABLE);
  76. }
  77. }
  78. // 发送完成中断
  79. if (RESET != USART_GetITStatus(UART4, USART_IT_TC))
  80. {
  81. USART_ClearITPendingBit(UART4, USART_IT_TC);
  82. USART_ITConfig(UART4, USART_IT_TC, DISABLE);
  83. USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
  84. }
  85. // 发送错误
  86. if (RESET != USART_GetITStatus(UART4, USART_IT_ORE))
  87. USART_ClearITPendingBit(UART4, USART_IT_ORE);
  88. }
  89. void uart4_rx_ticks(void)
  90. {
  91. if (uart_msg.rx_over_time_count != 0xFF)
  92. {
  93. if (++uart_msg.rx_over_time_count >= uart_msg.rx_over_time)
  94. {
  95. uart_msg.rx_over_time_count = 0xFF;
  96. uart_msg.rx_finished_flg = 1;
  97. }
  98. }
  99. if (uart_msg.disconnect_count != 0xFFFF)
  100. {
  101. if (++uart_msg.disconnect_count >= 2500) // 5000ms
  102. {
  103. uart_msg.disconnect_count = 0xFFFF;
  104. uart_msg.disconnect_flg = 1;
  105. }
  106. }
  107. }
  108. void uart4_start_send(uart_type *p_msg)
  109. {
  110. p_msg->tx_count_u8 = 0;
  111. USART_ITConfig(UART4, USART_IT_RXNE, DISABLE);
  112. USART_SendData(UART4, p_msg->tx[p_msg->tx_count_u8++]);
  113. USART_ITConfig(UART4, USART_IT_TXE, ENABLE);
  114. }