Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HAL_L476/stm32l4xx_hal_uart.c@1:d0dfbce63a89, 2017-02-24 (annotated)
- Committer:
- elmot
- Date:
- Fri Feb 24 21:13:56 2017 +0000
- Revision:
- 1:d0dfbce63a89
Ready-to-copy
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elmot | 1:d0dfbce63a89 | 1 | /** |
elmot | 1:d0dfbce63a89 | 2 | ****************************************************************************** |
elmot | 1:d0dfbce63a89 | 3 | * @file stm32l4xx_hal_uart.c |
elmot | 1:d0dfbce63a89 | 4 | * @author MCD Application Team |
elmot | 1:d0dfbce63a89 | 5 | * @version V1.5.1 |
elmot | 1:d0dfbce63a89 | 6 | * @date 31-May-2016 |
elmot | 1:d0dfbce63a89 | 7 | * @brief UART HAL module driver. |
elmot | 1:d0dfbce63a89 | 8 | * This file provides firmware functions to manage the following |
elmot | 1:d0dfbce63a89 | 9 | * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART). |
elmot | 1:d0dfbce63a89 | 10 | * + Initialization and de-initialization functions |
elmot | 1:d0dfbce63a89 | 11 | * + IO operation functions |
elmot | 1:d0dfbce63a89 | 12 | * + Peripheral Control functions |
elmot | 1:d0dfbce63a89 | 13 | * |
elmot | 1:d0dfbce63a89 | 14 | * |
elmot | 1:d0dfbce63a89 | 15 | @verbatim |
elmot | 1:d0dfbce63a89 | 16 | =============================================================================== |
elmot | 1:d0dfbce63a89 | 17 | ##### How to use this driver ##### |
elmot | 1:d0dfbce63a89 | 18 | =============================================================================== |
elmot | 1:d0dfbce63a89 | 19 | [..] |
elmot | 1:d0dfbce63a89 | 20 | The UART HAL driver can be used as follows: |
elmot | 1:d0dfbce63a89 | 21 | |
elmot | 1:d0dfbce63a89 | 22 | (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart). |
elmot | 1:d0dfbce63a89 | 23 | (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API: |
elmot | 1:d0dfbce63a89 | 24 | (++) Enable the USARTx interface clock. |
elmot | 1:d0dfbce63a89 | 25 | (++) UART pins configuration: |
elmot | 1:d0dfbce63a89 | 26 | (+++) Enable the clock for the UART GPIOs. |
elmot | 1:d0dfbce63a89 | 27 | (+++) Configure these UART pins as alternate function pull-up. |
elmot | 1:d0dfbce63a89 | 28 | (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT() |
elmot | 1:d0dfbce63a89 | 29 | and HAL_UART_Receive_IT() APIs): |
elmot | 1:d0dfbce63a89 | 30 | (+++) Configure the USARTx interrupt priority. |
elmot | 1:d0dfbce63a89 | 31 | (+++) Enable the NVIC USART IRQ handle. |
elmot | 1:d0dfbce63a89 | 32 | (++) UART interrupts handling: |
elmot | 1:d0dfbce63a89 | 33 | -@@- The specific UART interrupts (Transmission complete interrupt, |
elmot | 1:d0dfbce63a89 | 34 | RXNE interrupt and Error Interrupts) are managed using the macros |
elmot | 1:d0dfbce63a89 | 35 | __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit and receive processes. |
elmot | 1:d0dfbce63a89 | 36 | (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA() |
elmot | 1:d0dfbce63a89 | 37 | and HAL_UART_Receive_DMA() APIs): |
elmot | 1:d0dfbce63a89 | 38 | (+++) Declare a DMA handle structure for the Tx/Rx channel. |
elmot | 1:d0dfbce63a89 | 39 | (+++) Enable the DMAx interface clock. |
elmot | 1:d0dfbce63a89 | 40 | (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. |
elmot | 1:d0dfbce63a89 | 41 | (+++) Configure the DMA Tx/Rx channel. |
elmot | 1:d0dfbce63a89 | 42 | (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle. |
elmot | 1:d0dfbce63a89 | 43 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel. |
elmot | 1:d0dfbce63a89 | 44 | |
elmot | 1:d0dfbce63a89 | 45 | (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware |
elmot | 1:d0dfbce63a89 | 46 | flow control and Mode (Receiver/Transmitter) in the huart handle Init structure. |
elmot | 1:d0dfbce63a89 | 47 | |
elmot | 1:d0dfbce63a89 | 48 | (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...) |
elmot | 1:d0dfbce63a89 | 49 | in the huart handle AdvancedInit structure. |
elmot | 1:d0dfbce63a89 | 50 | |
elmot | 1:d0dfbce63a89 | 51 | (#) For the UART asynchronous mode, initialize the UART registers by calling |
elmot | 1:d0dfbce63a89 | 52 | the HAL_UART_Init() API. |
elmot | 1:d0dfbce63a89 | 53 | |
elmot | 1:d0dfbce63a89 | 54 | (#) For the UART Half duplex mode, initialize the UART registers by calling |
elmot | 1:d0dfbce63a89 | 55 | the HAL_HalfDuplex_Init() API. |
elmot | 1:d0dfbce63a89 | 56 | |
elmot | 1:d0dfbce63a89 | 57 | (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers |
elmot | 1:d0dfbce63a89 | 58 | by calling the HAL_LIN_Init() API. |
elmot | 1:d0dfbce63a89 | 59 | |
elmot | 1:d0dfbce63a89 | 60 | (#) For the UART Multiprocessor mode, initialize the UART registers |
elmot | 1:d0dfbce63a89 | 61 | by calling the HAL_MultiProcessor_Init() API. |
elmot | 1:d0dfbce63a89 | 62 | |
elmot | 1:d0dfbce63a89 | 63 | (#) For the UART RS485 Driver Enabled mode, initialize the UART registers |
elmot | 1:d0dfbce63a89 | 64 | by calling the HAL_RS485Ex_Init() API. |
elmot | 1:d0dfbce63a89 | 65 | |
elmot | 1:d0dfbce63a89 | 66 | [..] |
elmot | 1:d0dfbce63a89 | 67 | (@) These API's (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(), |
elmot | 1:d0dfbce63a89 | 68 | also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by |
elmot | 1:d0dfbce63a89 | 69 | calling the customized HAL_UART_MspInit() API. |
elmot | 1:d0dfbce63a89 | 70 | |
elmot | 1:d0dfbce63a89 | 71 | @endverbatim |
elmot | 1:d0dfbce63a89 | 72 | ****************************************************************************** |
elmot | 1:d0dfbce63a89 | 73 | * @attention |
elmot | 1:d0dfbce63a89 | 74 | * |
elmot | 1:d0dfbce63a89 | 75 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
elmot | 1:d0dfbce63a89 | 76 | * |
elmot | 1:d0dfbce63a89 | 77 | * Redistribution and use in source and binary forms, with or without modification, |
elmot | 1:d0dfbce63a89 | 78 | * are permitted provided that the following conditions are met: |
elmot | 1:d0dfbce63a89 | 79 | * 1. Redistributions of source code must retain the above copyright notice, |
elmot | 1:d0dfbce63a89 | 80 | * this list of conditions and the following disclaimer. |
elmot | 1:d0dfbce63a89 | 81 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
elmot | 1:d0dfbce63a89 | 82 | * this list of conditions and the following disclaimer in the documentation |
elmot | 1:d0dfbce63a89 | 83 | * and/or other materials provided with the distribution. |
elmot | 1:d0dfbce63a89 | 84 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
elmot | 1:d0dfbce63a89 | 85 | * may be used to endorse or promote products derived from this software |
elmot | 1:d0dfbce63a89 | 86 | * without specific prior written permission. |
elmot | 1:d0dfbce63a89 | 87 | * |
elmot | 1:d0dfbce63a89 | 88 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
elmot | 1:d0dfbce63a89 | 89 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
elmot | 1:d0dfbce63a89 | 90 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
elmot | 1:d0dfbce63a89 | 91 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
elmot | 1:d0dfbce63a89 | 92 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
elmot | 1:d0dfbce63a89 | 93 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
elmot | 1:d0dfbce63a89 | 94 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
elmot | 1:d0dfbce63a89 | 95 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
elmot | 1:d0dfbce63a89 | 96 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
elmot | 1:d0dfbce63a89 | 97 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
elmot | 1:d0dfbce63a89 | 98 | * |
elmot | 1:d0dfbce63a89 | 99 | ****************************************************************************** |
elmot | 1:d0dfbce63a89 | 100 | */ |
elmot | 1:d0dfbce63a89 | 101 | |
elmot | 1:d0dfbce63a89 | 102 | /* Includes ------------------------------------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 103 | #include "stm32l4xx_hal.h" |
elmot | 1:d0dfbce63a89 | 104 | |
elmot | 1:d0dfbce63a89 | 105 | /** @addtogroup STM32L4xx_HAL_Driver |
elmot | 1:d0dfbce63a89 | 106 | * @{ |
elmot | 1:d0dfbce63a89 | 107 | */ |
elmot | 1:d0dfbce63a89 | 108 | |
elmot | 1:d0dfbce63a89 | 109 | /** @defgroup UART UART |
elmot | 1:d0dfbce63a89 | 110 | * @brief HAL UART module driver |
elmot | 1:d0dfbce63a89 | 111 | * @{ |
elmot | 1:d0dfbce63a89 | 112 | */ |
elmot | 1:d0dfbce63a89 | 113 | |
elmot | 1:d0dfbce63a89 | 114 | #ifdef HAL_UART_MODULE_ENABLED |
elmot | 1:d0dfbce63a89 | 115 | |
elmot | 1:d0dfbce63a89 | 116 | /* Private typedef -----------------------------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 117 | /* Private define ------------------------------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 118 | /** @defgroup UART_Private_Constants UART Private Constants |
elmot | 1:d0dfbce63a89 | 119 | * @{ |
elmot | 1:d0dfbce63a89 | 120 | */ |
elmot | 1:d0dfbce63a89 | 121 | #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \ |
elmot | 1:d0dfbce63a89 | 122 | USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */ |
elmot | 1:d0dfbce63a89 | 123 | |
elmot | 1:d0dfbce63a89 | 124 | #define UART_LPUART_BRR_MIN ((uint32_t)0x00000300) /* LPUART BRR minimum authorized value */ |
elmot | 1:d0dfbce63a89 | 125 | #define UART_LPUART_BRR_MAX ((uint32_t)0x000FFFFF) /* LPUART BRR maximum authorized value */ |
elmot | 1:d0dfbce63a89 | 126 | /** |
elmot | 1:d0dfbce63a89 | 127 | * @} |
elmot | 1:d0dfbce63a89 | 128 | */ |
elmot | 1:d0dfbce63a89 | 129 | |
elmot | 1:d0dfbce63a89 | 130 | /* Private macros ------------------------------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 131 | /* Private variables ---------------------------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 132 | /* Private function prototypes -----------------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 133 | /** @addtogroup UART_Private_Functions |
elmot | 1:d0dfbce63a89 | 134 | * @{ |
elmot | 1:d0dfbce63a89 | 135 | */ |
elmot | 1:d0dfbce63a89 | 136 | static void UART_EndTxTransfer(UART_HandleTypeDef *huart); |
elmot | 1:d0dfbce63a89 | 137 | static void UART_EndRxTransfer(UART_HandleTypeDef *huart); |
elmot | 1:d0dfbce63a89 | 138 | static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma); |
elmot | 1:d0dfbce63a89 | 139 | static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma); |
elmot | 1:d0dfbce63a89 | 140 | static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma); |
elmot | 1:d0dfbce63a89 | 141 | static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma); |
elmot | 1:d0dfbce63a89 | 142 | static void UART_DMAError(DMA_HandleTypeDef *hdma); |
elmot | 1:d0dfbce63a89 | 143 | static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma); |
elmot | 1:d0dfbce63a89 | 144 | static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart); |
elmot | 1:d0dfbce63a89 | 145 | static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart); |
elmot | 1:d0dfbce63a89 | 146 | static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart); |
elmot | 1:d0dfbce63a89 | 147 | /** |
elmot | 1:d0dfbce63a89 | 148 | * @} |
elmot | 1:d0dfbce63a89 | 149 | */ |
elmot | 1:d0dfbce63a89 | 150 | |
elmot | 1:d0dfbce63a89 | 151 | /* Exported functions --------------------------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 152 | |
elmot | 1:d0dfbce63a89 | 153 | /** @defgroup UART_Exported_Functions UART Exported Functions |
elmot | 1:d0dfbce63a89 | 154 | * @{ |
elmot | 1:d0dfbce63a89 | 155 | */ |
elmot | 1:d0dfbce63a89 | 156 | |
elmot | 1:d0dfbce63a89 | 157 | /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions |
elmot | 1:d0dfbce63a89 | 158 | * @brief Initialization and Configuration functions |
elmot | 1:d0dfbce63a89 | 159 | * |
elmot | 1:d0dfbce63a89 | 160 | @verbatim |
elmot | 1:d0dfbce63a89 | 161 | =============================================================================== |
elmot | 1:d0dfbce63a89 | 162 | ##### Initialization and Configuration functions ##### |
elmot | 1:d0dfbce63a89 | 163 | =============================================================================== |
elmot | 1:d0dfbce63a89 | 164 | [..] |
elmot | 1:d0dfbce63a89 | 165 | This subsection provides a set of functions allowing to initialize the USARTx or the UARTy |
elmot | 1:d0dfbce63a89 | 166 | in asynchronous mode. |
elmot | 1:d0dfbce63a89 | 167 | (+) For the asynchronous mode the parameters below can be configured: |
elmot | 1:d0dfbce63a89 | 168 | (++) Baud Rate |
elmot | 1:d0dfbce63a89 | 169 | (++) Word Length |
elmot | 1:d0dfbce63a89 | 170 | (++) Stop Bit |
elmot | 1:d0dfbce63a89 | 171 | (++) Parity: If the parity is enabled, then the MSB bit of the data written |
elmot | 1:d0dfbce63a89 | 172 | in the data register is transmitted but is changed by the parity bit. |
elmot | 1:d0dfbce63a89 | 173 | (++) Hardware flow control |
elmot | 1:d0dfbce63a89 | 174 | (++) Receiver/transmitter modes |
elmot | 1:d0dfbce63a89 | 175 | (++) Over Sampling Method |
elmot | 1:d0dfbce63a89 | 176 | (++) One-Bit Sampling Method |
elmot | 1:d0dfbce63a89 | 177 | (+) For the asynchronous mode, the following advanced features can be configured as well: |
elmot | 1:d0dfbce63a89 | 178 | (++) TX and/or RX pin level inversion |
elmot | 1:d0dfbce63a89 | 179 | (++) data logical level inversion |
elmot | 1:d0dfbce63a89 | 180 | (++) RX and TX pins swap |
elmot | 1:d0dfbce63a89 | 181 | (++) RX overrun detection disabling |
elmot | 1:d0dfbce63a89 | 182 | (++) DMA disabling on RX error |
elmot | 1:d0dfbce63a89 | 183 | (++) MSB first on communication line |
elmot | 1:d0dfbce63a89 | 184 | (++) auto Baud rate detection |
elmot | 1:d0dfbce63a89 | 185 | [..] |
elmot | 1:d0dfbce63a89 | 186 | The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API |
elmot | 1:d0dfbce63a89 | 187 | follow respectively the UART asynchronous, UART Half duplex, UART LIN mode |
elmot | 1:d0dfbce63a89 | 188 | and UART multiprocessor mode configuration procedures (details for the procedures |
elmot | 1:d0dfbce63a89 | 189 | are available in reference manual). |
elmot | 1:d0dfbce63a89 | 190 | |
elmot | 1:d0dfbce63a89 | 191 | @endverbatim |
elmot | 1:d0dfbce63a89 | 192 | |
elmot | 1:d0dfbce63a89 | 193 | Depending on the frame length defined by the M1 and M0 bits (7-bit, |
elmot | 1:d0dfbce63a89 | 194 | 8-bit or 9-bit), the possible UART formats are listed in the |
elmot | 1:d0dfbce63a89 | 195 | following table. |
elmot | 1:d0dfbce63a89 | 196 | |
elmot | 1:d0dfbce63a89 | 197 | Table 1. UART frame format. |
elmot | 1:d0dfbce63a89 | 198 | +-----------------------------------------------------------------------+ |
elmot | 1:d0dfbce63a89 | 199 | | M1 bit | M0 bit | PCE bit | UART frame | |
elmot | 1:d0dfbce63a89 | 200 | |---------|---------|-----------|---------------------------------------| |
elmot | 1:d0dfbce63a89 | 201 | | 0 | 0 | 0 | | SB | 8 bit data | STB | | |
elmot | 1:d0dfbce63a89 | 202 | |---------|---------|-----------|---------------------------------------| |
elmot | 1:d0dfbce63a89 | 203 | | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | | |
elmot | 1:d0dfbce63a89 | 204 | |---------|---------|-----------|---------------------------------------| |
elmot | 1:d0dfbce63a89 | 205 | | 0 | 1 | 0 | | SB | 9 bit data | STB | | |
elmot | 1:d0dfbce63a89 | 206 | |---------|---------|-----------|---------------------------------------| |
elmot | 1:d0dfbce63a89 | 207 | | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | | |
elmot | 1:d0dfbce63a89 | 208 | |---------|---------|-----------|---------------------------------------| |
elmot | 1:d0dfbce63a89 | 209 | | 1 | 0 | 0 | | SB | 7 bit data | STB | | |
elmot | 1:d0dfbce63a89 | 210 | |---------|---------|-----------|---------------------------------------| |
elmot | 1:d0dfbce63a89 | 211 | | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | | |
elmot | 1:d0dfbce63a89 | 212 | +-----------------------------------------------------------------------+ |
elmot | 1:d0dfbce63a89 | 213 | |
elmot | 1:d0dfbce63a89 | 214 | * @{ |
elmot | 1:d0dfbce63a89 | 215 | */ |
elmot | 1:d0dfbce63a89 | 216 | |
elmot | 1:d0dfbce63a89 | 217 | /** |
elmot | 1:d0dfbce63a89 | 218 | * @brief Initialize the UART mode according to the specified |
elmot | 1:d0dfbce63a89 | 219 | * parameters in the UART_InitTypeDef and initialize the associated handle. |
elmot | 1:d0dfbce63a89 | 220 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 221 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 222 | */ |
elmot | 1:d0dfbce63a89 | 223 | HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 224 | { |
elmot | 1:d0dfbce63a89 | 225 | /* Check the UART handle allocation */ |
elmot | 1:d0dfbce63a89 | 226 | if(huart == NULL) |
elmot | 1:d0dfbce63a89 | 227 | { |
elmot | 1:d0dfbce63a89 | 228 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 229 | } |
elmot | 1:d0dfbce63a89 | 230 | |
elmot | 1:d0dfbce63a89 | 231 | if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE) |
elmot | 1:d0dfbce63a89 | 232 | { |
elmot | 1:d0dfbce63a89 | 233 | /* Check the parameters */ |
elmot | 1:d0dfbce63a89 | 234 | assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance)); |
elmot | 1:d0dfbce63a89 | 235 | } |
elmot | 1:d0dfbce63a89 | 236 | else |
elmot | 1:d0dfbce63a89 | 237 | { |
elmot | 1:d0dfbce63a89 | 238 | /* Check the parameters */ |
elmot | 1:d0dfbce63a89 | 239 | assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance))); |
elmot | 1:d0dfbce63a89 | 240 | } |
elmot | 1:d0dfbce63a89 | 241 | |
elmot | 1:d0dfbce63a89 | 242 | if(huart->gState == HAL_UART_STATE_RESET) |
elmot | 1:d0dfbce63a89 | 243 | { |
elmot | 1:d0dfbce63a89 | 244 | /* Allocate lock resource and initialize it */ |
elmot | 1:d0dfbce63a89 | 245 | huart->Lock = HAL_UNLOCKED; |
elmot | 1:d0dfbce63a89 | 246 | |
elmot | 1:d0dfbce63a89 | 247 | /* Init the low level hardware : GPIO, CLOCK */ |
elmot | 1:d0dfbce63a89 | 248 | HAL_UART_MspInit(huart); |
elmot | 1:d0dfbce63a89 | 249 | } |
elmot | 1:d0dfbce63a89 | 250 | |
elmot | 1:d0dfbce63a89 | 251 | huart->gState = HAL_UART_STATE_BUSY; |
elmot | 1:d0dfbce63a89 | 252 | |
elmot | 1:d0dfbce63a89 | 253 | /* Disable the Peripheral */ |
elmot | 1:d0dfbce63a89 | 254 | __HAL_UART_DISABLE(huart); |
elmot | 1:d0dfbce63a89 | 255 | |
elmot | 1:d0dfbce63a89 | 256 | /* Set the UART Communication parameters */ |
elmot | 1:d0dfbce63a89 | 257 | if (UART_SetConfig(huart) == HAL_ERROR) |
elmot | 1:d0dfbce63a89 | 258 | { |
elmot | 1:d0dfbce63a89 | 259 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 260 | } |
elmot | 1:d0dfbce63a89 | 261 | |
elmot | 1:d0dfbce63a89 | 262 | if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT) |
elmot | 1:d0dfbce63a89 | 263 | { |
elmot | 1:d0dfbce63a89 | 264 | UART_AdvFeatureConfig(huart); |
elmot | 1:d0dfbce63a89 | 265 | } |
elmot | 1:d0dfbce63a89 | 266 | |
elmot | 1:d0dfbce63a89 | 267 | /* In asynchronous mode, the following bits must be kept cleared: |
elmot | 1:d0dfbce63a89 | 268 | - LINEN and CLKEN bits in the USART_CR2 register, |
elmot | 1:d0dfbce63a89 | 269 | - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/ |
elmot | 1:d0dfbce63a89 | 270 | huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN); |
elmot | 1:d0dfbce63a89 | 271 | huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN); |
elmot | 1:d0dfbce63a89 | 272 | |
elmot | 1:d0dfbce63a89 | 273 | /* Enable the Peripheral */ |
elmot | 1:d0dfbce63a89 | 274 | __HAL_UART_ENABLE(huart); |
elmot | 1:d0dfbce63a89 | 275 | |
elmot | 1:d0dfbce63a89 | 276 | /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */ |
elmot | 1:d0dfbce63a89 | 277 | return (UART_CheckIdleState(huart)); |
elmot | 1:d0dfbce63a89 | 278 | } |
elmot | 1:d0dfbce63a89 | 279 | |
elmot | 1:d0dfbce63a89 | 280 | /** |
elmot | 1:d0dfbce63a89 | 281 | * @brief Initialize the half-duplex mode according to the specified |
elmot | 1:d0dfbce63a89 | 282 | * parameters in the UART_InitTypeDef and creates the associated handle. |
elmot | 1:d0dfbce63a89 | 283 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 284 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 285 | */ |
elmot | 1:d0dfbce63a89 | 286 | HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 287 | { |
elmot | 1:d0dfbce63a89 | 288 | /* Check the UART handle allocation */ |
elmot | 1:d0dfbce63a89 | 289 | if(huart == NULL) |
elmot | 1:d0dfbce63a89 | 290 | { |
elmot | 1:d0dfbce63a89 | 291 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 292 | } |
elmot | 1:d0dfbce63a89 | 293 | |
elmot | 1:d0dfbce63a89 | 294 | /* Check UART instance */ |
elmot | 1:d0dfbce63a89 | 295 | assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance)); |
elmot | 1:d0dfbce63a89 | 296 | |
elmot | 1:d0dfbce63a89 | 297 | if(huart->gState == HAL_UART_STATE_RESET) |
elmot | 1:d0dfbce63a89 | 298 | { |
elmot | 1:d0dfbce63a89 | 299 | /* Allocate lock resource and initialize it */ |
elmot | 1:d0dfbce63a89 | 300 | huart->Lock = HAL_UNLOCKED; |
elmot | 1:d0dfbce63a89 | 301 | |
elmot | 1:d0dfbce63a89 | 302 | /* Init the low level hardware : GPIO, CLOCK */ |
elmot | 1:d0dfbce63a89 | 303 | HAL_UART_MspInit(huart); |
elmot | 1:d0dfbce63a89 | 304 | } |
elmot | 1:d0dfbce63a89 | 305 | |
elmot | 1:d0dfbce63a89 | 306 | huart->gState = HAL_UART_STATE_BUSY; |
elmot | 1:d0dfbce63a89 | 307 | |
elmot | 1:d0dfbce63a89 | 308 | /* Disable the Peripheral */ |
elmot | 1:d0dfbce63a89 | 309 | __HAL_UART_DISABLE(huart); |
elmot | 1:d0dfbce63a89 | 310 | |
elmot | 1:d0dfbce63a89 | 311 | /* Set the UART Communication parameters */ |
elmot | 1:d0dfbce63a89 | 312 | if (UART_SetConfig(huart) == HAL_ERROR) |
elmot | 1:d0dfbce63a89 | 313 | { |
elmot | 1:d0dfbce63a89 | 314 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 315 | } |
elmot | 1:d0dfbce63a89 | 316 | |
elmot | 1:d0dfbce63a89 | 317 | if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT) |
elmot | 1:d0dfbce63a89 | 318 | { |
elmot | 1:d0dfbce63a89 | 319 | UART_AdvFeatureConfig(huart); |
elmot | 1:d0dfbce63a89 | 320 | } |
elmot | 1:d0dfbce63a89 | 321 | |
elmot | 1:d0dfbce63a89 | 322 | /* In half-duplex mode, the following bits must be kept cleared: |
elmot | 1:d0dfbce63a89 | 323 | - LINEN and CLKEN bits in the USART_CR2 register, |
elmot | 1:d0dfbce63a89 | 324 | - SCEN and IREN bits in the USART_CR3 register.*/ |
elmot | 1:d0dfbce63a89 | 325 | huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN); |
elmot | 1:d0dfbce63a89 | 326 | huart->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_SCEN); |
elmot | 1:d0dfbce63a89 | 327 | |
elmot | 1:d0dfbce63a89 | 328 | /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */ |
elmot | 1:d0dfbce63a89 | 329 | huart->Instance->CR3 |= USART_CR3_HDSEL; |
elmot | 1:d0dfbce63a89 | 330 | |
elmot | 1:d0dfbce63a89 | 331 | /* Enable the Peripheral */ |
elmot | 1:d0dfbce63a89 | 332 | __HAL_UART_ENABLE(huart); |
elmot | 1:d0dfbce63a89 | 333 | |
elmot | 1:d0dfbce63a89 | 334 | /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */ |
elmot | 1:d0dfbce63a89 | 335 | return (UART_CheckIdleState(huart)); |
elmot | 1:d0dfbce63a89 | 336 | } |
elmot | 1:d0dfbce63a89 | 337 | |
elmot | 1:d0dfbce63a89 | 338 | |
elmot | 1:d0dfbce63a89 | 339 | /** |
elmot | 1:d0dfbce63a89 | 340 | * @brief Initialize the LIN mode according to the specified |
elmot | 1:d0dfbce63a89 | 341 | * parameters in the UART_InitTypeDef and creates the associated handle . |
elmot | 1:d0dfbce63a89 | 342 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 343 | * @param BreakDetectLength: specifies the LIN break detection length. |
elmot | 1:d0dfbce63a89 | 344 | * This parameter can be one of the following values: |
elmot | 1:d0dfbce63a89 | 345 | * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection |
elmot | 1:d0dfbce63a89 | 346 | * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection |
elmot | 1:d0dfbce63a89 | 347 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 348 | */ |
elmot | 1:d0dfbce63a89 | 349 | HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength) |
elmot | 1:d0dfbce63a89 | 350 | { |
elmot | 1:d0dfbce63a89 | 351 | /* Check the UART handle allocation */ |
elmot | 1:d0dfbce63a89 | 352 | if(huart == NULL) |
elmot | 1:d0dfbce63a89 | 353 | { |
elmot | 1:d0dfbce63a89 | 354 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 355 | } |
elmot | 1:d0dfbce63a89 | 356 | |
elmot | 1:d0dfbce63a89 | 357 | /* Check the LIN UART instance */ |
elmot | 1:d0dfbce63a89 | 358 | assert_param(IS_UART_LIN_INSTANCE(huart->Instance)); |
elmot | 1:d0dfbce63a89 | 359 | /* Check the Break detection length parameter */ |
elmot | 1:d0dfbce63a89 | 360 | assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength)); |
elmot | 1:d0dfbce63a89 | 361 | |
elmot | 1:d0dfbce63a89 | 362 | /* LIN mode limited to 16-bit oversampling only */ |
elmot | 1:d0dfbce63a89 | 363 | if(huart->Init.OverSampling == UART_OVERSAMPLING_8) |
elmot | 1:d0dfbce63a89 | 364 | { |
elmot | 1:d0dfbce63a89 | 365 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 366 | } |
elmot | 1:d0dfbce63a89 | 367 | /* LIN mode limited to 8-bit data length */ |
elmot | 1:d0dfbce63a89 | 368 | if(huart->Init.WordLength != UART_WORDLENGTH_8B) |
elmot | 1:d0dfbce63a89 | 369 | { |
elmot | 1:d0dfbce63a89 | 370 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 371 | } |
elmot | 1:d0dfbce63a89 | 372 | |
elmot | 1:d0dfbce63a89 | 373 | if(huart->gState == HAL_UART_STATE_RESET) |
elmot | 1:d0dfbce63a89 | 374 | { |
elmot | 1:d0dfbce63a89 | 375 | /* Allocate lock resource and initialize it */ |
elmot | 1:d0dfbce63a89 | 376 | huart->Lock = HAL_UNLOCKED; |
elmot | 1:d0dfbce63a89 | 377 | |
elmot | 1:d0dfbce63a89 | 378 | /* Init the low level hardware : GPIO, CLOCK */ |
elmot | 1:d0dfbce63a89 | 379 | HAL_UART_MspInit(huart); |
elmot | 1:d0dfbce63a89 | 380 | } |
elmot | 1:d0dfbce63a89 | 381 | |
elmot | 1:d0dfbce63a89 | 382 | huart->gState = HAL_UART_STATE_BUSY; |
elmot | 1:d0dfbce63a89 | 383 | |
elmot | 1:d0dfbce63a89 | 384 | /* Disable the Peripheral */ |
elmot | 1:d0dfbce63a89 | 385 | __HAL_UART_DISABLE(huart); |
elmot | 1:d0dfbce63a89 | 386 | |
elmot | 1:d0dfbce63a89 | 387 | /* Set the UART Communication parameters */ |
elmot | 1:d0dfbce63a89 | 388 | if (UART_SetConfig(huart) == HAL_ERROR) |
elmot | 1:d0dfbce63a89 | 389 | { |
elmot | 1:d0dfbce63a89 | 390 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 391 | } |
elmot | 1:d0dfbce63a89 | 392 | |
elmot | 1:d0dfbce63a89 | 393 | if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT) |
elmot | 1:d0dfbce63a89 | 394 | { |
elmot | 1:d0dfbce63a89 | 395 | UART_AdvFeatureConfig(huart); |
elmot | 1:d0dfbce63a89 | 396 | } |
elmot | 1:d0dfbce63a89 | 397 | |
elmot | 1:d0dfbce63a89 | 398 | /* In LIN mode, the following bits must be kept cleared: |
elmot | 1:d0dfbce63a89 | 399 | - LINEN and CLKEN bits in the USART_CR2 register, |
elmot | 1:d0dfbce63a89 | 400 | - SCEN and IREN bits in the USART_CR3 register.*/ |
elmot | 1:d0dfbce63a89 | 401 | huart->Instance->CR2 &= ~(USART_CR2_CLKEN); |
elmot | 1:d0dfbce63a89 | 402 | huart->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN); |
elmot | 1:d0dfbce63a89 | 403 | |
elmot | 1:d0dfbce63a89 | 404 | /* Enable the LIN mode by setting the LINEN bit in the CR2 register */ |
elmot | 1:d0dfbce63a89 | 405 | huart->Instance->CR2 |= USART_CR2_LINEN; |
elmot | 1:d0dfbce63a89 | 406 | |
elmot | 1:d0dfbce63a89 | 407 | /* Set the USART LIN Break detection length. */ |
elmot | 1:d0dfbce63a89 | 408 | MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength); |
elmot | 1:d0dfbce63a89 | 409 | |
elmot | 1:d0dfbce63a89 | 410 | /* Enable the Peripheral */ |
elmot | 1:d0dfbce63a89 | 411 | __HAL_UART_ENABLE(huart); |
elmot | 1:d0dfbce63a89 | 412 | |
elmot | 1:d0dfbce63a89 | 413 | /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */ |
elmot | 1:d0dfbce63a89 | 414 | return (UART_CheckIdleState(huart)); |
elmot | 1:d0dfbce63a89 | 415 | } |
elmot | 1:d0dfbce63a89 | 416 | |
elmot | 1:d0dfbce63a89 | 417 | |
elmot | 1:d0dfbce63a89 | 418 | /** |
elmot | 1:d0dfbce63a89 | 419 | * @brief Initialize the multiprocessor mode according to the specified |
elmot | 1:d0dfbce63a89 | 420 | * parameters in the UART_InitTypeDef and initialize the associated handle. |
elmot | 1:d0dfbce63a89 | 421 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 422 | * @param Address: UART node address (4-, 6-, 7- or 8-bit long). |
elmot | 1:d0dfbce63a89 | 423 | * @param WakeUpMethod: specifies the UART wakeup method. |
elmot | 1:d0dfbce63a89 | 424 | * This parameter can be one of the following values: |
elmot | 1:d0dfbce63a89 | 425 | * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection |
elmot | 1:d0dfbce63a89 | 426 | * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark |
elmot | 1:d0dfbce63a89 | 427 | * @note If the user resorts to idle line detection wake up, the Address parameter |
elmot | 1:d0dfbce63a89 | 428 | * is useless and ignored by the initialization function. |
elmot | 1:d0dfbce63a89 | 429 | * @note If the user resorts to address mark wake up, the address length detection |
elmot | 1:d0dfbce63a89 | 430 | * is configured by default to 4 bits only. For the UART to be able to |
elmot | 1:d0dfbce63a89 | 431 | * manage 6-, 7- or 8-bit long addresses detection, the API |
elmot | 1:d0dfbce63a89 | 432 | * HAL_MultiProcessorEx_AddressLength_Set() must be called after |
elmot | 1:d0dfbce63a89 | 433 | * HAL_MultiProcessor_Init(). |
elmot | 1:d0dfbce63a89 | 434 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 435 | */ |
elmot | 1:d0dfbce63a89 | 436 | HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod) |
elmot | 1:d0dfbce63a89 | 437 | { |
elmot | 1:d0dfbce63a89 | 438 | /* Check the UART handle allocation */ |
elmot | 1:d0dfbce63a89 | 439 | if(huart == NULL) |
elmot | 1:d0dfbce63a89 | 440 | { |
elmot | 1:d0dfbce63a89 | 441 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 442 | } |
elmot | 1:d0dfbce63a89 | 443 | |
elmot | 1:d0dfbce63a89 | 444 | /* Check the wake up method parameter */ |
elmot | 1:d0dfbce63a89 | 445 | assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod)); |
elmot | 1:d0dfbce63a89 | 446 | |
elmot | 1:d0dfbce63a89 | 447 | if(huart->gState == HAL_UART_STATE_RESET) |
elmot | 1:d0dfbce63a89 | 448 | { |
elmot | 1:d0dfbce63a89 | 449 | /* Allocate lock resource and initialize it */ |
elmot | 1:d0dfbce63a89 | 450 | huart->Lock = HAL_UNLOCKED; |
elmot | 1:d0dfbce63a89 | 451 | |
elmot | 1:d0dfbce63a89 | 452 | /* Init the low level hardware : GPIO, CLOCK */ |
elmot | 1:d0dfbce63a89 | 453 | HAL_UART_MspInit(huart); |
elmot | 1:d0dfbce63a89 | 454 | } |
elmot | 1:d0dfbce63a89 | 455 | |
elmot | 1:d0dfbce63a89 | 456 | huart->gState = HAL_UART_STATE_BUSY; |
elmot | 1:d0dfbce63a89 | 457 | |
elmot | 1:d0dfbce63a89 | 458 | /* Disable the Peripheral */ |
elmot | 1:d0dfbce63a89 | 459 | __HAL_UART_DISABLE(huart); |
elmot | 1:d0dfbce63a89 | 460 | |
elmot | 1:d0dfbce63a89 | 461 | /* Set the UART Communication parameters */ |
elmot | 1:d0dfbce63a89 | 462 | if (UART_SetConfig(huart) == HAL_ERROR) |
elmot | 1:d0dfbce63a89 | 463 | { |
elmot | 1:d0dfbce63a89 | 464 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 465 | } |
elmot | 1:d0dfbce63a89 | 466 | |
elmot | 1:d0dfbce63a89 | 467 | if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT) |
elmot | 1:d0dfbce63a89 | 468 | { |
elmot | 1:d0dfbce63a89 | 469 | UART_AdvFeatureConfig(huart); |
elmot | 1:d0dfbce63a89 | 470 | } |
elmot | 1:d0dfbce63a89 | 471 | |
elmot | 1:d0dfbce63a89 | 472 | /* In multiprocessor mode, the following bits must be kept cleared: |
elmot | 1:d0dfbce63a89 | 473 | - LINEN and CLKEN bits in the USART_CR2 register, |
elmot | 1:d0dfbce63a89 | 474 | - SCEN, HDSEL and IREN bits in the USART_CR3 register. */ |
elmot | 1:d0dfbce63a89 | 475 | huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN); |
elmot | 1:d0dfbce63a89 | 476 | huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN); |
elmot | 1:d0dfbce63a89 | 477 | |
elmot | 1:d0dfbce63a89 | 478 | if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK) |
elmot | 1:d0dfbce63a89 | 479 | { |
elmot | 1:d0dfbce63a89 | 480 | /* If address mark wake up method is chosen, set the USART address node */ |
elmot | 1:d0dfbce63a89 | 481 | MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS)); |
elmot | 1:d0dfbce63a89 | 482 | } |
elmot | 1:d0dfbce63a89 | 483 | |
elmot | 1:d0dfbce63a89 | 484 | /* Set the wake up method by setting the WAKE bit in the CR1 register */ |
elmot | 1:d0dfbce63a89 | 485 | MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod); |
elmot | 1:d0dfbce63a89 | 486 | |
elmot | 1:d0dfbce63a89 | 487 | /* Enable the Peripheral */ |
elmot | 1:d0dfbce63a89 | 488 | __HAL_UART_ENABLE(huart); |
elmot | 1:d0dfbce63a89 | 489 | |
elmot | 1:d0dfbce63a89 | 490 | /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */ |
elmot | 1:d0dfbce63a89 | 491 | return (UART_CheckIdleState(huart)); |
elmot | 1:d0dfbce63a89 | 492 | } |
elmot | 1:d0dfbce63a89 | 493 | |
elmot | 1:d0dfbce63a89 | 494 | |
elmot | 1:d0dfbce63a89 | 495 | /** |
elmot | 1:d0dfbce63a89 | 496 | * @brief DeInitialize the UART peripheral. |
elmot | 1:d0dfbce63a89 | 497 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 498 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 499 | */ |
elmot | 1:d0dfbce63a89 | 500 | HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 501 | { |
elmot | 1:d0dfbce63a89 | 502 | /* Check the UART handle allocation */ |
elmot | 1:d0dfbce63a89 | 503 | if(huart == NULL) |
elmot | 1:d0dfbce63a89 | 504 | { |
elmot | 1:d0dfbce63a89 | 505 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 506 | } |
elmot | 1:d0dfbce63a89 | 507 | |
elmot | 1:d0dfbce63a89 | 508 | /* Check the parameters */ |
elmot | 1:d0dfbce63a89 | 509 | assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance))); |
elmot | 1:d0dfbce63a89 | 510 | |
elmot | 1:d0dfbce63a89 | 511 | huart->gState = HAL_UART_STATE_BUSY; |
elmot | 1:d0dfbce63a89 | 512 | |
elmot | 1:d0dfbce63a89 | 513 | /* Disable the Peripheral */ |
elmot | 1:d0dfbce63a89 | 514 | __HAL_UART_DISABLE(huart); |
elmot | 1:d0dfbce63a89 | 515 | |
elmot | 1:d0dfbce63a89 | 516 | huart->Instance->CR1 = 0x0; |
elmot | 1:d0dfbce63a89 | 517 | huart->Instance->CR2 = 0x0; |
elmot | 1:d0dfbce63a89 | 518 | huart->Instance->CR3 = 0x0; |
elmot | 1:d0dfbce63a89 | 519 | |
elmot | 1:d0dfbce63a89 | 520 | /* DeInit the low level hardware */ |
elmot | 1:d0dfbce63a89 | 521 | HAL_UART_MspDeInit(huart); |
elmot | 1:d0dfbce63a89 | 522 | |
elmot | 1:d0dfbce63a89 | 523 | huart->ErrorCode = HAL_UART_ERROR_NONE; |
elmot | 1:d0dfbce63a89 | 524 | huart->gState = HAL_UART_STATE_RESET; |
elmot | 1:d0dfbce63a89 | 525 | huart->RxState = HAL_UART_STATE_RESET; |
elmot | 1:d0dfbce63a89 | 526 | |
elmot | 1:d0dfbce63a89 | 527 | /* Process Unlock */ |
elmot | 1:d0dfbce63a89 | 528 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 529 | |
elmot | 1:d0dfbce63a89 | 530 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 531 | } |
elmot | 1:d0dfbce63a89 | 532 | |
elmot | 1:d0dfbce63a89 | 533 | /** |
elmot | 1:d0dfbce63a89 | 534 | * @brief Initialize the UART MSP. |
elmot | 1:d0dfbce63a89 | 535 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 536 | * @retval None |
elmot | 1:d0dfbce63a89 | 537 | */ |
elmot | 1:d0dfbce63a89 | 538 | __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 539 | { |
elmot | 1:d0dfbce63a89 | 540 | /* Prevent unused argument(s) compilation warning */ |
elmot | 1:d0dfbce63a89 | 541 | UNUSED(huart); |
elmot | 1:d0dfbce63a89 | 542 | |
elmot | 1:d0dfbce63a89 | 543 | /* NOTE : This function should not be modified, when the callback is needed, |
elmot | 1:d0dfbce63a89 | 544 | the HAL_UART_MspInit can be implemented in the user file |
elmot | 1:d0dfbce63a89 | 545 | */ |
elmot | 1:d0dfbce63a89 | 546 | } |
elmot | 1:d0dfbce63a89 | 547 | |
elmot | 1:d0dfbce63a89 | 548 | /** |
elmot | 1:d0dfbce63a89 | 549 | * @brief DeInitialize the UART MSP. |
elmot | 1:d0dfbce63a89 | 550 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 551 | * @retval None |
elmot | 1:d0dfbce63a89 | 552 | */ |
elmot | 1:d0dfbce63a89 | 553 | __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 554 | { |
elmot | 1:d0dfbce63a89 | 555 | /* Prevent unused argument(s) compilation warning */ |
elmot | 1:d0dfbce63a89 | 556 | UNUSED(huart); |
elmot | 1:d0dfbce63a89 | 557 | |
elmot | 1:d0dfbce63a89 | 558 | /* NOTE : This function should not be modified, when the callback is needed, |
elmot | 1:d0dfbce63a89 | 559 | the HAL_UART_MspDeInit can be implemented in the user file |
elmot | 1:d0dfbce63a89 | 560 | */ |
elmot | 1:d0dfbce63a89 | 561 | } |
elmot | 1:d0dfbce63a89 | 562 | |
elmot | 1:d0dfbce63a89 | 563 | /** |
elmot | 1:d0dfbce63a89 | 564 | * @} |
elmot | 1:d0dfbce63a89 | 565 | */ |
elmot | 1:d0dfbce63a89 | 566 | |
elmot | 1:d0dfbce63a89 | 567 | /** @defgroup UART_Exported_Functions_Group2 IO operation functions |
elmot | 1:d0dfbce63a89 | 568 | * @brief UART Transmit/Receive functions |
elmot | 1:d0dfbce63a89 | 569 | * |
elmot | 1:d0dfbce63a89 | 570 | @verbatim |
elmot | 1:d0dfbce63a89 | 571 | =============================================================================== |
elmot | 1:d0dfbce63a89 | 572 | ##### IO operation functions ##### |
elmot | 1:d0dfbce63a89 | 573 | =============================================================================== |
elmot | 1:d0dfbce63a89 | 574 | This subsection provides a set of functions allowing to manage the UART asynchronous |
elmot | 1:d0dfbce63a89 | 575 | and Half duplex data transfers. |
elmot | 1:d0dfbce63a89 | 576 | |
elmot | 1:d0dfbce63a89 | 577 | (#) There are two mode of transfer: |
elmot | 1:d0dfbce63a89 | 578 | (+) Blocking mode: The communication is performed in polling mode. |
elmot | 1:d0dfbce63a89 | 579 | The HAL status of all data processing is returned by the same function |
elmot | 1:d0dfbce63a89 | 580 | after finishing transfer. |
elmot | 1:d0dfbce63a89 | 581 | (+) No-Blocking mode: The communication is performed using Interrupts |
elmot | 1:d0dfbce63a89 | 582 | or DMA, These API's return the HAL status. |
elmot | 1:d0dfbce63a89 | 583 | The end of the data processing will be indicated through the |
elmot | 1:d0dfbce63a89 | 584 | dedicated UART IRQ when using Interrupt mode or the DMA IRQ when |
elmot | 1:d0dfbce63a89 | 585 | using DMA mode. |
elmot | 1:d0dfbce63a89 | 586 | The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks |
elmot | 1:d0dfbce63a89 | 587 | will be executed respectively at the end of the transmit or Receive process |
elmot | 1:d0dfbce63a89 | 588 | The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected |
elmot | 1:d0dfbce63a89 | 589 | |
elmot | 1:d0dfbce63a89 | 590 | (#) Blocking mode API's are : |
elmot | 1:d0dfbce63a89 | 591 | (+) HAL_UART_Transmit() |
elmot | 1:d0dfbce63a89 | 592 | (+) HAL_UART_Receive() |
elmot | 1:d0dfbce63a89 | 593 | |
elmot | 1:d0dfbce63a89 | 594 | (#) Non-Blocking mode API's with Interrupt are : |
elmot | 1:d0dfbce63a89 | 595 | (+) HAL_UART_Transmit_IT() |
elmot | 1:d0dfbce63a89 | 596 | (+) HAL_UART_Receive_IT() |
elmot | 1:d0dfbce63a89 | 597 | (+) HAL_UART_IRQHandler() |
elmot | 1:d0dfbce63a89 | 598 | |
elmot | 1:d0dfbce63a89 | 599 | (#) No-Blocking mode API's with DMA are : |
elmot | 1:d0dfbce63a89 | 600 | (+) HAL_UART_Transmit_DMA() |
elmot | 1:d0dfbce63a89 | 601 | (+) HAL_UART_Receive_DMA() |
elmot | 1:d0dfbce63a89 | 602 | (+) HAL_UART_DMAPause() |
elmot | 1:d0dfbce63a89 | 603 | (+) HAL_UART_DMAResume() |
elmot | 1:d0dfbce63a89 | 604 | (+) HAL_UART_DMAStop() |
elmot | 1:d0dfbce63a89 | 605 | |
elmot | 1:d0dfbce63a89 | 606 | (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode: |
elmot | 1:d0dfbce63a89 | 607 | (+) HAL_UART_TxHalfCpltCallback() |
elmot | 1:d0dfbce63a89 | 608 | (+) HAL_UART_TxCpltCallback() |
elmot | 1:d0dfbce63a89 | 609 | (+) HAL_UART_RxHalfCpltCallback() |
elmot | 1:d0dfbce63a89 | 610 | (+) HAL_UART_RxCpltCallback() |
elmot | 1:d0dfbce63a89 | 611 | (+) HAL_UART_ErrorCallback() |
elmot | 1:d0dfbce63a89 | 612 | |
elmot | 1:d0dfbce63a89 | 613 | (#) In Non-Blocking mode transfers, possible errors are split into 2 categories. |
elmot | 1:d0dfbce63a89 | 614 | Errors are handled as follows : |
elmot | 1:d0dfbce63a89 | 615 | (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is |
elmot | 1:d0dfbce63a89 | 616 | to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception . |
elmot | 1:d0dfbce63a89 | 617 | Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type, |
elmot | 1:d0dfbce63a89 | 618 | and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side. |
elmot | 1:d0dfbce63a89 | 619 | (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted. |
elmot | 1:d0dfbce63a89 | 620 | This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode. |
elmot | 1:d0dfbce63a89 | 621 | Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed. |
elmot | 1:d0dfbce63a89 | 622 | |
elmot | 1:d0dfbce63a89 | 623 | -@- In the Half duplex communication, it is forbidden to run the transmit |
elmot | 1:d0dfbce63a89 | 624 | and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful. |
elmot | 1:d0dfbce63a89 | 625 | |
elmot | 1:d0dfbce63a89 | 626 | @endverbatim |
elmot | 1:d0dfbce63a89 | 627 | * @{ |
elmot | 1:d0dfbce63a89 | 628 | */ |
elmot | 1:d0dfbce63a89 | 629 | |
elmot | 1:d0dfbce63a89 | 630 | /** |
elmot | 1:d0dfbce63a89 | 631 | * @brief Send an amount of data in blocking mode. |
elmot | 1:d0dfbce63a89 | 632 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 633 | * @param pData: Pointer to data buffer. |
elmot | 1:d0dfbce63a89 | 634 | * @param Size: Amount of data to be sent. |
elmot | 1:d0dfbce63a89 | 635 | * @param Timeout: Timeout duration. |
elmot | 1:d0dfbce63a89 | 636 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 637 | */ |
elmot | 1:d0dfbce63a89 | 638 | HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
elmot | 1:d0dfbce63a89 | 639 | { |
elmot | 1:d0dfbce63a89 | 640 | uint16_t* tmp; |
elmot | 1:d0dfbce63a89 | 641 | uint32_t tickstart = 0; |
elmot | 1:d0dfbce63a89 | 642 | |
elmot | 1:d0dfbce63a89 | 643 | /* Check that a Tx process is not already ongoing */ |
elmot | 1:d0dfbce63a89 | 644 | if(huart->gState == HAL_UART_STATE_READY) |
elmot | 1:d0dfbce63a89 | 645 | { |
elmot | 1:d0dfbce63a89 | 646 | if((pData == NULL ) || (Size == 0)) |
elmot | 1:d0dfbce63a89 | 647 | { |
elmot | 1:d0dfbce63a89 | 648 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 649 | } |
elmot | 1:d0dfbce63a89 | 650 | |
elmot | 1:d0dfbce63a89 | 651 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 652 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 653 | |
elmot | 1:d0dfbce63a89 | 654 | huart->ErrorCode = HAL_UART_ERROR_NONE; |
elmot | 1:d0dfbce63a89 | 655 | huart->gState = HAL_UART_STATE_BUSY_TX; |
elmot | 1:d0dfbce63a89 | 656 | |
elmot | 1:d0dfbce63a89 | 657 | /* Init tickstart for timeout managment*/ |
elmot | 1:d0dfbce63a89 | 658 | tickstart = HAL_GetTick(); |
elmot | 1:d0dfbce63a89 | 659 | |
elmot | 1:d0dfbce63a89 | 660 | huart->TxXferSize = Size; |
elmot | 1:d0dfbce63a89 | 661 | huart->TxXferCount = Size; |
elmot | 1:d0dfbce63a89 | 662 | while(huart->TxXferCount > 0) |
elmot | 1:d0dfbce63a89 | 663 | { |
elmot | 1:d0dfbce63a89 | 664 | huart->TxXferCount--; |
elmot | 1:d0dfbce63a89 | 665 | if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
elmot | 1:d0dfbce63a89 | 666 | { |
elmot | 1:d0dfbce63a89 | 667 | return HAL_TIMEOUT; |
elmot | 1:d0dfbce63a89 | 668 | } |
elmot | 1:d0dfbce63a89 | 669 | if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) |
elmot | 1:d0dfbce63a89 | 670 | { |
elmot | 1:d0dfbce63a89 | 671 | tmp = (uint16_t*) pData; |
elmot | 1:d0dfbce63a89 | 672 | huart->Instance->TDR = (*tmp & (uint16_t)0x01FF); |
elmot | 1:d0dfbce63a89 | 673 | pData += 2; |
elmot | 1:d0dfbce63a89 | 674 | } |
elmot | 1:d0dfbce63a89 | 675 | else |
elmot | 1:d0dfbce63a89 | 676 | { |
elmot | 1:d0dfbce63a89 | 677 | huart->Instance->TDR = (*pData++ & (uint8_t)0xFF); |
elmot | 1:d0dfbce63a89 | 678 | } |
elmot | 1:d0dfbce63a89 | 679 | } |
elmot | 1:d0dfbce63a89 | 680 | if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK) |
elmot | 1:d0dfbce63a89 | 681 | { |
elmot | 1:d0dfbce63a89 | 682 | return HAL_TIMEOUT; |
elmot | 1:d0dfbce63a89 | 683 | } |
elmot | 1:d0dfbce63a89 | 684 | |
elmot | 1:d0dfbce63a89 | 685 | /* At end of Tx process, restore huart->gState to Ready */ |
elmot | 1:d0dfbce63a89 | 686 | huart->gState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 687 | |
elmot | 1:d0dfbce63a89 | 688 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 689 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 690 | |
elmot | 1:d0dfbce63a89 | 691 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 692 | } |
elmot | 1:d0dfbce63a89 | 693 | else |
elmot | 1:d0dfbce63a89 | 694 | { |
elmot | 1:d0dfbce63a89 | 695 | return HAL_BUSY; |
elmot | 1:d0dfbce63a89 | 696 | } |
elmot | 1:d0dfbce63a89 | 697 | } |
elmot | 1:d0dfbce63a89 | 698 | |
elmot | 1:d0dfbce63a89 | 699 | /** |
elmot | 1:d0dfbce63a89 | 700 | * @brief Receive an amount of data in blocking mode. |
elmot | 1:d0dfbce63a89 | 701 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 702 | * @param pData: pointer to data buffer. |
elmot | 1:d0dfbce63a89 | 703 | * @param Size: amount of data to be received. |
elmot | 1:d0dfbce63a89 | 704 | * @param Timeout: Timeout duration. |
elmot | 1:d0dfbce63a89 | 705 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 706 | */ |
elmot | 1:d0dfbce63a89 | 707 | HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
elmot | 1:d0dfbce63a89 | 708 | { |
elmot | 1:d0dfbce63a89 | 709 | uint16_t* tmp; |
elmot | 1:d0dfbce63a89 | 710 | uint16_t uhMask; |
elmot | 1:d0dfbce63a89 | 711 | uint32_t tickstart = 0; |
elmot | 1:d0dfbce63a89 | 712 | |
elmot | 1:d0dfbce63a89 | 713 | /* Check that a Rx process is not already ongoing */ |
elmot | 1:d0dfbce63a89 | 714 | if(huart->RxState == HAL_UART_STATE_READY) |
elmot | 1:d0dfbce63a89 | 715 | { |
elmot | 1:d0dfbce63a89 | 716 | if((pData == NULL ) || (Size == 0)) |
elmot | 1:d0dfbce63a89 | 717 | { |
elmot | 1:d0dfbce63a89 | 718 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 719 | } |
elmot | 1:d0dfbce63a89 | 720 | |
elmot | 1:d0dfbce63a89 | 721 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 722 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 723 | |
elmot | 1:d0dfbce63a89 | 724 | huart->ErrorCode = HAL_UART_ERROR_NONE; |
elmot | 1:d0dfbce63a89 | 725 | huart->RxState = HAL_UART_STATE_BUSY_RX; |
elmot | 1:d0dfbce63a89 | 726 | |
elmot | 1:d0dfbce63a89 | 727 | /* Init tickstart for timeout managment*/ |
elmot | 1:d0dfbce63a89 | 728 | tickstart = HAL_GetTick(); |
elmot | 1:d0dfbce63a89 | 729 | |
elmot | 1:d0dfbce63a89 | 730 | huart->RxXferSize = Size; |
elmot | 1:d0dfbce63a89 | 731 | huart->RxXferCount = Size; |
elmot | 1:d0dfbce63a89 | 732 | |
elmot | 1:d0dfbce63a89 | 733 | /* Computation of UART mask to apply to RDR register */ |
elmot | 1:d0dfbce63a89 | 734 | UART_MASK_COMPUTATION(huart); |
elmot | 1:d0dfbce63a89 | 735 | uhMask = huart->Mask; |
elmot | 1:d0dfbce63a89 | 736 | |
elmot | 1:d0dfbce63a89 | 737 | /* as long as data have to be received */ |
elmot | 1:d0dfbce63a89 | 738 | while(huart->RxXferCount > 0) |
elmot | 1:d0dfbce63a89 | 739 | { |
elmot | 1:d0dfbce63a89 | 740 | huart->RxXferCount--; |
elmot | 1:d0dfbce63a89 | 741 | if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) |
elmot | 1:d0dfbce63a89 | 742 | { |
elmot | 1:d0dfbce63a89 | 743 | return HAL_TIMEOUT; |
elmot | 1:d0dfbce63a89 | 744 | } |
elmot | 1:d0dfbce63a89 | 745 | if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) |
elmot | 1:d0dfbce63a89 | 746 | { |
elmot | 1:d0dfbce63a89 | 747 | tmp = (uint16_t*) pData ; |
elmot | 1:d0dfbce63a89 | 748 | *tmp = (uint16_t)(huart->Instance->RDR & uhMask); |
elmot | 1:d0dfbce63a89 | 749 | pData +=2; |
elmot | 1:d0dfbce63a89 | 750 | } |
elmot | 1:d0dfbce63a89 | 751 | else |
elmot | 1:d0dfbce63a89 | 752 | { |
elmot | 1:d0dfbce63a89 | 753 | *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask); |
elmot | 1:d0dfbce63a89 | 754 | } |
elmot | 1:d0dfbce63a89 | 755 | } |
elmot | 1:d0dfbce63a89 | 756 | |
elmot | 1:d0dfbce63a89 | 757 | /* At end of Rx process, restore huart->RxState to Ready */ |
elmot | 1:d0dfbce63a89 | 758 | huart->RxState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 759 | |
elmot | 1:d0dfbce63a89 | 760 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 761 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 762 | |
elmot | 1:d0dfbce63a89 | 763 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 764 | } |
elmot | 1:d0dfbce63a89 | 765 | else |
elmot | 1:d0dfbce63a89 | 766 | { |
elmot | 1:d0dfbce63a89 | 767 | return HAL_BUSY; |
elmot | 1:d0dfbce63a89 | 768 | } |
elmot | 1:d0dfbce63a89 | 769 | } |
elmot | 1:d0dfbce63a89 | 770 | |
elmot | 1:d0dfbce63a89 | 771 | /** |
elmot | 1:d0dfbce63a89 | 772 | * @brief Send an amount of data in interrupt mode. |
elmot | 1:d0dfbce63a89 | 773 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 774 | * @param pData: pointer to data buffer. |
elmot | 1:d0dfbce63a89 | 775 | * @param Size: amount of data to be sent. |
elmot | 1:d0dfbce63a89 | 776 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 777 | */ |
elmot | 1:d0dfbce63a89 | 778 | HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) |
elmot | 1:d0dfbce63a89 | 779 | { |
elmot | 1:d0dfbce63a89 | 780 | /* Check that a Tx process is not already ongoing */ |
elmot | 1:d0dfbce63a89 | 781 | if(huart->gState == HAL_UART_STATE_READY) |
elmot | 1:d0dfbce63a89 | 782 | { |
elmot | 1:d0dfbce63a89 | 783 | if((pData == NULL ) || (Size == 0)) |
elmot | 1:d0dfbce63a89 | 784 | { |
elmot | 1:d0dfbce63a89 | 785 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 786 | } |
elmot | 1:d0dfbce63a89 | 787 | |
elmot | 1:d0dfbce63a89 | 788 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 789 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 790 | |
elmot | 1:d0dfbce63a89 | 791 | huart->pTxBuffPtr = pData; |
elmot | 1:d0dfbce63a89 | 792 | huart->TxXferSize = Size; |
elmot | 1:d0dfbce63a89 | 793 | huart->TxXferCount = Size; |
elmot | 1:d0dfbce63a89 | 794 | |
elmot | 1:d0dfbce63a89 | 795 | huart->ErrorCode = HAL_UART_ERROR_NONE; |
elmot | 1:d0dfbce63a89 | 796 | huart->gState = HAL_UART_STATE_BUSY_TX; |
elmot | 1:d0dfbce63a89 | 797 | |
elmot | 1:d0dfbce63a89 | 798 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 799 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 800 | |
elmot | 1:d0dfbce63a89 | 801 | /* Enable the UART Transmit Data Register Empty Interrupt */ |
elmot | 1:d0dfbce63a89 | 802 | SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE); |
elmot | 1:d0dfbce63a89 | 803 | |
elmot | 1:d0dfbce63a89 | 804 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 805 | } |
elmot | 1:d0dfbce63a89 | 806 | else |
elmot | 1:d0dfbce63a89 | 807 | { |
elmot | 1:d0dfbce63a89 | 808 | return HAL_BUSY; |
elmot | 1:d0dfbce63a89 | 809 | } |
elmot | 1:d0dfbce63a89 | 810 | } |
elmot | 1:d0dfbce63a89 | 811 | |
elmot | 1:d0dfbce63a89 | 812 | /** |
elmot | 1:d0dfbce63a89 | 813 | * @brief Receive an amount of data in interrupt mode. |
elmot | 1:d0dfbce63a89 | 814 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 815 | * @param pData: pointer to data buffer. |
elmot | 1:d0dfbce63a89 | 816 | * @param Size: amount of data to be received. |
elmot | 1:d0dfbce63a89 | 817 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 818 | */ |
elmot | 1:d0dfbce63a89 | 819 | HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) |
elmot | 1:d0dfbce63a89 | 820 | { |
elmot | 1:d0dfbce63a89 | 821 | /* Check that a Rx process is not already ongoing */ |
elmot | 1:d0dfbce63a89 | 822 | if(huart->RxState == HAL_UART_STATE_READY) |
elmot | 1:d0dfbce63a89 | 823 | { |
elmot | 1:d0dfbce63a89 | 824 | if((pData == NULL ) || (Size == 0)) |
elmot | 1:d0dfbce63a89 | 825 | { |
elmot | 1:d0dfbce63a89 | 826 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 827 | } |
elmot | 1:d0dfbce63a89 | 828 | |
elmot | 1:d0dfbce63a89 | 829 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 830 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 831 | |
elmot | 1:d0dfbce63a89 | 832 | huart->pRxBuffPtr = pData; |
elmot | 1:d0dfbce63a89 | 833 | huart->RxXferSize = Size; |
elmot | 1:d0dfbce63a89 | 834 | huart->RxXferCount = Size; |
elmot | 1:d0dfbce63a89 | 835 | |
elmot | 1:d0dfbce63a89 | 836 | /* Computation of UART mask to apply to RDR register */ |
elmot | 1:d0dfbce63a89 | 837 | UART_MASK_COMPUTATION(huart); |
elmot | 1:d0dfbce63a89 | 838 | |
elmot | 1:d0dfbce63a89 | 839 | huart->ErrorCode = HAL_UART_ERROR_NONE; |
elmot | 1:d0dfbce63a89 | 840 | huart->RxState = HAL_UART_STATE_BUSY_RX; |
elmot | 1:d0dfbce63a89 | 841 | |
elmot | 1:d0dfbce63a89 | 842 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 843 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 844 | |
elmot | 1:d0dfbce63a89 | 845 | /* Enable the UART Parity Error Interrupt */ |
elmot | 1:d0dfbce63a89 | 846 | SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); |
elmot | 1:d0dfbce63a89 | 847 | |
elmot | 1:d0dfbce63a89 | 848 | /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ |
elmot | 1:d0dfbce63a89 | 849 | SET_BIT(huart->Instance->CR3, USART_CR3_EIE); |
elmot | 1:d0dfbce63a89 | 850 | |
elmot | 1:d0dfbce63a89 | 851 | /* Enable the UART Data Register not empty Interrupt */ |
elmot | 1:d0dfbce63a89 | 852 | SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE); |
elmot | 1:d0dfbce63a89 | 853 | |
elmot | 1:d0dfbce63a89 | 854 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 855 | } |
elmot | 1:d0dfbce63a89 | 856 | else |
elmot | 1:d0dfbce63a89 | 857 | { |
elmot | 1:d0dfbce63a89 | 858 | return HAL_BUSY; |
elmot | 1:d0dfbce63a89 | 859 | } |
elmot | 1:d0dfbce63a89 | 860 | } |
elmot | 1:d0dfbce63a89 | 861 | |
elmot | 1:d0dfbce63a89 | 862 | /** |
elmot | 1:d0dfbce63a89 | 863 | * @brief Send an amount of data in DMA mode. |
elmot | 1:d0dfbce63a89 | 864 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 865 | * @param pData: pointer to data buffer. |
elmot | 1:d0dfbce63a89 | 866 | * @param Size: amount of data to be sent. |
elmot | 1:d0dfbce63a89 | 867 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 868 | */ |
elmot | 1:d0dfbce63a89 | 869 | HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) |
elmot | 1:d0dfbce63a89 | 870 | { |
elmot | 1:d0dfbce63a89 | 871 | uint32_t *tmp; |
elmot | 1:d0dfbce63a89 | 872 | |
elmot | 1:d0dfbce63a89 | 873 | /* Check that a Tx process is not already ongoing */ |
elmot | 1:d0dfbce63a89 | 874 | if(huart->gState == HAL_UART_STATE_READY) |
elmot | 1:d0dfbce63a89 | 875 | { |
elmot | 1:d0dfbce63a89 | 876 | if((pData == NULL ) || (Size == 0)) |
elmot | 1:d0dfbce63a89 | 877 | { |
elmot | 1:d0dfbce63a89 | 878 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 879 | } |
elmot | 1:d0dfbce63a89 | 880 | |
elmot | 1:d0dfbce63a89 | 881 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 882 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 883 | |
elmot | 1:d0dfbce63a89 | 884 | huart->pTxBuffPtr = pData; |
elmot | 1:d0dfbce63a89 | 885 | huart->TxXferSize = Size; |
elmot | 1:d0dfbce63a89 | 886 | huart->TxXferCount = Size; |
elmot | 1:d0dfbce63a89 | 887 | |
elmot | 1:d0dfbce63a89 | 888 | huart->ErrorCode = HAL_UART_ERROR_NONE; |
elmot | 1:d0dfbce63a89 | 889 | huart->gState = HAL_UART_STATE_BUSY_TX; |
elmot | 1:d0dfbce63a89 | 890 | |
elmot | 1:d0dfbce63a89 | 891 | /* Set the UART DMA transfer complete callback */ |
elmot | 1:d0dfbce63a89 | 892 | huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt; |
elmot | 1:d0dfbce63a89 | 893 | |
elmot | 1:d0dfbce63a89 | 894 | /* Set the UART DMA Half transfer complete callback */ |
elmot | 1:d0dfbce63a89 | 895 | huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt; |
elmot | 1:d0dfbce63a89 | 896 | |
elmot | 1:d0dfbce63a89 | 897 | /* Set the DMA error callback */ |
elmot | 1:d0dfbce63a89 | 898 | huart->hdmatx->XferErrorCallback = UART_DMAError; |
elmot | 1:d0dfbce63a89 | 899 | |
elmot | 1:d0dfbce63a89 | 900 | /* Set the DMA abort callback */ |
elmot | 1:d0dfbce63a89 | 901 | huart->hdmatx->XferAbortCallback = NULL; |
elmot | 1:d0dfbce63a89 | 902 | |
elmot | 1:d0dfbce63a89 | 903 | /* Enable the UART transmit DMA channel */ |
elmot | 1:d0dfbce63a89 | 904 | tmp = (uint32_t*)&pData; |
elmot | 1:d0dfbce63a89 | 905 | HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->TDR, Size); |
elmot | 1:d0dfbce63a89 | 906 | |
elmot | 1:d0dfbce63a89 | 907 | /* Clear the TC flag in the ICR register */ |
elmot | 1:d0dfbce63a89 | 908 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); |
elmot | 1:d0dfbce63a89 | 909 | |
elmot | 1:d0dfbce63a89 | 910 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 911 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 912 | |
elmot | 1:d0dfbce63a89 | 913 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
elmot | 1:d0dfbce63a89 | 914 | in the UART CR3 register */ |
elmot | 1:d0dfbce63a89 | 915 | SET_BIT(huart->Instance->CR3, USART_CR3_DMAT); |
elmot | 1:d0dfbce63a89 | 916 | |
elmot | 1:d0dfbce63a89 | 917 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 918 | } |
elmot | 1:d0dfbce63a89 | 919 | else |
elmot | 1:d0dfbce63a89 | 920 | { |
elmot | 1:d0dfbce63a89 | 921 | return HAL_BUSY; |
elmot | 1:d0dfbce63a89 | 922 | } |
elmot | 1:d0dfbce63a89 | 923 | } |
elmot | 1:d0dfbce63a89 | 924 | |
elmot | 1:d0dfbce63a89 | 925 | /** |
elmot | 1:d0dfbce63a89 | 926 | * @brief Receive an amount of data in DMA mode. |
elmot | 1:d0dfbce63a89 | 927 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 928 | * @param pData: pointer to data buffer. |
elmot | 1:d0dfbce63a89 | 929 | * @param Size: amount of data to be received. |
elmot | 1:d0dfbce63a89 | 930 | * @note When the UART parity is enabled (PCE = 1), the received data contain |
elmot | 1:d0dfbce63a89 | 931 | * the parity bit (MSB position). |
elmot | 1:d0dfbce63a89 | 932 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 933 | */ |
elmot | 1:d0dfbce63a89 | 934 | HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) |
elmot | 1:d0dfbce63a89 | 935 | { |
elmot | 1:d0dfbce63a89 | 936 | uint32_t *tmp; |
elmot | 1:d0dfbce63a89 | 937 | |
elmot | 1:d0dfbce63a89 | 938 | /* Check that a Rx process is not already ongoing */ |
elmot | 1:d0dfbce63a89 | 939 | if(huart->RxState == HAL_UART_STATE_READY) |
elmot | 1:d0dfbce63a89 | 940 | { |
elmot | 1:d0dfbce63a89 | 941 | if((pData == NULL ) || (Size == 0)) |
elmot | 1:d0dfbce63a89 | 942 | { |
elmot | 1:d0dfbce63a89 | 943 | return HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 944 | } |
elmot | 1:d0dfbce63a89 | 945 | |
elmot | 1:d0dfbce63a89 | 946 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 947 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 948 | |
elmot | 1:d0dfbce63a89 | 949 | huart->pRxBuffPtr = pData; |
elmot | 1:d0dfbce63a89 | 950 | huart->RxXferSize = Size; |
elmot | 1:d0dfbce63a89 | 951 | |
elmot | 1:d0dfbce63a89 | 952 | huart->ErrorCode = HAL_UART_ERROR_NONE; |
elmot | 1:d0dfbce63a89 | 953 | huart->RxState = HAL_UART_STATE_BUSY_RX; |
elmot | 1:d0dfbce63a89 | 954 | |
elmot | 1:d0dfbce63a89 | 955 | /* Set the UART DMA transfer complete callback */ |
elmot | 1:d0dfbce63a89 | 956 | huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt; |
elmot | 1:d0dfbce63a89 | 957 | |
elmot | 1:d0dfbce63a89 | 958 | /* Set the UART DMA Half transfer complete callback */ |
elmot | 1:d0dfbce63a89 | 959 | huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt; |
elmot | 1:d0dfbce63a89 | 960 | |
elmot | 1:d0dfbce63a89 | 961 | /* Set the DMA error callback */ |
elmot | 1:d0dfbce63a89 | 962 | huart->hdmarx->XferErrorCallback = UART_DMAError; |
elmot | 1:d0dfbce63a89 | 963 | |
elmot | 1:d0dfbce63a89 | 964 | /* Set the DMA abort callback */ |
elmot | 1:d0dfbce63a89 | 965 | huart->hdmarx->XferAbortCallback = NULL; |
elmot | 1:d0dfbce63a89 | 966 | |
elmot | 1:d0dfbce63a89 | 967 | /* Enable the DMA channel */ |
elmot | 1:d0dfbce63a89 | 968 | tmp = (uint32_t*)&pData; |
elmot | 1:d0dfbce63a89 | 969 | HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, *(uint32_t*)tmp, Size); |
elmot | 1:d0dfbce63a89 | 970 | |
elmot | 1:d0dfbce63a89 | 971 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 972 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 973 | |
elmot | 1:d0dfbce63a89 | 974 | /* Enable the UART Parity Error Interrupt */ |
elmot | 1:d0dfbce63a89 | 975 | SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); |
elmot | 1:d0dfbce63a89 | 976 | |
elmot | 1:d0dfbce63a89 | 977 | /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ |
elmot | 1:d0dfbce63a89 | 978 | SET_BIT(huart->Instance->CR3, USART_CR3_EIE); |
elmot | 1:d0dfbce63a89 | 979 | |
elmot | 1:d0dfbce63a89 | 980 | /* Enable the DMA transfer for the receiver request by setting the DMAR bit |
elmot | 1:d0dfbce63a89 | 981 | in the UART CR3 register */ |
elmot | 1:d0dfbce63a89 | 982 | SET_BIT(huart->Instance->CR3, USART_CR3_DMAR); |
elmot | 1:d0dfbce63a89 | 983 | |
elmot | 1:d0dfbce63a89 | 984 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 985 | } |
elmot | 1:d0dfbce63a89 | 986 | else |
elmot | 1:d0dfbce63a89 | 987 | { |
elmot | 1:d0dfbce63a89 | 988 | return HAL_BUSY; |
elmot | 1:d0dfbce63a89 | 989 | } |
elmot | 1:d0dfbce63a89 | 990 | } |
elmot | 1:d0dfbce63a89 | 991 | |
elmot | 1:d0dfbce63a89 | 992 | /** |
elmot | 1:d0dfbce63a89 | 993 | * @brief Pause the DMA Transfer. |
elmot | 1:d0dfbce63a89 | 994 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 995 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 996 | */ |
elmot | 1:d0dfbce63a89 | 997 | HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 998 | { |
elmot | 1:d0dfbce63a89 | 999 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 1000 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 1001 | |
elmot | 1:d0dfbce63a89 | 1002 | if ((huart->gState == HAL_UART_STATE_BUSY_TX) && |
elmot | 1:d0dfbce63a89 | 1003 | (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))) |
elmot | 1:d0dfbce63a89 | 1004 | { |
elmot | 1:d0dfbce63a89 | 1005 | /* Disable the UART DMA Tx request */ |
elmot | 1:d0dfbce63a89 | 1006 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); |
elmot | 1:d0dfbce63a89 | 1007 | } |
elmot | 1:d0dfbce63a89 | 1008 | if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && |
elmot | 1:d0dfbce63a89 | 1009 | (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))) |
elmot | 1:d0dfbce63a89 | 1010 | { |
elmot | 1:d0dfbce63a89 | 1011 | /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */ |
elmot | 1:d0dfbce63a89 | 1012 | CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); |
elmot | 1:d0dfbce63a89 | 1013 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); |
elmot | 1:d0dfbce63a89 | 1014 | |
elmot | 1:d0dfbce63a89 | 1015 | /* Disable the UART DMA Rx request */ |
elmot | 1:d0dfbce63a89 | 1016 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); |
elmot | 1:d0dfbce63a89 | 1017 | } |
elmot | 1:d0dfbce63a89 | 1018 | |
elmot | 1:d0dfbce63a89 | 1019 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 1020 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 1021 | |
elmot | 1:d0dfbce63a89 | 1022 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 1023 | } |
elmot | 1:d0dfbce63a89 | 1024 | |
elmot | 1:d0dfbce63a89 | 1025 | /** |
elmot | 1:d0dfbce63a89 | 1026 | * @brief Resume the DMA Transfer. |
elmot | 1:d0dfbce63a89 | 1027 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1028 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 1029 | */ |
elmot | 1:d0dfbce63a89 | 1030 | HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1031 | { |
elmot | 1:d0dfbce63a89 | 1032 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 1033 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 1034 | |
elmot | 1:d0dfbce63a89 | 1035 | if(huart->gState == HAL_UART_STATE_BUSY_TX) |
elmot | 1:d0dfbce63a89 | 1036 | { |
elmot | 1:d0dfbce63a89 | 1037 | /* Enable the UART DMA Tx request */ |
elmot | 1:d0dfbce63a89 | 1038 | SET_BIT(huart->Instance->CR3, USART_CR3_DMAT); |
elmot | 1:d0dfbce63a89 | 1039 | } |
elmot | 1:d0dfbce63a89 | 1040 | if(huart->RxState == HAL_UART_STATE_BUSY_RX) |
elmot | 1:d0dfbce63a89 | 1041 | { |
elmot | 1:d0dfbce63a89 | 1042 | /* Clear the Overrun flag before resuming the Rx transfer */ |
elmot | 1:d0dfbce63a89 | 1043 | __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); |
elmot | 1:d0dfbce63a89 | 1044 | |
elmot | 1:d0dfbce63a89 | 1045 | /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */ |
elmot | 1:d0dfbce63a89 | 1046 | SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); |
elmot | 1:d0dfbce63a89 | 1047 | SET_BIT(huart->Instance->CR3, USART_CR3_EIE); |
elmot | 1:d0dfbce63a89 | 1048 | |
elmot | 1:d0dfbce63a89 | 1049 | /* Enable the UART DMA Rx request */ |
elmot | 1:d0dfbce63a89 | 1050 | SET_BIT(huart->Instance->CR3, USART_CR3_DMAR); |
elmot | 1:d0dfbce63a89 | 1051 | } |
elmot | 1:d0dfbce63a89 | 1052 | |
elmot | 1:d0dfbce63a89 | 1053 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 1054 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 1055 | |
elmot | 1:d0dfbce63a89 | 1056 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 1057 | } |
elmot | 1:d0dfbce63a89 | 1058 | |
elmot | 1:d0dfbce63a89 | 1059 | /** |
elmot | 1:d0dfbce63a89 | 1060 | * @brief Stop the DMA Transfer. |
elmot | 1:d0dfbce63a89 | 1061 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1062 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 1063 | */ |
elmot | 1:d0dfbce63a89 | 1064 | HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1065 | { |
elmot | 1:d0dfbce63a89 | 1066 | /* The Lock is not implemented on this API to allow the user application |
elmot | 1:d0dfbce63a89 | 1067 | to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() / |
elmot | 1:d0dfbce63a89 | 1068 | HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback: |
elmot | 1:d0dfbce63a89 | 1069 | indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete |
elmot | 1:d0dfbce63a89 | 1070 | interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of |
elmot | 1:d0dfbce63a89 | 1071 | the stream and the corresponding call back is executed. */ |
elmot | 1:d0dfbce63a89 | 1072 | |
elmot | 1:d0dfbce63a89 | 1073 | /* Stop UART DMA Tx request if ongoing */ |
elmot | 1:d0dfbce63a89 | 1074 | if ((huart->gState == HAL_UART_STATE_BUSY_TX) && |
elmot | 1:d0dfbce63a89 | 1075 | (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))) |
elmot | 1:d0dfbce63a89 | 1076 | { |
elmot | 1:d0dfbce63a89 | 1077 | UART_EndTxTransfer(huart); |
elmot | 1:d0dfbce63a89 | 1078 | |
elmot | 1:d0dfbce63a89 | 1079 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); |
elmot | 1:d0dfbce63a89 | 1080 | |
elmot | 1:d0dfbce63a89 | 1081 | /* Abort the UART DMA Tx channel */ |
elmot | 1:d0dfbce63a89 | 1082 | if(huart->hdmatx != NULL) |
elmot | 1:d0dfbce63a89 | 1083 | { |
elmot | 1:d0dfbce63a89 | 1084 | HAL_DMA_Abort(huart->hdmatx); |
elmot | 1:d0dfbce63a89 | 1085 | } |
elmot | 1:d0dfbce63a89 | 1086 | } |
elmot | 1:d0dfbce63a89 | 1087 | |
elmot | 1:d0dfbce63a89 | 1088 | /* Stop UART DMA Rx request if ongoing */ |
elmot | 1:d0dfbce63a89 | 1089 | if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && |
elmot | 1:d0dfbce63a89 | 1090 | (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))) |
elmot | 1:d0dfbce63a89 | 1091 | { |
elmot | 1:d0dfbce63a89 | 1092 | UART_EndRxTransfer(huart); |
elmot | 1:d0dfbce63a89 | 1093 | |
elmot | 1:d0dfbce63a89 | 1094 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); |
elmot | 1:d0dfbce63a89 | 1095 | |
elmot | 1:d0dfbce63a89 | 1096 | /* Abort the UART DMA Rx channel */ |
elmot | 1:d0dfbce63a89 | 1097 | if(huart->hdmarx != NULL) |
elmot | 1:d0dfbce63a89 | 1098 | { |
elmot | 1:d0dfbce63a89 | 1099 | HAL_DMA_Abort(huart->hdmarx); |
elmot | 1:d0dfbce63a89 | 1100 | } |
elmot | 1:d0dfbce63a89 | 1101 | } |
elmot | 1:d0dfbce63a89 | 1102 | |
elmot | 1:d0dfbce63a89 | 1103 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 1104 | } |
elmot | 1:d0dfbce63a89 | 1105 | |
elmot | 1:d0dfbce63a89 | 1106 | /** |
elmot | 1:d0dfbce63a89 | 1107 | * @brief Handle UART interrupt request. |
elmot | 1:d0dfbce63a89 | 1108 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1109 | * @retval None |
elmot | 1:d0dfbce63a89 | 1110 | */ |
elmot | 1:d0dfbce63a89 | 1111 | void HAL_UART_IRQHandler(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1112 | { |
elmot | 1:d0dfbce63a89 | 1113 | uint32_t isrflags = READ_REG(huart->Instance->ISR); |
elmot | 1:d0dfbce63a89 | 1114 | uint32_t cr1its = READ_REG(huart->Instance->CR1); |
elmot | 1:d0dfbce63a89 | 1115 | uint32_t cr3its; |
elmot | 1:d0dfbce63a89 | 1116 | uint32_t errorflags; |
elmot | 1:d0dfbce63a89 | 1117 | |
elmot | 1:d0dfbce63a89 | 1118 | /* If no error occurs */ |
elmot | 1:d0dfbce63a89 | 1119 | errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE)); |
elmot | 1:d0dfbce63a89 | 1120 | if (errorflags == RESET) |
elmot | 1:d0dfbce63a89 | 1121 | { |
elmot | 1:d0dfbce63a89 | 1122 | /* UART in mode Receiver ---------------------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 1123 | if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET)) |
elmot | 1:d0dfbce63a89 | 1124 | { |
elmot | 1:d0dfbce63a89 | 1125 | UART_Receive_IT(huart); |
elmot | 1:d0dfbce63a89 | 1126 | return; |
elmot | 1:d0dfbce63a89 | 1127 | } |
elmot | 1:d0dfbce63a89 | 1128 | } |
elmot | 1:d0dfbce63a89 | 1129 | |
elmot | 1:d0dfbce63a89 | 1130 | /* If some errors occur */ |
elmot | 1:d0dfbce63a89 | 1131 | cr3its = READ_REG(huart->Instance->CR3); |
elmot | 1:d0dfbce63a89 | 1132 | if((errorflags != RESET) && ((cr3its & (USART_CR3_EIE | USART_CR1_PEIE)) != RESET)) |
elmot | 1:d0dfbce63a89 | 1133 | { |
elmot | 1:d0dfbce63a89 | 1134 | /* UART parity error interrupt occurred -------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 1135 | if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET)) |
elmot | 1:d0dfbce63a89 | 1136 | { |
elmot | 1:d0dfbce63a89 | 1137 | __HAL_UART_CLEAR_IT(huart, UART_CLEAR_PEF); |
elmot | 1:d0dfbce63a89 | 1138 | |
elmot | 1:d0dfbce63a89 | 1139 | huart->ErrorCode |= HAL_UART_ERROR_PE; |
elmot | 1:d0dfbce63a89 | 1140 | } |
elmot | 1:d0dfbce63a89 | 1141 | |
elmot | 1:d0dfbce63a89 | 1142 | /* UART frame error interrupt occurred --------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 1143 | if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) |
elmot | 1:d0dfbce63a89 | 1144 | { |
elmot | 1:d0dfbce63a89 | 1145 | __HAL_UART_CLEAR_IT(huart, UART_CLEAR_FEF); |
elmot | 1:d0dfbce63a89 | 1146 | |
elmot | 1:d0dfbce63a89 | 1147 | huart->ErrorCode |= HAL_UART_ERROR_FE; |
elmot | 1:d0dfbce63a89 | 1148 | } |
elmot | 1:d0dfbce63a89 | 1149 | |
elmot | 1:d0dfbce63a89 | 1150 | /* UART noise error interrupt occurred --------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 1151 | if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) |
elmot | 1:d0dfbce63a89 | 1152 | { |
elmot | 1:d0dfbce63a89 | 1153 | __HAL_UART_CLEAR_IT(huart, UART_CLEAR_NEF); |
elmot | 1:d0dfbce63a89 | 1154 | |
elmot | 1:d0dfbce63a89 | 1155 | huart->ErrorCode |= HAL_UART_ERROR_NE; |
elmot | 1:d0dfbce63a89 | 1156 | } |
elmot | 1:d0dfbce63a89 | 1157 | |
elmot | 1:d0dfbce63a89 | 1158 | /* UART Over-Run interrupt occurred -----------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 1159 | if(((isrflags & USART_ISR_ORE) != RESET) && |
elmot | 1:d0dfbce63a89 | 1160 | (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET))) |
elmot | 1:d0dfbce63a89 | 1161 | { |
elmot | 1:d0dfbce63a89 | 1162 | __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF); |
elmot | 1:d0dfbce63a89 | 1163 | |
elmot | 1:d0dfbce63a89 | 1164 | huart->ErrorCode |= HAL_UART_ERROR_ORE; |
elmot | 1:d0dfbce63a89 | 1165 | } |
elmot | 1:d0dfbce63a89 | 1166 | |
elmot | 1:d0dfbce63a89 | 1167 | /* Call UART Error Call back function if need be --------------------------*/ |
elmot | 1:d0dfbce63a89 | 1168 | if(huart->ErrorCode != HAL_UART_ERROR_NONE) |
elmot | 1:d0dfbce63a89 | 1169 | { |
elmot | 1:d0dfbce63a89 | 1170 | /* UART in mode Receiver ---------------------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 1171 | if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET)) |
elmot | 1:d0dfbce63a89 | 1172 | { |
elmot | 1:d0dfbce63a89 | 1173 | UART_Receive_IT(huart); |
elmot | 1:d0dfbce63a89 | 1174 | } |
elmot | 1:d0dfbce63a89 | 1175 | |
elmot | 1:d0dfbce63a89 | 1176 | /* If Overrun error occurs, or if any error occurs in DMA mode reception, |
elmot | 1:d0dfbce63a89 | 1177 | consider error as blocking */ |
elmot | 1:d0dfbce63a89 | 1178 | if (((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) || |
elmot | 1:d0dfbce63a89 | 1179 | (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))) |
elmot | 1:d0dfbce63a89 | 1180 | { |
elmot | 1:d0dfbce63a89 | 1181 | /* Blocking error : transfer is aborted |
elmot | 1:d0dfbce63a89 | 1182 | Set the UART state ready to be able to start again the process, |
elmot | 1:d0dfbce63a89 | 1183 | Disable Rx Interrupts, and disable Rx DMA request, if ongoing */ |
elmot | 1:d0dfbce63a89 | 1184 | UART_EndRxTransfer(huart); |
elmot | 1:d0dfbce63a89 | 1185 | |
elmot | 1:d0dfbce63a89 | 1186 | /* Disable the UART DMA Rx request if enabled */ |
elmot | 1:d0dfbce63a89 | 1187 | if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) |
elmot | 1:d0dfbce63a89 | 1188 | { |
elmot | 1:d0dfbce63a89 | 1189 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); |
elmot | 1:d0dfbce63a89 | 1190 | |
elmot | 1:d0dfbce63a89 | 1191 | /* Abort the UART DMA Rx channel */ |
elmot | 1:d0dfbce63a89 | 1192 | if(huart->hdmarx != NULL) |
elmot | 1:d0dfbce63a89 | 1193 | { |
elmot | 1:d0dfbce63a89 | 1194 | /* Set the UART DMA Abort callback : |
elmot | 1:d0dfbce63a89 | 1195 | will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */ |
elmot | 1:d0dfbce63a89 | 1196 | huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError; |
elmot | 1:d0dfbce63a89 | 1197 | |
elmot | 1:d0dfbce63a89 | 1198 | /* Abort DMA RX */ |
elmot | 1:d0dfbce63a89 | 1199 | if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK) |
elmot | 1:d0dfbce63a89 | 1200 | { |
elmot | 1:d0dfbce63a89 | 1201 | /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */ |
elmot | 1:d0dfbce63a89 | 1202 | huart->hdmarx->XferAbortCallback(huart->hdmarx); |
elmot | 1:d0dfbce63a89 | 1203 | } |
elmot | 1:d0dfbce63a89 | 1204 | } |
elmot | 1:d0dfbce63a89 | 1205 | else |
elmot | 1:d0dfbce63a89 | 1206 | { |
elmot | 1:d0dfbce63a89 | 1207 | /* Call user error callback */ |
elmot | 1:d0dfbce63a89 | 1208 | HAL_UART_ErrorCallback(huart); |
elmot | 1:d0dfbce63a89 | 1209 | } |
elmot | 1:d0dfbce63a89 | 1210 | } |
elmot | 1:d0dfbce63a89 | 1211 | else |
elmot | 1:d0dfbce63a89 | 1212 | { |
elmot | 1:d0dfbce63a89 | 1213 | /* Call user error callback */ |
elmot | 1:d0dfbce63a89 | 1214 | HAL_UART_ErrorCallback(huart); |
elmot | 1:d0dfbce63a89 | 1215 | } |
elmot | 1:d0dfbce63a89 | 1216 | } |
elmot | 1:d0dfbce63a89 | 1217 | else |
elmot | 1:d0dfbce63a89 | 1218 | { |
elmot | 1:d0dfbce63a89 | 1219 | /* Non Blocking error : transfer could go on. |
elmot | 1:d0dfbce63a89 | 1220 | Error is notified to user through user error callback */ |
elmot | 1:d0dfbce63a89 | 1221 | HAL_UART_ErrorCallback(huart); |
elmot | 1:d0dfbce63a89 | 1222 | huart->ErrorCode = HAL_UART_ERROR_NONE; |
elmot | 1:d0dfbce63a89 | 1223 | } |
elmot | 1:d0dfbce63a89 | 1224 | } |
elmot | 1:d0dfbce63a89 | 1225 | return; |
elmot | 1:d0dfbce63a89 | 1226 | |
elmot | 1:d0dfbce63a89 | 1227 | } /* End if some error occurs */ |
elmot | 1:d0dfbce63a89 | 1228 | |
elmot | 1:d0dfbce63a89 | 1229 | /* UART wakeup from Stop mode interrupt occurred ---------------------------*/ |
elmot | 1:d0dfbce63a89 | 1230 | cr3its = READ_REG(huart->Instance->CR3); |
elmot | 1:d0dfbce63a89 | 1231 | if(((isrflags & USART_ISR_WUF) != RESET) && ((cr3its & USART_CR3_WUFIE) != RESET)) |
elmot | 1:d0dfbce63a89 | 1232 | { |
elmot | 1:d0dfbce63a89 | 1233 | __HAL_UART_CLEAR_IT(huart, UART_CLEAR_WUF); |
elmot | 1:d0dfbce63a89 | 1234 | /* Set the UART state ready to be able to start again the process */ |
elmot | 1:d0dfbce63a89 | 1235 | huart->gState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1236 | huart->RxState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1237 | HAL_UARTEx_WakeupCallback(huart); |
elmot | 1:d0dfbce63a89 | 1238 | return; |
elmot | 1:d0dfbce63a89 | 1239 | } |
elmot | 1:d0dfbce63a89 | 1240 | |
elmot | 1:d0dfbce63a89 | 1241 | /* UART in mode Transmitter ------------------------------------------------*/ |
elmot | 1:d0dfbce63a89 | 1242 | if(((isrflags & USART_ISR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET)) |
elmot | 1:d0dfbce63a89 | 1243 | { |
elmot | 1:d0dfbce63a89 | 1244 | UART_Transmit_IT(huart); |
elmot | 1:d0dfbce63a89 | 1245 | return; |
elmot | 1:d0dfbce63a89 | 1246 | } |
elmot | 1:d0dfbce63a89 | 1247 | |
elmot | 1:d0dfbce63a89 | 1248 | /* UART in mode Transmitter (transmission end) -----------------------------*/ |
elmot | 1:d0dfbce63a89 | 1249 | if(((isrflags & USART_ISR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET)) |
elmot | 1:d0dfbce63a89 | 1250 | { |
elmot | 1:d0dfbce63a89 | 1251 | UART_EndTransmit_IT(huart); |
elmot | 1:d0dfbce63a89 | 1252 | return; |
elmot | 1:d0dfbce63a89 | 1253 | } |
elmot | 1:d0dfbce63a89 | 1254 | |
elmot | 1:d0dfbce63a89 | 1255 | } |
elmot | 1:d0dfbce63a89 | 1256 | |
elmot | 1:d0dfbce63a89 | 1257 | /** |
elmot | 1:d0dfbce63a89 | 1258 | * @brief Tx Transfer completed callback. |
elmot | 1:d0dfbce63a89 | 1259 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1260 | * @retval None |
elmot | 1:d0dfbce63a89 | 1261 | */ |
elmot | 1:d0dfbce63a89 | 1262 | __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1263 | { |
elmot | 1:d0dfbce63a89 | 1264 | /* Prevent unused argument(s) compilation warning */ |
elmot | 1:d0dfbce63a89 | 1265 | UNUSED(huart); |
elmot | 1:d0dfbce63a89 | 1266 | |
elmot | 1:d0dfbce63a89 | 1267 | /* NOTE : This function should not be modified, when the callback is needed, |
elmot | 1:d0dfbce63a89 | 1268 | the HAL_UART_TxCpltCallback can be implemented in the user file. |
elmot | 1:d0dfbce63a89 | 1269 | */ |
elmot | 1:d0dfbce63a89 | 1270 | } |
elmot | 1:d0dfbce63a89 | 1271 | |
elmot | 1:d0dfbce63a89 | 1272 | /** |
elmot | 1:d0dfbce63a89 | 1273 | * @brief Tx Half Transfer completed callback. |
elmot | 1:d0dfbce63a89 | 1274 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1275 | * @retval None |
elmot | 1:d0dfbce63a89 | 1276 | */ |
elmot | 1:d0dfbce63a89 | 1277 | __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1278 | { |
elmot | 1:d0dfbce63a89 | 1279 | /* Prevent unused argument(s) compilation warning */ |
elmot | 1:d0dfbce63a89 | 1280 | UNUSED(huart); |
elmot | 1:d0dfbce63a89 | 1281 | |
elmot | 1:d0dfbce63a89 | 1282 | /* NOTE: This function should not be modified, when the callback is needed, |
elmot | 1:d0dfbce63a89 | 1283 | the HAL_UART_TxHalfCpltCallback can be implemented in the user file. |
elmot | 1:d0dfbce63a89 | 1284 | */ |
elmot | 1:d0dfbce63a89 | 1285 | } |
elmot | 1:d0dfbce63a89 | 1286 | |
elmot | 1:d0dfbce63a89 | 1287 | /** |
elmot | 1:d0dfbce63a89 | 1288 | * @brief Rx Transfer completed callback. |
elmot | 1:d0dfbce63a89 | 1289 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1290 | * @retval None |
elmot | 1:d0dfbce63a89 | 1291 | */ |
elmot | 1:d0dfbce63a89 | 1292 | __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1293 | { |
elmot | 1:d0dfbce63a89 | 1294 | /* Prevent unused argument(s) compilation warning */ |
elmot | 1:d0dfbce63a89 | 1295 | UNUSED(huart); |
elmot | 1:d0dfbce63a89 | 1296 | |
elmot | 1:d0dfbce63a89 | 1297 | /* NOTE : This function should not be modified, when the callback is needed, |
elmot | 1:d0dfbce63a89 | 1298 | the HAL_UART_RxCpltCallback can be implemented in the user file. |
elmot | 1:d0dfbce63a89 | 1299 | */ |
elmot | 1:d0dfbce63a89 | 1300 | } |
elmot | 1:d0dfbce63a89 | 1301 | |
elmot | 1:d0dfbce63a89 | 1302 | /** |
elmot | 1:d0dfbce63a89 | 1303 | * @brief Rx Half Transfer completed callback. |
elmot | 1:d0dfbce63a89 | 1304 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1305 | * @retval None |
elmot | 1:d0dfbce63a89 | 1306 | */ |
elmot | 1:d0dfbce63a89 | 1307 | __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1308 | { |
elmot | 1:d0dfbce63a89 | 1309 | /* Prevent unused argument(s) compilation warning */ |
elmot | 1:d0dfbce63a89 | 1310 | UNUSED(huart); |
elmot | 1:d0dfbce63a89 | 1311 | |
elmot | 1:d0dfbce63a89 | 1312 | /* NOTE: This function should not be modified, when the callback is needed, |
elmot | 1:d0dfbce63a89 | 1313 | the HAL_UART_RxHalfCpltCallback can be implemented in the user file. |
elmot | 1:d0dfbce63a89 | 1314 | */ |
elmot | 1:d0dfbce63a89 | 1315 | } |
elmot | 1:d0dfbce63a89 | 1316 | |
elmot | 1:d0dfbce63a89 | 1317 | /** |
elmot | 1:d0dfbce63a89 | 1318 | * @brief UART error callback. |
elmot | 1:d0dfbce63a89 | 1319 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1320 | * @retval None |
elmot | 1:d0dfbce63a89 | 1321 | */ |
elmot | 1:d0dfbce63a89 | 1322 | __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1323 | { |
elmot | 1:d0dfbce63a89 | 1324 | /* Prevent unused argument(s) compilation warning */ |
elmot | 1:d0dfbce63a89 | 1325 | UNUSED(huart); |
elmot | 1:d0dfbce63a89 | 1326 | |
elmot | 1:d0dfbce63a89 | 1327 | /* NOTE : This function should not be modified, when the callback is needed, |
elmot | 1:d0dfbce63a89 | 1328 | the HAL_UART_ErrorCallback can be implemented in the user file. |
elmot | 1:d0dfbce63a89 | 1329 | */ |
elmot | 1:d0dfbce63a89 | 1330 | } |
elmot | 1:d0dfbce63a89 | 1331 | |
elmot | 1:d0dfbce63a89 | 1332 | /** |
elmot | 1:d0dfbce63a89 | 1333 | * @} |
elmot | 1:d0dfbce63a89 | 1334 | */ |
elmot | 1:d0dfbce63a89 | 1335 | |
elmot | 1:d0dfbce63a89 | 1336 | /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions |
elmot | 1:d0dfbce63a89 | 1337 | * @brief UART control functions |
elmot | 1:d0dfbce63a89 | 1338 | * |
elmot | 1:d0dfbce63a89 | 1339 | @verbatim |
elmot | 1:d0dfbce63a89 | 1340 | =============================================================================== |
elmot | 1:d0dfbce63a89 | 1341 | ##### Peripheral Control functions ##### |
elmot | 1:d0dfbce63a89 | 1342 | =============================================================================== |
elmot | 1:d0dfbce63a89 | 1343 | [..] |
elmot | 1:d0dfbce63a89 | 1344 | This subsection provides a set of functions allowing to control the UART. |
elmot | 1:d0dfbce63a89 | 1345 | (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode |
elmot | 1:d0dfbce63a89 | 1346 | (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode |
elmot | 1:d0dfbce63a89 | 1347 | (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode |
elmot | 1:d0dfbce63a89 | 1348 | (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode |
elmot | 1:d0dfbce63a89 | 1349 | (+) UART_SetConfig() API configures the UART peripheral |
elmot | 1:d0dfbce63a89 | 1350 | (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features |
elmot | 1:d0dfbce63a89 | 1351 | (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization |
elmot | 1:d0dfbce63a89 | 1352 | (+) UART_Wakeup_AddressConfig() API configures the wake-up from stop mode parameters |
elmot | 1:d0dfbce63a89 | 1353 | (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter |
elmot | 1:d0dfbce63a89 | 1354 | (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver |
elmot | 1:d0dfbce63a89 | 1355 | (+) HAL_LIN_SendBreak() API transmits the break characters |
elmot | 1:d0dfbce63a89 | 1356 | @endverbatim |
elmot | 1:d0dfbce63a89 | 1357 | * @{ |
elmot | 1:d0dfbce63a89 | 1358 | */ |
elmot | 1:d0dfbce63a89 | 1359 | |
elmot | 1:d0dfbce63a89 | 1360 | /** |
elmot | 1:d0dfbce63a89 | 1361 | * @brief Enable UART in mute mode (does not mean UART enters mute mode; |
elmot | 1:d0dfbce63a89 | 1362 | * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called). |
elmot | 1:d0dfbce63a89 | 1363 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1364 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 1365 | */ |
elmot | 1:d0dfbce63a89 | 1366 | HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1367 | { |
elmot | 1:d0dfbce63a89 | 1368 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 1369 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 1370 | |
elmot | 1:d0dfbce63a89 | 1371 | huart->gState = HAL_UART_STATE_BUSY; |
elmot | 1:d0dfbce63a89 | 1372 | |
elmot | 1:d0dfbce63a89 | 1373 | /* Enable USART mute mode by setting the MME bit in the CR1 register */ |
elmot | 1:d0dfbce63a89 | 1374 | SET_BIT(huart->Instance->CR1, USART_CR1_MME); |
elmot | 1:d0dfbce63a89 | 1375 | |
elmot | 1:d0dfbce63a89 | 1376 | huart->gState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1377 | |
elmot | 1:d0dfbce63a89 | 1378 | return (UART_CheckIdleState(huart)); |
elmot | 1:d0dfbce63a89 | 1379 | } |
elmot | 1:d0dfbce63a89 | 1380 | |
elmot | 1:d0dfbce63a89 | 1381 | /** |
elmot | 1:d0dfbce63a89 | 1382 | * @brief Disable UART mute mode (does not mean the UART actually exits mute mode |
elmot | 1:d0dfbce63a89 | 1383 | * as it may not have been in mute mode at this very moment). |
elmot | 1:d0dfbce63a89 | 1384 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1385 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 1386 | */ |
elmot | 1:d0dfbce63a89 | 1387 | HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1388 | { |
elmot | 1:d0dfbce63a89 | 1389 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 1390 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 1391 | |
elmot | 1:d0dfbce63a89 | 1392 | huart->gState = HAL_UART_STATE_BUSY; |
elmot | 1:d0dfbce63a89 | 1393 | |
elmot | 1:d0dfbce63a89 | 1394 | /* Disable USART mute mode by clearing the MME bit in the CR1 register */ |
elmot | 1:d0dfbce63a89 | 1395 | CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME); |
elmot | 1:d0dfbce63a89 | 1396 | |
elmot | 1:d0dfbce63a89 | 1397 | huart->gState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1398 | |
elmot | 1:d0dfbce63a89 | 1399 | return (UART_CheckIdleState(huart)); |
elmot | 1:d0dfbce63a89 | 1400 | } |
elmot | 1:d0dfbce63a89 | 1401 | |
elmot | 1:d0dfbce63a89 | 1402 | /** |
elmot | 1:d0dfbce63a89 | 1403 | * @brief Enter UART mute mode (means UART actually enters mute mode). |
elmot | 1:d0dfbce63a89 | 1404 | * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called. |
elmot | 1:d0dfbce63a89 | 1405 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1406 | * @retval None |
elmot | 1:d0dfbce63a89 | 1407 | */ |
elmot | 1:d0dfbce63a89 | 1408 | void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1409 | { |
elmot | 1:d0dfbce63a89 | 1410 | __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST); |
elmot | 1:d0dfbce63a89 | 1411 | } |
elmot | 1:d0dfbce63a89 | 1412 | |
elmot | 1:d0dfbce63a89 | 1413 | /** |
elmot | 1:d0dfbce63a89 | 1414 | * @brief Enable the UART transmitter and disable the UART receiver. |
elmot | 1:d0dfbce63a89 | 1415 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1416 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 1417 | */ |
elmot | 1:d0dfbce63a89 | 1418 | HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1419 | { |
elmot | 1:d0dfbce63a89 | 1420 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 1421 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 1422 | huart->gState = HAL_UART_STATE_BUSY; |
elmot | 1:d0dfbce63a89 | 1423 | |
elmot | 1:d0dfbce63a89 | 1424 | /* Clear TE and RE bits */ |
elmot | 1:d0dfbce63a89 | 1425 | CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE)); |
elmot | 1:d0dfbce63a89 | 1426 | /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */ |
elmot | 1:d0dfbce63a89 | 1427 | SET_BIT(huart->Instance->CR1, USART_CR1_TE); |
elmot | 1:d0dfbce63a89 | 1428 | |
elmot | 1:d0dfbce63a89 | 1429 | huart->gState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1430 | |
elmot | 1:d0dfbce63a89 | 1431 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 1432 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 1433 | |
elmot | 1:d0dfbce63a89 | 1434 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 1435 | } |
elmot | 1:d0dfbce63a89 | 1436 | |
elmot | 1:d0dfbce63a89 | 1437 | /** |
elmot | 1:d0dfbce63a89 | 1438 | * @brief Enable the UART receiver and disable the UART transmitter. |
elmot | 1:d0dfbce63a89 | 1439 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1440 | * @retval HAL status. |
elmot | 1:d0dfbce63a89 | 1441 | */ |
elmot | 1:d0dfbce63a89 | 1442 | HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1443 | { |
elmot | 1:d0dfbce63a89 | 1444 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 1445 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 1446 | huart->gState = HAL_UART_STATE_BUSY; |
elmot | 1:d0dfbce63a89 | 1447 | |
elmot | 1:d0dfbce63a89 | 1448 | /* Clear TE and RE bits */ |
elmot | 1:d0dfbce63a89 | 1449 | CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE)); |
elmot | 1:d0dfbce63a89 | 1450 | /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */ |
elmot | 1:d0dfbce63a89 | 1451 | SET_BIT(huart->Instance->CR1, USART_CR1_RE); |
elmot | 1:d0dfbce63a89 | 1452 | |
elmot | 1:d0dfbce63a89 | 1453 | huart->gState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1454 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 1455 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 1456 | |
elmot | 1:d0dfbce63a89 | 1457 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 1458 | } |
elmot | 1:d0dfbce63a89 | 1459 | |
elmot | 1:d0dfbce63a89 | 1460 | |
elmot | 1:d0dfbce63a89 | 1461 | /** |
elmot | 1:d0dfbce63a89 | 1462 | * @brief Transmit break characters. |
elmot | 1:d0dfbce63a89 | 1463 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1464 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 1465 | */ |
elmot | 1:d0dfbce63a89 | 1466 | HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1467 | { |
elmot | 1:d0dfbce63a89 | 1468 | /* Check the parameters */ |
elmot | 1:d0dfbce63a89 | 1469 | assert_param(IS_UART_LIN_INSTANCE(huart->Instance)); |
elmot | 1:d0dfbce63a89 | 1470 | |
elmot | 1:d0dfbce63a89 | 1471 | /* Process Locked */ |
elmot | 1:d0dfbce63a89 | 1472 | __HAL_LOCK(huart); |
elmot | 1:d0dfbce63a89 | 1473 | |
elmot | 1:d0dfbce63a89 | 1474 | huart->gState = HAL_UART_STATE_BUSY; |
elmot | 1:d0dfbce63a89 | 1475 | |
elmot | 1:d0dfbce63a89 | 1476 | /* Send break characters */ |
elmot | 1:d0dfbce63a89 | 1477 | huart->Instance->RQR |= UART_SENDBREAK_REQUEST; |
elmot | 1:d0dfbce63a89 | 1478 | |
elmot | 1:d0dfbce63a89 | 1479 | huart->gState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1480 | |
elmot | 1:d0dfbce63a89 | 1481 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 1482 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 1483 | |
elmot | 1:d0dfbce63a89 | 1484 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 1485 | } |
elmot | 1:d0dfbce63a89 | 1486 | |
elmot | 1:d0dfbce63a89 | 1487 | |
elmot | 1:d0dfbce63a89 | 1488 | /** |
elmot | 1:d0dfbce63a89 | 1489 | * @} |
elmot | 1:d0dfbce63a89 | 1490 | */ |
elmot | 1:d0dfbce63a89 | 1491 | |
elmot | 1:d0dfbce63a89 | 1492 | /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions |
elmot | 1:d0dfbce63a89 | 1493 | * @brief UART Peripheral State functions |
elmot | 1:d0dfbce63a89 | 1494 | * |
elmot | 1:d0dfbce63a89 | 1495 | @verbatim |
elmot | 1:d0dfbce63a89 | 1496 | ============================================================================== |
elmot | 1:d0dfbce63a89 | 1497 | ##### Peripheral State and Error functions ##### |
elmot | 1:d0dfbce63a89 | 1498 | ============================================================================== |
elmot | 1:d0dfbce63a89 | 1499 | [..] |
elmot | 1:d0dfbce63a89 | 1500 | This subsection provides functions allowing to : |
elmot | 1:d0dfbce63a89 | 1501 | (+) Return the UART handle state. |
elmot | 1:d0dfbce63a89 | 1502 | (+) Return the UART handle error code |
elmot | 1:d0dfbce63a89 | 1503 | |
elmot | 1:d0dfbce63a89 | 1504 | @endverbatim |
elmot | 1:d0dfbce63a89 | 1505 | * @{ |
elmot | 1:d0dfbce63a89 | 1506 | */ |
elmot | 1:d0dfbce63a89 | 1507 | |
elmot | 1:d0dfbce63a89 | 1508 | /** |
elmot | 1:d0dfbce63a89 | 1509 | * @brief Return the UART handle state. |
elmot | 1:d0dfbce63a89 | 1510 | * @param huart Pointer to a UART_HandleTypeDef structure that contains |
elmot | 1:d0dfbce63a89 | 1511 | * the configuration information for the specified UART. |
elmot | 1:d0dfbce63a89 | 1512 | * @retval HAL state |
elmot | 1:d0dfbce63a89 | 1513 | */ |
elmot | 1:d0dfbce63a89 | 1514 | HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1515 | { |
elmot | 1:d0dfbce63a89 | 1516 | uint32_t temp1= 0x00, temp2 = 0x00; |
elmot | 1:d0dfbce63a89 | 1517 | temp1 = huart->gState; |
elmot | 1:d0dfbce63a89 | 1518 | temp2 = huart->RxState; |
elmot | 1:d0dfbce63a89 | 1519 | |
elmot | 1:d0dfbce63a89 | 1520 | return (HAL_UART_StateTypeDef)(temp1 | temp2); |
elmot | 1:d0dfbce63a89 | 1521 | } |
elmot | 1:d0dfbce63a89 | 1522 | |
elmot | 1:d0dfbce63a89 | 1523 | /** |
elmot | 1:d0dfbce63a89 | 1524 | * @brief Return the UART handle error code. |
elmot | 1:d0dfbce63a89 | 1525 | * @param huart Pointer to a UART_HandleTypeDef structure that contains |
elmot | 1:d0dfbce63a89 | 1526 | * the configuration information for the specified UART. |
elmot | 1:d0dfbce63a89 | 1527 | * @retval UART Error Code |
elmot | 1:d0dfbce63a89 | 1528 | */ |
elmot | 1:d0dfbce63a89 | 1529 | uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1530 | { |
elmot | 1:d0dfbce63a89 | 1531 | return huart->ErrorCode; |
elmot | 1:d0dfbce63a89 | 1532 | } |
elmot | 1:d0dfbce63a89 | 1533 | /** |
elmot | 1:d0dfbce63a89 | 1534 | * @} |
elmot | 1:d0dfbce63a89 | 1535 | */ |
elmot | 1:d0dfbce63a89 | 1536 | |
elmot | 1:d0dfbce63a89 | 1537 | /** |
elmot | 1:d0dfbce63a89 | 1538 | * @} |
elmot | 1:d0dfbce63a89 | 1539 | */ |
elmot | 1:d0dfbce63a89 | 1540 | |
elmot | 1:d0dfbce63a89 | 1541 | /** @defgroup UART_Private_Functions UART Private Functions |
elmot | 1:d0dfbce63a89 | 1542 | * @{ |
elmot | 1:d0dfbce63a89 | 1543 | */ |
elmot | 1:d0dfbce63a89 | 1544 | |
elmot | 1:d0dfbce63a89 | 1545 | /** |
elmot | 1:d0dfbce63a89 | 1546 | * @brief Configure the UART peripheral. |
elmot | 1:d0dfbce63a89 | 1547 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1548 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 1549 | */ |
elmot | 1:d0dfbce63a89 | 1550 | HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1551 | { |
elmot | 1:d0dfbce63a89 | 1552 | uint32_t tmpreg = 0x00000000; |
elmot | 1:d0dfbce63a89 | 1553 | UART_ClockSourceTypeDef clocksource = UART_CLOCKSOURCE_UNDEFINED; |
elmot | 1:d0dfbce63a89 | 1554 | uint16_t brrtemp = 0x0000; |
elmot | 1:d0dfbce63a89 | 1555 | uint16_t usartdiv = 0x0000; |
elmot | 1:d0dfbce63a89 | 1556 | HAL_StatusTypeDef ret = HAL_OK; |
elmot | 1:d0dfbce63a89 | 1557 | |
elmot | 1:d0dfbce63a89 | 1558 | /* Check the parameters */ |
elmot | 1:d0dfbce63a89 | 1559 | assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1560 | assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); |
elmot | 1:d0dfbce63a89 | 1561 | if(UART_INSTANCE_LOWPOWER(huart)) |
elmot | 1:d0dfbce63a89 | 1562 | { |
elmot | 1:d0dfbce63a89 | 1563 | assert_param(IS_LPUART_STOPBITS(huart->Init.StopBits)); |
elmot | 1:d0dfbce63a89 | 1564 | } |
elmot | 1:d0dfbce63a89 | 1565 | else |
elmot | 1:d0dfbce63a89 | 1566 | { |
elmot | 1:d0dfbce63a89 | 1567 | assert_param(IS_UART_STOPBITS(huart->Init.StopBits)); |
elmot | 1:d0dfbce63a89 | 1568 | assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling)); |
elmot | 1:d0dfbce63a89 | 1569 | } |
elmot | 1:d0dfbce63a89 | 1570 | |
elmot | 1:d0dfbce63a89 | 1571 | assert_param(IS_UART_PARITY(huart->Init.Parity)); |
elmot | 1:d0dfbce63a89 | 1572 | assert_param(IS_UART_MODE(huart->Init.Mode)); |
elmot | 1:d0dfbce63a89 | 1573 | assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl)); |
elmot | 1:d0dfbce63a89 | 1574 | assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); |
elmot | 1:d0dfbce63a89 | 1575 | |
elmot | 1:d0dfbce63a89 | 1576 | |
elmot | 1:d0dfbce63a89 | 1577 | /*-------------------------- USART CR1 Configuration -----------------------*/ |
elmot | 1:d0dfbce63a89 | 1578 | /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure |
elmot | 1:d0dfbce63a89 | 1579 | * the UART Word Length, Parity, Mode and oversampling: |
elmot | 1:d0dfbce63a89 | 1580 | * set the M bits according to huart->Init.WordLength value |
elmot | 1:d0dfbce63a89 | 1581 | * set PCE and PS bits according to huart->Init.Parity value |
elmot | 1:d0dfbce63a89 | 1582 | * set TE and RE bits according to huart->Init.Mode value |
elmot | 1:d0dfbce63a89 | 1583 | * set OVER8 bit according to huart->Init.OverSampling value */ |
elmot | 1:d0dfbce63a89 | 1584 | tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ; |
elmot | 1:d0dfbce63a89 | 1585 | MODIFY_REG(huart->Instance->CR1, UART_CR1_FIELDS, tmpreg); |
elmot | 1:d0dfbce63a89 | 1586 | |
elmot | 1:d0dfbce63a89 | 1587 | /*-------------------------- USART CR2 Configuration -----------------------*/ |
elmot | 1:d0dfbce63a89 | 1588 | /* Configure the UART Stop Bits: Set STOP[13:12] bits according |
elmot | 1:d0dfbce63a89 | 1589 | * to huart->Init.StopBits value */ |
elmot | 1:d0dfbce63a89 | 1590 | MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits); |
elmot | 1:d0dfbce63a89 | 1591 | |
elmot | 1:d0dfbce63a89 | 1592 | /*-------------------------- USART CR3 Configuration -----------------------*/ |
elmot | 1:d0dfbce63a89 | 1593 | /* Configure |
elmot | 1:d0dfbce63a89 | 1594 | * - UART HardWare Flow Control: set CTSE and RTSE bits according |
elmot | 1:d0dfbce63a89 | 1595 | * to huart->Init.HwFlowCtl value |
elmot | 1:d0dfbce63a89 | 1596 | * - one-bit sampling method versus three samples' majority rule according |
elmot | 1:d0dfbce63a89 | 1597 | * to huart->Init.OneBitSampling (not applicable to LPUART) */ |
elmot | 1:d0dfbce63a89 | 1598 | tmpreg = (uint32_t)huart->Init.HwFlowCtl; |
elmot | 1:d0dfbce63a89 | 1599 | if (!(UART_INSTANCE_LOWPOWER(huart))) |
elmot | 1:d0dfbce63a89 | 1600 | { |
elmot | 1:d0dfbce63a89 | 1601 | tmpreg |= huart->Init.OneBitSampling; |
elmot | 1:d0dfbce63a89 | 1602 | } |
elmot | 1:d0dfbce63a89 | 1603 | MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT), tmpreg); |
elmot | 1:d0dfbce63a89 | 1604 | |
elmot | 1:d0dfbce63a89 | 1605 | /*-------------------------- USART BRR Configuration -----------------------*/ |
elmot | 1:d0dfbce63a89 | 1606 | UART_GETCLOCKSOURCE(huart, clocksource); |
elmot | 1:d0dfbce63a89 | 1607 | |
elmot | 1:d0dfbce63a89 | 1608 | /* Check LPUART instance */ |
elmot | 1:d0dfbce63a89 | 1609 | if(UART_INSTANCE_LOWPOWER(huart)) |
elmot | 1:d0dfbce63a89 | 1610 | { |
elmot | 1:d0dfbce63a89 | 1611 | /* Retrieve frequency clock */ |
elmot | 1:d0dfbce63a89 | 1612 | tmpreg = 0; |
elmot | 1:d0dfbce63a89 | 1613 | |
elmot | 1:d0dfbce63a89 | 1614 | switch (clocksource) |
elmot | 1:d0dfbce63a89 | 1615 | { |
elmot | 1:d0dfbce63a89 | 1616 | case UART_CLOCKSOURCE_PCLK1: |
elmot | 1:d0dfbce63a89 | 1617 | tmpreg = HAL_RCC_GetPCLK1Freq(); |
elmot | 1:d0dfbce63a89 | 1618 | break; |
elmot | 1:d0dfbce63a89 | 1619 | case UART_CLOCKSOURCE_HSI: |
elmot | 1:d0dfbce63a89 | 1620 | tmpreg = (uint32_t) HSI_VALUE; |
elmot | 1:d0dfbce63a89 | 1621 | break; |
elmot | 1:d0dfbce63a89 | 1622 | case UART_CLOCKSOURCE_SYSCLK: |
elmot | 1:d0dfbce63a89 | 1623 | tmpreg = HAL_RCC_GetSysClockFreq(); |
elmot | 1:d0dfbce63a89 | 1624 | break; |
elmot | 1:d0dfbce63a89 | 1625 | case UART_CLOCKSOURCE_LSE: |
elmot | 1:d0dfbce63a89 | 1626 | tmpreg = (uint32_t) LSE_VALUE; |
elmot | 1:d0dfbce63a89 | 1627 | break; |
elmot | 1:d0dfbce63a89 | 1628 | case UART_CLOCKSOURCE_UNDEFINED: |
elmot | 1:d0dfbce63a89 | 1629 | default: |
elmot | 1:d0dfbce63a89 | 1630 | ret = HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 1631 | break; |
elmot | 1:d0dfbce63a89 | 1632 | } |
elmot | 1:d0dfbce63a89 | 1633 | |
elmot | 1:d0dfbce63a89 | 1634 | /* if proper clock source reported */ |
elmot | 1:d0dfbce63a89 | 1635 | if (tmpreg != 0) |
elmot | 1:d0dfbce63a89 | 1636 | { |
elmot | 1:d0dfbce63a89 | 1637 | /* ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate] */ |
elmot | 1:d0dfbce63a89 | 1638 | if ( (tmpreg < (3 * huart->Init.BaudRate) ) || |
elmot | 1:d0dfbce63a89 | 1639 | (tmpreg > (4096 * huart->Init.BaudRate) )) |
elmot | 1:d0dfbce63a89 | 1640 | { |
elmot | 1:d0dfbce63a89 | 1641 | ret = HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 1642 | } |
elmot | 1:d0dfbce63a89 | 1643 | else |
elmot | 1:d0dfbce63a89 | 1644 | { |
elmot | 1:d0dfbce63a89 | 1645 | switch (clocksource) |
elmot | 1:d0dfbce63a89 | 1646 | { |
elmot | 1:d0dfbce63a89 | 1647 | case UART_CLOCKSOURCE_PCLK1: |
elmot | 1:d0dfbce63a89 | 1648 | tmpreg = (uint32_t)(UART_DIV_LPUART(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1649 | break; |
elmot | 1:d0dfbce63a89 | 1650 | case UART_CLOCKSOURCE_HSI: |
elmot | 1:d0dfbce63a89 | 1651 | tmpreg = (uint32_t)(UART_DIV_LPUART(HSI_VALUE, huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1652 | break; |
elmot | 1:d0dfbce63a89 | 1653 | case UART_CLOCKSOURCE_SYSCLK: |
elmot | 1:d0dfbce63a89 | 1654 | tmpreg = (uint32_t)(UART_DIV_LPUART(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1655 | break; |
elmot | 1:d0dfbce63a89 | 1656 | case UART_CLOCKSOURCE_LSE: |
elmot | 1:d0dfbce63a89 | 1657 | tmpreg = (uint32_t)(UART_DIV_LPUART(LSE_VALUE, huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1658 | break; |
elmot | 1:d0dfbce63a89 | 1659 | case UART_CLOCKSOURCE_UNDEFINED: |
elmot | 1:d0dfbce63a89 | 1660 | default: |
elmot | 1:d0dfbce63a89 | 1661 | ret = HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 1662 | break; |
elmot | 1:d0dfbce63a89 | 1663 | } |
elmot | 1:d0dfbce63a89 | 1664 | |
elmot | 1:d0dfbce63a89 | 1665 | if ((tmpreg >= UART_LPUART_BRR_MIN) && (tmpreg <= UART_LPUART_BRR_MAX)) |
elmot | 1:d0dfbce63a89 | 1666 | { |
elmot | 1:d0dfbce63a89 | 1667 | huart->Instance->BRR = tmpreg; |
elmot | 1:d0dfbce63a89 | 1668 | } |
elmot | 1:d0dfbce63a89 | 1669 | else |
elmot | 1:d0dfbce63a89 | 1670 | { |
elmot | 1:d0dfbce63a89 | 1671 | ret = HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 1672 | } |
elmot | 1:d0dfbce63a89 | 1673 | } /* if ( (tmpreg < (3 * huart->Init.BaudRate) ) || (tmpreg > (4096 * huart->Init.BaudRate) )) */ |
elmot | 1:d0dfbce63a89 | 1674 | } /* if (tmpreg != 0) */ |
elmot | 1:d0dfbce63a89 | 1675 | } |
elmot | 1:d0dfbce63a89 | 1676 | /* Check UART Over Sampling to set Baud Rate Register */ |
elmot | 1:d0dfbce63a89 | 1677 | else if (huart->Init.OverSampling == UART_OVERSAMPLING_8) |
elmot | 1:d0dfbce63a89 | 1678 | { |
elmot | 1:d0dfbce63a89 | 1679 | switch (clocksource) |
elmot | 1:d0dfbce63a89 | 1680 | { |
elmot | 1:d0dfbce63a89 | 1681 | case UART_CLOCKSOURCE_PCLK1: |
elmot | 1:d0dfbce63a89 | 1682 | usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1683 | break; |
elmot | 1:d0dfbce63a89 | 1684 | case UART_CLOCKSOURCE_PCLK2: |
elmot | 1:d0dfbce63a89 | 1685 | usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1686 | break; |
elmot | 1:d0dfbce63a89 | 1687 | case UART_CLOCKSOURCE_HSI: |
elmot | 1:d0dfbce63a89 | 1688 | usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1689 | break; |
elmot | 1:d0dfbce63a89 | 1690 | case UART_CLOCKSOURCE_SYSCLK: |
elmot | 1:d0dfbce63a89 | 1691 | usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1692 | break; |
elmot | 1:d0dfbce63a89 | 1693 | case UART_CLOCKSOURCE_LSE: |
elmot | 1:d0dfbce63a89 | 1694 | usartdiv = (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1695 | break; |
elmot | 1:d0dfbce63a89 | 1696 | case UART_CLOCKSOURCE_UNDEFINED: |
elmot | 1:d0dfbce63a89 | 1697 | default: |
elmot | 1:d0dfbce63a89 | 1698 | ret = HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 1699 | break; |
elmot | 1:d0dfbce63a89 | 1700 | } |
elmot | 1:d0dfbce63a89 | 1701 | |
elmot | 1:d0dfbce63a89 | 1702 | brrtemp = usartdiv & 0xFFF0; |
elmot | 1:d0dfbce63a89 | 1703 | brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000F) >> 1U); |
elmot | 1:d0dfbce63a89 | 1704 | huart->Instance->BRR = brrtemp; |
elmot | 1:d0dfbce63a89 | 1705 | } |
elmot | 1:d0dfbce63a89 | 1706 | else |
elmot | 1:d0dfbce63a89 | 1707 | { |
elmot | 1:d0dfbce63a89 | 1708 | switch (clocksource) |
elmot | 1:d0dfbce63a89 | 1709 | { |
elmot | 1:d0dfbce63a89 | 1710 | case UART_CLOCKSOURCE_PCLK1: |
elmot | 1:d0dfbce63a89 | 1711 | huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1712 | break; |
elmot | 1:d0dfbce63a89 | 1713 | case UART_CLOCKSOURCE_PCLK2: |
elmot | 1:d0dfbce63a89 | 1714 | huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1715 | break; |
elmot | 1:d0dfbce63a89 | 1716 | case UART_CLOCKSOURCE_HSI: |
elmot | 1:d0dfbce63a89 | 1717 | huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1718 | break; |
elmot | 1:d0dfbce63a89 | 1719 | case UART_CLOCKSOURCE_SYSCLK: |
elmot | 1:d0dfbce63a89 | 1720 | huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1721 | break; |
elmot | 1:d0dfbce63a89 | 1722 | case UART_CLOCKSOURCE_LSE: |
elmot | 1:d0dfbce63a89 | 1723 | huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate)); |
elmot | 1:d0dfbce63a89 | 1724 | break; |
elmot | 1:d0dfbce63a89 | 1725 | case UART_CLOCKSOURCE_UNDEFINED: |
elmot | 1:d0dfbce63a89 | 1726 | default: |
elmot | 1:d0dfbce63a89 | 1727 | ret = HAL_ERROR; |
elmot | 1:d0dfbce63a89 | 1728 | break; |
elmot | 1:d0dfbce63a89 | 1729 | } |
elmot | 1:d0dfbce63a89 | 1730 | } |
elmot | 1:d0dfbce63a89 | 1731 | |
elmot | 1:d0dfbce63a89 | 1732 | return ret; |
elmot | 1:d0dfbce63a89 | 1733 | |
elmot | 1:d0dfbce63a89 | 1734 | } |
elmot | 1:d0dfbce63a89 | 1735 | |
elmot | 1:d0dfbce63a89 | 1736 | /** |
elmot | 1:d0dfbce63a89 | 1737 | * @brief Configure the UART peripheral advanced features. |
elmot | 1:d0dfbce63a89 | 1738 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1739 | * @retval None |
elmot | 1:d0dfbce63a89 | 1740 | */ |
elmot | 1:d0dfbce63a89 | 1741 | void UART_AdvFeatureConfig(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1742 | { |
elmot | 1:d0dfbce63a89 | 1743 | /* Check whether the set of advanced features to configure is properly set */ |
elmot | 1:d0dfbce63a89 | 1744 | assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit)); |
elmot | 1:d0dfbce63a89 | 1745 | |
elmot | 1:d0dfbce63a89 | 1746 | /* if required, configure TX pin active level inversion */ |
elmot | 1:d0dfbce63a89 | 1747 | if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT)) |
elmot | 1:d0dfbce63a89 | 1748 | { |
elmot | 1:d0dfbce63a89 | 1749 | assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert)); |
elmot | 1:d0dfbce63a89 | 1750 | MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert); |
elmot | 1:d0dfbce63a89 | 1751 | } |
elmot | 1:d0dfbce63a89 | 1752 | |
elmot | 1:d0dfbce63a89 | 1753 | /* if required, configure RX pin active level inversion */ |
elmot | 1:d0dfbce63a89 | 1754 | if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT)) |
elmot | 1:d0dfbce63a89 | 1755 | { |
elmot | 1:d0dfbce63a89 | 1756 | assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert)); |
elmot | 1:d0dfbce63a89 | 1757 | MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert); |
elmot | 1:d0dfbce63a89 | 1758 | } |
elmot | 1:d0dfbce63a89 | 1759 | |
elmot | 1:d0dfbce63a89 | 1760 | /* if required, configure data inversion */ |
elmot | 1:d0dfbce63a89 | 1761 | if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT)) |
elmot | 1:d0dfbce63a89 | 1762 | { |
elmot | 1:d0dfbce63a89 | 1763 | assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert)); |
elmot | 1:d0dfbce63a89 | 1764 | MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert); |
elmot | 1:d0dfbce63a89 | 1765 | } |
elmot | 1:d0dfbce63a89 | 1766 | |
elmot | 1:d0dfbce63a89 | 1767 | /* if required, configure RX/TX pins swap */ |
elmot | 1:d0dfbce63a89 | 1768 | if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT)) |
elmot | 1:d0dfbce63a89 | 1769 | { |
elmot | 1:d0dfbce63a89 | 1770 | assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap)); |
elmot | 1:d0dfbce63a89 | 1771 | MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap); |
elmot | 1:d0dfbce63a89 | 1772 | } |
elmot | 1:d0dfbce63a89 | 1773 | |
elmot | 1:d0dfbce63a89 | 1774 | /* if required, configure RX overrun detection disabling */ |
elmot | 1:d0dfbce63a89 | 1775 | if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT)) |
elmot | 1:d0dfbce63a89 | 1776 | { |
elmot | 1:d0dfbce63a89 | 1777 | assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable)); |
elmot | 1:d0dfbce63a89 | 1778 | MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable); |
elmot | 1:d0dfbce63a89 | 1779 | } |
elmot | 1:d0dfbce63a89 | 1780 | |
elmot | 1:d0dfbce63a89 | 1781 | /* if required, configure DMA disabling on reception error */ |
elmot | 1:d0dfbce63a89 | 1782 | if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT)) |
elmot | 1:d0dfbce63a89 | 1783 | { |
elmot | 1:d0dfbce63a89 | 1784 | assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError)); |
elmot | 1:d0dfbce63a89 | 1785 | MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError); |
elmot | 1:d0dfbce63a89 | 1786 | } |
elmot | 1:d0dfbce63a89 | 1787 | |
elmot | 1:d0dfbce63a89 | 1788 | /* if required, configure auto Baud rate detection scheme */ |
elmot | 1:d0dfbce63a89 | 1789 | if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT)) |
elmot | 1:d0dfbce63a89 | 1790 | { |
elmot | 1:d0dfbce63a89 | 1791 | assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance)); |
elmot | 1:d0dfbce63a89 | 1792 | assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable)); |
elmot | 1:d0dfbce63a89 | 1793 | MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable); |
elmot | 1:d0dfbce63a89 | 1794 | /* set auto Baudrate detection parameters if detection is enabled */ |
elmot | 1:d0dfbce63a89 | 1795 | if(huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE) |
elmot | 1:d0dfbce63a89 | 1796 | { |
elmot | 1:d0dfbce63a89 | 1797 | assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode)); |
elmot | 1:d0dfbce63a89 | 1798 | MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode); |
elmot | 1:d0dfbce63a89 | 1799 | } |
elmot | 1:d0dfbce63a89 | 1800 | } |
elmot | 1:d0dfbce63a89 | 1801 | |
elmot | 1:d0dfbce63a89 | 1802 | /* if required, configure MSB first on communication line */ |
elmot | 1:d0dfbce63a89 | 1803 | if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT)) |
elmot | 1:d0dfbce63a89 | 1804 | { |
elmot | 1:d0dfbce63a89 | 1805 | assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst)); |
elmot | 1:d0dfbce63a89 | 1806 | MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst); |
elmot | 1:d0dfbce63a89 | 1807 | } |
elmot | 1:d0dfbce63a89 | 1808 | } |
elmot | 1:d0dfbce63a89 | 1809 | |
elmot | 1:d0dfbce63a89 | 1810 | /** |
elmot | 1:d0dfbce63a89 | 1811 | * @brief Check the UART Idle State. |
elmot | 1:d0dfbce63a89 | 1812 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1813 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 1814 | */ |
elmot | 1:d0dfbce63a89 | 1815 | HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1816 | { |
elmot | 1:d0dfbce63a89 | 1817 | uint32_t tickstart = 0; |
elmot | 1:d0dfbce63a89 | 1818 | |
elmot | 1:d0dfbce63a89 | 1819 | /* Initialize the UART ErrorCode */ |
elmot | 1:d0dfbce63a89 | 1820 | huart->ErrorCode = HAL_UART_ERROR_NONE; |
elmot | 1:d0dfbce63a89 | 1821 | |
elmot | 1:d0dfbce63a89 | 1822 | /* Init tickstart for timeout managment*/ |
elmot | 1:d0dfbce63a89 | 1823 | tickstart = HAL_GetTick(); |
elmot | 1:d0dfbce63a89 | 1824 | |
elmot | 1:d0dfbce63a89 | 1825 | /* Check if the Transmitter is enabled */ |
elmot | 1:d0dfbce63a89 | 1826 | if((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE) |
elmot | 1:d0dfbce63a89 | 1827 | { |
elmot | 1:d0dfbce63a89 | 1828 | /* Wait until TEACK flag is set */ |
elmot | 1:d0dfbce63a89 | 1829 | if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK) |
elmot | 1:d0dfbce63a89 | 1830 | { |
elmot | 1:d0dfbce63a89 | 1831 | /* Timeout occurred */ |
elmot | 1:d0dfbce63a89 | 1832 | return HAL_TIMEOUT; |
elmot | 1:d0dfbce63a89 | 1833 | } |
elmot | 1:d0dfbce63a89 | 1834 | } |
elmot | 1:d0dfbce63a89 | 1835 | /* Check if the Receiver is enabled */ |
elmot | 1:d0dfbce63a89 | 1836 | if((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE) |
elmot | 1:d0dfbce63a89 | 1837 | { |
elmot | 1:d0dfbce63a89 | 1838 | /* Wait until REACK flag is set */ |
elmot | 1:d0dfbce63a89 | 1839 | if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK) |
elmot | 1:d0dfbce63a89 | 1840 | { |
elmot | 1:d0dfbce63a89 | 1841 | /* Timeout occurred */ |
elmot | 1:d0dfbce63a89 | 1842 | return HAL_TIMEOUT; |
elmot | 1:d0dfbce63a89 | 1843 | } |
elmot | 1:d0dfbce63a89 | 1844 | } |
elmot | 1:d0dfbce63a89 | 1845 | |
elmot | 1:d0dfbce63a89 | 1846 | /* Initialize the UART State */ |
elmot | 1:d0dfbce63a89 | 1847 | huart->gState= HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1848 | huart->RxState= HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1849 | |
elmot | 1:d0dfbce63a89 | 1850 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 1851 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 1852 | |
elmot | 1:d0dfbce63a89 | 1853 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 1854 | } |
elmot | 1:d0dfbce63a89 | 1855 | |
elmot | 1:d0dfbce63a89 | 1856 | /** |
elmot | 1:d0dfbce63a89 | 1857 | * @brief Handle UART Communication Timeout. |
elmot | 1:d0dfbce63a89 | 1858 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1859 | * @param Flag Specifies the UART flag to check |
elmot | 1:d0dfbce63a89 | 1860 | * @param Status Flag status (SET or RESET) |
elmot | 1:d0dfbce63a89 | 1861 | * @param Tickstart Tick start value |
elmot | 1:d0dfbce63a89 | 1862 | * @param Timeout Timeout duration |
elmot | 1:d0dfbce63a89 | 1863 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 1864 | */ |
elmot | 1:d0dfbce63a89 | 1865 | HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout) |
elmot | 1:d0dfbce63a89 | 1866 | { |
elmot | 1:d0dfbce63a89 | 1867 | /* Wait until flag is set */ |
elmot | 1:d0dfbce63a89 | 1868 | while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status) |
elmot | 1:d0dfbce63a89 | 1869 | { |
elmot | 1:d0dfbce63a89 | 1870 | /* Check for the Timeout */ |
elmot | 1:d0dfbce63a89 | 1871 | if(Timeout != HAL_MAX_DELAY) |
elmot | 1:d0dfbce63a89 | 1872 | { |
elmot | 1:d0dfbce63a89 | 1873 | if((Timeout == 0) || ((HAL_GetTick()-Tickstart) > Timeout)) |
elmot | 1:d0dfbce63a89 | 1874 | { |
elmot | 1:d0dfbce63a89 | 1875 | /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */ |
elmot | 1:d0dfbce63a89 | 1876 | CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE)); |
elmot | 1:d0dfbce63a89 | 1877 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); |
elmot | 1:d0dfbce63a89 | 1878 | |
elmot | 1:d0dfbce63a89 | 1879 | huart->gState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1880 | huart->RxState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1881 | |
elmot | 1:d0dfbce63a89 | 1882 | /* Process Unlocked */ |
elmot | 1:d0dfbce63a89 | 1883 | __HAL_UNLOCK(huart); |
elmot | 1:d0dfbce63a89 | 1884 | |
elmot | 1:d0dfbce63a89 | 1885 | return HAL_TIMEOUT; |
elmot | 1:d0dfbce63a89 | 1886 | } |
elmot | 1:d0dfbce63a89 | 1887 | } |
elmot | 1:d0dfbce63a89 | 1888 | } |
elmot | 1:d0dfbce63a89 | 1889 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 1890 | } |
elmot | 1:d0dfbce63a89 | 1891 | |
elmot | 1:d0dfbce63a89 | 1892 | |
elmot | 1:d0dfbce63a89 | 1893 | /** |
elmot | 1:d0dfbce63a89 | 1894 | * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion). |
elmot | 1:d0dfbce63a89 | 1895 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1896 | * @retval None |
elmot | 1:d0dfbce63a89 | 1897 | */ |
elmot | 1:d0dfbce63a89 | 1898 | static void UART_EndTxTransfer(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1899 | { |
elmot | 1:d0dfbce63a89 | 1900 | /* At end of Tx process, restore huart->gState to Ready */ |
elmot | 1:d0dfbce63a89 | 1901 | huart->gState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1902 | |
elmot | 1:d0dfbce63a89 | 1903 | /* Disable TXEIE and TCIE interrupts */ |
elmot | 1:d0dfbce63a89 | 1904 | CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); |
elmot | 1:d0dfbce63a89 | 1905 | } |
elmot | 1:d0dfbce63a89 | 1906 | |
elmot | 1:d0dfbce63a89 | 1907 | |
elmot | 1:d0dfbce63a89 | 1908 | /** |
elmot | 1:d0dfbce63a89 | 1909 | * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion). |
elmot | 1:d0dfbce63a89 | 1910 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 1911 | * @retval None |
elmot | 1:d0dfbce63a89 | 1912 | */ |
elmot | 1:d0dfbce63a89 | 1913 | static void UART_EndRxTransfer(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 1914 | { |
elmot | 1:d0dfbce63a89 | 1915 | /* At end of Rx process, restore huart->RxState to Ready */ |
elmot | 1:d0dfbce63a89 | 1916 | huart->RxState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1917 | |
elmot | 1:d0dfbce63a89 | 1918 | /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
elmot | 1:d0dfbce63a89 | 1919 | CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); |
elmot | 1:d0dfbce63a89 | 1920 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); |
elmot | 1:d0dfbce63a89 | 1921 | } |
elmot | 1:d0dfbce63a89 | 1922 | |
elmot | 1:d0dfbce63a89 | 1923 | |
elmot | 1:d0dfbce63a89 | 1924 | /** |
elmot | 1:d0dfbce63a89 | 1925 | * @brief DMA UART transmit process complete callback. |
elmot | 1:d0dfbce63a89 | 1926 | * @param hdma: DMA handle. |
elmot | 1:d0dfbce63a89 | 1927 | * @retval None |
elmot | 1:d0dfbce63a89 | 1928 | */ |
elmot | 1:d0dfbce63a89 | 1929 | static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma) |
elmot | 1:d0dfbce63a89 | 1930 | { |
elmot | 1:d0dfbce63a89 | 1931 | UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
elmot | 1:d0dfbce63a89 | 1932 | |
elmot | 1:d0dfbce63a89 | 1933 | /* DMA Normal mode */ |
elmot | 1:d0dfbce63a89 | 1934 | if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) ) |
elmot | 1:d0dfbce63a89 | 1935 | { |
elmot | 1:d0dfbce63a89 | 1936 | huart->TxXferCount = 0; |
elmot | 1:d0dfbce63a89 | 1937 | |
elmot | 1:d0dfbce63a89 | 1938 | /* Disable the DMA transfer for transmit request by resetting the DMAT bit |
elmot | 1:d0dfbce63a89 | 1939 | in the UART CR3 register */ |
elmot | 1:d0dfbce63a89 | 1940 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); |
elmot | 1:d0dfbce63a89 | 1941 | |
elmot | 1:d0dfbce63a89 | 1942 | /* Enable the UART Transmit Complete Interrupt */ |
elmot | 1:d0dfbce63a89 | 1943 | SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); |
elmot | 1:d0dfbce63a89 | 1944 | } |
elmot | 1:d0dfbce63a89 | 1945 | /* DMA Circular mode */ |
elmot | 1:d0dfbce63a89 | 1946 | else |
elmot | 1:d0dfbce63a89 | 1947 | { |
elmot | 1:d0dfbce63a89 | 1948 | HAL_UART_TxCpltCallback(huart); |
elmot | 1:d0dfbce63a89 | 1949 | } |
elmot | 1:d0dfbce63a89 | 1950 | |
elmot | 1:d0dfbce63a89 | 1951 | } |
elmot | 1:d0dfbce63a89 | 1952 | |
elmot | 1:d0dfbce63a89 | 1953 | /** |
elmot | 1:d0dfbce63a89 | 1954 | * @brief DMA UART transmit process half complete callback. |
elmot | 1:d0dfbce63a89 | 1955 | * @param hdma : DMA handle. |
elmot | 1:d0dfbce63a89 | 1956 | * @retval None |
elmot | 1:d0dfbce63a89 | 1957 | */ |
elmot | 1:d0dfbce63a89 | 1958 | static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma) |
elmot | 1:d0dfbce63a89 | 1959 | { |
elmot | 1:d0dfbce63a89 | 1960 | UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
elmot | 1:d0dfbce63a89 | 1961 | |
elmot | 1:d0dfbce63a89 | 1962 | HAL_UART_TxHalfCpltCallback(huart); |
elmot | 1:d0dfbce63a89 | 1963 | } |
elmot | 1:d0dfbce63a89 | 1964 | |
elmot | 1:d0dfbce63a89 | 1965 | /** |
elmot | 1:d0dfbce63a89 | 1966 | * @brief DMA UART receive process complete callback. |
elmot | 1:d0dfbce63a89 | 1967 | * @param hdma: DMA handle. |
elmot | 1:d0dfbce63a89 | 1968 | * @retval None |
elmot | 1:d0dfbce63a89 | 1969 | */ |
elmot | 1:d0dfbce63a89 | 1970 | static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma) |
elmot | 1:d0dfbce63a89 | 1971 | { |
elmot | 1:d0dfbce63a89 | 1972 | UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
elmot | 1:d0dfbce63a89 | 1973 | |
elmot | 1:d0dfbce63a89 | 1974 | /* DMA Normal mode */ |
elmot | 1:d0dfbce63a89 | 1975 | if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) ) |
elmot | 1:d0dfbce63a89 | 1976 | { |
elmot | 1:d0dfbce63a89 | 1977 | huart->RxXferCount = 0; |
elmot | 1:d0dfbce63a89 | 1978 | |
elmot | 1:d0dfbce63a89 | 1979 | /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */ |
elmot | 1:d0dfbce63a89 | 1980 | CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); |
elmot | 1:d0dfbce63a89 | 1981 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); |
elmot | 1:d0dfbce63a89 | 1982 | |
elmot | 1:d0dfbce63a89 | 1983 | /* Disable the DMA transfer for the receiver request by resetting the DMAR bit |
elmot | 1:d0dfbce63a89 | 1984 | in the UART CR3 register */ |
elmot | 1:d0dfbce63a89 | 1985 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); |
elmot | 1:d0dfbce63a89 | 1986 | |
elmot | 1:d0dfbce63a89 | 1987 | /* At end of Rx process, restore huart->RxState to Ready */ |
elmot | 1:d0dfbce63a89 | 1988 | huart->RxState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 1989 | } |
elmot | 1:d0dfbce63a89 | 1990 | |
elmot | 1:d0dfbce63a89 | 1991 | HAL_UART_RxCpltCallback(huart); |
elmot | 1:d0dfbce63a89 | 1992 | } |
elmot | 1:d0dfbce63a89 | 1993 | |
elmot | 1:d0dfbce63a89 | 1994 | /** |
elmot | 1:d0dfbce63a89 | 1995 | * @brief DMA UART receive process half complete callback. |
elmot | 1:d0dfbce63a89 | 1996 | * @param hdma : DMA handle. |
elmot | 1:d0dfbce63a89 | 1997 | * @retval None |
elmot | 1:d0dfbce63a89 | 1998 | */ |
elmot | 1:d0dfbce63a89 | 1999 | static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) |
elmot | 1:d0dfbce63a89 | 2000 | { |
elmot | 1:d0dfbce63a89 | 2001 | UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
elmot | 1:d0dfbce63a89 | 2002 | |
elmot | 1:d0dfbce63a89 | 2003 | HAL_UART_RxHalfCpltCallback(huart); |
elmot | 1:d0dfbce63a89 | 2004 | } |
elmot | 1:d0dfbce63a89 | 2005 | |
elmot | 1:d0dfbce63a89 | 2006 | /** |
elmot | 1:d0dfbce63a89 | 2007 | * @brief DMA UART communication error callback. |
elmot | 1:d0dfbce63a89 | 2008 | * @param hdma: DMA handle. |
elmot | 1:d0dfbce63a89 | 2009 | * @retval None |
elmot | 1:d0dfbce63a89 | 2010 | */ |
elmot | 1:d0dfbce63a89 | 2011 | static void UART_DMAError(DMA_HandleTypeDef *hdma) |
elmot | 1:d0dfbce63a89 | 2012 | { |
elmot | 1:d0dfbce63a89 | 2013 | UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
elmot | 1:d0dfbce63a89 | 2014 | huart->RxXferCount = 0; |
elmot | 1:d0dfbce63a89 | 2015 | huart->TxXferCount = 0; |
elmot | 1:d0dfbce63a89 | 2016 | |
elmot | 1:d0dfbce63a89 | 2017 | /* Stop UART DMA Tx request if ongoing */ |
elmot | 1:d0dfbce63a89 | 2018 | if ( (huart->gState == HAL_UART_STATE_BUSY_TX) |
elmot | 1:d0dfbce63a89 | 2019 | &&(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) ) |
elmot | 1:d0dfbce63a89 | 2020 | { |
elmot | 1:d0dfbce63a89 | 2021 | UART_EndTxTransfer(huart); |
elmot | 1:d0dfbce63a89 | 2022 | } |
elmot | 1:d0dfbce63a89 | 2023 | |
elmot | 1:d0dfbce63a89 | 2024 | /* Stop UART DMA Rx request if ongoing */ |
elmot | 1:d0dfbce63a89 | 2025 | if ( (huart->RxState == HAL_UART_STATE_BUSY_RX) |
elmot | 1:d0dfbce63a89 | 2026 | &&(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) ) |
elmot | 1:d0dfbce63a89 | 2027 | { |
elmot | 1:d0dfbce63a89 | 2028 | UART_EndRxTransfer(huart); |
elmot | 1:d0dfbce63a89 | 2029 | } |
elmot | 1:d0dfbce63a89 | 2030 | |
elmot | 1:d0dfbce63a89 | 2031 | huart->ErrorCode |= HAL_UART_ERROR_DMA; |
elmot | 1:d0dfbce63a89 | 2032 | HAL_UART_ErrorCallback(huart); |
elmot | 1:d0dfbce63a89 | 2033 | } |
elmot | 1:d0dfbce63a89 | 2034 | |
elmot | 1:d0dfbce63a89 | 2035 | /** |
elmot | 1:d0dfbce63a89 | 2036 | * @brief DMA UART communication abort callback, when call by HAL services on Error |
elmot | 1:d0dfbce63a89 | 2037 | * (To be called at end of DMA Abort procedure following error occurrence). |
elmot | 1:d0dfbce63a89 | 2038 | * @param hdma: DMA handle. |
elmot | 1:d0dfbce63a89 | 2039 | * @retval None |
elmot | 1:d0dfbce63a89 | 2040 | */ |
elmot | 1:d0dfbce63a89 | 2041 | static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma) |
elmot | 1:d0dfbce63a89 | 2042 | { |
elmot | 1:d0dfbce63a89 | 2043 | UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
elmot | 1:d0dfbce63a89 | 2044 | huart->RxXferCount = 0; |
elmot | 1:d0dfbce63a89 | 2045 | huart->TxXferCount = 0; |
elmot | 1:d0dfbce63a89 | 2046 | |
elmot | 1:d0dfbce63a89 | 2047 | HAL_UART_ErrorCallback(huart); |
elmot | 1:d0dfbce63a89 | 2048 | } |
elmot | 1:d0dfbce63a89 | 2049 | |
elmot | 1:d0dfbce63a89 | 2050 | /** |
elmot | 1:d0dfbce63a89 | 2051 | * @brief Send an amount of data in interrupt mode. |
elmot | 1:d0dfbce63a89 | 2052 | * @note Function is called under interruption only, once |
elmot | 1:d0dfbce63a89 | 2053 | * interruptions have been enabled by HAL_UART_Transmit_IT(). |
elmot | 1:d0dfbce63a89 | 2054 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 2055 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 2056 | */ |
elmot | 1:d0dfbce63a89 | 2057 | static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 2058 | { |
elmot | 1:d0dfbce63a89 | 2059 | uint16_t* tmp; |
elmot | 1:d0dfbce63a89 | 2060 | |
elmot | 1:d0dfbce63a89 | 2061 | /* Check that a Tx process is ongoing */ |
elmot | 1:d0dfbce63a89 | 2062 | if (huart->gState == HAL_UART_STATE_BUSY_TX) |
elmot | 1:d0dfbce63a89 | 2063 | { |
elmot | 1:d0dfbce63a89 | 2064 | |
elmot | 1:d0dfbce63a89 | 2065 | if(huart->TxXferCount == 0) |
elmot | 1:d0dfbce63a89 | 2066 | { |
elmot | 1:d0dfbce63a89 | 2067 | /* Disable the UART Transmit Data Register Empty Interrupt */ |
elmot | 1:d0dfbce63a89 | 2068 | CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE); |
elmot | 1:d0dfbce63a89 | 2069 | |
elmot | 1:d0dfbce63a89 | 2070 | /* Enable the UART Transmit Complete Interrupt */ |
elmot | 1:d0dfbce63a89 | 2071 | SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); |
elmot | 1:d0dfbce63a89 | 2072 | |
elmot | 1:d0dfbce63a89 | 2073 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 2074 | } |
elmot | 1:d0dfbce63a89 | 2075 | else |
elmot | 1:d0dfbce63a89 | 2076 | { |
elmot | 1:d0dfbce63a89 | 2077 | if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) |
elmot | 1:d0dfbce63a89 | 2078 | { |
elmot | 1:d0dfbce63a89 | 2079 | tmp = (uint16_t*) huart->pTxBuffPtr; |
elmot | 1:d0dfbce63a89 | 2080 | huart->Instance->TDR = (*tmp & (uint16_t)0x01FF); |
elmot | 1:d0dfbce63a89 | 2081 | huart->pTxBuffPtr += 2; |
elmot | 1:d0dfbce63a89 | 2082 | } |
elmot | 1:d0dfbce63a89 | 2083 | else |
elmot | 1:d0dfbce63a89 | 2084 | { |
elmot | 1:d0dfbce63a89 | 2085 | huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0xFF); |
elmot | 1:d0dfbce63a89 | 2086 | } |
elmot | 1:d0dfbce63a89 | 2087 | |
elmot | 1:d0dfbce63a89 | 2088 | huart->TxXferCount--; |
elmot | 1:d0dfbce63a89 | 2089 | |
elmot | 1:d0dfbce63a89 | 2090 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 2091 | } |
elmot | 1:d0dfbce63a89 | 2092 | } |
elmot | 1:d0dfbce63a89 | 2093 | else |
elmot | 1:d0dfbce63a89 | 2094 | { |
elmot | 1:d0dfbce63a89 | 2095 | return HAL_BUSY; |
elmot | 1:d0dfbce63a89 | 2096 | } |
elmot | 1:d0dfbce63a89 | 2097 | } |
elmot | 1:d0dfbce63a89 | 2098 | |
elmot | 1:d0dfbce63a89 | 2099 | |
elmot | 1:d0dfbce63a89 | 2100 | /** |
elmot | 1:d0dfbce63a89 | 2101 | * @brief Wrap up transmission in non-blocking mode. |
elmot | 1:d0dfbce63a89 | 2102 | * @param huart: pointer to a UART_HandleTypeDef structure that contains |
elmot | 1:d0dfbce63a89 | 2103 | * the configuration information for the specified UART module. |
elmot | 1:d0dfbce63a89 | 2104 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 2105 | */ |
elmot | 1:d0dfbce63a89 | 2106 | static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 2107 | { |
elmot | 1:d0dfbce63a89 | 2108 | /* Disable the UART Transmit Complete Interrupt */ |
elmot | 1:d0dfbce63a89 | 2109 | CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE); |
elmot | 1:d0dfbce63a89 | 2110 | |
elmot | 1:d0dfbce63a89 | 2111 | /* Tx process is ended, restore huart->gState to Ready */ |
elmot | 1:d0dfbce63a89 | 2112 | huart->gState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 2113 | |
elmot | 1:d0dfbce63a89 | 2114 | HAL_UART_TxCpltCallback(huart); |
elmot | 1:d0dfbce63a89 | 2115 | |
elmot | 1:d0dfbce63a89 | 2116 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 2117 | } |
elmot | 1:d0dfbce63a89 | 2118 | |
elmot | 1:d0dfbce63a89 | 2119 | |
elmot | 1:d0dfbce63a89 | 2120 | /** |
elmot | 1:d0dfbce63a89 | 2121 | * @brief Receive an amount of data in interrupt mode. |
elmot | 1:d0dfbce63a89 | 2122 | * @note Function is called under interruption only, once |
elmot | 1:d0dfbce63a89 | 2123 | * interruptions have been enabled by HAL_UART_Receive_IT() |
elmot | 1:d0dfbce63a89 | 2124 | * @param huart: UART handle. |
elmot | 1:d0dfbce63a89 | 2125 | * @retval HAL status |
elmot | 1:d0dfbce63a89 | 2126 | */ |
elmot | 1:d0dfbce63a89 | 2127 | static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart) |
elmot | 1:d0dfbce63a89 | 2128 | { |
elmot | 1:d0dfbce63a89 | 2129 | uint16_t* tmp; |
elmot | 1:d0dfbce63a89 | 2130 | uint16_t uhMask = huart->Mask; |
elmot | 1:d0dfbce63a89 | 2131 | uint16_t uhdata; |
elmot | 1:d0dfbce63a89 | 2132 | |
elmot | 1:d0dfbce63a89 | 2133 | /* Check that a Rx process is ongoing */ |
elmot | 1:d0dfbce63a89 | 2134 | if(huart->RxState == HAL_UART_STATE_BUSY_RX) |
elmot | 1:d0dfbce63a89 | 2135 | { |
elmot | 1:d0dfbce63a89 | 2136 | uhdata = (uint16_t) READ_REG(huart->Instance->RDR); |
elmot | 1:d0dfbce63a89 | 2137 | if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) |
elmot | 1:d0dfbce63a89 | 2138 | { |
elmot | 1:d0dfbce63a89 | 2139 | tmp = (uint16_t*) huart->pRxBuffPtr ; |
elmot | 1:d0dfbce63a89 | 2140 | *tmp = (uint16_t)(uhdata & uhMask); |
elmot | 1:d0dfbce63a89 | 2141 | huart->pRxBuffPtr +=2; |
elmot | 1:d0dfbce63a89 | 2142 | } |
elmot | 1:d0dfbce63a89 | 2143 | else |
elmot | 1:d0dfbce63a89 | 2144 | { |
elmot | 1:d0dfbce63a89 | 2145 | *huart->pRxBuffPtr++ = (uint8_t)(uhdata & (uint8_t)uhMask); |
elmot | 1:d0dfbce63a89 | 2146 | } |
elmot | 1:d0dfbce63a89 | 2147 | |
elmot | 1:d0dfbce63a89 | 2148 | if(--huart->RxXferCount == 0) |
elmot | 1:d0dfbce63a89 | 2149 | { |
elmot | 1:d0dfbce63a89 | 2150 | /* Disable the UART Parity Error Interrupt and RXNE interrupt*/ |
elmot | 1:d0dfbce63a89 | 2151 | CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); |
elmot | 1:d0dfbce63a89 | 2152 | |
elmot | 1:d0dfbce63a89 | 2153 | /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */ |
elmot | 1:d0dfbce63a89 | 2154 | CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); |
elmot | 1:d0dfbce63a89 | 2155 | |
elmot | 1:d0dfbce63a89 | 2156 | /* Rx process is completed, restore huart->RxState to Ready */ |
elmot | 1:d0dfbce63a89 | 2157 | huart->RxState = HAL_UART_STATE_READY; |
elmot | 1:d0dfbce63a89 | 2158 | |
elmot | 1:d0dfbce63a89 | 2159 | HAL_UART_RxCpltCallback(huart); |
elmot | 1:d0dfbce63a89 | 2160 | |
elmot | 1:d0dfbce63a89 | 2161 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 2162 | } |
elmot | 1:d0dfbce63a89 | 2163 | |
elmot | 1:d0dfbce63a89 | 2164 | return HAL_OK; |
elmot | 1:d0dfbce63a89 | 2165 | } |
elmot | 1:d0dfbce63a89 | 2166 | else |
elmot | 1:d0dfbce63a89 | 2167 | { |
elmot | 1:d0dfbce63a89 | 2168 | /* Clear RXNE interrupt flag */ |
elmot | 1:d0dfbce63a89 | 2169 | __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); |
elmot | 1:d0dfbce63a89 | 2170 | |
elmot | 1:d0dfbce63a89 | 2171 | return HAL_BUSY; |
elmot | 1:d0dfbce63a89 | 2172 | } |
elmot | 1:d0dfbce63a89 | 2173 | } |
elmot | 1:d0dfbce63a89 | 2174 | |
elmot | 1:d0dfbce63a89 | 2175 | /** |
elmot | 1:d0dfbce63a89 | 2176 | * @} |
elmot | 1:d0dfbce63a89 | 2177 | */ |
elmot | 1:d0dfbce63a89 | 2178 | |
elmot | 1:d0dfbce63a89 | 2179 | #endif /* HAL_UART_MODULE_ENABLED */ |
elmot | 1:d0dfbce63a89 | 2180 | /** |
elmot | 1:d0dfbce63a89 | 2181 | * @} |
elmot | 1:d0dfbce63a89 | 2182 | */ |
elmot | 1:d0dfbce63a89 | 2183 | |
elmot | 1:d0dfbce63a89 | 2184 | /** |
elmot | 1:d0dfbce63a89 | 2185 | * @} |
elmot | 1:d0dfbce63a89 | 2186 | */ |
elmot | 1:d0dfbce63a89 | 2187 | |
elmot | 1:d0dfbce63a89 | 2188 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |