2 ******************************************************************************
\r
3 * @file stm32l1xx_gpio.c
\r
4 * @author MCD Application Team
\r
6 * @date 31-December-2010
\r
7 * @brief This file provides firmware functions to manage the following
\r
8 * functionalities of the GPIO peripheral:
\r
9 * - Initialization and Configuration
\r
10 * - GPIO Read and Write
\r
11 * - GPIO Alternate functions configuration
\r
15 * ===================================================================
\r
16 * How to use this driver
\r
17 * ===================================================================
\r
18 * 1. Enable the GPIO AHB clock using RCC_AHBPeriphClockCmd()
\r
20 * 2. Configure the GPIO pin(s) using GPIO_Init()
\r
21 * Four possible configuration are available for each pin:
\r
22 * - Input: Floating, Pull-up, Pull-down.
\r
23 * - Output: Push-Pull (Pull-up, Pull-down or no Pull)
\r
24 * Open Drain (Pull-up, Pull-down or no Pull).
\r
25 * In output mode, the speed is configurable: Very Low, Low,
\r
27 * - Alternate Function: Push-Pull (Pull-up, Pull-down or no Pull)
\r
28 * Open Drain (Pull-up, Pull-down or no Pull).
\r
29 * - Analog: required mode when a pin is to be used as ADC channel,
\r
30 * DAC output or comparator input.
\r
32 * 3- Peripherals alternate function:
\r
33 * - For ADC, DAC and comparators, configure the desired pin in
\r
34 * analog mode using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AN
\r
35 * - For other peripherals (TIM, USART...):
\r
36 * - Connect the pin to the desired peripherals' Alternate
\r
37 * Function (AF) using GPIO_PinAFConfig() function
\r
38 * - Configure the desired pin in alternate function mode using
\r
39 * GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
\r
40 * - Select the type, pull-up/pull-down and output speed via
\r
41 * GPIO_PuPd, GPIO_OType and GPIO_Speed members
\r
42 * - Call GPIO_Init() function
\r
44 * 4. To get the level of a pin configured in input mode use GPIO_ReadInputDataBit()
\r
46 * 5. To set/reset the level of a pin configured in output mode use
\r
47 * GPIO_SetBits()/GPIO_ResetBits()
\r
49 * 6. During and just after reset, the alternate functions are not
\r
50 * active and the GPIO pins are configured in input floating mode
\r
51 * (except JTAG pins).
\r
53 * 7. The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as
\r
54 * general-purpose (PC14 and PC15, respectively) when the LSE
\r
55 * oscillator is off. The LSE has priority over the GPIO function.
\r
57 * 8. The HSE oscillator pins OSC_IN/OSC_OUT can be used as
\r
58 * general-purpose PH0 and PH1, respectively, when the HSE
\r
59 * oscillator is off. The HSE has priority over the GPIO function.
\r
63 ******************************************************************************
\r
66 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
67 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
\r
68 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
\r
69 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
\r
70 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
\r
71 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
73 * <h2><center>© COPYRIGHT 2010 STMicroelectronics</center></h2>
\r
74 ******************************************************************************
\r
77 /* Includes ------------------------------------------------------------------*/
\r
78 #include "stm32l1xx_gpio.h"
\r
79 #include "stm32l1xx_rcc.h"
\r
81 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
86 * @brief GPIO driver modules
\r
90 /* Private typedef -----------------------------------------------------------*/
\r
91 /* Private define ------------------------------------------------------------*/
\r
92 /* Private macro -------------------------------------------------------------*/
\r
93 /* Private variables ---------------------------------------------------------*/
\r
94 /* Private function prototypes -----------------------------------------------*/
\r
95 /* Private functions ---------------------------------------------------------*/
\r
97 /** @defgroup GPIO_Private_Functions
\r
101 /** @defgroup GPIO_Group1 Initialization and Configuration
\r
102 * @brief Initialization and Configuration
\r
105 ===============================================================================
\r
106 Initialization and Configuration
\r
107 ===============================================================================
\r
114 * @brief Deinitializes the GPIOx peripheral registers to their default reset
\r
116 * By default, The GPIO pins are configured in input floating mode
\r
117 * (except JTAG pins).
\r
118 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
121 void GPIO_DeInit(GPIO_TypeDef* GPIOx)
\r
123 /* Check the parameters */
\r
124 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
128 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE);
\r
129 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, DISABLE);
\r
131 else if(GPIOx == GPIOB)
\r
133 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE);
\r
134 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, DISABLE);
\r
136 else if(GPIOx == GPIOC)
\r
138 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, ENABLE);
\r
139 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, DISABLE);
\r
141 else if(GPIOx == GPIOD)
\r
143 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, ENABLE);
\r
144 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, DISABLE);
\r
146 else if(GPIOx == GPIOE)
\r
148 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, ENABLE);
\r
149 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, DISABLE);
\r
155 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOH, ENABLE);
\r
156 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOH, DISABLE);
\r
162 * @brief Initializes the GPIOx peripheral according to the specified
\r
163 * parameters in the GPIO_InitStruct.
\r
164 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
165 * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
\r
166 * contains the configuration information for the specified GPIO
\r
168 * GPIO_Pin: selects the pin to be configured: GPIO_Pin_0 -> GPIO_Pin_15
\r
169 * GPIO_Mode: selects the mode of the pin:
\r
170 * - Input mode: GPIO_Mode_IN
\r
171 * - Output mode: GPIO_Mode_OUT
\r
172 * - Alternate Function mode: GPIO_Mode_AF
\r
173 * - Analog mode: GPIO_Mode_AN
\r
174 * GPIO_Speed: selects the speed of the pin if configured in Output:
\r
175 * - Very Low: GPIO_Speed_400KHz
\r
176 * - Low: GPIO_Speed_2MHz
\r
177 * - Medium: GPIO_Speed_10MHz
\r
178 * - High: GPIO_Speed_40MHz
\r
179 * GPIO_OType: selects the Output type (if the selected mode is output):
\r
180 * - Push-pull: GPIO_OType_PP
\r
181 * - Open Drain: GPIO_OType_OD
\r
182 * GPIO_PuPd: configures the Pull-up/Pull-down resistor on the pin:
\r
183 * - pull-up: GPIO_PuPd_UP
\r
184 * - pull-down: GPIO_PuPd_DOWN
\r
185 * - Neither pull-up nor Pull-down: GPIO_PuPd_NOPULL
\r
188 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
\r
190 uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
\r
192 /* Check the parameters */
\r
193 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
194 assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
\r
195 assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
\r
196 assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
\r
198 /* -------------------------Configure the port pins---------------- */
\r
199 /*-- GPIO Mode Configuration --*/
\r
200 for (pinpos = 0x00; pinpos < 0x10; pinpos++)
\r
202 pos = ((uint32_t)0x01) << pinpos;
\r
204 /* Get the port pins position */
\r
205 currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
\r
207 if (currentpin == pos)
\r
209 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
\r
211 GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
\r
213 if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
\r
215 /* Check Speed mode parameters */
\r
216 assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
\r
218 /* Speed mode configuration */
\r
219 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
\r
220 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
\r
222 /*Check Output mode parameters */
\r
223 assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
\r
225 /* Output mode configuration */
\r
226 GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos)) ;
\r
227 GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
\r
230 /* Pull-up Pull down resistor configuration */
\r
231 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
\r
232 GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
\r
238 * @brief Fills each GPIO_InitStruct member with its default value.
\r
239 * @param GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure which will
\r
243 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
\r
245 /* Reset GPIO init structure parameters values */
\r
246 GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
\r
247 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
\r
248 GPIO_InitStruct->GPIO_Speed = GPIO_Speed_400KHz;
\r
249 GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
\r
250 GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
\r
254 * @brief Locks GPIO Pins configuration registers.
\r
255 * The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
\r
256 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
\r
257 * The configuration of the locked GPIO pins can no longer be modified
\r
258 * until the next reset.
\r
259 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
260 * @param GPIO_Pin: specifies the port bit to be written.
\r
261 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
\r
264 void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
266 uint32_t tmp = 0x00010000;
\r
268 /* Check the parameters */
\r
269 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
270 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
275 /* Reset LCKK bit */
\r
276 GPIOx->LCKR = GPIO_Pin;
\r
289 /** @defgroup GPIO_Group2 GPIO Read and Write
\r
290 * @brief GPIO Read and Write
\r
293 ===============================================================================
\r
294 GPIO Read and Write
\r
295 ===============================================================================
\r
302 * @brief Reads the specified input port pin.
\r
303 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
304 * @param GPIO_Pin: specifies the port bit to read.
\r
305 * This parameter can be GPIO_Pin_x where x can be (0..15).
\r
306 * @retval The input port pin value.
\r
308 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
310 uint8_t bitstatus = 0x00;
\r
312 /* Check the parameters */
\r
313 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
314 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
\r
316 if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
\r
318 bitstatus = (uint8_t)Bit_SET;
\r
322 bitstatus = (uint8_t)Bit_RESET;
\r
328 * @brief Reads the specified GPIO input data port.
\r
329 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
330 * @retval GPIO input data port value.
\r
332 uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
\r
334 /* Check the parameters */
\r
335 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
337 return ((uint16_t)GPIOx->IDR);
\r
341 * @brief Reads the specified output data port bit.
\r
342 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
343 * @param GPIO_Pin: Specifies the port bit to read.
\r
344 * This parameter can be GPIO_Pin_x where x can be (0..15).
\r
345 * @retval The output port pin value.
\r
347 uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
349 uint8_t bitstatus = 0x00;
\r
351 /* Check the parameters */
\r
352 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
353 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
\r
355 if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
\r
357 bitstatus = (uint8_t)Bit_SET;
\r
361 bitstatus = (uint8_t)Bit_RESET;
\r
367 * @brief Reads the specified GPIO output data port.
\r
368 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
369 * @retval GPIO output data port value.
\r
371 uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
\r
373 /* Check the parameters */
\r
374 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
376 return ((uint16_t)GPIOx->ODR);
\r
380 * @brief Sets the selected data port bits.
\r
381 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
382 * @param GPIO_Pin: specifies the port bits to be written.
\r
383 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
\r
384 * @note This functions uses GPIOx_BSRR register to allow atomic read/modify
\r
385 * accesses. In this way, there is no risk of an IRQ occurring between
\r
386 * the read and the modify access.
\r
389 void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
391 /* Check the parameters */
\r
392 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
393 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
395 GPIOx->BSRRL = GPIO_Pin;
\r
399 * @brief Clears the selected data port bits.
\r
400 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
401 * @param GPIO_Pin: specifies the port bits to be written.
\r
402 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
\r
403 * @note This functions uses GPIOx_BSRR register to allow atomic read/modify
\r
404 * accesses. In this way, there is no risk of an IRQ occurring between
\r
405 * the read and the modify access.
\r
408 void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
410 /* Check the parameters */
\r
411 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
412 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
414 GPIOx->BSRRH = GPIO_Pin;
\r
418 * @brief Sets or clears the selected data port bit.
\r
419 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
420 * @param GPIO_Pin: specifies the port bit to be written.
\r
421 * This parameter can be one of GPIO_Pin_x where x can be (0..15).
\r
422 * @param BitVal: specifies the value to be written to the selected bit.
\r
423 * This parameter can be one of the BitAction enum values:
\r
424 * @arg Bit_RESET: to clear the port pin
\r
425 * @arg Bit_SET: to set the port pin
\r
428 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
\r
430 /* Check the parameters */
\r
431 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
432 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
\r
433 assert_param(IS_GPIO_BIT_ACTION(BitVal));
\r
435 if (BitVal != Bit_RESET)
\r
437 GPIOx->BSRRL = GPIO_Pin;
\r
441 GPIOx->BSRRH = GPIO_Pin ;
\r
446 * @brief Writes data to the specified GPIO data port.
\r
447 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
448 * @param PortVal: specifies the value to be written to the port output data
\r
452 void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
\r
454 /* Check the parameters */
\r
455 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
457 GPIOx->ODR = PortVal;
\r
464 /** @defgroup GPIO_Group3 GPIO Alternate functions configuration functions
\r
465 * @brief GPIO Alternate functions configuration functions
\r
468 ===============================================================================
\r
469 GPIO Alternate functions configuration functions
\r
470 ===============================================================================
\r
477 * @brief Changes the mapping of the specified pin.
\r
478 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
479 * @param GPIO_PinSource: specifies the pin for the Alternate function.
\r
480 * This parameter can be GPIO_PinSourcex where x can be (0..15).
\r
481 * @param GPIO_AFSelection: selects the pin to used as Alternat function.
\r
482 * This parameter can be one of the following values:
\r
483 * @arg GPIO_AF_RTC_50Hz: RTC 50/60 Hz synchronization
\r
484 * @arg GPIO_AF_MCO: Microcontroller clock output
\r
485 * @arg GPIO_AF_RTC_AF1: Time stamp, Tamper, Alarm A out, Alarm B out,
\r
486 * 512 Hz clock output (with an LSE oscillator of 32.768 kHz)
\r
487 * @arg GPIO_AF_WKUP: wakeup
\r
488 * @arg GPIO_AF_SWJ: SWJ (SW and JTAG)
\r
489 * @arg GPIO_AF_TRACE
\r
490 * @arg GPIO_AF_TIM2
\r
491 * @arg GPIO_AF_TIM3
\r
492 * @arg GPIO_AF_TIM4
\r
493 * @arg GPIO_AF_TIM9
\r
494 * @arg GPIO_AF_TIM10
\r
495 * @arg GPIO_AF_TIM11
\r
496 * @arg GPIO_AF_I2C1
\r
497 * @arg GPIO_AF_I2C2
\r
498 * @arg GPIO_AF_SPI1
\r
499 * @arg GPIO_AF_SPI2
\r
500 * @arg GPIO_AF_USART1
\r
501 * @arg GPIO_AF_USART2
\r
502 * @arg GPIO_AF_USART3
\r
506 * @arg GPIO_AF_EVENTOUT: Cortex-M3 EVENTOUT signal
\r
507 * @note: The pin should already been configured in Alternate Function mode(AF)
\r
508 * using GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
\r
509 * @note: Please refer to the Alternate function mapping table in the device
\r
510 * datasheet for the detailed mapping of the system and peripherals
\92\r
511 * alternate function I/O pins.
\r
512 * @note: EVENTOUT is not mapped on PH0, PH1 and PH2.
\r
515 void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
\r
517 uint32_t temp = 0x00;
\r
518 uint32_t temp_2 = 0x00;
\r
520 /* Check the parameters */
\r
521 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
522 assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
\r
523 assert_param(IS_GPIO_AF(GPIO_AF));
\r
525 temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
\r
526 GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
\r
527 temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
\r
528 GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
\r
546 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
\r