从 2 个 UART、STM32F4-Discovery、HAL 驱动程序接收数据
Receiving data from 2 UARTs, STM32F4-Discovery, HAL drivers
我想从通过 2 个 UART 连接到我的 STM32F4-Discovery 的 2 个传感器接收数据。从 STM32 向传感器传输数据有效,但接收无效。保证不是硬件问题。我找到了 1 个连接的 UART 的教程,但不是同时找到 2 个的教程。我想使用新的 HAL 驱动程序和 DMA 进行接收。感谢帮助。
UART_HandleTypeDef huart4;
UART_HandleTypeDef huart5;
DMA_HandleTypeDef hdma_uart4_rx;
DMA_HandleTypeDef hdma_uart5_rx;
uint8_t rxBuffer4 = '[=10=]0';
uint8_t rxBuffer5 = '[=10=]0';
void initGPIOs_UART4(void){
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
GPIO_InitStructure.Alternate = GPIO_AF8_UART4;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void initGPIOs_UART5(void){
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_PIN_12;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
GPIO_InitStructure.Alternate = GPIO_AF8_UART5;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_2;
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void initUART4(void){
initGPIOs_UART4();
__HAL_RCC_UART4_CLK_ENABLE();
huart4.Instance = UART4;
huart4.Init.BaudRate = 9600;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&huart4);
__HAL_UART_FLUSH_DRREGISTER(&huart4);
}
void initUART5(void){
initGPIOs_UART5();
__HAL_RCC_UART5_CLK_ENABLE();
huart5.Instance = UART5;
huart5.Init.BaudRate = 9600;
huart5.Init.WordLength = UART_WORDLENGTH_8B;
huart5.Init.StopBits = UART_STOPBITS_1;
huart5.Init.Parity = UART_PARITY_NONE;
huart5.Init.Mode = UART_MODE_TX_RX;
huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart5.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&huart5);
__HAL_UART_FLUSH_DRREGISTER(&huart5);
}
void receive_start_UART4(void){
HAL_UART_Receive_DMA(&huart4, &rxBuffer4, 1);
}
void receive_start_UART5(void){
HAL_UART_Receive_DMA(&huart5, &rxBuffer5, 1);
}
void print_UART4(uint8_t string[], uint8_t length){
HAL_UART_Transmit(&huart4, (uint8_t*)string, length, 5);
}
void print_UART5(uint8_t string[], uint8_t length){
HAL_UART_Transmit(&huart5, (uint8_t*)string, length, 5);
}
void initDMA1_UART4(void){
__HAL_RCC_DMA1_CLK_ENABLE();
hdma_uart4_rx.Instance = DMA1_Stream2;
hdma_uart4_rx.Init.Channel = DMA_CHANNEL_4;
hdma_uart4_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_uart4_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_uart4_rx.Init.MemInc = DMA_MINC_DISABLE;
hdma_uart4_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_uart4_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_uart4_rx.Init.Mode = DMA_CIRCULAR;
hdma_uart4_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_uart4_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
HAL_DMA_Init(&hdma_uart4_rx);
__HAL_LINKDMA(&huart4, hdmarx, hdma_uart4_rx);
HAL_NVIC_SetPriority(DMA1_Stream2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream2_IRQn);
}
void initDMA1_UART5(void){
__HAL_RCC_DMA1_CLK_ENABLE();
hdma_uart5_rx.Instance = DMA1_Stream1;
hdma_uart5_rx.Init.Channel = DMA_CHANNEL_5;
hdma_uart5_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_uart5_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_uart5_rx.Init.MemInc = DMA_MINC_DISABLE;
hdma_uart5_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_uart5_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_uart5_rx.Init.Mode = DMA_CIRCULAR;
hdma_uart5_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_uart5_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
HAL_DMA_Init(&hdma_uart5_rx);
__HAL_LINKDMA(&huart5, hdmarx, hdma_uart5_rx);
HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);
}
void DMA1_Stream2_IRQHandler(void){
HAL_NVIC_ClearPendingIRQ(DMA1_Stream2_IRQn);
HAL_DMA_IRQHandler(&hdma_uart4_rx);
}
void DMA1_Stream1_IRQHandler(void){
HAL_NVIC_ClearPendingIRQ(DMA1_Stream1_IRQn);
HAL_DMA_IRQHandler(&hdma_uart5_rx);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
__HAL_UART_FLUSH_DRREGISTER(&huart4);
__HAL_UART_FLUSH_DRREGISTER(&huart5);
trace_printf("%d - %d\n",rxBuffer4,rxBuffer5);
//Here I´m able to receive only from UART4
}
错误的 DMA Stream/Channel
DMA1_Stream1 DMA_CHANNEL_5
分配给 UART7_TX
。
UART5_RX
的 stream/channel 应该是 DMA1_Stream0 DMA_CHANNEL_4
。
见Reference Manual RM0090
第 308 页
我想从通过 2 个 UART 连接到我的 STM32F4-Discovery 的 2 个传感器接收数据。从 STM32 向传感器传输数据有效,但接收无效。保证不是硬件问题。我找到了 1 个连接的 UART 的教程,但不是同时找到 2 个的教程。我想使用新的 HAL 驱动程序和 DMA 进行接收。感谢帮助。
UART_HandleTypeDef huart4;
UART_HandleTypeDef huart5;
DMA_HandleTypeDef hdma_uart4_rx;
DMA_HandleTypeDef hdma_uart5_rx;
uint8_t rxBuffer4 = '[=10=]0';
uint8_t rxBuffer5 = '[=10=]0';
void initGPIOs_UART4(void){
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
GPIO_InitStructure.Alternate = GPIO_AF8_UART4;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void initGPIOs_UART5(void){
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_PIN_12;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
GPIO_InitStructure.Alternate = GPIO_AF8_UART5;
HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_2;
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void initUART4(void){
initGPIOs_UART4();
__HAL_RCC_UART4_CLK_ENABLE();
huart4.Instance = UART4;
huart4.Init.BaudRate = 9600;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&huart4);
__HAL_UART_FLUSH_DRREGISTER(&huart4);
}
void initUART5(void){
initGPIOs_UART5();
__HAL_RCC_UART5_CLK_ENABLE();
huart5.Instance = UART5;
huart5.Init.BaudRate = 9600;
huart5.Init.WordLength = UART_WORDLENGTH_8B;
huart5.Init.StopBits = UART_STOPBITS_1;
huart5.Init.Parity = UART_PARITY_NONE;
huart5.Init.Mode = UART_MODE_TX_RX;
huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart5.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&huart5);
__HAL_UART_FLUSH_DRREGISTER(&huart5);
}
void receive_start_UART4(void){
HAL_UART_Receive_DMA(&huart4, &rxBuffer4, 1);
}
void receive_start_UART5(void){
HAL_UART_Receive_DMA(&huart5, &rxBuffer5, 1);
}
void print_UART4(uint8_t string[], uint8_t length){
HAL_UART_Transmit(&huart4, (uint8_t*)string, length, 5);
}
void print_UART5(uint8_t string[], uint8_t length){
HAL_UART_Transmit(&huart5, (uint8_t*)string, length, 5);
}
void initDMA1_UART4(void){
__HAL_RCC_DMA1_CLK_ENABLE();
hdma_uart4_rx.Instance = DMA1_Stream2;
hdma_uart4_rx.Init.Channel = DMA_CHANNEL_4;
hdma_uart4_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_uart4_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_uart4_rx.Init.MemInc = DMA_MINC_DISABLE;
hdma_uart4_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_uart4_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_uart4_rx.Init.Mode = DMA_CIRCULAR;
hdma_uart4_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_uart4_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
HAL_DMA_Init(&hdma_uart4_rx);
__HAL_LINKDMA(&huart4, hdmarx, hdma_uart4_rx);
HAL_NVIC_SetPriority(DMA1_Stream2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream2_IRQn);
}
void initDMA1_UART5(void){
__HAL_RCC_DMA1_CLK_ENABLE();
hdma_uart5_rx.Instance = DMA1_Stream1;
hdma_uart5_rx.Init.Channel = DMA_CHANNEL_5;
hdma_uart5_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_uart5_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_uart5_rx.Init.MemInc = DMA_MINC_DISABLE;
hdma_uart5_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_uart5_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_uart5_rx.Init.Mode = DMA_CIRCULAR;
hdma_uart5_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_uart5_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
HAL_DMA_Init(&hdma_uart5_rx);
__HAL_LINKDMA(&huart5, hdmarx, hdma_uart5_rx);
HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);
}
void DMA1_Stream2_IRQHandler(void){
HAL_NVIC_ClearPendingIRQ(DMA1_Stream2_IRQn);
HAL_DMA_IRQHandler(&hdma_uart4_rx);
}
void DMA1_Stream1_IRQHandler(void){
HAL_NVIC_ClearPendingIRQ(DMA1_Stream1_IRQn);
HAL_DMA_IRQHandler(&hdma_uart5_rx);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
__HAL_UART_FLUSH_DRREGISTER(&huart4);
__HAL_UART_FLUSH_DRREGISTER(&huart5);
trace_printf("%d - %d\n",rxBuffer4,rxBuffer5);
//Here I´m able to receive only from UART4
}
错误的 DMA Stream/Channel
DMA1_Stream1 DMA_CHANNEL_5
分配给 UART7_TX
。
UART5_RX
的 stream/channel 应该是 DMA1_Stream0 DMA_CHANNEL_4
。
见Reference Manual RM0090 第 308 页