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