mbed SDK library sources

Fork of mbed-src by mbed official

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Mon Dec 02 11:30:05 2013 +0000
Revision:
52:a51c77007319
Child:
70:c1fbde68b492
Synchronized with git revision 49df530ae72ce97ccc773d1f2c13b38e868e6abd

Full URL: https://github.com/mbedmicro/mbed/commit/49df530ae72ce97ccc773d1f2c13b38e868e6abd/

Add STMicroelectronics NUCLEO_F103RB target

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