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