Senior Design: Sound Monitor / STM32L4xx_HAL_Driver
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_hal_usart.c Source File

stm32l4xx_hal_usart.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_usart.c
00004   * @author  MCD Application Team
00005   * @version V1.1.0
00006   * @date    16-September-2015
00007   * @brief   USART HAL module driver.
00008   *          This file provides firmware functions to manage the following
00009   *          functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
00010   *          Peripheral (USART).
00011   *           + Initialization and de-initialization functions
00012   *           + IO operation functions
00013   *           + Peripheral Control functions
00014   *           + Peripheral State and Error functions
00015   *
00016   @verbatim
00017  ===============================================================================
00018                         ##### How to use this driver #####
00019  ===============================================================================
00020     [..]
00021       The USART HAL driver can be used as follows:
00022 
00023       (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
00024       (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
00025           (++) Enable the USARTx interface clock.
00026           (++) USART pins configuration:
00027             (+++) Enable the clock for the USART GPIOs.
00028             (+++) Configure these USART pins as alternate function pull-up.
00029           (++) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
00030                 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
00031             (+++) Configure the USARTx interrupt priority.
00032             (+++) Enable the NVIC USART IRQ handle.
00033             (++) USART interrupts handling:
00034               -@@-   The specific USART interrupts (Transmission complete interrupt,
00035                   RXNE interrupt and Error Interrupts) will be managed using the macros
00036                   __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
00037           (++) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
00038                HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
00039             (+++) Declare a DMA handle structure for the Tx/Rx channel.
00040             (+++) Enable the DMAx interface clock.
00041             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
00042             (+++) Configure the DMA Tx/Rx channel.
00043             (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
00044             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
00045 
00046       (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
00047           flow control and Mode (Receiver/Transmitter) in the husart handle Init structure.
00048 
00049       (#) Initialize the USART registers by calling the HAL_USART_Init() API:
00050           (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
00051                by calling the customized HAL_USART_MspInit(&husart) API.
00052 
00053     [..]
00054      (@) To configure and enable/disable the USART to wake up the MCU from stop mode, resort to UART API's
00055         HAL_UARTEx_StopModeWakeUpSourceConfig(), HAL_UARTEx_EnableStopMode() and                 
00056         HAL_UARTEx_DisableStopMode() in casting the USART handle to UART type UART_HandleTypeDef.                
00057 
00058   @endverbatim
00059   ******************************************************************************
00060   * @attention
00061   *
00062   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00063   *
00064   * Redistribution and use in source and binary forms, with or without modification,
00065   * are permitted provided that the following conditions are met:
00066   *   1. Redistributions of source code must retain the above copyright notice,
00067   *      this list of conditions and the following disclaimer.
00068   *   2. Redistributions in binary form must reproduce the above copyright notice,
00069   *      this list of conditions and the following disclaimer in the documentation
00070   *      and/or other materials provided with the distribution.
00071   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00072   *      may be used to endorse or promote products derived from this software
00073   *      without specific prior written permission.
00074   *
00075   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00076   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00077   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00078   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00079   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00080   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00081   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00082   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00083   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00084   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00085   *
00086   ******************************************************************************
00087   */
00088 
00089 /* Includes ------------------------------------------------------------------*/
00090 #include "stm32l4xx_hal.h"
00091 
00092 /** @addtogroup STM32L4xx_HAL_Driver
00093   * @{
00094   */
00095 
00096 /** @defgroup USART USART
00097   * @brief HAL USART Synchronous module driver
00098   * @{
00099   */
00100 
00101 #ifdef HAL_USART_MODULE_ENABLED
00102 
00103 /* Private typedef -----------------------------------------------------------*/
00104 /* Private define ------------------------------------------------------------*/
00105 /** @defgroup USART_Private_Constants USART Private Constants
00106   * @{
00107   */
00108 #define USART_DUMMY_DATA          ((uint16_t) 0xFFFF)           /*!< USART transmitted dummy data                     */
00109 #define USART_TEACK_REACK_TIMEOUT ((uint32_t) 1000)             /*!< USART TX or RX enable acknowledge time-out value */
00110 #define USART_CR1_FIELDS          ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
00111                                               USART_CR1_TE | USART_CR1_RE  | USART_CR1_OVER8))    /*!< USART CR1 fields of parameters set by USART_SetConfig API */
00112 #define USART_CR2_FIELDS          ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | \
00113                                               USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by USART_SetConfig API */
00114 /**
00115   * @}
00116   */
00117 
00118 /* Private macros ------------------------------------------------------------*/
00119 /* Private variables ---------------------------------------------------------*/
00120 /* Private function prototypes -----------------------------------------------*/
00121 /** @addtogroup USART_Private_Functions
00122   * @{
00123   */
00124 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
00125 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
00126 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
00127 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
00128 static void USART_DMAError(DMA_HandleTypeDef *hdma);
00129 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
00130 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart);
00131 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart);
00132 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
00133 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
00134 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
00135 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
00136 /**
00137   * @}
00138   */
00139 
00140 /* Exported functions --------------------------------------------------------*/
00141 
00142 /** @defgroup USART_Exported_Functions USART Exported Functions
00143   * @{
00144   */
00145 
00146 /** @defgroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
00147   *  @brief    Initialization and Configuration functions
00148   *
00149 @verbatim
00150  ===============================================================================
00151             ##### Initialization and Configuration functions #####
00152  ===============================================================================
00153     [..]
00154     This subsection provides a set of functions allowing to initialize the USART
00155     in asynchronous and in synchronous modes.
00156       (+) For the asynchronous mode only these parameters can be configured:
00157         (++) Baud Rate
00158         (++) Word Length
00159         (++) Stop Bit
00160         (++) Parity: If the parity is enabled, then the MSB bit of the data written
00161              in the data register is transmitted but is changed by the parity bit.
00162              Depending on the frame length defined by the M1 and M0 bits (7-bit,
00163              8-bit or 9-bit), the possible USART frame formats are as listed in the
00164              following table:
00165 
00166             (+++)    Table 1. USART frame format.   
00167             (+++)    +-----------------------------------------------------------------------+
00168             (+++)    |  M1 bit |  M0 bit |  PCE bit  |            USART frame                |
00169             (+++)    |---------|---------|-----------|---------------------------------------|
00170             (+++)    |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |
00171             (+++)    |---------|---------|-----------|---------------------------------------|
00172             (+++)    |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
00173             (+++)    |---------|---------|-----------|---------------------------------------|
00174             (+++)    |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |
00175             (+++)    |---------|---------|-----------|---------------------------------------|
00176             (+++)    |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
00177             (+++)    |---------|---------|-----------|---------------------------------------|
00178             (+++)    |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |
00179             (+++)    |---------|---------|-----------|---------------------------------------|
00180             (+++)    |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |
00181             (+++)    +-----------------------------------------------------------------------+
00182         (++) USART polarity
00183         (++) USART phase
00184         (++) USART LastBit
00185         (++) Receiver/transmitter modes
00186 
00187     [..]
00188     The HAL_USART_Init() function follows the USART  synchronous configuration
00189     procedure (details for the procedure are available in reference manual).
00190 
00191 @endverbatim
00192   * @{
00193   */
00194 
00195 /**
00196   * @brief  Initializes the USART mode according to the specified
00197   *         parameters in the USART_InitTypeDef and initialize the associated handle.
00198   * @param husart: USART handle.
00199   * @retval HAL status
00200   */
00201 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
00202 {
00203   /* Check the USART handle allocation */
00204   if(husart == NULL)
00205   {
00206     return HAL_ERROR;
00207   }
00208 
00209   /* Check the parameters */
00210   assert_param(IS_USART_INSTANCE(husart->Instance));
00211 
00212   if(husart->State == HAL_USART_STATE_RESET)
00213   {
00214     /* Allocate lock resource and initialize it */
00215     husart->Lock = HAL_UNLOCKED;
00216 
00217     /* Init the low level hardware : GPIO, CLOCK */
00218     HAL_USART_MspInit(husart);
00219   }
00220 
00221   husart->State = HAL_USART_STATE_BUSY;
00222 
00223   /* Disable the Peripheral */
00224   __HAL_USART_DISABLE(husart);
00225 
00226   /* Set the Usart Communication parameters */
00227   if (USART_SetConfig(husart) == HAL_ERROR)
00228   {
00229     return HAL_ERROR;
00230   }
00231 
00232   /* In Synchronous mode, the following bits must be kept cleared:
00233   - LINEN bit in the USART_CR2 register
00234   - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/
00235   husart->Instance->CR2 &= ~USART_CR2_LINEN;
00236   husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
00237 
00238   /* Enable the Peripheral */
00239   __HAL_USART_ENABLE(husart);
00240 
00241   /* TEACK and/or REACK to check before moving husart->State to Ready */
00242   return (USART_CheckIdleState(husart));
00243 }
00244 
00245 /**
00246   * @brief DeInitialize the USART peripheral.
00247   * @param husart: USART handle.
00248   * @retval HAL status
00249   */
00250 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
00251 {
00252    /* Check the USART handle allocation */
00253   if(husart == NULL)
00254   {
00255     return HAL_ERROR;
00256   }
00257 
00258   /* Check the parameters */
00259   assert_param(IS_USART_INSTANCE(husart->Instance));
00260 
00261   husart->State = HAL_USART_STATE_BUSY;
00262 
00263   husart->Instance->CR1 = 0x0;
00264   husart->Instance->CR2 = 0x0;
00265   husart->Instance->CR3 = 0x0;
00266 
00267   /* DeInit the low level hardware */
00268   HAL_USART_MspDeInit(husart);
00269 
00270   husart->ErrorCode = HAL_USART_ERROR_NONE;
00271   husart->State = HAL_USART_STATE_RESET;
00272 
00273   /* Process Unlock */
00274   __HAL_UNLOCK(husart);
00275 
00276   return HAL_OK;
00277 }
00278 
00279 /**
00280   * @brief Initialize the USART MSP.
00281   * @param husart: USART handle.
00282   * @retval None
00283   */
00284  __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
00285 {
00286   /* NOTE : This function should not be modified, when the callback is needed,
00287             the HAL_USART_MspInit can be implemented in the user file
00288    */
00289 }
00290 
00291 /**
00292   * @brief DeInitialize the USART MSP.
00293   * @param husart: USART handle.
00294   * @retval None
00295   */
00296  __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
00297 {
00298   /* NOTE : This function should not be modified, when the callback is needed,
00299             the HAL_USART_MspDeInit can be implemented in the user file
00300    */
00301 }
00302 
00303 /**
00304   * @}
00305   */
00306 
00307 /** @defgroup USART_Exported_Functions_Group2 IO operation functions
00308   *  @brief   USART Transmit and Receive functions
00309   *
00310 @verbatim
00311  ===============================================================================
00312                       ##### IO operation functions #####
00313  ===============================================================================
00314     [..] This subsection provides a set of functions allowing to manage the USART synchronous
00315     data transfers.
00316 
00317     [..] The USART supports master mode only: it cannot receive or send data related to an input
00318          clock (SCLK is always an output).
00319 
00320     (#) There are two modes of transfer:
00321        (++) Blocking mode: The communication is performed in polling mode.
00322             The HAL status of all data processing is returned by the same function
00323             after finishing transfer.
00324        (++) No-Blocking mode: The communication is performed using Interrupts
00325            or DMA, These API's return the HAL status.
00326            The end of the data processing will be indicated through the
00327            dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
00328            using DMA mode.
00329            The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks
00330            will be executed respectively at the end of the transmit or Receive process
00331            The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected
00332 
00333     (#) Blocking mode API's are :
00334         (++) HAL_USART_Transmit()in simplex mode
00335         (++) HAL_USART_Receive() in full duplex receive only
00336         (++) HAL_USART_TransmitReceive() in full duplex mode
00337 
00338     (#) Non-Blocking mode API's with Interrupt are :
00339         (++) HAL_USART_Transmit_IT()in simplex mode
00340         (++) HAL_USART_Receive_IT() in full duplex receive only
00341         (++) HAL_USART_TransmitReceive_IT()in full duplex mode
00342         (++) HAL_USART_IRQHandler()
00343 
00344     (#) No-Blocking mode API's  with DMA are :
00345         (++) HAL_USART_Transmit_DMA()in simplex mode
00346         (++) HAL_USART_Receive_DMA() in full duplex receive only
00347         (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
00348         (++) HAL_USART_DMAPause()
00349         (++) HAL_USART_DMAResume()
00350         (++) HAL_USART_DMAStop()
00351 
00352     (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
00353         (++) HAL_USART_TxCpltCallback()
00354         (++) HAL_USART_RxCpltCallback()
00355         (++) HAL_USART_TxHalfCpltCallback()
00356         (++) HAL_USART_RxHalfCpltCallback()
00357         (++) HAL_USART_ErrorCallback()
00358         (++) HAL_USART_TxRxCpltCallback()
00359 
00360 @endverbatim
00361   * @{
00362   */
00363 
00364 /**
00365   * @brief  Simplex send an amount of data in blocking mode.
00366   * @param husart: USART handle.
00367   * @param pTxData: Pointer to data buffer.
00368   * @param Size: Amount of data to be sent.
00369   * @param Timeout: Timeout duration.
00370   * @retval HAL status
00371   */
00372 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
00373 {
00374   uint16_t* tmp;
00375 
00376   if(husart->State == HAL_USART_STATE_READY)
00377   {
00378     if((pTxData == NULL) || (Size == 0))
00379     {
00380       return  HAL_ERROR;
00381     }
00382 
00383     /* Process Locked */
00384     __HAL_LOCK(husart);
00385 
00386     husart->ErrorCode = HAL_USART_ERROR_NONE;
00387     husart->State = HAL_USART_STATE_BUSY_TX;
00388 
00389     husart->TxXferSize = Size;
00390     husart->TxXferCount = Size;
00391 
00392     /* Check the remaining data to be sent */
00393     while(husart->TxXferCount > 0)
00394     {
00395       husart->TxXferCount--;
00396       if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
00397         {
00398           return HAL_TIMEOUT;
00399         }
00400       if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
00401       {
00402         tmp = (uint16_t*) pTxData;
00403         husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
00404         pTxData += 2;
00405       }
00406       else
00407       {
00408         husart->Instance->TDR = (*pTxData++ & (uint8_t)0xFF);
00409       }
00410     }
00411 
00412     if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
00413     {
00414       return HAL_TIMEOUT;
00415     }
00416 
00417     husart->State = HAL_USART_STATE_READY;
00418 
00419     /* Process Unlocked */
00420     __HAL_UNLOCK(husart);
00421 
00422     return HAL_OK;
00423   }
00424   else
00425   {
00426     return HAL_BUSY;
00427   }
00428 }
00429 
00430 /**
00431   * @brief Receive an amount of data in blocking mode.
00432   * @note To receive synchronous data, dummy data are simultaneously transmitted.
00433   * @param husart: USART handle.
00434   * @param pRxData: Pointer to data buffer.
00435   * @param Size: Amount of data to be received.
00436   * @param Timeout: Timeout duration.
00437   * @retval HAL status
00438   */
00439 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
00440 {
00441   uint16_t* tmp;
00442   uint16_t uhMask;
00443 
00444   if(husart->State == HAL_USART_STATE_READY)
00445   {
00446     if((pRxData == NULL) || (Size == 0))
00447     {
00448       return  HAL_ERROR;
00449     }
00450     /* Process Locked */
00451     __HAL_LOCK(husart);
00452 
00453     husart->ErrorCode = HAL_USART_ERROR_NONE;
00454     husart->State = HAL_USART_STATE_BUSY_RX;
00455 
00456     husart->RxXferSize = Size;
00457     husart->RxXferCount = Size;
00458 
00459     /* Computation of USART mask to apply to RDR register */
00460     USART_MASK_COMPUTATION(husart);
00461     uhMask = husart->Mask;
00462 
00463     /* as long as data have to be received */
00464     while(husart->RxXferCount > 0)
00465     {
00466       husart->RxXferCount--;
00467 
00468       /* Wait until TC flag is set to send dummy byte in order to generate the
00469       * clock for the slave to send data.
00470        * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
00471        * can be written for all the cases. */
00472       if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
00473       {
00474         return HAL_TIMEOUT;
00475       }
00476       husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x0FF);
00477 
00478       /* Wait for RXNE Flag */
00479       if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
00480       {
00481         return HAL_TIMEOUT;
00482       }
00483 
00484       if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
00485       {
00486         tmp = (uint16_t*) pRxData ;
00487         *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
00488         pRxData +=2;
00489       }
00490       else
00491       {
00492         *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
00493       }
00494     }
00495 
00496     husart->State = HAL_USART_STATE_READY;
00497 
00498     /* Process Unlocked */
00499     __HAL_UNLOCK(husart);
00500 
00501     return HAL_OK;
00502   }
00503   else
00504   {
00505     return HAL_BUSY;
00506   }
00507 }
00508 
00509 /**
00510   * @brief Full-Duplex Send and Receive an amount of data in blocking mode.
00511   * @param husart: USART handle.
00512   * @param pTxData: pointer to TX data buffer.
00513   * @param pRxData: pointer to RX data buffer.
00514   * @param Size: amount of data to be sent (same amount to be received).
00515   * @param Timeout: Timeout duration.
00516   * @retval HAL status
00517   */
00518 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
00519 {
00520   uint16_t* tmp;
00521   uint16_t uhMask;
00522 
00523   if(husart->State == HAL_USART_STATE_READY)
00524   {
00525     if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
00526     {
00527       return  HAL_ERROR;
00528     }
00529     /* Process Locked */
00530     __HAL_LOCK(husart);
00531 
00532     husart->ErrorCode = HAL_USART_ERROR_NONE;
00533     husart->State = HAL_USART_STATE_BUSY_RX;
00534 
00535     husart->RxXferSize = Size;
00536     husart->TxXferSize = Size;
00537     husart->TxXferCount = Size;
00538     husart->RxXferCount = Size;
00539 
00540     /* Computation of USART mask to apply to RDR register */
00541     USART_MASK_COMPUTATION(husart);
00542     uhMask = husart->Mask;
00543 
00544     /* Check the remain data to be sent */
00545     while(husart->TxXferCount > 0)
00546     {
00547       husart->TxXferCount--;
00548       husart->RxXferCount--;
00549 
00550       /* Wait until TC flag is set to send data */
00551       if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
00552       {
00553         return HAL_TIMEOUT;
00554       }
00555       if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
00556       {
00557         tmp = (uint16_t*) pTxData;
00558         husart->Instance->TDR = (*tmp & uhMask);
00559         pTxData += 2;
00560       }
00561       else
00562       {
00563         husart->Instance->TDR = (*pTxData++ & (uint8_t)uhMask);
00564       }
00565 
00566       /* Wait for RXNE Flag */
00567       if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
00568       {
00569         return HAL_TIMEOUT;
00570       }
00571 
00572       if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
00573       {
00574         tmp = (uint16_t*) pRxData ;
00575         *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
00576         pRxData +=2;
00577       }
00578       else
00579       {
00580         *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
00581       }
00582     }
00583 
00584     husart->State = HAL_USART_STATE_READY;
00585 
00586     /* Process Unlocked */
00587     __HAL_UNLOCK(husart);
00588 
00589     return HAL_OK;
00590   }
00591   else
00592   {
00593     return HAL_BUSY;
00594   }
00595 }
00596 
00597 /**
00598   * @brief  Send an amount of data in interrupt mode.
00599   * @param husart: USART handle.
00600   * @param pTxData: pointer to data buffer.
00601   * @param Size: amount of data to be sent.
00602   * @retval HAL status
00603   */
00604 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
00605 {
00606   if(husart->State == HAL_USART_STATE_READY)
00607   {
00608     if((pTxData == NULL ) || (Size == 0))
00609     {
00610       return HAL_ERROR;
00611     }
00612 
00613     /* Process Locked */
00614     __HAL_LOCK(husart);
00615 
00616     husart->pTxBuffPtr = pTxData;
00617     husart->TxXferSize = Size;
00618     husart->TxXferCount = Size;
00619 
00620     husart->ErrorCode = HAL_USART_ERROR_NONE;
00621     husart->State = HAL_USART_STATE_BUSY_TX;
00622 
00623     /* The USART Error Interrupts: (Frame error, noise error, overrun error)
00624     are not managed by the USART Transmit Process to avoid the overrun interrupt
00625     when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
00626     to benefit for the frame error and noise interrupts the usart mode should be
00627     configured only for transmit "USART_MODE_TX" */
00628 
00629     /* Process Unlocked */
00630     __HAL_UNLOCK(husart);
00631 
00632     /* Enable the USART Transmit Data Register Empty Interrupt */
00633     __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
00634 
00635     return HAL_OK;
00636   }
00637   else
00638   {
00639     return HAL_BUSY;
00640   }
00641 }
00642 
00643 /**
00644   * @brief Receive an amount of data in blocking mode.
00645   * @note  To receive synchronous data, dummy data are simultaneously transmitted.
00646   * @param husart: USART handle.
00647   * @param pRxData: pointer to data buffer.
00648   * @param Size: amount of data to be received.
00649   * @retval HAL status
00650   */
00651 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
00652 {
00653   if(husart->State == HAL_USART_STATE_READY)
00654   {
00655     if((pRxData == NULL ) || (Size == 0))
00656     {
00657       return HAL_ERROR;
00658     }
00659     /* Process Locked */
00660     __HAL_LOCK(husart);
00661 
00662     husart->pRxBuffPtr = pRxData;
00663     husart->RxXferSize = Size;
00664     husart->RxXferCount = Size;
00665 
00666     USART_MASK_COMPUTATION(husart);
00667 
00668     husart->ErrorCode = HAL_USART_ERROR_NONE;
00669     husart->State = HAL_USART_STATE_BUSY_RX;
00670 
00671     /* Enable the USART Parity Error Interrupt */
00672     __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
00673 
00674     /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
00675     __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
00676 
00677     /* Enable the USART Data Register not empty Interrupt */
00678     __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
00679 
00680     /* Process Unlocked */
00681     __HAL_UNLOCK(husart);
00682 
00683 
00684     /* Send dummy byte in order to generate the clock for the Slave to send the next data */
00685     if(husart->Init.WordLength == USART_WORDLENGTH_9B)
00686     {
00687       husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x01FF);
00688     }
00689     else
00690     {
00691       husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
00692     }
00693 
00694     return HAL_OK;
00695   }
00696   else
00697   {
00698     return HAL_BUSY;
00699   }
00700 }
00701 
00702 /**
00703   * @brief Full-Duplex Send and Receive an amount of data in interrupt mode.
00704   * @param husart: USART handle.
00705   * @param pTxData: pointer to TX data buffer.
00706   * @param pRxData: pointer to RX data buffer.                         
00707   * @param Size: amount of data to be sent (same amount to be received).
00708   * @retval HAL status
00709   */
00710 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData,  uint16_t Size)
00711 {
00712 
00713   if(husart->State == HAL_USART_STATE_READY)
00714   {
00715     if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
00716     {
00717       return HAL_ERROR;
00718     }
00719     /* Process Locked */
00720     __HAL_LOCK(husart);
00721 
00722     husart->pRxBuffPtr = pRxData;
00723     husart->RxXferSize = Size;
00724     husart->RxXferCount = Size;
00725     husart->pTxBuffPtr = pTxData;
00726     husart->TxXferSize = Size;
00727     husart->TxXferCount = Size;
00728 
00729     /* Computation of USART mask to apply to RDR register */
00730     USART_MASK_COMPUTATION(husart);
00731 
00732     husart->ErrorCode = HAL_USART_ERROR_NONE;
00733     husart->State = HAL_USART_STATE_BUSY_TX_RX;
00734 
00735     /* Enable the USART Data Register not empty Interrupt */
00736     __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
00737 
00738     /* Enable the USART Parity Error Interrupt */
00739     __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
00740 
00741     /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
00742     __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
00743 
00744     /* Process Unlocked */
00745     __HAL_UNLOCK(husart);
00746 
00747     /* Enable the USART Transmit Data Register Empty Interrupt */
00748     __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
00749 
00750     return HAL_OK;
00751   }
00752   else
00753   {
00754     return HAL_BUSY;
00755   }
00756 
00757 }
00758 
00759 /**
00760   * @brief Send an amount of data in DMA mode.
00761   * @param husart: USART handle.
00762   * @param pTxData: pointer to data buffer.
00763   * @param Size: amount of data to be sent.
00764   * @retval HAL status
00765   */
00766 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
00767 {
00768   uint32_t *tmp;
00769 
00770   if(husart->State == HAL_USART_STATE_READY)
00771   {
00772     if((pTxData == NULL ) || (Size == 0))
00773     {
00774       return HAL_ERROR;
00775     }
00776     /* Process Locked */
00777     __HAL_LOCK(husart);
00778 
00779     husart->pTxBuffPtr = pTxData;
00780     husart->TxXferSize = Size;
00781     husart->TxXferCount = Size;
00782 
00783     husart->ErrorCode = HAL_USART_ERROR_NONE;
00784     husart->State = HAL_USART_STATE_BUSY_TX;
00785 
00786     /* Set the USART DMA transfer complete callback */
00787     husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
00788 
00789     /* Set the USART DMA Half transfer complete callback */
00790     husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
00791 
00792     /* Set the DMA error callback */
00793     husart->hdmatx->XferErrorCallback = USART_DMAError;
00794 
00795     /* Enable the USART transmit DMA channel */
00796     tmp = (uint32_t*)&pTxData;
00797     HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
00798 
00799     /* Clear the TC flag in the ICR register */
00800     __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
00801 
00802     /* Enable the DMA transfer for transmit request by setting the DMAT bit
00803        in the USART CR3 register */
00804     husart->Instance->CR3 |= USART_CR3_DMAT;
00805 
00806     /* Process Unlocked */
00807     __HAL_UNLOCK(husart);
00808 
00809     return HAL_OK;
00810   }
00811   else
00812   {
00813     return HAL_BUSY;
00814   }
00815 }
00816 
00817 /**
00818   * @brief Receive an amount of data in DMA mode.
00819   * @param husart: USART handle.
00820   * @param pRxData: pointer to data buffer.
00821   * @param Size: amount of data to be received.
00822   * @note   When the USART parity is enabled (PCE = 1), the received data contain
00823   *         the parity bit (MSB position).
00824   * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave.  
00825   * @retval HAL status
00826   */
00827 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
00828 {
00829   uint32_t *tmp;
00830 
00831   if(husart->State == HAL_USART_STATE_READY)
00832   {
00833     if((pRxData == NULL ) || (Size == 0))
00834     {
00835       return HAL_ERROR;
00836     }
00837 
00838     /* Process Locked */
00839     __HAL_LOCK(husart);
00840 
00841     husart->pRxBuffPtr = pRxData;
00842     husart->RxXferSize = Size;
00843     husart->pTxBuffPtr = pRxData;
00844     husart->TxXferSize = Size;
00845 
00846     husart->ErrorCode = HAL_USART_ERROR_NONE;
00847     husart->State = HAL_USART_STATE_BUSY_RX;
00848 
00849     /* Set the USART DMA Rx transfer complete callback */
00850     husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
00851 
00852     /* Set the USART DMA Half transfer complete callback */
00853     husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
00854 
00855     /* Set the USART DMA Rx transfer error callback */
00856     husart->hdmarx->XferErrorCallback = USART_DMAError;
00857 
00858     /* Enable the USART receive DMA channel */
00859     tmp = (uint32_t*)&pRxData;
00860     HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
00861 
00862     /* Enable the USART transmit DMA channel: the transmit channel is used in order
00863        to generate in the non-blocking mode the clock to the slave device,
00864        this mode isn't a simplex receive mode but a full-duplex receive mode */
00865     tmp = (uint32_t*)&pRxData;
00866     HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
00867 
00868     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
00869        in the USART CR3 register */
00870     husart->Instance->CR3 |= USART_CR3_DMAR;
00871 
00872     /* Enable the DMA transfer for transmit request by setting the DMAT bit
00873        in the USART CR3 register */
00874     husart->Instance->CR3 |= USART_CR3_DMAT;
00875 
00876     /* Process Unlocked */
00877     __HAL_UNLOCK(husart);
00878 
00879     return HAL_OK;
00880   }
00881   else
00882   {
00883     return HAL_BUSY;
00884   }
00885 }
00886 
00887 /**
00888   * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
00889   * @param husart: USART handle.
00890   * @param pTxData: pointer to TX data buffer.
00891   * @param pRxData: pointer to RX data buffer.
00892   * @param Size: amount of data to be received/sent.
00893   * @note   When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
00894   * @retval HAL status
00895   */
00896 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
00897 {
00898   uint32_t *tmp;
00899 
00900   if(husart->State == HAL_USART_STATE_READY)
00901   {
00902     if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
00903     {
00904       return HAL_ERROR;
00905     }
00906     /* Process Locked */
00907     __HAL_LOCK(husart);
00908 
00909     husart->pRxBuffPtr = pRxData;
00910     husart->RxXferSize = Size;
00911     husart->pTxBuffPtr = pTxData;
00912     husart->TxXferSize = Size;
00913 
00914     husart->ErrorCode = HAL_USART_ERROR_NONE;
00915     husart->State = HAL_USART_STATE_BUSY_TX_RX;
00916 
00917     /* Set the USART DMA Rx transfer complete callback */
00918     husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
00919 
00920     /* Set the USART DMA Half transfer complete callback */
00921     husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
00922 
00923     /* Set the USART DMA Tx transfer complete callback */
00924     husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
00925 
00926     /* Set the USART DMA Half transfer complete callback */
00927     husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
00928 
00929     /* Set the USART DMA Tx transfer error callback */
00930     husart->hdmatx->XferErrorCallback = USART_DMAError;
00931 
00932     /* Set the USART DMA Rx transfer error callback */
00933     husart->hdmarx->XferErrorCallback = USART_DMAError;
00934 
00935     /* Enable the USART receive DMA channel */
00936     tmp = (uint32_t*)&pRxData;
00937     HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
00938 
00939     /* Enable the USART transmit DMA channel */
00940     tmp = (uint32_t*)&pTxData;
00941     HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
00942 
00943     /* Clear the TC flag in the ICR register */
00944     __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
00945 
00946     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
00947        in the USART CR3 register */
00948     husart->Instance->CR3 |= USART_CR3_DMAR;
00949 
00950     /* Enable the DMA transfer for transmit request by setting the DMAT bit
00951        in the USART CR3 register */
00952     husart->Instance->CR3 |= USART_CR3_DMAT;
00953 
00954     /* Process Unlocked */
00955     __HAL_UNLOCK(husart);
00956 
00957     return HAL_OK;
00958   }
00959   else
00960   {
00961     return HAL_BUSY;
00962   }
00963 }
00964 
00965 /**
00966   * @brief Pause the DMA Transfer.
00967   * @param husart: USART handle.
00968   * @retval HAL status
00969   */
00970 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
00971 {
00972   /* Process Locked */
00973   __HAL_LOCK(husart);
00974 
00975   if(husart->State == HAL_USART_STATE_BUSY_TX)
00976   {
00977     /* Disable the USART DMA Tx request */
00978     husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
00979   }
00980   else if(husart->State == HAL_USART_STATE_BUSY_RX)
00981   {
00982     /* Disable the USART DMA Rx request */
00983     husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
00984   }
00985   else if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
00986   {
00987     /* Disable the USART DMA Tx request */
00988     husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
00989     /* Disable the USART DMA Rx request */
00990     husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
00991   }
00992 
00993   /* Process Unlocked */
00994   __HAL_UNLOCK(husart);
00995 
00996   return HAL_OK;
00997 }
00998 
00999 /**
01000   * @brief Resume the DMA Transfer.
01001   * @param husart: USART handle.
01002   * @retval HAL status
01003   */
01004 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
01005 {
01006   /* Process Locked */
01007   __HAL_LOCK(husart);
01008 
01009   if(husart->State == HAL_USART_STATE_BUSY_TX)
01010   {
01011     /* Enable the USART DMA Tx request */
01012     husart->Instance->CR3 |= USART_CR3_DMAT;
01013   }
01014   else if(husart->State == HAL_USART_STATE_BUSY_RX)
01015   {
01016     /* Clear the Overrun flag before resuming the Rx transfer*/
01017     __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF);    
01018 
01019     /* Enable the USART DMA Rx request */
01020     husart->Instance->CR3 |= USART_CR3_DMAR;
01021   }
01022   else if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
01023   {
01024     /* Clear the Overrun flag before resuming the Rx transfer*/
01025     __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF);    
01026 
01027     /* Enable the USART DMA Rx request  before the DMA Tx request */
01028     husart->Instance->CR3 |= USART_CR3_DMAR;
01029 
01030     /* Enable the USART DMA Tx request */
01031     husart->Instance->CR3 |= USART_CR3_DMAT;
01032   }
01033 
01034   /* Process Unlocked */
01035   __HAL_UNLOCK(husart);
01036 
01037   return HAL_OK;
01038 }
01039 
01040 /**
01041   * @brief Stop the DMA Transfer.
01042   * @param husart: USART handle.
01043   * @retval HAL status
01044   */
01045 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
01046 {
01047   /* The Lock is not implemented on this API to allow the user application
01048      to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() /
01049      HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback: 
01050      indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete  
01051      interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of 
01052      the stream and the corresponding call back is executed. */  
01053 
01054   /* Disable the USART Tx/Rx DMA requests */
01055   husart->Instance->CR3 &= ~USART_CR3_DMAT;
01056   husart->Instance->CR3 &= ~USART_CR3_DMAR;
01057 
01058   /* Abort the USART DMA tx channel */
01059   if(husart->hdmatx != NULL)
01060   {
01061     HAL_DMA_Abort(husart->hdmatx);
01062   }
01063   /* Abort the USART DMA rx channel */
01064   if(husart->hdmarx != NULL)
01065   {
01066     HAL_DMA_Abort(husart->hdmarx);
01067   }
01068 
01069   husart->State = HAL_USART_STATE_READY;
01070 
01071   return HAL_OK;
01072 }
01073 
01074 /**
01075   * @brief  Handle USART interrupt request.
01076   * @param husart: USART handle.
01077   * @retval None
01078   */
01079 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
01080 {
01081 
01082   /* USART parity error interrupt occurred ------------------------------------*/
01083   if((__HAL_USART_GET_IT(husart, USART_IT_PE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_PE) != RESET))
01084   {
01085     __HAL_USART_CLEAR_IT(husart, USART_CLEAR_PEF);
01086     husart->ErrorCode |= HAL_USART_ERROR_PE;
01087     /* Set the USART state ready to be able to start again the process */
01088     husart->State = HAL_USART_STATE_READY;
01089   }
01090 
01091   /* USART frame error interrupt occurred -------------------------------------*/
01092   if((__HAL_USART_GET_IT(husart, USART_IT_FE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
01093   {
01094     __HAL_USART_CLEAR_IT(husart, USART_CLEAR_FEF);
01095     husart->ErrorCode |= HAL_USART_ERROR_FE;
01096     /* Set the USART state ready to be able to start again the process */
01097     husart->State = HAL_USART_STATE_READY;
01098   }
01099 
01100   /* USART noise error interrupt occurred -------------------------------------*/
01101   if((__HAL_USART_GET_IT(husart, USART_IT_NE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
01102   {
01103     __HAL_USART_CLEAR_IT(husart, USART_CLEAR_NEF);
01104     husart->ErrorCode |= HAL_USART_ERROR_NE;
01105     /* Set the USART state ready to be able to start again the process */
01106     husart->State = HAL_USART_STATE_READY;
01107   }
01108 
01109   /* USART Over-Run interrupt occurred ----------------------------------------*/
01110   if((__HAL_USART_GET_IT(husart, USART_IT_ORE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
01111   {
01112     __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
01113     husart->ErrorCode |= HAL_USART_ERROR_ORE;
01114     /* Set the USART state ready to be able to start again the process */
01115     husart->State = HAL_USART_STATE_READY;
01116   }
01117 
01118    /* Call USART Error Call back function if need be --------------------------*/
01119   if(husart->ErrorCode != HAL_USART_ERROR_NONE)
01120   {
01121     HAL_USART_ErrorCallback(husart);
01122   }
01123 
01124   /* USART in mode Receiver --------------------------------------------------*/
01125   if((__HAL_USART_GET_IT(husart, USART_IT_RXNE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_RXNE) != RESET))
01126   {
01127     if(husart->State == HAL_USART_STATE_BUSY_RX)
01128     {
01129       USART_Receive_IT(husart);
01130     }
01131     else
01132     {
01133       USART_TransmitReceive_IT(husart);
01134     }
01135   }
01136 
01137   /* USART in mode Transmitter -----------------------------------------------*/
01138   if((__HAL_USART_GET_IT(husart, USART_IT_TXE) != RESET) &&(__HAL_USART_GET_IT_SOURCE(husart, USART_IT_TXE) != RESET))
01139   {
01140     if(husart->State == HAL_USART_STATE_BUSY_TX)
01141     {
01142       USART_Transmit_IT(husart);
01143     }
01144     else
01145     {
01146       USART_TransmitReceive_IT(husart);
01147     }
01148   }
01149 
01150   /* USART in mode Transmitter (transmission end) -----------------------------*/
01151   if((__HAL_USART_GET_IT(husart, USART_IT_TC) != RESET) &&(__HAL_USART_GET_IT_SOURCE(husart, USART_IT_TC) != RESET))
01152   {
01153     USART_EndTransmit_IT(husart);
01154   }
01155 
01156 }
01157 
01158 /**
01159   * @brief Tx Transfer completed callback.
01160   * @param husart: USART handle.
01161   * @retval None
01162   */
01163 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
01164 {
01165   /* NOTE : This function should not be modified, when the callback is needed,
01166             the HAL_USART_TxCpltCallback can be implemented in the user file.
01167    */
01168 }
01169 
01170 /**
01171   * @brief  Tx Half Transfer completed callback.
01172   * @param husart: USART handle.
01173   * @retval None
01174   */
01175  __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
01176 {
01177   /* NOTE: This function should not be modified, when the callback is needed,
01178            the HAL_USART_TxHalfCpltCallback can be implemented in the user file.
01179    */
01180 }
01181 
01182 /**
01183   * @brief  Rx Transfer completed callback.
01184   * @param husart: USART handle.
01185   * @retval None
01186   */
01187 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
01188 {
01189   /* NOTE: This function should not be modified, when the callback is needed,
01190            the HAL_USART_RxCpltCallback can be implemented in the user file.
01191    */
01192 }
01193 
01194 /**
01195   * @brief Rx Half Transfer completed callback.
01196   * @param husart: USART handle.
01197   * @retval None
01198   */
01199 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
01200 {
01201   /* NOTE : This function should not be modified, when the callback is needed,
01202             the HAL_USART_RxHalfCpltCallback can be implemented in the user file
01203    */
01204 }
01205 
01206 /**
01207   * @brief Tx/Rx Transfers completed callback for the non-blocking process.
01208   * @param husart: USART handle.
01209   * @retval None
01210   */
01211 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
01212 {
01213   /* NOTE : This function should not be modified, when the callback is needed,
01214             the HAL_USART_TxRxCpltCallback can be implemented in the user file
01215    */
01216 }
01217 
01218 /**
01219   * @brief USART error callback.
01220   * @param husart: USART handle.
01221   * @retval None
01222   */
01223 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
01224 {
01225   /* NOTE : This function should not be modified, when the callback is needed,
01226             the HAL_USART_ErrorCallback can be implemented in the user file.
01227    */
01228 }
01229 
01230 /**
01231   * @}
01232   */
01233 
01234 /** @defgroup USART_Exported_Functions_Group4 Peripheral State and Error functions
01235  *  @brief   USART Peripheral State and Error functions
01236  *
01237 @verbatim
01238   ==============================================================================
01239             ##### Peripheral State and Error functions #####
01240   ==============================================================================
01241     [..]
01242     This subsection provides functions allowing to :
01243       (+) Return the USART handle state
01244       (+) Return the USART handle error code
01245 
01246 @endverbatim
01247   * @{
01248   */
01249 
01250 
01251 /**
01252   * @brief Return the USART handle state.
01253   * @param husart : pointer to a USART_HandleTypeDef structure that contains
01254   *              the configuration information for the specified USART.
01255   * @retval USART handle state
01256   */
01257 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
01258 {
01259   return husart->State;
01260 }
01261 
01262 /**
01263   * @brief Return the USART error code.
01264   * @param husart : pointer to a USART_HandleTypeDef structure that contains
01265   *              the configuration information for the specified USART.
01266   * @retval USART handle Error Code
01267   */
01268 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
01269 {
01270   return husart->ErrorCode;
01271 }
01272 
01273 /**
01274   * @}
01275   */
01276 
01277 /**
01278   * @}
01279   */
01280 
01281 /** @defgroup USART_Private_Functions USART Private Functions
01282  * @{
01283  */
01284 
01285 /**
01286   * @brief DMA USART transmit process complete callback.
01287   * @param  hdma: DMA handle.
01288   * @retval None
01289   */
01290 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
01291 {
01292   USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
01293 
01294   /* DMA Normal mode */
01295   if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
01296   {  
01297     husart->TxXferCount = 0;
01298 
01299     if(husart->State == HAL_USART_STATE_BUSY_TX)
01300     {
01301       /* Disable the DMA transfer for transmit request by resetting the DMAT bit
01302          in the USART CR3 register */
01303       husart->Instance->CR3 &= ~(USART_CR3_DMAT);
01304 
01305       /* Enable the USART Transmit Complete Interrupt */
01306       __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
01307     }
01308   }
01309   /* DMA Circular mode */
01310   else
01311   {
01312     if(husart->State == HAL_USART_STATE_BUSY_TX)
01313     {
01314     HAL_USART_TxCpltCallback(husart);
01315    }
01316  }
01317 }
01318 
01319 
01320 /**
01321   * @brief DMA USART transmit process half complete callback.
01322   * @param hdma : DMA handle.
01323   * @retval None
01324   */
01325 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
01326 {
01327   USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
01328 
01329   HAL_USART_TxHalfCpltCallback(husart);
01330 }
01331 
01332 /**
01333   * @brief DMA USART receive process complete callback.
01334   * @param  hdma: DMA handle.
01335   * @retval None
01336   */
01337 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
01338 {
01339   USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
01340 
01341   /* DMA Normal mode */
01342   if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
01343   {
01344     husart->RxXferCount = 0;
01345 
01346     /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
01347     in USART CR3 register */
01348     husart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
01349     /* similarly, disable the DMA TX transfer that was started to provide the
01350        clock to the slave device */
01351     husart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
01352 
01353       if(husart->State == HAL_USART_STATE_BUSY_RX)
01354       {
01355         HAL_USART_RxCpltCallback(husart);
01356       }
01357       /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
01358       else
01359       {
01360         HAL_USART_TxRxCpltCallback(husart);
01361       }
01362     husart->State= HAL_USART_STATE_READY;
01363   }
01364   /* DMA circular mode */
01365   else
01366   {
01367     if(husart->State == HAL_USART_STATE_BUSY_RX)
01368     {
01369       HAL_USART_RxCpltCallback(husart);
01370     }
01371     /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
01372     else
01373     {
01374       HAL_USART_TxRxCpltCallback(husart);
01375     }
01376   }    
01377 
01378 }
01379 
01380 /**
01381   * @brief DMA USART receive process half complete callback.
01382   * @param hdma : DMA handle.
01383   * @retval None
01384   */
01385 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
01386 {
01387   USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
01388 
01389   HAL_USART_RxHalfCpltCallback(husart);
01390 }
01391 
01392 /**
01393   * @brief DMA USART communication error callback.
01394   * @param  hdma: DMA handle.
01395   * @retval None
01396   */
01397 static void USART_DMAError(DMA_HandleTypeDef *hdma)
01398 {
01399   USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
01400 
01401   husart->RxXferCount = 0;
01402   husart->TxXferCount = 0;
01403   husart->ErrorCode |= HAL_USART_ERROR_DMA;
01404   husart->State= HAL_USART_STATE_READY;
01405 
01406   HAL_USART_ErrorCallback(husart);
01407 }
01408 
01409 /**
01410   * @brief  Handle USART Communication Timeout.
01411   * @param husart: USART handle.
01412   * @param  Flag: specifies the USART flag to check.
01413   * @param  Status: the Flag status (SET or RESET).
01414   * @param  Timeout: timeout duration.
01415   * @retval HAL status
01416   */
01417 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
01418 {
01419   uint32_t tickstart = HAL_GetTick();
01420 
01421   /* Wait until flag is set */
01422   if(Status == RESET)
01423   {
01424     while(__HAL_USART_GET_FLAG(husart, Flag) == RESET)
01425     {
01426       /* Check for the Timeout */
01427       if(Timeout != HAL_MAX_DELAY)
01428       {
01429         if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
01430         {
01431           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
01432           __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
01433           __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
01434           __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
01435           __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
01436 
01437           husart->State= HAL_USART_STATE_READY;
01438 
01439           /* Process Unlocked */
01440           __HAL_UNLOCK(husart);
01441 
01442           return HAL_TIMEOUT;
01443         }
01444       }
01445     }
01446   }
01447   else
01448   {
01449     while(__HAL_USART_GET_FLAG(husart, Flag) != RESET)
01450     {
01451       /* Check for the Timeout */
01452       if(Timeout != HAL_MAX_DELAY)
01453       {
01454         if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
01455         {
01456           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
01457           __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
01458           __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
01459           __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
01460           __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
01461 
01462           husart->State= HAL_USART_STATE_READY;
01463 
01464           /* Process Unlocked */
01465           __HAL_UNLOCK(husart);
01466 
01467           return HAL_TIMEOUT;
01468         }
01469       }
01470     }
01471   }
01472   return HAL_OK;
01473 }
01474 
01475 /**
01476   * @brief Configure the USART peripheral.
01477   * @param husart: USART handle.
01478   * @retval HAL status
01479   */
01480 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart)
01481 {
01482   uint32_t tmpreg                      = 0x0;
01483   USART_ClockSourceTypeDef clocksource = USART_CLOCKSOURCE_UNDEFINED;
01484   HAL_StatusTypeDef ret                = HAL_OK;
01485   uint16_t brrtemp                     = 0x0000;
01486   uint16_t usartdiv                    = 0x0000;  
01487 
01488   /* Check the parameters */
01489   assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
01490   assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
01491   assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
01492   assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
01493   assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
01494   assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
01495   assert_param(IS_USART_PARITY(husart->Init.Parity));
01496   assert_param(IS_USART_MODE(husart->Init.Mode));
01497 
01498 
01499   /*-------------------------- USART CR1 Configuration -----------------------*/
01500    /* Clear M, PCE, PS, TE and RE bits and configure
01501    *  the USART Word Length, Parity and Mode:
01502    *  set the M bits according to husart->Init.WordLength value
01503    *  set PCE and PS bits according to husart->Init.Parity value
01504    *  set TE and RE bits according to husart->Init.Mode value 
01505    *  force OVER8 to 1 to allow to reach the maximum speed (Fclock/8)  */
01506   tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
01507   MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
01508 
01509   /*---------------------------- USART CR2 Configuration ---------------------*/
01510   /* Clear and configure the USART Clock, CPOL, CPHA, LBCL and STOP bits:
01511    * set CPOL bit according to husart->Init.CLKPolarity value
01512    * set CPHA bit according to husart->Init.CLKPhase value
01513    * set LBCL bit according to husart->Init.CLKLastBit value
01514    * set STOP[13:12] bits according to husart->Init.StopBits value */
01515   tmpreg = (uint32_t)(USART_CLOCK_ENABLE);
01516   tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase);
01517   tmpreg |= ((uint32_t)husart->Init.CLKLastBit | (uint32_t)husart->Init.StopBits);
01518   MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg);
01519 
01520   /*-------------------------- USART CR3 Configuration -----------------------*/
01521   /* no CR3 register configuration                                            */
01522 
01523   /*-------------------------- USART BRR Configuration -----------------------*/
01524   /* BRR is filled-up according to OVER8 bit setting which is forced to 1     */ 
01525   USART_GETCLOCKSOURCE(husart, clocksource);
01526   switch (clocksource)
01527   {
01528     case USART_CLOCKSOURCE_PCLK1:
01529       usartdiv = (uint16_t)((2*HAL_RCC_GetPCLK1Freq()) / husart->Init.BaudRate);
01530       break;
01531     case USART_CLOCKSOURCE_PCLK2:
01532       usartdiv = (uint16_t)((2*HAL_RCC_GetPCLK2Freq()) / husart->Init.BaudRate);
01533       break;
01534     case USART_CLOCKSOURCE_HSI:
01535       usartdiv = (uint16_t)((2*HSI_VALUE) / husart->Init.BaudRate);
01536       break;
01537     case USART_CLOCKSOURCE_SYSCLK:
01538       usartdiv = (uint16_t)((2*HAL_RCC_GetSysClockFreq()) / husart->Init.BaudRate);
01539       break;
01540     case USART_CLOCKSOURCE_LSE:
01541       usartdiv = (uint16_t)((2*LSE_VALUE) / husart->Init.BaudRate);
01542       break;
01543     case USART_CLOCKSOURCE_UNDEFINED:
01544     default:
01545       ret = HAL_ERROR;
01546       break;
01547   }
01548   
01549   brrtemp = usartdiv & 0xFFF0;
01550   brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000F) >> 1U);
01551   husart->Instance->BRR = brrtemp;
01552 
01553   return ret;
01554 }
01555 
01556 /**
01557   * @brief Check the USART Idle State.
01558   * @param husart: USART handle.
01559   * @retval HAL status
01560   */
01561 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart)
01562 {
01563   /* Initialize the USART ErrorCode */
01564   husart->ErrorCode = HAL_USART_ERROR_NONE;
01565 
01566   /* Check if the Transmitter is enabled */
01567   if((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
01568   {
01569     /* Wait until TEACK flag is set */
01570     if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
01571     {
01572       /* Timeout occurred */
01573       return HAL_TIMEOUT;
01574     }
01575   }
01576   /* Check if the Receiver is enabled */
01577   if((husart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
01578   {
01579     /* Wait until REACK flag is set */
01580     if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_REACK, RESET, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
01581     {
01582       /* Timeout occurred */
01583       return HAL_TIMEOUT;
01584     }
01585   }
01586 
01587   /* Initialize the USART state*/
01588   husart->State= HAL_USART_STATE_READY;
01589 
01590   /* Process Unlocked */
01591   __HAL_UNLOCK(husart);
01592 
01593   return HAL_OK;
01594 }
01595 
01596 /**
01597   * @brief  Simplex send an amount of data in non-blocking mode.
01598   * @note   Function called under interruption only, once
01599   *         interruptions have been enabled by HAL_USART_Transmit_IT().
01600   * @note   The USART errors are not managed to avoid the overrun error.
01601   * @param husart: USART handle.
01602   * @retval HAL status
01603   */
01604 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
01605 {
01606   uint16_t* tmp;
01607 
01608   if(husart->State == HAL_USART_STATE_BUSY_TX)
01609   {
01610 
01611     if(husart->TxXferCount == 0)
01612     {
01613       /* Disable the USART Transmit Complete Interrupt */
01614       __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
01615 
01616       /* Enable the USART Transmit Complete Interrupt */
01617       __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
01618 
01619       return HAL_OK;
01620     }
01621     else
01622     {
01623       if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01624       {
01625         tmp = (uint16_t*) husart->pTxBuffPtr;
01626         husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
01627         husart->pTxBuffPtr += 2;
01628       }
01629       else
01630       {
01631         husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0xFF);
01632       }
01633 
01634       husart->TxXferCount--;
01635 
01636       return HAL_OK;
01637     }
01638   }
01639   else
01640   {
01641     return HAL_BUSY;
01642   }
01643 }
01644 
01645 
01646 /**
01647   * @brief  Wraps up transmission in non-blocking mode.
01648   * @param  husart: pointer to a USART_HandleTypeDef structure that contains
01649   *                the configuration information for the specified USART module.
01650   * @retval HAL status
01651   */
01652 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
01653 {
01654   /* Disable the USART Transmit Complete Interrupt */
01655   __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
01656 
01657   /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01658   __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
01659 
01660   husart->State = HAL_USART_STATE_READY;
01661 
01662   HAL_USART_TxCpltCallback(husart);
01663 
01664   return HAL_OK;
01665 }
01666 
01667 
01668 /**
01669   * @brief  Simplex receive an amount of data in non-blocking mode.
01670   * @note   Function called under interruption only, once
01671   *         interruptions have been enabled by HAL_USART_Receive_IT().
01672   * @param husart: USART handle
01673   * @retval HAL status
01674   */
01675 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
01676 {
01677   uint16_t* tmp;
01678   uint16_t uhMask = husart->Mask;
01679 
01680   if(husart->State == HAL_USART_STATE_BUSY_RX)
01681   {
01682 
01683     if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01684     {
01685       tmp = (uint16_t*) husart->pRxBuffPtr;
01686       *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
01687       husart->pRxBuffPtr += 2;
01688     }
01689     else
01690     {
01691       *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
01692     }
01693       /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
01694       husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
01695 
01696     if(--husart->RxXferCount == 0)
01697     {
01698       __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
01699 
01700       /* Disable the USART Parity Error Interrupt */
01701       __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
01702 
01703       /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01704       __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
01705 
01706       husart->State = HAL_USART_STATE_READY;
01707 
01708       HAL_USART_RxCpltCallback(husart);
01709 
01710       return HAL_OK;
01711     }
01712 
01713     return HAL_OK;
01714   }
01715   else
01716   {
01717     return HAL_BUSY;
01718   }
01719 }
01720 
01721 /**
01722   * @brief  Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
01723   * @note   Function called under interruption only, once
01724   *         interruptions have been enabled by HAL_USART_TransmitReceive_IT().
01725   * @param husart: USART handle.
01726   * @retval HAL status
01727   */
01728 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
01729 {
01730   uint16_t* tmp;
01731   uint16_t uhMask = husart->Mask;
01732 
01733   if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
01734   {
01735 
01736     if(husart->TxXferCount != 0x00)
01737     {
01738       if(__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
01739       {
01740         if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01741         {
01742           tmp = (uint16_t*) husart->pTxBuffPtr;
01743           husart->Instance->TDR = (uint16_t)(*tmp & uhMask);
01744           husart->pTxBuffPtr += 2;
01745         }
01746         else
01747         {
01748           husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)uhMask);
01749         }
01750         husart->TxXferCount--;
01751 
01752         /* Check the latest data transmitted */
01753         if(husart->TxXferCount == 0)
01754         {
01755            __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
01756         }
01757       }
01758     }
01759 
01760     if(husart->RxXferCount != 0x00)
01761     {
01762       if(__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
01763       {
01764         if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01765         {
01766           tmp = (uint16_t*) husart->pRxBuffPtr;
01767           *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
01768           husart->pRxBuffPtr += 2;
01769         }
01770         else
01771         {
01772           *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
01773         }
01774         husart->RxXferCount--;
01775       }
01776     }
01777 
01778     /* Check the latest data received */
01779     if(husart->RxXferCount == 0)
01780     {
01781       __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
01782 
01783       /* Disable the USART Parity Error Interrupt */
01784       __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
01785 
01786       /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01787       __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
01788 
01789       husart->State = HAL_USART_STATE_READY;
01790 
01791       HAL_USART_TxRxCpltCallback(husart);
01792 
01793       return HAL_OK;
01794     }
01795 
01796     return HAL_OK;
01797   }
01798   else
01799   {
01800     return HAL_BUSY;
01801   }
01802 }
01803 
01804 /**
01805   * @}
01806   */
01807 
01808 #endif /* HAL_USART_MODULE_ENABLED */
01809 /**
01810   * @}
01811   */
01812 
01813 /**
01814   * @}
01815   */
01816 
01817 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
01818