STM32F411UART小结

更新时间:2024-04-10 09:42:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

STM32F401 NUCLEO板UART口调试小结

项目需要,用了两个UART口,分别为UART1使用DMA方式收发,UART2使用中断收发,相关初始化: ? UART1:

UART_HandleTypeDef ble_uart_handle;

/********************************************************************* * @fn BleUart_Init() *

* @brief uart1 initiate *

* @param none * @return none */

extern void BleUart_Init(void) { ble_uart_handle.Instance = USART1; ble_uart_handle.Init.BaudRate = 9600; ble_uart_handle.Init.WordLength = UART_WORDLENGTH_8B; ble_uart_handle.Init.StopBits = UART_STOPBITS_1; ble_uart_handle.Init.Parity = UART_PARITY_NONE; ble_uart_handle.Init.Mode = UART_MODE_TX_RX; ble_uart_handle.Init.HwFlowCtl = UART_HWCONTROL_NONE; ble_uart_handle.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&ble_uart_handle) != HAL_OK) { HAL_UART_Init(&ble_uart_handle); } }

? UART2

UART_HandleTypeDef gps_uart_handle;

/********************************************************************* * @fn GpsUart_Init() *

* @brief uart2 initiate *

* @param none * @return none */

extern void GpsUart_Init(void) { gps_uart_handle.Instance = USART2; gps_uart_handle.Init.BaudRate = 9600;

}

gps_uart_handle.Init.WordLength = UART_WORDLENGTH_8B; gps_uart_handle.Init.StopBits = UART_STOPBITS_1; gps_uart_handle.Init.Parity = UART_PARITY_NONE; gps_uart_handle.Init.Mode = UART_MODE_TX_RX;

gps_uart_handle.Init.HwFlowCtl = UART_HWCONTROL_NONE; gps_uart_handle.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&gps_uart_handle) != HAL_OK) { Error_Handler(); }

? 涉及硬件部分初始化:

void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; static DMA_HandleTypeDef hdma_tx; static DMA_HandleTypeDef hdma_rx; if (huart->Instance == USART1) { /* Peripheral clock enable */ __USART1_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); __GPIOA_CLK_ENABLE(); /**USART1 GPIO Configuration PA9 ------> USART1_CTS PA10 ------> USART1_RX PA12 ------> USART1_RTS PB6 ------> USART1_TX */ GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10| GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

#if _BLE_UART_DMA_ DMAx_CLK_ENABLE();//Enable DMA2 clock /* Configure the DMA handler for Transmission process */ hdma_tx.Instance = USARTx_TX_DMA_STREAM; hdma_tx.Init.Channel = USARTx_TX_DMA_CHANNEL; hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_tx.Init.Mode = DMA_NORMAL; hdma_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_tx.Init.MemBurst = DMA_MBURST_INC4; hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_tx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmatx, hdma_tx); /* Configure the DMA handler for Transmission process */ hdma_rx.Instance = USARTx_RX_DMA_STREAM; hdma_rx.Init.Channel = USARTx_RX_DMA_CHANNEL; hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_rx.Init.Mode = DMA_NORMAL; hdma_rx.Init.Priority = DMA_PRIORITY_HIGH; hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_rx.Init.MemBurst = DMA_MBURST_INC4; hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_rx); /* Associate the initialized DMA handle to the the UART handle */ __HAL_LINKDMA(huart, hdmarx, hdma_rx); /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */

HAL_NVIC_SetPriority(USARTx_DMA_TX_IRQn, 0, 1); HAL_NVIC_EnableIRQ(USARTx_DMA_TX_IRQn); /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */ HAL_NVIC_SetPriority(USARTx_DMA_RX_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USARTx_DMA_RX_IRQn);

#endif HAL_NVIC_SetPriority(USART1_IRQn, 0, 1); HAL_NVIC_EnableIRQ(USART1_IRQn); } else if (huart->Instance == USART2) { /* Peripheral clock enable */ __USART2_CLK_ENABLE(); GpsUart_TX_CLK_ENABLE(); GpsUart_RX_CLK_ENABLE(); /**USART2 GPIO Configuration PA3 ------> USART2_RX PA2 ------> USART2_TX */ GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_NVIC_SetPriority(USART2_IRQn, 0, 1); HAL_NVIC_EnableIRQ(USART2_IRQn); } }

? 其中GPIO CLK使能等宏定义在其他文件中,即:

? UART1

//enable DMA mode

#define _BLE_UART_DMA_ 1

/* Definition for USARTx clock resources */ #define DMAx_CLK_ENABLE() __DMA2_CLK_ENABLE()

#define USARTx_FORCE_RESET() __USART1_FORCE_RESET() #define USARTx_RELEASE_RESET() __USART1_RELEASE_RESET()

/* Definition for USARTx's DMA */

#define USARTx_TX_DMA_CHANNEL DMA_CHANNEL_4 #define USARTx_TX_DMA_STREAM DMA2_Stream7 #define USARTx_RX_DMA_CHANNEL DMA_CHANNEL_4 #define USARTx_RX_DMA_STREAM DMA2_Stream5

/* Definition for USARTx's NVIC */ #define USARTx_DMA_TX_IRQn DMA2_Stream7_IRQn #define USARTx_DMA_RX_IRQn DMA2_Stream5_IRQn #define BleUart_DMA_TX_IRQHandler DMA2_Stream7_IRQHandler #define BleUart_DMA_RX_IRQHandler DMA2_Stream5_IRQHandler #define USART_BLE_IRQHandler USART1_IRQHandler

? UART2:

#define GpsUart_TX_CLK_ENABLE() __GPIOA_CLK_ENABLE() #define GpsUart_RX_CLK_ENABLE() __GPIOA_CLK_ENABLE()

? 完成上述初始化工作后,UART1即可通过以下接口调用收发: BleUart_Receive_DMA(ble_comm.rx_buf, sizeof(ble_comm.rx_buf)); BleUart_Transmit_DMA(ble_comm.tx_buf, sizeof(buf));

? 相应中断函数: /**

* @brief This function handles DMA RX interrupt request. * @param None * @retval None */

void BleUart_DMA_RX_IRQHandler(void) { HAL_DMA_IRQHandler(ble_uart_handle.hdmarx); } /**

* @brief This function handles DMA TX interrupt request. * @param None * @retval None */

void BleUart_DMA_TX_IRQHandler(void) { HAL_DMA_IRQHandler(ble_uart_handle.hdmatx); }

? UART2通过以下接口开启中断接收去发送: ? 使能中断接收:

__HAL_UART_ENABLE_IT(&gps_uart_handle, UART_IT_RXNE);

? 中断发送函数:

/********************************************************************* * @fn BleUart_TransmitIT() *

* @brief transmit data via uart1 in interrupt mode. *

* @param uint8_t * data * @return none */

extern void GpsUart_TransmitIT(uint8_t *pbuff, uint16_t length) { HAL_UART_Transmit_IT(&gps_uart_handle, pbuff, length); }

? 中断响应函数:

/********************************************************************* * @fn GpsUart_IRQHandler() *

* @brief This function handles UART interrupt request. *

* @param none * @return none */

void GpsUart_IRQHandler(void) { if (USART2->SR & USART_SR_RXNE) { gps_comm.rx_buf[gps_comm.write_index] = (uint8_t)(USART2->DR & (uint8_t)0x00FF); if (gps_comm.write_index++ >= UART_RXBUF_SIZE) { gps_comm.write_index = 0; } if (gps_comm.data_len < UART_RXBUF_SIZE) { gps_comm.data_len++; } USART2->SR &= ~USART_SR_RXNE; HAL_UART_RxCpltCallback(&gps_uart_handle); GpsUart_Decode(); } else if (USART2->SR & USART_SR_TXE) { USART2->SR &= ~USART_SR_TXE; HAL_UART_IRQHandler(&gps_uart_handle); } }

注意对应中断服务函数需要重新定义。

/********************************************************************* * @fn BleUart_TransmitIT() *

* @brief transmit data via uart1 in interrupt mode. *

* @param uint8_t * data * @return none */

extern void GpsUart_TransmitIT(uint8_t *pbuff, uint16_t length) { HAL_UART_Transmit_IT(&gps_uart_handle, pbuff, length); }

? 中断响应函数:

/********************************************************************* * @fn GpsUart_IRQHandler() *

* @brief This function handles UART interrupt request. *

* @param none * @return none */

void GpsUart_IRQHandler(void) { if (USART2->SR & USART_SR_RXNE) { gps_comm.rx_buf[gps_comm.write_index] = (uint8_t)(USART2->DR & (uint8_t)0x00FF); if (gps_comm.write_index++ >= UART_RXBUF_SIZE) { gps_comm.write_index = 0; } if (gps_comm.data_len < UART_RXBUF_SIZE) { gps_comm.data_len++; } USART2->SR &= ~USART_SR_RXNE; HAL_UART_RxCpltCallback(&gps_uart_handle); GpsUart_Decode(); } else if (USART2->SR & USART_SR_TXE) { USART2->SR &= ~USART_SR_TXE; HAL_UART_IRQHandler(&gps_uart_handle); } }

注意对应中断服务函数需要重新定义。

本文来源:https://www.bwwdw.com/article/fx3r.html

Top