2 ******************************************************************************
\r
3 * @file stm32l1xx_gpio.c
\r
4 * @author MCD Application Team
\r
7 * @brief This file provides all the GPIO firmware functions.
\r
8 ******************************************************************************
\r
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
\r
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
\r
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
\r
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
\r
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
18 * <h2><center>© COPYRIGHT 2010 STMicroelectronics</center></h2>
\r
21 /* Includes ------------------------------------------------------------------*/
\r
22 #include "stm32l1xx_gpio.h"
\r
23 #include "stm32l1xx_rcc.h"
\r
25 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
30 * @brief GPIO driver modules
\r
34 /** @defgroup GPIO_Private_TypesDefinitions
\r
42 /** @defgroup GPIO_Private_Defines
\r
50 /** @defgroup GPIO_Private_Macros
\r
57 /** @defgroup GPIO_Private_Variables
\r
64 /** @defgroup GPIO_Private_FunctionPrototypes
\r
71 /** @defgroup GPIO_Private_Functions
\r
76 * @brief Deinitializes the GPIOx peripheral registers to their default reset
\r
78 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
81 void GPIO_DeInit(GPIO_TypeDef* GPIOx)
\r
83 /* Check the parameters */
\r
84 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
88 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE);
\r
89 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, DISABLE);
\r
91 else if(GPIOx == GPIOB)
\r
93 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE);
\r
94 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, DISABLE);
\r
96 else if(GPIOx == GPIOC)
\r
98 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, ENABLE);
\r
99 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, DISABLE);
\r
101 else if(GPIOx == GPIOD)
\r
103 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, ENABLE);
\r
104 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, DISABLE);
\r
106 else if(GPIOx == GPIOE)
\r
108 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, ENABLE);
\r
109 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, DISABLE);
\r
115 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOH, ENABLE);
\r
116 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOH, DISABLE);
\r
122 * @brief Initializes the GPIOx peripheral according to the specified
\r
123 * parameters in the GPIO_InitStruct.
\r
124 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
125 * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
\r
126 * contains the configuration information for the specified GPIO
\r
130 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
\r
132 uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;
\r
134 /* Check the parameters */
\r
135 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
136 assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
\r
137 assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
\r
138 assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));
\r
140 /* -------------------------Configure the port pins---------------- */
\r
141 /*-- GPIO Mode Configuration --*/
\r
142 for (pinpos = 0x00; pinpos < 0x10; pinpos++)
\r
144 pos = ((uint32_t)0x01) << pinpos;
\r
146 /* Get the port pins position */
\r
147 currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
\r
149 if (currentpin == pos)
\r
151 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
\r
153 GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
\r
155 if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
\r
157 /*Check Speed mode parameters */
\r
158 assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
\r
160 /*Speed mode configuration */
\r
161 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
\r
162 GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));
\r
164 /*Check Output mode parameters */
\r
165 assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));
\r
167 /* Output mode configuartion*/
\r
168 GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos)) ;
\r
169 GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
\r
172 /*Pull-up Pull down resistor configuration*/
\r
173 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
\r
174 GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
\r
180 * @brief Fills each GPIO_InitStruct member with its default value.
\r
181 * @param GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure which will
\r
185 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
\r
187 /* Reset GPIO init structure parameters values */
\r
188 GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
\r
189 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;
\r
190 GPIO_InitStruct->GPIO_Speed = GPIO_Speed_400KHz;
\r
191 GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;
\r
192 GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;
\r
196 * @brief Reads the specified input port pin.
\r
197 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
198 * @param GPIO_Pin: specifies the port bit to read.
\r
199 * This parameter can be GPIO_Pin_x where x can be (0..15).
\r
200 * @retval The input port pin value.
\r
202 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
204 uint8_t bitstatus = 0x00;
\r
206 /* Check the parameters */
\r
207 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
208 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
\r
210 if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)
\r
212 bitstatus = (uint8_t)Bit_SET;
\r
216 bitstatus = (uint8_t)Bit_RESET;
\r
222 * @brief Reads the specified GPIO input data port.
\r
223 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
224 * @retval GPIO input data port value.
\r
226 uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
\r
228 /* Check the parameters */
\r
229 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
231 return ((uint16_t)GPIOx->IDR);
\r
235 * @brief Reads the specified output data port bit.
\r
236 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
237 * @param GPIO_Pin: Specifies the port bit to read.
\r
238 * This parameter can be GPIO_Pin_x where x can be (0..15).
\r
239 * @retval The output port pin value.
\r
241 uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
243 uint8_t bitstatus = 0x00;
\r
245 /* Check the parameters */
\r
246 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
247 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
\r
249 if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
\r
251 bitstatus = (uint8_t)Bit_SET;
\r
255 bitstatus = (uint8_t)Bit_RESET;
\r
261 * @brief Reads the specified GPIO output data port.
\r
262 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
263 * @retval GPIO output data port value.
\r
265 uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
\r
267 /* Check the parameters */
\r
268 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
270 return ((uint16_t)GPIOx->ODR);
\r
274 * @brief Sets the selected data port bits.
\r
275 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
276 * @param GPIO_Pin: specifies the port bits to be written.
\r
277 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
\r
280 void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
282 /* Check the parameters */
\r
283 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
284 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
286 GPIOx->BSRRL = GPIO_Pin;
\r
290 * @brief Clears the selected data port bits.
\r
291 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
292 * @param GPIO_Pin: specifies the port bits to be written.
\r
293 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
\r
296 void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
298 /* Check the parameters */
\r
299 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
300 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
302 GPIOx->BSRRH = GPIO_Pin;
\r
306 * @brief Sets or clears the selected data port bit.
\r
307 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
308 * @param GPIO_Pin: specifies the port bit to be written.
\r
309 * This parameter can be one of GPIO_Pin_x where x can be (0..15).
\r
310 * @param BitVal: specifies the value to be written to the selected bit.
\r
311 * This parameter can be one of the BitAction enum values:
\r
312 * @arg Bit_RESET: to clear the port pin
\r
313 * @arg Bit_SET: to set the port pin
\r
316 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
\r
318 /* Check the parameters */
\r
319 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
320 assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
\r
321 assert_param(IS_GPIO_BIT_ACTION(BitVal));
\r
323 if (BitVal != Bit_RESET)
\r
325 GPIOx->BSRRL = GPIO_Pin;
\r
329 GPIOx->BSRRH = GPIO_Pin ;
\r
334 * @brief Writes data to the specified GPIO data port.
\r
335 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
336 * @param PortVal: specifies the value to be written to the port output data
\r
340 void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
\r
342 /* Check the parameters */
\r
343 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
345 GPIOx->ODR = PortVal;
\r
349 * @brief Locks GPIO Pins configuration registers.
\r
350 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
351 * @param GPIO_Pin: specifies the port bit to be written.
\r
352 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
\r
355 void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
357 uint32_t tmp = 0x00010000;
\r
359 /* Check the parameters */
\r
360 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
361 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
366 /* Reset LCKK bit */
\r
367 GPIOx->LCKR = GPIO_Pin;
\r
377 * @brief Changes the mapping of the specified pin.
\r
378 * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.
\r
379 * @param GPIO_PinSource: specifies the pin for the Alternate function.
\r
380 * This parameter can be GPIO_PinSourcex where x can be (0..15).
\r
381 * @param GPIO_AFSelection: selects the pin to used as Alternat function.
\r
382 * This parameter can be one of the following values:
\r
383 * @arg GPIO_AF_RTC_50Hz
\r
385 * @arg GPIO_AF_TAMPER
\r
386 * @arg GPIO_AF_WKUP
\r
388 * @arg GPIO_AF_TRACE
\r
389 * @arg GPIO_AF_TIMESTAMP
\r
390 * @arg GPIO_AF_CALIB
\r
391 * @arg GPIO_AF_TIM2
\r
392 * @arg GPIO_AF_TIM3
\r
393 * @arg GPIO_AF_TIM4
\r
394 * @arg GPIO_AF_TIM9
\r
395 * @arg GPIO_AF_TIM10
\r
396 * @arg GPIO_AF_TIM11
\r
397 * @arg GPIO_AF_I2C1
\r
398 * @arg GPIO_AF_I2C2
\r
399 * @arg GPIO_AF_SPI1
\r
400 * @arg GPIO_AF_SPI2
\r
401 * @arg GPIO_AF_USART1
\r
402 * @arg GPIO_AF_USART2
\r
403 * @arg GPIO_AF_USART3
\r
407 * @arg GPIO_AF_EVENTOUT
\r
410 void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
\r
412 uint32_t temp = 0x00;
\r
413 uint32_t temp_2 = 0x00;
\r
415 /* Check the parameters */
\r
416 assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
\r
417 assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
\r
418 assert_param(IS_GPIO_AF(GPIO_AF));
\r
420 temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
\r
421 GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;
\r
422 temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;
\r
423 GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;
\r
437 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
\r