923 lines
27 KiB
C
923 lines
27 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file usart.c
|
|
* @brief This file provides code for the configuration
|
|
* of the USART instances.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2024 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "usart.h"
|
|
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
UART_HandleTypeDef huart5;
|
|
UART_HandleTypeDef huart7;
|
|
UART_HandleTypeDef huart1;
|
|
UART_HandleTypeDef huart2;
|
|
UART_HandleTypeDef huart3;
|
|
UART_HandleTypeDef huart10;
|
|
DMA_HandleTypeDef hdma_uart5_rx;
|
|
DMA_HandleTypeDef hdma_uart7_rx;
|
|
DMA_HandleTypeDef hdma_uart7_tx;
|
|
DMA_HandleTypeDef hdma_usart1_tx;
|
|
DMA_HandleTypeDef hdma_usart1_rx;
|
|
DMA_HandleTypeDef hdma_usart2_rx;
|
|
DMA_HandleTypeDef hdma_usart2_tx;
|
|
DMA_HandleTypeDef hdma_usart3_tx;
|
|
DMA_HandleTypeDef hdma_usart3_rx;
|
|
DMA_HandleTypeDef hdma_usart10_rx;
|
|
DMA_HandleTypeDef hdma_usart10_tx;
|
|
|
|
/* UART5 init function */
|
|
void MX_UART5_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN UART5_Init 0 */
|
|
|
|
/* USER CODE END UART5_Init 0 */
|
|
|
|
/* USER CODE BEGIN UART5_Init 1 */
|
|
|
|
/* USER CODE END UART5_Init 1 */
|
|
huart5.Instance = UART5;
|
|
huart5.Init.BaudRate = 100000;
|
|
huart5.Init.WordLength = UART_WORDLENGTH_9B;
|
|
huart5.Init.StopBits = UART_STOPBITS_1;
|
|
huart5.Init.Parity = UART_PARITY_EVEN;
|
|
huart5.Init.Mode = UART_MODE_TX_RX;
|
|
huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart5.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart5.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart5.Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
huart5.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_UART_Init(&huart5) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetTxFifoThreshold(&huart5, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetRxFifoThreshold(&huart5, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_DisableFifoMode(&huart5) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN UART5_Init 2 */
|
|
|
|
/* USER CODE END UART5_Init 2 */
|
|
|
|
}
|
|
/* UART7 init function */
|
|
void MX_UART7_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN UART7_Init 0 */
|
|
|
|
/* USER CODE END UART7_Init 0 */
|
|
|
|
/* USER CODE BEGIN UART7_Init 1 */
|
|
|
|
/* USER CODE END UART7_Init 1 */
|
|
huart7.Instance = UART7;
|
|
huart7.Init.BaudRate = 115200;
|
|
huart7.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart7.Init.StopBits = UART_STOPBITS_1;
|
|
huart7.Init.Parity = UART_PARITY_NONE;
|
|
huart7.Init.Mode = UART_MODE_TX_RX;
|
|
huart7.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart7.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart7.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart7.Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
huart7.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_UART_Init(&huart7) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetTxFifoThreshold(&huart7, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetRxFifoThreshold(&huart7, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_DisableFifoMode(&huart7) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN UART7_Init 2 */
|
|
|
|
/* USER CODE END UART7_Init 2 */
|
|
|
|
}
|
|
/* USART1 init function */
|
|
|
|
void MX_USART1_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART1_Init 0 */
|
|
|
|
/* USER CODE END USART1_Init 0 */
|
|
|
|
/* USER CODE BEGIN USART1_Init 1 */
|
|
|
|
/* USER CODE END USART1_Init 1 */
|
|
huart1.Instance = USART1;
|
|
huart1.Init.BaudRate = 115200;
|
|
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart1.Init.StopBits = UART_STOPBITS_1;
|
|
huart1.Init.Parity = UART_PARITY_NONE;
|
|
huart1.Init.Mode = UART_MODE_TX_RX;
|
|
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_UART_Init(&huart1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN USART1_Init 2 */
|
|
|
|
/* USER CODE END USART1_Init 2 */
|
|
|
|
}
|
|
/* USART2 init function */
|
|
|
|
void MX_USART2_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART2_Init 0 */
|
|
|
|
/* USER CODE END USART2_Init 0 */
|
|
|
|
/* USER CODE BEGIN USART2_Init 1 */
|
|
|
|
/* USER CODE END USART2_Init 1 */
|
|
huart2.Instance = USART2;
|
|
huart2.Init.BaudRate = 115200;
|
|
huart2.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart2.Init.StopBits = UART_STOPBITS_1;
|
|
huart2.Init.Parity = UART_PARITY_NONE;
|
|
huart2.Init.Mode = UART_MODE_TX_RX;
|
|
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_RS485Ex_Init(&huart2, UART_DE_POLARITY_HIGH, 0, 0) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN USART2_Init 2 */
|
|
|
|
/* USER CODE END USART2_Init 2 */
|
|
|
|
}
|
|
/* USART3 init function */
|
|
|
|
void MX_USART3_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART3_Init 0 */
|
|
|
|
/* USER CODE END USART3_Init 0 */
|
|
|
|
/* USER CODE BEGIN USART3_Init 1 */
|
|
|
|
/* USER CODE END USART3_Init 1 */
|
|
huart3.Instance = USART3;
|
|
huart3.Init.BaudRate = 115200;
|
|
huart3.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart3.Init.StopBits = UART_STOPBITS_1;
|
|
huart3.Init.Parity = UART_PARITY_NONE;
|
|
huart3.Init.Mode = UART_MODE_TX_RX;
|
|
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_RS485Ex_Init(&huart3, UART_DE_POLARITY_HIGH, 0, 0) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN USART3_Init 2 */
|
|
|
|
/* USER CODE END USART3_Init 2 */
|
|
|
|
}
|
|
/* USART10 init function */
|
|
|
|
void MX_USART10_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART10_Init 0 */
|
|
|
|
/* USER CODE END USART10_Init 0 */
|
|
|
|
/* USER CODE BEGIN USART10_Init 1 */
|
|
|
|
/* USER CODE END USART10_Init 1 */
|
|
huart10.Instance = USART10;
|
|
huart10.Init.BaudRate = 115200;
|
|
huart10.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart10.Init.StopBits = UART_STOPBITS_1;
|
|
huart10.Init.Parity = UART_PARITY_NONE;
|
|
huart10.Init.Mode = UART_MODE_TX_RX;
|
|
huart10.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart10.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart10.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart10.Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
huart10.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_UART_Init(&huart10) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetTxFifoThreshold(&huart10, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetRxFifoThreshold(&huart10, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_DisableFifoMode(&huart10) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN USART10_Init 2 */
|
|
|
|
/* USER CODE END USART10_Init 2 */
|
|
|
|
}
|
|
|
|
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
|
{
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
|
if(uartHandle->Instance==UART5)
|
|
{
|
|
/* USER CODE BEGIN UART5_MspInit 0 */
|
|
|
|
/* USER CODE END UART5_MspInit 0 */
|
|
|
|
/** Initializes the peripherals clock
|
|
*/
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_UART5;
|
|
PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/* UART5 clock enable */
|
|
__HAL_RCC_UART5_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
/**UART5 GPIO Configuration
|
|
PC12 ------> UART5_TX
|
|
PD2 ------> UART5_RX
|
|
*/
|
|
GPIO_InitStruct.Pin = GPIO_PIN_12;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
|
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_2;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
/* UART5 DMA Init */
|
|
/* UART5_RX Init */
|
|
hdma_uart5_rx.Instance = DMA1_Stream0;
|
|
hdma_uart5_rx.Init.Request = DMA_REQUEST_UART5_RX;
|
|
hdma_uart5_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
hdma_uart5_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_uart5_rx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_uart5_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_uart5_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_uart5_rx.Init.Mode = DMA_NORMAL;
|
|
hdma_uart5_rx.Init.Priority = DMA_PRIORITY_LOW;
|
|
hdma_uart5_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_uart5_rx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmarx,hdma_uart5_rx);
|
|
|
|
/* UART5 interrupt Init */
|
|
HAL_NVIC_SetPriority(UART5_IRQn, 5, 0);
|
|
HAL_NVIC_EnableIRQ(UART5_IRQn);
|
|
/* USER CODE BEGIN UART5_MspInit 1 */
|
|
|
|
/* USER CODE END UART5_MspInit 1 */
|
|
}
|
|
else if(uartHandle->Instance==UART7)
|
|
{
|
|
/* USER CODE BEGIN UART7_MspInit 0 */
|
|
|
|
/* USER CODE END UART7_MspInit 0 */
|
|
|
|
/** Initializes the peripherals clock
|
|
*/
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_UART7;
|
|
PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/* UART7 clock enable */
|
|
__HAL_RCC_UART7_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOE_CLK_ENABLE();
|
|
/**UART7 GPIO Configuration
|
|
PE7 ------> UART7_RX
|
|
PE8 ------> UART7_TX
|
|
*/
|
|
GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.Alternate = GPIO_AF7_UART7;
|
|
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
|
|
|
/* UART7 DMA Init */
|
|
/* UART7_RX Init */
|
|
hdma_uart7_rx.Instance = DMA1_Stream7;
|
|
hdma_uart7_rx.Init.Request = DMA_REQUEST_UART7_RX;
|
|
hdma_uart7_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
hdma_uart7_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_uart7_rx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_uart7_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_uart7_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_uart7_rx.Init.Mode = DMA_NORMAL;
|
|
hdma_uart7_rx.Init.Priority = DMA_PRIORITY_LOW;
|
|
hdma_uart7_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_uart7_rx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmarx,hdma_uart7_rx);
|
|
|
|
/* UART7_TX Init */
|
|
hdma_uart7_tx.Instance = DMA2_Stream0;
|
|
hdma_uart7_tx.Init.Request = DMA_REQUEST_UART7_TX;
|
|
hdma_uart7_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
|
hdma_uart7_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_uart7_tx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_uart7_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_uart7_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_uart7_tx.Init.Mode = DMA_NORMAL;
|
|
hdma_uart7_tx.Init.Priority = DMA_PRIORITY_LOW;
|
|
hdma_uart7_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_uart7_tx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmatx,hdma_uart7_tx);
|
|
|
|
/* UART7 interrupt Init */
|
|
HAL_NVIC_SetPriority(UART7_IRQn, 5, 0);
|
|
HAL_NVIC_EnableIRQ(UART7_IRQn);
|
|
/* USER CODE BEGIN UART7_MspInit 1 */
|
|
|
|
/* USER CODE END UART7_MspInit 1 */
|
|
}
|
|
else if(uartHandle->Instance==USART1)
|
|
{
|
|
/* USER CODE BEGIN USART1_MspInit 0 */
|
|
|
|
/* USER CODE END USART1_MspInit 0 */
|
|
|
|
/** Initializes the peripherals clock
|
|
*/
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1;
|
|
PeriphClkInitStruct.Usart16ClockSelection = RCC_USART16910CLKSOURCE_D2PCLK2;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/* USART1 clock enable */
|
|
__HAL_RCC_USART1_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
/**USART1 GPIO Configuration
|
|
PA9 ------> USART1_TX
|
|
PA10 ------> USART1_RX
|
|
*/
|
|
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/* USART1 DMA Init */
|
|
/* USART1_TX Init */
|
|
hdma_usart1_tx.Instance = DMA1_Stream5;
|
|
hdma_usart1_tx.Init.Request = DMA_REQUEST_USART1_TX;
|
|
hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
|
hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_usart1_tx.Init.Mode = DMA_NORMAL;
|
|
hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW;
|
|
hdma_usart1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx);
|
|
|
|
/* USART1_RX Init */
|
|
hdma_usart1_rx.Instance = DMA1_Stream6;
|
|
hdma_usart1_rx.Init.Request = DMA_REQUEST_USART1_RX;
|
|
hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_usart1_rx.Init.Mode = DMA_NORMAL;
|
|
hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
|
|
hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart1_rx);
|
|
|
|
/* USART1 interrupt Init */
|
|
HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
|
|
HAL_NVIC_EnableIRQ(USART1_IRQn);
|
|
/* USER CODE BEGIN USART1_MspInit 1 */
|
|
|
|
/* USER CODE END USART1_MspInit 1 */
|
|
}
|
|
else if(uartHandle->Instance==USART2)
|
|
{
|
|
/* USER CODE BEGIN USART2_MspInit 0 */
|
|
|
|
/* USER CODE END USART2_MspInit 0 */
|
|
|
|
/** Initializes the peripherals clock
|
|
*/
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART2;
|
|
PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/* USART2 clock enable */
|
|
__HAL_RCC_USART2_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
/**USART2 GPIO Configuration
|
|
PD4 ------> USART2_DE
|
|
PD5 ------> USART2_TX
|
|
PD6 ------> USART2_RX
|
|
*/
|
|
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
/* USART2 DMA Init */
|
|
/* USART2_RX Init */
|
|
hdma_usart2_rx.Instance = DMA2_Stream1;
|
|
hdma_usart2_rx.Init.Request = DMA_REQUEST_USART2_RX;
|
|
hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_usart2_rx.Init.Mode = DMA_NORMAL;
|
|
hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
|
|
hdma_usart2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart2_rx);
|
|
|
|
/* USART2_TX Init */
|
|
hdma_usart2_tx.Instance = DMA2_Stream2;
|
|
hdma_usart2_tx.Init.Request = DMA_REQUEST_USART2_TX;
|
|
hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
|
hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_usart2_tx.Init.Mode = DMA_NORMAL;
|
|
hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW;
|
|
hdma_usart2_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_usart2_tx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart2_tx);
|
|
|
|
/* USART2 interrupt Init */
|
|
HAL_NVIC_SetPriority(USART2_IRQn, 5, 0);
|
|
HAL_NVIC_EnableIRQ(USART2_IRQn);
|
|
/* USER CODE BEGIN USART2_MspInit 1 */
|
|
|
|
/* USER CODE END USART2_MspInit 1 */
|
|
}
|
|
else if(uartHandle->Instance==USART3)
|
|
{
|
|
/* USER CODE BEGIN USART3_MspInit 0 */
|
|
|
|
/* USER CODE END USART3_MspInit 0 */
|
|
|
|
/** Initializes the peripherals clock
|
|
*/
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3;
|
|
PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/* USART3 clock enable */
|
|
__HAL_RCC_USART3_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
/**USART3 GPIO Configuration
|
|
PB14 ------> USART3_DE
|
|
PD8 ------> USART3_TX
|
|
PD9 ------> USART3_RX
|
|
*/
|
|
GPIO_InitStruct.Pin = GPIO_PIN_14;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
|
|
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
/* USART3 DMA Init */
|
|
/* USART3_TX Init */
|
|
hdma_usart3_tx.Instance = DMA1_Stream2;
|
|
hdma_usart3_tx.Init.Request = DMA_REQUEST_USART3_TX;
|
|
hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
|
hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_usart3_tx.Init.Mode = DMA_NORMAL;
|
|
hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
|
|
hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart3_tx);
|
|
|
|
/* USART3_RX Init */
|
|
hdma_usart3_rx.Instance = DMA1_Stream3;
|
|
hdma_usart3_rx.Init.Request = DMA_REQUEST_USART3_RX;
|
|
hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_usart3_rx.Init.Mode = DMA_NORMAL;
|
|
hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
|
|
hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx);
|
|
|
|
/* USART3 interrupt Init */
|
|
HAL_NVIC_SetPriority(USART3_IRQn, 5, 0);
|
|
HAL_NVIC_EnableIRQ(USART3_IRQn);
|
|
/* USER CODE BEGIN USART3_MspInit 1 */
|
|
|
|
/* USER CODE END USART3_MspInit 1 */
|
|
}
|
|
else if(uartHandle->Instance==USART10)
|
|
{
|
|
/* USER CODE BEGIN USART10_MspInit 0 */
|
|
|
|
/* USER CODE END USART10_MspInit 0 */
|
|
|
|
/** Initializes the peripherals clock
|
|
*/
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART10;
|
|
PeriphClkInitStruct.Usart16ClockSelection = RCC_USART16910CLKSOURCE_D2PCLK2;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/* USART10 clock enable */
|
|
__HAL_RCC_USART10_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOE_CLK_ENABLE();
|
|
/**USART10 GPIO Configuration
|
|
PE2 ------> USART10_RX
|
|
PE3 ------> USART10_TX
|
|
*/
|
|
GPIO_InitStruct.Pin = GPIO_PIN_2;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.Alternate = GPIO_AF4_USART10;
|
|
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_3;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.Alternate = GPIO_AF11_USART10;
|
|
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
|
|
|
|
/* USART10 DMA Init */
|
|
/* USART10_RX Init */
|
|
hdma_usart10_rx.Instance = DMA1_Stream1;
|
|
hdma_usart10_rx.Init.Request = DMA_REQUEST_USART10_RX;
|
|
hdma_usart10_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
hdma_usart10_rx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_usart10_rx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_usart10_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_usart10_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_usart10_rx.Init.Mode = DMA_NORMAL;
|
|
hdma_usart10_rx.Init.Priority = DMA_PRIORITY_MEDIUM;
|
|
hdma_usart10_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_usart10_rx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart10_rx);
|
|
|
|
/* USART10_TX Init */
|
|
hdma_usart10_tx.Instance = DMA1_Stream4;
|
|
hdma_usart10_tx.Init.Request = DMA_REQUEST_USART10_TX;
|
|
hdma_usart10_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
|
hdma_usart10_tx.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_usart10_tx.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_usart10_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
|
hdma_usart10_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
|
hdma_usart10_tx.Init.Mode = DMA_NORMAL;
|
|
hdma_usart10_tx.Init.Priority = DMA_PRIORITY_MEDIUM;
|
|
hdma_usart10_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
|
if (HAL_DMA_Init(&hdma_usart10_tx) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart10_tx);
|
|
|
|
/* USART10 interrupt Init */
|
|
HAL_NVIC_SetPriority(USART10_IRQn, 5, 0);
|
|
HAL_NVIC_EnableIRQ(USART10_IRQn);
|
|
/* USER CODE BEGIN USART10_MspInit 1 */
|
|
|
|
/* USER CODE END USART10_MspInit 1 */
|
|
}
|
|
}
|
|
|
|
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
|
|
{
|
|
|
|
if(uartHandle->Instance==UART5)
|
|
{
|
|
/* USER CODE BEGIN UART5_MspDeInit 0 */
|
|
|
|
/* USER CODE END UART5_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_UART5_CLK_DISABLE();
|
|
|
|
/**UART5 GPIO Configuration
|
|
PC12 ------> UART5_TX
|
|
PD2 ------> UART5_RX
|
|
*/
|
|
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_12);
|
|
|
|
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);
|
|
|
|
/* UART5 DMA DeInit */
|
|
HAL_DMA_DeInit(uartHandle->hdmarx);
|
|
|
|
/* UART5 interrupt Deinit */
|
|
HAL_NVIC_DisableIRQ(UART5_IRQn);
|
|
/* USER CODE BEGIN UART5_MspDeInit 1 */
|
|
|
|
/* USER CODE END UART5_MspDeInit 1 */
|
|
}
|
|
else if(uartHandle->Instance==UART7)
|
|
{
|
|
/* USER CODE BEGIN UART7_MspDeInit 0 */
|
|
|
|
/* USER CODE END UART7_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_UART7_CLK_DISABLE();
|
|
|
|
/**UART7 GPIO Configuration
|
|
PE7 ------> UART7_RX
|
|
PE8 ------> UART7_TX
|
|
*/
|
|
HAL_GPIO_DeInit(GPIOE, GPIO_PIN_7|GPIO_PIN_8);
|
|
|
|
/* UART7 DMA DeInit */
|
|
HAL_DMA_DeInit(uartHandle->hdmarx);
|
|
HAL_DMA_DeInit(uartHandle->hdmatx);
|
|
|
|
/* UART7 interrupt Deinit */
|
|
HAL_NVIC_DisableIRQ(UART7_IRQn);
|
|
/* USER CODE BEGIN UART7_MspDeInit 1 */
|
|
|
|
/* USER CODE END UART7_MspDeInit 1 */
|
|
}
|
|
else if(uartHandle->Instance==USART1)
|
|
{
|
|
/* USER CODE BEGIN USART1_MspDeInit 0 */
|
|
|
|
/* USER CODE END USART1_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_USART1_CLK_DISABLE();
|
|
|
|
/**USART1 GPIO Configuration
|
|
PA9 ------> USART1_TX
|
|
PA10 ------> USART1_RX
|
|
*/
|
|
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
|
|
|
|
/* USART1 DMA DeInit */
|
|
HAL_DMA_DeInit(uartHandle->hdmatx);
|
|
HAL_DMA_DeInit(uartHandle->hdmarx);
|
|
|
|
/* USART1 interrupt Deinit */
|
|
HAL_NVIC_DisableIRQ(USART1_IRQn);
|
|
/* USER CODE BEGIN USART1_MspDeInit 1 */
|
|
|
|
/* USER CODE END USART1_MspDeInit 1 */
|
|
}
|
|
else if(uartHandle->Instance==USART2)
|
|
{
|
|
/* USER CODE BEGIN USART2_MspDeInit 0 */
|
|
|
|
/* USER CODE END USART2_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_USART2_CLK_DISABLE();
|
|
|
|
/**USART2 GPIO Configuration
|
|
PD4 ------> USART2_DE
|
|
PD5 ------> USART2_TX
|
|
PD6 ------> USART2_RX
|
|
*/
|
|
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6);
|
|
|
|
/* USART2 DMA DeInit */
|
|
HAL_DMA_DeInit(uartHandle->hdmarx);
|
|
HAL_DMA_DeInit(uartHandle->hdmatx);
|
|
|
|
/* USART2 interrupt Deinit */
|
|
HAL_NVIC_DisableIRQ(USART2_IRQn);
|
|
/* USER CODE BEGIN USART2_MspDeInit 1 */
|
|
|
|
/* USER CODE END USART2_MspDeInit 1 */
|
|
}
|
|
else if(uartHandle->Instance==USART3)
|
|
{
|
|
/* USER CODE BEGIN USART3_MspDeInit 0 */
|
|
|
|
/* USER CODE END USART3_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_USART3_CLK_DISABLE();
|
|
|
|
/**USART3 GPIO Configuration
|
|
PB14 ------> USART3_DE
|
|
PD8 ------> USART3_TX
|
|
PD9 ------> USART3_RX
|
|
*/
|
|
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_14);
|
|
|
|
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8|GPIO_PIN_9);
|
|
|
|
/* USART3 DMA DeInit */
|
|
HAL_DMA_DeInit(uartHandle->hdmatx);
|
|
HAL_DMA_DeInit(uartHandle->hdmarx);
|
|
|
|
/* USART3 interrupt Deinit */
|
|
HAL_NVIC_DisableIRQ(USART3_IRQn);
|
|
/* USER CODE BEGIN USART3_MspDeInit 1 */
|
|
|
|
/* USER CODE END USART3_MspDeInit 1 */
|
|
}
|
|
else if(uartHandle->Instance==USART10)
|
|
{
|
|
/* USER CODE BEGIN USART10_MspDeInit 0 */
|
|
|
|
/* USER CODE END USART10_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_USART10_CLK_DISABLE();
|
|
|
|
/**USART10 GPIO Configuration
|
|
PE2 ------> USART10_RX
|
|
PE3 ------> USART10_TX
|
|
*/
|
|
HAL_GPIO_DeInit(GPIOE, GPIO_PIN_2|GPIO_PIN_3);
|
|
|
|
/* USART10 DMA DeInit */
|
|
HAL_DMA_DeInit(uartHandle->hdmarx);
|
|
HAL_DMA_DeInit(uartHandle->hdmatx);
|
|
|
|
/* USART10 interrupt Deinit */
|
|
HAL_NVIC_DisableIRQ(USART10_IRQn);
|
|
/* USER CODE BEGIN USART10_MspDeInit 1 */
|
|
|
|
/* USER CODE END USART10_MspDeInit 1 */
|
|
}
|
|
}
|
|
|
|
/* USER CODE BEGIN 1 */
|
|
|
|
/* USER CODE END 1 */
|