]> git.leonardobizzoni.com Git - pioneer-stm32/blob
ffc617dda253260cbea13da3d4ca3aba2bb42135
[pioneer-stm32] /
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_gpio.c\r
4   * @author  MCD Application Team\r
5   * @brief   GPIO HAL module driver.\r
6   *          This file provides firmware functions to manage the following \r
7   *          functionalities of the General Purpose Input/Output (GPIO) peripheral:\r
8   *           + Initialization and de-initialization functions\r
9   *           + IO operation functions\r
10   *\r
11   @verbatim\r
12   ==============================================================================\r
13                     ##### GPIO Peripheral features #####\r
14   ==============================================================================\r
15   [..] \r
16   Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each\r
17   port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software\r
18   in several modes:\r
19   (+) Input mode \r
20   (+) Analog mode\r
21   (+) Output mode\r
22   (+) Alternate function mode\r
23   (+) External interrupt/event lines\r
24 \r
25   [..]  \r
26   During and just after reset, the alternate functions and external interrupt  \r
27   lines are not active and the I/O ports are configured in input floating mode.\r
28   \r
29   [..]   \r
30   All GPIO pins have weak internal pull-up and pull-down resistors, which can be \r
31   activated or not.\r
32 \r
33   [..]\r
34   In Output or Alternate mode, each IO can be configured on open-drain or push-pull\r
35   type and the IO speed can be selected depending on the VDD value.\r
36 \r
37   [..]  \r
38   All ports have external interrupt/event capability. To use external interrupt \r
39   lines, the port must be configured in input mode. All available GPIO pins are \r
40   connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.\r
41   \r
42   [..]\r
43   The external interrupt/event controller consists of up to 23 edge detectors \r
44   (16 lines are connected to GPIO) for generating event/interrupt requests (each \r
45   input line can be independently configured to select the type (interrupt or event) \r
46   and the corresponding trigger event (rising or falling or both). Each line can \r
47   also be masked independently. \r
48 \r
49                      ##### How to use this driver #####\r
50   ==============================================================================  \r
51   [..]\r
52     (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE(). \r
53 \r
54     (#) Configure the GPIO pin(s) using HAL_GPIO_Init().\r
55         (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure\r
56         (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef \r
57              structure.\r
58         (++) In case of Output or alternate function mode selection: the speed is \r
59              configured through "Speed" member from GPIO_InitTypeDef structure.\r
60         (++) In alternate mode is selection, the alternate function connected to the IO\r
61              is configured through "Alternate" member from GPIO_InitTypeDef structure.\r
62         (++) Analog mode is required when a pin is to be used as ADC channel \r
63              or DAC output.\r
64         (++) In case of external interrupt/event selection the "Mode" member from \r
65              GPIO_InitTypeDef structure select the type (interrupt or event) and \r
66              the corresponding trigger event (rising or falling or both).\r
67 \r
68     (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority \r
69         mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using\r
70         HAL_NVIC_EnableIRQ().\r
71          \r
72     (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().\r
73             \r
74     (#) To set/reset the level of a pin configured in output mode use \r
75         HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().\r
76     \r
77     (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().\r
78 \r
79                  \r
80     (#) During and just after reset, the alternate functions are not \r
81         active and the GPIO pins are configured in input floating mode (except JTAG\r
82         pins).\r
83   \r
84     (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose \r
85         (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has \r
86         priority over the GPIO function.\r
87   \r
88     (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as \r
89         general purpose PH0 and PH1, respectively, when the HSE oscillator is off. \r
90         The HSE has priority over the GPIO function.\r
91   \r
92   @endverbatim\r
93   ******************************************************************************\r
94   * @attention\r
95   *\r
96   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.\r
97   * All rights reserved.</center></h2>\r
98   *\r
99   * This software component is licensed by ST under BSD 3-Clause license,\r
100   * the "License"; You may not use this file except in compliance with the\r
101   * License. You may obtain a copy of the License at:\r
102   *                        opensource.org/licenses/BSD-3-Clause\r
103   *\r
104   ******************************************************************************\r
105   */ \r
106 \r
107 /* Includes ------------------------------------------------------------------*/\r
108 #include "stm32f7xx_hal.h"\r
109 \r
110 /** @addtogroup STM32F7xx_HAL_Driver\r
111   * @{\r
112   */\r
113 \r
114 /** @defgroup GPIO GPIO\r
115   * @brief GPIO HAL module driver\r
116   * @{\r
117   */\r
118 \r
119 #ifdef HAL_GPIO_MODULE_ENABLED\r
120 \r
121 /* Private typedef -----------------------------------------------------------*/\r
122 /* Private define ------------------------------------------------------------*/\r
123 /** @addtogroup GPIO_Private_Constants GPIO Private Constants\r
124   * @{\r
125   */\r
126 #define GPIO_MODE             ((uint32_t)0x00000003U)\r
127 #define EXTI_MODE             ((uint32_t)0x10000000U)\r
128 #define GPIO_MODE_IT          ((uint32_t)0x00010000U)\r
129 #define GPIO_MODE_EVT         ((uint32_t)0x00020000U)\r
130 #define RISING_EDGE           ((uint32_t)0x00100000U)\r
131 #define FALLING_EDGE          ((uint32_t)0x00200000U)\r
132 #define GPIO_OUTPUT_TYPE      ((uint32_t)0x00000010U)\r
133 \r
134 #define GPIO_NUMBER           ((uint32_t)16U)\r
135 /**\r
136   * @}\r
137   */\r
138 /* Private macro -------------------------------------------------------------*/\r
139 /* Private variables ---------------------------------------------------------*/\r
140 /* Private function prototypes -----------------------------------------------*/\r
141 /* Private functions ---------------------------------------------------------*/\r
142 /* Exported functions --------------------------------------------------------*/\r
143 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions\r
144   * @{\r
145   */\r
146 \r
147 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions\r
148  *  @brief    Initialization and Configuration functions\r
149  *\r
150 @verbatim\r
151  ===============================================================================\r
152               ##### Initialization and de-initialization functions #####\r
153  ===============================================================================\r
154   [..]\r
155     This section provides functions allowing to initialize and de-initialize the GPIOs\r
156     to be ready for use.\r
157  \r
158 @endverbatim\r
159   * @{\r
160   */\r
161 \r
162 /**\r
163   * @brief  Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.\r
164   * @param  GPIOx where x can be (A..K) to select the GPIO peripheral.\r
165   * @param  GPIO_Init pointer to a GPIO_InitTypeDef structure that contains\r
166   *         the configuration information for the specified GPIO peripheral.\r
167   * @retval None\r
168   */\r
169 void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)\r
170 {\r
171   uint32_t position = 0x00;\r
172   uint32_t ioposition = 0x00;\r
173   uint32_t iocurrent = 0x00;\r
174   uint32_t temp = 0x00;\r
175 \r
176   /* Check the parameters */\r
177   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));\r
178   assert_param(IS_GPIO_PIN(GPIO_Init->Pin));\r
179   assert_param(IS_GPIO_MODE(GPIO_Init->Mode));\r
180   assert_param(IS_GPIO_PULL(GPIO_Init->Pull));\r
181 \r
182   /* Configure the port pins */\r
183   for(position = 0; position < GPIO_NUMBER; position++)\r
184   {\r
185     /* Get the IO position */\r
186     ioposition = ((uint32_t)0x01) << position;\r
187     /* Get the current IO position */\r
188     iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;\r
189 \r
190     if(iocurrent == ioposition)\r
191     {\r
192       /*--------------------- GPIO Mode Configuration ------------------------*/\r
193       /* In case of Alternate function mode selection */\r
194       if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))\r
195       {\r
196         /* Check the Alternate function parameter */\r
197         assert_param(IS_GPIO_AF(GPIO_Init->Alternate));\r
198         \r
199         /* Configure Alternate function mapped with the current IO */\r
200         temp = GPIOx->AFR[position >> 3];\r
201         temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;\r
202         temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4));\r
203         GPIOx->AFR[position >> 3] = temp;\r
204       }\r
205 \r
206       /* Configure IO Direction mode (Input, Output, Alternate or Analog) */\r
207       temp = GPIOx->MODER;\r
208       temp &= ~(GPIO_MODER_MODER0 << (position * 2));\r
209       temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2));\r
210       GPIOx->MODER = temp;\r
211 \r
212       /* In case of Output or Alternate function mode selection */\r
213       if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||\r
214          (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))\r
215       {\r
216         /* Check the Speed parameter */\r
217         assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));\r
218         /* Configure the IO Speed */\r
219         temp = GPIOx->OSPEEDR; \r
220         temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));\r
221         temp |= (GPIO_Init->Speed << (position * 2));\r
222         GPIOx->OSPEEDR = temp;\r
223 \r
224         /* Configure the IO Output Type */\r
225         temp = GPIOx->OTYPER;\r
226         temp &= ~(GPIO_OTYPER_OT_0 << position) ;\r
227         temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4) << position);\r
228         GPIOx->OTYPER = temp;\r
229       }\r
230 \r
231       /* Activate the Pull-up or Pull down resistor for the current IO */\r
232       temp = GPIOx->PUPDR;\r
233       temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));\r
234       temp |= ((GPIO_Init->Pull) << (position * 2));\r
235       GPIOx->PUPDR = temp;\r
236 \r
237       /*--------------------- EXTI Mode Configuration ------------------------*/\r
238       /* Configure the External Interrupt or event for the current IO */\r
239       if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)\r
240       {\r
241         /* Enable SYSCFG Clock */\r
242         __HAL_RCC_SYSCFG_CLK_ENABLE();\r
243 \r
244         temp = SYSCFG->EXTICR[position >> 2];\r
245         temp &= ~(((uint32_t)0x0F) << (4 * (position & 0x03)));\r
246         temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03)));\r
247         SYSCFG->EXTICR[position >> 2] = temp;\r
248 \r
249         /* Clear EXTI line configuration */\r
250         temp = EXTI->IMR;\r
251         temp &= ~((uint32_t)iocurrent);\r
252         if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)\r
253         {\r
254           temp |= iocurrent;\r
255         }\r
256         EXTI->IMR = temp;\r
257 \r
258         temp = EXTI->EMR;\r
259         temp &= ~((uint32_t)iocurrent);\r
260         if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)\r
261         {\r
262           temp |= iocurrent;\r
263         }\r
264         EXTI->EMR = temp;\r
265 \r
266         /* Clear Rising Falling edge configuration */\r
267         temp = EXTI->RTSR;\r
268         temp &= ~((uint32_t)iocurrent);\r
269         if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)\r
270         {\r
271           temp |= iocurrent;\r
272         }\r
273         EXTI->RTSR = temp;\r
274 \r
275         temp = EXTI->FTSR;\r
276         temp &= ~((uint32_t)iocurrent);\r
277         if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)\r
278         {\r
279           temp |= iocurrent;\r
280         }\r
281         EXTI->FTSR = temp;\r
282       }\r
283     }\r
284   }\r
285 }\r
286 \r
287 /**\r
288   * @brief  De-initializes the GPIOx peripheral registers to their default reset values.\r
289   * @param  GPIOx where x can be (A..K) to select the GPIO peripheral.\r
290   * @param  GPIO_Pin specifies the port bit to be written.\r
291   *          This parameter can be one of GPIO_PIN_x where x can be (0..15).\r
292   * @retval None\r
293   */\r
294 void HAL_GPIO_DeInit(GPIO_TypeDef  *GPIOx, uint32_t GPIO_Pin)\r
295 {\r
296   uint32_t position;\r
297   uint32_t ioposition = 0x00;\r
298   uint32_t iocurrent = 0x00;\r
299   uint32_t tmp = 0x00;\r
300 \r
301   /* Check the parameters */\r
302   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));\r
303   \r
304   /* Configure the port pins */\r
305   for(position = 0; position < GPIO_NUMBER; position++)\r
306   {\r
307     /* Get the IO position */\r
308     ioposition = ((uint32_t)0x01) << position;\r
309     /* Get the current IO position */\r
310     iocurrent = (GPIO_Pin) & ioposition;\r
311 \r
312     if(iocurrent == ioposition)\r
313     {\r
314       /*------------------------- EXTI Mode Configuration --------------------*/\r
315       tmp = SYSCFG->EXTICR[position >> 2];\r
316       tmp &= (((uint32_t)0x0F) << (4 * (position & 0x03)));\r
317       if(tmp == ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03))))\r
318       {\r
319         /* Configure the External Interrupt or event for the current IO */\r
320         tmp = ((uint32_t)0x0F) << (4 * (position & 0x03));\r
321         SYSCFG->EXTICR[position >> 2] &= ~tmp;\r
322 \r
323         /* Clear EXTI line configuration */\r
324         EXTI->IMR &= ~((uint32_t)iocurrent);\r
325         EXTI->EMR &= ~((uint32_t)iocurrent);\r
326 \r
327         /* Clear Rising Falling edge configuration */\r
328         EXTI->RTSR &= ~((uint32_t)iocurrent);\r
329         EXTI->FTSR &= ~((uint32_t)iocurrent);\r
330       }\r
331       /*------------------------- GPIO Mode Configuration --------------------*/\r
332       /* Configure IO Direction in Input Floating Mode */\r
333       GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2));\r
334 \r
335       /* Configure the default Alternate Function in current IO */\r
336       GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;\r
337 \r
338       /* Configure the default value for IO Speed */\r
339       GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));\r
340 \r
341       /* Configure the default value IO Output Type */\r
342       GPIOx->OTYPER  &= ~(GPIO_OTYPER_OT_0 << position) ;\r
343 \r
344       /* Deactivate the Pull-up and Pull-down resistor for the current IO */\r
345       GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));\r
346     }\r
347   }\r
348 }\r
349 \r
350 /**\r
351   * @}\r
352   */\r
353 \r
354 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions \r
355  *  @brief   GPIO Read and Write\r
356  *\r
357 @verbatim\r
358  ===============================================================================\r
359                        ##### IO operation functions #####\r
360  ===============================================================================\r
361 \r
362 @endverbatim\r
363   * @{\r
364   */\r
365 \r
366 /**\r
367   * @brief  Reads the specified input port pin.\r
368   * @param  GPIOx where x can be (A..K) to select the GPIO peripheral.\r
369   * @param  GPIO_Pin specifies the port bit to read.\r
370   *         This parameter can be GPIO_PIN_x where x can be (0..15).\r
371   * @retval The input port pin value.\r
372   */\r
373 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)\r
374 {\r
375   GPIO_PinState bitstatus;\r
376 \r
377   /* Check the parameters */\r
378   assert_param(IS_GPIO_PIN(GPIO_Pin));\r
379 \r
380   if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)\r
381   {\r
382     bitstatus = GPIO_PIN_SET;\r
383   }\r
384   else\r
385   {\r
386     bitstatus = GPIO_PIN_RESET;\r
387   }\r
388   return bitstatus;\r
389 }\r
390 \r
391 /**\r
392   * @brief  Sets or clears the selected data port bit.\r
393   *\r
394   * @note   This function uses GPIOx_BSRR register to allow atomic read/modify\r
395   *         accesses. In this way, there is no risk of an IRQ occurring between\r
396   *         the read and the modify access.\r
397   *\r
398   * @param  GPIOx where x can be (A..K) to select the GPIO peripheral.\r
399   * @param  GPIO_Pin specifies the port bit to be written.\r
400   *          This parameter can be one of GPIO_PIN_x where x can be (0..15).\r
401   * @param  PinState specifies the value to be written to the selected bit.\r
402   *          This parameter can be one of the GPIO_PinState enum values:\r
403   *            @arg GPIO_PIN_RESET: to clear the port pin\r
404   *            @arg GPIO_PIN_SET: to set the port pin\r
405   * @retval None\r
406   */\r
407 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)\r
408 {\r
409   /* Check the parameters */\r
410   assert_param(IS_GPIO_PIN(GPIO_Pin));\r
411   assert_param(IS_GPIO_PIN_ACTION(PinState));\r
412 \r
413   if(PinState != GPIO_PIN_RESET)\r
414   {\r
415     GPIOx->BSRR = GPIO_Pin;\r
416   }\r
417   else\r
418   {\r
419     GPIOx->BSRR = (uint32_t)GPIO_Pin << 16;\r
420   }\r
421 }\r
422 \r
423 /**\r
424   * @brief  Toggles the specified GPIO pins.\r
425   * @param  GPIOx Where x can be (A..I) to select the GPIO peripheral.\r
426   * @param  GPIO_Pin Specifies the pins to be toggled.\r
427   * @retval None\r
428   */\r
429 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)\r
430 {\r
431   /* Check the parameters */\r
432   assert_param(IS_GPIO_PIN(GPIO_Pin));\r
433 \r
434   if ((GPIOx->ODR & GPIO_Pin) == GPIO_Pin)\r
435   {\r
436     GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER;\r
437   }\r
438   else\r
439   {\r
440     GPIOx->BSRR = GPIO_Pin;\r
441   }\r
442 }\r
443 \r
444 /**\r
445   * @brief  Locks GPIO Pins configuration registers.\r
446   * @note   The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,\r
447   *         GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.\r
448   * @note   The configuration of the locked GPIO pins can no longer be modified\r
449   *         until the next reset.\r
450   * @param  GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F7 family\r
451   * @param  GPIO_Pin specifies the port bit to be locked.\r
452   *         This parameter can be any combination of GPIO_PIN_x where x can be (0..15).\r
453   * @retval None\r
454   */\r
455 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)\r
456 {\r
457   __IO uint32_t tmp = GPIO_LCKR_LCKK;\r
458 \r
459   /* Check the parameters */\r
460   assert_param(IS_GPIO_PIN(GPIO_Pin));\r
461 \r
462   /* Apply lock key write sequence */\r
463   tmp |= GPIO_Pin;\r
464   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */\r
465   GPIOx->LCKR = tmp;\r
466   /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */\r
467   GPIOx->LCKR = GPIO_Pin;\r
468   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */\r
469   GPIOx->LCKR = tmp;\r
470   /* Read LCKK bit*/\r
471   tmp = GPIOx->LCKR;\r
472 \r
473  if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)\r
474   {\r
475     return HAL_OK;\r
476   }\r
477   else\r
478   {\r
479     return HAL_ERROR;\r
480   }\r
481 }\r
482 \r
483 /**\r
484   * @brief  This function handles EXTI interrupt request.\r
485   * @param  GPIO_Pin Specifies the pins connected EXTI line\r
486   * @retval None\r
487   */\r
488 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)\r
489 {\r
490   /* EXTI line interrupt detected */\r
491   if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)\r
492   {\r
493     __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);\r
494     HAL_GPIO_EXTI_Callback(GPIO_Pin);\r
495   }\r
496 }\r
497 \r
498 /**\r
499   * @brief  EXTI line detection callbacks.\r
500   * @param  GPIO_Pin Specifies the pins connected EXTI line\r
501   * @retval None\r
502   */\r
503 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)\r
504 {\r
505   /* Prevent unused argument(s) compilation warning */\r
506   UNUSED(GPIO_Pin);\r
507   \r
508   /* NOTE: This function Should not be modified, when the callback is needed,\r
509            the HAL_GPIO_EXTI_Callback could be implemented in the user file\r
510    */\r
511 }\r
512 \r
513 /**\r
514   * @}\r
515   */\r
516 \r
517 \r
518 /**\r
519   * @}\r
520   */\r
521 \r
522 #endif /* HAL_GPIO_MODULE_ENABLED */\r
523 /**\r
524   * @}\r
525   */\r
526 \r
527 /**\r
528   * @}\r
529   */\r
530 \r
531 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r