]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_STM32F103_IAR/STM32F10xFWLib/src/stm32f10x_gpio.c
Initial commit
[cmsis-freertos] / Demo / CORTEX_STM32F103_IAR / STM32F10xFWLib / src / stm32f10x_gpio.c
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 ********************************************************************************
7 * History:
8 * 04/02/2007: V0.2
9 * 02/05/2007: V0.1
10 * 09/29/2006: V0.01
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 *******************************************************************************/
19
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32f10x_gpio.h"
22 #include "stm32f10x_rcc.h"
23
24 /* Private typedef -----------------------------------------------------------*/
25 /* Private define ------------------------------------------------------------*/
26 /* ------------ RCC registers bit address in the alias region ----------- */
27 #define AFIO_OFFSET                 (AFIO_BASE - PERIPH_BASE)
28
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))
34
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)
41
42 /* Private macro -------------------------------------------------------------*/
43 /* Private variables ---------------------------------------------------------*/
44 /* Private function prototypes -----------------------------------------------*/
45 /* Private functions ---------------------------------------------------------*/
46
47 /*******************************************************************************
48 * Function Name  : GPIO_DeInit
49 * Description    : Deinitializes the GPIOx peripheral registers to their default
50 *                  reset values.
51 * Input          : - GPIOx: where x can be (A..E) to select the GPIO peripheral.
52 * Output         : None
53 * Return         : None
54 *******************************************************************************/
55 void GPIO_DeInit(GPIO_TypeDef* GPIOx)
56 {
57   switch (*(u32*)&GPIOx)
58   {
59     case GPIOA_BASE:
60       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE);
61       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE);
62       break;
63
64     case GPIOB_BASE:
65       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE);
66       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE);
67       break;
68
69     case GPIOC_BASE:
70       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, ENABLE);
71       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, DISABLE);
72       break;
73
74     case GPIOD_BASE:
75       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, ENABLE);
76       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, DISABLE);
77       break;
78       
79     case GPIOE_BASE:
80       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, ENABLE);
81       RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, DISABLE);
82       break;            
83
84     default:
85       break;
86   }
87 }
88
89 /*******************************************************************************
90 * Function Name  : GPIO_AFIODeInit
91 * Description    : Deinitializes the Alternate Functions (remap, event control
92 *                  and EXTI configuration) registers to their default reset
93 *                  values.
94 * Input          : None
95 * Output         : None
96 * Return         : None
97 *******************************************************************************/
98 void GPIO_AFIODeInit(void)
99 {
100   RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, ENABLE);
101   RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, DISABLE);
102 }
103
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
111 *                    peripheral.
112 * Output         : None
113 * Return         : None
114 *******************************************************************************/
115 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
116 {
117   u32 currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
118   u32 tmpreg = 0x00, pinmask = 0x00;
119
120   /* Check the parameters */
121   assert(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
122   assert(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));  
123   
124 /*---------------------------- GPIO Mode Configuration -----------------------*/
125   currentmode = ((u32)GPIO_InitStruct->GPIO_Mode) & ((u32)0x0F);
126
127   if ((((u32)GPIO_InitStruct->GPIO_Mode) & ((u32)0x10)) != 0x00)
128   { 
129     /* Check the parameters */
130     assert(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
131     /* Output mode */
132     currentmode |= (u32)GPIO_InitStruct->GPIO_Speed;
133   }
134
135 /*---------------------------- GPIO CRL Configuration ------------------------*/
136   /* Configure the eight low port pins */
137   if (((u32)GPIO_InitStruct->GPIO_Pin & ((u32)0x00FF)) != 0x00)
138   {
139     tmpreg = GPIOx->CRL;
140
141     for (pinpos = 0x00; pinpos < 0x08; pinpos++)
142     {
143       pos = ((u32)0x01) << pinpos;
144       /* Get the port pins position */
145       currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
146
147       if (currentpin == pos)
148       {
149         pos = pinpos << 2;
150         /* Clear the corresponding low control register bits */
151         pinmask = ((u32)0x0F) << pos;
152         tmpreg &= ~pinmask;
153
154         /* Write the mode configuration in the corresponding bits */
155         tmpreg |= (currentmode << pos);
156
157         /* Reset the corresponding ODR bit */
158         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
159         {
160           GPIOx->BRR = (((u32)0x01) << pinpos);
161         }
162         /* Set the corresponding ODR bit */
163         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
164         {
165           GPIOx->BSRR = (((u32)0x01) << pinpos);
166         }
167       }
168     }
169     GPIOx->CRL = tmpreg;
170     tmpreg = 0;
171   }
172
173 /*---------------------------- GPIO CRH Configuration ------------------------*/
174   /* Configure the eight high port pins */
175   if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
176   {
177     tmpreg = GPIOx->CRH;
178     for (pinpos = 0x00; pinpos < 0x08; pinpos++)
179     {
180       pos = (((u32)0x01) << (pinpos + 0x08));
181       /* Get the port pins position */
182       currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
183       if (currentpin == pos)
184       {
185         pos = pinpos << 2;
186         /* Clear the corresponding high control register bits */
187         pinmask = ((u32)0x0F) << pos;
188         tmpreg &= ~pinmask;
189
190         /* Write the mode configuration in the corresponding bits */
191         tmpreg |= (currentmode << pos);
192
193         /* Reset the corresponding ODR bit */
194         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
195         {
196           GPIOx->BRR = (((u32)0x01) << (pinpos + 0x08));
197         }
198         /* Set the corresponding ODR bit */
199         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
200         {
201           GPIOx->BSRR = (((u32)0x01) << (pinpos + 0x08));
202         }
203       }
204     }
205     GPIOx->CRH = tmpreg;
206   }
207 }
208
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.
214 * Output         : None
215 * Return         : None
216 *******************************************************************************/
217 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
218 {
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;
223 }
224
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).
231 * Output         : None
232 * Return         : The input port pin value.
233 *******************************************************************************/
234 u8 GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
235 {
236   u8 bitstatus = 0x00;
237   
238   /* Check the parameters */
239   assert(IS_GPIO_PIN(GPIO_Pin)); 
240   
241   if ((GPIOx->IDR & GPIO_Pin) != (u32)Bit_RESET)
242   {
243     bitstatus = (u8)Bit_SET;
244   }
245   else
246   {
247     bitstatus = (u8)Bit_RESET;
248   }
249   return bitstatus;
250 }
251
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.
256 * Output         : None
257 * Return         : GPIO input data port value.
258 *******************************************************************************/
259 u16 GPIO_ReadInputData(GPIO_TypeDef* GPIOx)
260 {
261   return ((u16)GPIOx->IDR);
262 }
263
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).
270 * Output         : None
271 * Return         : The output port pin value.
272 *******************************************************************************/
273 u8 GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
274 {
275   u8 bitstatus = 0x00;
276
277   /* Check the parameters */
278   assert(IS_GPIO_PIN(GPIO_Pin)); 
279   
280   if ((GPIOx->ODR & GPIO_Pin) != (u32)Bit_RESET)
281   {
282     bitstatus = (u8)Bit_SET;
283   }
284   else
285   {
286     bitstatus = (u8)Bit_RESET;
287   }
288   return bitstatus;
289 }
290
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.
295 * Output         : None
296 * Return         : GPIO output data port value.
297 *******************************************************************************/
298 u16 GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)
299 {
300   return ((u16)GPIOx->ODR);
301 }
302
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
313 * Output         : None
314 * Return         : None
315 *******************************************************************************/
316 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, u16 GPIO_Pin, BitAction BitVal)
317 {
318   /* Check the parameters */
319   assert(IS_GPIO_PIN(GPIO_Pin));
320   assert(IS_GPIO_BIT_ACTION(BitVal)); 
321   
322   if (BitVal != Bit_RESET)
323   {
324     GPIOx->BSRR = GPIO_Pin;
325   }
326   else
327   {
328     GPIOx->BRR = GPIO_Pin;
329   }
330 }
331
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
337 *                    data register.
338 * Output         : None
339 * Return         : None
340 *******************************************************************************/
341 void GPIO_Write(GPIO_TypeDef* GPIOx, u16 PortVal)
342 {
343   GPIOx->ODR = PortVal;
344 }
345
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).
352 * Output         : None
353 * Return         : None
354 *******************************************************************************/
355 void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
356 {
357   u32 tmp = 0x00010000;
358   
359   /* Check the parameters */
360   assert(IS_GPIO_PIN(GPIO_Pin));
361   
362   tmp |= GPIO_Pin;
363   /* Set LCKK bit */
364   GPIOx->LCKR = tmp;
365   /* Reset LCKK bit */
366   GPIOx->LCKR =  GPIO_Pin;
367   /* Set LCKK bit */
368   GPIOx->LCKR = tmp;
369   /* Read LCKK bit*/
370   tmp = GPIOx->LCKR;
371   /* Read LCKK bit*/
372   tmp = GPIOx->LCKR;
373 }
374
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
379 *                    for Event output.
380 *                    This parameter can be GPIO_PortSourceGPIOx where x can be
381 *                    (A..E).
382 *                  - GPIO_PinSource: specifies the pin for the Event output.
383 *                    This parameter can be GPIO_PinSourcex where x can be (0..15).
384 * Output         : None
385 * Return         : None
386 *******************************************************************************/
387 void GPIO_EventOutputConfig(u8 GPIO_PortSource, u8 GPIO_PinSource)
388 {
389   u32 tmpreg = 0x00;
390
391   /* Check the parameters */
392   assert(IS_GPIO_PORT_SOURCE(GPIO_PortSource));
393   assert(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
394     
395   tmpreg = AFIO->EVCR;
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;
400
401   AFIO->EVCR = tmpreg;
402 }
403
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.
409 * Output         : None
410 * Return         : None
411 *******************************************************************************/
412 void GPIO_EventOutputCmd(FunctionalState NewState)
413 {
414   /* Check the parameters */
415   assert(IS_FUNCTIONAL_STATE(NewState));
416   
417   *(vu32 *) EVCR_EVOE_BB = (u32)NewState;
418 }
419
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:
425 *                       - GPIO_Remap_SPI1
426 *                       - GPIO_Remap_I2C1
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
438 *                       - GPIO_Remap_TIM4
439 *                       - GPIO_Remap1_CAN
440 *                       - GPIO_Remap2_CAN
441 *                       - GPIO_Remap_PD01
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.
447 * Output         : None
448 * Return         : None
449 *******************************************************************************/
450 void GPIO_PinRemapConfig(u32 GPIO_Remap, FunctionalState NewState)
451 {
452   u32 tmp = 0x00, tmp1 = 0x00, tmpreg = 0x00, tmpmask = 0x00;
453
454   /* Check the parameters */
455   assert(IS_GPIO_REMAP(GPIO_Remap));
456   assert(IS_FUNCTIONAL_STATE(NewState));  
457   
458   tmpreg = AFIO->MAPR;
459
460   tmpmask = (GPIO_Remap & DBGAFR_POSITION_MASK) >> 0x10;
461   tmp = GPIO_Remap & LSB_MASK;
462
463   if ((GPIO_Remap & DBGAFR_LOCATION_MASK) == DBGAFR_LOCATION_MASK)
464   {
465     tmpreg &= DBGAFR_SWJCFG_MASK;
466   }
467   else if ((GPIO_Remap & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK)
468   {
469     tmp1 = ((u32)0x03) << tmpmask;
470     tmpreg &= ~tmp1;
471   }
472   else
473   {
474     tmpreg &= ~tmp;
475   }
476
477   if (NewState != DISABLE)
478   {
479     if ((GPIO_Remap & DBGAFR_LOCATION_MASK) == DBGAFR_LOCATION_MASK)
480     {
481       tmpreg |= (tmp << 0x10);
482     }
483     else
484     {
485       tmpreg |= tmp;
486     }
487   }
488   AFIO->MAPR = tmpreg;
489 }
490
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).
498 * Output         : None
499 * Return         : None
500 *******************************************************************************/
501 void GPIO_EXTILineConfig(u8 GPIO_PortSource, u8 GPIO_PinSource)
502 {
503   u32 tmp = 0x00;
504
505   /* Check the parameters */
506   assert(IS_GPIO_PORT_SOURCE(GPIO_PortSource));
507   assert(IS_GPIO_PIN_SOURCE(GPIO_PinSource));
508   
509   tmp = ((u32)0x0F) << (0x04 * (GPIO_PinSource & (u8)0x03));
510
511   AFIO->EXTICR[GPIO_PinSource >> 0x02] &= ~tmp;
512   AFIO->EXTICR[GPIO_PinSource >> 0x02] |= (((u32)GPIO_PortSource) << (0x04 * (GPIO_PinSource & (u8)0x03)));
513 }
514
515 /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/