mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Feb 26 09:45:12 2014 +0000
Revision:
106:ced8cbb51063
Parent:
80:66393a7b209d
Synchronized with git revision 4222735eff5868389433f0e9271976b39c8115cd

Full URL: https://github.com/mbedmicro/mbed/commit/4222735eff5868389433f0e9271976b39c8115cd/

[NUCLEO_xxx] Update STM32CubeF4 driver V1.0.0 + update license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 76:aeb1df146756 1 /**
mbed_official 76:aeb1df146756 2 ******************************************************************************
mbed_official 76:aeb1df146756 3 * @file stm32l1xx_usart.c
mbed_official 76:aeb1df146756 4 * @author MCD Application Team
mbed_official 80:66393a7b209d 5 * @version V1.3.0
mbed_official 80:66393a7b209d 6 * @date 31-January-2014
mbed_official 76:aeb1df146756 7 * @brief This file provides firmware functions to manage the following
mbed_official 76:aeb1df146756 8 * functionalities of the Universal synchronous asynchronous receiver
mbed_official 76:aeb1df146756 9 * transmitter (USART):
mbed_official 76:aeb1df146756 10 * + Initialization and Configuration
mbed_official 76:aeb1df146756 11 * + Data transfers
mbed_official 76:aeb1df146756 12 * + Multi-Processor Communication
mbed_official 76:aeb1df146756 13 * + LIN mode
mbed_official 76:aeb1df146756 14 * + Half-duplex mode
mbed_official 76:aeb1df146756 15 * + Smartcard mode
mbed_official 76:aeb1df146756 16 * + IrDA mode
mbed_official 76:aeb1df146756 17 * + DMA transfers management
mbed_official 76:aeb1df146756 18 * + Interrupts and flags management
mbed_official 76:aeb1df146756 19 *
mbed_official 76:aeb1df146756 20 * @verbatim
mbed_official 76:aeb1df146756 21 ===============================================================================
mbed_official 76:aeb1df146756 22 ##### How to use this driver #####
mbed_official 76:aeb1df146756 23 ===============================================================================
mbed_official 76:aeb1df146756 24 [..]
mbed_official 76:aeb1df146756 25 (#) Enable peripheral clock using
mbed_official 76:aeb1df146756 26 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE) function for
mbed_official 76:aeb1df146756 27 USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE)
mbed_official 76:aeb1df146756 28 function for USART2 and USART3.
mbed_official 76:aeb1df146756 29 (#) According to the USART mode, enable the GPIO clocks using
mbed_official 76:aeb1df146756 30 RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS,
mbed_official 76:aeb1df146756 31 or and SCLK).
mbed_official 76:aeb1df146756 32 (#) Peripheral's alternate function:
mbed_official 76:aeb1df146756 33 (++) Connect the pin to the desired peripherals' Alternate
mbed_official 76:aeb1df146756 34 Function (AF) using GPIO_PinAFConfig() function.
mbed_official 76:aeb1df146756 35 (++) Configure the desired pin in alternate function by:
mbed_official 76:aeb1df146756 36 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF.
mbed_official 76:aeb1df146756 37 (++) Select the type, pull-up/pull-down and output speed via
mbed_official 76:aeb1df146756 38 GPIO_PuPd, GPIO_OType and GPIO_Speed members.
mbed_official 76:aeb1df146756 39 (++) Call GPIO_Init() function.
mbed_official 76:aeb1df146756 40 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
mbed_official 76:aeb1df146756 41 flow control and Mode(Receiver/Transmitter) using the SPI_Init()
mbed_official 76:aeb1df146756 42 function.
mbed_official 76:aeb1df146756 43 (#) For synchronous mode, enable the clock and program the polarity,
mbed_official 76:aeb1df146756 44 phase and last bit using the USART_ClockInit() function.
mbed_official 76:aeb1df146756 45 (#) Enable the NVIC and the corresponding interrupt using the function
mbed_official 76:aeb1df146756 46 USART_ITConfig() if you need to use interrupt mode.
mbed_official 76:aeb1df146756 47 (#) When using the DMA mode.
mbed_official 76:aeb1df146756 48 (++) Configure the DMA using DMA_Init() function.
mbed_official 76:aeb1df146756 49 (++) Active the needed channel Request using USART_DMACmd() function.
mbed_official 76:aeb1df146756 50 (#) Enable the USART using the USART_Cmd() function.
mbed_official 76:aeb1df146756 51 (#) Enable the DMA using the DMA_Cmd() function, when using DMA mode.
mbed_official 76:aeb1df146756 52 [..]
mbed_official 76:aeb1df146756 53 Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections
mbed_official 76:aeb1df146756 54 for more details.
mbed_official 76:aeb1df146756 55
mbed_official 76:aeb1df146756 56 @endverbatim
mbed_official 76:aeb1df146756 57
mbed_official 76:aeb1df146756 58 ******************************************************************************
mbed_official 76:aeb1df146756 59 * @attention
mbed_official 76:aeb1df146756 60 *
mbed_official 106:ced8cbb51063 61 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 76:aeb1df146756 62 *
mbed_official 106:ced8cbb51063 63 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 106:ced8cbb51063 64 * are permitted provided that the following conditions are met:
mbed_official 106:ced8cbb51063 65 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 106:ced8cbb51063 66 * this list of conditions and the following disclaimer.
mbed_official 106:ced8cbb51063 67 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 106:ced8cbb51063 68 * this list of conditions and the following disclaimer in the documentation
mbed_official 106:ced8cbb51063 69 * and/or other materials provided with the distribution.
mbed_official 106:ced8cbb51063 70 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 106:ced8cbb51063 71 * may be used to endorse or promote products derived from this software
mbed_official 106:ced8cbb51063 72 * without specific prior written permission.
mbed_official 76:aeb1df146756 73 *
mbed_official 106:ced8cbb51063 74 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 106:ced8cbb51063 75 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 106:ced8cbb51063 76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 106:ced8cbb51063 77 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 106:ced8cbb51063 78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 106:ced8cbb51063 79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 106:ced8cbb51063 80 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 106:ced8cbb51063 81 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 106:ced8cbb51063 82 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 106:ced8cbb51063 83 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 76:aeb1df146756 84 *
mbed_official 76:aeb1df146756 85 ******************************************************************************
mbed_official 76:aeb1df146756 86 */
mbed_official 76:aeb1df146756 87
mbed_official 76:aeb1df146756 88 /* Includes ------------------------------------------------------------------*/
mbed_official 76:aeb1df146756 89 #include "stm32l1xx_usart.h"
mbed_official 76:aeb1df146756 90 #include "stm32l1xx_rcc.h"
mbed_official 76:aeb1df146756 91
mbed_official 76:aeb1df146756 92 /** @addtogroup STM32L1xx_StdPeriph_Driver
mbed_official 76:aeb1df146756 93 * @{
mbed_official 76:aeb1df146756 94 */
mbed_official 76:aeb1df146756 95
mbed_official 76:aeb1df146756 96 /** @defgroup USART
mbed_official 76:aeb1df146756 97 * @brief USART driver modules
mbed_official 76:aeb1df146756 98 * @{
mbed_official 76:aeb1df146756 99 */
mbed_official 76:aeb1df146756 100
mbed_official 76:aeb1df146756 101 /* Private typedef -----------------------------------------------------------*/
mbed_official 76:aeb1df146756 102 /* Private define ------------------------------------------------------------*/
mbed_official 76:aeb1df146756 103
mbed_official 76:aeb1df146756 104 /*!< USART CR1 register clear Mask ((~(uint16_t)0xE9F3)) */
mbed_official 76:aeb1df146756 105 #define CR1_CLEAR_MASK ((uint16_t)(USART_CR1_M | USART_CR1_PCE | \
mbed_official 76:aeb1df146756 106 USART_CR1_PS | USART_CR1_TE | \
mbed_official 76:aeb1df146756 107 USART_CR1_RE))
mbed_official 76:aeb1df146756 108
mbed_official 76:aeb1df146756 109 /*!< USART CR2 register clock bits clear Mask ((~(uint16_t)0xF0FF)) */
mbed_official 76:aeb1df146756 110 #define CR2_CLOCK_CLEAR_MASK ((uint16_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
mbed_official 76:aeb1df146756 111 USART_CR2_CPHA | USART_CR2_LBCL))
mbed_official 76:aeb1df146756 112
mbed_official 76:aeb1df146756 113 /*!< USART CR3 register clear Mask ((~(uint16_t)0xFCFF)) */
mbed_official 76:aeb1df146756 114 #define CR3_CLEAR_MASK ((uint16_t)(USART_CR3_RTSE | USART_CR3_CTSE))
mbed_official 76:aeb1df146756 115
mbed_official 76:aeb1df146756 116 /*!< USART Interrupts mask */
mbed_official 76:aeb1df146756 117 #define IT_MASK ((uint16_t)0x001F)
mbed_official 76:aeb1df146756 118
mbed_official 76:aeb1df146756 119 /* Private macro -------------------------------------------------------------*/
mbed_official 76:aeb1df146756 120 /* Private variables ---------------------------------------------------------*/
mbed_official 76:aeb1df146756 121 /* Private function prototypes -----------------------------------------------*/
mbed_official 76:aeb1df146756 122 /* Private functions ---------------------------------------------------------*/
mbed_official 76:aeb1df146756 123
mbed_official 76:aeb1df146756 124 /** @defgroup USART_Private_Functions
mbed_official 76:aeb1df146756 125 * @{
mbed_official 76:aeb1df146756 126 */
mbed_official 76:aeb1df146756 127
mbed_official 76:aeb1df146756 128 /** @defgroup USART_Group1 Initialization and Configuration functions
mbed_official 76:aeb1df146756 129 * @brief Initialization and Configuration functions
mbed_official 76:aeb1df146756 130 *
mbed_official 76:aeb1df146756 131 @verbatim
mbed_official 76:aeb1df146756 132 ===============================================================================
mbed_official 76:aeb1df146756 133 ##### Initialization and Configuration functions #####
mbed_official 76:aeb1df146756 134 ===============================================================================
mbed_official 76:aeb1df146756 135 [..]
mbed_official 76:aeb1df146756 136 This subsection provides a set of functions allowing to initialize the USART
mbed_official 76:aeb1df146756 137 in asynchronous and in synchronous modes.
mbed_official 76:aeb1df146756 138 (+) For the asynchronous mode only these parameters can be configured:
mbed_official 76:aeb1df146756 139 (+) Baud Rate.
mbed_official 76:aeb1df146756 140 (+) Word Length.
mbed_official 76:aeb1df146756 141 (+) Stop Bit.
mbed_official 76:aeb1df146756 142 (+) Parity: If the parity is enabled, then the MSB bit of the data written
mbed_official 76:aeb1df146756 143 in the data register is transmitted but is changed by the parity bit.
mbed_official 76:aeb1df146756 144 Depending on the frame length defined by the M bit (8-bits or 9-bits),
mbed_official 76:aeb1df146756 145 the possible USART frame formats are as listed in the following table:
mbed_official 76:aeb1df146756 146 [..]
mbed_official 76:aeb1df146756 147 +-------------------------------------------------------------+
mbed_official 76:aeb1df146756 148 | M bit | PCE bit | USART frame |
mbed_official 76:aeb1df146756 149 |---------------------|---------------------------------------|
mbed_official 76:aeb1df146756 150 | 0 | 0 | | SB | 8 bit data | STB | |
mbed_official 76:aeb1df146756 151 |---------|-----------|---------------------------------------|
mbed_official 76:aeb1df146756 152 | 0 | 1 | | SB | 7 bit data | PB | STB | |
mbed_official 76:aeb1df146756 153 |---------|-----------|---------------------------------------|
mbed_official 76:aeb1df146756 154 | 1 | 0 | | SB | 9 bit data | STB | |
mbed_official 76:aeb1df146756 155 |---------|-----------|---------------------------------------|
mbed_official 76:aeb1df146756 156 | 1 | 1 | | SB | 8 bit data | PB | STB | |
mbed_official 76:aeb1df146756 157 +-------------------------------------------------------------+
mbed_official 76:aeb1df146756 158 [..]
mbed_official 76:aeb1df146756 159 (+) Hardware flow control.
mbed_official 76:aeb1df146756 160 (+) Receiver/transmitter modes.
mbed_official 76:aeb1df146756 161 [..] The USART_Init() function follows the USART asynchronous configuration
mbed_official 76:aeb1df146756 162 procedure(details for the procedure are available in reference manual
mbed_official 76:aeb1df146756 163 (RM0038)).
mbed_official 76:aeb1df146756 164 (+) For the synchronous mode in addition to the asynchronous mode parameters
mbed_official 76:aeb1df146756 165 these parameters should be also configured:
mbed_official 76:aeb1df146756 166 (++) USART Clock Enabled.
mbed_official 76:aeb1df146756 167 (++) USART polarity.
mbed_official 76:aeb1df146756 168 (++) USART phase.
mbed_official 76:aeb1df146756 169 (++) USART LastBit.
mbed_official 76:aeb1df146756 170 [..] These parameters can be configured using the USART_ClockInit() function.
mbed_official 76:aeb1df146756 171
mbed_official 76:aeb1df146756 172 @endverbatim
mbed_official 76:aeb1df146756 173 * @{
mbed_official 76:aeb1df146756 174 */
mbed_official 76:aeb1df146756 175
mbed_official 76:aeb1df146756 176 /**
mbed_official 76:aeb1df146756 177 * @brief Deinitializes the USARTx peripheral registers to their default reset values.
mbed_official 76:aeb1df146756 178 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 179 * This parameter can be one of the following values: USART1, USART2, USART3,
mbed_official 76:aeb1df146756 180 * UART4 or UART5.
mbed_official 76:aeb1df146756 181 * @retval None.
mbed_official 76:aeb1df146756 182 */
mbed_official 76:aeb1df146756 183 void USART_DeInit(USART_TypeDef* USARTx)
mbed_official 76:aeb1df146756 184 {
mbed_official 76:aeb1df146756 185 /* Check the parameters */
mbed_official 76:aeb1df146756 186 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 187
mbed_official 76:aeb1df146756 188 if (USARTx == USART1)
mbed_official 76:aeb1df146756 189 {
mbed_official 76:aeb1df146756 190 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
mbed_official 76:aeb1df146756 191 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
mbed_official 76:aeb1df146756 192 }
mbed_official 76:aeb1df146756 193 else if (USARTx == USART2)
mbed_official 76:aeb1df146756 194 {
mbed_official 76:aeb1df146756 195 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
mbed_official 76:aeb1df146756 196 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
mbed_official 76:aeb1df146756 197 }
mbed_official 76:aeb1df146756 198 else if (USARTx == USART3)
mbed_official 76:aeb1df146756 199 {
mbed_official 76:aeb1df146756 200 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
mbed_official 76:aeb1df146756 201 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
mbed_official 76:aeb1df146756 202 }
mbed_official 76:aeb1df146756 203 else if (USARTx == UART4)
mbed_official 76:aeb1df146756 204 {
mbed_official 76:aeb1df146756 205 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
mbed_official 76:aeb1df146756 206 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
mbed_official 76:aeb1df146756 207 }
mbed_official 76:aeb1df146756 208 else
mbed_official 76:aeb1df146756 209 {
mbed_official 76:aeb1df146756 210 if (USARTx == UART5)
mbed_official 76:aeb1df146756 211 {
mbed_official 76:aeb1df146756 212 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
mbed_official 76:aeb1df146756 213 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
mbed_official 76:aeb1df146756 214 }
mbed_official 76:aeb1df146756 215 }
mbed_official 76:aeb1df146756 216 }
mbed_official 76:aeb1df146756 217
mbed_official 76:aeb1df146756 218 /**
mbed_official 76:aeb1df146756 219 * @brief Initializes the USARTx peripheral according to the specified
mbed_official 76:aeb1df146756 220 * parameters in the USART_InitStruct.
mbed_official 76:aeb1df146756 221 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 222 * This parameter can be one of the following values: USART1, USART2, USART3,
mbed_official 76:aeb1df146756 223 * UART4 or UART5.
mbed_official 76:aeb1df146756 224 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure that
mbed_official 76:aeb1df146756 225 * contains the configuration information for the specified USART peripheral.
mbed_official 76:aeb1df146756 226 * @retval None.
mbed_official 76:aeb1df146756 227 */
mbed_official 76:aeb1df146756 228 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
mbed_official 76:aeb1df146756 229 {
mbed_official 76:aeb1df146756 230 uint32_t tmpreg = 0x00, apbclock = 0x00;
mbed_official 76:aeb1df146756 231 uint32_t integerdivider = 0x00;
mbed_official 76:aeb1df146756 232 uint32_t fractionaldivider = 0x00;
mbed_official 76:aeb1df146756 233 RCC_ClocksTypeDef RCC_ClocksStatus;
mbed_official 76:aeb1df146756 234
mbed_official 76:aeb1df146756 235 /* Check the parameters */
mbed_official 76:aeb1df146756 236 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 237 assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
mbed_official 76:aeb1df146756 238 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
mbed_official 76:aeb1df146756 239 assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
mbed_official 76:aeb1df146756 240 assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
mbed_official 76:aeb1df146756 241 assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
mbed_official 76:aeb1df146756 242 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
mbed_official 76:aeb1df146756 243
mbed_official 76:aeb1df146756 244 /* The hardware flow control is available only for USART1, USART2 and USART3 */
mbed_official 76:aeb1df146756 245 if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
mbed_official 76:aeb1df146756 246 {
mbed_official 76:aeb1df146756 247 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 76:aeb1df146756 248 }
mbed_official 76:aeb1df146756 249
mbed_official 76:aeb1df146756 250 /*---------------------------- USART CR2 Configuration -----------------------*/
mbed_official 76:aeb1df146756 251 tmpreg = USARTx->CR2;
mbed_official 76:aeb1df146756 252 /* Clear STOP[13:12] bits */
mbed_official 76:aeb1df146756 253 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
mbed_official 76:aeb1df146756 254
mbed_official 76:aeb1df146756 255 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
mbed_official 76:aeb1df146756 256 /* Set STOP[13:12] bits according to USART_StopBits value */
mbed_official 76:aeb1df146756 257 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
mbed_official 76:aeb1df146756 258
mbed_official 76:aeb1df146756 259 /* Write to USART CR2 */
mbed_official 76:aeb1df146756 260 USARTx->CR2 = (uint16_t)tmpreg;
mbed_official 76:aeb1df146756 261
mbed_official 76:aeb1df146756 262 /*---------------------------- USART CR1 Configuration -----------------------*/
mbed_official 76:aeb1df146756 263 tmpreg = USARTx->CR1;
mbed_official 76:aeb1df146756 264 /* Clear M, PCE, PS, TE and RE bits */
mbed_official 76:aeb1df146756 265 tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
mbed_official 76:aeb1df146756 266
mbed_official 76:aeb1df146756 267 /* Configure the USART Word Length, Parity and mode ----------------------- */
mbed_official 76:aeb1df146756 268 /* Set the M bits according to USART_WordLength value */
mbed_official 76:aeb1df146756 269 /* Set PCE and PS bits according to USART_Parity value */
mbed_official 76:aeb1df146756 270 /* Set TE and RE bits according to USART_Mode value */
mbed_official 76:aeb1df146756 271 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
mbed_official 76:aeb1df146756 272 USART_InitStruct->USART_Mode;
mbed_official 76:aeb1df146756 273
mbed_official 76:aeb1df146756 274 /* Write to USART CR1 */
mbed_official 76:aeb1df146756 275 USARTx->CR1 = (uint16_t)tmpreg;
mbed_official 76:aeb1df146756 276
mbed_official 76:aeb1df146756 277 /*---------------------------- USART CR3 Configuration -----------------------*/
mbed_official 76:aeb1df146756 278 tmpreg = USARTx->CR3;
mbed_official 76:aeb1df146756 279 /* Clear CTSE and RTSE bits */
mbed_official 76:aeb1df146756 280 tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
mbed_official 76:aeb1df146756 281
mbed_official 76:aeb1df146756 282 /* Configure the USART HFC -------------------------------------------------*/
mbed_official 76:aeb1df146756 283 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
mbed_official 76:aeb1df146756 284 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
mbed_official 76:aeb1df146756 285
mbed_official 76:aeb1df146756 286 /* Write to USART CR3 */
mbed_official 76:aeb1df146756 287 USARTx->CR3 = (uint16_t)tmpreg;
mbed_official 76:aeb1df146756 288
mbed_official 76:aeb1df146756 289 /*---------------------------- USART BRR Configuration -----------------------*/
mbed_official 76:aeb1df146756 290 /* Configure the USART Baud Rate -------------------------------------------*/
mbed_official 76:aeb1df146756 291 RCC_GetClocksFreq(&RCC_ClocksStatus);
mbed_official 76:aeb1df146756 292 if (USARTx == USART1)
mbed_official 76:aeb1df146756 293 {
mbed_official 76:aeb1df146756 294 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
mbed_official 76:aeb1df146756 295 }
mbed_official 76:aeb1df146756 296 else
mbed_official 76:aeb1df146756 297 {
mbed_official 76:aeb1df146756 298 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
mbed_official 76:aeb1df146756 299 }
mbed_official 76:aeb1df146756 300
mbed_official 76:aeb1df146756 301 /* Determine the integer part */
mbed_official 76:aeb1df146756 302 if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
mbed_official 76:aeb1df146756 303 {
mbed_official 76:aeb1df146756 304 /* Integer part computing in case Oversampling mode is 8 Samples */
mbed_official 76:aeb1df146756 305 integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
mbed_official 76:aeb1df146756 306 }
mbed_official 76:aeb1df146756 307 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
mbed_official 76:aeb1df146756 308 {
mbed_official 76:aeb1df146756 309 /* Integer part computing in case Oversampling mode is 16 Samples */
mbed_official 76:aeb1df146756 310 integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
mbed_official 76:aeb1df146756 311 }
mbed_official 76:aeb1df146756 312 tmpreg = (integerdivider / 100) << 4;
mbed_official 76:aeb1df146756 313
mbed_official 76:aeb1df146756 314 /* Determine the fractional part */
mbed_official 76:aeb1df146756 315 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
mbed_official 76:aeb1df146756 316
mbed_official 76:aeb1df146756 317 /* Implement the fractional part in the register */
mbed_official 76:aeb1df146756 318 if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
mbed_official 76:aeb1df146756 319 {
mbed_official 76:aeb1df146756 320 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
mbed_official 76:aeb1df146756 321 }
mbed_official 76:aeb1df146756 322 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
mbed_official 76:aeb1df146756 323 {
mbed_official 76:aeb1df146756 324 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
mbed_official 76:aeb1df146756 325 }
mbed_official 76:aeb1df146756 326
mbed_official 76:aeb1df146756 327 /* Write to USART BRR */
mbed_official 76:aeb1df146756 328 USARTx->BRR = (uint16_t)tmpreg;
mbed_official 76:aeb1df146756 329 }
mbed_official 76:aeb1df146756 330
mbed_official 76:aeb1df146756 331 /**
mbed_official 76:aeb1df146756 332 * @brief Fills each USART_InitStruct member with its default value.
mbed_official 76:aeb1df146756 333 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
mbed_official 76:aeb1df146756 334 * which will be initialized.
mbed_official 76:aeb1df146756 335 * @retval None
mbed_official 76:aeb1df146756 336 */
mbed_official 76:aeb1df146756 337 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
mbed_official 76:aeb1df146756 338 {
mbed_official 76:aeb1df146756 339 /* USART_InitStruct members default value */
mbed_official 76:aeb1df146756 340 USART_InitStruct->USART_BaudRate = 9600;
mbed_official 76:aeb1df146756 341 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
mbed_official 76:aeb1df146756 342 USART_InitStruct->USART_StopBits = USART_StopBits_1;
mbed_official 76:aeb1df146756 343 USART_InitStruct->USART_Parity = USART_Parity_No ;
mbed_official 76:aeb1df146756 344 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
mbed_official 76:aeb1df146756 345 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
mbed_official 76:aeb1df146756 346 }
mbed_official 76:aeb1df146756 347
mbed_official 76:aeb1df146756 348 /**
mbed_official 76:aeb1df146756 349 * @brief Initializes the USARTx peripheral Clock according to the
mbed_official 76:aeb1df146756 350 * specified parameters in the USART_ClockInitStruct.
mbed_official 76:aeb1df146756 351 * @param USARTx: where x can be 1, 2, 3 to select the USART peripheral.
mbed_official 76:aeb1df146756 352 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
mbed_official 76:aeb1df146756 353 * structure that contains the configuration information for the specified
mbed_official 76:aeb1df146756 354 * USART peripheral.
mbed_official 76:aeb1df146756 355 * @note The Smart Card and Synchronous modes are not available for UART4 and UART5.
mbed_official 76:aeb1df146756 356 * @retval None.
mbed_official 76:aeb1df146756 357 */
mbed_official 76:aeb1df146756 358 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
mbed_official 76:aeb1df146756 359 {
mbed_official 76:aeb1df146756 360 uint32_t tmpreg = 0x00;
mbed_official 76:aeb1df146756 361 /* Check the parameters */
mbed_official 76:aeb1df146756 362 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 76:aeb1df146756 363 assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
mbed_official 76:aeb1df146756 364 assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
mbed_official 76:aeb1df146756 365 assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
mbed_official 76:aeb1df146756 366 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
mbed_official 76:aeb1df146756 367
mbed_official 76:aeb1df146756 368 /*---------------------------- USART CR2 Configuration -----------------------*/
mbed_official 76:aeb1df146756 369 tmpreg = USARTx->CR2;
mbed_official 76:aeb1df146756 370 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
mbed_official 76:aeb1df146756 371 tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
mbed_official 76:aeb1df146756 372 /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
mbed_official 76:aeb1df146756 373 /* Set CLKEN bit according to USART_Clock value */
mbed_official 76:aeb1df146756 374 /* Set CPOL bit according to USART_CPOL value */
mbed_official 76:aeb1df146756 375 /* Set CPHA bit according to USART_CPHA value */
mbed_official 76:aeb1df146756 376 /* Set LBCL bit according to USART_LastBit value */
mbed_official 76:aeb1df146756 377 tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
mbed_official 76:aeb1df146756 378 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
mbed_official 76:aeb1df146756 379 /* Write to USART CR2 */
mbed_official 76:aeb1df146756 380 USARTx->CR2 = (uint16_t)tmpreg;
mbed_official 76:aeb1df146756 381 }
mbed_official 76:aeb1df146756 382
mbed_official 76:aeb1df146756 383 /**
mbed_official 76:aeb1df146756 384 * @brief Fills each USART_ClockInitStruct member with its default value.
mbed_official 76:aeb1df146756 385 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
mbed_official 76:aeb1df146756 386 * structure which will be initialized.
mbed_official 76:aeb1df146756 387 * @retval None
mbed_official 76:aeb1df146756 388 */
mbed_official 76:aeb1df146756 389 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
mbed_official 76:aeb1df146756 390 {
mbed_official 76:aeb1df146756 391 /* USART_ClockInitStruct members default value */
mbed_official 76:aeb1df146756 392 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
mbed_official 76:aeb1df146756 393 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
mbed_official 76:aeb1df146756 394 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
mbed_official 76:aeb1df146756 395 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
mbed_official 76:aeb1df146756 396 }
mbed_official 76:aeb1df146756 397
mbed_official 76:aeb1df146756 398 /**
mbed_official 76:aeb1df146756 399 * @brief Enables or disables the specified USART peripheral.
mbed_official 76:aeb1df146756 400 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 401 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 402 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 403 * @param NewState: new state of the USARTx peripheral.
mbed_official 76:aeb1df146756 404 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 405 * @retval None.
mbed_official 76:aeb1df146756 406 */
mbed_official 76:aeb1df146756 407 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 76:aeb1df146756 408 {
mbed_official 76:aeb1df146756 409 /* Check the parameters */
mbed_official 76:aeb1df146756 410 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 411 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 412
mbed_official 76:aeb1df146756 413 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 414 {
mbed_official 76:aeb1df146756 415 /* Enable the selected USART by setting the UE bit in the CR1 register */
mbed_official 76:aeb1df146756 416 USARTx->CR1 |= USART_CR1_UE;
mbed_official 76:aeb1df146756 417 }
mbed_official 76:aeb1df146756 418 else
mbed_official 76:aeb1df146756 419 {
mbed_official 76:aeb1df146756 420 /* Disable the selected USART by clearing the UE bit in the CR1 register */
mbed_official 76:aeb1df146756 421 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_UE);
mbed_official 76:aeb1df146756 422 }
mbed_official 76:aeb1df146756 423 }
mbed_official 76:aeb1df146756 424
mbed_official 76:aeb1df146756 425 /**
mbed_official 76:aeb1df146756 426 * @brief Sets the system clock prescaler.
mbed_official 76:aeb1df146756 427 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 428 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 429 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 430 * @param USART_Prescaler: specifies the prescaler clock.
mbed_official 76:aeb1df146756 431 * @note The function is used for IrDA mode with UART4 and UART5.
mbed_official 76:aeb1df146756 432 * @retval None.
mbed_official 76:aeb1df146756 433 */
mbed_official 76:aeb1df146756 434 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
mbed_official 76:aeb1df146756 435 {
mbed_official 76:aeb1df146756 436 /* Check the parameters */
mbed_official 76:aeb1df146756 437 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 438
mbed_official 76:aeb1df146756 439 /* Clear the USART prescaler */
mbed_official 76:aeb1df146756 440 USARTx->GTPR &= USART_GTPR_GT;
mbed_official 76:aeb1df146756 441 /* Set the USART prescaler */
mbed_official 76:aeb1df146756 442 USARTx->GTPR |= USART_Prescaler;
mbed_official 76:aeb1df146756 443 }
mbed_official 76:aeb1df146756 444
mbed_official 76:aeb1df146756 445 /**
mbed_official 76:aeb1df146756 446 * @brief Enables or disables the USART's 8x oversampling mode.
mbed_official 76:aeb1df146756 447 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 448 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 449 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 450 * @param NewState: new state of the USART 8x oversampling mode.
mbed_official 76:aeb1df146756 451 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 452 *
mbed_official 76:aeb1df146756 453 * @note
mbed_official 76:aeb1df146756 454 * This function has to be called before calling USART_Init()
mbed_official 76:aeb1df146756 455 * function in order to have correct baudrate Divider value.
mbed_official 76:aeb1df146756 456 * @retval None
mbed_official 76:aeb1df146756 457 */
mbed_official 76:aeb1df146756 458 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 76:aeb1df146756 459 {
mbed_official 76:aeb1df146756 460 /* Check the parameters */
mbed_official 76:aeb1df146756 461 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 462 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 463
mbed_official 76:aeb1df146756 464 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 465 {
mbed_official 76:aeb1df146756 466 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
mbed_official 76:aeb1df146756 467 USARTx->CR1 |= USART_CR1_OVER8;
mbed_official 76:aeb1df146756 468 }
mbed_official 76:aeb1df146756 469 else
mbed_official 76:aeb1df146756 470 {
mbed_official 76:aeb1df146756 471 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
mbed_official 76:aeb1df146756 472 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_OVER8);
mbed_official 76:aeb1df146756 473 }
mbed_official 76:aeb1df146756 474 }
mbed_official 76:aeb1df146756 475
mbed_official 76:aeb1df146756 476 /**
mbed_official 76:aeb1df146756 477 * @brief Enables or disables the USART's one bit sampling method.
mbed_official 76:aeb1df146756 478 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 479 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 480 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 481 * @param NewState: new state of the USART one bit sampling method.
mbed_official 76:aeb1df146756 482 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 483 * @retval None.
mbed_official 76:aeb1df146756 484 */
mbed_official 76:aeb1df146756 485 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 76:aeb1df146756 486 {
mbed_official 76:aeb1df146756 487 /* Check the parameters */
mbed_official 76:aeb1df146756 488 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 489 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 490
mbed_official 76:aeb1df146756 491 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 492 {
mbed_official 76:aeb1df146756 493 /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
mbed_official 76:aeb1df146756 494 USARTx->CR3 |= USART_CR3_ONEBIT;
mbed_official 76:aeb1df146756 495 }
mbed_official 76:aeb1df146756 496 else
mbed_official 76:aeb1df146756 497 {
mbed_official 76:aeb1df146756 498 /* Disable the one bit method by clearing the ONEBITE bit in the CR3 register */
mbed_official 76:aeb1df146756 499 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT);
mbed_official 76:aeb1df146756 500 }
mbed_official 76:aeb1df146756 501 }
mbed_official 76:aeb1df146756 502
mbed_official 76:aeb1df146756 503 /**
mbed_official 76:aeb1df146756 504 * @}
mbed_official 76:aeb1df146756 505 */
mbed_official 76:aeb1df146756 506
mbed_official 76:aeb1df146756 507 /** @defgroup USART_Group2 Data transfers functions
mbed_official 76:aeb1df146756 508 * @brief Data transfers functions
mbed_official 76:aeb1df146756 509 *
mbed_official 76:aeb1df146756 510 @verbatim
mbed_official 76:aeb1df146756 511 ===============================================================================
mbed_official 76:aeb1df146756 512 ##### Data transfers functions #####
mbed_official 76:aeb1df146756 513 ===============================================================================
mbed_official 76:aeb1df146756 514 [..] This subsection provides a set of functions allowing to manage
mbed_official 76:aeb1df146756 515 the USART data transfers.
mbed_official 76:aeb1df146756 516 [..] During an USART reception, data shifts in least significant bit first
mbed_official 76:aeb1df146756 517 through the RX pin. In this mode, the USART_DR register consists of
mbed_official 76:aeb1df146756 518 a buffer (RDR) between the internal bus and the received shift register.
mbed_official 76:aeb1df146756 519 When a transmission is taking place, a write instruction to
mbed_official 76:aeb1df146756 520 the USART_DR register stores the data in the TDR register and which is
mbed_official 76:aeb1df146756 521 copied in the shift register at the end of the current transmission.
mbed_official 76:aeb1df146756 522 [..] The read access of the USART_DR register can be done using
mbed_official 76:aeb1df146756 523 the USART_ReceiveData() function and returns the RDR buffered value.
mbed_official 76:aeb1df146756 524 Whereas a write access to the USART_DR can be done using USART_SendData()
mbed_official 76:aeb1df146756 525 function and stores the written data into TDR buffer.
mbed_official 76:aeb1df146756 526
mbed_official 76:aeb1df146756 527 @endverbatim
mbed_official 76:aeb1df146756 528 * @{
mbed_official 76:aeb1df146756 529 */
mbed_official 76:aeb1df146756 530
mbed_official 76:aeb1df146756 531 /**
mbed_official 76:aeb1df146756 532 * @brief Transmits single data through the USARTx peripheral.
mbed_official 76:aeb1df146756 533 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 534 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 535 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 536 * @param Data: the data to transmit.
mbed_official 76:aeb1df146756 537 * @retval None.
mbed_official 76:aeb1df146756 538 */
mbed_official 76:aeb1df146756 539 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
mbed_official 76:aeb1df146756 540 {
mbed_official 76:aeb1df146756 541 /* Check the parameters */
mbed_official 76:aeb1df146756 542 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 543 assert_param(IS_USART_DATA(Data));
mbed_official 76:aeb1df146756 544
mbed_official 76:aeb1df146756 545 /* Transmit Data */
mbed_official 76:aeb1df146756 546 USARTx->DR = (Data & (uint16_t)0x01FF);
mbed_official 76:aeb1df146756 547 }
mbed_official 76:aeb1df146756 548
mbed_official 76:aeb1df146756 549 /**
mbed_official 76:aeb1df146756 550 * @brief Returns the most recent received data by the USARTx peripheral.
mbed_official 76:aeb1df146756 551 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 552 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 553 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 554 * @retval The received data.
mbed_official 76:aeb1df146756 555 */
mbed_official 76:aeb1df146756 556 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
mbed_official 76:aeb1df146756 557 {
mbed_official 76:aeb1df146756 558 /* Check the parameters */
mbed_official 76:aeb1df146756 559 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 560
mbed_official 76:aeb1df146756 561 /* Receive Data */
mbed_official 76:aeb1df146756 562 return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
mbed_official 76:aeb1df146756 563 }
mbed_official 76:aeb1df146756 564
mbed_official 76:aeb1df146756 565 /**
mbed_official 76:aeb1df146756 566 * @}
mbed_official 76:aeb1df146756 567 */
mbed_official 76:aeb1df146756 568
mbed_official 76:aeb1df146756 569 /** @defgroup USART_Group3 MultiProcessor Communication functions
mbed_official 76:aeb1df146756 570 * @brief Multi-Processor Communication functions
mbed_official 76:aeb1df146756 571 *
mbed_official 76:aeb1df146756 572 @verbatim
mbed_official 76:aeb1df146756 573 ===============================================================================
mbed_official 76:aeb1df146756 574 ##### Multi-Processor Communication functions #####
mbed_official 76:aeb1df146756 575 ===============================================================================
mbed_official 76:aeb1df146756 576 [..] This subsection provides a set of functions allowing to manage the USART
mbed_official 76:aeb1df146756 577 multiprocessor communication.
mbed_official 76:aeb1df146756 578 [..] For instance one of the USARTs can be the master, its TX output is
mbed_official 76:aeb1df146756 579 connected to the RX input of the other USART. The others are slaves,
mbed_official 76:aeb1df146756 580 their respective TX outputs are logically ANDed together and connected
mbed_official 76:aeb1df146756 581 to the RX input of the master. USART multiprocessor communication is
mbed_official 76:aeb1df146756 582 possible through the following procedure:
mbed_official 76:aeb1df146756 583 (#) Program the Baud rate, Word length = 9 bits, Stop bits, Parity,
mbed_official 76:aeb1df146756 584 Mode transmitter or Mode receiver and hardware flow control values
mbed_official 76:aeb1df146756 585 using the USART_Init() function.
mbed_official 76:aeb1df146756 586 (#) Configures the USART address using the USART_SetAddress() function.
mbed_official 76:aeb1df146756 587 (#) Configures the wake up methode (USART_WakeUp_IdleLine or
mbed_official 76:aeb1df146756 588 USART_WakeUp_AddressMark) using USART_WakeUpConfig() function only
mbed_official 76:aeb1df146756 589 for the slaves.
mbed_official 76:aeb1df146756 590 (#) Enable the USART using the USART_Cmd() function.
mbed_official 76:aeb1df146756 591 (#) Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd()
mbed_official 76:aeb1df146756 592 function.
mbed_official 76:aeb1df146756 593
mbed_official 76:aeb1df146756 594 [..] The USART Slave exit from mute mode when receive the wake up condition.
mbed_official 76:aeb1df146756 595
mbed_official 76:aeb1df146756 596 @endverbatim
mbed_official 76:aeb1df146756 597 * @{
mbed_official 76:aeb1df146756 598 */
mbed_official 76:aeb1df146756 599
mbed_official 76:aeb1df146756 600 /**
mbed_official 76:aeb1df146756 601 * @brief Sets the address of the USART node.
mbed_official 76:aeb1df146756 602 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 603 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 604 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 605 * @param USART_Address: Indicates the address of the USART node.
mbed_official 76:aeb1df146756 606 * @retval None
mbed_official 76:aeb1df146756 607 */
mbed_official 76:aeb1df146756 608 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
mbed_official 76:aeb1df146756 609 {
mbed_official 76:aeb1df146756 610 /* Check the parameters */
mbed_official 76:aeb1df146756 611 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 612 assert_param(IS_USART_ADDRESS(USART_Address));
mbed_official 76:aeb1df146756 613
mbed_official 76:aeb1df146756 614 /* Clear the USART address */
mbed_official 76:aeb1df146756 615 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_ADD);
mbed_official 76:aeb1df146756 616 /* Set the USART address node */
mbed_official 76:aeb1df146756 617 USARTx->CR2 |= USART_Address;
mbed_official 76:aeb1df146756 618 }
mbed_official 76:aeb1df146756 619
mbed_official 76:aeb1df146756 620 /**
mbed_official 76:aeb1df146756 621 * @brief Determines if the USART is in mute mode or not.
mbed_official 76:aeb1df146756 622 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 623 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 624 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 625 * @param NewState: new state of the USART mute mode.
mbed_official 76:aeb1df146756 626 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 627 * @retval None
mbed_official 76:aeb1df146756 628 */
mbed_official 76:aeb1df146756 629 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 76:aeb1df146756 630 {
mbed_official 76:aeb1df146756 631 /* Check the parameters */
mbed_official 76:aeb1df146756 632 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 633 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 634
mbed_official 76:aeb1df146756 635 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 636 {
mbed_official 76:aeb1df146756 637 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
mbed_official 76:aeb1df146756 638 USARTx->CR1 |= USART_CR1_RWU;
mbed_official 76:aeb1df146756 639 }
mbed_official 76:aeb1df146756 640 else
mbed_official 76:aeb1df146756 641 {
mbed_official 76:aeb1df146756 642 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
mbed_official 76:aeb1df146756 643 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_RWU);
mbed_official 76:aeb1df146756 644 }
mbed_official 76:aeb1df146756 645 }
mbed_official 76:aeb1df146756 646 /**
mbed_official 76:aeb1df146756 647 * @brief Selects the USART WakeUp method.
mbed_official 76:aeb1df146756 648 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 649 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 650 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 651 * @param USART_WakeUp: specifies the USART wakeup method.
mbed_official 76:aeb1df146756 652 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 653 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection.
mbed_official 76:aeb1df146756 654 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark.
mbed_official 76:aeb1df146756 655 * @retval None.
mbed_official 76:aeb1df146756 656 */
mbed_official 76:aeb1df146756 657 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
mbed_official 76:aeb1df146756 658 {
mbed_official 76:aeb1df146756 659 /* Check the parameters */
mbed_official 76:aeb1df146756 660 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 661 assert_param(IS_USART_WAKEUP(USART_WakeUp));
mbed_official 76:aeb1df146756 662
mbed_official 76:aeb1df146756 663 USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_WAKE);
mbed_official 76:aeb1df146756 664 USARTx->CR1 |= USART_WakeUp;
mbed_official 76:aeb1df146756 665 }
mbed_official 76:aeb1df146756 666
mbed_official 76:aeb1df146756 667 /**
mbed_official 76:aeb1df146756 668 * @}
mbed_official 76:aeb1df146756 669 */
mbed_official 76:aeb1df146756 670
mbed_official 76:aeb1df146756 671 /** @defgroup USART_Group4 LIN mode functions
mbed_official 76:aeb1df146756 672 * @brief LIN mode functions
mbed_official 76:aeb1df146756 673 *
mbed_official 76:aeb1df146756 674 @verbatim
mbed_official 76:aeb1df146756 675 ===============================================================================
mbed_official 76:aeb1df146756 676 ##### LIN mode functions #####
mbed_official 76:aeb1df146756 677 ===============================================================================
mbed_official 76:aeb1df146756 678 [..] This subsection provides a set of functions allowing to manage the USART
mbed_official 76:aeb1df146756 679 LIN Mode communication.
mbed_official 76:aeb1df146756 680 [..] In LIN mode, 8-bit data format with 1 stop bit is required in accordance
mbed_official 76:aeb1df146756 681 with the LIN standard.
mbed_official 76:aeb1df146756 682 [..] Only this LIN Feature is supported by the USART IP:
mbed_official 76:aeb1df146756 683 (+) LIN Master Synchronous Break send capability and LIN slave break
mbed_official 76:aeb1df146756 684 detection capability : 13-bit break generation and 10/11 bit break
mbed_official 76:aeb1df146756 685 detection.
mbed_official 76:aeb1df146756 686 [..] USART LIN Master transmitter communication is possible through the
mbed_official 76:aeb1df146756 687 following procedure:
mbed_official 76:aeb1df146756 688 (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
mbed_official 76:aeb1df146756 689 Mode transmitter or Mode receiver and hardware flow control values
mbed_official 76:aeb1df146756 690 using the USART_Init() function.
mbed_official 76:aeb1df146756 691 (#) Enable the USART using the USART_Cmd() function.
mbed_official 76:aeb1df146756 692 (#) Enable the LIN mode using the USART_LINCmd() function.
mbed_official 76:aeb1df146756 693 (#) Send the break character using USART_SendBreak() function.
mbed_official 76:aeb1df146756 694 [..] USART LIN Master receiver communication is possible through the
mbed_official 76:aeb1df146756 695 following procedure:
mbed_official 76:aeb1df146756 696 (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
mbed_official 76:aeb1df146756 697 Mode transmitter or Mode receiver and hardware flow control values
mbed_official 76:aeb1df146756 698 using the USART_Init() function.
mbed_official 76:aeb1df146756 699 (#) Enable the USART using the USART_Cmd() function.
mbed_official 76:aeb1df146756 700 (#) Configures the break detection length
mbed_official 76:aeb1df146756 701 using the USART_LINBreakDetectLengthConfig() function.
mbed_official 76:aeb1df146756 702 (#) Enable the LIN mode using the USART_LINCmd() function.
mbed_official 76:aeb1df146756 703 -@- In LIN mode, the following bits must be kept cleared:
mbed_official 76:aeb1df146756 704 (+@) CLKEN in the USART_CR2 register.
mbed_official 76:aeb1df146756 705 (+@) STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register.
mbed_official 76:aeb1df146756 706
mbed_official 76:aeb1df146756 707 @endverbatim
mbed_official 76:aeb1df146756 708 * @{
mbed_official 76:aeb1df146756 709 */
mbed_official 76:aeb1df146756 710
mbed_official 76:aeb1df146756 711 /**
mbed_official 76:aeb1df146756 712 * @brief Sets the USART LIN Break detection length.
mbed_official 76:aeb1df146756 713 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 714 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 715 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 716 * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
mbed_official 76:aeb1df146756 717 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 718 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection.
mbed_official 76:aeb1df146756 719 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection.
mbed_official 76:aeb1df146756 720 * @retval None.
mbed_official 76:aeb1df146756 721 */
mbed_official 76:aeb1df146756 722 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
mbed_official 76:aeb1df146756 723 {
mbed_official 76:aeb1df146756 724 /* Check the parameters */
mbed_official 76:aeb1df146756 725 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 726 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
mbed_official 76:aeb1df146756 727
mbed_official 76:aeb1df146756 728 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LBDL);
mbed_official 76:aeb1df146756 729 USARTx->CR2 |= USART_LINBreakDetectLength;
mbed_official 76:aeb1df146756 730 }
mbed_official 76:aeb1df146756 731
mbed_official 76:aeb1df146756 732 /**
mbed_official 76:aeb1df146756 733 * @brief Enables or disables the USART's LIN mode.
mbed_official 76:aeb1df146756 734 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 735 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 736 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 737 * @param NewState: new state of the USART LIN mode.
mbed_official 76:aeb1df146756 738 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 739 * @retval None.
mbed_official 76:aeb1df146756 740 */
mbed_official 76:aeb1df146756 741 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 76:aeb1df146756 742 {
mbed_official 76:aeb1df146756 743 /* Check the parameters */
mbed_official 76:aeb1df146756 744 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 745 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 746
mbed_official 76:aeb1df146756 747 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 748 {
mbed_official 76:aeb1df146756 749 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
mbed_official 76:aeb1df146756 750 USARTx->CR2 |= USART_CR2_LINEN;
mbed_official 76:aeb1df146756 751 }
mbed_official 76:aeb1df146756 752 else
mbed_official 76:aeb1df146756 753 {
mbed_official 76:aeb1df146756 754 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
mbed_official 76:aeb1df146756 755 USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LINEN);
mbed_official 76:aeb1df146756 756 }
mbed_official 76:aeb1df146756 757 }
mbed_official 76:aeb1df146756 758
mbed_official 76:aeb1df146756 759 /**
mbed_official 76:aeb1df146756 760 * @brief Transmits break characters.
mbed_official 76:aeb1df146756 761 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 762 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 763 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 764 * @retval None.
mbed_official 76:aeb1df146756 765 */
mbed_official 76:aeb1df146756 766 void USART_SendBreak(USART_TypeDef* USARTx)
mbed_official 76:aeb1df146756 767 {
mbed_official 76:aeb1df146756 768 /* Check the parameters */
mbed_official 76:aeb1df146756 769 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 770
mbed_official 76:aeb1df146756 771 /* Send break characters */
mbed_official 76:aeb1df146756 772 USARTx->CR1 |= USART_CR1_SBK;
mbed_official 76:aeb1df146756 773 }
mbed_official 76:aeb1df146756 774
mbed_official 76:aeb1df146756 775 /**
mbed_official 76:aeb1df146756 776 * @}
mbed_official 76:aeb1df146756 777 */
mbed_official 76:aeb1df146756 778
mbed_official 76:aeb1df146756 779 /** @defgroup USART_Group5 Halfduplex mode function
mbed_official 76:aeb1df146756 780 * @brief Half-duplex mode function
mbed_official 76:aeb1df146756 781 *
mbed_official 76:aeb1df146756 782 @verbatim
mbed_official 76:aeb1df146756 783 ===============================================================================
mbed_official 76:aeb1df146756 784 ##### Half-duplex mode function #####
mbed_official 76:aeb1df146756 785 ===============================================================================
mbed_official 76:aeb1df146756 786 [..] This subsection provides a set of functions allowing to manage the USART
mbed_official 76:aeb1df146756 787 Half-duplex communication.
mbed_official 76:aeb1df146756 788 [..] The USART can be configured to follow a single-wire half-duplex protocol
mbed_official 76:aeb1df146756 789 where the TX and RX lines are internally connected.
mbed_official 76:aeb1df146756 790 [..] USART Half duplex communication is possible through the following procedure:
mbed_official 76:aeb1df146756 791 (#) Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter
mbed_official 76:aeb1df146756 792 or Mode receiver and hardware flow control values using the USART_Init()
mbed_official 76:aeb1df146756 793 function.
mbed_official 76:aeb1df146756 794 (#) Configures the USART address using the USART_SetAddress() function.
mbed_official 76:aeb1df146756 795 (#) Enable the USART using the USART_Cmd() function.
mbed_official 76:aeb1df146756 796 (#) Enable the half duplex mode using USART_HalfDuplexCmd() function.
mbed_official 76:aeb1df146756 797 -@- The RX pin is no longer used.
mbed_official 76:aeb1df146756 798 -@- In Half-duplex mode the following bits must be kept cleared:
mbed_official 76:aeb1df146756 799 (+@) LINEN and CLKEN bits in the USART_CR2 register.
mbed_official 76:aeb1df146756 800 (+@) SCEN and IREN bits in the USART_CR3 register.
mbed_official 76:aeb1df146756 801
mbed_official 76:aeb1df146756 802 @endverbatim
mbed_official 76:aeb1df146756 803 * @{
mbed_official 76:aeb1df146756 804 */
mbed_official 76:aeb1df146756 805
mbed_official 76:aeb1df146756 806 /**
mbed_official 76:aeb1df146756 807 * @brief Enables or disables the USART's Half Duplex communication.
mbed_official 76:aeb1df146756 808 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 809 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 810 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 811 * @param NewState: new state of the USART Communication.
mbed_official 76:aeb1df146756 812 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 813 * @retval None
mbed_official 76:aeb1df146756 814 */
mbed_official 76:aeb1df146756 815 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 76:aeb1df146756 816 {
mbed_official 76:aeb1df146756 817 /* Check the parameters */
mbed_official 76:aeb1df146756 818 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 819 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 820
mbed_official 76:aeb1df146756 821 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 822 {
mbed_official 76:aeb1df146756 823 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
mbed_official 76:aeb1df146756 824 USARTx->CR3 |= USART_CR3_HDSEL;
mbed_official 76:aeb1df146756 825 }
mbed_official 76:aeb1df146756 826 else
mbed_official 76:aeb1df146756 827 {
mbed_official 76:aeb1df146756 828 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
mbed_official 76:aeb1df146756 829 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_HDSEL);
mbed_official 76:aeb1df146756 830 }
mbed_official 76:aeb1df146756 831 }
mbed_official 76:aeb1df146756 832
mbed_official 76:aeb1df146756 833 /**
mbed_official 76:aeb1df146756 834 * @}
mbed_official 76:aeb1df146756 835 */
mbed_official 76:aeb1df146756 836
mbed_official 76:aeb1df146756 837
mbed_official 76:aeb1df146756 838 /** @defgroup USART_Group6 Smartcard mode functions
mbed_official 76:aeb1df146756 839 * @brief Smartcard mode functions
mbed_official 76:aeb1df146756 840 *
mbed_official 76:aeb1df146756 841 @verbatim
mbed_official 76:aeb1df146756 842 ===============================================================================
mbed_official 76:aeb1df146756 843 ##### Smartcard mode functions #####
mbed_official 76:aeb1df146756 844 ===============================================================================
mbed_official 76:aeb1df146756 845 [..] This subsection provides a set of functions allowing to manage the USART
mbed_official 76:aeb1df146756 846 Smartcard communication.
mbed_official 76:aeb1df146756 847 [..] The Smartcard interface is designed to support asynchronous protocol
mbed_official 76:aeb1df146756 848 Smartcards as defined in the ISO 7816-3 standard. The USART can provide
mbed_official 76:aeb1df146756 849 a clock to the smartcard through the SCLK output. In smartcard mode,
mbed_official 76:aeb1df146756 850 SCLK is not associated to the communication but is simply derived from
mbed_official 76:aeb1df146756 851 the internal peripheral input clock through a 5-bit prescaler.
mbed_official 76:aeb1df146756 852 [..] Smartcard communication is possible through the following procedure:
mbed_official 76:aeb1df146756 853 (#) Configures the Smartcard Prsecaler using the USART_SetPrescaler()
mbed_official 76:aeb1df146756 854 function.
mbed_official 76:aeb1df146756 855 (#) Configures the Smartcard Guard Time using the USART_SetGuardTime()
mbed_official 76:aeb1df146756 856 function.
mbed_official 76:aeb1df146756 857 (#) Program the USART clock using the USART_ClockInit() function as following:
mbed_official 76:aeb1df146756 858 (++) USART Clock enabled.
mbed_official 76:aeb1df146756 859 (++) USART CPOL Low.
mbed_official 76:aeb1df146756 860 (++) USART CPHA on first edge.
mbed_official 76:aeb1df146756 861 (++) USART Last Bit Clock Enabled.
mbed_official 76:aeb1df146756 862 (#) Program the Smartcard interface using the USART_Init() function as
mbed_official 76:aeb1df146756 863 following:
mbed_official 76:aeb1df146756 864 (++) Word Length = 9 Bits.
mbed_official 76:aeb1df146756 865 (++) 1.5 Stop Bit.
mbed_official 76:aeb1df146756 866 (++) Even parity.
mbed_official 76:aeb1df146756 867 (++) BaudRate = 12096 baud.
mbed_official 76:aeb1df146756 868 (++) Hardware flow control disabled (RTS and CTS signals).
mbed_official 76:aeb1df146756 869 (++) Tx and Rx enabled
mbed_official 76:aeb1df146756 870 (#) Optionally you can enable the parity error interrupt using
mbed_official 76:aeb1df146756 871 the USART_ITConfig() function.
mbed_official 76:aeb1df146756 872 (#) Enable the USART using the USART_Cmd() function.
mbed_official 76:aeb1df146756 873 (#) Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
mbed_official 76:aeb1df146756 874 (#) Enable the Smartcard interface using the USART_SmartCardCmd() function.
mbed_official 76:aeb1df146756 875 [..]
mbed_official 76:aeb1df146756 876 Please refer to the ISO 7816-3 specification for more details.
mbed_official 76:aeb1df146756 877 [..]
mbed_official 76:aeb1df146756 878 (@) It is also possible to choose 0.5 stop bit for receiving but it is
mbed_official 76:aeb1df146756 879 recommended to use 1.5 stop bits for both transmitting and receiving
mbed_official 76:aeb1df146756 880 to avoid switching between the two configurations.
mbed_official 76:aeb1df146756 881 (@) In smartcard mode, the following bits must be kept cleared:
mbed_official 76:aeb1df146756 882 (+@) LINEN bit in the USART_CR2 register.
mbed_official 76:aeb1df146756 883 (+@) HDSEL and IREN bits in the USART_CR3 register.
mbed_official 76:aeb1df146756 884
mbed_official 76:aeb1df146756 885 @endverbatim
mbed_official 76:aeb1df146756 886 * @{
mbed_official 76:aeb1df146756 887 */
mbed_official 76:aeb1df146756 888
mbed_official 76:aeb1df146756 889 /**
mbed_official 76:aeb1df146756 890 * @brief Sets the specified USART guard time.
mbed_official 76:aeb1df146756 891 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 892 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 893 * USART1, USART2 or USART3.
mbed_official 76:aeb1df146756 894 * @param USART_GuardTime: specifies the guard time.
mbed_official 76:aeb1df146756 895 * @retval None.
mbed_official 76:aeb1df146756 896 */
mbed_official 76:aeb1df146756 897 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
mbed_official 76:aeb1df146756 898 {
mbed_official 76:aeb1df146756 899 /* Check the parameters */
mbed_official 76:aeb1df146756 900 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 76:aeb1df146756 901
mbed_official 76:aeb1df146756 902 /* Clear the USART Guard time */
mbed_official 76:aeb1df146756 903 USARTx->GTPR &= USART_GTPR_PSC;
mbed_official 76:aeb1df146756 904 /* Set the USART guard time */
mbed_official 76:aeb1df146756 905 USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
mbed_official 76:aeb1df146756 906 }
mbed_official 76:aeb1df146756 907
mbed_official 76:aeb1df146756 908 /**
mbed_official 76:aeb1df146756 909 * @brief Enables or disables the USART's Smart Card mode.
mbed_official 76:aeb1df146756 910 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 911 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 912 * USART1, USART2 or USART3.
mbed_official 76:aeb1df146756 913 * @param NewState: new state of the Smart Card mode.
mbed_official 76:aeb1df146756 914 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 915 * @retval None
mbed_official 76:aeb1df146756 916 */
mbed_official 76:aeb1df146756 917 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 76:aeb1df146756 918 {
mbed_official 76:aeb1df146756 919 /* Check the parameters */
mbed_official 76:aeb1df146756 920 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 76:aeb1df146756 921 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 922 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 923 {
mbed_official 76:aeb1df146756 924 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
mbed_official 76:aeb1df146756 925 USARTx->CR3 |= USART_CR3_SCEN;
mbed_official 76:aeb1df146756 926 }
mbed_official 76:aeb1df146756 927 else
mbed_official 76:aeb1df146756 928 {
mbed_official 76:aeb1df146756 929 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
mbed_official 76:aeb1df146756 930 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_SCEN);
mbed_official 76:aeb1df146756 931 }
mbed_official 76:aeb1df146756 932 }
mbed_official 76:aeb1df146756 933
mbed_official 76:aeb1df146756 934 /**
mbed_official 76:aeb1df146756 935 * @brief Enables or disables NACK transmission.
mbed_official 76:aeb1df146756 936 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 937 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 938 * USART1, USART2 or USART3.
mbed_official 76:aeb1df146756 939 * @param NewState: new state of the NACK transmission.
mbed_official 76:aeb1df146756 940 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 941 * @retval None.
mbed_official 76:aeb1df146756 942 */
mbed_official 76:aeb1df146756 943 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 76:aeb1df146756 944 {
mbed_official 76:aeb1df146756 945 /* Check the parameters */
mbed_official 76:aeb1df146756 946 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 76:aeb1df146756 947 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 948 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 949 {
mbed_official 76:aeb1df146756 950 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
mbed_official 76:aeb1df146756 951 USARTx->CR3 |= USART_CR3_NACK;
mbed_official 76:aeb1df146756 952 }
mbed_official 76:aeb1df146756 953 else
mbed_official 76:aeb1df146756 954 {
mbed_official 76:aeb1df146756 955 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
mbed_official 76:aeb1df146756 956 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_NACK);
mbed_official 76:aeb1df146756 957 }
mbed_official 76:aeb1df146756 958 }
mbed_official 76:aeb1df146756 959
mbed_official 76:aeb1df146756 960 /**
mbed_official 76:aeb1df146756 961 * @}
mbed_official 76:aeb1df146756 962 */
mbed_official 76:aeb1df146756 963
mbed_official 76:aeb1df146756 964 /** @defgroup USART_Group7 IrDA mode functions
mbed_official 76:aeb1df146756 965 * @brief IrDA mode functions
mbed_official 76:aeb1df146756 966 *
mbed_official 76:aeb1df146756 967 @verbatim
mbed_official 76:aeb1df146756 968 ===============================================================================
mbed_official 76:aeb1df146756 969 ##### IrDA mode functions #####
mbed_official 76:aeb1df146756 970 ===============================================================================
mbed_official 76:aeb1df146756 971 [..] This subsection provides a set of functions allowing to manage the USART
mbed_official 76:aeb1df146756 972 IrDA communication.
mbed_official 76:aeb1df146756 973 [..] IrDA is a half duplex communication protocol. If the Transmitter is busy,
mbed_official 76:aeb1df146756 974 any data on the IrDA receive line will be ignored by the IrDA decoder
mbed_official 76:aeb1df146756 975 and if the Receiver is busy, data on the TX from the USART to IrDA will
mbed_official 76:aeb1df146756 976 not be encoded by IrDA. While receiving data, transmission should be
mbed_official 76:aeb1df146756 977 avoided as the data to be transmitted could be corrupted.
mbed_official 76:aeb1df146756 978
mbed_official 76:aeb1df146756 979 [..] IrDA communication is possible through the following procedure:
mbed_official 76:aeb1df146756 980 (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity,
mbed_official 76:aeb1df146756 981 Transmitter/Receiver modes and hardware flow control values using
mbed_official 76:aeb1df146756 982 the USART_Init() function.
mbed_official 76:aeb1df146756 983 (#) Enable the USART using the USART_Cmd() function.
mbed_official 76:aeb1df146756 984 (#) Configures the IrDA pulse width by configuring the prescaler using
mbed_official 76:aeb1df146756 985 the USART_SetPrescaler() function.
mbed_official 76:aeb1df146756 986 (#) Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal
mbed_official 76:aeb1df146756 987 mode using the USART_IrDAConfig() function.
mbed_official 76:aeb1df146756 988 (#) Enable the IrDA using the USART_IrDACmd() function.
mbed_official 76:aeb1df146756 989
mbed_official 76:aeb1df146756 990 [..]
mbed_official 76:aeb1df146756 991 (@) A pulse of width less than two and greater than one PSC period(s) may or
mbed_official 76:aeb1df146756 992 may not be rejected.
mbed_official 76:aeb1df146756 993 (@) The receiver set up time should be managed by software. The IrDA physical
mbed_official 76:aeb1df146756 994 layer specification specifies a minimum of 10 ms delay between
mbed_official 76:aeb1df146756 995 transmission and reception (IrDA is a half duplex protocol).
mbed_official 76:aeb1df146756 996 (@) In IrDA mode, the following bits must be kept cleared:
mbed_official 76:aeb1df146756 997 (+@) LINEN, STOP and CLKEN bits in the USART_CR2 register.
mbed_official 76:aeb1df146756 998 (+@) SCEN and HDSEL bits in the USART_CR3 register.
mbed_official 76:aeb1df146756 999
mbed_official 76:aeb1df146756 1000 @endverbatim
mbed_official 76:aeb1df146756 1001 * @{
mbed_official 76:aeb1df146756 1002 */
mbed_official 76:aeb1df146756 1003
mbed_official 76:aeb1df146756 1004 /**
mbed_official 76:aeb1df146756 1005 * @brief Configures the USART's IrDA interface.
mbed_official 76:aeb1df146756 1006 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 1007 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1008 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 1009 * @param USART_IrDAMode: specifies the IrDA mode.
mbed_official 76:aeb1df146756 1010 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1011 * @arg USART_IrDAMode_LowPower: USART IrDA Low Power mode selected.
mbed_official 76:aeb1df146756 1012 * @arg USART_IrDAMode_Normal: USART IrDA Normal mode selected.
mbed_official 76:aeb1df146756 1013 * @retval None
mbed_official 76:aeb1df146756 1014 */
mbed_official 76:aeb1df146756 1015 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
mbed_official 76:aeb1df146756 1016 {
mbed_official 76:aeb1df146756 1017 /* Check the parameters */
mbed_official 76:aeb1df146756 1018 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1019 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
mbed_official 76:aeb1df146756 1020
mbed_official 76:aeb1df146756 1021 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IRLP);
mbed_official 76:aeb1df146756 1022 USARTx->CR3 |= USART_IrDAMode;
mbed_official 76:aeb1df146756 1023 }
mbed_official 76:aeb1df146756 1024
mbed_official 76:aeb1df146756 1025 /**
mbed_official 76:aeb1df146756 1026 * @brief Enables or disables the USART's IrDA interface.
mbed_official 76:aeb1df146756 1027 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 1028 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1029 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 1030 * @param NewState: new state of the IrDA mode.
mbed_official 76:aeb1df146756 1031 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 1032 * @retval None
mbed_official 76:aeb1df146756 1033 */
mbed_official 76:aeb1df146756 1034 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 76:aeb1df146756 1035 {
mbed_official 76:aeb1df146756 1036 /* Check the parameters */
mbed_official 76:aeb1df146756 1037 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1038 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 1039
mbed_official 76:aeb1df146756 1040 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 1041 {
mbed_official 76:aeb1df146756 1042 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
mbed_official 76:aeb1df146756 1043 USARTx->CR3 |= USART_CR3_IREN;
mbed_official 76:aeb1df146756 1044 }
mbed_official 76:aeb1df146756 1045 else
mbed_official 76:aeb1df146756 1046 {
mbed_official 76:aeb1df146756 1047 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
mbed_official 76:aeb1df146756 1048 USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IREN);
mbed_official 76:aeb1df146756 1049 }
mbed_official 76:aeb1df146756 1050 }
mbed_official 76:aeb1df146756 1051
mbed_official 76:aeb1df146756 1052 /**
mbed_official 76:aeb1df146756 1053 * @}
mbed_official 76:aeb1df146756 1054 */
mbed_official 76:aeb1df146756 1055
mbed_official 76:aeb1df146756 1056 /** @defgroup USART_Group8 DMA transfers management functions
mbed_official 76:aeb1df146756 1057 * @brief DMA transfers management functions
mbed_official 76:aeb1df146756 1058 *
mbed_official 76:aeb1df146756 1059 @verbatim
mbed_official 76:aeb1df146756 1060 ===============================================================================
mbed_official 76:aeb1df146756 1061 ##### DMA transfers management functions #####
mbed_official 76:aeb1df146756 1062 ===============================================================================
mbed_official 76:aeb1df146756 1063
mbed_official 76:aeb1df146756 1064 @endverbatim
mbed_official 76:aeb1df146756 1065 * @{
mbed_official 76:aeb1df146756 1066 */
mbed_official 76:aeb1df146756 1067
mbed_official 76:aeb1df146756 1068 /**
mbed_official 76:aeb1df146756 1069 * @brief Enables or disables the USART's DMA interface.
mbed_official 76:aeb1df146756 1070 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 1071 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1072 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 1073 * @param USART_DMAReq: specifies the DMA request.
mbed_official 76:aeb1df146756 1074 * This parameter can be any combination of the following values:
mbed_official 76:aeb1df146756 1075 * @arg USART_DMAReq_Tx: USART DMA transmit request.
mbed_official 76:aeb1df146756 1076 * @arg USART_DMAReq_Rx: USART DMA receive request.
mbed_official 76:aeb1df146756 1077 * @param NewState: new state of the DMA Request sources.
mbed_official 76:aeb1df146756 1078 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 1079 * @retval None
mbed_official 76:aeb1df146756 1080 */
mbed_official 76:aeb1df146756 1081 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
mbed_official 76:aeb1df146756 1082 {
mbed_official 76:aeb1df146756 1083 /* Check the parameters */
mbed_official 76:aeb1df146756 1084 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1085 assert_param(IS_USART_DMAREQ(USART_DMAReq));
mbed_official 76:aeb1df146756 1086 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 1087
mbed_official 76:aeb1df146756 1088 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 1089 {
mbed_official 76:aeb1df146756 1090 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
mbed_official 76:aeb1df146756 1091 DMAR bits in the USART CR3 register */
mbed_official 76:aeb1df146756 1092 USARTx->CR3 |= USART_DMAReq;
mbed_official 76:aeb1df146756 1093 }
mbed_official 76:aeb1df146756 1094 else
mbed_official 76:aeb1df146756 1095 {
mbed_official 76:aeb1df146756 1096 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
mbed_official 76:aeb1df146756 1097 DMAR bits in the USART CR3 register */
mbed_official 76:aeb1df146756 1098 USARTx->CR3 &= (uint16_t)~USART_DMAReq;
mbed_official 76:aeb1df146756 1099 }
mbed_official 76:aeb1df146756 1100 }
mbed_official 76:aeb1df146756 1101
mbed_official 76:aeb1df146756 1102 /**
mbed_official 76:aeb1df146756 1103 * @}
mbed_official 76:aeb1df146756 1104 */
mbed_official 76:aeb1df146756 1105
mbed_official 76:aeb1df146756 1106 /** @defgroup USART_Group9 Interrupts and flags management functions
mbed_official 76:aeb1df146756 1107 * @brief Interrupts and flags management functions
mbed_official 76:aeb1df146756 1108 *
mbed_official 76:aeb1df146756 1109 @verbatim
mbed_official 76:aeb1df146756 1110 ===============================================================================
mbed_official 76:aeb1df146756 1111 ##### Interrupts and flags management functions #####
mbed_official 76:aeb1df146756 1112 ===============================================================================
mbed_official 76:aeb1df146756 1113 [..] This subsection provides a set of functions allowing to configure the
mbed_official 76:aeb1df146756 1114 USART Interrupts sources, DMA channels requests and check or clear the
mbed_official 76:aeb1df146756 1115 flags or pending bits status. The user should identify which mode will
mbed_official 76:aeb1df146756 1116 be used in his application to manage the communication: Polling mode,
mbed_official 76:aeb1df146756 1117 Interrupt mode or DMA mode.
mbed_official 76:aeb1df146756 1118 *** Polling Mode ***
mbed_official 76:aeb1df146756 1119 ====================
mbed_official 76:aeb1df146756 1120 [..] In Polling Mode, the SPI communication can be managed by 10 flags:
mbed_official 76:aeb1df146756 1121 (#) USART_FLAG_TXE: to indicate the status of the transmit buffer register.
mbed_official 76:aeb1df146756 1122 (#) USART_FLAG_RXNE: to indicate the status of the receive buffer register.
mbed_official 76:aeb1df146756 1123 (#) USART_FLAG_TC: to indicate the status of the transmit operation.
mbed_official 76:aeb1df146756 1124 (#) USART_FLAG_IDLE: to indicate the status of the Idle Line.
mbed_official 76:aeb1df146756 1125 (#) USART_FLAG_CTS: to indicate the status of the nCTS input.
mbed_official 76:aeb1df146756 1126 (#) USART_FLAG_LBD: to indicate the status of the LIN break detection.
mbed_official 76:aeb1df146756 1127 (#) USART_FLAG_NE: to indicate if a noise error occur.
mbed_official 76:aeb1df146756 1128 (#) USART_FLAG_FE: to indicate if a frame error occur.
mbed_official 76:aeb1df146756 1129 (#) USART_FLAG_PE: to indicate if a parity error occur.
mbed_official 76:aeb1df146756 1130 (#) USART_FLAG_ORE: to indicate if an Overrun error occur.
mbed_official 76:aeb1df146756 1131 [..] In this Mode it is advised to use the following functions:
mbed_official 76:aeb1df146756 1132 (+) FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG).
mbed_official 76:aeb1df146756 1133 (+) void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG).
mbed_official 76:aeb1df146756 1134
mbed_official 76:aeb1df146756 1135 *** Interrupt Mode ***
mbed_official 76:aeb1df146756 1136 ======================
mbed_official 76:aeb1df146756 1137 [..] In Interrupt Mode, the USART communication can be managed by 8 interrupt
mbed_official 76:aeb1df146756 1138 sources and 10 pending bits:
mbed_official 76:aeb1df146756 1139 (+) Pending Bits:
mbed_official 76:aeb1df146756 1140 (##) USART_IT_TXE: to indicate the status of the transmit buffer
mbed_official 76:aeb1df146756 1141 register.
mbed_official 76:aeb1df146756 1142 (##) USART_IT_RXNE: to indicate the status of the receive buffer
mbed_official 76:aeb1df146756 1143 register.
mbed_official 76:aeb1df146756 1144 (##) USART_IT_TC: to indicate the status of the transmit operation.
mbed_official 76:aeb1df146756 1145 (##) USART_IT_IDLE: to indicate the status of the Idle Line.
mbed_official 76:aeb1df146756 1146 (##) USART_IT_CTS: to indicate the status of the nCTS input.
mbed_official 76:aeb1df146756 1147 (##) USART_IT_LBD: to indicate the status of the LIN break detection.
mbed_official 76:aeb1df146756 1148 (##) USART_IT_NE: to indicate if a noise error occur.
mbed_official 76:aeb1df146756 1149 (##) USART_IT_FE: to indicate if a frame error occur.
mbed_official 76:aeb1df146756 1150 (##) USART_IT_PE: to indicate if a parity error occur.
mbed_official 76:aeb1df146756 1151 (##) USART_IT_ORE: to indicate if an Overrun error occur
mbed_official 76:aeb1df146756 1152 (if the RXNEIE or EIE bits are set).
mbed_official 76:aeb1df146756 1153
mbed_official 76:aeb1df146756 1154 (+) Interrupt Source:
mbed_official 76:aeb1df146756 1155 (##) USART_IT_TXE: specifies the interrupt source for the Tx buffer
mbed_official 76:aeb1df146756 1156 empty interrupt.
mbed_official 76:aeb1df146756 1157 (##) USART_IT_RXNE: specifies the interrupt source for the Rx buffer
mbed_official 76:aeb1df146756 1158 not empty interrupt.
mbed_official 76:aeb1df146756 1159 (##) USART_IT_TC: specifies the interrupt source for the Transmit
mbed_official 76:aeb1df146756 1160 complete interrupt.
mbed_official 76:aeb1df146756 1161 (##) USART_IT_IDLE: specifies the interrupt source for the Idle Line
mbed_official 76:aeb1df146756 1162 interrupt.
mbed_official 76:aeb1df146756 1163 (##) USART_IT_CTS: specifies the interrupt source for the CTS interrupt.
mbed_official 76:aeb1df146756 1164 (##) USART_IT_LBD: specifies the interrupt source for the LIN break
mbed_official 76:aeb1df146756 1165 detection interrupt.
mbed_official 76:aeb1df146756 1166 (##) USART_IT_PE: specifies the interrupt source for theparity error
mbed_official 76:aeb1df146756 1167 interrupt.
mbed_official 76:aeb1df146756 1168 (##) USART_IT_ERR: specifies the interrupt source for the errors
mbed_official 76:aeb1df146756 1169 interrupt.
mbed_official 76:aeb1df146756 1170 -@@- Some parameters are coded in order to use them as interrupt
mbed_official 76:aeb1df146756 1171 source or as pending bits.
mbed_official 76:aeb1df146756 1172 [..] In this Mode it is advised to use the following functions:
mbed_official 76:aeb1df146756 1173 (+) void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT,
mbed_official 76:aeb1df146756 1174 FunctionalState NewState).
mbed_official 76:aeb1df146756 1175 (+) ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT).
mbed_official 76:aeb1df146756 1176 (+) void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT).
mbed_official 76:aeb1df146756 1177
mbed_official 76:aeb1df146756 1178 *** DMA Mode ***
mbed_official 76:aeb1df146756 1179 ================
mbed_official 76:aeb1df146756 1180 [..] In DMA Mode, the USART communication can be managed by 2 DMA Channel
mbed_official 76:aeb1df146756 1181 requests:
mbed_official 76:aeb1df146756 1182 (#) USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request.
mbed_official 76:aeb1df146756 1183 (#) USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request.
mbed_official 76:aeb1df146756 1184 [..] In this Mode it is advised to use the following function:
mbed_official 76:aeb1df146756 1185 (+) void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq,
mbed_official 76:aeb1df146756 1186 FunctionalState NewState).
mbed_official 76:aeb1df146756 1187 @endverbatim
mbed_official 76:aeb1df146756 1188 * @{
mbed_official 76:aeb1df146756 1189 */
mbed_official 76:aeb1df146756 1190
mbed_official 76:aeb1df146756 1191 /**
mbed_official 76:aeb1df146756 1192 * @brief Enables or disables the specified USART interrupts.
mbed_official 76:aeb1df146756 1193 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 1194 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1195 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 1196 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
mbed_official 76:aeb1df146756 1197 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1198 * @arg USART_IT_CTS: CTS change interrupt.
mbed_official 76:aeb1df146756 1199 * @arg USART_IT_LBD: LIN Break detection interrupt.
mbed_official 76:aeb1df146756 1200 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt.
mbed_official 76:aeb1df146756 1201 * @arg USART_IT_TC: Transmission complete interrupt.
mbed_official 76:aeb1df146756 1202 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
mbed_official 76:aeb1df146756 1203 * @arg USART_IT_IDLE: Idle line detection interrupt.
mbed_official 76:aeb1df146756 1204 * @arg USART_IT_PE: Parity Error interrupt.
mbed_official 76:aeb1df146756 1205 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error).
mbed_official 76:aeb1df146756 1206 * @param NewState: new state of the specified USARTx interrupts.
mbed_official 76:aeb1df146756 1207 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 1208 * @retval None.
mbed_official 76:aeb1df146756 1209 */
mbed_official 76:aeb1df146756 1210 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
mbed_official 76:aeb1df146756 1211 {
mbed_official 76:aeb1df146756 1212 uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
mbed_official 76:aeb1df146756 1213 uint32_t usartxbase = 0x00;
mbed_official 76:aeb1df146756 1214 /* Check the parameters */
mbed_official 76:aeb1df146756 1215 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1216 assert_param(IS_USART_CONFIG_IT(USART_IT));
mbed_official 76:aeb1df146756 1217 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 1218
mbed_official 76:aeb1df146756 1219 /* The CTS interrupt is not available for UART4 and UART5 */
mbed_official 76:aeb1df146756 1220 if (USART_IT == USART_IT_CTS)
mbed_official 76:aeb1df146756 1221 {
mbed_official 76:aeb1df146756 1222 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1223 }
mbed_official 76:aeb1df146756 1224
mbed_official 76:aeb1df146756 1225 usartxbase = (uint32_t)USARTx;
mbed_official 76:aeb1df146756 1226
mbed_official 76:aeb1df146756 1227 /* Get the USART register index */
mbed_official 76:aeb1df146756 1228 usartreg = (((uint8_t)USART_IT) >> 0x05);
mbed_official 76:aeb1df146756 1229
mbed_official 76:aeb1df146756 1230 /* Get the interrupt position */
mbed_official 76:aeb1df146756 1231 itpos = USART_IT & IT_MASK;
mbed_official 76:aeb1df146756 1232 itmask = (((uint32_t)0x01) << itpos);
mbed_official 76:aeb1df146756 1233
mbed_official 76:aeb1df146756 1234 if (usartreg == 0x01) /* The IT is in CR1 register */
mbed_official 76:aeb1df146756 1235 {
mbed_official 76:aeb1df146756 1236 usartxbase += 0x0C;
mbed_official 76:aeb1df146756 1237 }
mbed_official 76:aeb1df146756 1238 else if (usartreg == 0x02) /* The IT is in CR2 register */
mbed_official 76:aeb1df146756 1239 {
mbed_official 76:aeb1df146756 1240 usartxbase += 0x10;
mbed_official 76:aeb1df146756 1241 }
mbed_official 76:aeb1df146756 1242 else /* The IT is in CR3 register */
mbed_official 76:aeb1df146756 1243 {
mbed_official 76:aeb1df146756 1244 usartxbase += 0x14;
mbed_official 76:aeb1df146756 1245 }
mbed_official 76:aeb1df146756 1246 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 1247 {
mbed_official 76:aeb1df146756 1248 *(__IO uint32_t*)usartxbase |= itmask;
mbed_official 76:aeb1df146756 1249 }
mbed_official 76:aeb1df146756 1250 else
mbed_official 76:aeb1df146756 1251 {
mbed_official 76:aeb1df146756 1252 *(__IO uint32_t*)usartxbase &= ~itmask;
mbed_official 76:aeb1df146756 1253 }
mbed_official 76:aeb1df146756 1254 }
mbed_official 76:aeb1df146756 1255
mbed_official 76:aeb1df146756 1256 /**
mbed_official 76:aeb1df146756 1257 * @brief Checks whether the specified USART flag is set or not.
mbed_official 76:aeb1df146756 1258 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 1259 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1260 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 1261 * @param USART_FLAG: specifies the flag to check.
mbed_official 76:aeb1df146756 1262 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1263 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
mbed_official 76:aeb1df146756 1264 * @arg USART_FLAG_LBD: LIN Break detection flag.
mbed_official 76:aeb1df146756 1265 * @arg USART_FLAG_TXE: Transmit data register empty flag.
mbed_official 76:aeb1df146756 1266 * @arg USART_FLAG_TC: Transmission Complete flag.
mbed_official 76:aeb1df146756 1267 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
mbed_official 76:aeb1df146756 1268 * @arg USART_FLAG_IDLE: Idle Line detection flag.
mbed_official 76:aeb1df146756 1269 * @arg USART_FLAG_ORE: OverRun Error flag.
mbed_official 76:aeb1df146756 1270 * @arg USART_FLAG_NE: Noise Error flag.
mbed_official 76:aeb1df146756 1271 * @arg USART_FLAG_FE: Framing Error flag.
mbed_official 76:aeb1df146756 1272 * @arg USART_FLAG_PE: Parity Error flag.
mbed_official 76:aeb1df146756 1273 * @retval The new state of USART_FLAG (SET or RESET).
mbed_official 76:aeb1df146756 1274 */
mbed_official 76:aeb1df146756 1275 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
mbed_official 76:aeb1df146756 1276 {
mbed_official 76:aeb1df146756 1277 FlagStatus bitstatus = RESET;
mbed_official 76:aeb1df146756 1278 /* Check the parameters */
mbed_official 76:aeb1df146756 1279 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1280 assert_param(IS_USART_FLAG(USART_FLAG));
mbed_official 76:aeb1df146756 1281
mbed_official 76:aeb1df146756 1282 /* The CTS flag is not available for UART4 and UART5 */
mbed_official 76:aeb1df146756 1283 if (USART_FLAG == USART_FLAG_CTS)
mbed_official 76:aeb1df146756 1284 {
mbed_official 76:aeb1df146756 1285 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1286 }
mbed_official 76:aeb1df146756 1287
mbed_official 76:aeb1df146756 1288 if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
mbed_official 76:aeb1df146756 1289 {
mbed_official 76:aeb1df146756 1290 bitstatus = SET;
mbed_official 76:aeb1df146756 1291 }
mbed_official 76:aeb1df146756 1292 else
mbed_official 76:aeb1df146756 1293 {
mbed_official 76:aeb1df146756 1294 bitstatus = RESET;
mbed_official 76:aeb1df146756 1295 }
mbed_official 76:aeb1df146756 1296 return bitstatus;
mbed_official 76:aeb1df146756 1297 }
mbed_official 76:aeb1df146756 1298
mbed_official 76:aeb1df146756 1299 /**
mbed_official 76:aeb1df146756 1300 * @brief Clears the USARTx's pending flags.
mbed_official 76:aeb1df146756 1301 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 1302 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1303 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 1304 * @param USART_FLAG: specifies the flag to clear.
mbed_official 76:aeb1df146756 1305 * This parameter can be any combination of the following values:
mbed_official 76:aeb1df146756 1306 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
mbed_official 76:aeb1df146756 1307 * @arg USART_FLAG_LBD: LIN Break detection flag.
mbed_official 76:aeb1df146756 1308 * @arg USART_FLAG_TC: Transmission Complete flag.
mbed_official 76:aeb1df146756 1309 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
mbed_official 76:aeb1df146756 1310 *
mbed_official 76:aeb1df146756 1311 *
mbed_official 76:aeb1df146756 1312 * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
mbed_official 76:aeb1df146756 1313 * error) and IDLE (Idle line detected) flags are cleared by software
mbed_official 76:aeb1df146756 1314 * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
mbed_official 76:aeb1df146756 1315 * followed by a read operation to USART_DR register (USART_ReceiveData()).
mbed_official 76:aeb1df146756 1316 * @note RXNE flag can be also cleared by a read to the USART_DR register
mbed_official 76:aeb1df146756 1317 * (USART_ReceiveData()).
mbed_official 76:aeb1df146756 1318 * @note TC flag can be also cleared by software sequence: a read operation to
mbed_official 76:aeb1df146756 1319 * USART_SR register (USART_GetFlagStatus()) followed by a write operation
mbed_official 76:aeb1df146756 1320 * to USART_DR register (USART_SendData()).
mbed_official 76:aeb1df146756 1321 * @note TXE flag is cleared only by a write to the USART_DR register
mbed_official 76:aeb1df146756 1322 * (USART_SendData()).
mbed_official 76:aeb1df146756 1323 * @retval None
mbed_official 76:aeb1df146756 1324 */
mbed_official 76:aeb1df146756 1325 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
mbed_official 76:aeb1df146756 1326 {
mbed_official 76:aeb1df146756 1327 /* Check the parameters */
mbed_official 76:aeb1df146756 1328 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1329 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
mbed_official 76:aeb1df146756 1330
mbed_official 76:aeb1df146756 1331 /* The CTS flag is not available for UART4 and UART5 */
mbed_official 76:aeb1df146756 1332 if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
mbed_official 76:aeb1df146756 1333 {
mbed_official 76:aeb1df146756 1334 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1335 }
mbed_official 76:aeb1df146756 1336
mbed_official 76:aeb1df146756 1337 USARTx->SR = (uint16_t)~USART_FLAG;
mbed_official 76:aeb1df146756 1338 }
mbed_official 76:aeb1df146756 1339
mbed_official 76:aeb1df146756 1340 /**
mbed_official 76:aeb1df146756 1341 * @brief Checks whether the specified USART interrupt has occurred or not.
mbed_official 76:aeb1df146756 1342 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 1343 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1344 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 1345 * @param USART_IT: specifies the USART interrupt source to check.
mbed_official 76:aeb1df146756 1346 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1347 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
mbed_official 76:aeb1df146756 1348 * @arg USART_IT_LBD: LIN Break detection interrupt
mbed_official 76:aeb1df146756 1349 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
mbed_official 76:aeb1df146756 1350 * @arg USART_IT_TC: Transmission complete interrupt
mbed_official 76:aeb1df146756 1351 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
mbed_official 76:aeb1df146756 1352 * @arg USART_IT_IDLE: Idle line detection interrupt
mbed_official 76:aeb1df146756 1353 * @arg USART_IT_ORE_RX: OverRun Error interrupt if the RXNEIE bit is set.
mbed_official 76:aeb1df146756 1354 * @arg USART_IT_ORE_ER: OverRun Error interrupt if the EIE bit is set.
mbed_official 76:aeb1df146756 1355 * @arg USART_IT_NE: Noise Error interrupt
mbed_official 76:aeb1df146756 1356 * @arg USART_IT_FE: Framing Error interrupt
mbed_official 76:aeb1df146756 1357 * @arg USART_IT_PE: Parity Error interrupt
mbed_official 76:aeb1df146756 1358 * @retval The new state of USART_IT (SET or RESET).
mbed_official 76:aeb1df146756 1359 */
mbed_official 76:aeb1df146756 1360 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
mbed_official 76:aeb1df146756 1361 {
mbed_official 76:aeb1df146756 1362 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
mbed_official 76:aeb1df146756 1363 ITStatus bitstatus = RESET;
mbed_official 76:aeb1df146756 1364 /* Check the parameters */
mbed_official 76:aeb1df146756 1365 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1366 assert_param(IS_USART_GET_IT(USART_IT));
mbed_official 76:aeb1df146756 1367
mbed_official 76:aeb1df146756 1368 /* The CTS interrupt is not available for UART4 and UART5 */
mbed_official 76:aeb1df146756 1369 if (USART_IT == USART_IT_CTS)
mbed_official 76:aeb1df146756 1370 {
mbed_official 76:aeb1df146756 1371 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1372 }
mbed_official 76:aeb1df146756 1373
mbed_official 76:aeb1df146756 1374 /* Get the USART register index */
mbed_official 76:aeb1df146756 1375 usartreg = (((uint8_t)USART_IT) >> 0x05);
mbed_official 76:aeb1df146756 1376 /* Get the interrupt position */
mbed_official 76:aeb1df146756 1377 itmask = USART_IT & IT_MASK;
mbed_official 76:aeb1df146756 1378 itmask = (uint32_t)0x01 << itmask;
mbed_official 76:aeb1df146756 1379
mbed_official 76:aeb1df146756 1380 if (usartreg == 0x01) /* The IT is in CR1 register */
mbed_official 76:aeb1df146756 1381 {
mbed_official 76:aeb1df146756 1382 itmask &= USARTx->CR1;
mbed_official 76:aeb1df146756 1383 }
mbed_official 76:aeb1df146756 1384 else if (usartreg == 0x02) /* The IT is in CR2 register */
mbed_official 76:aeb1df146756 1385 {
mbed_official 76:aeb1df146756 1386 itmask &= USARTx->CR2;
mbed_official 76:aeb1df146756 1387 }
mbed_official 76:aeb1df146756 1388 else /* The IT is in CR3 register */
mbed_official 76:aeb1df146756 1389 {
mbed_official 76:aeb1df146756 1390 itmask &= USARTx->CR3;
mbed_official 76:aeb1df146756 1391 }
mbed_official 76:aeb1df146756 1392
mbed_official 76:aeb1df146756 1393 bitpos = USART_IT >> 0x08;
mbed_official 76:aeb1df146756 1394 bitpos = (uint32_t)0x01 << bitpos;
mbed_official 76:aeb1df146756 1395 bitpos &= USARTx->SR;
mbed_official 76:aeb1df146756 1396 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
mbed_official 76:aeb1df146756 1397 {
mbed_official 76:aeb1df146756 1398 bitstatus = SET;
mbed_official 76:aeb1df146756 1399 }
mbed_official 76:aeb1df146756 1400 else
mbed_official 76:aeb1df146756 1401 {
mbed_official 76:aeb1df146756 1402 bitstatus = RESET;
mbed_official 76:aeb1df146756 1403 }
mbed_official 76:aeb1df146756 1404
mbed_official 76:aeb1df146756 1405 return bitstatus;
mbed_official 76:aeb1df146756 1406 }
mbed_official 76:aeb1df146756 1407
mbed_official 76:aeb1df146756 1408 /**
mbed_official 76:aeb1df146756 1409 * @brief Clears the USARTx's interrupt pending bits.
mbed_official 76:aeb1df146756 1410 * @param USARTx: Select the USART peripheral.
mbed_official 76:aeb1df146756 1411 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1412 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 76:aeb1df146756 1413 * @param USART_IT: specifies the interrupt pending bit to clear.
mbed_official 76:aeb1df146756 1414 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1415 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
mbed_official 76:aeb1df146756 1416 * @arg USART_IT_LBD: LIN Break detection interrupt
mbed_official 76:aeb1df146756 1417 * @arg USART_IT_TC: Transmission complete interrupt.
mbed_official 76:aeb1df146756 1418 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
mbed_official 76:aeb1df146756 1419 *
mbed_official 76:aeb1df146756 1420
mbed_official 76:aeb1df146756 1421 * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
mbed_official 76:aeb1df146756 1422 * error) and IDLE (Idle line detected) pending bits are cleared by
mbed_official 76:aeb1df146756 1423 * software sequence: a read operation to USART_SR register
mbed_official 76:aeb1df146756 1424 * (USART_GetITStatus()) followed by a read operation to USART_DR register
mbed_official 76:aeb1df146756 1425 * (USART_ReceiveData()).
mbed_official 76:aeb1df146756 1426 * @note RXNE pending bit can be also cleared by a read to the USART_DR register
mbed_official 76:aeb1df146756 1427 * (USART_ReceiveData()).
mbed_official 76:aeb1df146756 1428 * @note TC pending bit can be also cleared by software sequence: a read
mbed_official 76:aeb1df146756 1429 * operation to USART_SR register (USART_GetITStatus()) followed by a write
mbed_official 76:aeb1df146756 1430 * operation to USART_DR register (USART_SendData()).
mbed_official 76:aeb1df146756 1431 * @note TXE pending bit is cleared only by a write to the USART_DR register
mbed_official 76:aeb1df146756 1432 * (USART_SendData()).
mbed_official 76:aeb1df146756 1433 * @retval None
mbed_official 76:aeb1df146756 1434 */
mbed_official 76:aeb1df146756 1435 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
mbed_official 76:aeb1df146756 1436 {
mbed_official 76:aeb1df146756 1437 uint16_t bitpos = 0x00, itmask = 0x00;
mbed_official 76:aeb1df146756 1438 /* Check the parameters */
mbed_official 76:aeb1df146756 1439 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1440 assert_param(IS_USART_CLEAR_IT(USART_IT));
mbed_official 76:aeb1df146756 1441
mbed_official 76:aeb1df146756 1442 /* The CTS interrupt is not available for UART4 and UART5 */
mbed_official 76:aeb1df146756 1443 if (USART_IT == USART_IT_CTS)
mbed_official 76:aeb1df146756 1444 {
mbed_official 76:aeb1df146756 1445 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 76:aeb1df146756 1446 }
mbed_official 76:aeb1df146756 1447
mbed_official 76:aeb1df146756 1448 bitpos = USART_IT >> 0x08;
mbed_official 76:aeb1df146756 1449 itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
mbed_official 76:aeb1df146756 1450 USARTx->SR = (uint16_t)~itmask;
mbed_official 76:aeb1df146756 1451 }
mbed_official 76:aeb1df146756 1452
mbed_official 76:aeb1df146756 1453 /**
mbed_official 76:aeb1df146756 1454 * @}
mbed_official 76:aeb1df146756 1455 */
mbed_official 76:aeb1df146756 1456
mbed_official 76:aeb1df146756 1457 /**
mbed_official 76:aeb1df146756 1458 * @}
mbed_official 76:aeb1df146756 1459 */
mbed_official 76:aeb1df146756 1460
mbed_official 76:aeb1df146756 1461 /**
mbed_official 76:aeb1df146756 1462 * @}
mbed_official 76:aeb1df146756 1463 */
mbed_official 76:aeb1df146756 1464
mbed_official 76:aeb1df146756 1465 /**
mbed_official 76:aeb1df146756 1466 * @}
mbed_official 76:aeb1df146756 1467 */
mbed_official 76:aeb1df146756 1468
mbed_official 76:aeb1df146756 1469 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/