mbed library with additional peripherals for ST F401 board

Fork of mbed-src by mbed official

This mbed LIB has additional peripherals for ST F401 board

  • UART2 : PA_3 rx, PA_2 tx
  • UART3 : PC_7 rx, PC_6 tx
  • I2C2 : PB_3 SDA, PB_10 SCL
  • I2C3 : PB_4 SDA, PA_8 SCL
Committer:
mbed_official
Date:
Tue Jan 07 11:00:05 2014 +0000
Revision:
70:c1fbde68b492
Parent:
52:a51c77007319
Child:
84:f54042cbc282
Synchronized with git revision 3f438a307904431f2782db3c8fa49946b9fc1d85

Full URL: https://github.com/mbedmicro/mbed/commit/3f438a307904431f2782db3c8fa49946b9fc1d85/

[NUCLEO_F103RB] license text changed + sleep hal updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 52:a51c77007319 1 /**
mbed_official 52:a51c77007319 2 ******************************************************************************
mbed_official 52:a51c77007319 3 * @file stm32f10x_usart.c
mbed_official 52:a51c77007319 4 * @author MCD Application Team
mbed_official 52:a51c77007319 5 * @version V3.5.0
mbed_official 52:a51c77007319 6 * @date 11-March-2011
mbed_official 52:a51c77007319 7 * @brief This file provides all the USART firmware functions.
mbed_official 70:c1fbde68b492 8 *******************************************************************************
mbed_official 70:c1fbde68b492 9 * Copyright (c) 2014, STMicroelectronics
mbed_official 70:c1fbde68b492 10 * All rights reserved.
mbed_official 70:c1fbde68b492 11 *
mbed_official 70:c1fbde68b492 12 * Redistribution and use in source and binary forms, with or without
mbed_official 70:c1fbde68b492 13 * modification, are permitted provided that the following conditions are met:
mbed_official 70:c1fbde68b492 14 *
mbed_official 70:c1fbde68b492 15 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 70:c1fbde68b492 16 * this list of conditions and the following disclaimer.
mbed_official 70:c1fbde68b492 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 70:c1fbde68b492 18 * this list of conditions and the following disclaimer in the documentation
mbed_official 70:c1fbde68b492 19 * and/or other materials provided with the distribution.
mbed_official 70:c1fbde68b492 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 70:c1fbde68b492 21 * may be used to endorse or promote products derived from this software
mbed_official 70:c1fbde68b492 22 * without specific prior written permission.
mbed_official 70:c1fbde68b492 23 *
mbed_official 70:c1fbde68b492 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 70:c1fbde68b492 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 70:c1fbde68b492 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 70:c1fbde68b492 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 70:c1fbde68b492 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 70:c1fbde68b492 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 70:c1fbde68b492 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 70:c1fbde68b492 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 70:c1fbde68b492 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 70:c1fbde68b492 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 70:c1fbde68b492 34 *******************************************************************************
mbed_official 70:c1fbde68b492 35 */
mbed_official 52:a51c77007319 36
mbed_official 52:a51c77007319 37 /* Includes ------------------------------------------------------------------*/
mbed_official 52:a51c77007319 38 #include "stm32f10x_usart.h"
mbed_official 52:a51c77007319 39 #include "stm32f10x_rcc.h"
mbed_official 52:a51c77007319 40
mbed_official 52:a51c77007319 41 /** @addtogroup STM32F10x_StdPeriph_Driver
mbed_official 52:a51c77007319 42 * @{
mbed_official 52:a51c77007319 43 */
mbed_official 52:a51c77007319 44
mbed_official 52:a51c77007319 45 /** @defgroup USART
mbed_official 52:a51c77007319 46 * @brief USART driver modules
mbed_official 52:a51c77007319 47 * @{
mbed_official 52:a51c77007319 48 */
mbed_official 52:a51c77007319 49
mbed_official 52:a51c77007319 50 /** @defgroup USART_Private_TypesDefinitions
mbed_official 52:a51c77007319 51 * @{
mbed_official 52:a51c77007319 52 */
mbed_official 52:a51c77007319 53
mbed_official 52:a51c77007319 54 /**
mbed_official 52:a51c77007319 55 * @}
mbed_official 52:a51c77007319 56 */
mbed_official 52:a51c77007319 57
mbed_official 52:a51c77007319 58 /** @defgroup USART_Private_Defines
mbed_official 52:a51c77007319 59 * @{
mbed_official 52:a51c77007319 60 */
mbed_official 52:a51c77007319 61
mbed_official 52:a51c77007319 62 #define CR1_UE_Set ((uint16_t)0x2000) /*!< USART Enable Mask */
mbed_official 52:a51c77007319 63 #define CR1_UE_Reset ((uint16_t)0xDFFF) /*!< USART Disable Mask */
mbed_official 52:a51c77007319 64
mbed_official 52:a51c77007319 65 #define CR1_WAKE_Mask ((uint16_t)0xF7FF) /*!< USART WakeUp Method Mask */
mbed_official 52:a51c77007319 66
mbed_official 52:a51c77007319 67 #define CR1_RWU_Set ((uint16_t)0x0002) /*!< USART mute mode Enable Mask */
mbed_official 52:a51c77007319 68 #define CR1_RWU_Reset ((uint16_t)0xFFFD) /*!< USART mute mode Enable Mask */
mbed_official 52:a51c77007319 69 #define CR1_SBK_Set ((uint16_t)0x0001) /*!< USART Break Character send Mask */
mbed_official 52:a51c77007319 70 #define CR1_CLEAR_Mask ((uint16_t)0xE9F3) /*!< USART CR1 Mask */
mbed_official 52:a51c77007319 71 #define CR2_Address_Mask ((uint16_t)0xFFF0) /*!< USART address Mask */
mbed_official 52:a51c77007319 72
mbed_official 52:a51c77007319 73 #define CR2_LINEN_Set ((uint16_t)0x4000) /*!< USART LIN Enable Mask */
mbed_official 52:a51c77007319 74 #define CR2_LINEN_Reset ((uint16_t)0xBFFF) /*!< USART LIN Disable Mask */
mbed_official 52:a51c77007319 75
mbed_official 52:a51c77007319 76 #define CR2_LBDL_Mask ((uint16_t)0xFFDF) /*!< USART LIN Break detection Mask */
mbed_official 52:a51c77007319 77 #define CR2_STOP_CLEAR_Mask ((uint16_t)0xCFFF) /*!< USART CR2 STOP Bits Mask */
mbed_official 52:a51c77007319 78 #define CR2_CLOCK_CLEAR_Mask ((uint16_t)0xF0FF) /*!< USART CR2 Clock Mask */
mbed_official 52:a51c77007319 79
mbed_official 52:a51c77007319 80 #define CR3_SCEN_Set ((uint16_t)0x0020) /*!< USART SC Enable Mask */
mbed_official 52:a51c77007319 81 #define CR3_SCEN_Reset ((uint16_t)0xFFDF) /*!< USART SC Disable Mask */
mbed_official 52:a51c77007319 82
mbed_official 52:a51c77007319 83 #define CR3_NACK_Set ((uint16_t)0x0010) /*!< USART SC NACK Enable Mask */
mbed_official 52:a51c77007319 84 #define CR3_NACK_Reset ((uint16_t)0xFFEF) /*!< USART SC NACK Disable Mask */
mbed_official 52:a51c77007319 85
mbed_official 52:a51c77007319 86 #define CR3_HDSEL_Set ((uint16_t)0x0008) /*!< USART Half-Duplex Enable Mask */
mbed_official 52:a51c77007319 87 #define CR3_HDSEL_Reset ((uint16_t)0xFFF7) /*!< USART Half-Duplex Disable Mask */
mbed_official 52:a51c77007319 88
mbed_official 52:a51c77007319 89 #define CR3_IRLP_Mask ((uint16_t)0xFFFB) /*!< USART IrDA LowPower mode Mask */
mbed_official 52:a51c77007319 90 #define CR3_CLEAR_Mask ((uint16_t)0xFCFF) /*!< USART CR3 Mask */
mbed_official 52:a51c77007319 91
mbed_official 52:a51c77007319 92 #define CR3_IREN_Set ((uint16_t)0x0002) /*!< USART IrDA Enable Mask */
mbed_official 52:a51c77007319 93 #define CR3_IREN_Reset ((uint16_t)0xFFFD) /*!< USART IrDA Disable Mask */
mbed_official 52:a51c77007319 94 #define GTPR_LSB_Mask ((uint16_t)0x00FF) /*!< Guard Time Register LSB Mask */
mbed_official 52:a51c77007319 95 #define GTPR_MSB_Mask ((uint16_t)0xFF00) /*!< Guard Time Register MSB Mask */
mbed_official 52:a51c77007319 96 #define IT_Mask ((uint16_t)0x001F) /*!< USART Interrupt Mask */
mbed_official 52:a51c77007319 97
mbed_official 52:a51c77007319 98 /* USART OverSampling-8 Mask */
mbed_official 52:a51c77007319 99 #define CR1_OVER8_Set ((u16)0x8000) /* USART OVER8 mode Enable Mask */
mbed_official 52:a51c77007319 100 #define CR1_OVER8_Reset ((u16)0x7FFF) /* USART OVER8 mode Disable Mask */
mbed_official 52:a51c77007319 101
mbed_official 52:a51c77007319 102 /* USART One Bit Sampling Mask */
mbed_official 52:a51c77007319 103 #define CR3_ONEBITE_Set ((u16)0x0800) /* USART ONEBITE mode Enable Mask */
mbed_official 52:a51c77007319 104 #define CR3_ONEBITE_Reset ((u16)0xF7FF) /* USART ONEBITE mode Disable Mask */
mbed_official 52:a51c77007319 105
mbed_official 52:a51c77007319 106 /**
mbed_official 52:a51c77007319 107 * @}
mbed_official 52:a51c77007319 108 */
mbed_official 52:a51c77007319 109
mbed_official 52:a51c77007319 110 /** @defgroup USART_Private_Macros
mbed_official 52:a51c77007319 111 * @{
mbed_official 52:a51c77007319 112 */
mbed_official 52:a51c77007319 113
mbed_official 52:a51c77007319 114 /**
mbed_official 52:a51c77007319 115 * @}
mbed_official 52:a51c77007319 116 */
mbed_official 52:a51c77007319 117
mbed_official 52:a51c77007319 118 /** @defgroup USART_Private_Variables
mbed_official 52:a51c77007319 119 * @{
mbed_official 52:a51c77007319 120 */
mbed_official 52:a51c77007319 121
mbed_official 52:a51c77007319 122 /**
mbed_official 52:a51c77007319 123 * @}
mbed_official 52:a51c77007319 124 */
mbed_official 52:a51c77007319 125
mbed_official 52:a51c77007319 126 /** @defgroup USART_Private_FunctionPrototypes
mbed_official 52:a51c77007319 127 * @{
mbed_official 52:a51c77007319 128 */
mbed_official 52:a51c77007319 129
mbed_official 52:a51c77007319 130 /**
mbed_official 52:a51c77007319 131 * @}
mbed_official 52:a51c77007319 132 */
mbed_official 52:a51c77007319 133
mbed_official 52:a51c77007319 134 /** @defgroup USART_Private_Functions
mbed_official 52:a51c77007319 135 * @{
mbed_official 52:a51c77007319 136 */
mbed_official 52:a51c77007319 137
mbed_official 52:a51c77007319 138 /**
mbed_official 52:a51c77007319 139 * @brief Deinitializes the USARTx peripheral registers to their default reset values.
mbed_official 52:a51c77007319 140 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 141 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 142 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 143 * @retval None
mbed_official 52:a51c77007319 144 */
mbed_official 52:a51c77007319 145 void USART_DeInit(USART_TypeDef* USARTx)
mbed_official 52:a51c77007319 146 {
mbed_official 52:a51c77007319 147 /* Check the parameters */
mbed_official 52:a51c77007319 148 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 149
mbed_official 52:a51c77007319 150 if (USARTx == USART1)
mbed_official 52:a51c77007319 151 {
mbed_official 52:a51c77007319 152 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
mbed_official 52:a51c77007319 153 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
mbed_official 52:a51c77007319 154 }
mbed_official 52:a51c77007319 155 else if (USARTx == USART2)
mbed_official 52:a51c77007319 156 {
mbed_official 52:a51c77007319 157 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
mbed_official 52:a51c77007319 158 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
mbed_official 52:a51c77007319 159 }
mbed_official 52:a51c77007319 160 else if (USARTx == USART3)
mbed_official 52:a51c77007319 161 {
mbed_official 52:a51c77007319 162 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
mbed_official 52:a51c77007319 163 RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
mbed_official 52:a51c77007319 164 }
mbed_official 52:a51c77007319 165 else if (USARTx == UART4)
mbed_official 52:a51c77007319 166 {
mbed_official 52:a51c77007319 167 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
mbed_official 52:a51c77007319 168 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
mbed_official 52:a51c77007319 169 }
mbed_official 52:a51c77007319 170 else
mbed_official 52:a51c77007319 171 {
mbed_official 52:a51c77007319 172 if (USARTx == UART5)
mbed_official 52:a51c77007319 173 {
mbed_official 52:a51c77007319 174 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
mbed_official 52:a51c77007319 175 RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
mbed_official 52:a51c77007319 176 }
mbed_official 52:a51c77007319 177 }
mbed_official 52:a51c77007319 178 }
mbed_official 52:a51c77007319 179
mbed_official 52:a51c77007319 180 /**
mbed_official 52:a51c77007319 181 * @brief Initializes the USARTx peripheral according to the specified
mbed_official 52:a51c77007319 182 * parameters in the USART_InitStruct .
mbed_official 52:a51c77007319 183 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 184 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 185 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 186 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
mbed_official 52:a51c77007319 187 * that contains the configuration information for the specified USART
mbed_official 52:a51c77007319 188 * peripheral.
mbed_official 52:a51c77007319 189 * @retval None
mbed_official 52:a51c77007319 190 */
mbed_official 52:a51c77007319 191 void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
mbed_official 52:a51c77007319 192 {
mbed_official 52:a51c77007319 193 uint32_t tmpreg = 0x00, apbclock = 0x00;
mbed_official 52:a51c77007319 194 uint32_t integerdivider = 0x00;
mbed_official 52:a51c77007319 195 uint32_t fractionaldivider = 0x00;
mbed_official 52:a51c77007319 196 uint32_t usartxbase = 0;
mbed_official 52:a51c77007319 197 RCC_ClocksTypeDef RCC_ClocksStatus;
mbed_official 52:a51c77007319 198 /* Check the parameters */
mbed_official 52:a51c77007319 199 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 200 assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
mbed_official 52:a51c77007319 201 assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
mbed_official 52:a51c77007319 202 assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
mbed_official 52:a51c77007319 203 assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
mbed_official 52:a51c77007319 204 assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
mbed_official 52:a51c77007319 205 assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
mbed_official 52:a51c77007319 206 /* The hardware flow control is available only for USART1, USART2 and USART3 */
mbed_official 52:a51c77007319 207 if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
mbed_official 52:a51c77007319 208 {
mbed_official 52:a51c77007319 209 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 52:a51c77007319 210 }
mbed_official 52:a51c77007319 211
mbed_official 52:a51c77007319 212 usartxbase = (uint32_t)USARTx;
mbed_official 52:a51c77007319 213
mbed_official 52:a51c77007319 214 /*---------------------------- USART CR2 Configuration -----------------------*/
mbed_official 52:a51c77007319 215 tmpreg = USARTx->CR2;
mbed_official 52:a51c77007319 216 /* Clear STOP[13:12] bits */
mbed_official 52:a51c77007319 217 tmpreg &= CR2_STOP_CLEAR_Mask;
mbed_official 52:a51c77007319 218 /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
mbed_official 52:a51c77007319 219 /* Set STOP[13:12] bits according to USART_StopBits value */
mbed_official 52:a51c77007319 220 tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
mbed_official 52:a51c77007319 221
mbed_official 52:a51c77007319 222 /* Write to USART CR2 */
mbed_official 52:a51c77007319 223 USARTx->CR2 = (uint16_t)tmpreg;
mbed_official 52:a51c77007319 224
mbed_official 52:a51c77007319 225 /*---------------------------- USART CR1 Configuration -----------------------*/
mbed_official 52:a51c77007319 226 tmpreg = USARTx->CR1;
mbed_official 52:a51c77007319 227 /* Clear M, PCE, PS, TE and RE bits */
mbed_official 52:a51c77007319 228 tmpreg &= CR1_CLEAR_Mask;
mbed_official 52:a51c77007319 229 /* Configure the USART Word Length, Parity and mode ----------------------- */
mbed_official 52:a51c77007319 230 /* Set the M bits according to USART_WordLength value */
mbed_official 52:a51c77007319 231 /* Set PCE and PS bits according to USART_Parity value */
mbed_official 52:a51c77007319 232 /* Set TE and RE bits according to USART_Mode value */
mbed_official 52:a51c77007319 233 tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
mbed_official 52:a51c77007319 234 USART_InitStruct->USART_Mode;
mbed_official 52:a51c77007319 235 /* Write to USART CR1 */
mbed_official 52:a51c77007319 236 USARTx->CR1 = (uint16_t)tmpreg;
mbed_official 52:a51c77007319 237
mbed_official 52:a51c77007319 238 /*---------------------------- USART CR3 Configuration -----------------------*/
mbed_official 52:a51c77007319 239 tmpreg = USARTx->CR3;
mbed_official 52:a51c77007319 240 /* Clear CTSE and RTSE bits */
mbed_official 52:a51c77007319 241 tmpreg &= CR3_CLEAR_Mask;
mbed_official 52:a51c77007319 242 /* Configure the USART HFC -------------------------------------------------*/
mbed_official 52:a51c77007319 243 /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
mbed_official 52:a51c77007319 244 tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
mbed_official 52:a51c77007319 245 /* Write to USART CR3 */
mbed_official 52:a51c77007319 246 USARTx->CR3 = (uint16_t)tmpreg;
mbed_official 52:a51c77007319 247
mbed_official 52:a51c77007319 248 /*---------------------------- USART BRR Configuration -----------------------*/
mbed_official 52:a51c77007319 249 /* Configure the USART Baud Rate -------------------------------------------*/
mbed_official 52:a51c77007319 250 RCC_GetClocksFreq(&RCC_ClocksStatus);
mbed_official 52:a51c77007319 251 if (usartxbase == USART1_BASE)
mbed_official 52:a51c77007319 252 {
mbed_official 52:a51c77007319 253 apbclock = RCC_ClocksStatus.PCLK2_Frequency;
mbed_official 52:a51c77007319 254 }
mbed_official 52:a51c77007319 255 else
mbed_official 52:a51c77007319 256 {
mbed_official 52:a51c77007319 257 apbclock = RCC_ClocksStatus.PCLK1_Frequency;
mbed_official 52:a51c77007319 258 }
mbed_official 52:a51c77007319 259
mbed_official 52:a51c77007319 260 /* Determine the integer part */
mbed_official 52:a51c77007319 261 if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
mbed_official 52:a51c77007319 262 {
mbed_official 52:a51c77007319 263 /* Integer part computing in case Oversampling mode is 8 Samples */
mbed_official 52:a51c77007319 264 integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
mbed_official 52:a51c77007319 265 }
mbed_official 52:a51c77007319 266 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
mbed_official 52:a51c77007319 267 {
mbed_official 52:a51c77007319 268 /* Integer part computing in case Oversampling mode is 16 Samples */
mbed_official 52:a51c77007319 269 integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
mbed_official 52:a51c77007319 270 }
mbed_official 52:a51c77007319 271 tmpreg = (integerdivider / 100) << 4;
mbed_official 52:a51c77007319 272
mbed_official 52:a51c77007319 273 /* Determine the fractional part */
mbed_official 52:a51c77007319 274 fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
mbed_official 52:a51c77007319 275
mbed_official 52:a51c77007319 276 /* Implement the fractional part in the register */
mbed_official 52:a51c77007319 277 if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
mbed_official 52:a51c77007319 278 {
mbed_official 52:a51c77007319 279 tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
mbed_official 52:a51c77007319 280 }
mbed_official 52:a51c77007319 281 else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
mbed_official 52:a51c77007319 282 {
mbed_official 52:a51c77007319 283 tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
mbed_official 52:a51c77007319 284 }
mbed_official 52:a51c77007319 285
mbed_official 52:a51c77007319 286 /* Write to USART BRR */
mbed_official 52:a51c77007319 287 USARTx->BRR = (uint16_t)tmpreg;
mbed_official 52:a51c77007319 288 }
mbed_official 52:a51c77007319 289
mbed_official 52:a51c77007319 290 /**
mbed_official 52:a51c77007319 291 * @brief Fills each USART_InitStruct member with its default value.
mbed_official 52:a51c77007319 292 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
mbed_official 52:a51c77007319 293 * which will be initialized.
mbed_official 52:a51c77007319 294 * @retval None
mbed_official 52:a51c77007319 295 */
mbed_official 52:a51c77007319 296 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
mbed_official 52:a51c77007319 297 {
mbed_official 52:a51c77007319 298 /* USART_InitStruct members default value */
mbed_official 52:a51c77007319 299 USART_InitStruct->USART_BaudRate = 9600;
mbed_official 52:a51c77007319 300 USART_InitStruct->USART_WordLength = USART_WordLength_8b;
mbed_official 52:a51c77007319 301 USART_InitStruct->USART_StopBits = USART_StopBits_1;
mbed_official 52:a51c77007319 302 USART_InitStruct->USART_Parity = USART_Parity_No ;
mbed_official 52:a51c77007319 303 USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
mbed_official 52:a51c77007319 304 USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
mbed_official 52:a51c77007319 305 }
mbed_official 52:a51c77007319 306
mbed_official 52:a51c77007319 307 /**
mbed_official 52:a51c77007319 308 * @brief Initializes the USARTx peripheral Clock according to the
mbed_official 52:a51c77007319 309 * specified parameters in the USART_ClockInitStruct .
mbed_official 52:a51c77007319 310 * @param USARTx: where x can be 1, 2, 3 to select the USART peripheral.
mbed_official 52:a51c77007319 311 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
mbed_official 52:a51c77007319 312 * structure that contains the configuration information for the specified
mbed_official 52:a51c77007319 313 * USART peripheral.
mbed_official 52:a51c77007319 314 * @note The Smart Card and Synchronous modes are not available for UART4 and UART5.
mbed_official 52:a51c77007319 315 * @retval None
mbed_official 52:a51c77007319 316 */
mbed_official 52:a51c77007319 317 void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
mbed_official 52:a51c77007319 318 {
mbed_official 52:a51c77007319 319 uint32_t tmpreg = 0x00;
mbed_official 52:a51c77007319 320 /* Check the parameters */
mbed_official 52:a51c77007319 321 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 52:a51c77007319 322 assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
mbed_official 52:a51c77007319 323 assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
mbed_official 52:a51c77007319 324 assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
mbed_official 52:a51c77007319 325 assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
mbed_official 52:a51c77007319 326
mbed_official 52:a51c77007319 327 /*---------------------------- USART CR2 Configuration -----------------------*/
mbed_official 52:a51c77007319 328 tmpreg = USARTx->CR2;
mbed_official 52:a51c77007319 329 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
mbed_official 52:a51c77007319 330 tmpreg &= CR2_CLOCK_CLEAR_Mask;
mbed_official 52:a51c77007319 331 /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
mbed_official 52:a51c77007319 332 /* Set CLKEN bit according to USART_Clock value */
mbed_official 52:a51c77007319 333 /* Set CPOL bit according to USART_CPOL value */
mbed_official 52:a51c77007319 334 /* Set CPHA bit according to USART_CPHA value */
mbed_official 52:a51c77007319 335 /* Set LBCL bit according to USART_LastBit value */
mbed_official 52:a51c77007319 336 tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
mbed_official 52:a51c77007319 337 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
mbed_official 52:a51c77007319 338 /* Write to USART CR2 */
mbed_official 52:a51c77007319 339 USARTx->CR2 = (uint16_t)tmpreg;
mbed_official 52:a51c77007319 340 }
mbed_official 52:a51c77007319 341
mbed_official 52:a51c77007319 342 /**
mbed_official 52:a51c77007319 343 * @brief Fills each USART_ClockInitStruct member with its default value.
mbed_official 52:a51c77007319 344 * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
mbed_official 52:a51c77007319 345 * structure which will be initialized.
mbed_official 52:a51c77007319 346 * @retval None
mbed_official 52:a51c77007319 347 */
mbed_official 52:a51c77007319 348 void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
mbed_official 52:a51c77007319 349 {
mbed_official 52:a51c77007319 350 /* USART_ClockInitStruct members default value */
mbed_official 52:a51c77007319 351 USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
mbed_official 52:a51c77007319 352 USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
mbed_official 52:a51c77007319 353 USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
mbed_official 52:a51c77007319 354 USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
mbed_official 52:a51c77007319 355 }
mbed_official 52:a51c77007319 356
mbed_official 52:a51c77007319 357 /**
mbed_official 52:a51c77007319 358 * @brief Enables or disables the specified USART peripheral.
mbed_official 52:a51c77007319 359 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 360 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 361 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 362 * @param NewState: new state of the USARTx peripheral.
mbed_official 52:a51c77007319 363 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 364 * @retval None
mbed_official 52:a51c77007319 365 */
mbed_official 52:a51c77007319 366 void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 52:a51c77007319 367 {
mbed_official 52:a51c77007319 368 /* Check the parameters */
mbed_official 52:a51c77007319 369 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 370 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 371
mbed_official 52:a51c77007319 372 if (NewState != DISABLE)
mbed_official 52:a51c77007319 373 {
mbed_official 52:a51c77007319 374 /* Enable the selected USART by setting the UE bit in the CR1 register */
mbed_official 52:a51c77007319 375 USARTx->CR1 |= CR1_UE_Set;
mbed_official 52:a51c77007319 376 }
mbed_official 52:a51c77007319 377 else
mbed_official 52:a51c77007319 378 {
mbed_official 52:a51c77007319 379 /* Disable the selected USART by clearing the UE bit in the CR1 register */
mbed_official 52:a51c77007319 380 USARTx->CR1 &= CR1_UE_Reset;
mbed_official 52:a51c77007319 381 }
mbed_official 52:a51c77007319 382 }
mbed_official 52:a51c77007319 383
mbed_official 52:a51c77007319 384 /**
mbed_official 52:a51c77007319 385 * @brief Enables or disables the specified USART interrupts.
mbed_official 52:a51c77007319 386 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 387 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 388 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 389 * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
mbed_official 52:a51c77007319 390 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 391 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
mbed_official 52:a51c77007319 392 * @arg USART_IT_LBD: LIN Break detection interrupt
mbed_official 52:a51c77007319 393 * @arg USART_IT_TXE: Transmit Data Register empty interrupt
mbed_official 52:a51c77007319 394 * @arg USART_IT_TC: Transmission complete interrupt
mbed_official 52:a51c77007319 395 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
mbed_official 52:a51c77007319 396 * @arg USART_IT_IDLE: Idle line detection interrupt
mbed_official 52:a51c77007319 397 * @arg USART_IT_PE: Parity Error interrupt
mbed_official 52:a51c77007319 398 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
mbed_official 52:a51c77007319 399 * @param NewState: new state of the specified USARTx interrupts.
mbed_official 52:a51c77007319 400 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 401 * @retval None
mbed_official 52:a51c77007319 402 */
mbed_official 52:a51c77007319 403 void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
mbed_official 52:a51c77007319 404 {
mbed_official 52:a51c77007319 405 uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
mbed_official 52:a51c77007319 406 uint32_t usartxbase = 0x00;
mbed_official 52:a51c77007319 407 /* Check the parameters */
mbed_official 52:a51c77007319 408 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 409 assert_param(IS_USART_CONFIG_IT(USART_IT));
mbed_official 52:a51c77007319 410 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 411 /* The CTS interrupt is not available for UART4 and UART5 */
mbed_official 52:a51c77007319 412 if (USART_IT == USART_IT_CTS)
mbed_official 52:a51c77007319 413 {
mbed_official 52:a51c77007319 414 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 52:a51c77007319 415 }
mbed_official 52:a51c77007319 416
mbed_official 52:a51c77007319 417 usartxbase = (uint32_t)USARTx;
mbed_official 52:a51c77007319 418
mbed_official 52:a51c77007319 419 /* Get the USART register index */
mbed_official 52:a51c77007319 420 usartreg = (((uint8_t)USART_IT) >> 0x05);
mbed_official 52:a51c77007319 421
mbed_official 52:a51c77007319 422 /* Get the interrupt position */
mbed_official 52:a51c77007319 423 itpos = USART_IT & IT_Mask;
mbed_official 52:a51c77007319 424 itmask = (((uint32_t)0x01) << itpos);
mbed_official 52:a51c77007319 425
mbed_official 52:a51c77007319 426 if (usartreg == 0x01) /* The IT is in CR1 register */
mbed_official 52:a51c77007319 427 {
mbed_official 52:a51c77007319 428 usartxbase += 0x0C;
mbed_official 52:a51c77007319 429 }
mbed_official 52:a51c77007319 430 else if (usartreg == 0x02) /* The IT is in CR2 register */
mbed_official 52:a51c77007319 431 {
mbed_official 52:a51c77007319 432 usartxbase += 0x10;
mbed_official 52:a51c77007319 433 }
mbed_official 52:a51c77007319 434 else /* The IT is in CR3 register */
mbed_official 52:a51c77007319 435 {
mbed_official 52:a51c77007319 436 usartxbase += 0x14;
mbed_official 52:a51c77007319 437 }
mbed_official 52:a51c77007319 438 if (NewState != DISABLE)
mbed_official 52:a51c77007319 439 {
mbed_official 52:a51c77007319 440 *(__IO uint32_t*)usartxbase |= itmask;
mbed_official 52:a51c77007319 441 }
mbed_official 52:a51c77007319 442 else
mbed_official 52:a51c77007319 443 {
mbed_official 52:a51c77007319 444 *(__IO uint32_t*)usartxbase &= ~itmask;
mbed_official 52:a51c77007319 445 }
mbed_official 52:a51c77007319 446 }
mbed_official 52:a51c77007319 447
mbed_official 52:a51c77007319 448 /**
mbed_official 52:a51c77007319 449 * @brief Enables or disables the USART’s DMA interface.
mbed_official 52:a51c77007319 450 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 451 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 452 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 453 * @param USART_DMAReq: specifies the DMA request.
mbed_official 52:a51c77007319 454 * This parameter can be any combination of the following values:
mbed_official 52:a51c77007319 455 * @arg USART_DMAReq_Tx: USART DMA transmit request
mbed_official 52:a51c77007319 456 * @arg USART_DMAReq_Rx: USART DMA receive request
mbed_official 52:a51c77007319 457 * @param NewState: new state of the DMA Request sources.
mbed_official 52:a51c77007319 458 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 459 * @note The DMA mode is not available for UART5 except in the STM32
mbed_official 52:a51c77007319 460 * High density value line devices(STM32F10X_HD_VL).
mbed_official 52:a51c77007319 461 * @retval None
mbed_official 52:a51c77007319 462 */
mbed_official 52:a51c77007319 463 void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
mbed_official 52:a51c77007319 464 {
mbed_official 52:a51c77007319 465 /* Check the parameters */
mbed_official 52:a51c77007319 466 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 467 assert_param(IS_USART_DMAREQ(USART_DMAReq));
mbed_official 52:a51c77007319 468 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 469 if (NewState != DISABLE)
mbed_official 52:a51c77007319 470 {
mbed_official 52:a51c77007319 471 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
mbed_official 52:a51c77007319 472 DMAR bits in the USART CR3 register */
mbed_official 52:a51c77007319 473 USARTx->CR3 |= USART_DMAReq;
mbed_official 52:a51c77007319 474 }
mbed_official 52:a51c77007319 475 else
mbed_official 52:a51c77007319 476 {
mbed_official 52:a51c77007319 477 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
mbed_official 52:a51c77007319 478 DMAR bits in the USART CR3 register */
mbed_official 52:a51c77007319 479 USARTx->CR3 &= (uint16_t)~USART_DMAReq;
mbed_official 52:a51c77007319 480 }
mbed_official 52:a51c77007319 481 }
mbed_official 52:a51c77007319 482
mbed_official 52:a51c77007319 483 /**
mbed_official 52:a51c77007319 484 * @brief Sets the address of the USART node.
mbed_official 52:a51c77007319 485 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 486 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 487 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 488 * @param USART_Address: Indicates the address of the USART node.
mbed_official 52:a51c77007319 489 * @retval None
mbed_official 52:a51c77007319 490 */
mbed_official 52:a51c77007319 491 void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
mbed_official 52:a51c77007319 492 {
mbed_official 52:a51c77007319 493 /* Check the parameters */
mbed_official 52:a51c77007319 494 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 495 assert_param(IS_USART_ADDRESS(USART_Address));
mbed_official 52:a51c77007319 496
mbed_official 52:a51c77007319 497 /* Clear the USART address */
mbed_official 52:a51c77007319 498 USARTx->CR2 &= CR2_Address_Mask;
mbed_official 52:a51c77007319 499 /* Set the USART address node */
mbed_official 52:a51c77007319 500 USARTx->CR2 |= USART_Address;
mbed_official 52:a51c77007319 501 }
mbed_official 52:a51c77007319 502
mbed_official 52:a51c77007319 503 /**
mbed_official 52:a51c77007319 504 * @brief Selects the USART WakeUp method.
mbed_official 52:a51c77007319 505 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 506 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 507 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 508 * @param USART_WakeUp: specifies the USART wakeup method.
mbed_official 52:a51c77007319 509 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 510 * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
mbed_official 52:a51c77007319 511 * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
mbed_official 52:a51c77007319 512 * @retval None
mbed_official 52:a51c77007319 513 */
mbed_official 52:a51c77007319 514 void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
mbed_official 52:a51c77007319 515 {
mbed_official 52:a51c77007319 516 /* Check the parameters */
mbed_official 52:a51c77007319 517 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 518 assert_param(IS_USART_WAKEUP(USART_WakeUp));
mbed_official 52:a51c77007319 519
mbed_official 52:a51c77007319 520 USARTx->CR1 &= CR1_WAKE_Mask;
mbed_official 52:a51c77007319 521 USARTx->CR1 |= USART_WakeUp;
mbed_official 52:a51c77007319 522 }
mbed_official 52:a51c77007319 523
mbed_official 52:a51c77007319 524 /**
mbed_official 52:a51c77007319 525 * @brief Determines if the USART is in mute mode or not.
mbed_official 52:a51c77007319 526 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 527 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 528 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 529 * @param NewState: new state of the USART mute mode.
mbed_official 52:a51c77007319 530 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 531 * @retval None
mbed_official 52:a51c77007319 532 */
mbed_official 52:a51c77007319 533 void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 52:a51c77007319 534 {
mbed_official 52:a51c77007319 535 /* Check the parameters */
mbed_official 52:a51c77007319 536 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 537 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 538
mbed_official 52:a51c77007319 539 if (NewState != DISABLE)
mbed_official 52:a51c77007319 540 {
mbed_official 52:a51c77007319 541 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
mbed_official 52:a51c77007319 542 USARTx->CR1 |= CR1_RWU_Set;
mbed_official 52:a51c77007319 543 }
mbed_official 52:a51c77007319 544 else
mbed_official 52:a51c77007319 545 {
mbed_official 52:a51c77007319 546 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
mbed_official 52:a51c77007319 547 USARTx->CR1 &= CR1_RWU_Reset;
mbed_official 52:a51c77007319 548 }
mbed_official 52:a51c77007319 549 }
mbed_official 52:a51c77007319 550
mbed_official 52:a51c77007319 551 /**
mbed_official 52:a51c77007319 552 * @brief Sets the USART LIN Break detection length.
mbed_official 52:a51c77007319 553 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 554 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 555 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 556 * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
mbed_official 52:a51c77007319 557 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 558 * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
mbed_official 52:a51c77007319 559 * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
mbed_official 52:a51c77007319 560 * @retval None
mbed_official 52:a51c77007319 561 */
mbed_official 52:a51c77007319 562 void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
mbed_official 52:a51c77007319 563 {
mbed_official 52:a51c77007319 564 /* Check the parameters */
mbed_official 52:a51c77007319 565 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 566 assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
mbed_official 52:a51c77007319 567
mbed_official 52:a51c77007319 568 USARTx->CR2 &= CR2_LBDL_Mask;
mbed_official 52:a51c77007319 569 USARTx->CR2 |= USART_LINBreakDetectLength;
mbed_official 52:a51c77007319 570 }
mbed_official 52:a51c77007319 571
mbed_official 52:a51c77007319 572 /**
mbed_official 52:a51c77007319 573 * @brief Enables or disables the USART’s LIN mode.
mbed_official 52:a51c77007319 574 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 575 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 576 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 577 * @param NewState: new state of the USART LIN mode.
mbed_official 52:a51c77007319 578 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 579 * @retval None
mbed_official 52:a51c77007319 580 */
mbed_official 52:a51c77007319 581 void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 52:a51c77007319 582 {
mbed_official 52:a51c77007319 583 /* Check the parameters */
mbed_official 52:a51c77007319 584 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 585 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 586
mbed_official 52:a51c77007319 587 if (NewState != DISABLE)
mbed_official 52:a51c77007319 588 {
mbed_official 52:a51c77007319 589 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
mbed_official 52:a51c77007319 590 USARTx->CR2 |= CR2_LINEN_Set;
mbed_official 52:a51c77007319 591 }
mbed_official 52:a51c77007319 592 else
mbed_official 52:a51c77007319 593 {
mbed_official 52:a51c77007319 594 /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
mbed_official 52:a51c77007319 595 USARTx->CR2 &= CR2_LINEN_Reset;
mbed_official 52:a51c77007319 596 }
mbed_official 52:a51c77007319 597 }
mbed_official 52:a51c77007319 598
mbed_official 52:a51c77007319 599 /**
mbed_official 52:a51c77007319 600 * @brief Transmits single data through the USARTx peripheral.
mbed_official 52:a51c77007319 601 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 602 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 603 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 604 * @param Data: the data to transmit.
mbed_official 52:a51c77007319 605 * @retval None
mbed_official 52:a51c77007319 606 */
mbed_official 52:a51c77007319 607 void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
mbed_official 52:a51c77007319 608 {
mbed_official 52:a51c77007319 609 /* Check the parameters */
mbed_official 52:a51c77007319 610 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 611 assert_param(IS_USART_DATA(Data));
mbed_official 52:a51c77007319 612
mbed_official 52:a51c77007319 613 /* Transmit Data */
mbed_official 52:a51c77007319 614 USARTx->DR = (Data & (uint16_t)0x01FF);
mbed_official 52:a51c77007319 615 }
mbed_official 52:a51c77007319 616
mbed_official 52:a51c77007319 617 /**
mbed_official 52:a51c77007319 618 * @brief Returns the most recent received data by the USARTx peripheral.
mbed_official 52:a51c77007319 619 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 620 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 621 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 622 * @retval The received data.
mbed_official 52:a51c77007319 623 */
mbed_official 52:a51c77007319 624 uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
mbed_official 52:a51c77007319 625 {
mbed_official 52:a51c77007319 626 /* Check the parameters */
mbed_official 52:a51c77007319 627 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 628
mbed_official 52:a51c77007319 629 /* Receive Data */
mbed_official 52:a51c77007319 630 return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
mbed_official 52:a51c77007319 631 }
mbed_official 52:a51c77007319 632
mbed_official 52:a51c77007319 633 /**
mbed_official 52:a51c77007319 634 * @brief Transmits break characters.
mbed_official 52:a51c77007319 635 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 636 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 637 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 638 * @retval None
mbed_official 52:a51c77007319 639 */
mbed_official 52:a51c77007319 640 void USART_SendBreak(USART_TypeDef* USARTx)
mbed_official 52:a51c77007319 641 {
mbed_official 52:a51c77007319 642 /* Check the parameters */
mbed_official 52:a51c77007319 643 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 644
mbed_official 52:a51c77007319 645 /* Send break characters */
mbed_official 52:a51c77007319 646 USARTx->CR1 |= CR1_SBK_Set;
mbed_official 52:a51c77007319 647 }
mbed_official 52:a51c77007319 648
mbed_official 52:a51c77007319 649 /**
mbed_official 52:a51c77007319 650 * @brief Sets the specified USART guard time.
mbed_official 52:a51c77007319 651 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
mbed_official 52:a51c77007319 652 * @param USART_GuardTime: specifies the guard time.
mbed_official 52:a51c77007319 653 * @note The guard time bits are not available for UART4 and UART5.
mbed_official 52:a51c77007319 654 * @retval None
mbed_official 52:a51c77007319 655 */
mbed_official 52:a51c77007319 656 void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
mbed_official 52:a51c77007319 657 {
mbed_official 52:a51c77007319 658 /* Check the parameters */
mbed_official 52:a51c77007319 659 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 52:a51c77007319 660
mbed_official 52:a51c77007319 661 /* Clear the USART Guard time */
mbed_official 52:a51c77007319 662 USARTx->GTPR &= GTPR_LSB_Mask;
mbed_official 52:a51c77007319 663 /* Set the USART guard time */
mbed_official 52:a51c77007319 664 USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
mbed_official 52:a51c77007319 665 }
mbed_official 52:a51c77007319 666
mbed_official 52:a51c77007319 667 /**
mbed_official 52:a51c77007319 668 * @brief Sets the system clock prescaler.
mbed_official 52:a51c77007319 669 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 670 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 671 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 672 * @param USART_Prescaler: specifies the prescaler clock.
mbed_official 52:a51c77007319 673 * @note The function is used for IrDA mode with UART4 and UART5.
mbed_official 52:a51c77007319 674 * @retval None
mbed_official 52:a51c77007319 675 */
mbed_official 52:a51c77007319 676 void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
mbed_official 52:a51c77007319 677 {
mbed_official 52:a51c77007319 678 /* Check the parameters */
mbed_official 52:a51c77007319 679 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 680
mbed_official 52:a51c77007319 681 /* Clear the USART prescaler */
mbed_official 52:a51c77007319 682 USARTx->GTPR &= GTPR_MSB_Mask;
mbed_official 52:a51c77007319 683 /* Set the USART prescaler */
mbed_official 52:a51c77007319 684 USARTx->GTPR |= USART_Prescaler;
mbed_official 52:a51c77007319 685 }
mbed_official 52:a51c77007319 686
mbed_official 52:a51c77007319 687 /**
mbed_official 52:a51c77007319 688 * @brief Enables or disables the USART’s Smart Card mode.
mbed_official 52:a51c77007319 689 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
mbed_official 52:a51c77007319 690 * @param NewState: new state of the Smart Card mode.
mbed_official 52:a51c77007319 691 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 692 * @note The Smart Card mode is not available for UART4 and UART5.
mbed_official 52:a51c77007319 693 * @retval None
mbed_official 52:a51c77007319 694 */
mbed_official 52:a51c77007319 695 void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 52:a51c77007319 696 {
mbed_official 52:a51c77007319 697 /* Check the parameters */
mbed_official 52:a51c77007319 698 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 52:a51c77007319 699 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 700 if (NewState != DISABLE)
mbed_official 52:a51c77007319 701 {
mbed_official 52:a51c77007319 702 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
mbed_official 52:a51c77007319 703 USARTx->CR3 |= CR3_SCEN_Set;
mbed_official 52:a51c77007319 704 }
mbed_official 52:a51c77007319 705 else
mbed_official 52:a51c77007319 706 {
mbed_official 52:a51c77007319 707 /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
mbed_official 52:a51c77007319 708 USARTx->CR3 &= CR3_SCEN_Reset;
mbed_official 52:a51c77007319 709 }
mbed_official 52:a51c77007319 710 }
mbed_official 52:a51c77007319 711
mbed_official 52:a51c77007319 712 /**
mbed_official 52:a51c77007319 713 * @brief Enables or disables NACK transmission.
mbed_official 52:a51c77007319 714 * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
mbed_official 52:a51c77007319 715 * @param NewState: new state of the NACK transmission.
mbed_official 52:a51c77007319 716 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 717 * @note The Smart Card mode is not available for UART4 and UART5.
mbed_official 52:a51c77007319 718 * @retval None
mbed_official 52:a51c77007319 719 */
mbed_official 52:a51c77007319 720 void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 52:a51c77007319 721 {
mbed_official 52:a51c77007319 722 /* Check the parameters */
mbed_official 52:a51c77007319 723 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 52:a51c77007319 724 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 725 if (NewState != DISABLE)
mbed_official 52:a51c77007319 726 {
mbed_official 52:a51c77007319 727 /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
mbed_official 52:a51c77007319 728 USARTx->CR3 |= CR3_NACK_Set;
mbed_official 52:a51c77007319 729 }
mbed_official 52:a51c77007319 730 else
mbed_official 52:a51c77007319 731 {
mbed_official 52:a51c77007319 732 /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
mbed_official 52:a51c77007319 733 USARTx->CR3 &= CR3_NACK_Reset;
mbed_official 52:a51c77007319 734 }
mbed_official 52:a51c77007319 735 }
mbed_official 52:a51c77007319 736
mbed_official 52:a51c77007319 737 /**
mbed_official 52:a51c77007319 738 * @brief Enables or disables the USART’s Half Duplex communication.
mbed_official 52:a51c77007319 739 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 740 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 741 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 742 * @param NewState: new state of the USART Communication.
mbed_official 52:a51c77007319 743 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 744 * @retval None
mbed_official 52:a51c77007319 745 */
mbed_official 52:a51c77007319 746 void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 52:a51c77007319 747 {
mbed_official 52:a51c77007319 748 /* Check the parameters */
mbed_official 52:a51c77007319 749 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 750 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 751
mbed_official 52:a51c77007319 752 if (NewState != DISABLE)
mbed_official 52:a51c77007319 753 {
mbed_official 52:a51c77007319 754 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
mbed_official 52:a51c77007319 755 USARTx->CR3 |= CR3_HDSEL_Set;
mbed_official 52:a51c77007319 756 }
mbed_official 52:a51c77007319 757 else
mbed_official 52:a51c77007319 758 {
mbed_official 52:a51c77007319 759 /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
mbed_official 52:a51c77007319 760 USARTx->CR3 &= CR3_HDSEL_Reset;
mbed_official 52:a51c77007319 761 }
mbed_official 52:a51c77007319 762 }
mbed_official 52:a51c77007319 763
mbed_official 52:a51c77007319 764
mbed_official 52:a51c77007319 765 /**
mbed_official 52:a51c77007319 766 * @brief Enables or disables the USART's 8x oversampling mode.
mbed_official 52:a51c77007319 767 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 768 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 769 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 770 * @param NewState: new state of the USART one bit sampling method.
mbed_official 52:a51c77007319 771 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 772 * @note
mbed_official 52:a51c77007319 773 * This function has to be called before calling USART_Init()
mbed_official 52:a51c77007319 774 * function in order to have correct baudrate Divider value.
mbed_official 52:a51c77007319 775 * @retval None
mbed_official 52:a51c77007319 776 */
mbed_official 52:a51c77007319 777 void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 52:a51c77007319 778 {
mbed_official 52:a51c77007319 779 /* Check the parameters */
mbed_official 52:a51c77007319 780 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 781 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 782
mbed_official 52:a51c77007319 783 if (NewState != DISABLE)
mbed_official 52:a51c77007319 784 {
mbed_official 52:a51c77007319 785 /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
mbed_official 52:a51c77007319 786 USARTx->CR1 |= CR1_OVER8_Set;
mbed_official 52:a51c77007319 787 }
mbed_official 52:a51c77007319 788 else
mbed_official 52:a51c77007319 789 {
mbed_official 52:a51c77007319 790 /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
mbed_official 52:a51c77007319 791 USARTx->CR1 &= CR1_OVER8_Reset;
mbed_official 52:a51c77007319 792 }
mbed_official 52:a51c77007319 793 }
mbed_official 52:a51c77007319 794
mbed_official 52:a51c77007319 795 /**
mbed_official 52:a51c77007319 796 * @brief Enables or disables the USART's one bit sampling method.
mbed_official 52:a51c77007319 797 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 798 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 799 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 800 * @param NewState: new state of the USART one bit sampling method.
mbed_official 52:a51c77007319 801 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 802 * @retval None
mbed_official 52:a51c77007319 803 */
mbed_official 52:a51c77007319 804 void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 52:a51c77007319 805 {
mbed_official 52:a51c77007319 806 /* Check the parameters */
mbed_official 52:a51c77007319 807 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 808 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 809
mbed_official 52:a51c77007319 810 if (NewState != DISABLE)
mbed_official 52:a51c77007319 811 {
mbed_official 52:a51c77007319 812 /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
mbed_official 52:a51c77007319 813 USARTx->CR3 |= CR3_ONEBITE_Set;
mbed_official 52:a51c77007319 814 }
mbed_official 52:a51c77007319 815 else
mbed_official 52:a51c77007319 816 {
mbed_official 52:a51c77007319 817 /* Disable tthe one bit method by clearing the ONEBITE bit in the CR3 register */
mbed_official 52:a51c77007319 818 USARTx->CR3 &= CR3_ONEBITE_Reset;
mbed_official 52:a51c77007319 819 }
mbed_official 52:a51c77007319 820 }
mbed_official 52:a51c77007319 821
mbed_official 52:a51c77007319 822 /**
mbed_official 52:a51c77007319 823 * @brief Configures the USART's IrDA interface.
mbed_official 52:a51c77007319 824 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 825 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 826 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 827 * @param USART_IrDAMode: specifies the IrDA mode.
mbed_official 52:a51c77007319 828 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 829 * @arg USART_IrDAMode_LowPower
mbed_official 52:a51c77007319 830 * @arg USART_IrDAMode_Normal
mbed_official 52:a51c77007319 831 * @retval None
mbed_official 52:a51c77007319 832 */
mbed_official 52:a51c77007319 833 void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
mbed_official 52:a51c77007319 834 {
mbed_official 52:a51c77007319 835 /* Check the parameters */
mbed_official 52:a51c77007319 836 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 837 assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
mbed_official 52:a51c77007319 838
mbed_official 52:a51c77007319 839 USARTx->CR3 &= CR3_IRLP_Mask;
mbed_official 52:a51c77007319 840 USARTx->CR3 |= USART_IrDAMode;
mbed_official 52:a51c77007319 841 }
mbed_official 52:a51c77007319 842
mbed_official 52:a51c77007319 843 /**
mbed_official 52:a51c77007319 844 * @brief Enables or disables the USART's IrDA interface.
mbed_official 52:a51c77007319 845 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 846 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 847 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 848 * @param NewState: new state of the IrDA mode.
mbed_official 52:a51c77007319 849 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 850 * @retval None
mbed_official 52:a51c77007319 851 */
mbed_official 52:a51c77007319 852 void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
mbed_official 52:a51c77007319 853 {
mbed_official 52:a51c77007319 854 /* Check the parameters */
mbed_official 52:a51c77007319 855 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 856 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 857
mbed_official 52:a51c77007319 858 if (NewState != DISABLE)
mbed_official 52:a51c77007319 859 {
mbed_official 52:a51c77007319 860 /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
mbed_official 52:a51c77007319 861 USARTx->CR3 |= CR3_IREN_Set;
mbed_official 52:a51c77007319 862 }
mbed_official 52:a51c77007319 863 else
mbed_official 52:a51c77007319 864 {
mbed_official 52:a51c77007319 865 /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
mbed_official 52:a51c77007319 866 USARTx->CR3 &= CR3_IREN_Reset;
mbed_official 52:a51c77007319 867 }
mbed_official 52:a51c77007319 868 }
mbed_official 52:a51c77007319 869
mbed_official 52:a51c77007319 870 /**
mbed_official 52:a51c77007319 871 * @brief Checks whether the specified USART flag is set or not.
mbed_official 52:a51c77007319 872 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 873 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 874 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 875 * @param USART_FLAG: specifies the flag to check.
mbed_official 52:a51c77007319 876 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 877 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
mbed_official 52:a51c77007319 878 * @arg USART_FLAG_LBD: LIN Break detection flag
mbed_official 52:a51c77007319 879 * @arg USART_FLAG_TXE: Transmit data register empty flag
mbed_official 52:a51c77007319 880 * @arg USART_FLAG_TC: Transmission Complete flag
mbed_official 52:a51c77007319 881 * @arg USART_FLAG_RXNE: Receive data register not empty flag
mbed_official 52:a51c77007319 882 * @arg USART_FLAG_IDLE: Idle Line detection flag
mbed_official 52:a51c77007319 883 * @arg USART_FLAG_ORE: OverRun Error flag
mbed_official 52:a51c77007319 884 * @arg USART_FLAG_NE: Noise Error flag
mbed_official 52:a51c77007319 885 * @arg USART_FLAG_FE: Framing Error flag
mbed_official 52:a51c77007319 886 * @arg USART_FLAG_PE: Parity Error flag
mbed_official 52:a51c77007319 887 * @retval The new state of USART_FLAG (SET or RESET).
mbed_official 52:a51c77007319 888 */
mbed_official 52:a51c77007319 889 FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
mbed_official 52:a51c77007319 890 {
mbed_official 52:a51c77007319 891 FlagStatus bitstatus = RESET;
mbed_official 52:a51c77007319 892 /* Check the parameters */
mbed_official 52:a51c77007319 893 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 894 assert_param(IS_USART_FLAG(USART_FLAG));
mbed_official 52:a51c77007319 895 /* The CTS flag is not available for UART4 and UART5 */
mbed_official 52:a51c77007319 896 if (USART_FLAG == USART_FLAG_CTS)
mbed_official 52:a51c77007319 897 {
mbed_official 52:a51c77007319 898 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 52:a51c77007319 899 }
mbed_official 52:a51c77007319 900
mbed_official 52:a51c77007319 901 if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
mbed_official 52:a51c77007319 902 {
mbed_official 52:a51c77007319 903 bitstatus = SET;
mbed_official 52:a51c77007319 904 }
mbed_official 52:a51c77007319 905 else
mbed_official 52:a51c77007319 906 {
mbed_official 52:a51c77007319 907 bitstatus = RESET;
mbed_official 52:a51c77007319 908 }
mbed_official 52:a51c77007319 909 return bitstatus;
mbed_official 52:a51c77007319 910 }
mbed_official 52:a51c77007319 911
mbed_official 52:a51c77007319 912 /**
mbed_official 52:a51c77007319 913 * @brief Clears the USARTx's pending flags.
mbed_official 52:a51c77007319 914 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 915 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 916 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 917 * @param USART_FLAG: specifies the flag to clear.
mbed_official 52:a51c77007319 918 * This parameter can be any combination of the following values:
mbed_official 52:a51c77007319 919 * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
mbed_official 52:a51c77007319 920 * @arg USART_FLAG_LBD: LIN Break detection flag.
mbed_official 52:a51c77007319 921 * @arg USART_FLAG_TC: Transmission Complete flag.
mbed_official 52:a51c77007319 922 * @arg USART_FLAG_RXNE: Receive data register not empty flag.
mbed_official 52:a51c77007319 923 *
mbed_official 52:a51c77007319 924 * @note
mbed_official 52:a51c77007319 925 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
mbed_official 52:a51c77007319 926 * error) and IDLE (Idle line detected) flags are cleared by software
mbed_official 52:a51c77007319 927 * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
mbed_official 52:a51c77007319 928 * followed by a read operation to USART_DR register (USART_ReceiveData()).
mbed_official 52:a51c77007319 929 * - RXNE flag can be also cleared by a read to the USART_DR register
mbed_official 52:a51c77007319 930 * (USART_ReceiveData()).
mbed_official 52:a51c77007319 931 * - TC flag can be also cleared by software sequence: a read operation to
mbed_official 52:a51c77007319 932 * USART_SR register (USART_GetFlagStatus()) followed by a write operation
mbed_official 52:a51c77007319 933 * to USART_DR register (USART_SendData()).
mbed_official 52:a51c77007319 934 * - TXE flag is cleared only by a write to the USART_DR register
mbed_official 52:a51c77007319 935 * (USART_SendData()).
mbed_official 52:a51c77007319 936 * @retval None
mbed_official 52:a51c77007319 937 */
mbed_official 52:a51c77007319 938 void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
mbed_official 52:a51c77007319 939 {
mbed_official 52:a51c77007319 940 /* Check the parameters */
mbed_official 52:a51c77007319 941 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 942 assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
mbed_official 52:a51c77007319 943 /* The CTS flag is not available for UART4 and UART5 */
mbed_official 52:a51c77007319 944 if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
mbed_official 52:a51c77007319 945 {
mbed_official 52:a51c77007319 946 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 52:a51c77007319 947 }
mbed_official 52:a51c77007319 948
mbed_official 52:a51c77007319 949 USARTx->SR = (uint16_t)~USART_FLAG;
mbed_official 52:a51c77007319 950 }
mbed_official 52:a51c77007319 951
mbed_official 52:a51c77007319 952 /**
mbed_official 52:a51c77007319 953 * @brief Checks whether the specified USART interrupt has occurred or not.
mbed_official 52:a51c77007319 954 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 955 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 956 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 957 * @param USART_IT: specifies the USART interrupt source to check.
mbed_official 52:a51c77007319 958 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 959 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
mbed_official 52:a51c77007319 960 * @arg USART_IT_LBD: LIN Break detection interrupt
mbed_official 52:a51c77007319 961 * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
mbed_official 52:a51c77007319 962 * @arg USART_IT_TC: Transmission complete interrupt
mbed_official 52:a51c77007319 963 * @arg USART_IT_RXNE: Receive Data register not empty interrupt
mbed_official 52:a51c77007319 964 * @arg USART_IT_IDLE: Idle line detection interrupt
mbed_official 52:a51c77007319 965 * @arg USART_IT_ORE: OverRun Error interrupt
mbed_official 52:a51c77007319 966 * @arg USART_IT_NE: Noise Error interrupt
mbed_official 52:a51c77007319 967 * @arg USART_IT_FE: Framing Error interrupt
mbed_official 52:a51c77007319 968 * @arg USART_IT_PE: Parity Error interrupt
mbed_official 52:a51c77007319 969 * @retval The new state of USART_IT (SET or RESET).
mbed_official 52:a51c77007319 970 */
mbed_official 52:a51c77007319 971 ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
mbed_official 52:a51c77007319 972 {
mbed_official 52:a51c77007319 973 uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
mbed_official 52:a51c77007319 974 ITStatus bitstatus = RESET;
mbed_official 52:a51c77007319 975 /* Check the parameters */
mbed_official 52:a51c77007319 976 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 977 assert_param(IS_USART_GET_IT(USART_IT));
mbed_official 52:a51c77007319 978 /* The CTS interrupt is not available for UART4 and UART5 */
mbed_official 52:a51c77007319 979 if (USART_IT == USART_IT_CTS)
mbed_official 52:a51c77007319 980 {
mbed_official 52:a51c77007319 981 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 52:a51c77007319 982 }
mbed_official 52:a51c77007319 983
mbed_official 52:a51c77007319 984 /* Get the USART register index */
mbed_official 52:a51c77007319 985 usartreg = (((uint8_t)USART_IT) >> 0x05);
mbed_official 52:a51c77007319 986 /* Get the interrupt position */
mbed_official 52:a51c77007319 987 itmask = USART_IT & IT_Mask;
mbed_official 52:a51c77007319 988 itmask = (uint32_t)0x01 << itmask;
mbed_official 52:a51c77007319 989
mbed_official 52:a51c77007319 990 if (usartreg == 0x01) /* The IT is in CR1 register */
mbed_official 52:a51c77007319 991 {
mbed_official 52:a51c77007319 992 itmask &= USARTx->CR1;
mbed_official 52:a51c77007319 993 }
mbed_official 52:a51c77007319 994 else if (usartreg == 0x02) /* The IT is in CR2 register */
mbed_official 52:a51c77007319 995 {
mbed_official 52:a51c77007319 996 itmask &= USARTx->CR2;
mbed_official 52:a51c77007319 997 }
mbed_official 52:a51c77007319 998 else /* The IT is in CR3 register */
mbed_official 52:a51c77007319 999 {
mbed_official 52:a51c77007319 1000 itmask &= USARTx->CR3;
mbed_official 52:a51c77007319 1001 }
mbed_official 52:a51c77007319 1002
mbed_official 52:a51c77007319 1003 bitpos = USART_IT >> 0x08;
mbed_official 52:a51c77007319 1004 bitpos = (uint32_t)0x01 << bitpos;
mbed_official 52:a51c77007319 1005 bitpos &= USARTx->SR;
mbed_official 52:a51c77007319 1006 if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
mbed_official 52:a51c77007319 1007 {
mbed_official 52:a51c77007319 1008 bitstatus = SET;
mbed_official 52:a51c77007319 1009 }
mbed_official 52:a51c77007319 1010 else
mbed_official 52:a51c77007319 1011 {
mbed_official 52:a51c77007319 1012 bitstatus = RESET;
mbed_official 52:a51c77007319 1013 }
mbed_official 52:a51c77007319 1014
mbed_official 52:a51c77007319 1015 return bitstatus;
mbed_official 52:a51c77007319 1016 }
mbed_official 52:a51c77007319 1017
mbed_official 52:a51c77007319 1018 /**
mbed_official 52:a51c77007319 1019 * @brief Clears the USARTx's interrupt pending bits.
mbed_official 52:a51c77007319 1020 * @param USARTx: Select the USART or the UART peripheral.
mbed_official 52:a51c77007319 1021 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 1022 * USART1, USART2, USART3, UART4 or UART5.
mbed_official 52:a51c77007319 1023 * @param USART_IT: specifies the interrupt pending bit to clear.
mbed_official 52:a51c77007319 1024 * This parameter can be one of the following values:
mbed_official 52:a51c77007319 1025 * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
mbed_official 52:a51c77007319 1026 * @arg USART_IT_LBD: LIN Break detection interrupt
mbed_official 52:a51c77007319 1027 * @arg USART_IT_TC: Transmission complete interrupt.
mbed_official 52:a51c77007319 1028 * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
mbed_official 52:a51c77007319 1029 *
mbed_official 52:a51c77007319 1030 * @note
mbed_official 52:a51c77007319 1031 * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
mbed_official 52:a51c77007319 1032 * error) and IDLE (Idle line detected) pending bits are cleared by
mbed_official 52:a51c77007319 1033 * software sequence: a read operation to USART_SR register
mbed_official 52:a51c77007319 1034 * (USART_GetITStatus()) followed by a read operation to USART_DR register
mbed_official 52:a51c77007319 1035 * (USART_ReceiveData()).
mbed_official 52:a51c77007319 1036 * - RXNE pending bit can be also cleared by a read to the USART_DR register
mbed_official 52:a51c77007319 1037 * (USART_ReceiveData()).
mbed_official 52:a51c77007319 1038 * - TC pending bit can be also cleared by software sequence: a read
mbed_official 52:a51c77007319 1039 * operation to USART_SR register (USART_GetITStatus()) followed by a write
mbed_official 52:a51c77007319 1040 * operation to USART_DR register (USART_SendData()).
mbed_official 52:a51c77007319 1041 * - TXE pending bit is cleared only by a write to the USART_DR register
mbed_official 52:a51c77007319 1042 * (USART_SendData()).
mbed_official 52:a51c77007319 1043 * @retval None
mbed_official 52:a51c77007319 1044 */
mbed_official 52:a51c77007319 1045 void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
mbed_official 52:a51c77007319 1046 {
mbed_official 52:a51c77007319 1047 uint16_t bitpos = 0x00, itmask = 0x00;
mbed_official 52:a51c77007319 1048 /* Check the parameters */
mbed_official 52:a51c77007319 1049 assert_param(IS_USART_ALL_PERIPH(USARTx));
mbed_official 52:a51c77007319 1050 assert_param(IS_USART_CLEAR_IT(USART_IT));
mbed_official 52:a51c77007319 1051 /* The CTS interrupt is not available for UART4 and UART5 */
mbed_official 52:a51c77007319 1052 if (USART_IT == USART_IT_CTS)
mbed_official 52:a51c77007319 1053 {
mbed_official 52:a51c77007319 1054 assert_param(IS_USART_123_PERIPH(USARTx));
mbed_official 52:a51c77007319 1055 }
mbed_official 52:a51c77007319 1056
mbed_official 52:a51c77007319 1057 bitpos = USART_IT >> 0x08;
mbed_official 52:a51c77007319 1058 itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
mbed_official 52:a51c77007319 1059 USARTx->SR = (uint16_t)~itmask;
mbed_official 52:a51c77007319 1060 }
mbed_official 52:a51c77007319 1061 /**
mbed_official 52:a51c77007319 1062 * @}
mbed_official 52:a51c77007319 1063 */
mbed_official 52:a51c77007319 1064
mbed_official 52:a51c77007319 1065 /**
mbed_official 52:a51c77007319 1066 * @}
mbed_official 52:a51c77007319 1067 */
mbed_official 52:a51c77007319 1068
mbed_official 52:a51c77007319 1069 /**
mbed_official 52:a51c77007319 1070 * @}
mbed_official 52:a51c77007319 1071 */
mbed_official 52:a51c77007319 1072
mbed_official 52:a51c77007319 1073 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/