Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * @file stm32f1xx_hal_usart.c |
||
4 | * @author MCD Application Team |
||
5 | * @brief USART HAL module driver. |
||
6 | * This file provides firmware functions to manage the following |
||
7 | * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter |
||
8 | * Peripheral (USART). |
||
9 | * + Initialization and de-initialization functions |
||
10 | * + IO operation functions |
||
11 | * + Peripheral Control functions |
||
12 | @verbatim |
||
13 | ============================================================================== |
||
14 | ##### How to use this driver ##### |
||
15 | ============================================================================== |
||
16 | [..] |
||
17 | The USART HAL driver can be used as follows: |
||
18 | |||
19 | (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart). |
||
20 | (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API: |
||
21 | (##) Enable the USARTx interface clock. |
||
22 | (##) USART pins configuration: |
||
23 | (+++) Enable the clock for the USART GPIOs. |
||
24 | (+++) Configure the USART pins as alternate function pull-up. |
||
25 | (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(), |
||
26 | HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs): |
||
27 | (+++) Configure the USARTx interrupt priority. |
||
28 | (+++) Enable the NVIC USART IRQ handle. |
||
29 | (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA() |
||
30 | HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs): |
||
31 | (+++) Declare a DMA handle structure for the Tx/Rx channel. |
||
32 | (+++) Enable the DMAx interface clock. |
||
33 | (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. |
||
34 | (+++) Configure the DMA Tx/Rx channel. |
||
35 | (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle. |
||
36 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel. |
||
37 | (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle |
||
38 | (used for last byte sending completion detection in DMA non circular mode) |
||
39 | |||
40 | (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware |
||
41 | flow control and Mode(Receiver/Transmitter) in the husart Init structure. |
||
42 | |||
43 | (#) Initialize the USART registers by calling the HAL_USART_Init() API: |
||
44 | (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc) |
||
45 | by calling the customized HAL_USART_MspInit(&husart) API. |
||
46 | |||
47 | -@@- The specific USART interrupts (Transmission complete interrupt, |
||
48 | RXNE interrupt and Error Interrupts) will be managed using the macros |
||
49 | __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process. |
||
50 | |||
51 | (#) Three operation modes are available within this driver : |
||
52 | |||
53 | *** Polling mode IO operation *** |
||
54 | ================================= |
||
55 | [..] |
||
56 | (+) Send an amount of data in blocking mode using HAL_USART_Transmit() |
||
57 | (+) Receive an amount of data in blocking mode using HAL_USART_Receive() |
||
58 | |||
59 | *** Interrupt mode IO operation *** |
||
60 | =================================== |
||
61 | [..] |
||
62 | (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT() |
||
63 | (+) At transmission end of transfer HAL_USART_TxHalfCpltCallback is executed and user can |
||
64 | add his own code by customization of function pointer HAL_USART_TxCpltCallback |
||
65 | (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT() |
||
66 | (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can |
||
67 | add his own code by customization of function pointer HAL_USART_RxCpltCallback |
||
68 | (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can |
||
69 | add his own code by customization of function pointer HAL_USART_ErrorCallback |
||
70 | |||
71 | *** DMA mode IO operation *** |
||
72 | ============================== |
||
73 | [..] |
||
74 | (+) Send an amount of data in non blocking mode (DMA) using HAL_USART_Transmit_DMA() |
||
75 | (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can |
||
76 | add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback |
||
77 | (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can |
||
78 | add his own code by customization of function pointer HAL_USART_TxCpltCallback |
||
79 | (+) Receive an amount of data in non blocking mode (DMA) using HAL_USART_Receive_DMA() |
||
80 | (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can |
||
81 | add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback |
||
82 | (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can |
||
83 | add his own code by customization of function pointer HAL_USART_RxCpltCallback |
||
84 | (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can |
||
85 | add his own code by customization of function pointer HAL_USART_ErrorCallback |
||
86 | (+) Pause the DMA Transfer using HAL_USART_DMAPause() |
||
87 | (+) Resume the DMA Transfer using HAL_USART_DMAResume() |
||
88 | (+) Stop the DMA Transfer using HAL_USART_DMAStop() |
||
89 | |||
90 | *** USART HAL driver macros list *** |
||
91 | ============================================= |
||
92 | [..] |
||
93 | Below the list of most used macros in USART HAL driver. |
||
94 | |||
95 | (+) __HAL_USART_ENABLE: Enable the USART peripheral |
||
96 | (+) __HAL_USART_DISABLE: Disable the USART peripheral |
||
97 | (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not |
||
98 | (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag |
||
99 | (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt |
||
100 | (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt |
||
101 | |||
102 | [..] |
||
103 | (@) You can refer to the USART HAL driver header file for more useful macros |
||
104 | |||
105 | ##### Callback registration ##### |
||
106 | ================================== |
||
107 | |||
108 | [..] |
||
109 | The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1 |
||
110 | allows the user to configure dynamically the driver callbacks. |
||
111 | |||
112 | [..] |
||
113 | Use Function @ref HAL_USART_RegisterCallback() to register a user callback. |
||
114 | Function @ref HAL_USART_RegisterCallback() allows to register following callbacks: |
||
115 | (+) TxHalfCpltCallback : Tx Half Complete Callback. |
||
116 | (+) TxCpltCallback : Tx Complete Callback. |
||
117 | (+) RxHalfCpltCallback : Rx Half Complete Callback. |
||
118 | (+) RxCpltCallback : Rx Complete Callback. |
||
119 | (+) TxRxCpltCallback : Tx Rx Complete Callback. |
||
120 | (+) ErrorCallback : Error Callback. |
||
121 | (+) AbortCpltCallback : Abort Complete Callback. |
||
122 | (+) MspInitCallback : USART MspInit. |
||
123 | (+) MspDeInitCallback : USART MspDeInit. |
||
124 | This function takes as parameters the HAL peripheral handle, the Callback ID |
||
125 | and a pointer to the user callback function. |
||
126 | |||
127 | [..] |
||
128 | Use function @ref HAL_USART_UnRegisterCallback() to reset a callback to the default |
||
129 | weak (surcharged) function. |
||
130 | @ref HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle, |
||
131 | and the Callback ID. |
||
132 | This function allows to reset following callbacks: |
||
133 | (+) TxHalfCpltCallback : Tx Half Complete Callback. |
||
134 | (+) TxCpltCallback : Tx Complete Callback. |
||
135 | (+) RxHalfCpltCallback : Rx Half Complete Callback. |
||
136 | (+) RxCpltCallback : Rx Complete Callback. |
||
137 | (+) TxRxCpltCallback : Tx Rx Complete Callback. |
||
138 | (+) ErrorCallback : Error Callback. |
||
139 | (+) AbortCpltCallback : Abort Complete Callback. |
||
140 | (+) MspInitCallback : USART MspInit. |
||
141 | (+) MspDeInitCallback : USART MspDeInit. |
||
142 | |||
143 | [..] |
||
144 | By default, after the @ref HAL_USART_Init() and when the state is HAL_USART_STATE_RESET |
||
145 | all callbacks are set to the corresponding weak (surcharged) functions: |
||
146 | examples @ref HAL_USART_TxCpltCallback(), @ref HAL_USART_RxHalfCpltCallback(). |
||
147 | Exception done for MspInit and MspDeInit functions that are respectively |
||
148 | reset to the legacy weak (surcharged) functions in the @ref HAL_USART_Init() |
||
149 | and @ref HAL_USART_DeInit() only when these callbacks are null (not registered beforehand). |
||
150 | If not, MspInit or MspDeInit are not null, the @ref HAL_USART_Init() and @ref HAL_USART_DeInit() |
||
151 | keep and use the user MspInit/MspDeInit callbacks (registered beforehand). |
||
152 | |||
153 | [..] |
||
154 | Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only. |
||
155 | Exception done MspInit/MspDeInit that can be registered/unregistered |
||
156 | in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user) |
||
157 | MspInit/DeInit callbacks can be used during the Init/DeInit. |
||
158 | In that case first register the MspInit/MspDeInit user callbacks |
||
159 | using @ref HAL_USART_RegisterCallback() before calling @ref HAL_USART_DeInit() |
||
160 | or @ref HAL_USART_Init() function. |
||
161 | |||
162 | [..] |
||
163 | When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or |
||
164 | not defined, the callback registration feature is not available |
||
165 | and weak (surcharged) callbacks are used. |
||
166 | |||
167 | @endverbatim |
||
168 | [..] |
||
169 | (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written |
||
170 | in the data register is transmitted but is changed by the parity bit. |
||
171 | Depending on the frame length defined by the M bit (8-bits or 9-bits), |
||
172 | the possible USART frame formats are as listed in the following table: |
||
173 | +-------------------------------------------------------------+ |
||
174 | | M bit | PCE bit | USART frame | |
||
175 | |---------------------|---------------------------------------| |
||
176 | | 0 | 0 | | SB | 8 bit data | STB | | |
||
177 | |---------|-----------|---------------------------------------| |
||
178 | | 0 | 1 | | SB | 7 bit data | PB | STB | | |
||
179 | |---------|-----------|---------------------------------------| |
||
180 | | 1 | 0 | | SB | 9 bit data | STB | | |
||
181 | |---------|-----------|---------------------------------------| |
||
182 | | 1 | 1 | | SB | 8 bit data | PB | STB | | |
||
183 | +-------------------------------------------------------------+ |
||
184 | ****************************************************************************** |
||
185 | * @attention |
||
186 | * |
||
187 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
||
188 | * All rights reserved.</center></h2> |
||
189 | * |
||
190 | * This software component is licensed by ST under BSD 3-Clause license, |
||
191 | * the "License"; You may not use this file except in compliance with the |
||
192 | * License. You may obtain a copy of the License at: |
||
193 | * opensource.org/licenses/BSD-3-Clause |
||
194 | * |
||
195 | ****************************************************************************** |
||
196 | */ |
||
197 | |||
198 | /* Includes ------------------------------------------------------------------*/ |
||
199 | #include "stm32f1xx_hal.h" |
||
200 | |||
201 | /** @addtogroup STM32F1xx_HAL_Driver |
||
202 | * @{ |
||
203 | */ |
||
204 | |||
205 | /** @defgroup USART USART |
||
206 | * @brief HAL USART Synchronous module driver |
||
207 | * @{ |
||
208 | */ |
||
209 | #ifdef HAL_USART_MODULE_ENABLED |
||
210 | /* Private typedef -----------------------------------------------------------*/ |
||
211 | /* Private define ------------------------------------------------------------*/ |
||
212 | /** @addtogroup USART_Private_Constants |
||
213 | * @{ |
||
214 | */ |
||
215 | #define DUMMY_DATA 0xFFFFU |
||
216 | #define USART_TIMEOUT_VALUE 22000U |
||
217 | /** |
||
218 | * @} |
||
219 | */ |
||
220 | /* Private macro -------------------------------------------------------------*/ |
||
221 | /* Private variables ---------------------------------------------------------*/ |
||
222 | /* Private function prototypes -----------------------------------------------*/ |
||
223 | /* Private functions ---------------------------------------------------------*/ |
||
224 | /** @addtogroup USART_Private_Functions |
||
225 | * @{ |
||
226 | */ |
||
227 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
228 | void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart); |
||
229 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
230 | static void USART_EndTxTransfer(USART_HandleTypeDef *husart); |
||
231 | static void USART_EndRxTransfer(USART_HandleTypeDef *husart); |
||
232 | static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart); |
||
233 | static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart); |
||
234 | static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart); |
||
235 | static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart); |
||
236 | static void USART_SetConfig(USART_HandleTypeDef *husart); |
||
237 | static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma); |
||
238 | static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma); |
||
239 | static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma); |
||
240 | static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma); |
||
241 | static void USART_DMAError(DMA_HandleTypeDef *hdma); |
||
242 | static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma); |
||
243 | static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma); |
||
244 | static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma); |
||
245 | |||
246 | static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout); |
||
247 | /** |
||
248 | * @} |
||
249 | */ |
||
250 | |||
251 | /* Exported functions --------------------------------------------------------*/ |
||
252 | /** @defgroup USART_Exported_Functions USART Exported Functions |
||
253 | * @{ |
||
254 | */ |
||
255 | |||
256 | /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions |
||
257 | * @brief Initialization and Configuration functions |
||
258 | * |
||
259 | @verbatim |
||
260 | ============================================================================== |
||
261 | ##### Initialization and Configuration functions ##### |
||
262 | ============================================================================== |
||
263 | [..] |
||
264 | This subsection provides a set of functions allowing to initialize the USART |
||
265 | in asynchronous and in synchronous modes. |
||
266 | (+) For the asynchronous mode only these parameters can be configured: |
||
267 | (++) Baud Rate |
||
268 | (++) Word Length |
||
269 | (++) Stop Bit |
||
270 | (++) Parity: If the parity is enabled, then the MSB bit of the data written |
||
271 | in the data register is transmitted but is changed by the parity bit. |
||
272 | Depending on the frame length defined by the M bit (8-bits or 9-bits), |
||
273 | please refer to Reference manual for possible USART frame formats. |
||
274 | (++) USART polarity |
||
275 | (++) USART phase |
||
276 | (++) USART LastBit |
||
277 | (++) Receiver/transmitter modes |
||
278 | |||
279 | [..] |
||
280 | The HAL_USART_Init() function follows the USART synchronous configuration |
||
281 | procedures (details for the procedures are available in reference manuals |
||
282 | (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)). |
||
283 | |||
284 | @endverbatim |
||
285 | * @{ |
||
286 | */ |
||
287 | |||
288 | /** |
||
289 | * @brief Initialize the USART mode according to the specified |
||
290 | * parameters in the USART_InitTypeDef and initialize the associated handle. |
||
291 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
292 | * the configuration information for the specified USART module. |
||
293 | * @retval HAL status |
||
294 | */ |
||
295 | HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart) |
||
296 | { |
||
297 | /* Check the USART handle allocation */ |
||
298 | if (husart == NULL) |
||
299 | { |
||
300 | return HAL_ERROR; |
||
301 | } |
||
302 | |||
303 | /* Check the parameters */ |
||
304 | assert_param(IS_USART_INSTANCE(husart->Instance)); |
||
305 | |||
306 | if (husart->State == HAL_USART_STATE_RESET) |
||
307 | { |
||
308 | /* Allocate lock resource and initialize it */ |
||
309 | husart->Lock = HAL_UNLOCKED; |
||
310 | |||
311 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
312 | USART_InitCallbacksToDefault(husart); |
||
313 | |||
314 | if (husart->MspInitCallback == NULL) |
||
315 | { |
||
316 | husart->MspInitCallback = HAL_USART_MspInit; |
||
317 | } |
||
318 | |||
319 | /* Init the low level hardware */ |
||
320 | husart->MspInitCallback(husart); |
||
321 | #else |
||
322 | /* Init the low level hardware : GPIO, CLOCK */ |
||
323 | HAL_USART_MspInit(husart); |
||
324 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
325 | } |
||
326 | |||
327 | husart->State = HAL_USART_STATE_BUSY; |
||
328 | |||
329 | /* Set the USART Communication parameters */ |
||
330 | USART_SetConfig(husart); |
||
331 | |||
332 | /* In USART mode, the following bits must be kept cleared: |
||
333 | - LINEN bit in the USART_CR2 register |
||
334 | - HDSEL, SCEN and IREN bits in the USART_CR3 register */ |
||
335 | CLEAR_BIT(husart->Instance->CR2, USART_CR2_LINEN); |
||
336 | CLEAR_BIT(husart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); |
||
337 | |||
338 | /* Enable the Peripheral */ |
||
339 | __HAL_USART_ENABLE(husart); |
||
340 | |||
341 | /* Initialize the USART state */ |
||
342 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
343 | husart->State = HAL_USART_STATE_READY; |
||
344 | |||
345 | return HAL_OK; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * @brief DeInitializes the USART peripheral. |
||
350 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
351 | * the configuration information for the specified USART module. |
||
352 | * @retval HAL status |
||
353 | */ |
||
354 | HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart) |
||
355 | { |
||
356 | /* Check the USART handle allocation */ |
||
357 | if (husart == NULL) |
||
358 | { |
||
359 | return HAL_ERROR; |
||
360 | } |
||
361 | |||
362 | /* Check the parameters */ |
||
363 | assert_param(IS_USART_INSTANCE(husart->Instance)); |
||
364 | |||
365 | husart->State = HAL_USART_STATE_BUSY; |
||
366 | |||
367 | /* Disable the Peripheral */ |
||
368 | __HAL_USART_DISABLE(husart); |
||
369 | |||
370 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
371 | if (husart->MspDeInitCallback == NULL) |
||
372 | { |
||
373 | husart->MspDeInitCallback = HAL_USART_MspDeInit; |
||
374 | } |
||
375 | /* DeInit the low level hardware */ |
||
376 | husart->MspDeInitCallback(husart); |
||
377 | #else |
||
378 | /* DeInit the low level hardware */ |
||
379 | HAL_USART_MspDeInit(husart); |
||
380 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
381 | |||
382 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
383 | husart->State = HAL_USART_STATE_RESET; |
||
384 | |||
385 | /* Release Lock */ |
||
386 | __HAL_UNLOCK(husart); |
||
387 | |||
388 | return HAL_OK; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @brief USART MSP Init. |
||
393 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
394 | * the configuration information for the specified USART module. |
||
395 | * @retval None |
||
396 | */ |
||
397 | __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart) |
||
398 | { |
||
399 | /* Prevent unused argument(s) compilation warning */ |
||
400 | UNUSED(husart); |
||
401 | /* NOTE: This function should not be modified, when the callback is needed, |
||
402 | the HAL_USART_MspInit could be implemented in the user file |
||
403 | */ |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * @brief USART MSP DeInit. |
||
408 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
409 | * the configuration information for the specified USART module. |
||
410 | * @retval None |
||
411 | */ |
||
412 | __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart) |
||
413 | { |
||
414 | /* Prevent unused argument(s) compilation warning */ |
||
415 | UNUSED(husart); |
||
416 | /* NOTE: This function should not be modified, when the callback is needed, |
||
417 | the HAL_USART_MspDeInit could be implemented in the user file |
||
418 | */ |
||
419 | } |
||
420 | |||
421 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
422 | /** |
||
423 | * @brief Register a User USART Callback |
||
424 | * To be used instead of the weak predefined callback |
||
425 | * @param husart usart handle |
||
426 | * @param CallbackID ID of the callback to be registered |
||
427 | * This parameter can be one of the following values: |
||
428 | * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID |
||
429 | * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID |
||
430 | * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID |
||
431 | * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID |
||
432 | * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID |
||
433 | * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID |
||
434 | * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID |
||
435 | * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID |
||
436 | * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID |
||
437 | * @param pCallback pointer to the Callback function |
||
438 | * @retval HAL status |
||
439 | + */ |
||
440 | HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID, pUSART_CallbackTypeDef pCallback) |
||
441 | { |
||
442 | HAL_StatusTypeDef status = HAL_OK; |
||
443 | |||
444 | if (pCallback == NULL) |
||
445 | { |
||
446 | /* Update the error code */ |
||
447 | husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; |
||
448 | |||
449 | return HAL_ERROR; |
||
450 | } |
||
451 | /* Process locked */ |
||
452 | __HAL_LOCK(husart); |
||
453 | |||
454 | if (husart->State == HAL_USART_STATE_READY) |
||
455 | { |
||
456 | switch (CallbackID) |
||
457 | { |
||
458 | case HAL_USART_TX_HALFCOMPLETE_CB_ID : |
||
459 | husart->TxHalfCpltCallback = pCallback; |
||
460 | break; |
||
461 | |||
462 | case HAL_USART_TX_COMPLETE_CB_ID : |
||
463 | husart->TxCpltCallback = pCallback; |
||
464 | break; |
||
465 | |||
466 | case HAL_USART_RX_HALFCOMPLETE_CB_ID : |
||
467 | husart->RxHalfCpltCallback = pCallback; |
||
468 | break; |
||
469 | |||
470 | case HAL_USART_RX_COMPLETE_CB_ID : |
||
471 | husart->RxCpltCallback = pCallback; |
||
472 | break; |
||
473 | |||
474 | case HAL_USART_TX_RX_COMPLETE_CB_ID : |
||
475 | husart->TxRxCpltCallback = pCallback; |
||
476 | break; |
||
477 | |||
478 | case HAL_USART_ERROR_CB_ID : |
||
479 | husart->ErrorCallback = pCallback; |
||
480 | break; |
||
481 | |||
482 | case HAL_USART_ABORT_COMPLETE_CB_ID : |
||
483 | husart->AbortCpltCallback = pCallback; |
||
484 | break; |
||
485 | |||
486 | case HAL_USART_MSPINIT_CB_ID : |
||
487 | husart->MspInitCallback = pCallback; |
||
488 | break; |
||
489 | |||
490 | case HAL_USART_MSPDEINIT_CB_ID : |
||
491 | husart->MspDeInitCallback = pCallback; |
||
492 | break; |
||
493 | |||
494 | default : |
||
495 | /* Update the error code */ |
||
496 | husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; |
||
497 | |||
498 | /* Return error status */ |
||
499 | status = HAL_ERROR; |
||
500 | break; |
||
501 | } |
||
502 | } |
||
503 | else if (husart->State == HAL_USART_STATE_RESET) |
||
504 | { |
||
505 | switch (CallbackID) |
||
506 | { |
||
507 | case HAL_USART_MSPINIT_CB_ID : |
||
508 | husart->MspInitCallback = pCallback; |
||
509 | break; |
||
510 | |||
511 | case HAL_USART_MSPDEINIT_CB_ID : |
||
512 | husart->MspDeInitCallback = pCallback; |
||
513 | break; |
||
514 | |||
515 | default : |
||
516 | /* Update the error code */ |
||
517 | husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; |
||
518 | |||
519 | /* Return error status */ |
||
520 | status = HAL_ERROR; |
||
521 | break; |
||
522 | } |
||
523 | } |
||
524 | else |
||
525 | { |
||
526 | /* Update the error code */ |
||
527 | husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; |
||
528 | |||
529 | /* Return error status */ |
||
530 | status = HAL_ERROR; |
||
531 | } |
||
532 | |||
533 | /* Release Lock */ |
||
534 | __HAL_UNLOCK(husart); |
||
535 | |||
536 | return status; |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * @brief Unregister an UART Callback |
||
541 | * UART callaback is redirected to the weak predefined callback |
||
542 | * @param husart uart handle |
||
543 | * @param CallbackID ID of the callback to be unregistered |
||
544 | * This parameter can be one of the following values: |
||
545 | * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID |
||
546 | * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID |
||
547 | * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID |
||
548 | * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID |
||
549 | * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID |
||
550 | * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID |
||
551 | * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID |
||
552 | * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID |
||
553 | * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID |
||
554 | * @retval HAL status |
||
555 | */ |
||
556 | HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID) |
||
557 | { |
||
558 | HAL_StatusTypeDef status = HAL_OK; |
||
559 | |||
560 | /* Process locked */ |
||
561 | __HAL_LOCK(husart); |
||
562 | |||
563 | if (husart->State == HAL_USART_STATE_READY) |
||
564 | { |
||
565 | switch (CallbackID) |
||
566 | { |
||
567 | case HAL_USART_TX_HALFCOMPLETE_CB_ID : |
||
568 | husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ |
||
569 | break; |
||
570 | |||
571 | case HAL_USART_TX_COMPLETE_CB_ID : |
||
572 | husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */ |
||
573 | break; |
||
574 | |||
575 | case HAL_USART_RX_HALFCOMPLETE_CB_ID : |
||
576 | husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ |
||
577 | break; |
||
578 | |||
579 | case HAL_USART_RX_COMPLETE_CB_ID : |
||
580 | husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */ |
||
581 | break; |
||
582 | |||
583 | case HAL_USART_TX_RX_COMPLETE_CB_ID : |
||
584 | husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */ |
||
585 | break; |
||
586 | |||
587 | case HAL_USART_ERROR_CB_ID : |
||
588 | husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */ |
||
589 | break; |
||
590 | |||
591 | case HAL_USART_ABORT_COMPLETE_CB_ID : |
||
592 | husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ |
||
593 | break; |
||
594 | |||
595 | case HAL_USART_MSPINIT_CB_ID : |
||
596 | husart->MspInitCallback = HAL_USART_MspInit; /* Legacy weak MspInitCallback */ |
||
597 | break; |
||
598 | |||
599 | case HAL_USART_MSPDEINIT_CB_ID : |
||
600 | husart->MspDeInitCallback = HAL_USART_MspDeInit; /* Legacy weak MspDeInitCallback */ |
||
601 | break; |
||
602 | |||
603 | default : |
||
604 | /* Update the error code */ |
||
605 | husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; |
||
606 | |||
607 | /* Return error status */ |
||
608 | status = HAL_ERROR; |
||
609 | break; |
||
610 | } |
||
611 | } |
||
612 | else if (husart->State == HAL_USART_STATE_RESET) |
||
613 | { |
||
614 | switch (CallbackID) |
||
615 | { |
||
616 | case HAL_USART_MSPINIT_CB_ID : |
||
617 | husart->MspInitCallback = HAL_USART_MspInit; |
||
618 | break; |
||
619 | |||
620 | case HAL_USART_MSPDEINIT_CB_ID : |
||
621 | husart->MspDeInitCallback = HAL_USART_MspDeInit; |
||
622 | break; |
||
623 | |||
624 | default : |
||
625 | /* Update the error code */ |
||
626 | husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; |
||
627 | |||
628 | /* Return error status */ |
||
629 | status = HAL_ERROR; |
||
630 | break; |
||
631 | } |
||
632 | } |
||
633 | else |
||
634 | { |
||
635 | /* Update the error code */ |
||
636 | husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; |
||
637 | |||
638 | /* Return error status */ |
||
639 | status = HAL_ERROR; |
||
640 | } |
||
641 | |||
642 | /* Release Lock */ |
||
643 | __HAL_UNLOCK(husart); |
||
644 | |||
645 | return status; |
||
646 | } |
||
647 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
648 | |||
649 | /** |
||
650 | * @} |
||
651 | */ |
||
652 | |||
653 | /** @defgroup USART_Exported_Functions_Group2 IO operation functions |
||
654 | * @brief USART Transmit and Receive functions |
||
655 | * |
||
656 | @verbatim |
||
657 | ============================================================================== |
||
658 | ##### IO operation functions ##### |
||
659 | ============================================================================== |
||
660 | [..] |
||
661 | This subsection provides a set of functions allowing to manage the USART synchronous |
||
662 | data transfers. |
||
663 | |||
664 | [..] |
||
665 | The USART supports master mode only: it cannot receive or send data related to an input |
||
666 | clock (SCLK is always an output). |
||
667 | |||
668 | (#) There are two modes of transfer: |
||
669 | (++) Blocking mode: The communication is performed in polling mode. |
||
670 | The HAL status of all data processing is returned by the same function |
||
671 | after finishing transfer. |
||
672 | (++) No-Blocking mode: The communication is performed using Interrupts |
||
673 | or DMA, These API's return the HAL status. |
||
674 | The end of the data processing will be indicated through the |
||
675 | dedicated USART IRQ when using Interrupt mode or the DMA IRQ when |
||
676 | using DMA mode. |
||
677 | The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() |
||
678 | user callbacks |
||
679 | will be executed respectively at the end of the transmit or Receive process |
||
680 | The HAL_USART_ErrorCallback() user callback will be executed when a communication |
||
681 | error is detected |
||
682 | |||
683 | (#) Blocking mode APIs are : |
||
684 | (++) HAL_USART_Transmit() in simplex mode |
||
685 | (++) HAL_USART_Receive() in full duplex receive only |
||
686 | (++) HAL_USART_TransmitReceive() in full duplex mode |
||
687 | |||
688 | (#) Non Blocking mode APIs with Interrupt are : |
||
689 | (++) HAL_USART_Transmit_IT()in simplex mode |
||
690 | (++) HAL_USART_Receive_IT() in full duplex receive only |
||
691 | (++) HAL_USART_TransmitReceive_IT() in full duplex mode |
||
692 | (++) HAL_USART_IRQHandler() |
||
693 | |||
694 | (#) Non Blocking mode functions with DMA are : |
||
695 | (++) HAL_USART_Transmit_DMA()in simplex mode |
||
696 | (++) HAL_USART_Receive_DMA() in full duplex receive only |
||
697 | (++) HAL_USART_TransmitReceive_DMA() in full duplex mode |
||
698 | (++) HAL_USART_DMAPause() |
||
699 | (++) HAL_USART_DMAResume() |
||
700 | (++) HAL_USART_DMAStop() |
||
701 | |||
702 | (#) A set of Transfer Complete Callbacks are provided in non Blocking mode: |
||
703 | (++) HAL_USART_TxHalfCpltCallback() |
||
704 | (++) HAL_USART_TxCpltCallback() |
||
705 | (++) HAL_USART_RxHalfCpltCallback() |
||
706 | (++) HAL_USART_RxCpltCallback() |
||
707 | (++) HAL_USART_ErrorCallback() |
||
708 | (++) HAL_USART_TxRxCpltCallback() |
||
709 | |||
710 | (#) Non-Blocking mode transfers could be aborted using Abort API's : |
||
711 | (++) HAL_USART_Abort() |
||
712 | (++) HAL_USART_Abort_IT() |
||
713 | |||
714 | (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided: |
||
715 | (++) HAL_USART_AbortCpltCallback() |
||
716 | |||
717 | (#) In Non-Blocking mode transfers, possible errors are split into 2 categories. |
||
718 | Errors are handled as follows : |
||
719 | (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is |
||
720 | to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception . |
||
721 | Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type, |
||
722 | and HAL_USART_ErrorCallback() user callback is executed. Transfer is kept ongoing on USART side. |
||
723 | If user wants to abort it, Abort services should be called by user. |
||
724 | (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted. |
||
725 | This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode. |
||
726 | Error code is set to allow user to identify error type, and HAL_USART_ErrorCallback() user callback is executed. |
||
727 | |||
728 | @endverbatim |
||
729 | * @{ |
||
730 | */ |
||
731 | |||
732 | /** |
||
733 | * @brief Simplex Send an amount of data in blocking mode. |
||
734 | * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), |
||
735 | * the sent data is handled as a set of u16. In this case, Size must indicate the number |
||
736 | * of u16 provided through pTxData. |
||
737 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
738 | * the configuration information for the specified USART module. |
||
739 | * @param pTxData Pointer to data buffer (u8 or u16 data elements). |
||
740 | * @param Size Amount of data elements (u8 or u16) to be sent. |
||
741 | * @param Timeout Timeout duration. |
||
742 | * @retval HAL status |
||
743 | */ |
||
744 | HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout) |
||
745 | { |
||
746 | uint16_t *tmp; |
||
747 | uint32_t tickstart = 0U; |
||
748 | |||
749 | if (husart->State == HAL_USART_STATE_READY) |
||
750 | { |
||
751 | if ((pTxData == NULL) || (Size == 0)) |
||
752 | { |
||
753 | return HAL_ERROR; |
||
754 | } |
||
755 | |||
756 | /* Process Locked */ |
||
757 | __HAL_LOCK(husart); |
||
758 | |||
759 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
760 | husart->State = HAL_USART_STATE_BUSY_TX; |
||
761 | |||
762 | /* Init tickstart for timeout management */ |
||
763 | tickstart = HAL_GetTick(); |
||
764 | |||
765 | husart->TxXferSize = Size; |
||
766 | husart->TxXferCount = Size; |
||
767 | while (husart->TxXferCount > 0U) |
||
768 | { |
||
769 | husart->TxXferCount--; |
||
770 | if (husart->Init.WordLength == USART_WORDLENGTH_9B) |
||
771 | { |
||
772 | /* Wait for TC flag in order to write data in DR */ |
||
773 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
||
774 | { |
||
775 | return HAL_TIMEOUT; |
||
776 | } |
||
777 | tmp = (uint16_t *) pTxData; |
||
778 | husart->Instance->DR = (*tmp & (uint16_t)0x01FF); |
||
779 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
780 | { |
||
781 | pTxData += 2U; |
||
782 | } |
||
783 | else |
||
784 | { |
||
785 | pTxData += 1U; |
||
786 | } |
||
787 | } |
||
788 | else |
||
789 | { |
||
790 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
||
791 | { |
||
792 | return HAL_TIMEOUT; |
||
793 | } |
||
794 | husart->Instance->DR = (*pTxData++ & (uint8_t)0xFF); |
||
795 | } |
||
796 | } |
||
797 | |||
798 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK) |
||
799 | { |
||
800 | return HAL_TIMEOUT; |
||
801 | } |
||
802 | |||
803 | husart->State = HAL_USART_STATE_READY; |
||
804 | |||
805 | /* Process Unlocked */ |
||
806 | __HAL_UNLOCK(husart); |
||
807 | |||
808 | return HAL_OK; |
||
809 | } |
||
810 | else |
||
811 | { |
||
812 | return HAL_BUSY; |
||
813 | } |
||
814 | } |
||
815 | |||
816 | /** |
||
817 | * @brief Full-Duplex Receive an amount of data in blocking mode. |
||
818 | * @note To receive synchronous data, dummy data are simultaneously transmitted. |
||
819 | * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), |
||
820 | * the received data is handled as a set of u16. In this case, Size must indicate the number |
||
821 | * of u16 available through pRxData. |
||
822 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
823 | * the configuration information for the specified USART module. |
||
824 | * @param pRxData Pointer to data buffer (u8 or u16 data elements). |
||
825 | * @param Size Amount of data elements (u8 or u16) to be received. |
||
826 | * @param Timeout Timeout duration. |
||
827 | * @retval HAL status |
||
828 | */ |
||
829 | HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) |
||
830 | { |
||
831 | uint16_t *tmp; |
||
832 | uint32_t tickstart = 0U; |
||
833 | |||
834 | if (husart->State == HAL_USART_STATE_READY) |
||
835 | { |
||
836 | if ((pRxData == NULL) || (Size == 0)) |
||
837 | { |
||
838 | return HAL_ERROR; |
||
839 | } |
||
840 | /* Process Locked */ |
||
841 | __HAL_LOCK(husart); |
||
842 | |||
843 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
844 | husart->State = HAL_USART_STATE_BUSY_RX; |
||
845 | |||
846 | /* Init tickstart for timeout management */ |
||
847 | tickstart = HAL_GetTick(); |
||
848 | |||
849 | husart->RxXferSize = Size; |
||
850 | husart->RxXferCount = Size; |
||
851 | /* Check the remain data to be received */ |
||
852 | while (husart->RxXferCount > 0U) |
||
853 | { |
||
854 | husart->RxXferCount--; |
||
855 | if (husart->Init.WordLength == USART_WORDLENGTH_9B) |
||
856 | { |
||
857 | /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */ |
||
858 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
||
859 | { |
||
860 | return HAL_TIMEOUT; |
||
861 | } |
||
862 | /* Send dummy byte in order to generate clock */ |
||
863 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF); |
||
864 | |||
865 | /* Wait for RXNE Flag */ |
||
866 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) |
||
867 | { |
||
868 | return HAL_TIMEOUT; |
||
869 | } |
||
870 | tmp = (uint16_t *) pRxData ; |
||
871 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
872 | { |
||
873 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
||
874 | pRxData += 2U; |
||
875 | } |
||
876 | else |
||
877 | { |
||
878 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
||
879 | pRxData += 1U; |
||
880 | } |
||
881 | } |
||
882 | else |
||
883 | { |
||
884 | /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */ |
||
885 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
||
886 | { |
||
887 | return HAL_TIMEOUT; |
||
888 | } |
||
889 | |||
890 | /* Send Dummy Byte in order to generate clock */ |
||
891 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF); |
||
892 | |||
893 | /* Wait until RXNE flag is set to receive the byte */ |
||
894 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) |
||
895 | { |
||
896 | return HAL_TIMEOUT; |
||
897 | } |
||
898 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
899 | { |
||
900 | /* Receive data */ |
||
901 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
||
902 | } |
||
903 | else |
||
904 | { |
||
905 | /* Receive data */ |
||
906 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
||
907 | } |
||
908 | |||
909 | } |
||
910 | } |
||
911 | |||
912 | husart->State = HAL_USART_STATE_READY; |
||
913 | |||
914 | /* Process Unlocked */ |
||
915 | __HAL_UNLOCK(husart); |
||
916 | |||
917 | return HAL_OK; |
||
918 | } |
||
919 | else |
||
920 | { |
||
921 | return HAL_BUSY; |
||
922 | } |
||
923 | } |
||
924 | |||
925 | /** |
||
926 | * @brief Full-Duplex Send and Receive an amount of data in full-duplex mode (blocking mode). |
||
927 | * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), |
||
928 | * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number |
||
929 | * of u16 available through pTxData and through pRxData. |
||
930 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
931 | * the configuration information for the specified USART module. |
||
932 | * @param pTxData Pointer to TX data buffer (u8 or u16 data elements). |
||
933 | * @param pRxData Pointer to RX data buffer (u8 or u16 data elements). |
||
934 | * @param Size Amount of data elements (u8 or u16) to be sent (same amount to be received). |
||
935 | * @param Timeout Timeout duration |
||
936 | * @retval HAL status |
||
937 | */ |
||
938 | HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) |
||
939 | { |
||
940 | uint16_t *tmp; |
||
941 | uint32_t tickstart = 0U; |
||
942 | |||
943 | if (husart->State == HAL_USART_STATE_READY) |
||
944 | { |
||
945 | if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0)) |
||
946 | { |
||
947 | return HAL_ERROR; |
||
948 | } |
||
949 | /* Process Locked */ |
||
950 | __HAL_LOCK(husart); |
||
951 | |||
952 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
953 | husart->State = HAL_USART_STATE_BUSY_RX; |
||
954 | |||
955 | /* Init tickstart for timeout management */ |
||
956 | tickstart = HAL_GetTick(); |
||
957 | |||
958 | husart->RxXferSize = Size; |
||
959 | husart->TxXferSize = Size; |
||
960 | husart->TxXferCount = Size; |
||
961 | husart->RxXferCount = Size; |
||
962 | |||
963 | /* Check the remain data to be received */ |
||
964 | while (husart->TxXferCount > 0U) |
||
965 | { |
||
966 | husart->TxXferCount--; |
||
967 | husart->RxXferCount--; |
||
968 | if (husart->Init.WordLength == USART_WORDLENGTH_9B) |
||
969 | { |
||
970 | /* Wait for TC flag in order to write data in DR */ |
||
971 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
||
972 | { |
||
973 | return HAL_TIMEOUT; |
||
974 | } |
||
975 | tmp = (uint16_t *) pTxData; |
||
976 | husart->Instance->DR = (*tmp & (uint16_t)0x01FF); |
||
977 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
978 | { |
||
979 | pTxData += 2U; |
||
980 | } |
||
981 | else |
||
982 | { |
||
983 | pTxData += 1U; |
||
984 | } |
||
985 | |||
986 | /* Wait for RXNE Flag */ |
||
987 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) |
||
988 | { |
||
989 | return HAL_TIMEOUT; |
||
990 | } |
||
991 | tmp = (uint16_t *) pRxData ; |
||
992 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
993 | { |
||
994 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
||
995 | pRxData += 2U; |
||
996 | } |
||
997 | else |
||
998 | { |
||
999 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
||
1000 | pRxData += 1U; |
||
1001 | } |
||
1002 | } |
||
1003 | else |
||
1004 | { |
||
1005 | /* Wait for TC flag in order to write data in DR */ |
||
1006 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
||
1007 | { |
||
1008 | return HAL_TIMEOUT; |
||
1009 | } |
||
1010 | husart->Instance->DR = (*pTxData++ & (uint8_t)0x00FF); |
||
1011 | |||
1012 | /* Wait for RXNE Flag */ |
||
1013 | if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) |
||
1014 | { |
||
1015 | return HAL_TIMEOUT; |
||
1016 | } |
||
1017 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
1018 | { |
||
1019 | /* Receive data */ |
||
1020 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
||
1021 | } |
||
1022 | else |
||
1023 | { |
||
1024 | /* Receive data */ |
||
1025 | *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
||
1026 | } |
||
1027 | } |
||
1028 | } |
||
1029 | |||
1030 | husart->State = HAL_USART_STATE_READY; |
||
1031 | |||
1032 | /* Process Unlocked */ |
||
1033 | __HAL_UNLOCK(husart); |
||
1034 | |||
1035 | return HAL_OK; |
||
1036 | } |
||
1037 | else |
||
1038 | { |
||
1039 | return HAL_BUSY; |
||
1040 | } |
||
1041 | } |
||
1042 | |||
1043 | /** |
||
1044 | * @brief Simplex Send an amount of data in non-blocking mode. |
||
1045 | * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), |
||
1046 | * the sent data is handled as a set of u16. In this case, Size must indicate the number |
||
1047 | * of u16 provided through pTxData. |
||
1048 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1049 | * the configuration information for the specified USART module. |
||
1050 | * @param pTxData Pointer to data buffer (u8 or u16 data elements). |
||
1051 | * @param Size Amount of data elements (u8 or u16) to be sent. |
||
1052 | * @retval HAL status |
||
1053 | * @note The USART errors are not managed to avoid the overrun error. |
||
1054 | */ |
||
1055 | HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size) |
||
1056 | { |
||
1057 | if (husart->State == HAL_USART_STATE_READY) |
||
1058 | { |
||
1059 | if ((pTxData == NULL) || (Size == 0)) |
||
1060 | { |
||
1061 | return HAL_ERROR; |
||
1062 | } |
||
1063 | |||
1064 | /* Process Locked */ |
||
1065 | __HAL_LOCK(husart); |
||
1066 | |||
1067 | husart->pTxBuffPtr = pTxData; |
||
1068 | husart->TxXferSize = Size; |
||
1069 | husart->TxXferCount = Size; |
||
1070 | |||
1071 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
1072 | husart->State = HAL_USART_STATE_BUSY_TX; |
||
1073 | |||
1074 | /* The USART Error Interrupts: (Frame error, Noise error, Overrun error) |
||
1075 | are not managed by the USART transmit process to avoid the overrun interrupt |
||
1076 | when the USART mode is configured for transmit and receive "USART_MODE_TX_RX" |
||
1077 | to benefit for the frame error and noise interrupts the USART mode should be |
||
1078 | configured only for transmit "USART_MODE_TX" |
||
1079 | The __HAL_USART_ENABLE_IT(husart, USART_IT_ERR) can be used to enable the Frame error, |
||
1080 | Noise error interrupt */ |
||
1081 | |||
1082 | /* Process Unlocked */ |
||
1083 | __HAL_UNLOCK(husart); |
||
1084 | |||
1085 | /* Enable the USART Transmit Data Register Empty Interrupt */ |
||
1086 | SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE); |
||
1087 | |||
1088 | return HAL_OK; |
||
1089 | } |
||
1090 | else |
||
1091 | { |
||
1092 | return HAL_BUSY; |
||
1093 | } |
||
1094 | } |
||
1095 | |||
1096 | /** |
||
1097 | * @brief Simplex Receive an amount of data in non-blocking mode. |
||
1098 | * @note To receive synchronous data, dummy data are simultaneously transmitted. |
||
1099 | * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), |
||
1100 | * the received data is handled as a set of u16. In this case, Size must indicate the number |
||
1101 | * of u16 available through pRxData. |
||
1102 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1103 | * the configuration information for the specified USART module. |
||
1104 | * @param pRxData Pointer to data buffer (u8 or u16 data elements). |
||
1105 | * @param Size Amount of data elements (u8 or u16) to be received. |
||
1106 | * @retval HAL status |
||
1107 | */ |
||
1108 | HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size) |
||
1109 | { |
||
1110 | if (husart->State == HAL_USART_STATE_READY) |
||
1111 | { |
||
1112 | if ((pRxData == NULL) || (Size == 0)) |
||
1113 | { |
||
1114 | return HAL_ERROR; |
||
1115 | } |
||
1116 | /* Process Locked */ |
||
1117 | __HAL_LOCK(husart); |
||
1118 | |||
1119 | husart->pRxBuffPtr = pRxData; |
||
1120 | husart->RxXferSize = Size; |
||
1121 | husart->RxXferCount = Size; |
||
1122 | |||
1123 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
1124 | husart->State = HAL_USART_STATE_BUSY_RX; |
||
1125 | |||
1126 | /* Process Unlocked */ |
||
1127 | __HAL_UNLOCK(husart); |
||
1128 | |||
1129 | /* Enable the USART Parity Error and Data Register not empty Interrupts */ |
||
1130 | SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE); |
||
1131 | |||
1132 | /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
||
1133 | SET_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
1134 | |||
1135 | /* Send dummy byte in order to generate the clock for the slave to send data */ |
||
1136 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF); |
||
1137 | |||
1138 | return HAL_OK; |
||
1139 | } |
||
1140 | else |
||
1141 | { |
||
1142 | return HAL_BUSY; |
||
1143 | } |
||
1144 | } |
||
1145 | |||
1146 | /** |
||
1147 | * @brief Full-Duplex Send and Receive an amount of data in full-duplex mode (non-blocking). |
||
1148 | * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), |
||
1149 | * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number |
||
1150 | * of u16 available through pTxData and through pRxData. |
||
1151 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1152 | * the configuration information for the specified USART module. |
||
1153 | * @param pTxData Pointer to TX data buffer (u8 or u16 data elements). |
||
1154 | * @param pRxData Pointer to RX data buffer (u8 or u16 data elements). |
||
1155 | * @param Size Amount of data elements (u8 or u16) to be sent (same amount to be received). |
||
1156 | * @retval HAL status |
||
1157 | */ |
||
1158 | HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) |
||
1159 | { |
||
1160 | if (husart->State == HAL_USART_STATE_READY) |
||
1161 | { |
||
1162 | if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0)) |
||
1163 | { |
||
1164 | return HAL_ERROR; |
||
1165 | } |
||
1166 | /* Process Locked */ |
||
1167 | __HAL_LOCK(husart); |
||
1168 | |||
1169 | husart->pRxBuffPtr = pRxData; |
||
1170 | husart->RxXferSize = Size; |
||
1171 | husart->RxXferCount = Size; |
||
1172 | husart->pTxBuffPtr = pTxData; |
||
1173 | husart->TxXferSize = Size; |
||
1174 | husart->TxXferCount = Size; |
||
1175 | |||
1176 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
1177 | husart->State = HAL_USART_STATE_BUSY_TX_RX; |
||
1178 | |||
1179 | /* Process Unlocked */ |
||
1180 | __HAL_UNLOCK(husart); |
||
1181 | |||
1182 | /* Enable the USART Data Register not empty Interrupt */ |
||
1183 | SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE); |
||
1184 | |||
1185 | /* Enable the USART Parity Error Interrupt */ |
||
1186 | SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
||
1187 | |||
1188 | /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
||
1189 | SET_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
1190 | |||
1191 | /* Enable the USART Transmit Data Register Empty Interrupt */ |
||
1192 | SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE); |
||
1193 | |||
1194 | return HAL_OK; |
||
1195 | } |
||
1196 | else |
||
1197 | { |
||
1198 | return HAL_BUSY; |
||
1199 | } |
||
1200 | } |
||
1201 | |||
1202 | /** |
||
1203 | * @brief Simplex Send an amount of data in DMA mode. |
||
1204 | * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), |
||
1205 | * the sent data is handled as a set of u16. In this case, Size must indicate the number |
||
1206 | * of u16 provided through pTxData. |
||
1207 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1208 | * the configuration information for the specified USART module. |
||
1209 | * @param pTxData Pointer to data buffer (u8 or u16 data elements). |
||
1210 | * @param Size Amount of data elements (u8 or u16) to be sent. |
||
1211 | * @retval HAL status |
||
1212 | */ |
||
1213 | HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size) |
||
1214 | { |
||
1215 | uint32_t *tmp; |
||
1216 | |||
1217 | if (husart->State == HAL_USART_STATE_READY) |
||
1218 | { |
||
1219 | if ((pTxData == NULL) || (Size == 0)) |
||
1220 | { |
||
1221 | return HAL_ERROR; |
||
1222 | } |
||
1223 | /* Process Locked */ |
||
1224 | __HAL_LOCK(husart); |
||
1225 | |||
1226 | husart->pTxBuffPtr = pTxData; |
||
1227 | husart->TxXferSize = Size; |
||
1228 | husart->TxXferCount = Size; |
||
1229 | |||
1230 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
1231 | husart->State = HAL_USART_STATE_BUSY_TX; |
||
1232 | |||
1233 | /* Set the USART DMA transfer complete callback */ |
||
1234 | husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt; |
||
1235 | |||
1236 | /* Set the USART DMA Half transfer complete callback */ |
||
1237 | husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt; |
||
1238 | |||
1239 | /* Set the DMA error callback */ |
||
1240 | husart->hdmatx->XferErrorCallback = USART_DMAError; |
||
1241 | |||
1242 | /* Set the DMA abort callback */ |
||
1243 | husart->hdmatx->XferAbortCallback = NULL; |
||
1244 | |||
1245 | /* Enable the USART transmit DMA channel */ |
||
1246 | tmp = (uint32_t *)&pTxData; |
||
1247 | HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size); |
||
1248 | |||
1249 | /* Clear the TC flag in the SR register by writing 0 to it */ |
||
1250 | __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC); |
||
1251 | |||
1252 | /* Process Unlocked */ |
||
1253 | __HAL_UNLOCK(husart); |
||
1254 | |||
1255 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
||
1256 | in the USART CR3 register */ |
||
1257 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
||
1258 | |||
1259 | return HAL_OK; |
||
1260 | } |
||
1261 | else |
||
1262 | { |
||
1263 | return HAL_BUSY; |
||
1264 | } |
||
1265 | } |
||
1266 | |||
1267 | /** |
||
1268 | * @brief Full-Duplex Receive an amount of data in DMA mode. |
||
1269 | * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), |
||
1270 | * the received data is handled as a set of u16. In this case, Size must indicate the number |
||
1271 | * of u16 available through pRxData. |
||
1272 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1273 | * the configuration information for the specified USART module. |
||
1274 | * @param pRxData Pointer to data buffer (u8 or u16 data elements). |
||
1275 | * @param Size Amount of data elements (u8 or u16) to be received. |
||
1276 | * @retval HAL status |
||
1277 | * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave. |
||
1278 | * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit. |
||
1279 | */ |
||
1280 | HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size) |
||
1281 | { |
||
1282 | uint32_t *tmp; |
||
1283 | |||
1284 | if (husart->State == HAL_USART_STATE_READY) |
||
1285 | { |
||
1286 | if ((pRxData == NULL) || (Size == 0)) |
||
1287 | { |
||
1288 | return HAL_ERROR; |
||
1289 | } |
||
1290 | |||
1291 | /* Process Locked */ |
||
1292 | __HAL_LOCK(husart); |
||
1293 | |||
1294 | husart->pRxBuffPtr = pRxData; |
||
1295 | husart->RxXferSize = Size; |
||
1296 | husart->pTxBuffPtr = pRxData; |
||
1297 | husart->TxXferSize = Size; |
||
1298 | |||
1299 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
1300 | husart->State = HAL_USART_STATE_BUSY_RX; |
||
1301 | |||
1302 | /* Set the USART DMA Rx transfer complete callback */ |
||
1303 | husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt; |
||
1304 | |||
1305 | /* Set the USART DMA Half transfer complete callback */ |
||
1306 | husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt; |
||
1307 | |||
1308 | /* Set the USART DMA Rx transfer error callback */ |
||
1309 | husart->hdmarx->XferErrorCallback = USART_DMAError; |
||
1310 | |||
1311 | /* Set the DMA abort callback */ |
||
1312 | husart->hdmarx->XferAbortCallback = NULL; |
||
1313 | |||
1314 | /* Set the USART Tx DMA transfer complete callback as NULL because the communication closing |
||
1315 | is performed in DMA reception complete callback */ |
||
1316 | husart->hdmatx->XferHalfCpltCallback = NULL; |
||
1317 | husart->hdmatx->XferCpltCallback = NULL; |
||
1318 | |||
1319 | /* Set the DMA error callback */ |
||
1320 | husart->hdmatx->XferErrorCallback = USART_DMAError; |
||
1321 | |||
1322 | /* Set the DMA AbortCpltCallback */ |
||
1323 | husart->hdmatx->XferAbortCallback = NULL; |
||
1324 | |||
1325 | /* Enable the USART receive DMA channel */ |
||
1326 | tmp = (uint32_t *)&pRxData; |
||
1327 | HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t *)tmp, Size); |
||
1328 | |||
1329 | /* Enable the USART transmit DMA channel: the transmit channel is used in order |
||
1330 | to generate in the non-blocking mode the clock to the slave device, |
||
1331 | this mode isn't a simplex receive mode but a full-duplex receive one */ |
||
1332 | HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size); |
||
1333 | |||
1334 | /* Clear the Overrun flag just before enabling the DMA Rx request: mandatory for the second transfer */ |
||
1335 | __HAL_USART_CLEAR_OREFLAG(husart); |
||
1336 | |||
1337 | /* Process Unlocked */ |
||
1338 | __HAL_UNLOCK(husart); |
||
1339 | |||
1340 | /* Enable the USART Parity Error Interrupt */ |
||
1341 | SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
||
1342 | |||
1343 | /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
||
1344 | SET_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
1345 | |||
1346 | /* Enable the DMA transfer for the receiver request by setting the DMAR bit |
||
1347 | in the USART CR3 register */ |
||
1348 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
||
1349 | |||
1350 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
||
1351 | in the USART CR3 register */ |
||
1352 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
||
1353 | |||
1354 | return HAL_OK; |
||
1355 | } |
||
1356 | else |
||
1357 | { |
||
1358 | return HAL_BUSY; |
||
1359 | } |
||
1360 | } |
||
1361 | |||
1362 | /** |
||
1363 | * @brief Full-Duplex Transmit Receive an amount of data in DMA mode. |
||
1364 | * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), |
||
1365 | * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number |
||
1366 | * of u16 available through pTxData and through pRxData. |
||
1367 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1368 | * the configuration information for the specified USART module. |
||
1369 | * @param pTxData Pointer to TX data buffer (u8 or u16 data elements). |
||
1370 | * @param pRxData Pointer to RX data buffer (u8 or u16 data elements). |
||
1371 | * @param Size Amount of data elements (u8 or u16) to be received/sent. |
||
1372 | * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit. |
||
1373 | * @retval HAL status |
||
1374 | */ |
||
1375 | HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) |
||
1376 | { |
||
1377 | uint32_t *tmp; |
||
1378 | |||
1379 | if (husart->State == HAL_USART_STATE_READY) |
||
1380 | { |
||
1381 | if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0)) |
||
1382 | { |
||
1383 | return HAL_ERROR; |
||
1384 | } |
||
1385 | /* Process Locked */ |
||
1386 | __HAL_LOCK(husart); |
||
1387 | |||
1388 | husart->pRxBuffPtr = pRxData; |
||
1389 | husart->RxXferSize = Size; |
||
1390 | husart->pTxBuffPtr = pTxData; |
||
1391 | husart->TxXferSize = Size; |
||
1392 | |||
1393 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
1394 | husart->State = HAL_USART_STATE_BUSY_TX_RX; |
||
1395 | |||
1396 | /* Set the USART DMA Rx transfer complete callback */ |
||
1397 | husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt; |
||
1398 | |||
1399 | /* Set the USART DMA Half transfer complete callback */ |
||
1400 | husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt; |
||
1401 | |||
1402 | /* Set the USART DMA Tx transfer complete callback */ |
||
1403 | husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt; |
||
1404 | |||
1405 | /* Set the USART DMA Half transfer complete callback */ |
||
1406 | husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt; |
||
1407 | |||
1408 | /* Set the USART DMA Tx transfer error callback */ |
||
1409 | husart->hdmatx->XferErrorCallback = USART_DMAError; |
||
1410 | |||
1411 | /* Set the USART DMA Rx transfer error callback */ |
||
1412 | husart->hdmarx->XferErrorCallback = USART_DMAError; |
||
1413 | |||
1414 | /* Set the DMA abort callback */ |
||
1415 | husart->hdmarx->XferAbortCallback = NULL; |
||
1416 | |||
1417 | /* Enable the USART receive DMA channel */ |
||
1418 | tmp = (uint32_t *)&pRxData; |
||
1419 | HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t *)tmp, Size); |
||
1420 | |||
1421 | /* Enable the USART transmit DMA channel */ |
||
1422 | tmp = (uint32_t *)&pTxData; |
||
1423 | HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size); |
||
1424 | |||
1425 | /* Clear the TC flag in the SR register by writing 0 to it */ |
||
1426 | __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC); |
||
1427 | |||
1428 | /* Clear the Overrun flag: mandatory for the second transfer in circular mode */ |
||
1429 | __HAL_USART_CLEAR_OREFLAG(husart); |
||
1430 | |||
1431 | /* Process Unlocked */ |
||
1432 | __HAL_UNLOCK(husart); |
||
1433 | |||
1434 | /* Enable the USART Parity Error Interrupt */ |
||
1435 | SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
||
1436 | |||
1437 | /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
||
1438 | SET_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
1439 | |||
1440 | /* Enable the DMA transfer for the receiver request by setting the DMAR bit |
||
1441 | in the USART CR3 register */ |
||
1442 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
||
1443 | |||
1444 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
||
1445 | in the USART CR3 register */ |
||
1446 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
||
1447 | |||
1448 | return HAL_OK; |
||
1449 | } |
||
1450 | else |
||
1451 | { |
||
1452 | return HAL_BUSY; |
||
1453 | } |
||
1454 | } |
||
1455 | |||
1456 | /** |
||
1457 | * @brief Pauses the DMA Transfer. |
||
1458 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1459 | * the configuration information for the specified USART module. |
||
1460 | * @retval HAL status |
||
1461 | */ |
||
1462 | HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart) |
||
1463 | { |
||
1464 | /* Process Locked */ |
||
1465 | __HAL_LOCK(husart); |
||
1466 | |||
1467 | /* Disable the USART DMA Tx request */ |
||
1468 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
||
1469 | |||
1470 | /* Process Unlocked */ |
||
1471 | __HAL_UNLOCK(husart); |
||
1472 | |||
1473 | return HAL_OK; |
||
1474 | } |
||
1475 | |||
1476 | /** |
||
1477 | * @brief Resumes the DMA Transfer. |
||
1478 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1479 | * the configuration information for the specified USART module. |
||
1480 | * @retval HAL status |
||
1481 | */ |
||
1482 | HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart) |
||
1483 | { |
||
1484 | /* Process Locked */ |
||
1485 | __HAL_LOCK(husart); |
||
1486 | |||
1487 | /* Enable the USART DMA Tx request */ |
||
1488 | SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
||
1489 | |||
1490 | /* Process Unlocked */ |
||
1491 | __HAL_UNLOCK(husart); |
||
1492 | |||
1493 | return HAL_OK; |
||
1494 | } |
||
1495 | |||
1496 | /** |
||
1497 | * @brief Stops the DMA Transfer. |
||
1498 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1499 | * the configuration information for the specified USART module. |
||
1500 | * @retval HAL status |
||
1501 | */ |
||
1502 | HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart) |
||
1503 | { |
||
1504 | uint32_t dmarequest = 0x00U; |
||
1505 | /* The Lock is not implemented on this API to allow the user application |
||
1506 | to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback(): |
||
1507 | when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated |
||
1508 | and the correspond call back is executed HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() |
||
1509 | */ |
||
1510 | |||
1511 | /* Stop USART DMA Tx request if ongoing */ |
||
1512 | dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT); |
||
1513 | if ((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest) |
||
1514 | { |
||
1515 | USART_EndTxTransfer(husart); |
||
1516 | |||
1517 | /* Abort the USART DMA Tx channel */ |
||
1518 | if (husart->hdmatx != NULL) |
||
1519 | { |
||
1520 | HAL_DMA_Abort(husart->hdmatx); |
||
1521 | } |
||
1522 | |||
1523 | /* Disable the USART Tx DMA request */ |
||
1524 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
||
1525 | } |
||
1526 | |||
1527 | /* Stop USART DMA Rx request if ongoing */ |
||
1528 | dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR); |
||
1529 | if ((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest) |
||
1530 | { |
||
1531 | USART_EndRxTransfer(husart); |
||
1532 | |||
1533 | /* Abort the USART DMA Rx channel */ |
||
1534 | if (husart->hdmarx != NULL) |
||
1535 | { |
||
1536 | HAL_DMA_Abort(husart->hdmarx); |
||
1537 | } |
||
1538 | |||
1539 | /* Disable the USART Rx DMA request */ |
||
1540 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
||
1541 | } |
||
1542 | |||
1543 | return HAL_OK; |
||
1544 | } |
||
1545 | |||
1546 | /** |
||
1547 | * @brief Abort ongoing transfer (blocking mode). |
||
1548 | * @param husart USART handle. |
||
1549 | * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx, |
||
1550 | * as described by TransferType parameter) started in Interrupt or DMA mode. |
||
1551 | * This procedure performs following operations : |
||
1552 | * - Disable PPP Interrupts (depending of transfer direction) |
||
1553 | * - Disable the DMA transfer in the peripheral register (if enabled) |
||
1554 | * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) |
||
1555 | * - Set handle State to READY |
||
1556 | * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. |
||
1557 | * @retval HAL status |
||
1558 | */ |
||
1559 | HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart) |
||
1560 | { |
||
1561 | /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
||
1562 | CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE)); |
||
1563 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
1564 | |||
1565 | /* Disable the USART DMA Tx request if enabled */ |
||
1566 | if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) |
||
1567 | { |
||
1568 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
||
1569 | |||
1570 | /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */ |
||
1571 | if (husart->hdmatx != NULL) |
||
1572 | { |
||
1573 | /* Set the USART DMA Abort callback to Null. |
||
1574 | No call back execution at end of DMA abort procedure */ |
||
1575 | husart->hdmatx->XferAbortCallback = NULL; |
||
1576 | |||
1577 | HAL_DMA_Abort(husart->hdmatx); |
||
1578 | } |
||
1579 | } |
||
1580 | |||
1581 | /* Disable the USART DMA Rx request if enabled */ |
||
1582 | if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) |
||
1583 | { |
||
1584 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
||
1585 | |||
1586 | /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */ |
||
1587 | if (husart->hdmarx != NULL) |
||
1588 | { |
||
1589 | /* Set the USART DMA Abort callback to Null. |
||
1590 | No call back execution at end of DMA abort procedure */ |
||
1591 | husart->hdmarx->XferAbortCallback = NULL; |
||
1592 | |||
1593 | HAL_DMA_Abort(husart->hdmarx); |
||
1594 | } |
||
1595 | } |
||
1596 | |||
1597 | /* Reset Tx and Rx transfer counters */ |
||
1598 | husart->TxXferCount = 0x00U; |
||
1599 | husart->RxXferCount = 0x00U; |
||
1600 | |||
1601 | /* Restore husart->State to Ready */ |
||
1602 | husart->State = HAL_USART_STATE_READY; |
||
1603 | |||
1604 | /* Reset Handle ErrorCode to No Error */ |
||
1605 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
1606 | |||
1607 | return HAL_OK; |
||
1608 | } |
||
1609 | |||
1610 | /** |
||
1611 | * @brief Abort ongoing transfer (Interrupt mode). |
||
1612 | * @param husart USART handle. |
||
1613 | * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx, |
||
1614 | * as described by TransferType parameter) started in Interrupt or DMA mode. |
||
1615 | * This procedure performs following operations : |
||
1616 | * - Disable PPP Interrupts (depending of transfer direction) |
||
1617 | * - Disable the DMA transfer in the peripheral register (if enabled) |
||
1618 | * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) |
||
1619 | * - Set handle State to READY |
||
1620 | * - At abort completion, call user abort complete callback |
||
1621 | * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be |
||
1622 | * considered as completed only when user abort complete callback is executed (not when exiting function). |
||
1623 | * @retval HAL status |
||
1624 | */ |
||
1625 | HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart) |
||
1626 | { |
||
1627 | uint32_t AbortCplt = 0x01U; |
||
1628 | |||
1629 | /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
||
1630 | CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE)); |
||
1631 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
1632 | |||
1633 | /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised |
||
1634 | before any call to DMA Abort functions */ |
||
1635 | /* DMA Tx Handle is valid */ |
||
1636 | if (husart->hdmatx != NULL) |
||
1637 | { |
||
1638 | /* Set DMA Abort Complete callback if USART DMA Tx request if enabled. |
||
1639 | Otherwise, set it to NULL */ |
||
1640 | if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) |
||
1641 | { |
||
1642 | husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback; |
||
1643 | } |
||
1644 | else |
||
1645 | { |
||
1646 | husart->hdmatx->XferAbortCallback = NULL; |
||
1647 | } |
||
1648 | } |
||
1649 | /* DMA Rx Handle is valid */ |
||
1650 | if (husart->hdmarx != NULL) |
||
1651 | { |
||
1652 | /* Set DMA Abort Complete callback if USART DMA Rx request if enabled. |
||
1653 | Otherwise, set it to NULL */ |
||
1654 | if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) |
||
1655 | { |
||
1656 | husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback; |
||
1657 | } |
||
1658 | else |
||
1659 | { |
||
1660 | husart->hdmarx->XferAbortCallback = NULL; |
||
1661 | } |
||
1662 | } |
||
1663 | |||
1664 | /* Disable the USART DMA Tx request if enabled */ |
||
1665 | if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) |
||
1666 | { |
||
1667 | /* Disable DMA Tx at USART level */ |
||
1668 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
||
1669 | |||
1670 | /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */ |
||
1671 | if (husart->hdmatx != NULL) |
||
1672 | { |
||
1673 | /* USART Tx DMA Abort callback has already been initialised : |
||
1674 | will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */ |
||
1675 | |||
1676 | /* Abort DMA TX */ |
||
1677 | if (HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK) |
||
1678 | { |
||
1679 | husart->hdmatx->XferAbortCallback = NULL; |
||
1680 | } |
||
1681 | else |
||
1682 | { |
||
1683 | AbortCplt = 0x00U; |
||
1684 | } |
||
1685 | } |
||
1686 | } |
||
1687 | |||
1688 | /* Disable the USART DMA Rx request if enabled */ |
||
1689 | if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) |
||
1690 | { |
||
1691 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
||
1692 | |||
1693 | /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */ |
||
1694 | if (husart->hdmarx != NULL) |
||
1695 | { |
||
1696 | /* USART Rx DMA Abort callback has already been initialised : |
||
1697 | will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */ |
||
1698 | |||
1699 | /* Abort DMA RX */ |
||
1700 | if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK) |
||
1701 | { |
||
1702 | husart->hdmarx->XferAbortCallback = NULL; |
||
1703 | AbortCplt = 0x01U; |
||
1704 | } |
||
1705 | else |
||
1706 | { |
||
1707 | AbortCplt = 0x00U; |
||
1708 | } |
||
1709 | } |
||
1710 | } |
||
1711 | |||
1712 | /* if no DMA abort complete callback execution is required => call user Abort Complete callback */ |
||
1713 | if (AbortCplt == 0x01U) |
||
1714 | { |
||
1715 | /* Reset Tx and Rx transfer counters */ |
||
1716 | husart->TxXferCount = 0x00U; |
||
1717 | husart->RxXferCount = 0x00U; |
||
1718 | |||
1719 | /* Reset errorCode */ |
||
1720 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
1721 | |||
1722 | /* Restore husart->State to Ready */ |
||
1723 | husart->State = HAL_USART_STATE_READY; |
||
1724 | |||
1725 | /* As no DMA to be aborted, call directly user Abort complete callback */ |
||
1726 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
1727 | /* Call registered Abort Complete Callback */ |
||
1728 | husart->AbortCpltCallback(husart); |
||
1729 | #else |
||
1730 | /* Call legacy weak Abort Complete Callback */ |
||
1731 | HAL_USART_AbortCpltCallback(husart); |
||
1732 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
1733 | } |
||
1734 | |||
1735 | return HAL_OK; |
||
1736 | } |
||
1737 | |||
1738 | /** |
||
1739 | * @brief This function handles USART interrupt request. |
||
1740 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1741 | * the configuration information for the specified USART module. |
||
1742 | * @retval None |
||
1743 | */ |
||
1744 | void HAL_USART_IRQHandler(USART_HandleTypeDef *husart) |
||
1745 | { |
||
1746 | uint32_t isrflags = READ_REG(husart->Instance->SR); |
||
1747 | uint32_t cr1its = READ_REG(husart->Instance->CR1); |
||
1748 | uint32_t cr3its = READ_REG(husart->Instance->CR3); |
||
1749 | uint32_t errorflags = 0x00U; |
||
1750 | uint32_t dmarequest = 0x00U; |
||
1751 | |||
1752 | /* If no error occurs */ |
||
1753 | errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE)); |
||
1754 | if (errorflags == RESET) |
||
1755 | { |
||
1756 | /* USART in mode Receiver -------------------------------------------------*/ |
||
1757 | if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET)) |
||
1758 | { |
||
1759 | if (husart->State == HAL_USART_STATE_BUSY_RX) |
||
1760 | { |
||
1761 | USART_Receive_IT(husart); |
||
1762 | } |
||
1763 | else |
||
1764 | { |
||
1765 | USART_TransmitReceive_IT(husart); |
||
1766 | } |
||
1767 | return; |
||
1768 | } |
||
1769 | } |
||
1770 | /* If some errors occur */ |
||
1771 | if ((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET))) |
||
1772 | { |
||
1773 | /* USART parity error interrupt occurred ----------------------------------*/ |
||
1774 | if (((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET)) |
||
1775 | { |
||
1776 | husart->ErrorCode |= HAL_USART_ERROR_PE; |
||
1777 | } |
||
1778 | |||
1779 | /* USART noise error interrupt occurred --------------------------------*/ |
||
1780 | if (((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) |
||
1781 | { |
||
1782 | husart->ErrorCode |= HAL_USART_ERROR_NE; |
||
1783 | } |
||
1784 | |||
1785 | /* USART frame error interrupt occurred --------------------------------*/ |
||
1786 | if (((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) |
||
1787 | { |
||
1788 | husart->ErrorCode |= HAL_USART_ERROR_FE; |
||
1789 | } |
||
1790 | |||
1791 | /* USART Over-Run interrupt occurred -----------------------------------*/ |
||
1792 | if (((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) |
||
1793 | { |
||
1794 | husart->ErrorCode |= HAL_USART_ERROR_ORE; |
||
1795 | } |
||
1796 | |||
1797 | if (husart->ErrorCode != HAL_USART_ERROR_NONE) |
||
1798 | { |
||
1799 | /* USART in mode Receiver -----------------------------------------------*/ |
||
1800 | if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET)) |
||
1801 | { |
||
1802 | if (husart->State == HAL_USART_STATE_BUSY_RX) |
||
1803 | { |
||
1804 | USART_Receive_IT(husart); |
||
1805 | } |
||
1806 | else |
||
1807 | { |
||
1808 | USART_TransmitReceive_IT(husart); |
||
1809 | } |
||
1810 | } |
||
1811 | /* If Overrun error occurs, or if any error occurs in DMA mode reception, |
||
1812 | consider error as blocking */ |
||
1813 | dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR); |
||
1814 | if (((husart->ErrorCode & HAL_USART_ERROR_ORE) != RESET) || dmarequest) |
||
1815 | { |
||
1816 | /* Set the USART state ready to be able to start again the process, |
||
1817 | Disable Rx Interrupts, and disable Rx DMA request, if ongoing */ |
||
1818 | USART_EndRxTransfer(husart); |
||
1819 | |||
1820 | /* Disable the USART DMA Rx request if enabled */ |
||
1821 | if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) |
||
1822 | { |
||
1823 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
||
1824 | |||
1825 | /* Abort the USART DMA Rx channel */ |
||
1826 | if (husart->hdmarx != NULL) |
||
1827 | { |
||
1828 | /* Set the USART DMA Abort callback : |
||
1829 | will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */ |
||
1830 | husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError; |
||
1831 | |||
1832 | if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK) |
||
1833 | { |
||
1834 | /* Call Directly XferAbortCallback function in case of error */ |
||
1835 | husart->hdmarx->XferAbortCallback(husart->hdmarx); |
||
1836 | } |
||
1837 | } |
||
1838 | else |
||
1839 | { |
||
1840 | /* Call user error callback */ |
||
1841 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
1842 | /* Call registered Error Callback */ |
||
1843 | husart->ErrorCallback(husart); |
||
1844 | #else |
||
1845 | /* Call legacy weak Error Callback */ |
||
1846 | HAL_USART_ErrorCallback(husart); |
||
1847 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
1848 | } |
||
1849 | } |
||
1850 | else |
||
1851 | { |
||
1852 | /* Call user error callback */ |
||
1853 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
1854 | /* Call registered Error Callback */ |
||
1855 | husart->ErrorCallback(husart); |
||
1856 | #else |
||
1857 | /* Call legacy weak Error Callback */ |
||
1858 | HAL_USART_ErrorCallback(husart); |
||
1859 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
1860 | } |
||
1861 | } |
||
1862 | else |
||
1863 | { |
||
1864 | /* Call user error callback */ |
||
1865 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
1866 | /* Call registered Error Callback */ |
||
1867 | husart->ErrorCallback(husart); |
||
1868 | #else |
||
1869 | /* Call legacy weak Error Callback */ |
||
1870 | HAL_USART_ErrorCallback(husart); |
||
1871 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
1872 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
1873 | } |
||
1874 | } |
||
1875 | return; |
||
1876 | } |
||
1877 | |||
1878 | /* USART in mode Transmitter -----------------------------------------------*/ |
||
1879 | if (((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET)) |
||
1880 | { |
||
1881 | if (husart->State == HAL_USART_STATE_BUSY_TX) |
||
1882 | { |
||
1883 | USART_Transmit_IT(husart); |
||
1884 | } |
||
1885 | else |
||
1886 | { |
||
1887 | USART_TransmitReceive_IT(husart); |
||
1888 | } |
||
1889 | return; |
||
1890 | } |
||
1891 | |||
1892 | /* USART in mode Transmitter (transmission end) ----------------------------*/ |
||
1893 | if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET)) |
||
1894 | { |
||
1895 | USART_EndTransmit_IT(husart); |
||
1896 | return; |
||
1897 | } |
||
1898 | } |
||
1899 | |||
1900 | /** |
||
1901 | * @brief Tx Transfer completed callbacks. |
||
1902 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1903 | * the configuration information for the specified USART module. |
||
1904 | * @retval None |
||
1905 | */ |
||
1906 | __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart) |
||
1907 | { |
||
1908 | /* Prevent unused argument(s) compilation warning */ |
||
1909 | UNUSED(husart); |
||
1910 | /* NOTE: This function should not be modified, when the callback is needed, |
||
1911 | the HAL_USART_TxCpltCallback could be implemented in the user file |
||
1912 | */ |
||
1913 | } |
||
1914 | |||
1915 | /** |
||
1916 | * @brief Tx Half Transfer completed callbacks. |
||
1917 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1918 | * the configuration information for the specified USART module. |
||
1919 | * @retval None |
||
1920 | */ |
||
1921 | __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart) |
||
1922 | { |
||
1923 | /* Prevent unused argument(s) compilation warning */ |
||
1924 | UNUSED(husart); |
||
1925 | /* NOTE: This function should not be modified, when the callback is needed, |
||
1926 | the HAL_USART_TxHalfCpltCallback could be implemented in the user file |
||
1927 | */ |
||
1928 | } |
||
1929 | |||
1930 | /** |
||
1931 | * @brief Rx Transfer completed callbacks. |
||
1932 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1933 | * the configuration information for the specified USART module. |
||
1934 | * @retval None |
||
1935 | */ |
||
1936 | __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart) |
||
1937 | { |
||
1938 | /* Prevent unused argument(s) compilation warning */ |
||
1939 | UNUSED(husart); |
||
1940 | /* NOTE: This function should not be modified, when the callback is needed, |
||
1941 | the HAL_USART_RxCpltCallback could be implemented in the user file |
||
1942 | */ |
||
1943 | } |
||
1944 | |||
1945 | /** |
||
1946 | * @brief Rx Half Transfer completed callbacks. |
||
1947 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1948 | * the configuration information for the specified USART module. |
||
1949 | * @retval None |
||
1950 | */ |
||
1951 | __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart) |
||
1952 | { |
||
1953 | /* Prevent unused argument(s) compilation warning */ |
||
1954 | UNUSED(husart); |
||
1955 | /* NOTE: This function should not be modified, when the callback is needed, |
||
1956 | the HAL_USART_RxHalfCpltCallback could be implemented in the user file |
||
1957 | */ |
||
1958 | } |
||
1959 | |||
1960 | /** |
||
1961 | * @brief Tx/Rx Transfers completed callback for the non-blocking process. |
||
1962 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1963 | * the configuration information for the specified USART module. |
||
1964 | * @retval None |
||
1965 | */ |
||
1966 | __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart) |
||
1967 | { |
||
1968 | /* Prevent unused argument(s) compilation warning */ |
||
1969 | UNUSED(husart); |
||
1970 | /* NOTE: This function should not be modified, when the callback is needed, |
||
1971 | the HAL_USART_TxRxCpltCallback could be implemented in the user file |
||
1972 | */ |
||
1973 | } |
||
1974 | |||
1975 | /** |
||
1976 | * @brief USART error callbacks. |
||
1977 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
1978 | * the configuration information for the specified USART module. |
||
1979 | * @retval None |
||
1980 | */ |
||
1981 | __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart) |
||
1982 | { |
||
1983 | /* Prevent unused argument(s) compilation warning */ |
||
1984 | UNUSED(husart); |
||
1985 | /* NOTE: This function should not be modified, when the callback is needed, |
||
1986 | the HAL_USART_ErrorCallback could be implemented in the user file |
||
1987 | */ |
||
1988 | } |
||
1989 | |||
1990 | /** |
||
1991 | * @brief USART Abort Complete callback. |
||
1992 | * @param husart USART handle. |
||
1993 | * @retval None |
||
1994 | */ |
||
1995 | __weak void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart) |
||
1996 | { |
||
1997 | /* Prevent unused argument(s) compilation warning */ |
||
1998 | UNUSED(husart); |
||
1999 | |||
2000 | /* NOTE : This function should not be modified, when the callback is needed, |
||
2001 | the HAL_USART_AbortCpltCallback can be implemented in the user file. |
||
2002 | */ |
||
2003 | } |
||
2004 | |||
2005 | /** |
||
2006 | * @} |
||
2007 | */ |
||
2008 | |||
2009 | /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions |
||
2010 | * @brief USART State and Errors functions |
||
2011 | * |
||
2012 | @verbatim |
||
2013 | ============================================================================== |
||
2014 | ##### Peripheral State and Errors functions ##### |
||
2015 | ============================================================================== |
||
2016 | [..] |
||
2017 | This subsection provides a set of functions allowing to return the State of |
||
2018 | USART communication |
||
2019 | process, return Peripheral Errors occurred during communication process |
||
2020 | (+) HAL_USART_GetState() API can be helpful to check in run-time the state |
||
2021 | of the USART peripheral. |
||
2022 | (+) HAL_USART_GetError() check in run-time errors that could be occurred during |
||
2023 | communication. |
||
2024 | @endverbatim |
||
2025 | * @{ |
||
2026 | */ |
||
2027 | |||
2028 | /** |
||
2029 | * @brief Returns the USART state. |
||
2030 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
2031 | * the configuration information for the specified USART module. |
||
2032 | * @retval HAL state |
||
2033 | */ |
||
2034 | HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart) |
||
2035 | { |
||
2036 | return husart->State; |
||
2037 | } |
||
2038 | |||
2039 | /** |
||
2040 | * @brief Return the USART error code |
||
2041 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
2042 | * the configuration information for the specified USART. |
||
2043 | * @retval USART Error Code |
||
2044 | */ |
||
2045 | uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart) |
||
2046 | { |
||
2047 | return husart->ErrorCode; |
||
2048 | } |
||
2049 | |||
2050 | /** |
||
2051 | * @} |
||
2052 | */ |
||
2053 | |||
2054 | /** @defgroup USART_Private_Functions USART Private Functions |
||
2055 | * @{ |
||
2056 | */ |
||
2057 | |||
2058 | /** |
||
2059 | * @brief Initialize the callbacks to their default values. |
||
2060 | * @param husart USART handle. |
||
2061 | * @retval none |
||
2062 | */ |
||
2063 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2064 | void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart) |
||
2065 | { |
||
2066 | /* Init the USART Callback settings */ |
||
2067 | husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ |
||
2068 | husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */ |
||
2069 | husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ |
||
2070 | husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */ |
||
2071 | husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */ |
||
2072 | husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */ |
||
2073 | husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ |
||
2074 | } |
||
2075 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2076 | |||
2077 | /** |
||
2078 | * @brief DMA USART transmit process complete callback. |
||
2079 | * @param hdma Pointer to a DMA_HandleTypeDef structure that contains |
||
2080 | * the configuration information for the specified DMA module. |
||
2081 | * @retval None |
||
2082 | */ |
||
2083 | static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma) |
||
2084 | { |
||
2085 | USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
2086 | /* DMA Normal mode */ |
||
2087 | if ((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U) |
||
2088 | { |
||
2089 | husart->TxXferCount = 0U; |
||
2090 | if (husart->State == HAL_USART_STATE_BUSY_TX) |
||
2091 | { |
||
2092 | /* Disable the DMA transfer for transmit request by resetting the DMAT bit |
||
2093 | in the USART CR3 register */ |
||
2094 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
||
2095 | |||
2096 | /* Enable the USART Transmit Complete Interrupt */ |
||
2097 | SET_BIT(husart->Instance->CR1, USART_CR1_TCIE); |
||
2098 | } |
||
2099 | } |
||
2100 | /* DMA Circular mode */ |
||
2101 | else |
||
2102 | { |
||
2103 | if (husart->State == HAL_USART_STATE_BUSY_TX) |
||
2104 | { |
||
2105 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2106 | /* Call registered Tx Complete Callback */ |
||
2107 | husart->TxCpltCallback(husart); |
||
2108 | #else |
||
2109 | /* Call legacy weak Tx Complete Callback */ |
||
2110 | HAL_USART_TxCpltCallback(husart); |
||
2111 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2112 | } |
||
2113 | } |
||
2114 | } |
||
2115 | |||
2116 | /** |
||
2117 | * @brief DMA USART transmit process half complete callback |
||
2118 | * @param hdma Pointer to a DMA_HandleTypeDef structure that contains |
||
2119 | * the configuration information for the specified DMA module. |
||
2120 | * @retval None |
||
2121 | */ |
||
2122 | static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma) |
||
2123 | { |
||
2124 | USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
2125 | |||
2126 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2127 | /* Call registered Tx Half Complete Callback */ |
||
2128 | husart->TxHalfCpltCallback(husart); |
||
2129 | #else |
||
2130 | /* Call legacy weak Tx Half Complete Callback */ |
||
2131 | HAL_USART_TxHalfCpltCallback(husart); |
||
2132 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2133 | } |
||
2134 | |||
2135 | /** |
||
2136 | * @brief DMA USART receive process complete callback. |
||
2137 | * @param hdma Pointer to a DMA_HandleTypeDef structure that contains |
||
2138 | * the configuration information for the specified DMA module. |
||
2139 | * @retval None |
||
2140 | */ |
||
2141 | static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma) |
||
2142 | { |
||
2143 | USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
2144 | /* DMA Normal mode */ |
||
2145 | if ((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U) |
||
2146 | { |
||
2147 | husart->RxXferCount = 0x00U; |
||
2148 | |||
2149 | /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
||
2150 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
||
2151 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
2152 | |||
2153 | /* Disable the DMA transfer for the Transmit/receiver request by clearing the DMAT/DMAR bit |
||
2154 | in the USART CR3 register */ |
||
2155 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); |
||
2156 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); |
||
2157 | |||
2158 | husart->State = HAL_USART_STATE_READY; |
||
2159 | |||
2160 | /* The USART state is HAL_USART_STATE_BUSY_RX */ |
||
2161 | if (husart->State == HAL_USART_STATE_BUSY_RX) |
||
2162 | { |
||
2163 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2164 | /* Call registered Rx Complete Callback */ |
||
2165 | husart->RxCpltCallback(husart); |
||
2166 | #else |
||
2167 | /* Call legacy weak Rx Complete Callback */ |
||
2168 | HAL_USART_RxCpltCallback(husart); |
||
2169 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2170 | } |
||
2171 | /* The USART state is HAL_USART_STATE_BUSY_TX_RX */ |
||
2172 | else |
||
2173 | { |
||
2174 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2175 | /* Call registered Tx Rx Complete Callback */ |
||
2176 | husart->TxRxCpltCallback(husart); |
||
2177 | #else |
||
2178 | /* Call legacy weak Tx Rx Complete Callback */ |
||
2179 | HAL_USART_TxRxCpltCallback(husart); |
||
2180 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2181 | } |
||
2182 | } |
||
2183 | /* DMA circular mode */ |
||
2184 | else |
||
2185 | { |
||
2186 | if (husart->State == HAL_USART_STATE_BUSY_RX) |
||
2187 | { |
||
2188 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2189 | /* Call registered Rx Complete Callback */ |
||
2190 | husart->RxCpltCallback(husart); |
||
2191 | #else |
||
2192 | /* Call legacy weak Rx Complete Callback */ |
||
2193 | HAL_USART_RxCpltCallback(husart); |
||
2194 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2195 | } |
||
2196 | /* The USART state is HAL_USART_STATE_BUSY_TX_RX */ |
||
2197 | else |
||
2198 | { |
||
2199 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2200 | /* Call registered Tx Rx Complete Callback */ |
||
2201 | husart->TxRxCpltCallback(husart); |
||
2202 | #else |
||
2203 | /* Call legacy weak Tx Rx Complete Callback */ |
||
2204 | HAL_USART_TxRxCpltCallback(husart); |
||
2205 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2206 | } |
||
2207 | } |
||
2208 | } |
||
2209 | |||
2210 | /** |
||
2211 | * @brief DMA USART receive process half complete callback |
||
2212 | * @param hdma Pointer to a DMA_HandleTypeDef structure that contains |
||
2213 | * the configuration information for the specified DMA module. |
||
2214 | * @retval None |
||
2215 | */ |
||
2216 | static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) |
||
2217 | { |
||
2218 | USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
2219 | |||
2220 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2221 | /* Call registered Rx Half Complete Callback */ |
||
2222 | husart->RxHalfCpltCallback(husart); |
||
2223 | #else |
||
2224 | /* Call legacy weak Rx Half Complete Callback */ |
||
2225 | HAL_USART_RxHalfCpltCallback(husart); |
||
2226 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2227 | } |
||
2228 | |||
2229 | /** |
||
2230 | * @brief DMA USART communication error callback. |
||
2231 | * @param hdma Pointer to a DMA_HandleTypeDef structure that contains |
||
2232 | * the configuration information for the specified DMA module. |
||
2233 | * @retval None |
||
2234 | */ |
||
2235 | static void USART_DMAError(DMA_HandleTypeDef *hdma) |
||
2236 | { |
||
2237 | uint32_t dmarequest = 0x00U; |
||
2238 | USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
2239 | husart->RxXferCount = 0x00U; |
||
2240 | husart->TxXferCount = 0x00U; |
||
2241 | |||
2242 | /* Stop USART DMA Tx request if ongoing */ |
||
2243 | dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT); |
||
2244 | if ((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest) |
||
2245 | { |
||
2246 | USART_EndTxTransfer(husart); |
||
2247 | } |
||
2248 | |||
2249 | /* Stop USART DMA Rx request if ongoing */ |
||
2250 | dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR); |
||
2251 | if ((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest) |
||
2252 | { |
||
2253 | USART_EndRxTransfer(husart); |
||
2254 | } |
||
2255 | |||
2256 | husart->ErrorCode |= HAL_USART_ERROR_DMA; |
||
2257 | husart->State = HAL_USART_STATE_READY; |
||
2258 | |||
2259 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2260 | /* Call registered Error Callback */ |
||
2261 | husart->ErrorCallback(husart); |
||
2262 | #else |
||
2263 | /* Call legacy weak Error Callback */ |
||
2264 | HAL_USART_ErrorCallback(husart); |
||
2265 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2266 | } |
||
2267 | |||
2268 | /** |
||
2269 | * @brief This function handles USART Communication Timeout. |
||
2270 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
2271 | * the configuration information for the specified USART module. |
||
2272 | * @param Flag specifies the USART flag to check. |
||
2273 | * @param Status The new Flag status (SET or RESET). |
||
2274 | * @param Tickstart Tick start value. |
||
2275 | * @param Timeout Timeout duration. |
||
2276 | * @retval HAL status |
||
2277 | */ |
||
2278 | static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout) |
||
2279 | { |
||
2280 | /* Wait until flag is set */ |
||
2281 | while ((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status) |
||
2282 | { |
||
2283 | /* Check for the Timeout */ |
||
2284 | if (Timeout != HAL_MAX_DELAY) |
||
2285 | { |
||
2286 | if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout)) |
||
2287 | { |
||
2288 | /* Disable the USART Transmit Complete Interrupt */ |
||
2289 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE); |
||
2290 | |||
2291 | /* Disable the USART RXNE Interrupt */ |
||
2292 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE); |
||
2293 | |||
2294 | /* Disable the USART Parity Error Interrupt */ |
||
2295 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
||
2296 | |||
2297 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
||
2298 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
2299 | |||
2300 | husart->State = HAL_USART_STATE_READY; |
||
2301 | |||
2302 | /* Process Unlocked */ |
||
2303 | __HAL_UNLOCK(husart); |
||
2304 | |||
2305 | return HAL_TIMEOUT; |
||
2306 | } |
||
2307 | } |
||
2308 | } |
||
2309 | return HAL_OK; |
||
2310 | } |
||
2311 | |||
2312 | /** |
||
2313 | * @brief End ongoing Tx transfer on USART peripheral (following error detection or Transmit completion). |
||
2314 | * @param husart USART handle. |
||
2315 | * @retval None |
||
2316 | */ |
||
2317 | static void USART_EndTxTransfer(USART_HandleTypeDef *husart) |
||
2318 | { |
||
2319 | /* Disable TXEIE and TCIE interrupts */ |
||
2320 | CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); |
||
2321 | |||
2322 | /* At end of Tx process, restore husart->State to Ready */ |
||
2323 | husart->State = HAL_USART_STATE_READY; |
||
2324 | } |
||
2325 | |||
2326 | /** |
||
2327 | * @brief End ongoing Rx transfer on USART peripheral (following error detection or Reception completion). |
||
2328 | * @param husart USART handle. |
||
2329 | * @retval None |
||
2330 | */ |
||
2331 | static void USART_EndRxTransfer(USART_HandleTypeDef *husart) |
||
2332 | { |
||
2333 | /* Disable RXNE, PE and ERR interrupts */ |
||
2334 | CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); |
||
2335 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
2336 | |||
2337 | /* At end of Rx process, restore husart->State to Ready */ |
||
2338 | husart->State = HAL_USART_STATE_READY; |
||
2339 | } |
||
2340 | |||
2341 | /** |
||
2342 | * @brief DMA USART communication abort callback, when initiated by HAL services on Error |
||
2343 | * (To be called at end of DMA Abort procedure following error occurrence). |
||
2344 | * @param hdma DMA handle. |
||
2345 | * @retval None |
||
2346 | */ |
||
2347 | static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma) |
||
2348 | { |
||
2349 | USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
2350 | husart->RxXferCount = 0x00U; |
||
2351 | husart->TxXferCount = 0x00U; |
||
2352 | |||
2353 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2354 | /* Call registered Error Callback */ |
||
2355 | husart->ErrorCallback(husart); |
||
2356 | #else |
||
2357 | /* Call legacy weak Error Callback */ |
||
2358 | HAL_USART_ErrorCallback(husart); |
||
2359 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2360 | } |
||
2361 | |||
2362 | /** |
||
2363 | * @brief DMA USART Tx communication abort callback, when initiated by user |
||
2364 | * (To be called at end of DMA Tx Abort procedure following user abort request). |
||
2365 | * @note When this callback is executed, User Abort complete call back is called only if no |
||
2366 | * Abort still ongoing for Rx DMA Handle. |
||
2367 | * @param hdma DMA handle. |
||
2368 | * @retval None |
||
2369 | */ |
||
2370 | static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma) |
||
2371 | { |
||
2372 | USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
2373 | |||
2374 | husart->hdmatx->XferAbortCallback = NULL; |
||
2375 | |||
2376 | /* Check if an Abort process is still ongoing */ |
||
2377 | if (husart->hdmarx != NULL) |
||
2378 | { |
||
2379 | if (husart->hdmarx->XferAbortCallback != NULL) |
||
2380 | { |
||
2381 | return; |
||
2382 | } |
||
2383 | } |
||
2384 | |||
2385 | /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ |
||
2386 | husart->TxXferCount = 0x00U; |
||
2387 | husart->RxXferCount = 0x00U; |
||
2388 | |||
2389 | /* Reset errorCode */ |
||
2390 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
2391 | |||
2392 | /* Restore husart->State to Ready */ |
||
2393 | husart->State = HAL_USART_STATE_READY; |
||
2394 | |||
2395 | /* Call user Abort complete callback */ |
||
2396 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2397 | /* Call registered Abort Complete Callback */ |
||
2398 | husart->AbortCpltCallback(husart); |
||
2399 | #else |
||
2400 | /* Call legacy weak Abort Complete Callback */ |
||
2401 | HAL_USART_AbortCpltCallback(husart); |
||
2402 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2403 | } |
||
2404 | |||
2405 | /** |
||
2406 | * @brief DMA USART Rx communication abort callback, when initiated by user |
||
2407 | * (To be called at end of DMA Rx Abort procedure following user abort request). |
||
2408 | * @note When this callback is executed, User Abort complete call back is called only if no |
||
2409 | * Abort still ongoing for Tx DMA Handle. |
||
2410 | * @param hdma DMA handle. |
||
2411 | * @retval None |
||
2412 | */ |
||
2413 | static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma) |
||
2414 | { |
||
2415 | USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; |
||
2416 | |||
2417 | husart->hdmarx->XferAbortCallback = NULL; |
||
2418 | |||
2419 | /* Check if an Abort process is still ongoing */ |
||
2420 | if (husart->hdmatx != NULL) |
||
2421 | { |
||
2422 | if (husart->hdmatx->XferAbortCallback != NULL) |
||
2423 | { |
||
2424 | return; |
||
2425 | } |
||
2426 | } |
||
2427 | |||
2428 | /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ |
||
2429 | husart->TxXferCount = 0x00U; |
||
2430 | husart->RxXferCount = 0x00U; |
||
2431 | |||
2432 | /* Reset errorCode */ |
||
2433 | husart->ErrorCode = HAL_USART_ERROR_NONE; |
||
2434 | |||
2435 | /* Restore husart->State to Ready */ |
||
2436 | husart->State = HAL_USART_STATE_READY; |
||
2437 | |||
2438 | /* Call user Abort complete callback */ |
||
2439 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2440 | /* Call registered Abort Complete Callback */ |
||
2441 | husart->AbortCpltCallback(husart); |
||
2442 | #else |
||
2443 | /* Call legacy weak Abort Complete Callback */ |
||
2444 | HAL_USART_AbortCpltCallback(husart); |
||
2445 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2446 | } |
||
2447 | |||
2448 | /** |
||
2449 | * @brief Simplex Send an amount of data in non-blocking mode. |
||
2450 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
2451 | * the configuration information for the specified USART module. |
||
2452 | * @retval HAL status |
||
2453 | * @note The USART errors are not managed to avoid the overrun error. |
||
2454 | */ |
||
2455 | static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart) |
||
2456 | { |
||
2457 | uint16_t *tmp; |
||
2458 | |||
2459 | if (husart->State == HAL_USART_STATE_BUSY_TX) |
||
2460 | { |
||
2461 | if (husart->Init.WordLength == USART_WORDLENGTH_9B) |
||
2462 | { |
||
2463 | tmp = (uint16_t *) husart->pTxBuffPtr; |
||
2464 | husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF); |
||
2465 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
2466 | { |
||
2467 | husart->pTxBuffPtr += 2U; |
||
2468 | } |
||
2469 | else |
||
2470 | { |
||
2471 | husart->pTxBuffPtr += 1U; |
||
2472 | } |
||
2473 | } |
||
2474 | else |
||
2475 | { |
||
2476 | husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF); |
||
2477 | } |
||
2478 | |||
2479 | if (--husart->TxXferCount == 0U) |
||
2480 | { |
||
2481 | /* Disable the USART Transmit data register empty Interrupt */ |
||
2482 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE); |
||
2483 | |||
2484 | /* Enable the USART Transmit Complete Interrupt */ |
||
2485 | SET_BIT(husart->Instance->CR1, USART_CR1_TCIE); |
||
2486 | } |
||
2487 | return HAL_OK; |
||
2488 | } |
||
2489 | else |
||
2490 | { |
||
2491 | return HAL_BUSY; |
||
2492 | } |
||
2493 | } |
||
2494 | |||
2495 | /** |
||
2496 | * @brief Wraps up transmission in non blocking mode. |
||
2497 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
2498 | * the configuration information for the specified USART module. |
||
2499 | * @retval HAL status |
||
2500 | */ |
||
2501 | static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart) |
||
2502 | { |
||
2503 | /* Disable the USART Transmit Complete Interrupt */ |
||
2504 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_TCIE); |
||
2505 | |||
2506 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
||
2507 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
2508 | |||
2509 | husart->State = HAL_USART_STATE_READY; |
||
2510 | |||
2511 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2512 | /* Call registered Tx Complete Callback */ |
||
2513 | husart->TxCpltCallback(husart); |
||
2514 | #else |
||
2515 | /* Call legacy weak Tx Complete Callback */ |
||
2516 | HAL_USART_TxCpltCallback(husart); |
||
2517 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2518 | |||
2519 | return HAL_OK; |
||
2520 | } |
||
2521 | |||
2522 | /** |
||
2523 | * @brief Simplex Receive an amount of data in non-blocking mode. |
||
2524 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
2525 | * the configuration information for the specified USART module. |
||
2526 | * @retval HAL status |
||
2527 | */ |
||
2528 | static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart) |
||
2529 | { |
||
2530 | uint16_t *tmp; |
||
2531 | if (husart->State == HAL_USART_STATE_BUSY_RX) |
||
2532 | { |
||
2533 | if (husart->Init.WordLength == USART_WORDLENGTH_9B) |
||
2534 | { |
||
2535 | tmp = (uint16_t *) husart->pRxBuffPtr; |
||
2536 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
2537 | { |
||
2538 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
||
2539 | husart->pRxBuffPtr += 2U; |
||
2540 | } |
||
2541 | else |
||
2542 | { |
||
2543 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
||
2544 | husart->pRxBuffPtr += 1U; |
||
2545 | } |
||
2546 | if (--husart->RxXferCount != 0x00U) |
||
2547 | { |
||
2548 | /* Send dummy byte in order to generate the clock for the slave to send the next data */ |
||
2549 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF); |
||
2550 | } |
||
2551 | } |
||
2552 | else |
||
2553 | { |
||
2554 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
2555 | { |
||
2556 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
||
2557 | } |
||
2558 | else |
||
2559 | { |
||
2560 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
||
2561 | } |
||
2562 | |||
2563 | if (--husart->RxXferCount != 0x00U) |
||
2564 | { |
||
2565 | /* Send dummy byte in order to generate the clock for the slave to send the next data */ |
||
2566 | husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF); |
||
2567 | } |
||
2568 | } |
||
2569 | |||
2570 | if (husart->RxXferCount == 0U) |
||
2571 | { |
||
2572 | /* Disable the USART RXNE Interrupt */ |
||
2573 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE); |
||
2574 | |||
2575 | /* Disable the USART Parity Error Interrupt */ |
||
2576 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
||
2577 | |||
2578 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
||
2579 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
2580 | |||
2581 | husart->State = HAL_USART_STATE_READY; |
||
2582 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2583 | /* Call registered Rx Complete Callback */ |
||
2584 | husart->RxCpltCallback(husart); |
||
2585 | #else |
||
2586 | /* Call legacy weak Rx Complete Callback */ |
||
2587 | HAL_USART_RxCpltCallback(husart); |
||
2588 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2589 | |||
2590 | return HAL_OK; |
||
2591 | } |
||
2592 | return HAL_OK; |
||
2593 | } |
||
2594 | else |
||
2595 | { |
||
2596 | return HAL_BUSY; |
||
2597 | } |
||
2598 | } |
||
2599 | |||
2600 | /** |
||
2601 | * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking). |
||
2602 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
2603 | * the configuration information for the specified USART module. |
||
2604 | * @retval HAL status |
||
2605 | */ |
||
2606 | static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart) |
||
2607 | { |
||
2608 | uint16_t *tmp; |
||
2609 | |||
2610 | if (husart->State == HAL_USART_STATE_BUSY_TX_RX) |
||
2611 | { |
||
2612 | if (husart->TxXferCount != 0x00U) |
||
2613 | { |
||
2614 | if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET) |
||
2615 | { |
||
2616 | if (husart->Init.WordLength == USART_WORDLENGTH_9B) |
||
2617 | { |
||
2618 | tmp = (uint16_t *) husart->pTxBuffPtr; |
||
2619 | husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF); |
||
2620 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
2621 | { |
||
2622 | husart->pTxBuffPtr += 2U; |
||
2623 | } |
||
2624 | else |
||
2625 | { |
||
2626 | husart->pTxBuffPtr += 1U; |
||
2627 | } |
||
2628 | } |
||
2629 | else |
||
2630 | { |
||
2631 | husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF); |
||
2632 | } |
||
2633 | husart->TxXferCount--; |
||
2634 | |||
2635 | /* Check the latest data transmitted */ |
||
2636 | if (husart->TxXferCount == 0U) |
||
2637 | { |
||
2638 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE); |
||
2639 | } |
||
2640 | } |
||
2641 | } |
||
2642 | |||
2643 | if (husart->RxXferCount != 0x00U) |
||
2644 | { |
||
2645 | if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET) |
||
2646 | { |
||
2647 | if (husart->Init.WordLength == USART_WORDLENGTH_9B) |
||
2648 | { |
||
2649 | tmp = (uint16_t *) husart->pRxBuffPtr; |
||
2650 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
2651 | { |
||
2652 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF); |
||
2653 | husart->pRxBuffPtr += 2U; |
||
2654 | } |
||
2655 | else |
||
2656 | { |
||
2657 | *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF); |
||
2658 | husart->pRxBuffPtr += 1U; |
||
2659 | } |
||
2660 | } |
||
2661 | else |
||
2662 | { |
||
2663 | if (husart->Init.Parity == USART_PARITY_NONE) |
||
2664 | { |
||
2665 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF); |
||
2666 | } |
||
2667 | else |
||
2668 | { |
||
2669 | *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F); |
||
2670 | } |
||
2671 | } |
||
2672 | husart->RxXferCount--; |
||
2673 | } |
||
2674 | } |
||
2675 | |||
2676 | /* Check the latest data received */ |
||
2677 | if (husart->RxXferCount == 0U) |
||
2678 | { |
||
2679 | /* Disable the USART RXNE Interrupt */ |
||
2680 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE); |
||
2681 | |||
2682 | /* Disable the USART Parity Error Interrupt */ |
||
2683 | CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); |
||
2684 | |||
2685 | /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ |
||
2686 | CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); |
||
2687 | |||
2688 | husart->State = HAL_USART_STATE_READY; |
||
2689 | |||
2690 | #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) |
||
2691 | /* Call registered Tx Rx Complete Callback */ |
||
2692 | husart->TxRxCpltCallback(husart); |
||
2693 | #else |
||
2694 | /* Call legacy weak Tx Rx Complete Callback */ |
||
2695 | HAL_USART_TxRxCpltCallback(husart); |
||
2696 | #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ |
||
2697 | |||
2698 | return HAL_OK; |
||
2699 | } |
||
2700 | |||
2701 | return HAL_OK; |
||
2702 | } |
||
2703 | else |
||
2704 | { |
||
2705 | return HAL_BUSY; |
||
2706 | } |
||
2707 | } |
||
2708 | |||
2709 | /** |
||
2710 | * @brief Configures the USART peripheral. |
||
2711 | * @param husart Pointer to a USART_HandleTypeDef structure that contains |
||
2712 | * the configuration information for the specified USART module. |
||
2713 | * @retval None |
||
2714 | */ |
||
2715 | static void USART_SetConfig(USART_HandleTypeDef *husart) |
||
2716 | { |
||
2717 | uint32_t tmpreg = 0x00U; |
||
2718 | uint32_t pclk; |
||
2719 | |||
2720 | /* Check the parameters */ |
||
2721 | assert_param(IS_USART_INSTANCE(husart->Instance)); |
||
2722 | assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity)); |
||
2723 | assert_param(IS_USART_PHASE(husart->Init.CLKPhase)); |
||
2724 | assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit)); |
||
2725 | assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate)); |
||
2726 | assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength)); |
||
2727 | assert_param(IS_USART_STOPBITS(husart->Init.StopBits)); |
||
2728 | assert_param(IS_USART_PARITY(husart->Init.Parity)); |
||
2729 | assert_param(IS_USART_MODE(husart->Init.Mode)); |
||
2730 | |||
2731 | /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the |
||
2732 | receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */ |
||
2733 | CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE)); |
||
2734 | |||
2735 | /*---------------------------- USART CR2 Configuration ---------------------*/ |
||
2736 | tmpreg = husart->Instance->CR2; |
||
2737 | /* Clear CLKEN, CPOL, CPHA and LBCL bits */ |
||
2738 | tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP)); |
||
2739 | /* Configure the USART Clock, CPOL, CPHA and LastBit -----------------------*/ |
||
2740 | /* Set CPOL bit according to husart->Init.CLKPolarity value */ |
||
2741 | /* Set CPHA bit according to husart->Init.CLKPhase value */ |
||
2742 | /* Set LBCL bit according to husart->Init.CLKLastBit value */ |
||
2743 | /* Set Stop Bits: Set STOP[13:12] bits according to husart->Init.StopBits value */ |
||
2744 | tmpreg |= (uint32_t)(USART_CLOCK_ENABLE | husart->Init.CLKPolarity | |
||
2745 | husart->Init.CLKPhase | husart->Init.CLKLastBit | husart->Init.StopBits); |
||
2746 | /* Write to USART CR2 */ |
||
2747 | WRITE_REG(husart->Instance->CR2, (uint32_t)tmpreg); |
||
2748 | |||
2749 | /*-------------------------- USART CR1 Configuration -----------------------*/ |
||
2750 | tmpreg = husart->Instance->CR1; |
||
2751 | |||
2752 | /* Clear M, PCE, PS, TE and RE bits */ |
||
2753 | tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)); |
||
2754 | |||
2755 | /* Configure the USART Word Length, Parity and mode: |
||
2756 | Set the M bits according to husart->Init.WordLength value |
||
2757 | Set PCE and PS bits according to husart->Init.Parity value |
||
2758 | Set TE and RE bits according to husart->Init.Mode value |
||
2759 | */ |
||
2760 | tmpreg |= (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode; |
||
2761 | |||
2762 | /* Write to USART CR1 */ |
||
2763 | WRITE_REG(husart->Instance->CR1, (uint32_t)tmpreg); |
||
2764 | |||
2765 | /*-------------------------- USART CR3 Configuration -----------------------*/ |
||
2766 | /* Clear CTSE and RTSE bits */ |
||
2767 | CLEAR_BIT(husart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE)); |
||
2768 | |||
2769 | /*-------------------------- USART BRR Configuration -----------------------*/ |
||
2770 | if((husart->Instance == USART1)) |
||
2771 | { |
||
2772 | pclk = HAL_RCC_GetPCLK2Freq(); |
||
2773 | husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate); |
||
2774 | } |
||
2775 | else |
||
2776 | { |
||
2777 | pclk = HAL_RCC_GetPCLK1Freq(); |
||
2778 | husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate); |
||
2779 | } |
||
2780 | } |
||
2781 | |||
2782 | /** |
||
2783 | * @} |
||
2784 | */ |
||
2785 | |||
2786 | /** |
||
2787 | * @} |
||
2788 | */ |
||
2789 | |||
2790 | #endif /* HAL_USART_MODULE_ENABLED */ |
||
2791 | /** |
||
2792 | * @} |
||
2793 | */ |
||
2794 | |||
2795 | /** |
||
2796 | * @} |
||
2797 | */ |
||
2798 | |||
2799 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |