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