38
|
1 /**
|
|
2 ******************************************************************************
|
|
3 * @file stm32f4xx_hal_uart.c
|
|
4 * @author MCD Application Team
|
|
5 * @version V1.2.0
|
|
6 * @date 26-December-2014
|
|
7 * @brief UART HAL module driver.
|
|
8 * This file provides firmware functions to manage the following
|
|
9 * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
|
|
10 * + Initialization and de-initialization functions
|
|
11 * + IO operation functions
|
|
12 * + Peripheral Control functions
|
|
13 * + Peripheral State and Errors functions
|
|
14 *
|
|
15 @verbatim
|
|
16 ==============================================================================
|
|
17 ##### How to use this driver #####
|
|
18 ==============================================================================
|
|
19 [..]
|
|
20 The UART HAL driver can be used as follows:
|
|
21
|
|
22 (#) Declare a UART_HandleTypeDef handle structure.
|
|
23
|
|
24 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
|
|
25 (##) Enable the USARTx interface clock.
|
|
26 (##) UART pins configuration:
|
|
27 (+++) Enable the clock for the UART GPIOs.
|
|
28 (+++) Configure these UART pins as alternate function pull-up.
|
|
29 (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
|
|
30 and HAL_UART_Receive_IT() APIs):
|
|
31 (+++) Configure the USARTx interrupt priority.
|
|
32 (+++) Enable the NVIC USART IRQ handle.
|
|
33 (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
|
|
34 and HAL_UART_Receive_DMA() APIs):
|
|
35 (+++) Declare a DMA handle structure for the Tx/Rx stream.
|
|
36 (+++) Enable the DMAx interface clock.
|
|
37 (+++) Configure the declared DMA handle structure with the required
|
|
38 Tx/Rx parameters.
|
|
39 (+++) Configure the DMA Tx/Rx Stream.
|
|
40 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
|
|
41 (+++) Configure the priority and enable the NVIC for the transfer complete
|
|
42 interrupt on the DMA Tx/Rx Stream.
|
|
43
|
|
44 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
|
|
45 flow control and Mode(Receiver/Transmitter) in the Init structure.
|
|
46
|
|
47 (#) For the UART asynchronous mode, initialize the UART registers by calling
|
|
48 the HAL_UART_Init() API.
|
|
49
|
|
50 (#) For the UART Half duplex mode, initialize the UART registers by calling
|
|
51 the HAL_HalfDuplex_Init() API.
|
|
52
|
|
53 (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
|
|
54
|
|
55 (#) For the Multi-Processor mode, initialize the UART registers by calling
|
|
56 the HAL_MultiProcessor_Init() API.
|
|
57
|
|
58 [..]
|
|
59 (@) The specific UART interrupts (Transmission complete interrupt,
|
|
60 RXNE interrupt and Error Interrupts) will be managed using the macros
|
|
61 __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit
|
|
62 and receive process.
|
|
63
|
|
64 [..]
|
|
65 (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the
|
|
66 low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customized
|
|
67 HAL_UART_MspInit() API.
|
|
68
|
|
69 [..]
|
|
70 Three operation modes are available within this driver :
|
|
71
|
|
72 *** Polling mode IO operation ***
|
|
73 =================================
|
|
74 [..]
|
|
75 (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
|
|
76 (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
|
|
77
|
|
78 *** Interrupt mode IO operation ***
|
|
79 ===================================
|
|
80 [..]
|
|
81 (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
|
|
82 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
|
|
83 add his own code by customization of function pointer HAL_UART_TxCpltCallback
|
|
84 (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
|
|
85 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
|
|
86 add his own code by customization of function pointer HAL_UART_RxCpltCallback
|
|
87 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
|
|
88 add his own code by customization of function pointer HAL_UART_ErrorCallback
|
|
89
|
|
90 *** DMA mode IO operation ***
|
|
91 ==============================
|
|
92 [..]
|
|
93 (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
|
|
94 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
|
|
95 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
|
|
96 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
|
|
97 add his own code by customization of function pointer HAL_UART_TxCpltCallback
|
|
98 (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
|
|
99 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
|
|
100 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
|
|
101 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
|
|
102 add his own code by customization of function pointer HAL_UART_RxCpltCallback
|
|
103 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
|
|
104 add his own code by customization of function pointer HAL_UART_ErrorCallback
|
|
105 (+) Pause the DMA Transfer using HAL_UART_DMAPause()
|
|
106 (+) Resume the DMA Transfer using HAL_UART_DMAResume()
|
|
107 (+) Stop the DMA Transfer using HAL_UART_DMAStop()
|
|
108
|
|
109 *** UART HAL driver macros list ***
|
|
110 =============================================
|
|
111 [..]
|
|
112 Below the list of most used macros in UART HAL driver.
|
|
113
|
|
114 (+) __HAL_UART_ENABLE: Enable the UART peripheral
|
|
115 (+) __HAL_UART_DISABLE: Disable the UART peripheral
|
|
116 (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
|
|
117 (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
|
|
118 (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
|
|
119 (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
|
|
120 (+) __HAL_UART_GET_IT_SOURCE: Check whether the specified UART interrupt has occurred or not
|
|
121
|
|
122 [..]
|
|
123 (@) You can refer to the UART HAL driver header file for more useful macros
|
|
124
|
|
125 @endverbatim
|
|
126 ******************************************************************************
|
|
127 * @attention
|
|
128 *
|
|
129 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
|
|
130 *
|
|
131 * Redistribution and use in source and binary forms, with or without modification,
|
|
132 * are permitted provided that the following conditions are met:
|
|
133 * 1. Redistributions of source code must retain the above copyright notice,
|
|
134 * this list of conditions and the following disclaimer.
|
|
135 * 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
136 * this list of conditions and the following disclaimer in the documentation
|
|
137 * and/or other materials provided with the distribution.
|
|
138 * 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
139 * may be used to endorse or promote products derived from this software
|
|
140 * without specific prior written permission.
|
|
141 *
|
|
142 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
143 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
144 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
145 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
146 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
147 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
148 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
149 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
150 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
151 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
152 *
|
|
153 ******************************************************************************
|
|
154 */
|
|
155
|
|
156 /* Includes ------------------------------------------------------------------*/
|
|
157 #include "stm32f4xx_hal.h"
|
|
158
|
|
159 /** @addtogroup STM32F4xx_HAL_Driver
|
|
160 * @{
|
|
161 */
|
|
162
|
|
163 /** @defgroup UART UART
|
|
164 * @brief HAL UART module driver
|
|
165 * @{
|
|
166 */
|
|
167 #ifdef HAL_UART_MODULE_ENABLED
|
|
168
|
|
169 /* Private typedef -----------------------------------------------------------*/
|
|
170 /* Private define ------------------------------------------------------------*/
|
|
171 /** @addtogroup UART_Private_Constants
|
|
172 * @{
|
|
173 */
|
|
174 #define UART_TIMEOUT_VALUE 22000
|
|
175 /**
|
|
176 * @}
|
|
177 */
|
|
178 /* Private macro -------------------------------------------------------------*/
|
|
179 /* Private variables ---------------------------------------------------------*/
|
|
180 /* Private function prototypes -----------------------------------------------*/
|
|
181 /** @addtogroup UART_Private_Functions UART Private Functions
|
|
182 * @{
|
|
183 */
|
|
184 static void UART_SetConfig (UART_HandleTypeDef *huart);
|
|
185 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
|
|
186 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
|
|
187 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
|
|
188 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
|
|
189 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
|
|
190 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
|
|
191 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
|
|
192 static void UART_DMAError(DMA_HandleTypeDef *hdma);
|
|
193 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
|
|
194 /**
|
|
195 * @}
|
|
196 */
|
|
197
|
|
198 /* Exported functions ---------------------------------------------------------*/
|
|
199 /** @defgroup UART_Exported_Functions UART Exported Functions
|
|
200 * @{
|
|
201 */
|
|
202
|
|
203 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
|
|
204 * @brief Initialization and Configuration functions
|
|
205 *
|
|
206 @verbatim
|
|
207 ===============================================================================
|
|
208 ##### Initialization and Configuration functions #####
|
|
209 ===============================================================================
|
|
210 [..]
|
|
211 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
|
|
212 in asynchronous mode.
|
|
213 (+) For the asynchronous mode only these parameters can be configured:
|
|
214 (++) Baud Rate
|
|
215 (++) Word Length
|
|
216 (++) Stop Bit
|
|
217 (++) Parity: If the parity is enabled, then the MSB bit of the data written
|
|
218 in the data register is transmitted but is changed by the parity bit.
|
|
219 Depending on the frame length defined by the M bit (8-bits or 9-bits),
|
|
220 please refer to Reference manual for possible UART frame formats.
|
|
221 (++) Hardware flow control
|
|
222 (++) Receiver/transmitter modes
|
|
223 (++) Over Sampling Method
|
|
224 [..]
|
|
225 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
|
|
226 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor
|
|
227 configuration procedures (details for the procedures are available in reference manual (RM0329)).
|
|
228
|
|
229 @endverbatim
|
|
230 * @{
|
|
231 */
|
|
232
|
|
233 /**
|
|
234 * @brief Initializes the UART mode according to the specified parameters in
|
|
235 * the UART_InitTypeDef and create the associated handle.
|
|
236 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
237 * the configuration information for the specified UART module.
|
|
238 * @retval HAL status
|
|
239 */
|
|
240 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
|
|
241 {
|
|
242 /* Check the UART handle allocation */
|
|
243 if(huart == NULL)
|
|
244 {
|
|
245 return HAL_ERROR;
|
|
246 }
|
|
247
|
|
248 /* Check the parameters */
|
|
249 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
|
|
250 {
|
|
251 /* The hardware flow control is available only for USART1, USART2, USART3 and USART6 */
|
|
252 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
|
|
253 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
|
|
254 }
|
|
255 else
|
|
256 {
|
|
257 assert_param(IS_UART_INSTANCE(huart->Instance));
|
|
258 }
|
|
259 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
|
|
260 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
|
|
261
|
|
262 if(huart->State == HAL_UART_STATE_RESET)
|
|
263 {
|
|
264 /* Init the low level hardware */
|
|
265 HAL_UART_MspInit(huart);
|
|
266 }
|
|
267
|
|
268 huart->State = HAL_UART_STATE_BUSY;
|
|
269
|
|
270 /* Disable the peripheral */
|
|
271 __HAL_UART_DISABLE(huart);
|
|
272
|
|
273 /* Set the UART Communication parameters */
|
|
274 UART_SetConfig(huart);
|
|
275
|
|
276 /* In asynchronous mode, the following bits must be kept cleared:
|
|
277 - LINEN and CLKEN bits in the USART_CR2 register,
|
|
278 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
|
|
279 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
|
|
280 huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
|
|
281
|
|
282 /* Enable the peripheral */
|
|
283 __HAL_UART_ENABLE(huart);
|
|
284
|
|
285 /* Initialize the UART state */
|
|
286 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
287 huart->State= HAL_UART_STATE_READY;
|
|
288
|
|
289 return HAL_OK;
|
|
290 }
|
|
291
|
|
292 /**
|
|
293 * @brief Initializes the half-duplex mode according to the specified
|
|
294 * parameters in the UART_InitTypeDef and create the associated handle.
|
|
295 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
296 * the configuration information for the specified UART module.
|
|
297 * @retval HAL status
|
|
298 */
|
|
299 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
|
|
300 {
|
|
301 /* Check the UART handle allocation */
|
|
302 if(huart == NULL)
|
|
303 {
|
|
304 return HAL_ERROR;
|
|
305 }
|
|
306
|
|
307 /* Check the parameters */
|
|
308 assert_param(IS_UART_INSTANCE(huart->Instance));
|
|
309 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
|
|
310 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
|
|
311
|
|
312 if(huart->State == HAL_UART_STATE_RESET)
|
|
313 {
|
|
314 /* Init the low level hardware */
|
|
315 HAL_UART_MspInit(huart);
|
|
316 }
|
|
317
|
|
318 huart->State = HAL_UART_STATE_BUSY;
|
|
319
|
|
320 /* Disable the peripheral */
|
|
321 __HAL_UART_DISABLE(huart);
|
|
322
|
|
323 /* Set the UART Communication parameters */
|
|
324 UART_SetConfig(huart);
|
|
325
|
|
326 /* In half-duplex mode, the following bits must be kept cleared:
|
|
327 - LINEN and CLKEN bits in the USART_CR2 register,
|
|
328 - SCEN and IREN bits in the USART_CR3 register.*/
|
|
329 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
|
|
330 huart->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_SCEN);
|
|
331
|
|
332 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
|
|
333 huart->Instance->CR3 |= USART_CR3_HDSEL;
|
|
334
|
|
335 /* Enable the peripheral */
|
|
336 __HAL_UART_ENABLE(huart);
|
|
337
|
|
338 /* Initialize the UART state*/
|
|
339 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
340 huart->State= HAL_UART_STATE_READY;
|
|
341
|
|
342 return HAL_OK;
|
|
343 }
|
|
344
|
|
345 /**
|
|
346 * @brief Initializes the LIN mode according to the specified
|
|
347 * parameters in the UART_InitTypeDef and create the associated handle.
|
|
348 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
349 * the configuration information for the specified UART module.
|
|
350 * @param BreakDetectLength: Specifies the LIN break detection length.
|
|
351 * This parameter can be one of the following values:
|
|
352 * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
|
|
353 * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
|
|
354 * @retval HAL status
|
|
355 */
|
|
356 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
|
|
357 {
|
|
358 /* Check the UART handle allocation */
|
|
359 if(huart == NULL)
|
|
360 {
|
|
361 return HAL_ERROR;
|
|
362 }
|
|
363
|
|
364 /* Check the parameters */
|
|
365 assert_param(IS_UART_INSTANCE(huart->Instance));
|
|
366 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
|
|
367 assert_param(IS_UART_LIN_WORD_LENGTH(huart->Init.WordLength));
|
|
368 assert_param(IS_UART_LIN_OVERSAMPLING(huart->Init.OverSampling));
|
|
369
|
|
370 if(huart->State == HAL_UART_STATE_RESET)
|
|
371 {
|
|
372 /* Init the low level hardware */
|
|
373 HAL_UART_MspInit(huart);
|
|
374 }
|
|
375
|
|
376 huart->State = HAL_UART_STATE_BUSY;
|
|
377
|
|
378 /* Disable the peripheral */
|
|
379 __HAL_UART_DISABLE(huart);
|
|
380
|
|
381 /* Set the UART Communication parameters */
|
|
382 UART_SetConfig(huart);
|
|
383
|
|
384 /* In LIN mode, the following bits must be kept cleared:
|
|
385 - LINEN and CLKEN bits in the USART_CR2 register,
|
|
386 - SCEN and IREN bits in the USART_CR3 register.*/
|
|
387 huart->Instance->CR2 &= ~(USART_CR2_CLKEN);
|
|
388 huart->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN);
|
|
389
|
|
390 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
|
|
391 huart->Instance->CR2 |= USART_CR2_LINEN;
|
|
392
|
|
393 /* Set the USART LIN Break detection length. */
|
|
394 huart->Instance->CR2 &= ~(USART_CR2_LBDL);
|
|
395 huart->Instance->CR2 |= BreakDetectLength;
|
|
396
|
|
397 /* Enable the peripheral */
|
|
398 __HAL_UART_ENABLE(huart);
|
|
399
|
|
400 /* Initialize the UART state*/
|
|
401 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
402 huart->State= HAL_UART_STATE_READY;
|
|
403
|
|
404 return HAL_OK;
|
|
405 }
|
|
406
|
|
407 /**
|
|
408 * @brief Initializes the Multi-Processor mode according to the specified
|
|
409 * parameters in the UART_InitTypeDef and create the associated handle.
|
|
410 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
411 * the configuration information for the specified UART module.
|
|
412 * @param Address: USART address
|
|
413 * @param WakeUpMethod: specifies the USART wake-up method.
|
|
414 * This parameter can be one of the following values:
|
|
415 * @arg UART_WAKEUPMETHOD_IDLELINE: Wake-up by an idle line detection
|
|
416 * @arg UART_WAKEUPMETHOD_ADDRESSMARK: Wake-up by an address mark
|
|
417 * @retval HAL status
|
|
418 */
|
|
419 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
|
|
420 {
|
|
421 /* Check the UART handle allocation */
|
|
422 if(huart == NULL)
|
|
423 {
|
|
424 return HAL_ERROR;
|
|
425 }
|
|
426
|
|
427 /* Check the parameters */
|
|
428 assert_param(IS_UART_INSTANCE(huart->Instance));
|
|
429 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
|
|
430 assert_param(IS_UART_ADDRESS(Address));
|
|
431 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
|
|
432 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
|
|
433
|
|
434 if(huart->State == HAL_UART_STATE_RESET)
|
|
435 {
|
|
436 /* Init the low level hardware */
|
|
437 HAL_UART_MspInit(huart);
|
|
438 }
|
|
439
|
|
440 huart->State = HAL_UART_STATE_BUSY;
|
|
441
|
|
442 /* Disable the peripheral */
|
|
443 __HAL_UART_DISABLE(huart);
|
|
444
|
|
445 /* Set the UART Communication parameters */
|
|
446 UART_SetConfig(huart);
|
|
447
|
|
448 /* In Multi-Processor mode, the following bits must be kept cleared:
|
|
449 - LINEN and CLKEN bits in the USART_CR2 register,
|
|
450 - SCEN, HDSEL and IREN bits in the USART_CR3 register */
|
|
451 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
|
|
452 huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
|
|
453
|
|
454 /* Clear the USART address */
|
|
455 huart->Instance->CR2 &= ~(USART_CR2_ADD);
|
|
456 /* Set the USART address node */
|
|
457 huart->Instance->CR2 |= Address;
|
|
458
|
|
459 /* Set the wake up method by setting the WAKE bit in the CR1 register */
|
|
460 huart->Instance->CR1 &= ~(USART_CR1_WAKE);
|
|
461 huart->Instance->CR1 |= WakeUpMethod;
|
|
462
|
|
463 /* Enable the peripheral */
|
|
464 __HAL_UART_ENABLE(huart);
|
|
465
|
|
466 /* Initialize the UART state */
|
|
467 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
468 huart->State= HAL_UART_STATE_READY;
|
|
469
|
|
470 return HAL_OK;
|
|
471 }
|
|
472
|
|
473 /**
|
|
474 * @brief DeInitializes the UART peripheral.
|
|
475 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
476 * the configuration information for the specified UART module.
|
|
477 * @retval HAL status
|
|
478 */
|
|
479 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
|
|
480 {
|
|
481 /* Check the UART handle allocation */
|
|
482 if(huart == NULL)
|
|
483 {
|
|
484 return HAL_ERROR;
|
|
485 }
|
|
486
|
|
487 /* Check the parameters */
|
|
488 assert_param(IS_UART_INSTANCE(huart->Instance));
|
|
489
|
|
490 huart->State = HAL_UART_STATE_BUSY;
|
|
491
|
|
492 /* DeInit the low level hardware */
|
|
493 HAL_UART_MspDeInit(huart);
|
|
494
|
|
495 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
496 huart->State = HAL_UART_STATE_RESET;
|
|
497
|
|
498 /* Process Lock */
|
|
499 __HAL_UNLOCK(huart);
|
|
500
|
|
501 return HAL_OK;
|
|
502 }
|
|
503
|
|
504 /**
|
|
505 * @brief UART MSP Init.
|
|
506 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
507 * the configuration information for the specified UART module.
|
|
508 * @retval None
|
|
509 */
|
|
510 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
|
|
511 {
|
|
512 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
513 the HAL_UART_MspInit could be implemented in the user file
|
|
514 */
|
|
515 }
|
|
516
|
|
517 /**
|
|
518 * @brief UART MSP DeInit.
|
|
519 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
520 * the configuration information for the specified UART module.
|
|
521 * @retval None
|
|
522 */
|
|
523 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
|
|
524 {
|
|
525 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
526 the HAL_UART_MspDeInit could be implemented in the user file
|
|
527 */
|
|
528 }
|
|
529
|
|
530 /**
|
|
531 * @}
|
|
532 */
|
|
533
|
|
534 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
|
|
535 * @brief UART Transmit and Receive functions
|
|
536 *
|
|
537 @verbatim
|
|
538 ==============================================================================
|
|
539 ##### IO operation functions #####
|
|
540 ==============================================================================
|
|
541 [..]
|
|
542 This subsection provides a set of functions allowing to manage the UART asynchronous
|
|
543 and Half duplex data transfers.
|
|
544
|
|
545 (#) There are two modes of transfer:
|
|
546 (++) Blocking mode: The communication is performed in polling mode.
|
|
547 The HAL status of all data processing is returned by the same function
|
|
548 after finishing transfer.
|
|
549 (++) Non blocking mode: The communication is performed using Interrupts
|
|
550 or DMA, these APIs return the HAL status.
|
|
551 The end of the data processing will be indicated through the
|
|
552 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
|
|
553 using DMA mode.
|
|
554 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
|
|
555 will be executed respectively at the end of the transmit or receive process.
|
|
556 The HAL_UART_ErrorCallback() user callback will be executed when
|
|
557 a communication error is detected.
|
|
558
|
|
559 (#) Blocking mode APIs are:
|
|
560 (++) HAL_UART_Transmit()
|
|
561 (++) HAL_UART_Receive()
|
|
562
|
|
563 (#) Non Blocking mode APIs with Interrupt are:
|
|
564 (++) HAL_UART_Transmit_IT()
|
|
565 (++) HAL_UART_Receive_IT()
|
|
566 (++) HAL_UART_IRQHandler()
|
|
567
|
|
568 (#) Non Blocking mode functions with DMA are:
|
|
569 (++) HAL_UART_Transmit_DMA()
|
|
570 (++) HAL_UART_Receive_DMA()
|
|
571
|
|
572 (#) A set of Transfer Complete Callbacks are provided in non blocking mode:
|
|
573 (++) HAL_UART_TxCpltCallback()
|
|
574 (++) HAL_UART_RxCpltCallback()
|
|
575 (++) HAL_UART_ErrorCallback()
|
|
576
|
|
577 [..]
|
|
578 (@) In the Half duplex communication, it is forbidden to run the transmit
|
|
579 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX
|
|
580 can't be useful.
|
|
581
|
|
582 @endverbatim
|
|
583 * @{
|
|
584 */
|
|
585
|
|
586 /**
|
|
587 * @brief Sends an amount of data in blocking mode.
|
|
588 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
589 * the configuration information for the specified UART module.
|
|
590 * @param pData: Pointer to data buffer
|
|
591 * @param Size: Amount of data to be sent
|
|
592 * @param Timeout: Timeout duration
|
|
593 * @retval HAL status
|
|
594 */
|
|
595 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
|
|
596 {
|
|
597 uint16_t* tmp;
|
|
598 uint32_t tmp1 = 0;
|
|
599
|
|
600 tmp1 = huart->State;
|
|
601 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_RX))
|
|
602 {
|
|
603 if((pData == NULL ) || (Size == 0))
|
|
604 {
|
|
605 return HAL_ERROR;
|
|
606 }
|
|
607
|
|
608 /* Process Locked */
|
|
609 __HAL_LOCK(huart);
|
|
610
|
|
611 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
612 /* Check if a non-blocking receive process is ongoing or not */
|
|
613 if(huart->State == HAL_UART_STATE_BUSY_RX)
|
|
614 {
|
|
615 huart->State = HAL_UART_STATE_BUSY_TX_RX;
|
|
616 }
|
|
617 else
|
|
618 {
|
|
619 huart->State = HAL_UART_STATE_BUSY_TX;
|
|
620 }
|
|
621
|
|
622 huart->TxXferSize = Size;
|
|
623 huart->TxXferCount = Size;
|
|
624 while(huart->TxXferCount > 0)
|
|
625 {
|
|
626 huart->TxXferCount--;
|
|
627 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
|
|
628 {
|
|
629 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
|
|
630 {
|
|
631 return HAL_TIMEOUT;
|
|
632 }
|
|
633 tmp = (uint16_t*) pData;
|
|
634 huart->Instance->DR = (*tmp & (uint16_t)0x01FF);
|
|
635 if(huart->Init.Parity == UART_PARITY_NONE)
|
|
636 {
|
|
637 pData +=2;
|
|
638 }
|
|
639 else
|
|
640 {
|
|
641 pData +=1;
|
|
642 }
|
|
643 }
|
|
644 else
|
|
645 {
|
|
646 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
|
|
647 {
|
|
648 return HAL_TIMEOUT;
|
|
649 }
|
|
650 huart->Instance->DR = (*pData++ & (uint8_t)0xFF);
|
|
651 }
|
|
652 }
|
|
653
|
|
654 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, Timeout) != HAL_OK)
|
|
655 {
|
|
656 return HAL_TIMEOUT;
|
|
657 }
|
|
658
|
|
659 /* Check if a non-blocking receive process is ongoing or not */
|
|
660 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
|
|
661 {
|
|
662 huart->State = HAL_UART_STATE_BUSY_RX;
|
|
663 }
|
|
664 else
|
|
665 {
|
|
666 huart->State = HAL_UART_STATE_READY;
|
|
667 }
|
|
668
|
|
669 /* Process Unlocked */
|
|
670 __HAL_UNLOCK(huart);
|
|
671
|
|
672 return HAL_OK;
|
|
673 }
|
|
674 else
|
|
675 {
|
|
676 return HAL_BUSY;
|
|
677 }
|
|
678 }
|
|
679
|
|
680 /**
|
|
681 * @brief Receives an amount of data in blocking mode.
|
|
682 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
683 * the configuration information for the specified UART module.
|
|
684 * @param pData: Pointer to data buffer
|
|
685 * @param Size: Amount of data to be received
|
|
686 * @param Timeout: Timeout duration
|
|
687 * @retval HAL status
|
|
688 */
|
|
689 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
|
|
690 {
|
|
691 uint16_t* tmp;
|
|
692 uint32_t tmp1 = 0;
|
|
693
|
|
694 tmp1 = huart->State;
|
|
695 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_TX))
|
|
696 {
|
|
697 if((pData == NULL ) || (Size == 0))
|
|
698 {
|
|
699 return HAL_ERROR;
|
|
700 }
|
|
701
|
|
702 /* Process Locked */
|
|
703 __HAL_LOCK(huart);
|
|
704
|
|
705 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
706 /* Check if a non-blocking transmit process is ongoing or not */
|
|
707 if(huart->State == HAL_UART_STATE_BUSY_TX)
|
|
708 {
|
|
709 huart->State = HAL_UART_STATE_BUSY_TX_RX;
|
|
710 }
|
|
711 else
|
|
712 {
|
|
713 huart->State = HAL_UART_STATE_BUSY_RX;
|
|
714 }
|
|
715
|
|
716 huart->RxXferSize = Size;
|
|
717 huart->RxXferCount = Size;
|
|
718
|
|
719 /* Check the remain data to be received */
|
|
720 while(huart->RxXferCount > 0)
|
|
721 {
|
|
722 huart->RxXferCount--;
|
|
723 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
|
|
724 {
|
|
725 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
|
|
726 {
|
|
727 return HAL_TIMEOUT;
|
|
728 }
|
|
729 tmp = (uint16_t*) pData ;
|
|
730 if(huart->Init.Parity == UART_PARITY_NONE)
|
|
731 {
|
|
732 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
|
|
733 pData +=2;
|
|
734 }
|
|
735 else
|
|
736 {
|
|
737 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
|
|
738 pData +=1;
|
|
739 }
|
|
740
|
|
741 }
|
|
742 else
|
|
743 {
|
|
744 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
|
|
745 {
|
|
746 return HAL_TIMEOUT;
|
|
747 }
|
|
748 if(huart->Init.Parity == UART_PARITY_NONE)
|
|
749 {
|
|
750 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
|
|
751 }
|
|
752 else
|
|
753 {
|
|
754 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
|
|
755 }
|
|
756
|
|
757 }
|
|
758 }
|
|
759
|
|
760 /* Check if a non-blocking transmit process is ongoing or not */
|
|
761 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
|
|
762 {
|
|
763 huart->State = HAL_UART_STATE_BUSY_TX;
|
|
764 }
|
|
765 else
|
|
766 {
|
|
767 huart->State = HAL_UART_STATE_READY;
|
|
768 }
|
|
769 /* Process Unlocked */
|
|
770 __HAL_UNLOCK(huart);
|
|
771
|
|
772 return HAL_OK;
|
|
773 }
|
|
774 else
|
|
775 {
|
|
776 return HAL_BUSY;
|
|
777 }
|
|
778 }
|
|
779
|
|
780 /**
|
|
781 * @brief Sends an amount of data in non blocking mode.
|
|
782 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
783 * the configuration information for the specified UART module.
|
|
784 * @param pData: Pointer to data buffer
|
|
785 * @param Size: Amount of data to be sent
|
|
786 * @retval HAL status
|
|
787 */
|
|
788 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
|
|
789 {
|
|
790 uint32_t tmp = 0;
|
|
791
|
|
792 tmp = huart->State;
|
|
793 if((tmp == HAL_UART_STATE_READY) || (tmp == HAL_UART_STATE_BUSY_RX))
|
|
794 {
|
|
795 if((pData == NULL ) || (Size == 0))
|
|
796 {
|
|
797 return HAL_ERROR;
|
|
798 }
|
|
799
|
|
800 /* Process Locked */
|
|
801 __HAL_LOCK(huart);
|
|
802
|
|
803 huart->pTxBuffPtr = pData;
|
|
804 huart->TxXferSize = Size;
|
|
805 huart->TxXferCount = Size;
|
|
806
|
|
807 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
808 /* Check if a receive process is ongoing or not */
|
|
809 if(huart->State == HAL_UART_STATE_BUSY_RX)
|
|
810 {
|
|
811 huart->State = HAL_UART_STATE_BUSY_TX_RX;
|
|
812 }
|
|
813 else
|
|
814 {
|
|
815 huart->State = HAL_UART_STATE_BUSY_TX;
|
|
816 }
|
|
817
|
|
818 /* Enable the UART Parity Error Interrupt */
|
|
819 __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
|
|
820
|
|
821 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
|
|
822 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
|
|
823
|
|
824 /* Process Unlocked */
|
|
825 __HAL_UNLOCK(huart);
|
|
826
|
|
827 /* Enable the UART Transmit data register empty Interrupt */
|
|
828 __HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
|
|
829
|
|
830 return HAL_OK;
|
|
831 }
|
|
832 else
|
|
833 {
|
|
834 return HAL_BUSY;
|
|
835 }
|
|
836 }
|
|
837
|
|
838 /**
|
|
839 * @brief Receives an amount of data in non blocking mode
|
|
840 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
841 * the configuration information for the specified UART module.
|
|
842 * @param pData: Pointer to data buffer
|
|
843 * @param Size: Amount of data to be received
|
|
844 * @retval HAL status
|
|
845 */
|
|
846 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
|
|
847 {
|
|
848 uint32_t tmp = 0;
|
|
849
|
|
850 tmp = huart->State;
|
|
851 if((tmp == HAL_UART_STATE_READY) || (tmp == HAL_UART_STATE_BUSY_TX))
|
|
852 {
|
|
853 if((pData == NULL ) || (Size == 0))
|
|
854 {
|
|
855 return HAL_ERROR;
|
|
856 }
|
|
857
|
|
858 /* Process Locked */
|
|
859 __HAL_LOCK(huart);
|
|
860
|
|
861 huart->pRxBuffPtr = pData;
|
|
862 huart->RxXferSize = Size;
|
|
863 huart->RxXferCount = Size;
|
|
864
|
|
865 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
866 /* Check if a transmit process is ongoing or not */
|
|
867 if(huart->State == HAL_UART_STATE_BUSY_TX)
|
|
868 {
|
|
869 huart->State = HAL_UART_STATE_BUSY_TX_RX;
|
|
870 }
|
|
871 else
|
|
872 {
|
|
873 huart->State = HAL_UART_STATE_BUSY_RX;
|
|
874 }
|
|
875
|
|
876 /* Enable the UART Parity Error Interrupt */
|
|
877 __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
|
|
878
|
|
879 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
|
|
880 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
|
|
881
|
|
882 /* Process Unlocked */
|
|
883 __HAL_UNLOCK(huart);
|
|
884
|
|
885 /* Enable the UART Data Register not empty Interrupt */
|
|
886 __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
|
|
887
|
|
888 return HAL_OK;
|
|
889 }
|
|
890 else
|
|
891 {
|
|
892 return HAL_BUSY;
|
|
893 }
|
|
894 }
|
|
895
|
|
896 /**
|
|
897 * @brief Sends an amount of data in non blocking mode.
|
|
898 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
899 * the configuration information for the specified UART module.
|
|
900 * @param pData: Pointer to data buffer
|
|
901 * @param Size: Amount of data to be sent
|
|
902 * @retval HAL status
|
|
903 */
|
|
904 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
|
|
905 {
|
|
906 uint32_t *tmp;
|
|
907 uint32_t tmp1 = 0;
|
|
908
|
|
909 tmp1 = huart->State;
|
|
910 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_RX))
|
|
911 {
|
|
912 if((pData == NULL ) || (Size == 0))
|
|
913 {
|
|
914 return HAL_ERROR;
|
|
915 }
|
|
916
|
|
917 /* Process Locked */
|
|
918 __HAL_LOCK(huart);
|
|
919
|
|
920 huart->pTxBuffPtr = pData;
|
|
921 huart->TxXferSize = Size;
|
|
922 huart->TxXferCount = Size;
|
|
923
|
|
924 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
925 /* Check if a receive process is ongoing or not */
|
|
926 if(huart->State == HAL_UART_STATE_BUSY_RX)
|
|
927 {
|
|
928 huart->State = HAL_UART_STATE_BUSY_TX_RX;
|
|
929 }
|
|
930 else
|
|
931 {
|
|
932 huart->State = HAL_UART_STATE_BUSY_TX;
|
|
933 }
|
|
934
|
|
935 /* Set the UART DMA transfer complete callback */
|
|
936 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
|
|
937
|
|
938 /* Set the UART DMA Half transfer complete callback */
|
|
939 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
|
|
940
|
|
941 /* Set the DMA error callback */
|
|
942 huart->hdmatx->XferErrorCallback = UART_DMAError;
|
|
943
|
|
944 /* Enable the UART transmit DMA Stream */
|
|
945 tmp = (uint32_t*)&pData;
|
|
946 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->DR, Size);
|
|
947
|
|
948 /* Clear the TC flag in the SR register by writing 0 to it */
|
|
949 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
|
|
950
|
|
951 /* Enable the DMA transfer for transmit request by setting the DMAT bit
|
|
952 in the UART CR3 register */
|
|
953 huart->Instance->CR3 |= USART_CR3_DMAT;
|
|
954
|
|
955 /* Process Unlocked */
|
|
956 __HAL_UNLOCK(huart);
|
|
957
|
|
958 return HAL_OK;
|
|
959 }
|
|
960 else
|
|
961 {
|
|
962 return HAL_BUSY;
|
|
963 }
|
|
964 }
|
|
965
|
|
966 /**
|
|
967 * @brief Receives an amount of data in non blocking mode.
|
|
968 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
969 * the configuration information for the specified UART module.
|
|
970 * @param pData: Pointer to data buffer
|
|
971 * @param Size: Amount of data to be received
|
|
972 * @note When the UART parity is enabled (PCE = 1) the data received contain the parity bit.
|
|
973 * @retval HAL status
|
|
974 */
|
|
975 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
|
|
976 {
|
|
977 uint32_t *tmp;
|
|
978 uint32_t tmp1 = 0;
|
|
979
|
|
980 tmp1 = huart->State;
|
|
981 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_TX))
|
|
982 {
|
|
983 if((pData == NULL ) || (Size == 0))
|
|
984 {
|
|
985 return HAL_ERROR;
|
|
986 }
|
|
987
|
|
988 /* Process Locked */
|
|
989 __HAL_LOCK(huart);
|
|
990
|
|
991 huart->pRxBuffPtr = pData;
|
|
992 huart->RxXferSize = Size;
|
|
993
|
|
994 huart->ErrorCode = HAL_UART_ERROR_NONE;
|
|
995 /* Check if a transmit process is ongoing or not */
|
|
996 if(huart->State == HAL_UART_STATE_BUSY_TX)
|
|
997 {
|
|
998 huart->State = HAL_UART_STATE_BUSY_TX_RX;
|
|
999 }
|
|
1000 else
|
|
1001 {
|
|
1002 huart->State = HAL_UART_STATE_BUSY_RX;
|
|
1003 }
|
|
1004
|
|
1005 /* Set the UART DMA transfer complete callback */
|
|
1006 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
|
|
1007
|
|
1008 /* Set the UART DMA Half transfer complete callback */
|
|
1009 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
|
|
1010
|
|
1011 /* Set the DMA error callback */
|
|
1012 huart->hdmarx->XferErrorCallback = UART_DMAError;
|
|
1013
|
|
1014 /* Enable the DMA Stream */
|
|
1015 tmp = (uint32_t*)&pData;
|
|
1016 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size);
|
|
1017
|
|
1018 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
|
|
1019 in the UART CR3 register */
|
|
1020 huart->Instance->CR3 |= USART_CR3_DMAR;
|
|
1021
|
|
1022 /* Process Unlocked */
|
|
1023 __HAL_UNLOCK(huart);
|
|
1024
|
|
1025 return HAL_OK;
|
|
1026 }
|
|
1027 else
|
|
1028 {
|
|
1029 return HAL_BUSY;
|
|
1030 }
|
|
1031 }
|
|
1032
|
|
1033 /**
|
|
1034 * @brief Pauses the DMA Transfer.
|
|
1035 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1036 * the configuration information for the specified UART module.
|
|
1037 * @retval HAL status
|
|
1038 */
|
|
1039 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
|
|
1040 {
|
|
1041 /* Process Locked */
|
|
1042 __HAL_LOCK(huart);
|
|
1043
|
|
1044 if(huart->State == HAL_UART_STATE_BUSY_TX)
|
|
1045 {
|
|
1046 /* Disable the UART DMA Tx request */
|
|
1047 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
|
|
1048 }
|
|
1049 else if(huart->State == HAL_UART_STATE_BUSY_RX)
|
|
1050 {
|
|
1051 /* Disable the UART DMA Rx request */
|
|
1052 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
|
|
1053 }
|
|
1054 else if (huart->State == HAL_UART_STATE_BUSY_TX_RX)
|
|
1055 {
|
|
1056 /* Disable the UART DMA Tx & Rx requests */
|
|
1057 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
|
|
1058 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
|
|
1059 }
|
|
1060
|
|
1061 /* Process Unlocked */
|
|
1062 __HAL_UNLOCK(huart);
|
|
1063
|
|
1064 return HAL_OK;
|
|
1065 }
|
|
1066
|
|
1067 /**
|
|
1068 * @brief Resumes the DMA Transfer.
|
|
1069 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1070 * the configuration information for the specified UART module.
|
|
1071 * @retval HAL status
|
|
1072 */
|
|
1073 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
|
|
1074 {
|
|
1075 /* Process Locked */
|
|
1076 __HAL_LOCK(huart);
|
|
1077
|
|
1078 if(huart->State == HAL_UART_STATE_BUSY_TX)
|
|
1079 {
|
|
1080 /* Enable the UART DMA Tx request */
|
|
1081 huart->Instance->CR3 |= USART_CR3_DMAT;
|
|
1082 }
|
|
1083 else if(huart->State == HAL_UART_STATE_BUSY_RX)
|
|
1084 {
|
|
1085 /* Clear the Overrun flag before resuming the Rx transfer*/
|
|
1086 __HAL_UART_CLEAR_OREFLAG(huart);
|
|
1087 /* Enable the UART DMA Rx request */
|
|
1088 huart->Instance->CR3 |= USART_CR3_DMAR;
|
|
1089 }
|
|
1090 else if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
|
|
1091 {
|
|
1092 /* Clear the Overrun flag before resuming the Rx transfer*/
|
|
1093 __HAL_UART_CLEAR_OREFLAG(huart);
|
|
1094 /* Enable the UART DMA Tx & Rx request */
|
|
1095 huart->Instance->CR3 |= USART_CR3_DMAT;
|
|
1096 huart->Instance->CR3 |= USART_CR3_DMAR;
|
|
1097 }
|
|
1098
|
|
1099 /* Process Unlocked */
|
|
1100 __HAL_UNLOCK(huart);
|
|
1101
|
|
1102 return HAL_OK;
|
|
1103 }
|
|
1104
|
|
1105 /**
|
|
1106 * @brief Stops the DMA Transfer.
|
|
1107 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1108 * the configuration information for the specified UART module.
|
|
1109 * @retval HAL status
|
|
1110 */
|
|
1111 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
|
|
1112 {
|
|
1113 /* The Lock is not implemented on this API to allow the user application
|
|
1114 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
|
|
1115 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
|
|
1116 and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
|
|
1117 */
|
|
1118
|
|
1119 /* Disable the UART Tx/Rx DMA requests */
|
|
1120 huart->Instance->CR3 &= ~USART_CR3_DMAT;
|
|
1121 huart->Instance->CR3 &= ~USART_CR3_DMAR;
|
|
1122
|
|
1123 /* Abort the UART DMA tx Stream */
|
|
1124 if(huart->hdmatx != NULL)
|
|
1125 {
|
|
1126 HAL_DMA_Abort(huart->hdmatx);
|
|
1127 }
|
|
1128 /* Abort the UART DMA rx Stream */
|
|
1129 if(huart->hdmarx != NULL)
|
|
1130 {
|
|
1131 HAL_DMA_Abort(huart->hdmarx);
|
|
1132 }
|
|
1133
|
|
1134 huart->State = HAL_UART_STATE_READY;
|
|
1135
|
|
1136 return HAL_OK;
|
|
1137 }
|
|
1138
|
|
1139 /**
|
|
1140 * @brief This function handles UART interrupt request.
|
|
1141 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1142 * the configuration information for the specified UART module.
|
|
1143 * @retval None
|
|
1144 */
|
|
1145 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
|
|
1146 {
|
|
1147 uint32_t tmp1 = 0, tmp2 = 0;
|
|
1148
|
|
1149 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_PE);
|
|
1150 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE);
|
|
1151 /* UART parity error interrupt occurred ------------------------------------*/
|
|
1152 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1153 {
|
|
1154 __HAL_UART_CLEAR_PEFLAG(huart);
|
|
1155
|
|
1156 huart->ErrorCode |= HAL_UART_ERROR_PE;
|
|
1157 }
|
|
1158
|
|
1159 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_FE);
|
|
1160 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
|
|
1161 /* UART frame error interrupt occurred -------------------------------------*/
|
|
1162 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1163 {
|
|
1164 __HAL_UART_CLEAR_FEFLAG(huart);
|
|
1165
|
|
1166 huart->ErrorCode |= HAL_UART_ERROR_FE;
|
|
1167 }
|
|
1168
|
|
1169 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_NE);
|
|
1170 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
|
|
1171 /* UART noise error interrupt occurred -------------------------------------*/
|
|
1172 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1173 {
|
|
1174 __HAL_UART_CLEAR_NEFLAG(huart);
|
|
1175
|
|
1176 huart->ErrorCode |= HAL_UART_ERROR_NE;
|
|
1177 }
|
|
1178
|
|
1179 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_ORE);
|
|
1180 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
|
|
1181 /* UART Over-Run interrupt occurred ----------------------------------------*/
|
|
1182 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1183 {
|
|
1184 __HAL_UART_CLEAR_OREFLAG(huart);
|
|
1185
|
|
1186 huart->ErrorCode |= HAL_UART_ERROR_ORE;
|
|
1187 }
|
|
1188
|
|
1189 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE);
|
|
1190 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE);
|
|
1191 /* UART in mode Receiver ---------------------------------------------------*/
|
|
1192 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1193 {
|
|
1194 UART_Receive_IT(huart);
|
|
1195 }
|
|
1196
|
|
1197 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_TXE);
|
|
1198 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE);
|
|
1199 /* UART in mode Transmitter ------------------------------------------------*/
|
|
1200 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1201 {
|
|
1202 UART_Transmit_IT(huart);
|
|
1203 }
|
|
1204
|
|
1205 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_TC);
|
|
1206 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC);
|
|
1207 /* UART in mode Transmitter end --------------------------------------------*/
|
|
1208 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1209 {
|
|
1210 UART_EndTransmit_IT(huart);
|
|
1211 }
|
|
1212
|
|
1213 if(huart->ErrorCode != HAL_UART_ERROR_NONE)
|
|
1214 {
|
|
1215 /* Set the UART state ready to be able to start again the process */
|
|
1216 huart->State = HAL_UART_STATE_READY;
|
|
1217
|
|
1218 HAL_UART_ErrorCallback(huart);
|
|
1219 }
|
|
1220 }
|
|
1221
|
|
1222 /**
|
|
1223 * @brief Tx Transfer completed callbacks.
|
|
1224 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1225 * the configuration information for the specified UART module.
|
|
1226 * @retval None
|
|
1227 */
|
|
1228 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
|
1229 {
|
|
1230 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1231 the HAL_UART_TxCpltCallback could be implemented in the user file
|
|
1232 */
|
|
1233 }
|
|
1234
|
|
1235 /**
|
|
1236 * @brief Tx Half Transfer completed callbacks.
|
|
1237 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1238 * the configuration information for the specified UART module.
|
|
1239 * @retval None
|
|
1240 */
|
|
1241 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
|
|
1242 {
|
|
1243 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1244 the HAL_UART_TxCpltCallback could be implemented in the user file
|
|
1245 */
|
|
1246 }
|
|
1247
|
|
1248 /**
|
|
1249 * @brief Rx Transfer completed callbacks.
|
|
1250 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1251 * the configuration information for the specified UART module.
|
|
1252 * @retval None
|
|
1253 */
|
|
1254 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
1255 {
|
|
1256 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1257 the HAL_UART_TxCpltCallback could be implemented in the user file
|
|
1258 */
|
|
1259 }
|
|
1260
|
|
1261 /**
|
|
1262 * @brief Rx Half Transfer completed callbacks.
|
|
1263 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1264 * the configuration information for the specified UART module.
|
|
1265 * @retval None
|
|
1266 */
|
|
1267 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
|
|
1268 {
|
|
1269 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1270 the HAL_UART_TxCpltCallback could be implemented in the user file
|
|
1271 */
|
|
1272 }
|
|
1273
|
|
1274 /**
|
|
1275 * @brief UART error callbacks.
|
|
1276 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1277 * the configuration information for the specified UART module.
|
|
1278 * @retval None
|
|
1279 */
|
|
1280 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
|
|
1281 {
|
|
1282 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1283 the HAL_UART_ErrorCallback could be implemented in the user file
|
|
1284 */
|
|
1285 }
|
|
1286
|
|
1287 /**
|
|
1288 * @}
|
|
1289 */
|
|
1290
|
|
1291 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
|
|
1292 * @brief UART control functions
|
|
1293 *
|
|
1294 @verbatim
|
|
1295 ==============================================================================
|
|
1296 ##### Peripheral Control functions #####
|
|
1297 ==============================================================================
|
|
1298 [..]
|
|
1299 This subsection provides a set of functions allowing to control the UART:
|
|
1300 (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character.
|
|
1301 (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode.
|
|
1302 (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software.
|
|
1303
|
|
1304 @endverbatim
|
|
1305 * @{
|
|
1306 */
|
|
1307
|
|
1308 /**
|
|
1309 * @brief Transmits break characters.
|
|
1310 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1311 * the configuration information for the specified UART module.
|
|
1312 * @retval HAL status
|
|
1313 */
|
|
1314 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
|
|
1315 {
|
|
1316 /* Check the parameters */
|
|
1317 assert_param(IS_UART_INSTANCE(huart->Instance));
|
|
1318
|
|
1319 /* Process Locked */
|
|
1320 __HAL_LOCK(huart);
|
|
1321
|
|
1322 huart->State = HAL_UART_STATE_BUSY;
|
|
1323
|
|
1324 /* Send break characters */
|
|
1325 huart->Instance->CR1 |= USART_CR1_SBK;
|
|
1326
|
|
1327 huart->State = HAL_UART_STATE_READY;
|
|
1328
|
|
1329 /* Process Unlocked */
|
|
1330 __HAL_UNLOCK(huart);
|
|
1331
|
|
1332 return HAL_OK;
|
|
1333 }
|
|
1334
|
|
1335 /**
|
|
1336 * @brief Enters the UART in mute mode.
|
|
1337 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1338 * the configuration information for the specified UART module.
|
|
1339 * @retval HAL status
|
|
1340 */
|
|
1341 HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
|
|
1342 {
|
|
1343 /* Check the parameters */
|
|
1344 assert_param(IS_UART_INSTANCE(huart->Instance));
|
|
1345
|
|
1346 /* Process Locked */
|
|
1347 __HAL_LOCK(huart);
|
|
1348
|
|
1349 huart->State = HAL_UART_STATE_BUSY;
|
|
1350
|
|
1351 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
|
|
1352 huart->Instance->CR1 |= USART_CR1_RWU;
|
|
1353
|
|
1354 huart->State = HAL_UART_STATE_READY;
|
|
1355
|
|
1356 /* Process Unlocked */
|
|
1357 __HAL_UNLOCK(huart);
|
|
1358
|
|
1359 return HAL_OK;
|
|
1360 }
|
|
1361
|
|
1362 /**
|
|
1363 * @brief Exits the UART mute mode: wake up software.
|
|
1364 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1365 * the configuration information for the specified UART module.
|
|
1366 * @retval HAL status
|
|
1367 */
|
|
1368 HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart)
|
|
1369 {
|
|
1370 /* Check the parameters */
|
|
1371 assert_param(IS_UART_INSTANCE(huart->Instance));
|
|
1372
|
|
1373 /* Process Locked */
|
|
1374 __HAL_LOCK(huart);
|
|
1375
|
|
1376 huart->State = HAL_UART_STATE_BUSY;
|
|
1377
|
|
1378 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
|
|
1379 huart->Instance->CR1 &= (uint32_t)~((uint32_t)USART_CR1_RWU);
|
|
1380
|
|
1381 huart->State = HAL_UART_STATE_READY;
|
|
1382
|
|
1383 /* Process Unlocked */
|
|
1384 __HAL_UNLOCK(huart);
|
|
1385
|
|
1386 return HAL_OK;
|
|
1387 }
|
|
1388
|
|
1389 /**
|
|
1390 * @brief Enables the UART transmitter and disables the UART receiver.
|
|
1391 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1392 * the configuration information for the specified UART module.
|
|
1393 * @retval HAL status
|
|
1394 */
|
|
1395 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
|
|
1396 {
|
|
1397 uint32_t tmpreg = 0x00;
|
|
1398
|
|
1399 /* Process Locked */
|
|
1400 __HAL_LOCK(huart);
|
|
1401
|
|
1402 huart->State = HAL_UART_STATE_BUSY;
|
|
1403
|
|
1404 /*-------------------------- USART CR1 Configuration -----------------------*/
|
|
1405 tmpreg = huart->Instance->CR1;
|
|
1406
|
|
1407 /* Clear TE and RE bits */
|
|
1408 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
|
|
1409
|
|
1410 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
|
|
1411 tmpreg |= (uint32_t)USART_CR1_TE;
|
|
1412
|
|
1413 /* Write to USART CR1 */
|
|
1414 huart->Instance->CR1 = (uint32_t)tmpreg;
|
|
1415
|
|
1416 huart->State = HAL_UART_STATE_READY;
|
|
1417
|
|
1418 /* Process Unlocked */
|
|
1419 __HAL_UNLOCK(huart);
|
|
1420
|
|
1421 return HAL_OK;
|
|
1422 }
|
|
1423
|
|
1424 /**
|
|
1425 * @brief Enables the UART receiver and disables the UART transmitter.
|
|
1426 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1427 * the configuration information for the specified UART module.
|
|
1428 * @retval HAL status
|
|
1429 */
|
|
1430 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
|
|
1431 {
|
|
1432 uint32_t tmpreg = 0x00;
|
|
1433
|
|
1434 /* Process Locked */
|
|
1435 __HAL_LOCK(huart);
|
|
1436
|
|
1437 huart->State = HAL_UART_STATE_BUSY;
|
|
1438
|
|
1439 /*-------------------------- USART CR1 Configuration -----------------------*/
|
|
1440 tmpreg = huart->Instance->CR1;
|
|
1441
|
|
1442 /* Clear TE and RE bits */
|
|
1443 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
|
|
1444
|
|
1445 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
|
|
1446 tmpreg |= (uint32_t)USART_CR1_RE;
|
|
1447
|
|
1448 /* Write to USART CR1 */
|
|
1449 huart->Instance->CR1 = (uint32_t)tmpreg;
|
|
1450
|
|
1451 huart->State = HAL_UART_STATE_READY;
|
|
1452
|
|
1453 /* Process Unlocked */
|
|
1454 __HAL_UNLOCK(huart);
|
|
1455
|
|
1456 return HAL_OK;
|
|
1457 }
|
|
1458
|
|
1459 /**
|
|
1460 * @}
|
|
1461 */
|
|
1462
|
|
1463 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Errors functions
|
|
1464 * @brief UART State and Errors functions
|
|
1465 *
|
|
1466 @verbatim
|
|
1467 ==============================================================================
|
|
1468 ##### Peripheral State and Errors functions #####
|
|
1469 ==============================================================================
|
|
1470 [..]
|
|
1471 This subsection provides a set of functions allowing to return the State of
|
|
1472 UART communication process, return Peripheral Errors occurred during communication
|
|
1473 process
|
|
1474 (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral.
|
|
1475 (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication.
|
|
1476
|
|
1477 @endverbatim
|
|
1478 * @{
|
|
1479 */
|
|
1480
|
|
1481 /**
|
|
1482 * @brief Returns the UART state.
|
|
1483 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1484 * the configuration information for the specified UART module.
|
|
1485 * @retval HAL state
|
|
1486 */
|
|
1487 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
|
|
1488 {
|
|
1489 return huart->State;
|
|
1490 }
|
|
1491
|
|
1492 /**
|
|
1493 * @brief Return the UART error code
|
|
1494 * @param huart : pointer to a UART_HandleTypeDef structure that contains
|
|
1495 * the configuration information for the specified UART.
|
|
1496 * @retval UART Error Code
|
|
1497 */
|
|
1498 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
|
|
1499 {
|
|
1500 return huart->ErrorCode;
|
|
1501 }
|
|
1502
|
|
1503 /**
|
|
1504 * @}
|
|
1505 */
|
|
1506
|
|
1507 /**
|
|
1508 * @brief DMA UART transmit process complete callback.
|
|
1509 * @param hdma: DMA handle
|
|
1510 * @retval None
|
|
1511 */
|
|
1512 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
|
|
1513 {
|
|
1514 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
|
1515 /* DMA Normal mode*/
|
|
1516 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
|
|
1517 {
|
|
1518 huart->TxXferCount = 0;
|
|
1519
|
|
1520 /* Disable the DMA transfer for transmit request by setting the DMAT bit
|
|
1521 in the UART CR3 register */
|
|
1522 huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
|
|
1523
|
|
1524 /* Enable the UART Transmit Complete Interrupt */
|
|
1525 __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
|
|
1526 }
|
|
1527 /* DMA Circular mode */
|
|
1528 else
|
|
1529 {
|
|
1530 HAL_UART_TxCpltCallback(huart);
|
|
1531 }
|
|
1532 }
|
|
1533
|
|
1534 /**
|
|
1535 * @brief DMA UART transmit process half complete callback
|
|
1536 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
|
|
1537 * the configuration information for the specified DMA module.
|
|
1538 * @retval None
|
|
1539 */
|
|
1540 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
|
|
1541 {
|
|
1542 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
|
|
1543
|
|
1544 HAL_UART_TxHalfCpltCallback(huart);
|
|
1545 }
|
|
1546
|
|
1547 /**
|
|
1548 * @brief DMA UART receive process complete callback.
|
|
1549 * @param hdma: DMA handle
|
|
1550 * @retval None
|
|
1551 */
|
|
1552 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
|
|
1553 {
|
|
1554 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
|
1555 /* DMA Normal mode*/
|
|
1556 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
|
|
1557 {
|
|
1558 huart->RxXferCount = 0;
|
|
1559
|
|
1560 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
|
|
1561 in the UART CR3 register */
|
|
1562 huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
|
|
1563
|
|
1564 /* Check if a transmit process is ongoing or not */
|
|
1565 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
|
|
1566 {
|
|
1567 huart->State = HAL_UART_STATE_BUSY_TX;
|
|
1568 }
|
|
1569 else
|
|
1570 {
|
|
1571 huart->State = HAL_UART_STATE_READY;
|
|
1572 }
|
|
1573 }
|
|
1574 HAL_UART_RxCpltCallback(huart);
|
|
1575 }
|
|
1576
|
|
1577 /**
|
|
1578 * @brief DMA UART receive process half complete callback
|
|
1579 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
|
|
1580 * the configuration information for the specified DMA module.
|
|
1581 * @retval None
|
|
1582 */
|
|
1583 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
|
|
1584 {
|
|
1585 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
|
|
1586
|
|
1587 HAL_UART_RxHalfCpltCallback(huart);
|
|
1588 }
|
|
1589
|
|
1590 /**
|
|
1591 * @brief DMA UART communication error callback.
|
|
1592 * @param hdma: DMA handle
|
|
1593 * @retval None
|
|
1594 */
|
|
1595 static void UART_DMAError(DMA_HandleTypeDef *hdma)
|
|
1596 {
|
|
1597 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
|
1598 huart->RxXferCount = 0;
|
|
1599 huart->TxXferCount = 0;
|
|
1600 huart->State= HAL_UART_STATE_READY;
|
|
1601 huart->ErrorCode |= HAL_UART_ERROR_DMA;
|
|
1602 HAL_UART_ErrorCallback(huart);
|
|
1603 }
|
|
1604
|
|
1605 /**
|
|
1606 * @brief This function handles UART Communication Timeout.
|
|
1607 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1608 * the configuration information for the specified UART module.
|
|
1609 * @param Flag: specifies the UART flag to check.
|
|
1610 * @param Status: The new Flag status (SET or RESET).
|
|
1611 * @param Timeout: Timeout duration
|
|
1612 * @retval HAL status
|
|
1613 */
|
|
1614 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
|
|
1615 {
|
|
1616 uint32_t tickstart = 0;
|
|
1617
|
|
1618 /* Get tick */
|
|
1619 tickstart = HAL_GetTick();
|
|
1620
|
|
1621 /* Wait until flag is set */
|
|
1622 if(Status == RESET)
|
|
1623 {
|
|
1624 while(__HAL_UART_GET_FLAG(huart, Flag) == RESET)
|
|
1625 {
|
|
1626 /* Check for the Timeout */
|
|
1627 if(Timeout != HAL_MAX_DELAY)
|
|
1628 {
|
|
1629 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
|
|
1630 {
|
|
1631 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
|
|
1632 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
|
|
1633 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
|
|
1634 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
|
|
1635 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
|
|
1636
|
|
1637 huart->State= HAL_UART_STATE_READY;
|
|
1638
|
|
1639 /* Process Unlocked */
|
|
1640 __HAL_UNLOCK(huart);
|
|
1641
|
|
1642 return HAL_TIMEOUT;
|
|
1643 }
|
|
1644 }
|
|
1645 }
|
|
1646 }
|
|
1647 else
|
|
1648 {
|
|
1649 while(__HAL_UART_GET_FLAG(huart, Flag) != RESET)
|
|
1650 {
|
|
1651 /* Check for the Timeout */
|
|
1652 if(Timeout != HAL_MAX_DELAY)
|
|
1653 {
|
|
1654 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
|
|
1655 {
|
|
1656 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
|
|
1657 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
|
|
1658 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
|
|
1659 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
|
|
1660 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
|
|
1661
|
|
1662 huart->State= HAL_UART_STATE_READY;
|
|
1663
|
|
1664 /* Process Unlocked */
|
|
1665 __HAL_UNLOCK(huart);
|
|
1666
|
|
1667 return HAL_TIMEOUT;
|
|
1668 }
|
|
1669 }
|
|
1670 }
|
|
1671 }
|
|
1672 return HAL_OK;
|
|
1673 }
|
|
1674
|
|
1675 /**
|
|
1676 * @brief Sends an amount of data in non blocking mode.
|
|
1677 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
|
|
1678 * the configuration information for the specified UART module.
|
|
1679 * @retval HAL status
|
|
1680 */
|
|
1681 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
|
|
1682 {
|
|
1683 uint16_t* tmp;
|
|
1684 uint32_t tmp1 = 0;
|
|
1685
|
|
1686 tmp1 = huart->State;
|
|
1687 if((tmp1 == HAL_UART_STATE_BUSY_TX) || (tmp1 == HAL_UART_STATE_BUSY_TX_RX))
|
|
1688 {
|
|
1689 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
|
|
1690 {
|
|
1691 tmp = (uint16_t*) huart->pTxBuffPtr;
|
|
1692 huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
|
|
1693 if(huart->Init.Parity == UART_PARITY_NONE)
|
|
1694 {
|
|
1695 huart->pTxBuffPtr += 2;
|
|
1696 }
|
|
1697 else
|
|
1698 {
|
|
1699 huart->pTxBuffPtr += 1;
|
|
1700 }
|
|
1701 }
|
|
1702 else
|
|
1703 {
|
|
1704 huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF);
|
|
1705 }
|
|
1706
|
|
1707 if(--huart->TxXferCount == 0)
|
|
1708 {
|
|
1709 /* Disable the UART Transmit Complete Interrupt */
|
|
1710 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
|
|
1711
|
|
1712 /* Enable the UART Transmit Complete Interrupt */
|
|
1713 __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
|
|
1714 }
|
|
1715 return HAL_OK;
|
|
1716 }
|
|
1717 else
|
|
1718 {
|
|
1719 return HAL_BUSY;
|
|
1720 }
|
|
1721 }
|
|
1722
|
|
1723
|
|
1724 /**
|
|
1725 * @brief Wraps up transmission in non blocking mode.
|
|
1726 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1727 * the configuration information for the specified UART module.
|
|
1728 * @retval HAL status
|
|
1729 */
|
|
1730 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
|
|
1731 {
|
|
1732 /* Disable the UART Transmit Complete Interrupt */
|
|
1733 __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
|
|
1734
|
|
1735 /* Check if a receive process is ongoing or not */
|
|
1736 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
|
|
1737 {
|
|
1738 huart->State = HAL_UART_STATE_BUSY_RX;
|
|
1739 }
|
|
1740 else
|
|
1741 {
|
|
1742 /* Disable the UART Parity Error Interrupt */
|
|
1743 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
|
|
1744
|
|
1745 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
|
|
1746 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
|
|
1747
|
|
1748 huart->State = HAL_UART_STATE_READY;
|
|
1749 }
|
|
1750
|
|
1751 HAL_UART_TxCpltCallback(huart);
|
|
1752
|
|
1753 return HAL_OK;
|
|
1754 }
|
|
1755
|
|
1756 /**
|
|
1757 * @brief Receives an amount of data in non blocking mode
|
|
1758 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1759 * the configuration information for the specified UART module.
|
|
1760 * @retval HAL status
|
|
1761 */
|
|
1762 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
|
|
1763 {
|
|
1764 uint16_t* tmp;
|
|
1765 uint32_t tmp1 = 0;
|
|
1766
|
|
1767 tmp1 = huart->State;
|
|
1768 if((tmp1 == HAL_UART_STATE_BUSY_RX) || (tmp1 == HAL_UART_STATE_BUSY_TX_RX))
|
|
1769 {
|
|
1770 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
|
|
1771 {
|
|
1772 tmp = (uint16_t*) huart->pRxBuffPtr;
|
|
1773 if(huart->Init.Parity == UART_PARITY_NONE)
|
|
1774 {
|
|
1775 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
|
|
1776 huart->pRxBuffPtr += 2;
|
|
1777 }
|
|
1778 else
|
|
1779 {
|
|
1780 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
|
|
1781 huart->pRxBuffPtr += 1;
|
|
1782 }
|
|
1783 }
|
|
1784 else
|
|
1785 {
|
|
1786 if(huart->Init.Parity == UART_PARITY_NONE)
|
|
1787 {
|
|
1788 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
|
|
1789 }
|
|
1790 else
|
|
1791 {
|
|
1792 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
|
|
1793 }
|
|
1794 }
|
|
1795
|
|
1796 if(--huart->RxXferCount == 0)
|
|
1797 {
|
|
1798 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
|
|
1799
|
|
1800 /* Check if a transmit process is ongoing or not */
|
|
1801 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
|
|
1802 {
|
|
1803 huart->State = HAL_UART_STATE_BUSY_TX;
|
|
1804 }
|
|
1805 else
|
|
1806 {
|
|
1807 /* Disable the UART Parity Error Interrupt */
|
|
1808 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
|
|
1809
|
|
1810 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
|
|
1811 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
|
|
1812
|
|
1813 huart->State = HAL_UART_STATE_READY;
|
|
1814 }
|
|
1815 HAL_UART_RxCpltCallback(huart);
|
|
1816
|
|
1817 return HAL_OK;
|
|
1818 }
|
|
1819 return HAL_OK;
|
|
1820 }
|
|
1821 else
|
|
1822 {
|
|
1823 return HAL_BUSY;
|
|
1824 }
|
|
1825 }
|
|
1826
|
|
1827 /**
|
|
1828 * @brief Configures the UART peripheral.
|
|
1829 * @param huart: pointer to a UART_HandleTypeDef structure that contains
|
|
1830 * the configuration information for the specified UART module.
|
|
1831 * @retval None
|
|
1832 */
|
|
1833 static void UART_SetConfig(UART_HandleTypeDef *huart)
|
|
1834 {
|
|
1835 uint32_t tmpreg = 0x00;
|
|
1836
|
|
1837 /* Check the parameters */
|
|
1838 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
|
|
1839 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
|
|
1840 assert_param(IS_UART_PARITY(huart->Init.Parity));
|
|
1841 assert_param(IS_UART_MODE(huart->Init.Mode));
|
|
1842
|
|
1843 /*-------------------------- USART CR2 Configuration -----------------------*/
|
|
1844 tmpreg = huart->Instance->CR2;
|
|
1845
|
|
1846 /* Clear STOP[13:12] bits */
|
|
1847 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
|
|
1848
|
|
1849 /* Configure the UART Stop Bits: Set STOP[13:12] bits according to huart->Init.StopBits value */
|
|
1850 tmpreg |= (uint32_t)huart->Init.StopBits;
|
|
1851
|
|
1852 /* Write to USART CR2 */
|
|
1853 huart->Instance->CR2 = (uint32_t)tmpreg;
|
|
1854
|
|
1855 /*-------------------------- USART CR1 Configuration -----------------------*/
|
|
1856 tmpreg = huart->Instance->CR1;
|
|
1857
|
|
1858 /* Clear M, PCE, PS, TE and RE bits */
|
|
1859 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
|
|
1860 USART_CR1_RE | USART_CR1_OVER8));
|
|
1861
|
|
1862 /* Configure the UART Word Length, Parity and mode:
|
|
1863 Set the M bits according to huart->Init.WordLength value
|
|
1864 Set PCE and PS bits according to huart->Init.Parity value
|
|
1865 Set TE and RE bits according to huart->Init.Mode value
|
|
1866 Set OVER8 bit according to huart->Init.OverSampling value */
|
|
1867 tmpreg |= (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling;
|
|
1868
|
|
1869 /* Write to USART CR1 */
|
|
1870 huart->Instance->CR1 = (uint32_t)tmpreg;
|
|
1871
|
|
1872 /*-------------------------- USART CR3 Configuration -----------------------*/
|
|
1873 tmpreg = huart->Instance->CR3;
|
|
1874
|
|
1875 /* Clear CTSE and RTSE bits */
|
|
1876 tmpreg &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
|
|
1877
|
|
1878 /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
|
|
1879 tmpreg |= huart->Init.HwFlowCtl;
|
|
1880
|
|
1881 /* Write to USART CR3 */
|
|
1882 huart->Instance->CR3 = (uint32_t)tmpreg;
|
|
1883
|
|
1884 /* Check the Over Sampling */
|
|
1885 if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
|
|
1886 {
|
|
1887 /*-------------------------- USART BRR Configuration ---------------------*/
|
|
1888 if((huart->Instance == USART1) || (huart->Instance == USART6))
|
|
1889 {
|
|
1890 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
|
|
1891 }
|
|
1892 else
|
|
1893 {
|
|
1894 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
|
|
1895 }
|
|
1896 }
|
|
1897 else
|
|
1898 {
|
|
1899 /*-------------------------- USART BRR Configuration ---------------------*/
|
|
1900 if((huart->Instance == USART1) || (huart->Instance == USART6))
|
|
1901 {
|
|
1902 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
|
|
1903 }
|
|
1904 else
|
|
1905 {
|
|
1906 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
|
|
1907 }
|
|
1908 }
|
|
1909 }
|
|
1910
|
|
1911 /**
|
|
1912 * @}
|
|
1913 */
|
|
1914
|
|
1915 #endif /* HAL_UART_MODULE_ENABLED */
|
|
1916 /**
|
|
1917 * @}
|
|
1918 */
|
|
1919
|
|
1920 /**
|
|
1921 * @}
|
|
1922 */
|
|
1923
|
|
1924 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|