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