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