mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
157:90e3acc479a2
test with CLOCK_SETUP = 0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 157:90e3acc479a2 1 /**
mbed_official 157:90e3acc479a2 2 ******************************************************************************
mbed_official 157:90e3acc479a2 3 * @file stm32f30x_gpio.c
mbed_official 157:90e3acc479a2 4 * @author MCD Application Team
mbed_official 157:90e3acc479a2 5 * @version V1.1.0
mbed_official 157:90e3acc479a2 6 * @date 27-February-2014
mbed_official 157:90e3acc479a2 7 * @brief This file provides firmware functions to manage the following
mbed_official 157:90e3acc479a2 8 * functionalities of the GPIO peripheral:
mbed_official 157:90e3acc479a2 9 * + Initialization and Configuration functions
mbed_official 157:90e3acc479a2 10 * + GPIO Read and Write functions
mbed_official 157:90e3acc479a2 11 * + GPIO Alternate functions configuration functions
mbed_official 157:90e3acc479a2 12 *
mbed_official 157:90e3acc479a2 13 * @verbatim
mbed_official 157:90e3acc479a2 14
mbed_official 157:90e3acc479a2 15
mbed_official 157:90e3acc479a2 16 ===============================================================================
mbed_official 157:90e3acc479a2 17 ##### How to use this driver #####
mbed_official 157:90e3acc479a2 18 ===============================================================================
mbed_official 157:90e3acc479a2 19 [..]
mbed_official 157:90e3acc479a2 20 (#) Enable the GPIO AHB clock using RCC_AHBPeriphClockCmd()
mbed_official 157:90e3acc479a2 21 (#) Configure the GPIO pin(s) using GPIO_Init()
mbed_official 157:90e3acc479a2 22 Four possible configuration are available for each pin:
mbed_official 157:90e3acc479a2 23 (++) Input: Floating, Pull-up, Pull-down.
mbed_official 157:90e3acc479a2 24 (++) Output: Push-Pull (Pull-up, Pull-down or no Pull),
mbed_official 157:90e3acc479a2 25 Open Drain (Pull-up, Pull-down or no Pull).
mbed_official 157:90e3acc479a2 26 In output mode, the speed is configurable: Low, Medium, Fast or High.
mbed_official 157:90e3acc479a2 27 (++) Alternate Function: Push-Pull (Pull-up, Pull-down or no Pull),
mbed_official 157:90e3acc479a2 28 Open Drain (Pull-up, Pull-down or no Pull).
mbed_official 157:90e3acc479a2 29 (++) Analog: required mode when a pin is to be used as ADC channel,
mbed_official 157:90e3acc479a2 30 DAC output or comparator input.
mbed_official 157:90e3acc479a2 31 (#) Peripherals alternate function:
mbed_official 157:90e3acc479a2 32 (++) For ADC, DAC and comparators, configure the desired pin in
mbed_official 157:90e3acc479a2 33 analog mode using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AN
mbed_official 157:90e3acc479a2 34 (++) For other peripherals (TIM, USART...):
mbed_official 157:90e3acc479a2 35 (+++) Connect the pin to the desired peripherals' Alternate
mbed_official 157:90e3acc479a2 36 Function (AF) using GPIO_PinAFConfig() function.
mbed_official 157:90e3acc479a2 37 (+++) Configure the desired pin in alternate function mode using
mbed_official 157:90e3acc479a2 38 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
mbed_official 157:90e3acc479a2 39 (+++) Select the type, pull-up/pull-down and output speed via
mbed_official 157:90e3acc479a2 40 GPIO_PuPd, GPIO_OType and GPIO_Speed members.
mbed_official 157:90e3acc479a2 41 (+++) Call GPIO_Init() function.
mbed_official 157:90e3acc479a2 42 (#) To get the level of a pin configured in input mode use GPIO_ReadInputDataBit()
mbed_official 157:90e3acc479a2 43 (#) To set/reset the level of a pin configured in output mode use
mbed_official 157:90e3acc479a2 44 GPIO_SetBits()/GPIO_ResetBits()
mbed_official 157:90e3acc479a2 45 (#) During and just after reset, the alternate functions are not active
mbed_official 157:90e3acc479a2 46 and the GPIO pins are configured in input floating mode (except JTAG pins).
mbed_official 157:90e3acc479a2 47 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as
mbed_official 157:90e3acc479a2 48 general-purpose (PC14 and PC15, respectively) when the LSE
mbed_official 157:90e3acc479a2 49 oscillator is off. The LSE has priority over the GPIO function.
mbed_official 157:90e3acc479a2 50 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as general-purpose
mbed_official 157:90e3acc479a2 51 (PF0 and PF1 respectively) when the HSE oscillator is off. The HSE has
mbed_official 157:90e3acc479a2 52 the priority over the GPIO function.
mbed_official 157:90e3acc479a2 53
mbed_official 157:90e3acc479a2 54 @endverbatim
mbed_official 157:90e3acc479a2 55
mbed_official 157:90e3acc479a2 56 ******************************************************************************
mbed_official 157:90e3acc479a2 57 * @attention
mbed_official 157:90e3acc479a2 58 *
mbed_official 157:90e3acc479a2 59 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 157:90e3acc479a2 60 *
mbed_official 157:90e3acc479a2 61 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 157:90e3acc479a2 62 * are permitted provided that the following conditions are met:
mbed_official 157:90e3acc479a2 63 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 157:90e3acc479a2 64 * this list of conditions and the following disclaimer.
mbed_official 157:90e3acc479a2 65 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 157:90e3acc479a2 66 * this list of conditions and the following disclaimer in the documentation
mbed_official 157:90e3acc479a2 67 * and/or other materials provided with the distribution.
mbed_official 157:90e3acc479a2 68 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 157:90e3acc479a2 69 * may be used to endorse or promote products derived from this software
mbed_official 157:90e3acc479a2 70 * without specific prior written permission.
mbed_official 157:90e3acc479a2 71 *
mbed_official 157:90e3acc479a2 72 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 157:90e3acc479a2 73 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 157:90e3acc479a2 74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 157:90e3acc479a2 75 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 157:90e3acc479a2 76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 157:90e3acc479a2 77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 157:90e3acc479a2 78 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 157:90e3acc479a2 79 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 157:90e3acc479a2 80 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 157:90e3acc479a2 81 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 157:90e3acc479a2 82 *
mbed_official 157:90e3acc479a2 83 ******************************************************************************
mbed_official 157:90e3acc479a2 84 */
mbed_official 157:90e3acc479a2 85
mbed_official 157:90e3acc479a2 86 /* Includes ------------------------------------------------------------------*/
mbed_official 157:90e3acc479a2 87 #include "stm32f30x_gpio.h"
mbed_official 157:90e3acc479a2 88 #include "stm32f30x_rcc.h"
mbed_official 157:90e3acc479a2 89
mbed_official 157:90e3acc479a2 90 /** @addtogroup STM32F30x_StdPeriph_Driver
mbed_official 157:90e3acc479a2 91 * @{
mbed_official 157:90e3acc479a2 92 */
mbed_official 157:90e3acc479a2 93
mbed_official 157:90e3acc479a2 94 /** @defgroup GPIO
mbed_official 157:90e3acc479a2 95 * @brief GPIO driver modules
mbed_official 157:90e3acc479a2 96 * @{
mbed_official 157:90e3acc479a2 97 */
mbed_official 157:90e3acc479a2 98
mbed_official 157:90e3acc479a2 99
mbed_official 157:90e3acc479a2 100 /* Private typedef -----------------------------------------------------------*/
mbed_official 157:90e3acc479a2 101 /* Private define ------------------------------------------------------------*/
mbed_official 157:90e3acc479a2 102
mbed_official 157:90e3acc479a2 103
mbed_official 157:90e3acc479a2 104 /* Private macro -------------------------------------------------------------*/
mbed_official 157:90e3acc479a2 105 /* Private variables ---------------------------------------------------------*/
mbed_official 157:90e3acc479a2 106 /* Private function prototypes -----------------------------------------------*/
mbed_official 157:90e3acc479a2 107 /* Private functions ---------------------------------------------------------*/
mbed_official 157:90e3acc479a2 108
mbed_official 157:90e3acc479a2 109 /** @defgroup GPIO_Private_Functions
mbed_official 157:90e3acc479a2 110 * @{
mbed_official 157:90e3acc479a2 111 */
mbed_official 157:90e3acc479a2 112
mbed_official 157:90e3acc479a2 113 /** @defgroup GPIO_Group1 Initialization and Configuration
mbed_official 157:90e3acc479a2 114 * @brief Initialization and Configuration
mbed_official 157:90e3acc479a2 115 *
mbed_official 157:90e3acc479a2 116 @verbatim
mbed_official 157:90e3acc479a2 117 ===============================================================================
mbed_official 157:90e3acc479a2 118 ##### Initialization and Configuration #####
mbed_official 157:90e3acc479a2 119 ===============================================================================
mbed_official 157:90e3acc479a2 120
mbed_official 157:90e3acc479a2 121 @endverbatim
mbed_official 157:90e3acc479a2 122 * @{
mbed_official 157:90e3acc479a2 123 */
mbed_official 157:90e3acc479a2 124
mbed_official 157:90e3acc479a2 125 /**
mbed_official 157:90e3acc479a2 126 * @brief Deinitializes the GPIOx peripheral registers to their default reset
mbed_official 157:90e3acc479a2 127 * values.
mbed_official 157:90e3acc479a2 128 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 129 * @retval None
mbed_official 157:90e3acc479a2 130 */
mbed_official 157:90e3acc479a2 131 void GPIO_DeInit(GPIO_TypeDef* GPIOx)
mbed_official 157:90e3acc479a2 132 {
mbed_official 157:90e3acc479a2 133 /* Check the parameters */
mbed_official 157:90e3acc479a2 134 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 135
mbed_official 157:90e3acc479a2 136 if(GPIOx == GPIOA)
mbed_official 157:90e3acc479a2 137 {
mbed_official 157:90e3acc479a2 138 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE);
mbed_official 157:90e3acc479a2 139 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, DISABLE);
mbed_official 157:90e3acc479a2 140 }
mbed_official 157:90e3acc479a2 141 else if(GPIOx == GPIOB)
mbed_official 157:90e3acc479a2 142 {
mbed_official 157:90e3acc479a2 143 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE);
mbed_official 157:90e3acc479a2 144 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, DISABLE);
mbed_official 157:90e3acc479a2 145 }
mbed_official 157:90e3acc479a2 146 else if(GPIOx == GPIOC)
mbed_official 157:90e3acc479a2 147 {
mbed_official 157:90e3acc479a2 148 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, ENABLE);
mbed_official 157:90e3acc479a2 149 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, DISABLE);
mbed_official 157:90e3acc479a2 150 }
mbed_official 157:90e3acc479a2 151 else if(GPIOx == GPIOD)
mbed_official 157:90e3acc479a2 152 {
mbed_official 157:90e3acc479a2 153 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, ENABLE);
mbed_official 157:90e3acc479a2 154 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, DISABLE);
mbed_official 157:90e3acc479a2 155 }
mbed_official 157:90e3acc479a2 156 else if(GPIOx == GPIOE)
mbed_official 157:90e3acc479a2 157 {
mbed_official 157:90e3acc479a2 158 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, ENABLE);
mbed_official 157:90e3acc479a2 159 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, DISABLE);
mbed_official 157:90e3acc479a2 160 }
mbed_official 157:90e3acc479a2 161 else
mbed_official 157:90e3acc479a2 162 {
mbed_official 157:90e3acc479a2 163 if(GPIOx == GPIOF)
mbed_official 157:90e3acc479a2 164 {
mbed_official 157:90e3acc479a2 165 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF, ENABLE);
mbed_official 157:90e3acc479a2 166 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOF, DISABLE);
mbed_official 157:90e3acc479a2 167 }
mbed_official 157:90e3acc479a2 168 }
mbed_official 157:90e3acc479a2 169 }
mbed_official 157:90e3acc479a2 170
mbed_official 157:90e3acc479a2 171 /**
mbed_official 157:90e3acc479a2 172 * @brief Initializes the GPIOx peripheral according to the specified
mbed_official 157:90e3acc479a2 173 * parameters in the GPIO_InitStruct.
mbed_official 157:90e3acc479a2 174 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 175 * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
mbed_official 157:90e3acc479a2 176 * contains the configuration information for the specified GPIO
mbed_official 157:90e3acc479a2 177 * peripheral.
mbed_official 157:90e3acc479a2 178 * @note GPIO_Pin: selects the pin to be configured:
mbed_official 157:90e3acc479a2 179 * GPIO_Pin_0->GPIO_Pin_15 for GPIOA, GPIOB, GPIOC, GPIOD and GPIOE;
mbed_official 157:90e3acc479a2 180 * GPIO_Pin_0->GPIO_Pin_2, GPIO_Pin_4, GPIO_Pin_6, GPIO_Pin_9
mbed_official 157:90e3acc479a2 181 * and GPIO_Pin_10 for GPIOF.
mbed_official 157:90e3acc479a2 182 * @retval None
mbed_official 157:90e3acc479a2 183 */
mbed_official 157:90e3acc479a2 184 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
mbed_official 157:90e3acc479a2 185 {
mbed_official 157:90e3acc479a2 186 uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
mbed_official 157:90e3acc479a2 187 uint32_t tmpreg = 0x00;
mbed_official 157:90e3acc479a2 188
mbed_official 157:90e3acc479a2 189 /* Check the parameters */
mbed_official 157:90e3acc479a2 190 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 191 assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
mbed_official 157:90e3acc479a2 192 assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
mbed_official 157:90e3acc479a2 193 assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
mbed_official 157:90e3acc479a2 194
mbed_official 157:90e3acc479a2 195 /*-------------------------- Configure the port pins -----------------------*/
mbed_official 157:90e3acc479a2 196 /*-- GPIO Mode Configuration --*/
mbed_official 157:90e3acc479a2 197 for (pinpos = 0x00; pinpos < 0x10; pinpos++)
mbed_official 157:90e3acc479a2 198 {
mbed_official 157:90e3acc479a2 199 pos = ((uint32_t)0x01) << pinpos;
mbed_official 157:90e3acc479a2 200
mbed_official 157:90e3acc479a2 201 /* Get the port pins position */
mbed_official 157:90e3acc479a2 202 currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
mbed_official 157:90e3acc479a2 203
mbed_official 157:90e3acc479a2 204 if (currentpin == pos)
mbed_official 157:90e3acc479a2 205 {
mbed_official 157:90e3acc479a2 206 if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
mbed_official 157:90e3acc479a2 207 {
mbed_official 157:90e3acc479a2 208 /* Check Speed mode parameters */
mbed_official 157:90e3acc479a2 209 assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
mbed_official 157:90e3acc479a2 210
mbed_official 157:90e3acc479a2 211 /* Speed mode configuration */
mbed_official 157:90e3acc479a2 212 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
mbed_official 157:90e3acc479a2 213 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
mbed_official 157:90e3acc479a2 214
mbed_official 157:90e3acc479a2 215 /* Check Output mode parameters */
mbed_official 157:90e3acc479a2 216 assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
mbed_official 157:90e3acc479a2 217
mbed_official 157:90e3acc479a2 218 /* Output mode configuration */
mbed_official 157:90e3acc479a2 219 GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos));
mbed_official 157:90e3acc479a2 220 GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
mbed_official 157:90e3acc479a2 221 }
mbed_official 157:90e3acc479a2 222
mbed_official 157:90e3acc479a2 223 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
mbed_official 157:90e3acc479a2 224
mbed_official 157:90e3acc479a2 225 GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
mbed_official 157:90e3acc479a2 226
mbed_official 157:90e3acc479a2 227 /* Use temporary variable to update PUPDR register configuration, to avoid
mbed_official 157:90e3acc479a2 228 unexpected transition in the GPIO pin configuration. */
mbed_official 157:90e3acc479a2 229 tmpreg = GPIOx->PUPDR;
mbed_official 157:90e3acc479a2 230 tmpreg &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
mbed_official 157:90e3acc479a2 231 tmpreg |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
mbed_official 157:90e3acc479a2 232 GPIOx->PUPDR = tmpreg;
mbed_official 157:90e3acc479a2 233 }
mbed_official 157:90e3acc479a2 234 }
mbed_official 157:90e3acc479a2 235 }
mbed_official 157:90e3acc479a2 236
mbed_official 157:90e3acc479a2 237 /**
mbed_official 157:90e3acc479a2 238 * @brief Fills each GPIO_InitStruct member with its default value.
mbed_official 157:90e3acc479a2 239 * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure which will
mbed_official 157:90e3acc479a2 240 * be initialized.
mbed_official 157:90e3acc479a2 241 * @retval None
mbed_official 157:90e3acc479a2 242 */
mbed_official 157:90e3acc479a2 243 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
mbed_official 157:90e3acc479a2 244 {
mbed_official 157:90e3acc479a2 245 /* Reset GPIO init structure parameters values */
mbed_official 157:90e3acc479a2 246 GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
mbed_official 157:90e3acc479a2 247 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
mbed_official 157:90e3acc479a2 248 GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
mbed_official 157:90e3acc479a2 249 GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
mbed_official 157:90e3acc479a2 250 GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
mbed_official 157:90e3acc479a2 251 }
mbed_official 157:90e3acc479a2 252
mbed_official 157:90e3acc479a2 253 /**
mbed_official 157:90e3acc479a2 254 * @brief Locks GPIO Pins configuration registers.
mbed_official 157:90e3acc479a2 255 * The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
mbed_official 157:90e3acc479a2 256 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
mbed_official 157:90e3acc479a2 257 * @note The configuration of the locked GPIO pins can no longer be modified
mbed_official 157:90e3acc479a2 258 * until the next reset.
mbed_official 157:90e3acc479a2 259 * @param GPIOx: where x can be (A or B or D) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 260 * @param GPIO_Pin: specifies the port bit to be written.
mbed_official 157:90e3acc479a2 261 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
mbed_official 157:90e3acc479a2 262 * @retval None
mbed_official 157:90e3acc479a2 263 */
mbed_official 157:90e3acc479a2 264 void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
mbed_official 157:90e3acc479a2 265 {
mbed_official 157:90e3acc479a2 266 uint32_t tmp = 0x00010000;
mbed_official 157:90e3acc479a2 267
mbed_official 157:90e3acc479a2 268 /* Check the parameters */
mbed_official 157:90e3acc479a2 269 assert_param(IS_GPIO_LIST_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 270 assert_param(IS_GPIO_PIN(GPIO_Pin));
mbed_official 157:90e3acc479a2 271
mbed_official 157:90e3acc479a2 272 tmp |= GPIO_Pin;
mbed_official 157:90e3acc479a2 273 /* Set LCKK bit */
mbed_official 157:90e3acc479a2 274 GPIOx->LCKR = tmp;
mbed_official 157:90e3acc479a2 275 /* Reset LCKK bit */
mbed_official 157:90e3acc479a2 276 GPIOx->LCKR = GPIO_Pin;
mbed_official 157:90e3acc479a2 277 /* Set LCKK bit */
mbed_official 157:90e3acc479a2 278 GPIOx->LCKR = tmp;
mbed_official 157:90e3acc479a2 279 /* Read LCKK bit */
mbed_official 157:90e3acc479a2 280 tmp = GPIOx->LCKR;
mbed_official 157:90e3acc479a2 281 /* Read LCKK bit */
mbed_official 157:90e3acc479a2 282 tmp = GPIOx->LCKR;
mbed_official 157:90e3acc479a2 283 }
mbed_official 157:90e3acc479a2 284
mbed_official 157:90e3acc479a2 285 /**
mbed_official 157:90e3acc479a2 286 * @}
mbed_official 157:90e3acc479a2 287 */
mbed_official 157:90e3acc479a2 288
mbed_official 157:90e3acc479a2 289 /** @defgroup GPIO_Group2 GPIO Read and Write
mbed_official 157:90e3acc479a2 290 * @brief GPIO Read and Write
mbed_official 157:90e3acc479a2 291 *
mbed_official 157:90e3acc479a2 292 @verbatim
mbed_official 157:90e3acc479a2 293 ===============================================================================
mbed_official 157:90e3acc479a2 294 ##### GPIO Read and Write #####
mbed_official 157:90e3acc479a2 295 ===============================================================================
mbed_official 157:90e3acc479a2 296
mbed_official 157:90e3acc479a2 297 @endverbatim
mbed_official 157:90e3acc479a2 298 * @{
mbed_official 157:90e3acc479a2 299 */
mbed_official 157:90e3acc479a2 300
mbed_official 157:90e3acc479a2 301 /**
mbed_official 157:90e3acc479a2 302 * @brief Reads the specified input port pin.
mbed_official 157:90e3acc479a2 303 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 304 * @param GPIO_Pin: specifies the port bit to read.
mbed_official 157:90e3acc479a2 305 * @note This parameter can be GPIO_Pin_x where x can be :
mbed_official 157:90e3acc479a2 306 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
mbed_official 157:90e3acc479a2 307 * (0..2, 4, 6, 9..10) for GPIOF.
mbed_official 157:90e3acc479a2 308 * @retval The input port pin value.
mbed_official 157:90e3acc479a2 309 */
mbed_official 157:90e3acc479a2 310 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
mbed_official 157:90e3acc479a2 311 {
mbed_official 157:90e3acc479a2 312 uint8_t bitstatus = 0x00;
mbed_official 157:90e3acc479a2 313
mbed_official 157:90e3acc479a2 314 /* Check the parameters */
mbed_official 157:90e3acc479a2 315 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 316 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
mbed_official 157:90e3acc479a2 317
mbed_official 157:90e3acc479a2 318 if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
mbed_official 157:90e3acc479a2 319 {
mbed_official 157:90e3acc479a2 320 bitstatus = (uint8_t)Bit_SET;
mbed_official 157:90e3acc479a2 321 }
mbed_official 157:90e3acc479a2 322 else
mbed_official 157:90e3acc479a2 323 {
mbed_official 157:90e3acc479a2 324 bitstatus = (uint8_t)Bit_RESET;
mbed_official 157:90e3acc479a2 325 }
mbed_official 157:90e3acc479a2 326 return bitstatus;
mbed_official 157:90e3acc479a2 327 }
mbed_official 157:90e3acc479a2 328
mbed_official 157:90e3acc479a2 329 /**
mbed_official 157:90e3acc479a2 330 * @brief Reads the specified input port pin.
mbed_official 157:90e3acc479a2 331 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 332 * @retval The input port pin value.
mbed_official 157:90e3acc479a2 333 */
mbed_official 157:90e3acc479a2 334 uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
mbed_official 157:90e3acc479a2 335 {
mbed_official 157:90e3acc479a2 336 /* Check the parameters */
mbed_official 157:90e3acc479a2 337 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 338
mbed_official 157:90e3acc479a2 339 return ((uint16_t)GPIOx->IDR);
mbed_official 157:90e3acc479a2 340 }
mbed_official 157:90e3acc479a2 341
mbed_official 157:90e3acc479a2 342 /**
mbed_official 157:90e3acc479a2 343 * @brief Reads the specified output data port bit.
mbed_official 157:90e3acc479a2 344 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 345 * @param GPIO_Pin: Specifies the port bit to read.
mbed_official 157:90e3acc479a2 346 * @note This parameter can be GPIO_Pin_x where x can be :
mbed_official 157:90e3acc479a2 347 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
mbed_official 157:90e3acc479a2 348 * (0..2, 4, 6, 9..10) for GPIOF.
mbed_official 157:90e3acc479a2 349 * @retval The output port pin value.
mbed_official 157:90e3acc479a2 350 */
mbed_official 157:90e3acc479a2 351 uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
mbed_official 157:90e3acc479a2 352 {
mbed_official 157:90e3acc479a2 353 uint8_t bitstatus = 0x00;
mbed_official 157:90e3acc479a2 354
mbed_official 157:90e3acc479a2 355 /* Check the parameters */
mbed_official 157:90e3acc479a2 356 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 357 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
mbed_official 157:90e3acc479a2 358
mbed_official 157:90e3acc479a2 359 if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
mbed_official 157:90e3acc479a2 360 {
mbed_official 157:90e3acc479a2 361 bitstatus = (uint8_t)Bit_SET;
mbed_official 157:90e3acc479a2 362 }
mbed_official 157:90e3acc479a2 363 else
mbed_official 157:90e3acc479a2 364 {
mbed_official 157:90e3acc479a2 365 bitstatus = (uint8_t)Bit_RESET;
mbed_official 157:90e3acc479a2 366 }
mbed_official 157:90e3acc479a2 367 return bitstatus;
mbed_official 157:90e3acc479a2 368 }
mbed_official 157:90e3acc479a2 369
mbed_official 157:90e3acc479a2 370 /**
mbed_official 157:90e3acc479a2 371 * @brief Reads the specified GPIO output data port.
mbed_official 157:90e3acc479a2 372 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 373 * @retval GPIO output data port value.
mbed_official 157:90e3acc479a2 374 */
mbed_official 157:90e3acc479a2 375 uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
mbed_official 157:90e3acc479a2 376 {
mbed_official 157:90e3acc479a2 377 /* Check the parameters */
mbed_official 157:90e3acc479a2 378 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 379
mbed_official 157:90e3acc479a2 380 return ((uint16_t)GPIOx->ODR);
mbed_official 157:90e3acc479a2 381 }
mbed_official 157:90e3acc479a2 382
mbed_official 157:90e3acc479a2 383 /**
mbed_official 157:90e3acc479a2 384 * @brief Sets the selected data port bits.
mbed_official 157:90e3acc479a2 385 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 386 * @param GPIO_Pin: specifies the port bits to be written.
mbed_official 157:90e3acc479a2 387 * @note This parameter can be GPIO_Pin_x where x can be :
mbed_official 157:90e3acc479a2 388 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
mbed_official 157:90e3acc479a2 389 * (0..2, 4, 6, 9..10) for GPIOF.
mbed_official 157:90e3acc479a2 390 * @retval None
mbed_official 157:90e3acc479a2 391 */
mbed_official 157:90e3acc479a2 392 void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
mbed_official 157:90e3acc479a2 393 {
mbed_official 157:90e3acc479a2 394 /* Check the parameters */
mbed_official 157:90e3acc479a2 395 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 396 assert_param(IS_GPIO_PIN(GPIO_Pin));
mbed_official 157:90e3acc479a2 397
mbed_official 157:90e3acc479a2 398 GPIOx->BSRR = GPIO_Pin;
mbed_official 157:90e3acc479a2 399 }
mbed_official 157:90e3acc479a2 400
mbed_official 157:90e3acc479a2 401 /**
mbed_official 157:90e3acc479a2 402 * @brief Clears the selected data port bits.
mbed_official 157:90e3acc479a2 403 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 404 * @param GPIO_Pin: specifies the port bits to be written.
mbed_official 157:90e3acc479a2 405 * @note This parameter can be GPIO_Pin_x where x can be :
mbed_official 157:90e3acc479a2 406 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
mbed_official 157:90e3acc479a2 407 * (0..2, 4, 6, 9..10) for GPIOF.
mbed_official 157:90e3acc479a2 408 * @retval None
mbed_official 157:90e3acc479a2 409 */
mbed_official 157:90e3acc479a2 410 void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
mbed_official 157:90e3acc479a2 411 {
mbed_official 157:90e3acc479a2 412 /* Check the parameters */
mbed_official 157:90e3acc479a2 413 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 414 assert_param(IS_GPIO_PIN(GPIO_Pin));
mbed_official 157:90e3acc479a2 415
mbed_official 157:90e3acc479a2 416 GPIOx->BRR = GPIO_Pin;
mbed_official 157:90e3acc479a2 417 }
mbed_official 157:90e3acc479a2 418
mbed_official 157:90e3acc479a2 419 /**
mbed_official 157:90e3acc479a2 420 * @brief Sets or clears the selected data port bit.
mbed_official 157:90e3acc479a2 421 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 422 * @param GPIO_Pin: specifies the port bit to be written.
mbed_official 157:90e3acc479a2 423 * @note This parameter can be GPIO_Pin_x where x can be :
mbed_official 157:90e3acc479a2 424 * (0..15) for GPIOA, GPIOB, GPIOC, GPIOD or GPIOE;
mbed_official 157:90e3acc479a2 425 * (0..2, 4, 6, 9..10) for GPIOF.
mbed_official 157:90e3acc479a2 426 * @param BitVal: specifies the value to be written to the selected bit.
mbed_official 157:90e3acc479a2 427 * This parameter can be one of the BitAction enumeration values:
mbed_official 157:90e3acc479a2 428 * @arg Bit_RESET: to clear the port pin
mbed_official 157:90e3acc479a2 429 * @arg Bit_SET: to set the port pin
mbed_official 157:90e3acc479a2 430 * @retval None
mbed_official 157:90e3acc479a2 431 */
mbed_official 157:90e3acc479a2 432 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
mbed_official 157:90e3acc479a2 433 {
mbed_official 157:90e3acc479a2 434 /* Check the parameters */
mbed_official 157:90e3acc479a2 435 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 436 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
mbed_official 157:90e3acc479a2 437 assert_param(IS_GPIO_BIT_ACTION(BitVal));
mbed_official 157:90e3acc479a2 438
mbed_official 157:90e3acc479a2 439 if (BitVal != Bit_RESET)
mbed_official 157:90e3acc479a2 440 {
mbed_official 157:90e3acc479a2 441 GPIOx->BSRR = GPIO_Pin;
mbed_official 157:90e3acc479a2 442 }
mbed_official 157:90e3acc479a2 443 else
mbed_official 157:90e3acc479a2 444 {
mbed_official 157:90e3acc479a2 445 GPIOx->BRR = GPIO_Pin ;
mbed_official 157:90e3acc479a2 446 }
mbed_official 157:90e3acc479a2 447 }
mbed_official 157:90e3acc479a2 448
mbed_official 157:90e3acc479a2 449 /**
mbed_official 157:90e3acc479a2 450 * @brief Writes data to the specified GPIO data port.
mbed_official 157:90e3acc479a2 451 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 452 * @param PortVal: specifies the value to be written to the port output data
mbed_official 157:90e3acc479a2 453 * register.
mbed_official 157:90e3acc479a2 454 * @retval None
mbed_official 157:90e3acc479a2 455 */
mbed_official 157:90e3acc479a2 456 void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
mbed_official 157:90e3acc479a2 457 {
mbed_official 157:90e3acc479a2 458 /* Check the parameters */
mbed_official 157:90e3acc479a2 459 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 460
mbed_official 157:90e3acc479a2 461 GPIOx->ODR = PortVal;
mbed_official 157:90e3acc479a2 462 }
mbed_official 157:90e3acc479a2 463
mbed_official 157:90e3acc479a2 464 /**
mbed_official 157:90e3acc479a2 465 * @}
mbed_official 157:90e3acc479a2 466 */
mbed_official 157:90e3acc479a2 467
mbed_official 157:90e3acc479a2 468 /** @defgroup GPIO_Group3 GPIO Alternate functions configuration functions
mbed_official 157:90e3acc479a2 469 * @brief GPIO Alternate functions configuration functions
mbed_official 157:90e3acc479a2 470 *
mbed_official 157:90e3acc479a2 471 @verbatim
mbed_official 157:90e3acc479a2 472 ===============================================================================
mbed_official 157:90e3acc479a2 473 ##### GPIO Alternate functions configuration functions #####
mbed_official 157:90e3acc479a2 474 ===============================================================================
mbed_official 157:90e3acc479a2 475
mbed_official 157:90e3acc479a2 476 @endverbatim
mbed_official 157:90e3acc479a2 477 * @{
mbed_official 157:90e3acc479a2 478 */
mbed_official 157:90e3acc479a2 479
mbed_official 157:90e3acc479a2 480 /**
mbed_official 157:90e3acc479a2 481 * @brief Writes data to the specified GPIO data port.
mbed_official 157:90e3acc479a2 482 * @param GPIOx: where x can be (A, B, C, D, E or F) to select the GPIO peripheral.
mbed_official 157:90e3acc479a2 483 * @param GPIO_PinSource: specifies the pin for the Alternate function.
mbed_official 157:90e3acc479a2 484 * This parameter can be GPIO_PinSourcex where x can be (0..15).
mbed_official 157:90e3acc479a2 485 * @param GPIO_AF: selects the pin to be used as Alternate function.
mbed_official 157:90e3acc479a2 486 * This parameter can be one of the following value:
mbed_official 157:90e3acc479a2 487 * @arg GPIO_AF_0: JTCK-SWCLK, JTDI, JTDO/TRACESW0, JTMS-SWDAT, MCO, NJTRST,
mbed_official 157:90e3acc479a2 488 * TRACED, TRACECK.
mbed_official 157:90e3acc479a2 489 * @arg GPIO_AF_1: OUT, TIM2, TIM15, TIM16, TIM17.
mbed_official 157:90e3acc479a2 490 * @arg GPIO_AF_2: COMP1_OUT, TIM1, TIM2, TIM3, TIM4, TIM8, TIM15, TIM16.
mbed_official 157:90e3acc479a2 491 * @arg GPIO_AF_3: COMP7_OUT, TIM8, TIM15, Touch, HRTIM.
mbed_official 157:90e3acc479a2 492 * @arg GPIO_AF_4: I2C1, I2C2, TIM1, TIM8, TIM16, TIM17.
mbed_official 157:90e3acc479a2 493 * @arg GPIO_AF_5: IR_OUT, I2S2, I2S3, SPI1, SPI2, TIM8, USART4, USART5
mbed_official 157:90e3acc479a2 494 * @arg GPIO_AF_6: IR_OUT, I2S2, I2S3, SPI2, SPI3, TIM1, TIM8
mbed_official 157:90e3acc479a2 495 * @arg GPIO_AF_7: AOP2_OUT, CAN, COMP3_OUT, COMP5_OUT, COMP6_OUT, USART1,
mbed_official 157:90e3acc479a2 496 * USART2, USART3.
mbed_official 157:90e3acc479a2 497 * @arg GPIO_AF_8: COMP1_OUT, COMP2_OUT, COMP3_OUT, COMP4_OUT, COMP5_OUT,
mbed_official 157:90e3acc479a2 498 * COMP6_OUT.
mbed_official 157:90e3acc479a2 499 * @arg GPIO_AF_9: AOP4_OUT, CAN, TIM1, TIM8, TIM15.
mbed_official 157:90e3acc479a2 500 * @arg GPIO_AF_10: AOP1_OUT, AOP3_OUT, TIM2, TIM3, TIM4, TIM8, TIM17.
mbed_official 157:90e3acc479a2 501 * @arg GPIO_AF_11: TIM1, TIM8.
mbed_official 157:90e3acc479a2 502 * @arg GPIO_AF_12: TIM1, HRTIM.
mbed_official 157:90e3acc479a2 503 * @arg GPIO_AF_13: HRTIM, AOP2_OUT.
mbed_official 157:90e3acc479a2 504 * @arg GPIO_AF_14: USBDM, USBDP.
mbed_official 157:90e3acc479a2 505 * @arg GPIO_AF_15: OUT.
mbed_official 157:90e3acc479a2 506 * @note The pin should already been configured in Alternate Function mode(AF)
mbed_official 157:90e3acc479a2 507 * using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
mbed_official 157:90e3acc479a2 508 * @note Refer to the Alternate function mapping table in the device datasheet
mbed_official 157:90e3acc479a2 509 * for the detailed mapping of the system and peripherals alternate
mbed_official 157:90e3acc479a2 510 * function I/O pins.
mbed_official 157:90e3acc479a2 511 * @retval None
mbed_official 157:90e3acc479a2 512 */
mbed_official 157:90e3acc479a2 513 void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
mbed_official 157:90e3acc479a2 514 {
mbed_official 157:90e3acc479a2 515 uint32_t temp = 0x00;
mbed_official 157:90e3acc479a2 516 uint32_t temp_2 = 0x00;
mbed_official 157:90e3acc479a2 517
mbed_official 157:90e3acc479a2 518 /* Check the parameters */
mbed_official 157:90e3acc479a2 519 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
mbed_official 157:90e3acc479a2 520 assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
mbed_official 157:90e3acc479a2 521 assert_param(IS_GPIO_AF(GPIO_AF));
mbed_official 157:90e3acc479a2 522
mbed_official 157:90e3acc479a2 523 temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
mbed_official 157:90e3acc479a2 524 GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
mbed_official 157:90e3acc479a2 525 temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
mbed_official 157:90e3acc479a2 526 GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
mbed_official 157:90e3acc479a2 527 }
mbed_official 157:90e3acc479a2 528
mbed_official 157:90e3acc479a2 529 /**
mbed_official 157:90e3acc479a2 530 * @}
mbed_official 157:90e3acc479a2 531 */
mbed_official 157:90e3acc479a2 532
mbed_official 157:90e3acc479a2 533 /**
mbed_official 157:90e3acc479a2 534 * @}
mbed_official 157:90e3acc479a2 535 */
mbed_official 157:90e3acc479a2 536
mbed_official 157:90e3acc479a2 537 /**
mbed_official 157:90e3acc479a2 538 * @}
mbed_official 157:90e3acc479a2 539 */
mbed_official 157:90e3acc479a2 540
mbed_official 157:90e3acc479a2 541 /**
mbed_official 157:90e3acc479a2 542 * @}
mbed_official 157:90e3acc479a2 543 */
mbed_official 157:90e3acc479a2 544
mbed_official 157:90e3acc479a2 545 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/