mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
130:1dec54e4aec3
test with CLOCK_SETUP = 0

Who changed what in which revision?

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