1 | /* USER CODE BEGIN Header */ |
---|
2 | /** |
---|
3 | ****************************************************************************** |
---|
4 | * @file usart.c |
---|
5 | * @brief This file provides code for the configuration |
---|
6 | * of the USART instances. |
---|
7 | ****************************************************************************** |
---|
8 | * @attention |
---|
9 | * |
---|
10 | * Copyright (c) 2025 STMicroelectronics. |
---|
11 | * All rights reserved. |
---|
12 | * |
---|
13 | * This software is licensed under terms that can be found in the LICENSE file |
---|
14 | * in the root directory of this software component. |
---|
15 | * If no LICENSE file comes with this software, it is provided AS-IS. |
---|
16 | * |
---|
17 | ****************************************************************************** |
---|
18 | */ |
---|
19 | /* USER CODE END Header */ |
---|
20 | /* Includes ------------------------------------------------------------------*/ |
---|
21 | #include "usart.h" |
---|
22 | |
---|
23 | /* USER CODE BEGIN 0 */ |
---|
24 | |
---|
25 | /* USER CODE END 0 */ |
---|
26 | |
---|
27 | UART_HandleTypeDef huart3; |
---|
28 | DMA_HandleTypeDef hdma_usart3_rx; |
---|
29 | DMA_HandleTypeDef hdma_usart3_tx; |
---|
30 | |
---|
31 | /* USART3 init function */ |
---|
32 | |
---|
33 | void MX_USART3_UART_Init(void) |
---|
34 | { |
---|
35 | |
---|
36 | /* USER CODE BEGIN USART3_Init 0 */ |
---|
37 | |
---|
38 | /* USER CODE END USART3_Init 0 */ |
---|
39 | |
---|
40 | /* USER CODE BEGIN USART3_Init 1 */ |
---|
41 | |
---|
42 | /* USER CODE END USART3_Init 1 */ |
---|
43 | huart3.Instance = USART3; |
---|
44 | huart3.Init.BaudRate = 115200; |
---|
45 | huart3.Init.WordLength = UART_WORDLENGTH_8B; |
---|
46 | huart3.Init.StopBits = UART_STOPBITS_1; |
---|
47 | huart3.Init.Parity = UART_PARITY_NONE; |
---|
48 | huart3.Init.Mode = UART_MODE_TX_RX; |
---|
49 | huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
---|
50 | huart3.Init.OverSampling = UART_OVERSAMPLING_16; |
---|
51 | huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; |
---|
52 | huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1; |
---|
53 | huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_SWAP_INIT|UART_ADVFEATURE_DMADISABLEONERROR_INIT; |
---|
54 | huart3.AdvancedInit.Swap = UART_ADVFEATURE_SWAP_ENABLE; |
---|
55 | huart3.AdvancedInit.DMADisableonRxError = UART_ADVFEATURE_DMA_DISABLEONRXERROR; |
---|
56 | if (HAL_UART_Init(&huart3) != HAL_OK) |
---|
57 | { |
---|
58 | Error_Handler(); |
---|
59 | } |
---|
60 | if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) |
---|
61 | { |
---|
62 | Error_Handler(); |
---|
63 | } |
---|
64 | if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) |
---|
65 | { |
---|
66 | Error_Handler(); |
---|
67 | } |
---|
68 | if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK) |
---|
69 | { |
---|
70 | Error_Handler(); |
---|
71 | } |
---|
72 | /* USER CODE BEGIN USART3_Init 2 */ |
---|
73 | |
---|
74 | /* USER CODE END USART3_Init 2 */ |
---|
75 | |
---|
76 | } |
---|
77 | |
---|
78 | void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) |
---|
79 | { |
---|
80 | |
---|
81 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
---|
82 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
---|
83 | if(uartHandle->Instance==USART3) |
---|
84 | { |
---|
85 | /* USER CODE BEGIN USART3_MspInit 0 */ |
---|
86 | |
---|
87 | /* USER CODE END USART3_MspInit 0 */ |
---|
88 | |
---|
89 | /** Initializes the peripherals clock |
---|
90 | */ |
---|
91 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART3; |
---|
92 | PeriphClkInitStruct.PLL3.PLL3M = 25; |
---|
93 | PeriphClkInitStruct.PLL3.PLL3N = 200; |
---|
94 | PeriphClkInitStruct.PLL3.PLL3P = 2; |
---|
95 | PeriphClkInitStruct.PLL3.PLL3Q = 8; |
---|
96 | PeriphClkInitStruct.PLL3.PLL3R = 2; |
---|
97 | PeriphClkInitStruct.PLL3.PLL3RGE = RCC_PLL3VCIRANGE_0; |
---|
98 | PeriphClkInitStruct.PLL3.PLL3VCOSEL = RCC_PLL3VCOMEDIUM; |
---|
99 | PeriphClkInitStruct.PLL3.PLL3FRACN = 0; |
---|
100 | PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_PLL3; |
---|
101 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) |
---|
102 | { |
---|
103 | Error_Handler(); |
---|
104 | } |
---|
105 | |
---|
106 | /* USART3 clock enable */ |
---|
107 | __HAL_RCC_USART3_CLK_ENABLE(); |
---|
108 | |
---|
109 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
---|
110 | /**USART3 GPIO Configuration |
---|
111 | PD8 ------> USART3_TX |
---|
112 | PD9 ------> USART3_RX |
---|
113 | PD11 ------> USART3_CTS |
---|
114 | PD12 ------> USART3_RTS |
---|
115 | */ |
---|
116 | GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_11|GPIO_PIN_12; |
---|
117 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
---|
118 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
---|
119 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
---|
120 | GPIO_InitStruct.Alternate = GPIO_AF7_USART3; |
---|
121 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); |
---|
122 | |
---|
123 | /* USART3 DMA Init */ |
---|
124 | /* USART3_RX Init */ |
---|
125 | hdma_usart3_rx.Instance = DMA1_Stream1; |
---|
126 | hdma_usart3_rx.Init.Request = DMA_REQUEST_USART3_RX; |
---|
127 | hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; |
---|
128 | hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE; |
---|
129 | hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE; |
---|
130 | hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; |
---|
131 | hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; |
---|
132 | hdma_usart3_rx.Init.Mode = DMA_CIRCULAR; |
---|
133 | hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW; |
---|
134 | hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; |
---|
135 | if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK) |
---|
136 | { |
---|
137 | Error_Handler(); |
---|
138 | } |
---|
139 | |
---|
140 | __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx); |
---|
141 | |
---|
142 | /* USART3_TX Init */ |
---|
143 | hdma_usart3_tx.Instance = DMA1_Stream2; |
---|
144 | hdma_usart3_tx.Init.Request = DMA_REQUEST_USART3_TX; |
---|
145 | hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; |
---|
146 | hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE; |
---|
147 | hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE; |
---|
148 | hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; |
---|
149 | hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; |
---|
150 | hdma_usart3_tx.Init.Mode = DMA_NORMAL; |
---|
151 | hdma_usart3_tx.Init.Priority = DMA_PRIORITY_HIGH; |
---|
152 | hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; |
---|
153 | if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK) |
---|
154 | { |
---|
155 | Error_Handler(); |
---|
156 | } |
---|
157 | |
---|
158 | __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart3_tx); |
---|
159 | |
---|
160 | /* USART3 interrupt Init */ |
---|
161 | HAL_NVIC_SetPriority(USART3_IRQn, 0, 0); |
---|
162 | HAL_NVIC_EnableIRQ(USART3_IRQn); |
---|
163 | /* USER CODE BEGIN USART3_MspInit 1 */ |
---|
164 | |
---|
165 | /* USER CODE END USART3_MspInit 1 */ |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) |
---|
170 | { |
---|
171 | |
---|
172 | if(uartHandle->Instance==USART3) |
---|
173 | { |
---|
174 | /* USER CODE BEGIN USART3_MspDeInit 0 */ |
---|
175 | |
---|
176 | /* USER CODE END USART3_MspDeInit 0 */ |
---|
177 | /* Peripheral clock disable */ |
---|
178 | __HAL_RCC_USART3_CLK_DISABLE(); |
---|
179 | |
---|
180 | /**USART3 GPIO Configuration |
---|
181 | PD8 ------> USART3_TX |
---|
182 | PD9 ------> USART3_RX |
---|
183 | PD11 ------> USART3_CTS |
---|
184 | PD12 ------> USART3_RTS |
---|
185 | */ |
---|
186 | HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_11|GPIO_PIN_12); |
---|
187 | |
---|
188 | /* USART3 DMA DeInit */ |
---|
189 | HAL_DMA_DeInit(uartHandle->hdmarx); |
---|
190 | HAL_DMA_DeInit(uartHandle->hdmatx); |
---|
191 | |
---|
192 | /* USART3 interrupt Deinit */ |
---|
193 | HAL_NVIC_DisableIRQ(USART3_IRQn); |
---|
194 | /* USER CODE BEGIN USART3_MspDeInit 1 */ |
---|
195 | |
---|
196 | /* USER CODE END USART3_MspDeInit 1 */ |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | /* USER CODE BEGIN 1 */ |
---|
201 | |
---|
202 | /* USER CODE END 1 */ |
---|