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); } }
注意对应中断服务函数需要重新定义。
正在阅读:
STM32F411UART小结04-10
反应工程期末考试试题04-30
口腔病理复习题10-15
读《小橘灯》有感06-20
2018年高考题和高考模拟题物理分项版汇编专题12电磁感应06-19
我发现了秋的美作文500字06-25
基层工会换届选举请示样式12-14
某矿区遗留重金属废渣治理项目-方案设计06-10
柴油知识05-17
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 小结
- STM32F411UART
- 高频电子技术习题库
- 沟槽土方开挖施工组织设计
- 全等三角形的判定2 - 边角边
- 植保无人机飞防项目可行性研究报告
- 公司专检报告(导地线) - 图文
- 初中英语听力技巧分角度发展探微
- 2017年人教七年级语文上册(带句子)拼音生字词
- 国家重点研发计划专项项目申请书模板
- 东莞职业技术学院包装材料检测实训室设备采购项目
- 西师版三年级数学下册《长方形正方形面积的计算》说课稿
- 《银行业金融机构董事(理事)和高级管理人员任职资格管理办法》
- 通信概论习题总结
- 安全保卫常识140题
- 2012.5.1实施的贵州驾校科目一最新题库 - 图文
- 县委书记在全县统战工作会议上的讲话
- 债法总论笔记
- 世界各国主要年份农业人口比重统计(1990-2003)
- 监督部门、评标专家权利义务
- 《红色警戒2》RULES规则修改教程入门(超详细)
- 11.18山东教师招聘考试每日一练