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