1 /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
2 * File Name : stm32f10x_gpio.c
3 * Author : MCD Application Team
4 * Date First Issued : 09/29/2006
5 * Description : This file provides all the GPIO firmware functions.
6 ********************************************************************************
11 ********************************************************************************
12 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
14 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
15 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
16 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
17 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 *******************************************************************************/
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32f10x_gpio.h"
22 #include "stm32f10x_rcc.h"
24 /* Private typedef -----------------------------------------------------------*/
25 /* Private define ------------------------------------------------------------*/
26 /* ------------ RCC registers bit address in the alias region ----------- */
27 #define AFIO_OFFSET (AFIO_BASE - PERIPH_BASE)
29 /* --- EVENTCR Register ---*/
30 /* Alias word address of EVOE bit */
31 #define EVCR_OFFSET (AFIO_OFFSET + 0x00)
32 #define EVOE_BitNumber ((u8)0x07)
33 #define EVCR_EVOE_BB (PERIPH_BB_BASE + (EVCR_OFFSET * 32) + (EVOE_BitNumber * 4))
35 #define EVCR_PORTPINCONFIG_MASK ((u16)0xFF80)
36 #define LSB_MASK ((u16)0xFFFF)
37 #define DBGAFR_POSITION_MASK ((u32)0x000F0000)
38 #define DBGAFR_SWJCFG_MASK ((u32)0xF8FFFFFF)
39 #define DBGAFR_LOCATION_MASK ((u32)0x00200000)
40 #define DBGAFR_NUMBITS_MASK ((u32)0x00100000)
42 /* Private macro -------------------------------------------------------------*/
43 /* Private variables ---------------------------------------------------------*/
44 /* Private function prototypes -----------------------------------------------*/
45 /* Private functions ---------------------------------------------------------*/
47 /*******************************************************************************
48 * Function Name : GPIO_DeInit
49 * Description : Deinitializes the GPIOx peripheral registers to their default
51 * Input : - GPIOx: where x can be (A..E) to select the GPIO peripheral.
54 *******************************************************************************/
55 void GPIO_DeInit(GPIO_TypeDef* GPIOx)
57 switch (*(u32*)&GPIOx)
60 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE);
61 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE);
65 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE);
66 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE);
70 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, ENABLE);
71 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, DISABLE);
75 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, ENABLE);
76 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, DISABLE);
80 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, ENABLE);
81 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, DISABLE);
89 /*******************************************************************************
90 * Function Name : GPIO_AFIODeInit
91 * Description : Deinitializes the Alternate Functions (remap, event control
92 * and EXTI configuration) registers to their default reset
97 *******************************************************************************/
98 void GPIO_AFIODeInit(void)
100 RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, ENABLE);
101 RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, DISABLE);
104 /*******************************************************************************
105 * Function Name : GPIO_Init
106 * Description : Initializes the GPIOx peripheral according to the specified
107 * parameters in the GPIO_InitStruct.
108 * Input : - GPIOx: where x can be (A..E) to select the GPIO peripheral.
109 * - GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
110 * contains the configuration information for the specified GPIO
114 *******************************************************************************/
115 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
117 u32 currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
118 u32 tmpreg = 0x00, pinmask = 0x00;
120 /* Check the parameters */
121 assert(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
122 assert(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
124 /*---------------------------- GPIO Mode Configuration -----------------------*/
125 currentmode = ((u32)GPIO_InitStruct->GPIO_Mode) & ((u32)0x0F);
127 if ((((u32)GPIO_InitStruct->GPIO_Mode) & ((u32)0x10)) != 0x00)
129 /* Check the parameters */
130 assert(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
132 currentmode |= (u32)GPIO_InitStruct->GPIO_Speed;
135 /*---------------------------- GPIO CRL Configuration ------------------------*/
136 /* Configure the eight low port pins */
137 if (((u32)GPIO_InitStruct->GPIO_Pin & ((u32)0x00FF)) != 0x00)
141 for (pinpos = 0x00; pinpos < 0x08; pinpos++)
143 pos = ((u32)0x01) << pinpos;
144 /* Get the port pins position */
145 currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
147 if (currentpin == pos)
150 /* Clear the corresponding low control register bits */
151 pinmask = ((u32)0x0F) << pos;
154 /* Write the mode configuration in the corresponding bits */
155 tmpreg |= (currentmode << pos);
157 /* Reset the corresponding ODR bit */
158 if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
160 GPIOx->BRR = (((u32)0x01) << pinpos);
162 /* Set the corresponding ODR bit */
163 if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
165 GPIOx->BSRR = (((u32)0x01) << pinpos);
173 /*---------------------------- GPIO CRH Configuration ------------------------*/
174 /* Configure the eight high port pins */
175 if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
178 for (pinpos = 0x00; pinpos < 0x08; pinpos++)
180 pos = (((u32)0x01) << (pinpos + 0x08));
181 /* Get the port pins position */
182 currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
183 if (currentpin == pos)
186 /* Clear the corresponding high control register bits */
187 pinmask = ((u32)0x0F) << pos;
190 /* Write the mode configuration in the corresponding bits */
191 tmpreg |= (currentmode << pos);
193 /* Reset the corresponding ODR bit */
194 if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
196 GPIOx->BRR = (((u32)0x01) << (pinpos + 0x08));
198 /* Set the corresponding ODR bit */
199 if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
201 GPIOx->BSRR = (((u32)0x01) << (pinpos + 0x08));
209 /*******************************************************************************
210 * Function Name : GPIO_StructInit
211 * Description : Fills each GPIO_InitStruct member with its default value.
212 * Input : - GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure
213 * which will be initialized.
216 *******************************************************************************/
217 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
219 /* Reset GPIO init structure parameters values */
220 GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
221 GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
222 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
225 /*******************************************************************************
226 * Function Name : GPIO_ReadInputDataBit
227 * Description : Reads the specified input port pin.
228 * Input : - GPIOx: where x can be (A..E) to select the GPIO peripheral.
229 * : - GPIO_Pin: specifies the port bit to read.
230 * This parameter can be GPIO_Pin_x where x can be (0..15).
232 * Return : The input port pin value.
233 *******************************************************************************/
234 u8 GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
238 /* Check the parameters */
239 assert(IS_GPIO_PIN(GPIO_Pin));
241 if ((GPIOx->IDR & GPIO_Pin) != (u32)Bit_RESET)
243 bitstatus = (u8)Bit_SET;
247 bitstatus = (u8)Bit_RESET;
252 /*******************************************************************************
253 * Function Name : GPIO_ReadInputData
254 * Description : Reads the specified GPIO input data port.
255 * Input : - GPIOx: where x can be (A..E) to select the GPIO peripheral.
257 * Return : GPIO input data port value.
258 *******************************************************************************/
259 u16 GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
261 return ((u16)GPIOx->IDR);
264 /*******************************************************************************
265 * Function Name : GPIO_ReadOutputDataBit
266 * Description : Reads the specified output data port bit.
267 * Input : - GPIOx: where x can be (A..E) to select the GPIO peripheral.
268 * : - GPIO_Pin: specifies the port bit to read.
269 * This parameter can be GPIO_Pin_x where x can be (0..15).
271 * Return : The output port pin value.
272 *******************************************************************************/
273 u8 GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
277 /* Check the parameters */
278 assert(IS_GPIO_PIN(GPIO_Pin));
280 if ((GPIOx->ODR & GPIO_Pin) != (u32)Bit_RESET)
282 bitstatus = (u8)Bit_SET;
286 bitstatus = (u8)Bit_RESET;
291 /*******************************************************************************
292 * Function Name : GPIO_ReadOutputData
293 * Description : Reads the specified GPIO output data port.
294 * Input : - GPIOx: where x can be (A..E) to select the GPIO peripheral.
296 * Return : GPIO output data port value.
297 *******************************************************************************/
298 u16 GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
300 return ((u16)GPIOx->ODR);
303 /*******************************************************************************
304 * Function Name : GPIO_WriteBit
305 * Description : Sets or clears the selected data port bit.
306 * Input : - GPIOx: where x can be (A..E) to select the GPIO peripheral.
307 * - GPIO_Pin: specifies the port bit to be written.
308 * This parameter can be GPIO_Pin_x where x can be (0..15).
309 * - BitVal: specifies the value to be written to the selected bit.
310 * This parameter can be one of the BitAction enum values:
311 * - Bit_RESET: to clear the port pin
312 * - Bit_SET: to set the port pin
315 *******************************************************************************/
316 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, u16 GPIO_Pin, BitAction BitVal)
318 /* Check the parameters */
319 assert(IS_GPIO_PIN(GPIO_Pin));
320 assert(IS_GPIO_BIT_ACTION(BitVal));
322 if (BitVal != Bit_RESET)
324 GPIOx->BSRR = GPIO_Pin;
328 GPIOx->BRR = GPIO_Pin;
332 /*******************************************************************************
333 * Function Name : GPIO_Write
334 * Description : Writes data to the specified GPIO data port.
335 * Input : - GPIOx: where x can be (A..E) to select the GPIO peripheral.
336 * - PortVal: specifies the value to be written to the port output
340 *******************************************************************************/
341 void GPIO_Write(GPIO_TypeDef* GPIOx, u16 PortVal)
343 GPIOx->ODR = PortVal;
346 /*******************************************************************************
347 * Function Name : GPIO_PinLockConfig
348 * Description : Locks GPIO Pins configuration registers.
349 * Input : - GPIOx: where x can be (A..E) to select the GPIO peripheral.
350 * - GPIO_Pin: specifies the port bit to be written.
351 * This parameter can be GPIO_Pin_x where x can be (0..15).
354 *******************************************************************************/
355 void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
357 u32 tmp = 0x00010000;
359 /* Check the parameters */
360 assert(IS_GPIO_PIN(GPIO_Pin));
366 GPIOx->LCKR = GPIO_Pin;
375 /*******************************************************************************
376 * Function Name : GPIO_EventOutputConfig
377 * Description : Selects the GPIO pin used as Event output.
378 * Input : - GPIO_PortSource: selects the GPIO port to be used as source
380 * This parameter can be GPIO_PortSourceGPIOx where x can be
382 * - GPIO_PinSource: specifies the pin for the Event output.
383 * This parameter can be GPIO_PinSourcex where x can be (0..15).
386 *******************************************************************************/
387 void GPIO_EventOutputConfig(u8 GPIO_PortSource, u8 GPIO_PinSource)
391 /* Check the parameters */
392 assert(IS_GPIO_PORT_SOURCE(GPIO_PortSource));
393 assert(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
396 /* Clear the PORT[6:4] and PIN[3:0] bits */
397 tmpreg &= EVCR_PORTPINCONFIG_MASK;
398 tmpreg |= (u32)GPIO_PortSource << 0x04;
399 tmpreg |= GPIO_PinSource;
404 /*******************************************************************************
405 * Function Name : GPIO_EventOutputCmd
406 * Description : Enables or disables the Event Output.
407 * Input : - NewState: new state of the Event output.
408 * This parameter can be: ENABLE or DISABLE.
411 *******************************************************************************/
412 void GPIO_EventOutputCmd(FunctionalState NewState)
414 /* Check the parameters */
415 assert(IS_FUNCTIONAL_STATE(NewState));
417 *(vu32 *) EVCR_EVOE_BB = (u32)NewState;
420 /*******************************************************************************
421 * Function Name : GPIO_PinRemapConfig
422 * Description : Changes the mapping of the specified pin.
423 * Input : - GPIO_Remap: selects the pin to remap.
424 * This parameter can be one of the following values:
427 * - GPIO_Remap_USART1
428 * - GPIO_Remap_USART2
429 * - GPIO_PartialRemap_USART3
430 * - GPIO_FullRemap_USART3
431 * - GPIO_PartialRemap_TIM1
432 * - GPIO_FullRemap_TIM1
433 * - GPIO_PartialRemap1_TIM2
434 * - GPIO_PartialRemap2_TIM2
435 * - GPIO_FullRemap_TIM2
436 * - GPIO_PartialRemap_TIM3
437 * - GPIO_FullRemap_TIM3
442 * - GPIO_Remap_SWJ_NoJTRST
443 * - GPIO_Remap_SWJ_JTAGDisable
444 * - GPIO_Remap_SWJ_Disable
445 * - NewState: new state of the port pin remapping.
446 * This parameter can be: ENABLE or DISABLE.
449 *******************************************************************************/
450 void GPIO_PinRemapConfig(u32 GPIO_Remap, FunctionalState NewState)
452 u32 tmp = 0x00, tmp1 = 0x00, tmpreg = 0x00, tmpmask = 0x00;
454 /* Check the parameters */
455 assert(IS_GPIO_REMAP(GPIO_Remap));
456 assert(IS_FUNCTIONAL_STATE(NewState));
460 tmpmask = (GPIO_Remap & DBGAFR_POSITION_MASK) >> 0x10;
461 tmp = GPIO_Remap & LSB_MASK;
463 if ((GPIO_Remap & DBGAFR_LOCATION_MASK) == DBGAFR_LOCATION_MASK)
465 tmpreg &= DBGAFR_SWJCFG_MASK;
467 else if ((GPIO_Remap & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK)
469 tmp1 = ((u32)0x03) << tmpmask;
477 if (NewState != DISABLE)
479 if ((GPIO_Remap & DBGAFR_LOCATION_MASK) == DBGAFR_LOCATION_MASK)
481 tmpreg |= (tmp << 0x10);
491 /*******************************************************************************
492 * Function Name : GPIO_EXTILineConfig
493 * Description : Selects the GPIO pin used as EXTI Line.
494 * Input : - GPIO_PortSource: selects the GPIO port to be used as
495 * source for EXTI lines.
496 * - GPIO_PinSource: specifies the EXTI line to be configured.
497 * This parameter can be GPIO_PinSourcex where x can be (0..15).
500 *******************************************************************************/
501 void GPIO_EXTILineConfig(u8 GPIO_PortSource, u8 GPIO_PinSource)
505 /* Check the parameters */
506 assert(IS_GPIO_PORT_SOURCE(GPIO_PortSource));
507 assert(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
509 tmp = ((u32)0x0F) << (0x04 * (GPIO_PinSource & (u8)0x03));
511 AFIO->EXTICR[GPIO_PinSource >> 0x02] &= ~tmp;
512 AFIO->EXTICR[GPIO_PinSource >> 0x02] |= (((u32)GPIO_PortSource) << (0x04 * (GPIO_PinSource & (u8)0x03)));
515 /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/