Toyomasa Watarai / mbed-dev

Fork of mbed-dev by mbed official

Committer:
bogdanm
Date:
Thu Oct 01 15:25:22 2015 +0300
Revision:
0:9b334a45a8ff
Initial commit on mbed-dev

Replaces mbed-src (now inactive)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:9b334a45a8ff 1 /**
bogdanm 0:9b334a45a8ff 2 ******************************************************************************
bogdanm 0:9b334a45a8ff 3 * @file stm32f30x_usart.c
bogdanm 0:9b334a45a8ff 4 * @author MCD Application Team
bogdanm 0:9b334a45a8ff 5 * @version V1.1.0
bogdanm 0:9b334a45a8ff 6 * @date 27-February-2014
bogdanm 0:9b334a45a8ff 7 * @brief This file provides firmware functions to manage the following
bogdanm 0:9b334a45a8ff 8 * functionalities of the Universal synchronous asynchronous receiver
bogdanm 0:9b334a45a8ff 9 * transmitter (USART):
bogdanm 0:9b334a45a8ff 10 * + Initialization and Configuration
bogdanm 0:9b334a45a8ff 11 * + STOP Mode
bogdanm 0:9b334a45a8ff 12 * + AutoBaudRate
bogdanm 0:9b334a45a8ff 13 * + Data transfers
bogdanm 0:9b334a45a8ff 14 * + Multi-Processor Communication
bogdanm 0:9b334a45a8ff 15 * + LIN mode
bogdanm 0:9b334a45a8ff 16 * + Half-duplex mode
bogdanm 0:9b334a45a8ff 17 * + Smartcard mode
bogdanm 0:9b334a45a8ff 18 * + IrDA mode
bogdanm 0:9b334a45a8ff 19 * + RS485 mode
bogdanm 0:9b334a45a8ff 20 * + DMA transfers management
bogdanm 0:9b334a45a8ff 21 * + Interrupts and flags management
bogdanm 0:9b334a45a8ff 22 *
bogdanm 0:9b334a45a8ff 23 * @verbatim
bogdanm 0:9b334a45a8ff 24 ===============================================================================
bogdanm 0:9b334a45a8ff 25 ##### How to use this driver #####
bogdanm 0:9b334a45a8ff 26 ===============================================================================
bogdanm 0:9b334a45a8ff 27 [..]
bogdanm 0:9b334a45a8ff 28 (#) Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)
bogdanm 0:9b334a45a8ff 29 function for USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE)
bogdanm 0:9b334a45a8ff 30 function for USART2, USART3, UART4 and UART5.
bogdanm 0:9b334a45a8ff 31 (#) According to the USART mode, enable the GPIO clocks using
bogdanm 0:9b334a45a8ff 32 RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS,
bogdanm 0:9b334a45a8ff 33 or and SCLK).
bogdanm 0:9b334a45a8ff 34 (#) Peripheral's alternate function:
bogdanm 0:9b334a45a8ff 35 (++) Connect the pin to the desired peripherals' Alternate
bogdanm 0:9b334a45a8ff 36 Function (AF) using GPIO_PinAFConfig() function.
bogdanm 0:9b334a45a8ff 37 (++) Configure the desired pin in alternate function by:
bogdanm 0:9b334a45a8ff 38 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF.
bogdanm 0:9b334a45a8ff 39 (++) Select the type, pull-up/pull-down and output speed via
bogdanm 0:9b334a45a8ff 40 GPIO_PuPd, GPIO_OType and GPIO_Speed members.
bogdanm 0:9b334a45a8ff 41 (++) Call GPIO_Init() function.
bogdanm 0:9b334a45a8ff 42 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
bogdanm 0:9b334a45a8ff 43 flow control and Mode(Receiver/Transmitter) using the SPI_Init()
bogdanm 0:9b334a45a8ff 44 function.
bogdanm 0:9b334a45a8ff 45 (#) For synchronous mode, enable the clock and program the polarity,
bogdanm 0:9b334a45a8ff 46 phase and last bit using the USART_ClockInit() function.
bogdanm 0:9b334a45a8ff 47 (#) Enable the USART using the USART_Cmd() function.
bogdanm 0:9b334a45a8ff 48 (#) Enable the NVIC and the corresponding interrupt using the function
bogdanm 0:9b334a45a8ff 49 USART_ITConfig() if you need to use interrupt mode.
bogdanm 0:9b334a45a8ff 50 (#) When using the DMA mode:
bogdanm 0:9b334a45a8ff 51 (++) Configure the DMA using DMA_Init() function.
bogdanm 0:9b334a45a8ff 52 (++) Activate the needed channel Request using USART_DMACmd() function.
bogdanm 0:9b334a45a8ff 53 (#) Enable the DMA using the DMA_Cmd() function, when using DMA mode.
bogdanm 0:9b334a45a8ff 54 [..]
bogdanm 0:9b334a45a8ff 55 Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections
bogdanm 0:9b334a45a8ff 56 for more details.
bogdanm 0:9b334a45a8ff 57
bogdanm 0:9b334a45a8ff 58 @endverbatim
bogdanm 0:9b334a45a8ff 59
bogdanm 0:9b334a45a8ff 60 ******************************************************************************
bogdanm 0:9b334a45a8ff 61 * @attention
bogdanm 0:9b334a45a8ff 62 *
bogdanm 0:9b334a45a8ff 63 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
bogdanm 0:9b334a45a8ff 64 *
bogdanm 0:9b334a45a8ff 65 * Redistribution and use in source and binary forms, with or without modification,
bogdanm 0:9b334a45a8ff 66 * are permitted provided that the following conditions are met:
bogdanm 0:9b334a45a8ff 67 * 1. Redistributions of source code must retain the above copyright notice,
bogdanm 0:9b334a45a8ff 68 * this list of conditions and the following disclaimer.
bogdanm 0:9b334a45a8ff 69 * 2. Redistributions in binary form must reproduce the above copyright notice,
bogdanm 0:9b334a45a8ff 70 * this list of conditions and the following disclaimer in the documentation
bogdanm 0:9b334a45a8ff 71 * and/or other materials provided with the distribution.
bogdanm 0:9b334a45a8ff 72 * 3. Neither the name of STMicroelectronics nor the names of its contributors
bogdanm 0:9b334a45a8ff 73 * may be used to endorse or promote products derived from this software
bogdanm 0:9b334a45a8ff 74 * without specific prior written permission.
bogdanm 0:9b334a45a8ff 75 *
bogdanm 0:9b334a45a8ff 76 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
bogdanm 0:9b334a45a8ff 77 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
bogdanm 0:9b334a45a8ff 78 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
bogdanm 0:9b334a45a8ff 79 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
bogdanm 0:9b334a45a8ff 80 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
bogdanm 0:9b334a45a8ff 81 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
bogdanm 0:9b334a45a8ff 82 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
bogdanm 0:9b334a45a8ff 83 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
bogdanm 0:9b334a45a8ff 84 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
bogdanm 0:9b334a45a8ff 85 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bogdanm 0:9b334a45a8ff 86 *
bogdanm 0:9b334a45a8ff 87 ******************************************************************************
bogdanm 0:9b334a45a8ff 88 */
bogdanm 0:9b334a45a8ff 89
bogdanm 0:9b334a45a8ff 90 /* Includes ------------------------------------------------------------------*/
bogdanm 0:9b334a45a8ff 91 #include "stm32f30x_usart.h"
bogdanm 0:9b334a45a8ff 92 #include "stm32f30x_rcc.h"
bogdanm 0:9b334a45a8ff 93
bogdanm 0:9b334a45a8ff 94 /** @addtogroup STM32F30x_StdPeriph_Driver
bogdanm 0:9b334a45a8ff 95 * @{
bogdanm 0:9b334a45a8ff 96 */
bogdanm 0:9b334a45a8ff 97
bogdanm 0:9b334a45a8ff 98 /** @defgroup USART
bogdanm 0:9b334a45a8ff 99 * @brief USART driver modules
bogdanm 0:9b334a45a8ff 100 * @{
bogdanm 0:9b334a45a8ff 101 */
bogdanm 0:9b334a45a8ff 102
bogdanm 0:9b334a45a8ff 103 /* Private typedef -----------------------------------------------------------*/
bogdanm 0:9b334a45a8ff 104 /* Private define ------------------------------------------------------------*/
bogdanm 0:9b334a45a8ff 105
bogdanm 0:9b334a45a8ff 106 /*!< USART CR1 register clear Mask ((~(uint32_t)0xFFFFE6F3)) */
bogdanm 0:9b334a45a8ff 107 #define CR1_CLEAR_MASK ((uint32_t)(USART_CR1_M | USART_CR1_PCE | \
bogdanm 0:9b334a45a8ff 108 USART_CR1_PS | USART_CR1_TE | \
bogdanm 0:9b334a45a8ff 109 USART_CR1_RE))
bogdanm 0:9b334a45a8ff 110
bogdanm 0:9b334a45a8ff 111 /*!< USART CR2 register clock bits clear Mask ((~(uint32_t)0xFFFFF0FF)) */
bogdanm 0:9b334a45a8ff 112 #define CR2_CLOCK_CLEAR_MASK ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
bogdanm 0:9b334a45a8ff 113 USART_CR2_CPHA | USART_CR2_LBCL))
bogdanm 0:9b334a45a8ff 114
bogdanm 0:9b334a45a8ff 115 /*!< USART CR3 register clear Mask ((~(uint32_t)0xFFFFFCFF)) */
bogdanm 0:9b334a45a8ff 116 #define CR3_CLEAR_MASK ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
bogdanm 0:9b334a45a8ff 117
bogdanm 0:9b334a45a8ff 118 /*!< USART Interrupts mask */
bogdanm 0:9b334a45a8ff 119 #define IT_MASK ((uint32_t)0x000000FF)
bogdanm 0:9b334a45a8ff 120
bogdanm 0:9b334a45a8ff 121 /* Private macro -------------------------------------------------------------*/
bogdanm 0:9b334a45a8ff 122 /* Private variables ---------------------------------------------------------*/
bogdanm 0:9b334a45a8ff 123 /* Private function prototypes -----------------------------------------------*/
bogdanm 0:9b334a45a8ff 124 /* Private functions ---------------------------------------------------------*/
bogdanm 0:9b334a45a8ff 125
bogdanm 0:9b334a45a8ff 126 /** @defgroup USART_Private_Functions
bogdanm 0:9b334a45a8ff 127 * @{
bogdanm 0:9b334a45a8ff 128 */
bogdanm 0:9b334a45a8ff 129
bogdanm 0:9b334a45a8ff 130 /** @defgroup USART_Group1 Initialization and Configuration functions
bogdanm 0:9b334a45a8ff 131 * @brief Initialization and Configuration functions
bogdanm 0:9b334a45a8ff 132 *
bogdanm 0:9b334a45a8ff 133 @verbatim
bogdanm 0:9b334a45a8ff 134 ===============================================================================
bogdanm 0:9b334a45a8ff 135 ##### Initialization and Configuration functions #####
bogdanm 0:9b334a45a8ff 136 ===============================================================================
bogdanm 0:9b334a45a8ff 137 [..]
bogdanm 0:9b334a45a8ff 138 This subsection provides a set of functions allowing to initialize the USART
bogdanm 0:9b334a45a8ff 139 in asynchronous and in synchronous modes.
bogdanm 0:9b334a45a8ff 140 (+) For the asynchronous mode only these parameters can be configured:
bogdanm 0:9b334a45a8ff 141 (++) Baud Rate.
bogdanm 0:9b334a45a8ff 142 (++) Word Length.
bogdanm 0:9b334a45a8ff 143 (++) Stop Bit.
bogdanm 0:9b334a45a8ff 144 (++) Parity: If the parity is enabled, then the MSB bit of the data written
bogdanm 0:9b334a45a8ff 145 in the data register is transmitted but is changed by the parity bit.
bogdanm 0:9b334a45a8ff 146 Depending on the frame length defined by the M bit (8-bits or 9-bits),
bogdanm 0:9b334a45a8ff 147 the possible USART frame formats are as listed in the following table:
bogdanm 0:9b334a45a8ff 148 [..]
bogdanm 0:9b334a45a8ff 149 +-------------------------------------------------------------+
bogdanm 0:9b334a45a8ff 150 | M bit | PCE bit | USART frame |
bogdanm 0:9b334a45a8ff 151 |---------------------|---------------------------------------|
bogdanm 0:9b334a45a8ff 152 | 0 | 0 | | SB | 8 bit data | STB | |
bogdanm 0:9b334a45a8ff 153 |---------|-----------|---------------------------------------|
bogdanm 0:9b334a45a8ff 154 | 0 | 1 | | SB | 7 bit data | PB | STB | |
bogdanm 0:9b334a45a8ff 155 |---------|-----------|---------------------------------------|
bogdanm 0:9b334a45a8ff 156 | 1 | 0 | | SB | 9 bit data | STB | |
bogdanm 0:9b334a45a8ff 157 |---------|-----------|---------------------------------------|
bogdanm 0:9b334a45a8ff 158 | 1 | 1 | | SB | 8 bit data | PB | STB | |
bogdanm 0:9b334a45a8ff 159 +-------------------------------------------------------------+
bogdanm 0:9b334a45a8ff 160 [..]
bogdanm 0:9b334a45a8ff 161 (++) Hardware flow control.
bogdanm 0:9b334a45a8ff 162 (++) Receiver/transmitter modes.
bogdanm 0:9b334a45a8ff 163 [..] The USART_Init() function follows the USART asynchronous configuration
bogdanm 0:9b334a45a8ff 164 procedure(details for the procedure are available in reference manual.
bogdanm 0:9b334a45a8ff 165 (+) For the synchronous mode in addition to the asynchronous mode parameters
bogdanm 0:9b334a45a8ff 166 these parameters should be also configured:
bogdanm 0:9b334a45a8ff 167 (++) USART Clock Enabled.
bogdanm 0:9b334a45a8ff 168 (++) USART polarity.
bogdanm 0:9b334a45a8ff 169 (++) USART phase.
bogdanm 0:9b334a45a8ff 170 (++) USART LastBit.
bogdanm 0:9b334a45a8ff 171 [..] These parameters can be configured using the USART_ClockInit() function.
bogdanm 0:9b334a45a8ff 172
bogdanm 0:9b334a45a8ff 173 @endverbatim
bogdanm 0:9b334a45a8ff 174 * @{
bogdanm 0:9b334a45a8ff 175 */
bogdanm 0:9b334a45a8ff 176
bogdanm 0:9b334a45a8ff 177 /**
bogdanm 0:9b334a45a8ff 178 * @brief Deinitializes the USARTx peripheral registers to their default reset values.
bogdanm 0:9b334a45a8ff 179 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 180 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 181 * @retval None
bogdanm 0:9b334a45a8ff 182 */
bogdanm 0:9b334a45a8ff 183 void USART_DeInit(USART_TypeDef* USARTx)
bogdanm 0:9b334a45a8ff 184 {
bogdanm 0:9b334a45a8ff 185 /* Check the parameters */
bogdanm 0:9b334a45a8ff 186 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 187
bogdanm 0:9b334a45a8ff 188 if (USARTx == USART1)
bogdanm 0:9b334a45a8ff 189 {
bogdanm 0:9b334a45a8ff 190 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
bogdanm 0:9b334a45a8ff 191 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
bogdanm 0:9b334a45a8ff 192 }
bogdanm 0:9b334a45a8ff 193 else if (USARTx == USART2)
bogdanm 0:9b334a45a8ff 194 {
bogdanm 0:9b334a45a8ff 195 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
bogdanm 0:9b334a45a8ff 196 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
bogdanm 0:9b334a45a8ff 197 }
bogdanm 0:9b334a45a8ff 198 else if (USARTx == USART3)
bogdanm 0:9b334a45a8ff 199 {
bogdanm 0:9b334a45a8ff 200 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
bogdanm 0:9b334a45a8ff 201 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
bogdanm 0:9b334a45a8ff 202 }
bogdanm 0:9b334a45a8ff 203 else if (USARTx == UART4)
bogdanm 0:9b334a45a8ff 204 {
bogdanm 0:9b334a45a8ff 205 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
bogdanm 0:9b334a45a8ff 206 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
bogdanm 0:9b334a45a8ff 207 }
bogdanm 0:9b334a45a8ff 208 else
bogdanm 0:9b334a45a8ff 209 {
bogdanm 0:9b334a45a8ff 210 if (USARTx == UART5)
bogdanm 0:9b334a45a8ff 211 {
bogdanm 0:9b334a45a8ff 212 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
bogdanm 0:9b334a45a8ff 213 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
bogdanm 0:9b334a45a8ff 214 }
bogdanm 0:9b334a45a8ff 215 }
bogdanm 0:9b334a45a8ff 216 }
bogdanm 0:9b334a45a8ff 217
bogdanm 0:9b334a45a8ff 218 /**
bogdanm 0:9b334a45a8ff 219 * @brief Initializes the USARTx peripheral according to the specified
bogdanm 0:9b334a45a8ff 220 * parameters in the USART_InitStruct .
bogdanm 0:9b334a45a8ff 221 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 222 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 223 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
bogdanm 0:9b334a45a8ff 224 * that contains the configuration information for the specified USART peripheral.
bogdanm 0:9b334a45a8ff 225 * @retval None
bogdanm 0:9b334a45a8ff 226 */
bogdanm 0:9b334a45a8ff 227 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
bogdanm 0:9b334a45a8ff 228 {
bogdanm 0:9b334a45a8ff 229 uint32_t divider = 0, apbclock = 0, tmpreg = 0;
bogdanm 0:9b334a45a8ff 230 RCC_ClocksTypeDef RCC_ClocksStatus;
bogdanm 0:9b334a45a8ff 231
bogdanm 0:9b334a45a8ff 232 /* Check the parameters */
bogdanm 0:9b334a45a8ff 233 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 234 assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
bogdanm 0:9b334a45a8ff 235 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
bogdanm 0:9b334a45a8ff 236 assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
bogdanm 0:9b334a45a8ff 237 assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
bogdanm 0:9b334a45a8ff 238 assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
bogdanm 0:9b334a45a8ff 239 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
bogdanm 0:9b334a45a8ff 240
bogdanm 0:9b334a45a8ff 241 /* Disable USART */
bogdanm 0:9b334a45a8ff 242 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
bogdanm 0:9b334a45a8ff 243
bogdanm 0:9b334a45a8ff 244 /*---------------------------- USART CR2 Configuration -----------------------*/
bogdanm 0:9b334a45a8ff 245 tmpreg = USARTx->CR2;
bogdanm 0:9b334a45a8ff 246 /* Clear STOP[13:12] bits */
bogdanm 0:9b334a45a8ff 247 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
bogdanm 0:9b334a45a8ff 248
bogdanm 0:9b334a45a8ff 249 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
bogdanm 0:9b334a45a8ff 250 /* Set STOP[13:12] bits according to USART_StopBits value */
bogdanm 0:9b334a45a8ff 251 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
bogdanm 0:9b334a45a8ff 252
bogdanm 0:9b334a45a8ff 253 /* Write to USART CR2 */
bogdanm 0:9b334a45a8ff 254 USARTx->CR2 = tmpreg;
bogdanm 0:9b334a45a8ff 255
bogdanm 0:9b334a45a8ff 256 /*---------------------------- USART CR1 Configuration -----------------------*/
bogdanm 0:9b334a45a8ff 257 tmpreg = USARTx->CR1;
bogdanm 0:9b334a45a8ff 258 /* Clear M, PCE, PS, TE and RE bits */
bogdanm 0:9b334a45a8ff 259 tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
bogdanm 0:9b334a45a8ff 260
bogdanm 0:9b334a45a8ff 261 /* Configure the USART Word Length, Parity and mode ----------------------- */
bogdanm 0:9b334a45a8ff 262 /* Set the M bits according to USART_WordLength value */
bogdanm 0:9b334a45a8ff 263 /* Set PCE and PS bits according to USART_Parity value */
bogdanm 0:9b334a45a8ff 264 /* Set TE and RE bits according to USART_Mode value */
bogdanm 0:9b334a45a8ff 265 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
bogdanm 0:9b334a45a8ff 266 USART_InitStruct->USART_Mode;
bogdanm 0:9b334a45a8ff 267
bogdanm 0:9b334a45a8ff 268 /* Write to USART CR1 */
bogdanm 0:9b334a45a8ff 269 USARTx->CR1 = tmpreg;
bogdanm 0:9b334a45a8ff 270
bogdanm 0:9b334a45a8ff 271 /*---------------------------- USART CR3 Configuration -----------------------*/
bogdanm 0:9b334a45a8ff 272 tmpreg = USARTx->CR3;
bogdanm 0:9b334a45a8ff 273 /* Clear CTSE and RTSE bits */
bogdanm 0:9b334a45a8ff 274 tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
bogdanm 0:9b334a45a8ff 275
bogdanm 0:9b334a45a8ff 276 /* Configure the USART HFC -------------------------------------------------*/
bogdanm 0:9b334a45a8ff 277 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
bogdanm 0:9b334a45a8ff 278 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
bogdanm 0:9b334a45a8ff 279
bogdanm 0:9b334a45a8ff 280 /* Write to USART CR3 */
bogdanm 0:9b334a45a8ff 281 USARTx->CR3 = tmpreg;
bogdanm 0:9b334a45a8ff 282
bogdanm 0:9b334a45a8ff 283 /*---------------------------- USART BRR Configuration -----------------------*/
bogdanm 0:9b334a45a8ff 284 /* Configure the USART Baud Rate -------------------------------------------*/
bogdanm 0:9b334a45a8ff 285 RCC_GetClocksFreq(&RCC_ClocksStatus);
bogdanm 0:9b334a45a8ff 286
bogdanm 0:9b334a45a8ff 287 if (USARTx == USART1)
bogdanm 0:9b334a45a8ff 288 {
bogdanm 0:9b334a45a8ff 289 apbclock = RCC_ClocksStatus.USART1CLK_Frequency;
bogdanm 0:9b334a45a8ff 290 }
bogdanm 0:9b334a45a8ff 291 else if (USARTx == USART2)
bogdanm 0:9b334a45a8ff 292 {
bogdanm 0:9b334a45a8ff 293 apbclock = RCC_ClocksStatus.USART2CLK_Frequency;
bogdanm 0:9b334a45a8ff 294 }
bogdanm 0:9b334a45a8ff 295 else if (USARTx == USART3)
bogdanm 0:9b334a45a8ff 296 {
bogdanm 0:9b334a45a8ff 297 apbclock = RCC_ClocksStatus.USART3CLK_Frequency;
bogdanm 0:9b334a45a8ff 298 }
bogdanm 0:9b334a45a8ff 299 else if (USARTx == UART4)
bogdanm 0:9b334a45a8ff 300 {
bogdanm 0:9b334a45a8ff 301 apbclock = RCC_ClocksStatus.UART4CLK_Frequency;
bogdanm 0:9b334a45a8ff 302 }
bogdanm 0:9b334a45a8ff 303 else
bogdanm 0:9b334a45a8ff 304 {
bogdanm 0:9b334a45a8ff 305 apbclock = RCC_ClocksStatus.UART5CLK_Frequency;
bogdanm 0:9b334a45a8ff 306 }
bogdanm 0:9b334a45a8ff 307
bogdanm 0:9b334a45a8ff 308 /* Determine the integer part */
bogdanm 0:9b334a45a8ff 309 if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
bogdanm 0:9b334a45a8ff 310 {
bogdanm 0:9b334a45a8ff 311 /* (divider * 10) computing in case Oversampling mode is 8 Samples */
bogdanm 0:9b334a45a8ff 312 divider = (uint32_t)((2 * apbclock) / (USART_InitStruct->USART_BaudRate));
bogdanm 0:9b334a45a8ff 313 tmpreg = (uint32_t)((2 * apbclock) % (USART_InitStruct->USART_BaudRate));
bogdanm 0:9b334a45a8ff 314 }
bogdanm 0:9b334a45a8ff 315 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
bogdanm 0:9b334a45a8ff 316 {
bogdanm 0:9b334a45a8ff 317 /* (divider * 10) computing in case Oversampling mode is 16 Samples */
bogdanm 0:9b334a45a8ff 318 divider = (uint32_t)((apbclock) / (USART_InitStruct->USART_BaudRate));
bogdanm 0:9b334a45a8ff 319 tmpreg = (uint32_t)((apbclock) % (USART_InitStruct->USART_BaudRate));
bogdanm 0:9b334a45a8ff 320 }
bogdanm 0:9b334a45a8ff 321
bogdanm 0:9b334a45a8ff 322 /* round the divider : if fractional part i greater than 0.5 increment divider */
bogdanm 0:9b334a45a8ff 323 if (tmpreg >= (USART_InitStruct->USART_BaudRate) / 2)
bogdanm 0:9b334a45a8ff 324 {
bogdanm 0:9b334a45a8ff 325 divider++;
bogdanm 0:9b334a45a8ff 326 }
bogdanm 0:9b334a45a8ff 327
bogdanm 0:9b334a45a8ff 328 /* Implement the divider in case Oversampling mode is 8 Samples */
bogdanm 0:9b334a45a8ff 329 if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
bogdanm 0:9b334a45a8ff 330 {
bogdanm 0:9b334a45a8ff 331 /* get the LSB of divider and shift it to the right by 1 bit */
bogdanm 0:9b334a45a8ff 332 tmpreg = (divider & (uint16_t)0x000F) >> 1;
bogdanm 0:9b334a45a8ff 333
bogdanm 0:9b334a45a8ff 334 /* update the divider value */
bogdanm 0:9b334a45a8ff 335 divider = (divider & (uint16_t)0xFFF0) | tmpreg;
bogdanm 0:9b334a45a8ff 336 }
bogdanm 0:9b334a45a8ff 337
bogdanm 0:9b334a45a8ff 338 /* Write to USART BRR */
bogdanm 0:9b334a45a8ff 339 USARTx->BRR = (uint16_t)divider;
bogdanm 0:9b334a45a8ff 340 }
bogdanm 0:9b334a45a8ff 341
bogdanm 0:9b334a45a8ff 342 /**
bogdanm 0:9b334a45a8ff 343 * @brief Fills each USART_InitStruct member with its default value.
bogdanm 0:9b334a45a8ff 344 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
bogdanm 0:9b334a45a8ff 345 * which will be initialized.
bogdanm 0:9b334a45a8ff 346 * @retval None
bogdanm 0:9b334a45a8ff 347 */
bogdanm 0:9b334a45a8ff 348 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
bogdanm 0:9b334a45a8ff 349 {
bogdanm 0:9b334a45a8ff 350 /* USART_InitStruct members default value */
bogdanm 0:9b334a45a8ff 351 USART_InitStruct->USART_BaudRate = 9600;
bogdanm 0:9b334a45a8ff 352 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
bogdanm 0:9b334a45a8ff 353 USART_InitStruct->USART_StopBits = USART_StopBits_1;
bogdanm 0:9b334a45a8ff 354 USART_InitStruct->USART_Parity = USART_Parity_No ;
bogdanm 0:9b334a45a8ff 355 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
bogdanm 0:9b334a45a8ff 356 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
bogdanm 0:9b334a45a8ff 357 }
bogdanm 0:9b334a45a8ff 358
bogdanm 0:9b334a45a8ff 359 /**
bogdanm 0:9b334a45a8ff 360 * @brief Initializes the USARTx peripheral Clock according to the
bogdanm 0:9b334a45a8ff 361 * specified parameters in the USART_ClockInitStruct.
bogdanm 0:9b334a45a8ff 362 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 363 * following values: USART1 or USART2 or USART3.
bogdanm 0:9b334a45a8ff 364 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
bogdanm 0:9b334a45a8ff 365 * structure that contains the configuration information for the specified
bogdanm 0:9b334a45a8ff 366 * USART peripheral.
bogdanm 0:9b334a45a8ff 367 * @retval None
bogdanm 0:9b334a45a8ff 368 */
bogdanm 0:9b334a45a8ff 369 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
bogdanm 0:9b334a45a8ff 370 {
bogdanm 0:9b334a45a8ff 371 uint32_t tmpreg = 0;
bogdanm 0:9b334a45a8ff 372 /* Check the parameters */
bogdanm 0:9b334a45a8ff 373 assert_param(IS_USART_123_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 374 assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
bogdanm 0:9b334a45a8ff 375 assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
bogdanm 0:9b334a45a8ff 376 assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
bogdanm 0:9b334a45a8ff 377 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
bogdanm 0:9b334a45a8ff 378 /*---------------------------- USART CR2 Configuration -----------------------*/
bogdanm 0:9b334a45a8ff 379 tmpreg = USARTx->CR2;
bogdanm 0:9b334a45a8ff 380 /* Clear CLKEN, CPOL, CPHA, LBCL and SSM bits */
bogdanm 0:9b334a45a8ff 381 tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
bogdanm 0:9b334a45a8ff 382 /* Configure the USART Clock, CPOL, CPHA, LastBit and SSM ------------*/
bogdanm 0:9b334a45a8ff 383 /* Set CLKEN bit according to USART_Clock value */
bogdanm 0:9b334a45a8ff 384 /* Set CPOL bit according to USART_CPOL value */
bogdanm 0:9b334a45a8ff 385 /* Set CPHA bit according to USART_CPHA value */
bogdanm 0:9b334a45a8ff 386 /* Set LBCL bit according to USART_LastBit value */
bogdanm 0:9b334a45a8ff 387 tmpreg |= (uint32_t)(USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
bogdanm 0:9b334a45a8ff 388 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit);
bogdanm 0:9b334a45a8ff 389 /* Write to USART CR2 */
bogdanm 0:9b334a45a8ff 390 USARTx->CR2 = tmpreg;
bogdanm 0:9b334a45a8ff 391 }
bogdanm 0:9b334a45a8ff 392
bogdanm 0:9b334a45a8ff 393 /**
bogdanm 0:9b334a45a8ff 394 * @brief Fills each USART_ClockInitStruct member with its default value.
bogdanm 0:9b334a45a8ff 395 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
bogdanm 0:9b334a45a8ff 396 * structure which will be initialized.
bogdanm 0:9b334a45a8ff 397 * @retval None
bogdanm 0:9b334a45a8ff 398 */
bogdanm 0:9b334a45a8ff 399 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
bogdanm 0:9b334a45a8ff 400 {
bogdanm 0:9b334a45a8ff 401 /* USART_ClockInitStruct members default value */
bogdanm 0:9b334a45a8ff 402 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
bogdanm 0:9b334a45a8ff 403 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
bogdanm 0:9b334a45a8ff 404 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
bogdanm 0:9b334a45a8ff 405 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
bogdanm 0:9b334a45a8ff 406 }
bogdanm 0:9b334a45a8ff 407
bogdanm 0:9b334a45a8ff 408 /**
bogdanm 0:9b334a45a8ff 409 * @brief Enables or disables the specified USART peripheral.
bogdanm 0:9b334a45a8ff 410 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 411 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 412 * @param NewState: new state of the USARTx peripheral.
bogdanm 0:9b334a45a8ff 413 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 414 * @retval None
bogdanm 0:9b334a45a8ff 415 */
bogdanm 0:9b334a45a8ff 416 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 417 {
bogdanm 0:9b334a45a8ff 418 /* Check the parameters */
bogdanm 0:9b334a45a8ff 419 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 420 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 421
bogdanm 0:9b334a45a8ff 422 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 423 {
bogdanm 0:9b334a45a8ff 424 /* Enable the selected USART by setting the UE bit in the CR1 register */
bogdanm 0:9b334a45a8ff 425 USARTx->CR1 |= USART_CR1_UE;
bogdanm 0:9b334a45a8ff 426 }
bogdanm 0:9b334a45a8ff 427 else
bogdanm 0:9b334a45a8ff 428 {
bogdanm 0:9b334a45a8ff 429 /* Disable the selected USART by clearing the UE bit in the CR1 register */
bogdanm 0:9b334a45a8ff 430 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
bogdanm 0:9b334a45a8ff 431 }
bogdanm 0:9b334a45a8ff 432 }
bogdanm 0:9b334a45a8ff 433
bogdanm 0:9b334a45a8ff 434 /**
bogdanm 0:9b334a45a8ff 435 * @brief Enables or disables the USART's transmitter or receiver.
bogdanm 0:9b334a45a8ff 436 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 437 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 438 * @param USART_Direction: specifies the USART direction.
bogdanm 0:9b334a45a8ff 439 * This parameter can be any combination of the following values:
bogdanm 0:9b334a45a8ff 440 * @arg USART_Mode_Tx: USART Transmitter
bogdanm 0:9b334a45a8ff 441 * @arg USART_Mode_Rx: USART Receiver
bogdanm 0:9b334a45a8ff 442 * @param NewState: new state of the USART transfer direction.
bogdanm 0:9b334a45a8ff 443 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 444 * @retval None
bogdanm 0:9b334a45a8ff 445 */
bogdanm 0:9b334a45a8ff 446 void USART_DirectionModeCmd(USART_TypeDef* USARTx, uint32_t USART_DirectionMode, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 447 {
bogdanm 0:9b334a45a8ff 448 /* Check the parameters */
bogdanm 0:9b334a45a8ff 449 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 450 assert_param(IS_USART_MODE(USART_DirectionMode));
bogdanm 0:9b334a45a8ff 451 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 452
bogdanm 0:9b334a45a8ff 453 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 454 {
bogdanm 0:9b334a45a8ff 455 /* Enable the USART's transfer interface by setting the TE and/or RE bits
bogdanm 0:9b334a45a8ff 456 in the USART CR1 register */
bogdanm 0:9b334a45a8ff 457 USARTx->CR1 |= USART_DirectionMode;
bogdanm 0:9b334a45a8ff 458 }
bogdanm 0:9b334a45a8ff 459 else
bogdanm 0:9b334a45a8ff 460 {
bogdanm 0:9b334a45a8ff 461 /* Disable the USART's transfer interface by clearing the TE and/or RE bits
bogdanm 0:9b334a45a8ff 462 in the USART CR3 register */
bogdanm 0:9b334a45a8ff 463 USARTx->CR1 &= (uint32_t)~USART_DirectionMode;
bogdanm 0:9b334a45a8ff 464 }
bogdanm 0:9b334a45a8ff 465 }
bogdanm 0:9b334a45a8ff 466
bogdanm 0:9b334a45a8ff 467 /**
bogdanm 0:9b334a45a8ff 468 * @brief Enables or disables the USART's 8x oversampling mode.
bogdanm 0:9b334a45a8ff 469 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 470 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 471 * @param NewState: new state of the USART 8x oversampling mode.
bogdanm 0:9b334a45a8ff 472 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 473 * @note
bogdanm 0:9b334a45a8ff 474 * This function has to be called before calling USART_Init()
bogdanm 0:9b334a45a8ff 475 * function in order to have correct baudrate Divider value.
bogdanm 0:9b334a45a8ff 476 * @retval None
bogdanm 0:9b334a45a8ff 477 */
bogdanm 0:9b334a45a8ff 478 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 479 {
bogdanm 0:9b334a45a8ff 480 /* Check the parameters */
bogdanm 0:9b334a45a8ff 481 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 482 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 483
bogdanm 0:9b334a45a8ff 484 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 485 {
bogdanm 0:9b334a45a8ff 486 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
bogdanm 0:9b334a45a8ff 487 USARTx->CR1 |= USART_CR1_OVER8;
bogdanm 0:9b334a45a8ff 488 }
bogdanm 0:9b334a45a8ff 489 else
bogdanm 0:9b334a45a8ff 490 {
bogdanm 0:9b334a45a8ff 491 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
bogdanm 0:9b334a45a8ff 492 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_OVER8);
bogdanm 0:9b334a45a8ff 493 }
bogdanm 0:9b334a45a8ff 494 }
bogdanm 0:9b334a45a8ff 495
bogdanm 0:9b334a45a8ff 496 /**
bogdanm 0:9b334a45a8ff 497 * @brief Enables or disables the USART's one bit sampling method.
bogdanm 0:9b334a45a8ff 498 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 499 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 500 * @param NewState: new state of the USART one bit sampling method.
bogdanm 0:9b334a45a8ff 501 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 502 * @note
bogdanm 0:9b334a45a8ff 503 * This function has to be called before calling USART_Cmd() function.
bogdanm 0:9b334a45a8ff 504 * @retval None
bogdanm 0:9b334a45a8ff 505 */
bogdanm 0:9b334a45a8ff 506 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 507 {
bogdanm 0:9b334a45a8ff 508 /* Check the parameters */
bogdanm 0:9b334a45a8ff 509 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 510 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 511
bogdanm 0:9b334a45a8ff 512 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 513 {
bogdanm 0:9b334a45a8ff 514 /* Enable the one bit method by setting the ONEBIT bit in the CR3 register */
bogdanm 0:9b334a45a8ff 515 USARTx->CR3 |= USART_CR3_ONEBIT;
bogdanm 0:9b334a45a8ff 516 }
bogdanm 0:9b334a45a8ff 517 else
bogdanm 0:9b334a45a8ff 518 {
bogdanm 0:9b334a45a8ff 519 /* Disable the one bit method by clearing the ONEBIT bit in the CR3 register */
bogdanm 0:9b334a45a8ff 520 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_ONEBIT);
bogdanm 0:9b334a45a8ff 521 }
bogdanm 0:9b334a45a8ff 522 }
bogdanm 0:9b334a45a8ff 523
bogdanm 0:9b334a45a8ff 524 /**
bogdanm 0:9b334a45a8ff 525 * @brief Enables or disables the USART's most significant bit first
bogdanm 0:9b334a45a8ff 526 * transmitted/received following the start bit.
bogdanm 0:9b334a45a8ff 527 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 528 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 529 * @param NewState: new state of the USART most significant bit first
bogdanm 0:9b334a45a8ff 530 * transmitted/received following the start bit.
bogdanm 0:9b334a45a8ff 531 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 532 * @note
bogdanm 0:9b334a45a8ff 533 * This function has to be called before calling USART_Cmd() function.
bogdanm 0:9b334a45a8ff 534 * @retval None
bogdanm 0:9b334a45a8ff 535 */
bogdanm 0:9b334a45a8ff 536 void USART_MSBFirstCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 537 {
bogdanm 0:9b334a45a8ff 538 /* Check the parameters */
bogdanm 0:9b334a45a8ff 539 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 540 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 541
bogdanm 0:9b334a45a8ff 542 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 543 {
bogdanm 0:9b334a45a8ff 544 /* Enable the most significant bit first transmitted/received following the
bogdanm 0:9b334a45a8ff 545 start bit by setting the MSBFIRST bit in the CR2 register */
bogdanm 0:9b334a45a8ff 546 USARTx->CR2 |= USART_CR2_MSBFIRST;
bogdanm 0:9b334a45a8ff 547 }
bogdanm 0:9b334a45a8ff 548 else
bogdanm 0:9b334a45a8ff 549 {
bogdanm 0:9b334a45a8ff 550 /* Disable the most significant bit first transmitted/received following the
bogdanm 0:9b334a45a8ff 551 start bit by clearing the MSBFIRST bit in the CR2 register */
bogdanm 0:9b334a45a8ff 552 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_MSBFIRST);
bogdanm 0:9b334a45a8ff 553 }
bogdanm 0:9b334a45a8ff 554 }
bogdanm 0:9b334a45a8ff 555
bogdanm 0:9b334a45a8ff 556 /**
bogdanm 0:9b334a45a8ff 557 * @brief Enables or disables the binary data inversion.
bogdanm 0:9b334a45a8ff 558 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 559 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 560 * @param NewState: new defined levels for the USART data.
bogdanm 0:9b334a45a8ff 561 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 562 * @arg ENABLE: Logical data from the data register are send/received in negative
bogdanm 0:9b334a45a8ff 563 * logic. (1=L, 0=H). The parity bit is also inverted.
bogdanm 0:9b334a45a8ff 564 * @arg DISABLE: Logical data from the data register are send/received in positive
bogdanm 0:9b334a45a8ff 565 * logic. (1=H, 0=L)
bogdanm 0:9b334a45a8ff 566 * @note
bogdanm 0:9b334a45a8ff 567 * This function has to be called before calling USART_Cmd() function.
bogdanm 0:9b334a45a8ff 568 * @retval None
bogdanm 0:9b334a45a8ff 569 */
bogdanm 0:9b334a45a8ff 570 void USART_DataInvCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 571 {
bogdanm 0:9b334a45a8ff 572 /* Check the parameters */
bogdanm 0:9b334a45a8ff 573 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 574 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 575
bogdanm 0:9b334a45a8ff 576 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 577 {
bogdanm 0:9b334a45a8ff 578 /* Enable the binary data inversion feature by setting the DATAINV bit in
bogdanm 0:9b334a45a8ff 579 the CR2 register */
bogdanm 0:9b334a45a8ff 580 USARTx->CR2 |= USART_CR2_DATAINV;
bogdanm 0:9b334a45a8ff 581 }
bogdanm 0:9b334a45a8ff 582 else
bogdanm 0:9b334a45a8ff 583 {
bogdanm 0:9b334a45a8ff 584 /* Disable the binary data inversion feature by clearing the DATAINV bit in
bogdanm 0:9b334a45a8ff 585 the CR2 register */
bogdanm 0:9b334a45a8ff 586 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_DATAINV);
bogdanm 0:9b334a45a8ff 587 }
bogdanm 0:9b334a45a8ff 588 }
bogdanm 0:9b334a45a8ff 589
bogdanm 0:9b334a45a8ff 590 /**
bogdanm 0:9b334a45a8ff 591 * @brief Enables or disables the Pin(s) active level inversion.
bogdanm 0:9b334a45a8ff 592 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 593 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 594 * @param USART_InvPin: specifies the USART pin(s) to invert.
bogdanm 0:9b334a45a8ff 595 * This parameter can be any combination of the following values:
bogdanm 0:9b334a45a8ff 596 * @arg USART_InvPin_Tx: USART Tx pin active level inversion.
bogdanm 0:9b334a45a8ff 597 * @arg USART_InvPin_Rx: USART Rx pin active level inversion.
bogdanm 0:9b334a45a8ff 598 * @param NewState: new active level status for the USART pin(s).
bogdanm 0:9b334a45a8ff 599 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 600 * - ENABLE: pin(s) signal values are inverted (Vdd =0, Gnd =1).
bogdanm 0:9b334a45a8ff 601 * - DISABLE: pin(s) signal works using the standard logic levels (Vdd =1, Gnd =0).
bogdanm 0:9b334a45a8ff 602 * @note
bogdanm 0:9b334a45a8ff 603 * This function has to be called before calling USART_Cmd() function.
bogdanm 0:9b334a45a8ff 604 * @retval None
bogdanm 0:9b334a45a8ff 605 */
bogdanm 0:9b334a45a8ff 606 void USART_InvPinCmd(USART_TypeDef* USARTx, uint32_t USART_InvPin, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 607 {
bogdanm 0:9b334a45a8ff 608 /* Check the parameters */
bogdanm 0:9b334a45a8ff 609 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 610 assert_param(IS_USART_INVERSTION_PIN(USART_InvPin));
bogdanm 0:9b334a45a8ff 611 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 612
bogdanm 0:9b334a45a8ff 613 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 614 {
bogdanm 0:9b334a45a8ff 615 /* Enable the active level inversion for selected pins by setting the TXINV
bogdanm 0:9b334a45a8ff 616 and/or RXINV bits in the USART CR2 register */
bogdanm 0:9b334a45a8ff 617 USARTx->CR2 |= USART_InvPin;
bogdanm 0:9b334a45a8ff 618 }
bogdanm 0:9b334a45a8ff 619 else
bogdanm 0:9b334a45a8ff 620 {
bogdanm 0:9b334a45a8ff 621 /* Disable the active level inversion for selected requests by clearing the
bogdanm 0:9b334a45a8ff 622 TXINV and/or RXINV bits in the USART CR2 register */
bogdanm 0:9b334a45a8ff 623 USARTx->CR2 &= (uint32_t)~USART_InvPin;
bogdanm 0:9b334a45a8ff 624 }
bogdanm 0:9b334a45a8ff 625 }
bogdanm 0:9b334a45a8ff 626
bogdanm 0:9b334a45a8ff 627 /**
bogdanm 0:9b334a45a8ff 628 * @brief Enables or disables the swap Tx/Rx pins.
bogdanm 0:9b334a45a8ff 629 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 630 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 631 * @param NewState: new state of the USARTx TX/RX pins pinout.
bogdanm 0:9b334a45a8ff 632 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 633 * @arg ENABLE: The TX and RX pins functions are swapped.
bogdanm 0:9b334a45a8ff 634 * @arg DISABLE: TX/RX pins are used as defined in standard pinout
bogdanm 0:9b334a45a8ff 635 * @note
bogdanm 0:9b334a45a8ff 636 * This function has to be called before calling USART_Cmd() function.
bogdanm 0:9b334a45a8ff 637 * @retval None
bogdanm 0:9b334a45a8ff 638 */
bogdanm 0:9b334a45a8ff 639 void USART_SWAPPinCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 640 {
bogdanm 0:9b334a45a8ff 641 /* Check the parameters */
bogdanm 0:9b334a45a8ff 642 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 643 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 644
bogdanm 0:9b334a45a8ff 645 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 646 {
bogdanm 0:9b334a45a8ff 647 /* Enable the SWAP feature by setting the SWAP bit in the CR2 register */
bogdanm 0:9b334a45a8ff 648 USARTx->CR2 |= USART_CR2_SWAP;
bogdanm 0:9b334a45a8ff 649 }
bogdanm 0:9b334a45a8ff 650 else
bogdanm 0:9b334a45a8ff 651 {
bogdanm 0:9b334a45a8ff 652 /* Disable the SWAP feature by clearing the SWAP bit in the CR2 register */
bogdanm 0:9b334a45a8ff 653 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_SWAP);
bogdanm 0:9b334a45a8ff 654 }
bogdanm 0:9b334a45a8ff 655 }
bogdanm 0:9b334a45a8ff 656
bogdanm 0:9b334a45a8ff 657 /**
bogdanm 0:9b334a45a8ff 658 * @brief Enables or disables the receiver Time Out feature.
bogdanm 0:9b334a45a8ff 659 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 660 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 661 * @param NewState: new state of the USARTx receiver Time Out.
bogdanm 0:9b334a45a8ff 662 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 663 * @retval None
bogdanm 0:9b334a45a8ff 664 */
bogdanm 0:9b334a45a8ff 665 void USART_ReceiverTimeOutCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 666 {
bogdanm 0:9b334a45a8ff 667 /* Check the parameters */
bogdanm 0:9b334a45a8ff 668 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 669 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 670
bogdanm 0:9b334a45a8ff 671 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 672 {
bogdanm 0:9b334a45a8ff 673 /* Enable the receiver time out feature by setting the RTOEN bit in the CR2
bogdanm 0:9b334a45a8ff 674 register */
bogdanm 0:9b334a45a8ff 675 USARTx->CR2 |= USART_CR2_RTOEN;
bogdanm 0:9b334a45a8ff 676 }
bogdanm 0:9b334a45a8ff 677 else
bogdanm 0:9b334a45a8ff 678 {
bogdanm 0:9b334a45a8ff 679 /* Disable the receiver time out feature by clearing the RTOEN bit in the CR2
bogdanm 0:9b334a45a8ff 680 register */
bogdanm 0:9b334a45a8ff 681 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_RTOEN);
bogdanm 0:9b334a45a8ff 682 }
bogdanm 0:9b334a45a8ff 683 }
bogdanm 0:9b334a45a8ff 684
bogdanm 0:9b334a45a8ff 685 /**
bogdanm 0:9b334a45a8ff 686 * @brief Sets the receiver Time Out value.
bogdanm 0:9b334a45a8ff 687 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 688 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 689 * @param USART_ReceiverTimeOut: specifies the Receiver Time Out value.
bogdanm 0:9b334a45a8ff 690 * @retval None
bogdanm 0:9b334a45a8ff 691 */
bogdanm 0:9b334a45a8ff 692 void USART_SetReceiverTimeOut(USART_TypeDef* USARTx, uint32_t USART_ReceiverTimeOut)
bogdanm 0:9b334a45a8ff 693 {
bogdanm 0:9b334a45a8ff 694 /* Check the parameters */
bogdanm 0:9b334a45a8ff 695 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 696 assert_param(IS_USART_TIMEOUT(USART_ReceiverTimeOut));
bogdanm 0:9b334a45a8ff 697
bogdanm 0:9b334a45a8ff 698 /* Clear the receiver Time Out value by clearing the RTO[23:0] bits in the RTOR
bogdanm 0:9b334a45a8ff 699 register */
bogdanm 0:9b334a45a8ff 700 USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_RTO);
bogdanm 0:9b334a45a8ff 701 /* Set the receiver Time Out value by setting the RTO[23:0] bits in the RTOR
bogdanm 0:9b334a45a8ff 702 register */
bogdanm 0:9b334a45a8ff 703 USARTx->RTOR |= USART_ReceiverTimeOut;
bogdanm 0:9b334a45a8ff 704 }
bogdanm 0:9b334a45a8ff 705
bogdanm 0:9b334a45a8ff 706 /**
bogdanm 0:9b334a45a8ff 707 * @brief Sets the system clock prescaler.
bogdanm 0:9b334a45a8ff 708 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 709 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 710 * @param USART_Prescaler: specifies the prescaler clock.
bogdanm 0:9b334a45a8ff 711 * @note
bogdanm 0:9b334a45a8ff 712 * This function has to be called before calling USART_Cmd() function.
bogdanm 0:9b334a45a8ff 713 * @retval None
bogdanm 0:9b334a45a8ff 714 */
bogdanm 0:9b334a45a8ff 715 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
bogdanm 0:9b334a45a8ff 716 {
bogdanm 0:9b334a45a8ff 717 /* Check the parameters */
bogdanm 0:9b334a45a8ff 718 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 719
bogdanm 0:9b334a45a8ff 720 /* Clear the USART prescaler */
bogdanm 0:9b334a45a8ff 721 USARTx->GTPR &= USART_GTPR_GT;
bogdanm 0:9b334a45a8ff 722 /* Set the USART prescaler */
bogdanm 0:9b334a45a8ff 723 USARTx->GTPR |= USART_Prescaler;
bogdanm 0:9b334a45a8ff 724 }
bogdanm 0:9b334a45a8ff 725
bogdanm 0:9b334a45a8ff 726 /**
bogdanm 0:9b334a45a8ff 727 * @}
bogdanm 0:9b334a45a8ff 728 */
bogdanm 0:9b334a45a8ff 729
bogdanm 0:9b334a45a8ff 730
bogdanm 0:9b334a45a8ff 731 /** @defgroup USART_Group2 STOP Mode functions
bogdanm 0:9b334a45a8ff 732 * @brief STOP Mode functions
bogdanm 0:9b334a45a8ff 733 *
bogdanm 0:9b334a45a8ff 734 @verbatim
bogdanm 0:9b334a45a8ff 735 ===============================================================================
bogdanm 0:9b334a45a8ff 736 ##### STOP Mode functions #####
bogdanm 0:9b334a45a8ff 737 ===============================================================================
bogdanm 0:9b334a45a8ff 738 [..] This subsection provides a set of functions allowing to manage
bogdanm 0:9b334a45a8ff 739 WakeUp from STOP mode.
bogdanm 0:9b334a45a8ff 740
bogdanm 0:9b334a45a8ff 741 [..] The USART is able to WakeUp from Stop Mode if USART clock is set to HSI
bogdanm 0:9b334a45a8ff 742 or LSI.
bogdanm 0:9b334a45a8ff 743
bogdanm 0:9b334a45a8ff 744 [..] The WakeUp source is configured by calling USART_StopModeWakeUpSourceConfig()
bogdanm 0:9b334a45a8ff 745 function.
bogdanm 0:9b334a45a8ff 746
bogdanm 0:9b334a45a8ff 747 [..] After configuring the source of WakeUp and before entering in Stop Mode
bogdanm 0:9b334a45a8ff 748 USART_STOPModeCmd() function should be called to allow USART WakeUp.
bogdanm 0:9b334a45a8ff 749
bogdanm 0:9b334a45a8ff 750 @endverbatim
bogdanm 0:9b334a45a8ff 751 * @{
bogdanm 0:9b334a45a8ff 752 */
bogdanm 0:9b334a45a8ff 753
bogdanm 0:9b334a45a8ff 754 /**
bogdanm 0:9b334a45a8ff 755 * @brief Enables or disables the specified USART peripheral in STOP Mode.
bogdanm 0:9b334a45a8ff 756 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 757 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 758 * @param NewState: new state of the USARTx peripheral state in stop mode.
bogdanm 0:9b334a45a8ff 759 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 760 * @note
bogdanm 0:9b334a45a8ff 761 * This function has to be called when USART clock is set to HSI or LSE.
bogdanm 0:9b334a45a8ff 762 * @retval None
bogdanm 0:9b334a45a8ff 763 */
bogdanm 0:9b334a45a8ff 764 void USART_STOPModeCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 765 {
bogdanm 0:9b334a45a8ff 766 /* Check the parameters */
bogdanm 0:9b334a45a8ff 767 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 768 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 769
bogdanm 0:9b334a45a8ff 770 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 771 {
bogdanm 0:9b334a45a8ff 772 /* Enable the selected USART in STOP mode by setting the UESM bit in the CR1
bogdanm 0:9b334a45a8ff 773 register */
bogdanm 0:9b334a45a8ff 774 USARTx->CR1 |= USART_CR1_UESM;
bogdanm 0:9b334a45a8ff 775 }
bogdanm 0:9b334a45a8ff 776 else
bogdanm 0:9b334a45a8ff 777 {
bogdanm 0:9b334a45a8ff 778 /* Disable the selected USART in STOP mode by clearing the UE bit in the CR1
bogdanm 0:9b334a45a8ff 779 register */
bogdanm 0:9b334a45a8ff 780 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UESM);
bogdanm 0:9b334a45a8ff 781 }
bogdanm 0:9b334a45a8ff 782 }
bogdanm 0:9b334a45a8ff 783
bogdanm 0:9b334a45a8ff 784 /**
bogdanm 0:9b334a45a8ff 785 * @brief Selects the USART WakeUp method form stop mode.
bogdanm 0:9b334a45a8ff 786 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 787 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 788 * @param USART_WakeUp: specifies the selected USART wakeup method.
bogdanm 0:9b334a45a8ff 789 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 790 * @arg USART_WakeUpSource_AddressMatch: WUF active on address match.
bogdanm 0:9b334a45a8ff 791 * @arg USART_WakeUpSource_StartBit: WUF active on Start bit detection.
bogdanm 0:9b334a45a8ff 792 * @arg USART_WakeUpSource_RXNE: WUF active on RXNE.
bogdanm 0:9b334a45a8ff 793 * @note
bogdanm 0:9b334a45a8ff 794 * This function has to be called before calling USART_Cmd() function.
bogdanm 0:9b334a45a8ff 795 * @retval None
bogdanm 0:9b334a45a8ff 796 */
bogdanm 0:9b334a45a8ff 797 void USART_StopModeWakeUpSourceConfig(USART_TypeDef* USARTx, uint32_t USART_WakeUpSource)
bogdanm 0:9b334a45a8ff 798 {
bogdanm 0:9b334a45a8ff 799 /* Check the parameters */
bogdanm 0:9b334a45a8ff 800 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 801 assert_param(IS_USART_STOPMODE_WAKEUPSOURCE(USART_WakeUpSource));
bogdanm 0:9b334a45a8ff 802
bogdanm 0:9b334a45a8ff 803 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_WUS);
bogdanm 0:9b334a45a8ff 804 USARTx->CR3 |= USART_WakeUpSource;
bogdanm 0:9b334a45a8ff 805 }
bogdanm 0:9b334a45a8ff 806
bogdanm 0:9b334a45a8ff 807 /**
bogdanm 0:9b334a45a8ff 808 * @}
bogdanm 0:9b334a45a8ff 809 */
bogdanm 0:9b334a45a8ff 810
bogdanm 0:9b334a45a8ff 811
bogdanm 0:9b334a45a8ff 812 /** @defgroup USART_Group3 AutoBaudRate functions
bogdanm 0:9b334a45a8ff 813 * @brief AutoBaudRate functions
bogdanm 0:9b334a45a8ff 814 *
bogdanm 0:9b334a45a8ff 815 @verbatim
bogdanm 0:9b334a45a8ff 816 ===============================================================================
bogdanm 0:9b334a45a8ff 817 ##### AutoBaudRate functions #####
bogdanm 0:9b334a45a8ff 818 ===============================================================================
bogdanm 0:9b334a45a8ff 819 [..] This subsection provides a set of functions allowing to manage
bogdanm 0:9b334a45a8ff 820 the AutoBaudRate detections.
bogdanm 0:9b334a45a8ff 821
bogdanm 0:9b334a45a8ff 822 [..] Before Enabling AutoBaudRate detection using USART_AutoBaudRateCmd ()
bogdanm 0:9b334a45a8ff 823 The character patterns used to calculate baudrate must be chosen by calling
bogdanm 0:9b334a45a8ff 824 USART_AutoBaudRateConfig() function. These function take as parameter :
bogdanm 0:9b334a45a8ff 825 (#)USART_AutoBaudRate_StartBit : any character starting with a bit 1.
bogdanm 0:9b334a45a8ff 826 (#)USART_AutoBaudRate_FallingEdge : any character starting with a 10xx bit pattern.
bogdanm 0:9b334a45a8ff 827
bogdanm 0:9b334a45a8ff 828 [..] At any later time, another request for AutoBaudRate detection can be performed
bogdanm 0:9b334a45a8ff 829 using USART_RequestCmd() function.
bogdanm 0:9b334a45a8ff 830
bogdanm 0:9b334a45a8ff 831 [..] The AutoBaudRate detection is monitored by the status of ABRF flag which indicate
bogdanm 0:9b334a45a8ff 832 that the AutoBaudRate detection is completed. In addition to ABRF flag, the ABRE flag
bogdanm 0:9b334a45a8ff 833 indicate that this procedure is completed without success. USART_GetFlagStatus ()
bogdanm 0:9b334a45a8ff 834 function should be used to monitor the status of these flags.
bogdanm 0:9b334a45a8ff 835
bogdanm 0:9b334a45a8ff 836 @endverbatim
bogdanm 0:9b334a45a8ff 837 * @{
bogdanm 0:9b334a45a8ff 838 */
bogdanm 0:9b334a45a8ff 839
bogdanm 0:9b334a45a8ff 840 /**
bogdanm 0:9b334a45a8ff 841 * @brief Enables or disables the Auto Baud Rate.
bogdanm 0:9b334a45a8ff 842 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 843 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 844 * @param NewState: new state of the USARTx auto baud rate.
bogdanm 0:9b334a45a8ff 845 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 846 * @retval None
bogdanm 0:9b334a45a8ff 847 */
bogdanm 0:9b334a45a8ff 848 void USART_AutoBaudRateCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 849 {
bogdanm 0:9b334a45a8ff 850 /* Check the parameters */
bogdanm 0:9b334a45a8ff 851 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 852 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 853
bogdanm 0:9b334a45a8ff 854 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 855 {
bogdanm 0:9b334a45a8ff 856 /* Enable the auto baud rate feature by setting the ABREN bit in the CR2
bogdanm 0:9b334a45a8ff 857 register */
bogdanm 0:9b334a45a8ff 858 USARTx->CR2 |= USART_CR2_ABREN;
bogdanm 0:9b334a45a8ff 859 }
bogdanm 0:9b334a45a8ff 860 else
bogdanm 0:9b334a45a8ff 861 {
bogdanm 0:9b334a45a8ff 862 /* Disable the auto baud rate feature by clearing the ABREN bit in the CR2
bogdanm 0:9b334a45a8ff 863 register */
bogdanm 0:9b334a45a8ff 864 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABREN);
bogdanm 0:9b334a45a8ff 865 }
bogdanm 0:9b334a45a8ff 866 }
bogdanm 0:9b334a45a8ff 867
bogdanm 0:9b334a45a8ff 868 /**
bogdanm 0:9b334a45a8ff 869 * @brief Selects the USART auto baud rate method.
bogdanm 0:9b334a45a8ff 870 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 871 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 872 * @param USART_AutoBaudRate: specifies the selected USART auto baud rate method.
bogdanm 0:9b334a45a8ff 873 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 874 * @arg USART_AutoBaudRate_StartBit: Start Bit duration measurement.
bogdanm 0:9b334a45a8ff 875 * @arg USART_AutoBaudRate_FallingEdge: Falling edge to falling edge measurement.
bogdanm 0:9b334a45a8ff 876 * @arg USART_AutoBaudRate_0x7FFrame: 0x7F frame.
bogdanm 0:9b334a45a8ff 877 * @arg USART_AutoBaudRate_0x55Frame: 0x55 frame.
bogdanm 0:9b334a45a8ff 878 * @note
bogdanm 0:9b334a45a8ff 879 * This function has to be called before calling USART_Cmd() function.
bogdanm 0:9b334a45a8ff 880 * @retval None
bogdanm 0:9b334a45a8ff 881 */
bogdanm 0:9b334a45a8ff 882 void USART_AutoBaudRateConfig(USART_TypeDef* USARTx, uint32_t USART_AutoBaudRate)
bogdanm 0:9b334a45a8ff 883 {
bogdanm 0:9b334a45a8ff 884 /* Check the parameters */
bogdanm 0:9b334a45a8ff 885 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 886 assert_param(IS_USART_AUTOBAUDRATE_MODE(USART_AutoBaudRate));
bogdanm 0:9b334a45a8ff 887
bogdanm 0:9b334a45a8ff 888 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABRMODE);
bogdanm 0:9b334a45a8ff 889 USARTx->CR2 |= USART_AutoBaudRate;
bogdanm 0:9b334a45a8ff 890 }
bogdanm 0:9b334a45a8ff 891
bogdanm 0:9b334a45a8ff 892 /**
bogdanm 0:9b334a45a8ff 893 * @}
bogdanm 0:9b334a45a8ff 894 */
bogdanm 0:9b334a45a8ff 895
bogdanm 0:9b334a45a8ff 896
bogdanm 0:9b334a45a8ff 897 /** @defgroup USART_Group4 Data transfers functions
bogdanm 0:9b334a45a8ff 898 * @brief Data transfers functions
bogdanm 0:9b334a45a8ff 899 *
bogdanm 0:9b334a45a8ff 900 @verbatim
bogdanm 0:9b334a45a8ff 901 ===============================================================================
bogdanm 0:9b334a45a8ff 902 ##### Data transfers functions #####
bogdanm 0:9b334a45a8ff 903 ===============================================================================
bogdanm 0:9b334a45a8ff 904 [..] This subsection provides a set of functions allowing to manage
bogdanm 0:9b334a45a8ff 905 the USART data transfers.
bogdanm 0:9b334a45a8ff 906 [..] During an USART reception, data shifts in least significant bit first
bogdanm 0:9b334a45a8ff 907 through the RX pin. When a transmission is taking place, a write instruction to
bogdanm 0:9b334a45a8ff 908 the USART_TDR register stores the data in the shift register.
bogdanm 0:9b334a45a8ff 909 [..] The read access of the USART_RDR register can be done using
bogdanm 0:9b334a45a8ff 910 the USART_ReceiveData() function and returns the RDR value.
bogdanm 0:9b334a45a8ff 911 Whereas a write access to the USART_TDR can be done using USART_SendData()
bogdanm 0:9b334a45a8ff 912 function and stores the written data into TDR.
bogdanm 0:9b334a45a8ff 913
bogdanm 0:9b334a45a8ff 914 @endverbatim
bogdanm 0:9b334a45a8ff 915 * @{
bogdanm 0:9b334a45a8ff 916 */
bogdanm 0:9b334a45a8ff 917
bogdanm 0:9b334a45a8ff 918 /**
bogdanm 0:9b334a45a8ff 919 * @brief Transmits single data through the USARTx peripheral.
bogdanm 0:9b334a45a8ff 920 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 921 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 922 * @param Data: the data to transmit.
bogdanm 0:9b334a45a8ff 923 * @retval None
bogdanm 0:9b334a45a8ff 924 */
bogdanm 0:9b334a45a8ff 925 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
bogdanm 0:9b334a45a8ff 926 {
bogdanm 0:9b334a45a8ff 927 /* Check the parameters */
bogdanm 0:9b334a45a8ff 928 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 929 assert_param(IS_USART_DATA(Data));
bogdanm 0:9b334a45a8ff 930
bogdanm 0:9b334a45a8ff 931 /* Transmit Data */
bogdanm 0:9b334a45a8ff 932 USARTx->TDR = (Data & (uint16_t)0x01FF);
bogdanm 0:9b334a45a8ff 933 }
bogdanm 0:9b334a45a8ff 934
bogdanm 0:9b334a45a8ff 935 /**
bogdanm 0:9b334a45a8ff 936 * @brief Returns the most recent received data by the USARTx peripheral.
bogdanm 0:9b334a45a8ff 937 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 938 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 939 * @retval The received data.
bogdanm 0:9b334a45a8ff 940 */
bogdanm 0:9b334a45a8ff 941 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
bogdanm 0:9b334a45a8ff 942 {
bogdanm 0:9b334a45a8ff 943 /* Check the parameters */
bogdanm 0:9b334a45a8ff 944 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 945
bogdanm 0:9b334a45a8ff 946 /* Receive Data */
bogdanm 0:9b334a45a8ff 947 return (uint16_t)(USARTx->RDR & (uint16_t)0x01FF);
bogdanm 0:9b334a45a8ff 948 }
bogdanm 0:9b334a45a8ff 949
bogdanm 0:9b334a45a8ff 950 /**
bogdanm 0:9b334a45a8ff 951 * @}
bogdanm 0:9b334a45a8ff 952 */
bogdanm 0:9b334a45a8ff 953
bogdanm 0:9b334a45a8ff 954 /** @defgroup USART_Group5 MultiProcessor Communication functions
bogdanm 0:9b334a45a8ff 955 * @brief Multi-Processor Communication functions
bogdanm 0:9b334a45a8ff 956 *
bogdanm 0:9b334a45a8ff 957 @verbatim
bogdanm 0:9b334a45a8ff 958 ===============================================================================
bogdanm 0:9b334a45a8ff 959 ##### Multi-Processor Communication functions #####
bogdanm 0:9b334a45a8ff 960 ===============================================================================
bogdanm 0:9b334a45a8ff 961 [..] This subsection provides a set of functions allowing to manage the USART
bogdanm 0:9b334a45a8ff 962 multiprocessor communication.
bogdanm 0:9b334a45a8ff 963 [..] For instance one of the USARTs can be the master, its TX output is
bogdanm 0:9b334a45a8ff 964 connected to the RX input of the other USART. The others are slaves,
bogdanm 0:9b334a45a8ff 965 their respective TX outputs are logically ANDed together and connected
bogdanm 0:9b334a45a8ff 966 to the RX input of the master. USART multiprocessor communication is
bogdanm 0:9b334a45a8ff 967 possible through the following procedure:
bogdanm 0:9b334a45a8ff 968 (#) Program the Baud rate, Word length = 9 bits, Stop bits, Parity,
bogdanm 0:9b334a45a8ff 969 Mode transmitter or Mode receiver and hardware flow control values
bogdanm 0:9b334a45a8ff 970 using the USART_Init() function.
bogdanm 0:9b334a45a8ff 971 (#) Configures the USART address using the USART_SetAddress() function.
bogdanm 0:9b334a45a8ff 972 (#) Configures the wake up methode (USART_WakeUp_IdleLine or
bogdanm 0:9b334a45a8ff 973 USART_WakeUp_AddressMark) using USART_WakeUpConfig() function only
bogdanm 0:9b334a45a8ff 974 for the slaves.
bogdanm 0:9b334a45a8ff 975 (#) Enable the USART using the USART_Cmd() function.
bogdanm 0:9b334a45a8ff 976 (#) Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd()
bogdanm 0:9b334a45a8ff 977 function.
bogdanm 0:9b334a45a8ff 978 [..] The USART Slave exit from mute mode when receive the wake up condition.
bogdanm 0:9b334a45a8ff 979
bogdanm 0:9b334a45a8ff 980 @endverbatim
bogdanm 0:9b334a45a8ff 981 * @{
bogdanm 0:9b334a45a8ff 982 */
bogdanm 0:9b334a45a8ff 983
bogdanm 0:9b334a45a8ff 984 /**
bogdanm 0:9b334a45a8ff 985 * @brief Sets the address of the USART node.
bogdanm 0:9b334a45a8ff 986 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 987 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 988 * @param USART_Address: Indicates the address of the USART node.
bogdanm 0:9b334a45a8ff 989 * @retval None
bogdanm 0:9b334a45a8ff 990 */
bogdanm 0:9b334a45a8ff 991 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
bogdanm 0:9b334a45a8ff 992 {
bogdanm 0:9b334a45a8ff 993 /* Check the parameters */
bogdanm 0:9b334a45a8ff 994 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 995
bogdanm 0:9b334a45a8ff 996 /* Clear the USART address */
bogdanm 0:9b334a45a8ff 997 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADD);
bogdanm 0:9b334a45a8ff 998 /* Set the USART address node */
bogdanm 0:9b334a45a8ff 999 USARTx->CR2 |=((uint32_t)USART_Address << (uint32_t)0x18);
bogdanm 0:9b334a45a8ff 1000 }
bogdanm 0:9b334a45a8ff 1001
bogdanm 0:9b334a45a8ff 1002 /**
bogdanm 0:9b334a45a8ff 1003 * @brief Enables or disables the USART's mute mode.
bogdanm 0:9b334a45a8ff 1004 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1005 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1006 * @param NewState: new state of the USART mute mode.
bogdanm 0:9b334a45a8ff 1007 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 1008 * @retval None
bogdanm 0:9b334a45a8ff 1009 */
bogdanm 0:9b334a45a8ff 1010 void USART_MuteModeCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 1011 {
bogdanm 0:9b334a45a8ff 1012 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1013 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1014 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 1015
bogdanm 0:9b334a45a8ff 1016 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 1017 {
bogdanm 0:9b334a45a8ff 1018 /* Enable the USART mute mode by setting the MME bit in the CR1 register */
bogdanm 0:9b334a45a8ff 1019 USARTx->CR1 |= USART_CR1_MME;
bogdanm 0:9b334a45a8ff 1020 }
bogdanm 0:9b334a45a8ff 1021 else
bogdanm 0:9b334a45a8ff 1022 {
bogdanm 0:9b334a45a8ff 1023 /* Disable the USART mute mode by clearing the MME bit in the CR1 register */
bogdanm 0:9b334a45a8ff 1024 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_MME);
bogdanm 0:9b334a45a8ff 1025 }
bogdanm 0:9b334a45a8ff 1026 }
bogdanm 0:9b334a45a8ff 1027
bogdanm 0:9b334a45a8ff 1028 /**
bogdanm 0:9b334a45a8ff 1029 * @brief Selects the USART WakeUp method from mute mode.
bogdanm 0:9b334a45a8ff 1030 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1031 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1032 * @param USART_WakeUp: specifies the USART wakeup method.
bogdanm 0:9b334a45a8ff 1033 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 1034 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
bogdanm 0:9b334a45a8ff 1035 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
bogdanm 0:9b334a45a8ff 1036 * @retval None
bogdanm 0:9b334a45a8ff 1037 */
bogdanm 0:9b334a45a8ff 1038 void USART_MuteModeWakeUpConfig(USART_TypeDef* USARTx, uint32_t USART_WakeUp)
bogdanm 0:9b334a45a8ff 1039 {
bogdanm 0:9b334a45a8ff 1040 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1041 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1042 assert_param(IS_USART_MUTEMODE_WAKEUP(USART_WakeUp));
bogdanm 0:9b334a45a8ff 1043
bogdanm 0:9b334a45a8ff 1044 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_WAKE);
bogdanm 0:9b334a45a8ff 1045 USARTx->CR1 |= USART_WakeUp;
bogdanm 0:9b334a45a8ff 1046 }
bogdanm 0:9b334a45a8ff 1047
bogdanm 0:9b334a45a8ff 1048 /**
bogdanm 0:9b334a45a8ff 1049 * @brief Configure the the USART Address detection length.
bogdanm 0:9b334a45a8ff 1050 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1051 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1052 * @param USART_AddressLength: specifies the USART address length detection.
bogdanm 0:9b334a45a8ff 1053 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 1054 * @arg USART_AddressLength_4b: 4-bit address length detection
bogdanm 0:9b334a45a8ff 1055 * @arg USART_AddressLength_7b: 7-bit address length detection
bogdanm 0:9b334a45a8ff 1056 * @retval None
bogdanm 0:9b334a45a8ff 1057 */
bogdanm 0:9b334a45a8ff 1058 void USART_AddressDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_AddressLength)
bogdanm 0:9b334a45a8ff 1059 {
bogdanm 0:9b334a45a8ff 1060 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1061 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1062 assert_param(IS_USART_ADDRESS_DETECTION(USART_AddressLength));
bogdanm 0:9b334a45a8ff 1063
bogdanm 0:9b334a45a8ff 1064 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADDM7);
bogdanm 0:9b334a45a8ff 1065 USARTx->CR2 |= USART_AddressLength;
bogdanm 0:9b334a45a8ff 1066 }
bogdanm 0:9b334a45a8ff 1067
bogdanm 0:9b334a45a8ff 1068 /**
bogdanm 0:9b334a45a8ff 1069 * @}
bogdanm 0:9b334a45a8ff 1070 */
bogdanm 0:9b334a45a8ff 1071
bogdanm 0:9b334a45a8ff 1072 /** @defgroup USART_Group6 LIN mode functions
bogdanm 0:9b334a45a8ff 1073 * @brief LIN mode functions
bogdanm 0:9b334a45a8ff 1074 *
bogdanm 0:9b334a45a8ff 1075 @verbatim
bogdanm 0:9b334a45a8ff 1076 ===============================================================================
bogdanm 0:9b334a45a8ff 1077 ##### LIN mode functions #####
bogdanm 0:9b334a45a8ff 1078 ===============================================================================
bogdanm 0:9b334a45a8ff 1079 [..] This subsection provides a set of functions allowing to manage the USART
bogdanm 0:9b334a45a8ff 1080 LIN Mode communication.
bogdanm 0:9b334a45a8ff 1081 [..] In LIN mode, 8-bit data format with 1 stop bit is required in accordance
bogdanm 0:9b334a45a8ff 1082 with the LIN standard.
bogdanm 0:9b334a45a8ff 1083 [..] Only this LIN Feature is supported by the USART IP:
bogdanm 0:9b334a45a8ff 1084 (+) LIN Master Synchronous Break send capability and LIN slave break
bogdanm 0:9b334a45a8ff 1085 detection capability : 13-bit break generation and 10/11 bit break
bogdanm 0:9b334a45a8ff 1086 detection.
bogdanm 0:9b334a45a8ff 1087 [..] USART LIN Master transmitter communication is possible through the
bogdanm 0:9b334a45a8ff 1088 following procedure:
bogdanm 0:9b334a45a8ff 1089 (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
bogdanm 0:9b334a45a8ff 1090 Mode transmitter or Mode receiver and hardware flow control values
bogdanm 0:9b334a45a8ff 1091 using the USART_Init() function.
bogdanm 0:9b334a45a8ff 1092 (#) Enable the LIN mode using the USART_LINCmd() function.
bogdanm 0:9b334a45a8ff 1093 (#) Enable the USART using the USART_Cmd() function.
bogdanm 0:9b334a45a8ff 1094 (#) Send the break character using USART_SendBreak() function.
bogdanm 0:9b334a45a8ff 1095 [..] USART LIN Master receiver communication is possible through the
bogdanm 0:9b334a45a8ff 1096 following procedure:
bogdanm 0:9b334a45a8ff 1097 (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
bogdanm 0:9b334a45a8ff 1098 Mode transmitter or Mode receiver and hardware flow control values
bogdanm 0:9b334a45a8ff 1099 using the USART_Init() function.
bogdanm 0:9b334a45a8ff 1100 (#) Configures the break detection length
bogdanm 0:9b334a45a8ff 1101 using the USART_LINBreakDetectLengthConfig() function.
bogdanm 0:9b334a45a8ff 1102 (#) Enable the LIN mode using the USART_LINCmd() function.
bogdanm 0:9b334a45a8ff 1103 (#) Enable the USART using the USART_Cmd() function.
bogdanm 0:9b334a45a8ff 1104 [..]
bogdanm 0:9b334a45a8ff 1105 (@) In LIN mode, the following bits must be kept cleared:
bogdanm 0:9b334a45a8ff 1106 (+@) CLKEN in the USART_CR2 register.
bogdanm 0:9b334a45a8ff 1107 (+@) STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register.
bogdanm 0:9b334a45a8ff 1108
bogdanm 0:9b334a45a8ff 1109 @endverbatim
bogdanm 0:9b334a45a8ff 1110 * @{
bogdanm 0:9b334a45a8ff 1111 */
bogdanm 0:9b334a45a8ff 1112
bogdanm 0:9b334a45a8ff 1113 /**
bogdanm 0:9b334a45a8ff 1114 * @brief Sets the USART LIN Break detection length.
bogdanm 0:9b334a45a8ff 1115 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1116 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1117 * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
bogdanm 0:9b334a45a8ff 1118 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 1119 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
bogdanm 0:9b334a45a8ff 1120 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
bogdanm 0:9b334a45a8ff 1121 * @retval None
bogdanm 0:9b334a45a8ff 1122 */
bogdanm 0:9b334a45a8ff 1123 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint32_t USART_LINBreakDetectLength)
bogdanm 0:9b334a45a8ff 1124 {
bogdanm 0:9b334a45a8ff 1125 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1126 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1127 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
bogdanm 0:9b334a45a8ff 1128
bogdanm 0:9b334a45a8ff 1129 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_LBDL);
bogdanm 0:9b334a45a8ff 1130 USARTx->CR2 |= USART_LINBreakDetectLength;
bogdanm 0:9b334a45a8ff 1131 }
bogdanm 0:9b334a45a8ff 1132
bogdanm 0:9b334a45a8ff 1133 /**
bogdanm 0:9b334a45a8ff 1134 * @brief Enables or disables the USART's LIN mode.
bogdanm 0:9b334a45a8ff 1135 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1136 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1137 * @param NewState: new state of the USART LIN mode.
bogdanm 0:9b334a45a8ff 1138 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 1139 * @retval None
bogdanm 0:9b334a45a8ff 1140 */
bogdanm 0:9b334a45a8ff 1141 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 1142 {
bogdanm 0:9b334a45a8ff 1143 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1144 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1145 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 1146
bogdanm 0:9b334a45a8ff 1147 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 1148 {
bogdanm 0:9b334a45a8ff 1149 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
bogdanm 0:9b334a45a8ff 1150 USARTx->CR2 |= USART_CR2_LINEN;
bogdanm 0:9b334a45a8ff 1151 }
bogdanm 0:9b334a45a8ff 1152 else
bogdanm 0:9b334a45a8ff 1153 {
bogdanm 0:9b334a45a8ff 1154 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
bogdanm 0:9b334a45a8ff 1155 USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_LINEN);
bogdanm 0:9b334a45a8ff 1156 }
bogdanm 0:9b334a45a8ff 1157 }
bogdanm 0:9b334a45a8ff 1158
bogdanm 0:9b334a45a8ff 1159 /**
bogdanm 0:9b334a45a8ff 1160 * @}
bogdanm 0:9b334a45a8ff 1161 */
bogdanm 0:9b334a45a8ff 1162
bogdanm 0:9b334a45a8ff 1163 /** @defgroup USART_Group7 Halfduplex mode function
bogdanm 0:9b334a45a8ff 1164 * @brief Half-duplex mode function
bogdanm 0:9b334a45a8ff 1165 *
bogdanm 0:9b334a45a8ff 1166 @verbatim
bogdanm 0:9b334a45a8ff 1167 ===============================================================================
bogdanm 0:9b334a45a8ff 1168 ##### Half-duplex mode function #####
bogdanm 0:9b334a45a8ff 1169 ===============================================================================
bogdanm 0:9b334a45a8ff 1170 [..] This subsection provides a set of functions allowing to manage the USART
bogdanm 0:9b334a45a8ff 1171 Half-duplex communication.
bogdanm 0:9b334a45a8ff 1172 [..] The USART can be configured to follow a single-wire half-duplex protocol
bogdanm 0:9b334a45a8ff 1173 where the TX and RX lines are internally connected.
bogdanm 0:9b334a45a8ff 1174 [..] USART Half duplex communication is possible through the following procedure:
bogdanm 0:9b334a45a8ff 1175 (#) Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter
bogdanm 0:9b334a45a8ff 1176 or Mode receiver and hardware flow control values using the USART_Init()
bogdanm 0:9b334a45a8ff 1177 function.
bogdanm 0:9b334a45a8ff 1178 (#) Configures the USART address using the USART_SetAddress() function.
bogdanm 0:9b334a45a8ff 1179 (#) Enable the half duplex mode using USART_HalfDuplexCmd() function.
bogdanm 0:9b334a45a8ff 1180 (#) Enable the USART using the USART_Cmd() function.
bogdanm 0:9b334a45a8ff 1181 [..]
bogdanm 0:9b334a45a8ff 1182 (@) The RX pin is no longer used.
bogdanm 0:9b334a45a8ff 1183 (@) In Half-duplex mode the following bits must be kept cleared:
bogdanm 0:9b334a45a8ff 1184 (+@) LINEN and CLKEN bits in the USART_CR2 register.
bogdanm 0:9b334a45a8ff 1185 (+@) SCEN and IREN bits in the USART_CR3 register.
bogdanm 0:9b334a45a8ff 1186
bogdanm 0:9b334a45a8ff 1187 @endverbatim
bogdanm 0:9b334a45a8ff 1188 * @{
bogdanm 0:9b334a45a8ff 1189 */
bogdanm 0:9b334a45a8ff 1190
bogdanm 0:9b334a45a8ff 1191 /**
bogdanm 0:9b334a45a8ff 1192 * @brief Enables or disables the USART's Half Duplex communication.
bogdanm 0:9b334a45a8ff 1193 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1194 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1195 * @param NewState: new state of the USART Communication.
bogdanm 0:9b334a45a8ff 1196 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 1197 * @retval None
bogdanm 0:9b334a45a8ff 1198 */
bogdanm 0:9b334a45a8ff 1199 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 1200 {
bogdanm 0:9b334a45a8ff 1201 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1202 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1203 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 1204
bogdanm 0:9b334a45a8ff 1205 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 1206 {
bogdanm 0:9b334a45a8ff 1207 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
bogdanm 0:9b334a45a8ff 1208 USARTx->CR3 |= USART_CR3_HDSEL;
bogdanm 0:9b334a45a8ff 1209 }
bogdanm 0:9b334a45a8ff 1210 else
bogdanm 0:9b334a45a8ff 1211 {
bogdanm 0:9b334a45a8ff 1212 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
bogdanm 0:9b334a45a8ff 1213 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_HDSEL);
bogdanm 0:9b334a45a8ff 1214 }
bogdanm 0:9b334a45a8ff 1215 }
bogdanm 0:9b334a45a8ff 1216
bogdanm 0:9b334a45a8ff 1217 /**
bogdanm 0:9b334a45a8ff 1218 * @}
bogdanm 0:9b334a45a8ff 1219 */
bogdanm 0:9b334a45a8ff 1220
bogdanm 0:9b334a45a8ff 1221
bogdanm 0:9b334a45a8ff 1222 /** @defgroup USART_Group8 Smartcard mode functions
bogdanm 0:9b334a45a8ff 1223 * @brief Smartcard mode functions
bogdanm 0:9b334a45a8ff 1224 *
bogdanm 0:9b334a45a8ff 1225 @verbatim
bogdanm 0:9b334a45a8ff 1226 ===============================================================================
bogdanm 0:9b334a45a8ff 1227 ##### Smartcard mode functions #####
bogdanm 0:9b334a45a8ff 1228 ===============================================================================
bogdanm 0:9b334a45a8ff 1229 [..] This subsection provides a set of functions allowing to manage the USART
bogdanm 0:9b334a45a8ff 1230 Smartcard communication.
bogdanm 0:9b334a45a8ff 1231 [..] The Smartcard interface is designed to support asynchronous protocol
bogdanm 0:9b334a45a8ff 1232 Smartcards as defined in the ISO 7816-3 standard. The USART can provide
bogdanm 0:9b334a45a8ff 1233 a clock to the smartcard through the SCLK output. In smartcard mode,
bogdanm 0:9b334a45a8ff 1234 SCLK is not associated to the communication but is simply derived from
bogdanm 0:9b334a45a8ff 1235 the internal peripheral input clock through a 5-bit prescaler.
bogdanm 0:9b334a45a8ff 1236 [..] Smartcard communication is possible through the following procedure:
bogdanm 0:9b334a45a8ff 1237 (#) Configures the Smartcard Prsecaler using the USART_SetPrescaler()
bogdanm 0:9b334a45a8ff 1238 function.
bogdanm 0:9b334a45a8ff 1239 (#) Configures the Smartcard Guard Time using the USART_SetGuardTime()
bogdanm 0:9b334a45a8ff 1240 function.
bogdanm 0:9b334a45a8ff 1241 (#) Program the USART clock using the USART_ClockInit() function as following:
bogdanm 0:9b334a45a8ff 1242 (++) USART Clock enabled.
bogdanm 0:9b334a45a8ff 1243 (++) USART CPOL Low.
bogdanm 0:9b334a45a8ff 1244 (++) USART CPHA on first edge.
bogdanm 0:9b334a45a8ff 1245 (++) USART Last Bit Clock Enabled.
bogdanm 0:9b334a45a8ff 1246 (#) Program the Smartcard interface using the USART_Init() function as
bogdanm 0:9b334a45a8ff 1247 following:
bogdanm 0:9b334a45a8ff 1248 (++) Word Length = 9 Bits.
bogdanm 0:9b334a45a8ff 1249 (++) 1.5 Stop Bit.
bogdanm 0:9b334a45a8ff 1250 (++) Even parity.
bogdanm 0:9b334a45a8ff 1251 (++) BaudRate = 12096 baud.
bogdanm 0:9b334a45a8ff 1252 (++) Hardware flow control disabled (RTS and CTS signals).
bogdanm 0:9b334a45a8ff 1253 (++) Tx and Rx enabled
bogdanm 0:9b334a45a8ff 1254 (#) Optionally you can enable the parity error interrupt using
bogdanm 0:9b334a45a8ff 1255 the USART_ITConfig() function.
bogdanm 0:9b334a45a8ff 1256 (#) Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
bogdanm 0:9b334a45a8ff 1257 (#) Enable the Smartcard interface using the USART_SmartCardCmd() function.
bogdanm 0:9b334a45a8ff 1258 (#) Enable the USART using the USART_Cmd() function.
bogdanm 0:9b334a45a8ff 1259 [..]
bogdanm 0:9b334a45a8ff 1260 Please refer to the ISO 7816-3 specification for more details.
bogdanm 0:9b334a45a8ff 1261 [..]
bogdanm 0:9b334a45a8ff 1262 (@) It is also possible to choose 0.5 stop bit for receiving but it is
bogdanm 0:9b334a45a8ff 1263 recommended to use 1.5 stop bits for both transmitting and receiving
bogdanm 0:9b334a45a8ff 1264 to avoid switching between the two configurations.
bogdanm 0:9b334a45a8ff 1265 (@) In smartcard mode, the following bits must be kept cleared:
bogdanm 0:9b334a45a8ff 1266 (+@) LINEN bit in the USART_CR2 register.
bogdanm 0:9b334a45a8ff 1267 (+@) HDSEL and IREN bits in the USART_CR3 register.
bogdanm 0:9b334a45a8ff 1268
bogdanm 0:9b334a45a8ff 1269 @endverbatim
bogdanm 0:9b334a45a8ff 1270 * @{
bogdanm 0:9b334a45a8ff 1271 */
bogdanm 0:9b334a45a8ff 1272
bogdanm 0:9b334a45a8ff 1273 /**
bogdanm 0:9b334a45a8ff 1274 * @brief Sets the specified USART guard time.
bogdanm 0:9b334a45a8ff 1275 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1276 * following values: USART1 or USART2 or USART3.
bogdanm 0:9b334a45a8ff 1277 * @param USART_GuardTime: specifies the guard time.
bogdanm 0:9b334a45a8ff 1278 * @retval None
bogdanm 0:9b334a45a8ff 1279 */
bogdanm 0:9b334a45a8ff 1280 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
bogdanm 0:9b334a45a8ff 1281 {
bogdanm 0:9b334a45a8ff 1282 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1283 assert_param(IS_USART_123_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1284
bogdanm 0:9b334a45a8ff 1285 /* Clear the USART Guard time */
bogdanm 0:9b334a45a8ff 1286 USARTx->GTPR &= USART_GTPR_PSC;
bogdanm 0:9b334a45a8ff 1287 /* Set the USART guard time */
bogdanm 0:9b334a45a8ff 1288 USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
bogdanm 0:9b334a45a8ff 1289 }
bogdanm 0:9b334a45a8ff 1290
bogdanm 0:9b334a45a8ff 1291 /**
bogdanm 0:9b334a45a8ff 1292 * @brief Enables or disables the USART's Smart Card mode.
bogdanm 0:9b334a45a8ff 1293 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1294 * following values: USART1 or USART2 or USART3.
bogdanm 0:9b334a45a8ff 1295 * @param NewState: new state of the Smart Card mode.
bogdanm 0:9b334a45a8ff 1296 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 1297 * @retval None
bogdanm 0:9b334a45a8ff 1298 */
bogdanm 0:9b334a45a8ff 1299 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 1300 {
bogdanm 0:9b334a45a8ff 1301 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1302 assert_param(IS_USART_123_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1303 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 1304 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 1305 {
bogdanm 0:9b334a45a8ff 1306 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
bogdanm 0:9b334a45a8ff 1307 USARTx->CR3 |= USART_CR3_SCEN;
bogdanm 0:9b334a45a8ff 1308 }
bogdanm 0:9b334a45a8ff 1309 else
bogdanm 0:9b334a45a8ff 1310 {
bogdanm 0:9b334a45a8ff 1311 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
bogdanm 0:9b334a45a8ff 1312 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_SCEN);
bogdanm 0:9b334a45a8ff 1313 }
bogdanm 0:9b334a45a8ff 1314 }
bogdanm 0:9b334a45a8ff 1315
bogdanm 0:9b334a45a8ff 1316 /**
bogdanm 0:9b334a45a8ff 1317 * @brief Enables or disables NACK transmission.
bogdanm 0:9b334a45a8ff 1318 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1319 * following values: USART1 or USART2 or USART3.
bogdanm 0:9b334a45a8ff 1320 * @param NewState: new state of the NACK transmission.
bogdanm 0:9b334a45a8ff 1321 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 1322 * @retval None
bogdanm 0:9b334a45a8ff 1323 */
bogdanm 0:9b334a45a8ff 1324 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 1325 {
bogdanm 0:9b334a45a8ff 1326 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1327 assert_param(IS_USART_123_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1328 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 1329 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 1330 {
bogdanm 0:9b334a45a8ff 1331 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
bogdanm 0:9b334a45a8ff 1332 USARTx->CR3 |= USART_CR3_NACK;
bogdanm 0:9b334a45a8ff 1333 }
bogdanm 0:9b334a45a8ff 1334 else
bogdanm 0:9b334a45a8ff 1335 {
bogdanm 0:9b334a45a8ff 1336 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
bogdanm 0:9b334a45a8ff 1337 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_NACK);
bogdanm 0:9b334a45a8ff 1338 }
bogdanm 0:9b334a45a8ff 1339 }
bogdanm 0:9b334a45a8ff 1340
bogdanm 0:9b334a45a8ff 1341 /**
bogdanm 0:9b334a45a8ff 1342 * @brief Sets the Smart Card number of retries in transmit and receive.
bogdanm 0:9b334a45a8ff 1343 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1344 * following values: USART1 or USART2 or USART3.
bogdanm 0:9b334a45a8ff 1345 * @param USART_AutoCount: specifies the Smart Card auto retry count.
bogdanm 0:9b334a45a8ff 1346 * @retval None
bogdanm 0:9b334a45a8ff 1347 */
bogdanm 0:9b334a45a8ff 1348 void USART_SetAutoRetryCount(USART_TypeDef* USARTx, uint8_t USART_AutoCount)
bogdanm 0:9b334a45a8ff 1349 {
bogdanm 0:9b334a45a8ff 1350 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1351 assert_param(IS_USART_123_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1352 assert_param(IS_USART_AUTO_RETRY_COUNTER(USART_AutoCount));
bogdanm 0:9b334a45a8ff 1353 /* Clear the USART auto retry count */
bogdanm 0:9b334a45a8ff 1354 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_SCARCNT);
bogdanm 0:9b334a45a8ff 1355 /* Set the USART auto retry count*/
bogdanm 0:9b334a45a8ff 1356 USARTx->CR3 |= (uint32_t)((uint32_t)USART_AutoCount << 0x11);
bogdanm 0:9b334a45a8ff 1357 }
bogdanm 0:9b334a45a8ff 1358
bogdanm 0:9b334a45a8ff 1359 /**
bogdanm 0:9b334a45a8ff 1360 * @brief Sets the Smart Card Block length.
bogdanm 0:9b334a45a8ff 1361 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1362 * following values: USART1 or USART2 or USART3.
bogdanm 0:9b334a45a8ff 1363 * @param USART_BlockLength: specifies the Smart Card block length.
bogdanm 0:9b334a45a8ff 1364 * @retval None
bogdanm 0:9b334a45a8ff 1365 */
bogdanm 0:9b334a45a8ff 1366 void USART_SetBlockLength(USART_TypeDef* USARTx, uint8_t USART_BlockLength)
bogdanm 0:9b334a45a8ff 1367 {
bogdanm 0:9b334a45a8ff 1368 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1369 assert_param(IS_USART_123_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1370
bogdanm 0:9b334a45a8ff 1371 /* Clear the Smart card block length */
bogdanm 0:9b334a45a8ff 1372 USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_BLEN);
bogdanm 0:9b334a45a8ff 1373 /* Set the Smart Card block length */
bogdanm 0:9b334a45a8ff 1374 USARTx->RTOR |= (uint32_t)((uint32_t)USART_BlockLength << 0x18);
bogdanm 0:9b334a45a8ff 1375 }
bogdanm 0:9b334a45a8ff 1376
bogdanm 0:9b334a45a8ff 1377 /**
bogdanm 0:9b334a45a8ff 1378 * @}
bogdanm 0:9b334a45a8ff 1379 */
bogdanm 0:9b334a45a8ff 1380
bogdanm 0:9b334a45a8ff 1381 /** @defgroup USART_Group9 IrDA mode functions
bogdanm 0:9b334a45a8ff 1382 * @brief IrDA mode functions
bogdanm 0:9b334a45a8ff 1383 *
bogdanm 0:9b334a45a8ff 1384 @verbatim
bogdanm 0:9b334a45a8ff 1385 ===============================================================================
bogdanm 0:9b334a45a8ff 1386 ##### IrDA mode functions #####
bogdanm 0:9b334a45a8ff 1387 ===============================================================================
bogdanm 0:9b334a45a8ff 1388 [..] This subsection provides a set of functions allowing to manage the USART
bogdanm 0:9b334a45a8ff 1389 IrDA communication.
bogdanm 0:9b334a45a8ff 1390 [..] IrDA is a half duplex communication protocol. If the Transmitter is busy,
bogdanm 0:9b334a45a8ff 1391 any data on the IrDA receive line will be ignored by the IrDA decoder
bogdanm 0:9b334a45a8ff 1392 and if the Receiver is busy, data on the TX from the USART to IrDA will
bogdanm 0:9b334a45a8ff 1393 not be encoded by IrDA. While receiving data, transmission should be
bogdanm 0:9b334a45a8ff 1394 avoided as the data to be transmitted could be corrupted.
bogdanm 0:9b334a45a8ff 1395 [..] IrDA communication is possible through the following procedure:
bogdanm 0:9b334a45a8ff 1396 (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity,
bogdanm 0:9b334a45a8ff 1397 Transmitter/Receiver modes and hardware flow control values using
bogdanm 0:9b334a45a8ff 1398 the USART_Init() function.
bogdanm 0:9b334a45a8ff 1399 (#) Configures the IrDA pulse width by configuring the prescaler using
bogdanm 0:9b334a45a8ff 1400 the USART_SetPrescaler() function.
bogdanm 0:9b334a45a8ff 1401 (#) Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal
bogdanm 0:9b334a45a8ff 1402 mode using the USART_IrDAConfig() function.
bogdanm 0:9b334a45a8ff 1403 (#) Enable the IrDA using the USART_IrDACmd() function.
bogdanm 0:9b334a45a8ff 1404 (#) Enable the USART using the USART_Cmd() function.
bogdanm 0:9b334a45a8ff 1405 [..]
bogdanm 0:9b334a45a8ff 1406 (@) A pulse of width less than two and greater than one PSC period(s) may or
bogdanm 0:9b334a45a8ff 1407 may not be rejected.
bogdanm 0:9b334a45a8ff 1408 (@) The receiver set up time should be managed by software. The IrDA physical
bogdanm 0:9b334a45a8ff 1409 layer specification specifies a minimum of 10 ms delay between
bogdanm 0:9b334a45a8ff 1410 transmission and reception (IrDA is a half duplex protocol).
bogdanm 0:9b334a45a8ff 1411 (@) In IrDA mode, the following bits must be kept cleared:
bogdanm 0:9b334a45a8ff 1412 (+@) LINEN, STOP and CLKEN bits in the USART_CR2 register.
bogdanm 0:9b334a45a8ff 1413 (+@) SCEN and HDSEL bits in the USART_CR3 register.
bogdanm 0:9b334a45a8ff 1414
bogdanm 0:9b334a45a8ff 1415 @endverbatim
bogdanm 0:9b334a45a8ff 1416 * @{
bogdanm 0:9b334a45a8ff 1417 */
bogdanm 0:9b334a45a8ff 1418
bogdanm 0:9b334a45a8ff 1419 /**
bogdanm 0:9b334a45a8ff 1420 * @brief Configures the USART's IrDA interface.
bogdanm 0:9b334a45a8ff 1421 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1422 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1423 * @param USART_IrDAMode: specifies the IrDA mode.
bogdanm 0:9b334a45a8ff 1424 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 1425 * @arg USART_IrDAMode_LowPower
bogdanm 0:9b334a45a8ff 1426 * @arg USART_IrDAMode_Normal
bogdanm 0:9b334a45a8ff 1427 * @retval None
bogdanm 0:9b334a45a8ff 1428 */
bogdanm 0:9b334a45a8ff 1429 void USART_IrDAConfig(USART_TypeDef* USARTx, uint32_t USART_IrDAMode)
bogdanm 0:9b334a45a8ff 1430 {
bogdanm 0:9b334a45a8ff 1431 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1432 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1433 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
bogdanm 0:9b334a45a8ff 1434
bogdanm 0:9b334a45a8ff 1435 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_IRLP);
bogdanm 0:9b334a45a8ff 1436 USARTx->CR3 |= USART_IrDAMode;
bogdanm 0:9b334a45a8ff 1437 }
bogdanm 0:9b334a45a8ff 1438
bogdanm 0:9b334a45a8ff 1439 /**
bogdanm 0:9b334a45a8ff 1440 * @brief Enables or disables the USART's IrDA interface.
bogdanm 0:9b334a45a8ff 1441 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1442 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1443 * @param NewState: new state of the IrDA mode.
bogdanm 0:9b334a45a8ff 1444 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 1445 * @retval None
bogdanm 0:9b334a45a8ff 1446 */
bogdanm 0:9b334a45a8ff 1447 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 1448 {
bogdanm 0:9b334a45a8ff 1449 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1450 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1451 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 1452
bogdanm 0:9b334a45a8ff 1453 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 1454 {
bogdanm 0:9b334a45a8ff 1455 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
bogdanm 0:9b334a45a8ff 1456 USARTx->CR3 |= USART_CR3_IREN;
bogdanm 0:9b334a45a8ff 1457 }
bogdanm 0:9b334a45a8ff 1458 else
bogdanm 0:9b334a45a8ff 1459 {
bogdanm 0:9b334a45a8ff 1460 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
bogdanm 0:9b334a45a8ff 1461 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_IREN);
bogdanm 0:9b334a45a8ff 1462 }
bogdanm 0:9b334a45a8ff 1463 }
bogdanm 0:9b334a45a8ff 1464 /**
bogdanm 0:9b334a45a8ff 1465 * @}
bogdanm 0:9b334a45a8ff 1466 */
bogdanm 0:9b334a45a8ff 1467
bogdanm 0:9b334a45a8ff 1468 /** @defgroup USART_Group10 RS485 mode function
bogdanm 0:9b334a45a8ff 1469 * @brief RS485 mode function
bogdanm 0:9b334a45a8ff 1470 *
bogdanm 0:9b334a45a8ff 1471 @verbatim
bogdanm 0:9b334a45a8ff 1472 ===============================================================================
bogdanm 0:9b334a45a8ff 1473 ##### RS485 mode functions #####
bogdanm 0:9b334a45a8ff 1474 ===============================================================================
bogdanm 0:9b334a45a8ff 1475 [..] This subsection provides a set of functions allowing to manage the USART
bogdanm 0:9b334a45a8ff 1476 RS485 flow control.
bogdanm 0:9b334a45a8ff 1477 [..] RS485 flow control (Driver enable feature) handling is possible through
bogdanm 0:9b334a45a8ff 1478 the following procedure:
bogdanm 0:9b334a45a8ff 1479 (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity,
bogdanm 0:9b334a45a8ff 1480 Transmitter/Receiver modes and hardware flow control values using
bogdanm 0:9b334a45a8ff 1481 the USART_Init() function.
bogdanm 0:9b334a45a8ff 1482 (#) Enable the Driver Enable using the USART_DECmd() function.
bogdanm 0:9b334a45a8ff 1483 (#) Configures the Driver Enable polarity using the USART_DEPolarityConfig()
bogdanm 0:9b334a45a8ff 1484 function.
bogdanm 0:9b334a45a8ff 1485 (#) Configures the Driver Enable assertion time using USART_SetDEAssertionTime()
bogdanm 0:9b334a45a8ff 1486 function and deassertion time using the USART_SetDEDeassertionTime()
bogdanm 0:9b334a45a8ff 1487 function.
bogdanm 0:9b334a45a8ff 1488 (#) Enable the USART using the USART_Cmd() function.
bogdanm 0:9b334a45a8ff 1489 [..]
bogdanm 0:9b334a45a8ff 1490 (@) The assertion and dessertion times are expressed in sample time units (1/8 or
bogdanm 0:9b334a45a8ff 1491 1/16 bit time, depending on the oversampling rate).
bogdanm 0:9b334a45a8ff 1492
bogdanm 0:9b334a45a8ff 1493 @endverbatim
bogdanm 0:9b334a45a8ff 1494 * @{
bogdanm 0:9b334a45a8ff 1495 */
bogdanm 0:9b334a45a8ff 1496
bogdanm 0:9b334a45a8ff 1497 /**
bogdanm 0:9b334a45a8ff 1498 * @brief Enables or disables the USART's DE functionality.
bogdanm 0:9b334a45a8ff 1499 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1500 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1501 * @param NewState: new state of the driver enable mode.
bogdanm 0:9b334a45a8ff 1502 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 1503 * @retval None
bogdanm 0:9b334a45a8ff 1504 */
bogdanm 0:9b334a45a8ff 1505 void USART_DECmd(USART_TypeDef* USARTx, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 1506 {
bogdanm 0:9b334a45a8ff 1507 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1508 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1509 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 1510 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 1511 {
bogdanm 0:9b334a45a8ff 1512 /* Enable the DE functionality by setting the DEM bit in the CR3 register */
bogdanm 0:9b334a45a8ff 1513 USARTx->CR3 |= USART_CR3_DEM;
bogdanm 0:9b334a45a8ff 1514 }
bogdanm 0:9b334a45a8ff 1515 else
bogdanm 0:9b334a45a8ff 1516 {
bogdanm 0:9b334a45a8ff 1517 /* Disable the DE functionality by clearing the DEM bit in the CR3 register */
bogdanm 0:9b334a45a8ff 1518 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DEM);
bogdanm 0:9b334a45a8ff 1519 }
bogdanm 0:9b334a45a8ff 1520 }
bogdanm 0:9b334a45a8ff 1521
bogdanm 0:9b334a45a8ff 1522 /**
bogdanm 0:9b334a45a8ff 1523 * @brief Configures the USART's DE polarity
bogdanm 0:9b334a45a8ff 1524 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1525 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1526 * @param USART_DEPolarity: specifies the DE polarity.
bogdanm 0:9b334a45a8ff 1527 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 1528 * @arg USART_DEPolarity_Low
bogdanm 0:9b334a45a8ff 1529 * @arg USART_DEPolarity_High
bogdanm 0:9b334a45a8ff 1530 * @retval None
bogdanm 0:9b334a45a8ff 1531 */
bogdanm 0:9b334a45a8ff 1532 void USART_DEPolarityConfig(USART_TypeDef* USARTx, uint32_t USART_DEPolarity)
bogdanm 0:9b334a45a8ff 1533 {
bogdanm 0:9b334a45a8ff 1534 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1535 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1536 assert_param(IS_USART_DE_POLARITY(USART_DEPolarity));
bogdanm 0:9b334a45a8ff 1537
bogdanm 0:9b334a45a8ff 1538 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DEP);
bogdanm 0:9b334a45a8ff 1539 USARTx->CR3 |= USART_DEPolarity;
bogdanm 0:9b334a45a8ff 1540 }
bogdanm 0:9b334a45a8ff 1541
bogdanm 0:9b334a45a8ff 1542 /**
bogdanm 0:9b334a45a8ff 1543 * @brief Sets the specified RS485 DE assertion time
bogdanm 0:9b334a45a8ff 1544 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1545 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1546 * @param USART_AssertionTime: specifies the time between the activation of the DE
bogdanm 0:9b334a45a8ff 1547 * signal and the beginning of the start bit
bogdanm 0:9b334a45a8ff 1548 * @retval None
bogdanm 0:9b334a45a8ff 1549 */
bogdanm 0:9b334a45a8ff 1550 void USART_SetDEAssertionTime(USART_TypeDef* USARTx, uint32_t USART_DEAssertionTime)
bogdanm 0:9b334a45a8ff 1551 {
bogdanm 0:9b334a45a8ff 1552 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1553 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1554 assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEAssertionTime));
bogdanm 0:9b334a45a8ff 1555
bogdanm 0:9b334a45a8ff 1556 /* Clear the DE assertion time */
bogdanm 0:9b334a45a8ff 1557 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_DEAT);
bogdanm 0:9b334a45a8ff 1558 /* Set the new value for the DE assertion time */
bogdanm 0:9b334a45a8ff 1559 USARTx->CR1 |=((uint32_t)USART_DEAssertionTime << (uint32_t)0x15);
bogdanm 0:9b334a45a8ff 1560 }
bogdanm 0:9b334a45a8ff 1561
bogdanm 0:9b334a45a8ff 1562 /**
bogdanm 0:9b334a45a8ff 1563 * @brief Sets the specified RS485 DE deassertion time
bogdanm 0:9b334a45a8ff 1564 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1565 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1566 * @param USART_DeassertionTime: specifies the time between the middle of the last
bogdanm 0:9b334a45a8ff 1567 * stop bit in a transmitted message and the de-activation of the DE signal
bogdanm 0:9b334a45a8ff 1568 * @retval None
bogdanm 0:9b334a45a8ff 1569 */
bogdanm 0:9b334a45a8ff 1570 void USART_SetDEDeassertionTime(USART_TypeDef* USARTx, uint32_t USART_DEDeassertionTime)
bogdanm 0:9b334a45a8ff 1571 {
bogdanm 0:9b334a45a8ff 1572 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1573 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1574 assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEDeassertionTime));
bogdanm 0:9b334a45a8ff 1575
bogdanm 0:9b334a45a8ff 1576 /* Clear the DE deassertion time */
bogdanm 0:9b334a45a8ff 1577 USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_DEDT);
bogdanm 0:9b334a45a8ff 1578 /* Set the new value for the DE deassertion time */
bogdanm 0:9b334a45a8ff 1579 USARTx->CR1 |=((uint32_t)USART_DEDeassertionTime << (uint32_t)0x10);
bogdanm 0:9b334a45a8ff 1580 }
bogdanm 0:9b334a45a8ff 1581
bogdanm 0:9b334a45a8ff 1582 /**
bogdanm 0:9b334a45a8ff 1583 * @}
bogdanm 0:9b334a45a8ff 1584 */
bogdanm 0:9b334a45a8ff 1585
bogdanm 0:9b334a45a8ff 1586 /** @defgroup USART_Group11 DMA transfers management functions
bogdanm 0:9b334a45a8ff 1587 * @brief DMA transfers management functions
bogdanm 0:9b334a45a8ff 1588 *
bogdanm 0:9b334a45a8ff 1589 @verbatim
bogdanm 0:9b334a45a8ff 1590 ===============================================================================
bogdanm 0:9b334a45a8ff 1591 ##### DMA transfers management functions #####
bogdanm 0:9b334a45a8ff 1592 ===============================================================================
bogdanm 0:9b334a45a8ff 1593 [..] This section provides two functions that can be used only in DMA mode.
bogdanm 0:9b334a45a8ff 1594 [..] In DMA Mode, the USART communication can be managed by 2 DMA Channel
bogdanm 0:9b334a45a8ff 1595 requests:
bogdanm 0:9b334a45a8ff 1596 (#) USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request.
bogdanm 0:9b334a45a8ff 1597 (#) USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request.
bogdanm 0:9b334a45a8ff 1598 [..] In this Mode it is advised to use the following function:
bogdanm 0:9b334a45a8ff 1599 (+) void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq,
bogdanm 0:9b334a45a8ff 1600 FunctionalState NewState).
bogdanm 0:9b334a45a8ff 1601 @endverbatim
bogdanm 0:9b334a45a8ff 1602 * @{
bogdanm 0:9b334a45a8ff 1603 */
bogdanm 0:9b334a45a8ff 1604
bogdanm 0:9b334a45a8ff 1605 /**
bogdanm 0:9b334a45a8ff 1606 * @brief Enables or disables the USART's DMA interface.
bogdanm 0:9b334a45a8ff 1607 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1608 * following values: USART1 or USART2 or USART3 or UART4.
bogdanm 0:9b334a45a8ff 1609 * @param USART_DMAReq: specifies the DMA request.
bogdanm 0:9b334a45a8ff 1610 * This parameter can be any combination of the following values:
bogdanm 0:9b334a45a8ff 1611 * @arg USART_DMAReq_Tx: USART DMA transmit request
bogdanm 0:9b334a45a8ff 1612 * @arg USART_DMAReq_Rx: USART DMA receive request
bogdanm 0:9b334a45a8ff 1613 * @param NewState: new state of the DMA Request sources.
bogdanm 0:9b334a45a8ff 1614 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 1615 * @retval None
bogdanm 0:9b334a45a8ff 1616 */
bogdanm 0:9b334a45a8ff 1617 void USART_DMACmd(USART_TypeDef* USARTx, uint32_t USART_DMAReq, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 1618 {
bogdanm 0:9b334a45a8ff 1619 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1620 assert_param(IS_USART_1234_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1621 assert_param(IS_USART_DMAREQ(USART_DMAReq));
bogdanm 0:9b334a45a8ff 1622 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 1623
bogdanm 0:9b334a45a8ff 1624 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 1625 {
bogdanm 0:9b334a45a8ff 1626 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
bogdanm 0:9b334a45a8ff 1627 DMAR bits in the USART CR3 register */
bogdanm 0:9b334a45a8ff 1628 USARTx->CR3 |= USART_DMAReq;
bogdanm 0:9b334a45a8ff 1629 }
bogdanm 0:9b334a45a8ff 1630 else
bogdanm 0:9b334a45a8ff 1631 {
bogdanm 0:9b334a45a8ff 1632 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
bogdanm 0:9b334a45a8ff 1633 DMAR bits in the USART CR3 register */
bogdanm 0:9b334a45a8ff 1634 USARTx->CR3 &= (uint32_t)~USART_DMAReq;
bogdanm 0:9b334a45a8ff 1635 }
bogdanm 0:9b334a45a8ff 1636 }
bogdanm 0:9b334a45a8ff 1637
bogdanm 0:9b334a45a8ff 1638 /**
bogdanm 0:9b334a45a8ff 1639 * @brief Enables or disables the USART's DMA interface when reception error occurs.
bogdanm 0:9b334a45a8ff 1640 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1641 * following values: USART1 or USART2 or USART3 or UART4.
bogdanm 0:9b334a45a8ff 1642 * @param USART_DMAOnError: specifies the DMA status in case of reception error.
bogdanm 0:9b334a45a8ff 1643 * This parameter can be any combination of the following values:
bogdanm 0:9b334a45a8ff 1644 * @arg USART_DMAOnError_Enable: DMA receive request enabled when the USART DMA
bogdanm 0:9b334a45a8ff 1645 * reception error is asserted.
bogdanm 0:9b334a45a8ff 1646 * @arg USART_DMAOnError_Disable: DMA receive request disabled when the USART DMA
bogdanm 0:9b334a45a8ff 1647 * reception error is asserted.
bogdanm 0:9b334a45a8ff 1648 * @retval None
bogdanm 0:9b334a45a8ff 1649 */
bogdanm 0:9b334a45a8ff 1650 void USART_DMAReceptionErrorConfig(USART_TypeDef* USARTx, uint32_t USART_DMAOnError)
bogdanm 0:9b334a45a8ff 1651 {
bogdanm 0:9b334a45a8ff 1652 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1653 assert_param(IS_USART_1234_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1654 assert_param(IS_USART_DMAONERROR(USART_DMAOnError));
bogdanm 0:9b334a45a8ff 1655
bogdanm 0:9b334a45a8ff 1656 /* Clear the DMA Reception error detection bit */
bogdanm 0:9b334a45a8ff 1657 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DDRE);
bogdanm 0:9b334a45a8ff 1658 /* Set the new value for the DMA Reception error detection bit */
bogdanm 0:9b334a45a8ff 1659 USARTx->CR3 |= USART_DMAOnError;
bogdanm 0:9b334a45a8ff 1660 }
bogdanm 0:9b334a45a8ff 1661
bogdanm 0:9b334a45a8ff 1662 /**
bogdanm 0:9b334a45a8ff 1663 * @}
bogdanm 0:9b334a45a8ff 1664 */
bogdanm 0:9b334a45a8ff 1665
bogdanm 0:9b334a45a8ff 1666 /** @defgroup USART_Group12 Interrupts and flags management functions
bogdanm 0:9b334a45a8ff 1667 * @brief Interrupts and flags management functions
bogdanm 0:9b334a45a8ff 1668 *
bogdanm 0:9b334a45a8ff 1669 @verbatim
bogdanm 0:9b334a45a8ff 1670 ===============================================================================
bogdanm 0:9b334a45a8ff 1671 ##### Interrupts and flags management functions #####
bogdanm 0:9b334a45a8ff 1672 ===============================================================================
bogdanm 0:9b334a45a8ff 1673 [..] This subsection provides a set of functions allowing to configure the
bogdanm 0:9b334a45a8ff 1674 USART Interrupts sources, Requests and check or clear the flags or pending bits status.
bogdanm 0:9b334a45a8ff 1675 The user should identify which mode will be used in his application to
bogdanm 0:9b334a45a8ff 1676 manage the communication: Polling mode, Interrupt mode.
bogdanm 0:9b334a45a8ff 1677
bogdanm 0:9b334a45a8ff 1678 *** Polling Mode ***
bogdanm 0:9b334a45a8ff 1679 ====================
bogdanm 0:9b334a45a8ff 1680 [..] In Polling Mode, the SPI communication can be managed by these flags:
bogdanm 0:9b334a45a8ff 1681 (#) USART_FLAG_REACK: to indicate the status of the Receive Enable
bogdanm 0:9b334a45a8ff 1682 acknowledge flag
bogdanm 0:9b334a45a8ff 1683 (#) USART_FLAG_TEACK: to indicate the status of the Transmit Enable
bogdanm 0:9b334a45a8ff 1684 acknowledge flag.
bogdanm 0:9b334a45a8ff 1685 (#) USART_FLAG_WUF: to indicate the status of the Wake up flag.
bogdanm 0:9b334a45a8ff 1686 (#) USART_FLAG_RWU: to indicate the status of the Receive Wake up flag.
bogdanm 0:9b334a45a8ff 1687 (#) USART_FLAG_SBK: to indicate the status of the Send Break flag.
bogdanm 0:9b334a45a8ff 1688 (#) USART_FLAG_CMF: to indicate the status of the Character match flag.
bogdanm 0:9b334a45a8ff 1689 (#) USART_FLAG_BUSY: to indicate the status of the Busy flag.
bogdanm 0:9b334a45a8ff 1690 (#) USART_FLAG_ABRF: to indicate the status of the Auto baud rate flag.
bogdanm 0:9b334a45a8ff 1691 (#) USART_FLAG_ABRE: to indicate the status of the Auto baud rate error flag.
bogdanm 0:9b334a45a8ff 1692 (#) USART_FLAG_EOBF: to indicate the status of the End of block flag.
bogdanm 0:9b334a45a8ff 1693 (#) USART_FLAG_RTOF: to indicate the status of the Receive time out flag.
bogdanm 0:9b334a45a8ff 1694 (#) USART_FLAG_nCTSS: to indicate the status of the Inverted nCTS input
bogdanm 0:9b334a45a8ff 1695 bit status.
bogdanm 0:9b334a45a8ff 1696 (#) USART_FLAG_TXE: to indicate the status of the transmit buffer register.
bogdanm 0:9b334a45a8ff 1697 (#) USART_FLAG_RXNE: to indicate the status of the receive buffer register.
bogdanm 0:9b334a45a8ff 1698 (#) USART_FLAG_TC: to indicate the status of the transmit operation.
bogdanm 0:9b334a45a8ff 1699 (#) USART_FLAG_IDLE: to indicate the status of the Idle Line.
bogdanm 0:9b334a45a8ff 1700 (#) USART_FLAG_CTS: to indicate the status of the nCTS input.
bogdanm 0:9b334a45a8ff 1701 (#) USART_FLAG_LBD: to indicate the status of the LIN break detection.
bogdanm 0:9b334a45a8ff 1702 (#) USART_FLAG_NE: to indicate if a noise error occur.
bogdanm 0:9b334a45a8ff 1703 (#) USART_FLAG_FE: to indicate if a frame error occur.
bogdanm 0:9b334a45a8ff 1704 (#) USART_FLAG_PE: to indicate if a parity error occur.
bogdanm 0:9b334a45a8ff 1705 (#) USART_FLAG_ORE: to indicate if an Overrun error occur.
bogdanm 0:9b334a45a8ff 1706 [..] In this Mode it is advised to use the following functions:
bogdanm 0:9b334a45a8ff 1707 (+) FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG).
bogdanm 0:9b334a45a8ff 1708 (+) void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG).
bogdanm 0:9b334a45a8ff 1709
bogdanm 0:9b334a45a8ff 1710 *** Interrupt Mode ***
bogdanm 0:9b334a45a8ff 1711 ======================
bogdanm 0:9b334a45a8ff 1712 [..] In Interrupt Mode, the USART communication can be managed by 8 interrupt
bogdanm 0:9b334a45a8ff 1713 sources and 10 pending bits:
bogdanm 0:9b334a45a8ff 1714 (+) Pending Bits:
bogdanm 0:9b334a45a8ff 1715 (##) USART_IT_WU: to indicate the status of the Wake up interrupt.
bogdanm 0:9b334a45a8ff 1716 (##) USART_IT_CM: to indicate the status of Character match interrupt.
bogdanm 0:9b334a45a8ff 1717 (##) USART_IT_EOB: to indicate the status of End of block interrupt.
bogdanm 0:9b334a45a8ff 1718 (##) USART_IT_RTO: to indicate the status of Receive time out interrupt.
bogdanm 0:9b334a45a8ff 1719 (##) USART_IT_CTS: to indicate the status of CTS change interrupt.
bogdanm 0:9b334a45a8ff 1720 (##) USART_IT_LBD: to indicate the status of LIN Break detection interrupt.
bogdanm 0:9b334a45a8ff 1721 (##) USART_IT_TC: to indicate the status of Transmission complete interrupt.
bogdanm 0:9b334a45a8ff 1722 (##) USART_IT_IDLE: to indicate the status of IDLE line detected interrupt.
bogdanm 0:9b334a45a8ff 1723 (##) USART_IT_ORE: to indicate the status of OverRun Error interrupt.
bogdanm 0:9b334a45a8ff 1724 (##) USART_IT_NE: to indicate the status of Noise Error interrupt.
bogdanm 0:9b334a45a8ff 1725 (##) USART_IT_FE: to indicate the status of Framing Error interrupt.
bogdanm 0:9b334a45a8ff 1726 (##) USART_IT_PE: to indicate the status of Parity Error interrupt.
bogdanm 0:9b334a45a8ff 1727
bogdanm 0:9b334a45a8ff 1728 (+) Interrupt Source:
bogdanm 0:9b334a45a8ff 1729 (##) USART_IT_WU: specifies the interrupt source for Wake up interrupt.
bogdanm 0:9b334a45a8ff 1730 (##) USART_IT_CM: specifies the interrupt source for Character match
bogdanm 0:9b334a45a8ff 1731 interrupt.
bogdanm 0:9b334a45a8ff 1732 (##) USART_IT_EOB: specifies the interrupt source for End of block
bogdanm 0:9b334a45a8ff 1733 interrupt.
bogdanm 0:9b334a45a8ff 1734 (##) USART_IT_RTO: specifies the interrupt source for Receive time-out
bogdanm 0:9b334a45a8ff 1735 interrupt.
bogdanm 0:9b334a45a8ff 1736 (##) USART_IT_CTS: specifies the interrupt source for CTS change interrupt.
bogdanm 0:9b334a45a8ff 1737 (##) USART_IT_LBD: specifies the interrupt source for LIN Break
bogdanm 0:9b334a45a8ff 1738 detection interrupt.
bogdanm 0:9b334a45a8ff 1739 (##) USART_IT_TXE: specifies the interrupt source for Tansmit Data
bogdanm 0:9b334a45a8ff 1740 Register empty interrupt.
bogdanm 0:9b334a45a8ff 1741 (##) USART_IT_TC: specifies the interrupt source for Transmission
bogdanm 0:9b334a45a8ff 1742 complete interrupt.
bogdanm 0:9b334a45a8ff 1743 (##) USART_IT_RXNE: specifies the interrupt source for Receive Data
bogdanm 0:9b334a45a8ff 1744 register not empty interrupt.
bogdanm 0:9b334a45a8ff 1745 (##) USART_IT_IDLE: specifies the interrupt source for Idle line
bogdanm 0:9b334a45a8ff 1746 detection interrupt.
bogdanm 0:9b334a45a8ff 1747 (##) USART_IT_PE: specifies the interrupt source for Parity Error interrupt.
bogdanm 0:9b334a45a8ff 1748 (##) USART_IT_ERR: specifies the interrupt source for Error interrupt
bogdanm 0:9b334a45a8ff 1749 (Frame error, noise error, overrun error)
bogdanm 0:9b334a45a8ff 1750 -@@- Some parameters are coded in order to use them as interrupt
bogdanm 0:9b334a45a8ff 1751 source or as pending bits.
bogdanm 0:9b334a45a8ff 1752 [..] In this Mode it is advised to use the following functions:
bogdanm 0:9b334a45a8ff 1753 (+) void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState).
bogdanm 0:9b334a45a8ff 1754 (+) ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT).
bogdanm 0:9b334a45a8ff 1755 (+) void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT).
bogdanm 0:9b334a45a8ff 1756
bogdanm 0:9b334a45a8ff 1757 @endverbatim
bogdanm 0:9b334a45a8ff 1758 * @{
bogdanm 0:9b334a45a8ff 1759 */
bogdanm 0:9b334a45a8ff 1760
bogdanm 0:9b334a45a8ff 1761 /**
bogdanm 0:9b334a45a8ff 1762 * @brief Enables or disables the specified USART interrupts.
bogdanm 0:9b334a45a8ff 1763 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1764 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1765 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
bogdanm 0:9b334a45a8ff 1766 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 1767 * @arg USART_IT_WU: Wake up interrupt.
bogdanm 0:9b334a45a8ff 1768 * @arg USART_IT_CM: Character match interrupt.
bogdanm 0:9b334a45a8ff 1769 * @arg USART_IT_EOB: End of block interrupt.
bogdanm 0:9b334a45a8ff 1770 * @arg USART_IT_RTO: Receive time out interrupt.
bogdanm 0:9b334a45a8ff 1771 * @arg USART_IT_CTS: CTS change interrupt.
bogdanm 0:9b334a45a8ff 1772 * @arg USART_IT_LBD: LIN Break detection interrupt.
bogdanm 0:9b334a45a8ff 1773 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt.
bogdanm 0:9b334a45a8ff 1774 * @arg USART_IT_TC: Transmission complete interrupt.
bogdanm 0:9b334a45a8ff 1775 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
bogdanm 0:9b334a45a8ff 1776 * @arg USART_IT_IDLE: Idle line detection interrupt.
bogdanm 0:9b334a45a8ff 1777 * @arg USART_IT_PE: Parity Error interrupt.
bogdanm 0:9b334a45a8ff 1778 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
bogdanm 0:9b334a45a8ff 1779 * @param NewState: new state of the specified USARTx interrupts.
bogdanm 0:9b334a45a8ff 1780 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 1781 * @retval None
bogdanm 0:9b334a45a8ff 1782 */
bogdanm 0:9b334a45a8ff 1783 void USART_ITConfig(USART_TypeDef* USARTx, uint32_t USART_IT, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 1784 {
bogdanm 0:9b334a45a8ff 1785 uint32_t usartreg = 0, itpos = 0, itmask = 0;
bogdanm 0:9b334a45a8ff 1786 uint32_t usartxbase = 0;
bogdanm 0:9b334a45a8ff 1787 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1788 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1789 assert_param(IS_USART_CONFIG_IT(USART_IT));
bogdanm 0:9b334a45a8ff 1790 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 1791
bogdanm 0:9b334a45a8ff 1792 usartxbase = (uint32_t)USARTx;
bogdanm 0:9b334a45a8ff 1793
bogdanm 0:9b334a45a8ff 1794 /* Get the USART register index */
bogdanm 0:9b334a45a8ff 1795 usartreg = (((uint16_t)USART_IT) >> 0x08);
bogdanm 0:9b334a45a8ff 1796
bogdanm 0:9b334a45a8ff 1797 /* Get the interrupt position */
bogdanm 0:9b334a45a8ff 1798 itpos = USART_IT & IT_MASK;
bogdanm 0:9b334a45a8ff 1799 itmask = (((uint32_t)0x01) << itpos);
bogdanm 0:9b334a45a8ff 1800
bogdanm 0:9b334a45a8ff 1801 if (usartreg == 0x02) /* The IT is in CR2 register */
bogdanm 0:9b334a45a8ff 1802 {
bogdanm 0:9b334a45a8ff 1803 usartxbase += 0x04;
bogdanm 0:9b334a45a8ff 1804 }
bogdanm 0:9b334a45a8ff 1805 else if (usartreg == 0x03) /* The IT is in CR3 register */
bogdanm 0:9b334a45a8ff 1806 {
bogdanm 0:9b334a45a8ff 1807 usartxbase += 0x08;
bogdanm 0:9b334a45a8ff 1808 }
bogdanm 0:9b334a45a8ff 1809 else /* The IT is in CR1 register */
bogdanm 0:9b334a45a8ff 1810 {
bogdanm 0:9b334a45a8ff 1811 }
bogdanm 0:9b334a45a8ff 1812 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 1813 {
bogdanm 0:9b334a45a8ff 1814 *(__IO uint32_t*)usartxbase |= itmask;
bogdanm 0:9b334a45a8ff 1815 }
bogdanm 0:9b334a45a8ff 1816 else
bogdanm 0:9b334a45a8ff 1817 {
bogdanm 0:9b334a45a8ff 1818 *(__IO uint32_t*)usartxbase &= ~itmask;
bogdanm 0:9b334a45a8ff 1819 }
bogdanm 0:9b334a45a8ff 1820 }
bogdanm 0:9b334a45a8ff 1821
bogdanm 0:9b334a45a8ff 1822 /**
bogdanm 0:9b334a45a8ff 1823 * @brief Enables the specified USART's Request.
bogdanm 0:9b334a45a8ff 1824 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1825 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1826 * @param USART_Request: specifies the USART request.
bogdanm 0:9b334a45a8ff 1827 * This parameter can be any combination of the following values:
bogdanm 0:9b334a45a8ff 1828 * @arg USART_Request_TXFRQ: Transmit data flush ReQuest
bogdanm 0:9b334a45a8ff 1829 * @arg USART_Request_RXFRQ: Receive data flush ReQuest
bogdanm 0:9b334a45a8ff 1830 * @arg USART_Request_MMRQ: Mute Mode ReQuest
bogdanm 0:9b334a45a8ff 1831 * @arg USART_Request_SBKRQ: Send Break ReQuest
bogdanm 0:9b334a45a8ff 1832 * @arg USART_Request_ABRRQ: Auto Baud Rate ReQuest
bogdanm 0:9b334a45a8ff 1833 * @param NewState: new state of the DMA interface when reception error occurs.
bogdanm 0:9b334a45a8ff 1834 * This parameter can be: ENABLE or DISABLE.
bogdanm 0:9b334a45a8ff 1835 * @retval None
bogdanm 0:9b334a45a8ff 1836 */
bogdanm 0:9b334a45a8ff 1837 void USART_RequestCmd(USART_TypeDef* USARTx, uint32_t USART_Request, FunctionalState NewState)
bogdanm 0:9b334a45a8ff 1838 {
bogdanm 0:9b334a45a8ff 1839 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1840 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1841 assert_param(IS_USART_REQUEST(USART_Request));
bogdanm 0:9b334a45a8ff 1842 assert_param(IS_FUNCTIONAL_STATE(NewState));
bogdanm 0:9b334a45a8ff 1843
bogdanm 0:9b334a45a8ff 1844 if (NewState != DISABLE)
bogdanm 0:9b334a45a8ff 1845 {
bogdanm 0:9b334a45a8ff 1846 /* Enable the USART ReQuest by setting the dedicated request bit in the RQR
bogdanm 0:9b334a45a8ff 1847 register.*/
bogdanm 0:9b334a45a8ff 1848 USARTx->RQR |= USART_Request;
bogdanm 0:9b334a45a8ff 1849 }
bogdanm 0:9b334a45a8ff 1850 else
bogdanm 0:9b334a45a8ff 1851 {
bogdanm 0:9b334a45a8ff 1852 /* Disable the USART ReQuest by clearing the dedicated request bit in the RQR
bogdanm 0:9b334a45a8ff 1853 register.*/
bogdanm 0:9b334a45a8ff 1854 USARTx->RQR &= (uint32_t)~USART_Request;
bogdanm 0:9b334a45a8ff 1855 }
bogdanm 0:9b334a45a8ff 1856 }
bogdanm 0:9b334a45a8ff 1857
bogdanm 0:9b334a45a8ff 1858 /**
bogdanm 0:9b334a45a8ff 1859 * @brief Enables or disables the USART's Overrun detection.
bogdanm 0:9b334a45a8ff 1860 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1861 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1862 * @param USART_OVRDetection: specifies the OVR detection status in case of OVR error.
bogdanm 0:9b334a45a8ff 1863 * This parameter can be any combination of the following values:
bogdanm 0:9b334a45a8ff 1864 * @arg USART_OVRDetection_Enable: OVR error detection enabled when the USART OVR error
bogdanm 0:9b334a45a8ff 1865 * is asserted.
bogdanm 0:9b334a45a8ff 1866 * @arg USART_OVRDetection_Disable: OVR error detection disabled when the USART OVR error
bogdanm 0:9b334a45a8ff 1867 * is asserted.
bogdanm 0:9b334a45a8ff 1868 * @retval None
bogdanm 0:9b334a45a8ff 1869 */
bogdanm 0:9b334a45a8ff 1870 void USART_OverrunDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_OVRDetection)
bogdanm 0:9b334a45a8ff 1871 {
bogdanm 0:9b334a45a8ff 1872 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1873 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1874 assert_param(IS_USART_OVRDETECTION(USART_OVRDetection));
bogdanm 0:9b334a45a8ff 1875
bogdanm 0:9b334a45a8ff 1876 /* Clear the OVR detection bit */
bogdanm 0:9b334a45a8ff 1877 USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_OVRDIS);
bogdanm 0:9b334a45a8ff 1878 /* Set the new value for the OVR detection bit */
bogdanm 0:9b334a45a8ff 1879 USARTx->CR3 |= USART_OVRDetection;
bogdanm 0:9b334a45a8ff 1880 }
bogdanm 0:9b334a45a8ff 1881
bogdanm 0:9b334a45a8ff 1882 /**
bogdanm 0:9b334a45a8ff 1883 * @brief Checks whether the specified USART flag is set or not.
bogdanm 0:9b334a45a8ff 1884 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1885 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1886 * @param USART_FLAG: specifies the flag to check.
bogdanm 0:9b334a45a8ff 1887 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 1888 * @arg USART_FLAG_REACK: Receive Enable acknowledge flag.
bogdanm 0:9b334a45a8ff 1889 * @arg USART_FLAG_TEACK: Transmit Enable acknowledge flag.
bogdanm 0:9b334a45a8ff 1890 * @arg USART_FLAG_WUF: Wake up flag.
bogdanm 0:9b334a45a8ff 1891 * @arg USART_FLAG_RWU: Receive Wake up flag.
bogdanm 0:9b334a45a8ff 1892 * @arg USART_FLAG_SBK: Send Break flag.
bogdanm 0:9b334a45a8ff 1893 * @arg USART_FLAG_CMF: Character match flag.
bogdanm 0:9b334a45a8ff 1894 * @arg USART_FLAG_BUSY: Busy flag.
bogdanm 0:9b334a45a8ff 1895 * @arg USART_FLAG_ABRF: Auto baud rate flag.
bogdanm 0:9b334a45a8ff 1896 * @arg USART_FLAG_ABRE: Auto baud rate error flag.
bogdanm 0:9b334a45a8ff 1897 * @arg USART_FLAG_EOBF: End of block flag.
bogdanm 0:9b334a45a8ff 1898 * @arg USART_FLAG_RTOF: Receive time out flag.
bogdanm 0:9b334a45a8ff 1899 * @arg USART_FLAG_nCTSS: Inverted nCTS input bit status.
bogdanm 0:9b334a45a8ff 1900 * @arg USART_FLAG_CTS: CTS Change flag.
bogdanm 0:9b334a45a8ff 1901 * @arg USART_FLAG_LBD: LIN Break detection flag.
bogdanm 0:9b334a45a8ff 1902 * @arg USART_FLAG_TXE: Transmit data register empty flag.
bogdanm 0:9b334a45a8ff 1903 * @arg USART_FLAG_TC: Transmission Complete flag.
bogdanm 0:9b334a45a8ff 1904 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
bogdanm 0:9b334a45a8ff 1905 * @arg USART_FLAG_IDLE: Idle Line detection flag.
bogdanm 0:9b334a45a8ff 1906 * @arg USART_FLAG_ORE: OverRun Error flag.
bogdanm 0:9b334a45a8ff 1907 * @arg USART_FLAG_NE: Noise Error flag.
bogdanm 0:9b334a45a8ff 1908 * @arg USART_FLAG_FE: Framing Error flag.
bogdanm 0:9b334a45a8ff 1909 * @arg USART_FLAG_PE: Parity Error flag.
bogdanm 0:9b334a45a8ff 1910 * @retval The new state of USART_FLAG (SET or RESET).
bogdanm 0:9b334a45a8ff 1911 */
bogdanm 0:9b334a45a8ff 1912 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint32_t USART_FLAG)
bogdanm 0:9b334a45a8ff 1913 {
bogdanm 0:9b334a45a8ff 1914 FlagStatus bitstatus = RESET;
bogdanm 0:9b334a45a8ff 1915 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1916 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1917 assert_param(IS_USART_FLAG(USART_FLAG));
bogdanm 0:9b334a45a8ff 1918
bogdanm 0:9b334a45a8ff 1919 if ((USARTx->ISR & USART_FLAG) != (uint16_t)RESET)
bogdanm 0:9b334a45a8ff 1920 {
bogdanm 0:9b334a45a8ff 1921 bitstatus = SET;
bogdanm 0:9b334a45a8ff 1922 }
bogdanm 0:9b334a45a8ff 1923 else
bogdanm 0:9b334a45a8ff 1924 {
bogdanm 0:9b334a45a8ff 1925 bitstatus = RESET;
bogdanm 0:9b334a45a8ff 1926 }
bogdanm 0:9b334a45a8ff 1927 return bitstatus;
bogdanm 0:9b334a45a8ff 1928 }
bogdanm 0:9b334a45a8ff 1929
bogdanm 0:9b334a45a8ff 1930 /**
bogdanm 0:9b334a45a8ff 1931 * @brief Clears the USARTx's pending flags.
bogdanm 0:9b334a45a8ff 1932 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1933 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1934 * @param USART_FLAG: specifies the flag to clear.
bogdanm 0:9b334a45a8ff 1935 * This parameter can be any combination of the following values:
bogdanm 0:9b334a45a8ff 1936 * @arg USART_FLAG_WUF: Wake up flag.
bogdanm 0:9b334a45a8ff 1937 * @arg USART_FLAG_CMF: Character match flag.
bogdanm 0:9b334a45a8ff 1938 * @arg USART_FLAG_EOBF: End of block flag.
bogdanm 0:9b334a45a8ff 1939 * @arg USART_FLAG_RTOF: Receive time out flag.
bogdanm 0:9b334a45a8ff 1940 * @arg USART_FLAG_CTS: CTS Change flag.
bogdanm 0:9b334a45a8ff 1941 * @arg USART_FLAG_LBD: LIN Break detection flag.
bogdanm 0:9b334a45a8ff 1942 * @arg USART_FLAG_TC: Transmission Complete flag.
bogdanm 0:9b334a45a8ff 1943 * @arg USART_FLAG_IDLE: IDLE line detected flag.
bogdanm 0:9b334a45a8ff 1944 * @arg USART_FLAG_ORE: OverRun Error flag.
bogdanm 0:9b334a45a8ff 1945 * @arg USART_FLAG_NE: Noise Error flag.
bogdanm 0:9b334a45a8ff 1946 * @arg USART_FLAG_FE: Framing Error flag.
bogdanm 0:9b334a45a8ff 1947 * @arg USART_FLAG_PE: Parity Errorflag.
bogdanm 0:9b334a45a8ff 1948 *
bogdanm 0:9b334a45a8ff 1949 * @note
bogdanm 0:9b334a45a8ff 1950 * - RXNE pending bit is cleared by a read to the USART_RDR register
bogdanm 0:9b334a45a8ff 1951 * (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register USART_RQR
bogdanm 0:9b334a45a8ff 1952 * (USART_RequestCmd()).
bogdanm 0:9b334a45a8ff 1953 * - TC flag can be also cleared by software sequence: a read operation to
bogdanm 0:9b334a45a8ff 1954 * USART_SR register (USART_GetFlagStatus()) followed by a write operation
bogdanm 0:9b334a45a8ff 1955 * to USART_TDR register (USART_SendData()).
bogdanm 0:9b334a45a8ff 1956 * - TXE flag is cleared by a write to the USART_TDR register
bogdanm 0:9b334a45a8ff 1957 * (USART_SendData()) or by writing 1 to the TXFRQ in the register USART_RQR
bogdanm 0:9b334a45a8ff 1958 * (USART_RequestCmd()).
bogdanm 0:9b334a45a8ff 1959 * - SBKF flag is cleared by 1 to the SBKRQ in the register USART_RQR
bogdanm 0:9b334a45a8ff 1960 * (USART_RequestCmd()).
bogdanm 0:9b334a45a8ff 1961 * @retval None
bogdanm 0:9b334a45a8ff 1962 */
bogdanm 0:9b334a45a8ff 1963 void USART_ClearFlag(USART_TypeDef* USARTx, uint32_t USART_FLAG)
bogdanm 0:9b334a45a8ff 1964 {
bogdanm 0:9b334a45a8ff 1965 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1966 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 1967 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
bogdanm 0:9b334a45a8ff 1968
bogdanm 0:9b334a45a8ff 1969 USARTx->ICR = USART_FLAG;
bogdanm 0:9b334a45a8ff 1970 }
bogdanm 0:9b334a45a8ff 1971
bogdanm 0:9b334a45a8ff 1972 /**
bogdanm 0:9b334a45a8ff 1973 * @brief Checks whether the specified USART interrupt has occurred or not.
bogdanm 0:9b334a45a8ff 1974 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 1975 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 1976 * @param USART_IT: specifies the USART interrupt source to check.
bogdanm 0:9b334a45a8ff 1977 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 1978 * @arg USART_IT_WU: Wake up interrupt.
bogdanm 0:9b334a45a8ff 1979 * @arg USART_IT_CM: Character match interrupt.
bogdanm 0:9b334a45a8ff 1980 * @arg USART_IT_EOB: End of block interrupt.
bogdanm 0:9b334a45a8ff 1981 * @arg USART_IT_RTO: Receive time out interrupt.
bogdanm 0:9b334a45a8ff 1982 * @arg USART_IT_CTS: CTS change interrupt.
bogdanm 0:9b334a45a8ff 1983 * @arg USART_IT_LBD: LIN Break detection interrupt.
bogdanm 0:9b334a45a8ff 1984 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt.
bogdanm 0:9b334a45a8ff 1985 * @arg USART_IT_TC: Transmission complete interrupt.
bogdanm 0:9b334a45a8ff 1986 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
bogdanm 0:9b334a45a8ff 1987 * @arg USART_IT_IDLE: Idle line detection interrupt.
bogdanm 0:9b334a45a8ff 1988 * @arg USART_IT_ORE: OverRun Error interrupt.
bogdanm 0:9b334a45a8ff 1989 * @arg USART_IT_NE: Noise Error interrupt.
bogdanm 0:9b334a45a8ff 1990 * @arg USART_IT_FE: Framing Error interrupt.
bogdanm 0:9b334a45a8ff 1991 * @arg USART_IT_PE: Parity Error interrupt.
bogdanm 0:9b334a45a8ff 1992 * @retval The new state of USART_IT (SET or RESET).
bogdanm 0:9b334a45a8ff 1993 */
bogdanm 0:9b334a45a8ff 1994 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint32_t USART_IT)
bogdanm 0:9b334a45a8ff 1995 {
bogdanm 0:9b334a45a8ff 1996 uint32_t bitpos = 0, itmask = 0, usartreg = 0;
bogdanm 0:9b334a45a8ff 1997 ITStatus bitstatus = RESET;
bogdanm 0:9b334a45a8ff 1998 /* Check the parameters */
bogdanm 0:9b334a45a8ff 1999 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 2000 assert_param(IS_USART_GET_IT(USART_IT));
bogdanm 0:9b334a45a8ff 2001
bogdanm 0:9b334a45a8ff 2002 /* Get the USART register index */
bogdanm 0:9b334a45a8ff 2003 usartreg = (((uint16_t)USART_IT) >> 0x08);
bogdanm 0:9b334a45a8ff 2004 /* Get the interrupt position */
bogdanm 0:9b334a45a8ff 2005 itmask = USART_IT & IT_MASK;
bogdanm 0:9b334a45a8ff 2006 itmask = (uint32_t)0x01 << itmask;
bogdanm 0:9b334a45a8ff 2007
bogdanm 0:9b334a45a8ff 2008 if (usartreg == 0x01) /* The IT is in CR1 register */
bogdanm 0:9b334a45a8ff 2009 {
bogdanm 0:9b334a45a8ff 2010 itmask &= USARTx->CR1;
bogdanm 0:9b334a45a8ff 2011 }
bogdanm 0:9b334a45a8ff 2012 else if (usartreg == 0x02) /* The IT is in CR2 register */
bogdanm 0:9b334a45a8ff 2013 {
bogdanm 0:9b334a45a8ff 2014 itmask &= USARTx->CR2;
bogdanm 0:9b334a45a8ff 2015 }
bogdanm 0:9b334a45a8ff 2016 else /* The IT is in CR3 register */
bogdanm 0:9b334a45a8ff 2017 {
bogdanm 0:9b334a45a8ff 2018 itmask &= USARTx->CR3;
bogdanm 0:9b334a45a8ff 2019 }
bogdanm 0:9b334a45a8ff 2020
bogdanm 0:9b334a45a8ff 2021 bitpos = USART_IT >> 0x10;
bogdanm 0:9b334a45a8ff 2022 bitpos = (uint32_t)0x01 << bitpos;
bogdanm 0:9b334a45a8ff 2023 bitpos &= USARTx->ISR;
bogdanm 0:9b334a45a8ff 2024 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
bogdanm 0:9b334a45a8ff 2025 {
bogdanm 0:9b334a45a8ff 2026 bitstatus = SET;
bogdanm 0:9b334a45a8ff 2027 }
bogdanm 0:9b334a45a8ff 2028 else
bogdanm 0:9b334a45a8ff 2029 {
bogdanm 0:9b334a45a8ff 2030 bitstatus = RESET;
bogdanm 0:9b334a45a8ff 2031 }
bogdanm 0:9b334a45a8ff 2032
bogdanm 0:9b334a45a8ff 2033 return bitstatus;
bogdanm 0:9b334a45a8ff 2034 }
bogdanm 0:9b334a45a8ff 2035
bogdanm 0:9b334a45a8ff 2036 /**
bogdanm 0:9b334a45a8ff 2037 * @brief Clears the USARTx's interrupt pending bits.
bogdanm 0:9b334a45a8ff 2038 * @param USARTx: Select the USART peripheral. This parameter can be one of the
bogdanm 0:9b334a45a8ff 2039 * following values: USART1 or USART2 or USART3 or UART4 or UART5.
bogdanm 0:9b334a45a8ff 2040 * @param USART_IT: specifies the interrupt pending bit to clear.
bogdanm 0:9b334a45a8ff 2041 * This parameter can be one of the following values:
bogdanm 0:9b334a45a8ff 2042 * @arg USART_IT_WU: Wake up interrupt.
bogdanm 0:9b334a45a8ff 2043 * @arg USART_IT_CM: Character match interrupt.
bogdanm 0:9b334a45a8ff 2044 * @arg USART_IT_EOB: End of block interrupt.
bogdanm 0:9b334a45a8ff 2045 * @arg USART_IT_RTO: Receive time out interrupt.
bogdanm 0:9b334a45a8ff 2046 * @arg USART_IT_CTS: CTS change interrupt.
bogdanm 0:9b334a45a8ff 2047 * @arg USART_IT_LBD: LIN Break detection interrupt.
bogdanm 0:9b334a45a8ff 2048 * @arg USART_IT_TC: Transmission complete interrupt.
bogdanm 0:9b334a45a8ff 2049 * @arg USART_IT_IDLE: IDLE line detected interrupt.
bogdanm 0:9b334a45a8ff 2050 * @arg USART_IT_ORE: OverRun Error interrupt.
bogdanm 0:9b334a45a8ff 2051 * @arg USART_IT_NE: Noise Error interrupt.
bogdanm 0:9b334a45a8ff 2052 * @arg USART_IT_FE: Framing Error interrupt.
bogdanm 0:9b334a45a8ff 2053 * @arg USART_IT_PE: Parity Error interrupt.
bogdanm 0:9b334a45a8ff 2054 * @note
bogdanm 0:9b334a45a8ff 2055 * - RXNE pending bit is cleared by a read to the USART_RDR register
bogdanm 0:9b334a45a8ff 2056 * (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register USART_RQR
bogdanm 0:9b334a45a8ff 2057 * (USART_RequestCmd()).
bogdanm 0:9b334a45a8ff 2058 * - TC pending bit can be also cleared by software sequence: a read
bogdanm 0:9b334a45a8ff 2059 * operation to USART_SR register (USART_GetITStatus()) followed by a write
bogdanm 0:9b334a45a8ff 2060 * operation to USART_TDR register (USART_SendData()).
bogdanm 0:9b334a45a8ff 2061 * - TXE pending bit is cleared by a write to the USART_TDR register
bogdanm 0:9b334a45a8ff 2062 * (USART_SendData()) or by writing 1 to the TXFRQ in the register USART_RQR
bogdanm 0:9b334a45a8ff 2063 * (USART_RequestCmd()).
bogdanm 0:9b334a45a8ff 2064 * @retval None
bogdanm 0:9b334a45a8ff 2065 */
bogdanm 0:9b334a45a8ff 2066 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint32_t USART_IT)
bogdanm 0:9b334a45a8ff 2067 {
bogdanm 0:9b334a45a8ff 2068 uint32_t bitpos = 0, itmask = 0;
bogdanm 0:9b334a45a8ff 2069 /* Check the parameters */
bogdanm 0:9b334a45a8ff 2070 assert_param(IS_USART_ALL_PERIPH(USARTx));
bogdanm 0:9b334a45a8ff 2071 assert_param(IS_USART_CLEAR_IT(USART_IT));
bogdanm 0:9b334a45a8ff 2072
bogdanm 0:9b334a45a8ff 2073 bitpos = USART_IT >> 0x10;
bogdanm 0:9b334a45a8ff 2074 itmask = ((uint32_t)0x01 << (uint32_t)bitpos);
bogdanm 0:9b334a45a8ff 2075 USARTx->ICR = (uint32_t)itmask;
bogdanm 0:9b334a45a8ff 2076 }
bogdanm 0:9b334a45a8ff 2077
bogdanm 0:9b334a45a8ff 2078 /**
bogdanm 0:9b334a45a8ff 2079 * @}
bogdanm 0:9b334a45a8ff 2080 */
bogdanm 0:9b334a45a8ff 2081
bogdanm 0:9b334a45a8ff 2082 /**
bogdanm 0:9b334a45a8ff 2083 * @}
bogdanm 0:9b334a45a8ff 2084 */
bogdanm 0:9b334a45a8ff 2085
bogdanm 0:9b334a45a8ff 2086 /**
bogdanm 0:9b334a45a8ff 2087 * @}
bogdanm 0:9b334a45a8ff 2088 */
bogdanm 0:9b334a45a8ff 2089
bogdanm 0:9b334a45a8ff 2090 /**
bogdanm 0:9b334a45a8ff 2091 * @}
bogdanm 0:9b334a45a8ff 2092 */
bogdanm 0:9b334a45a8ff 2093
bogdanm 0:9b334a45a8ff 2094 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/