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