]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_gpio.c
Merge commit '4d116a04e94cf0d19317d5b44e4fa9f34a3e5594'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_gpio.c
1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_hal_gpio.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    12-Sept-2014
7   * @brief   GPIO HAL module driver.
8   *    
9   *          This file provides firmware functions to manage the following 
10   *          functionalities of the General Purpose Input/Output (GPIO) peripheral:
11   *           + Initialization/de-initialization functions
12   *           + IO operation functions
13   *         
14   @verbatim
15   ==============================================================================
16                     ##### GPIO specific features #####
17   ==============================================================================         
18   [..] 
19   Each port bit of the general-purpose I/O (GPIO) ports can be individually 
20   configured by software in several modes:
21   (+) Input mode 
22   (+) Analog mode
23   (+) Output mode
24   (+) Alternate function mode
25   (+) External interrupt/event lines
26  
27   [..]  
28   During and just after reset, the alternate functions and external interrupt  
29   lines are not active and the I/O ports are configured in input floating mode.
30   
31   [..]   
32   All GPIO pins have weak internal pull-up and pull-down resistors, which can be 
33   activated or not.
34            
35   [..]
36   In Output or Alternate mode, each IO can be configured on open-drain or push-pull
37   type and the IO speed can be selected depending on the VDD value.
38        
39   [..]
40   The microcontroller IO pins are connected to onboard peripherals/modules through a 
41   multiplexer that allows only one peripheral\92s alternate function (AF) connected 
42   to an IO pin at a time. In this way, there can be no conflict between peripherals 
43   sharing the same IO pin. 
44   
45   [..]  
46   All ports have external interrupt/event capability. To use external interrupt 
47   lines, the port must be configured in input mode. All available GPIO pins are 
48   connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
49   
50   [..]  
51   The external interrupt/event controller consists of up to 23 edge detectors 
52   (16 lines are connected to GPIO) for generating event/interrupt requests (each 
53   input line can be independently configured to select the type (interrupt or event) 
54   and the corresponding trigger event (rising or falling or both). Each line can 
55   also be masked independently. 
56    
57             ##### How to use this driver #####
58   ==============================================================================  
59            [..]
60    (#) Enable the GPIO AHB clock using the following function : __GPIOx_CLK_ENABLE(). 
61                                     
62    (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
63        (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
64        (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef 
65             structure.
66        (++) In case of Output or alternate function mode selection: the speed is 
67             configured through "Speed" member from GPIO_InitTypeDef structure, 
68             the speed is configurable: 2 MHz, 10 MHz and 50 MHz.
69        (++) If alternate mode is selected, the alternate function connected to the IO
70             is configured through "Alternate" member from GPIO_InitTypeDef structure
71        (++) Analog mode is required when a pin is to be used as ADC channel 
72             or DAC output.
73        (++) In case of external interrupt/event selection the "Mode" member from 
74             GPIO_InitTypeDef structure select the type (interrupt or event) and 
75             the corresponding trigger event (rising or falling or both).
76   
77    (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority 
78        mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
79        HAL_NVIC_EnableIRQ().
80   
81    (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
82   
83    (#) To set/reset the level of a pin configured in output mode use 
84        HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
85   
86    (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
87   
88    (#) During and just after reset, the alternate functions are not 
89        active and the GPIO pins are configured in input floating mode (except JTAG
90        pins).
91   
92    (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose 
93        (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has 
94        priority over the GPIO function.
95   
96    (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as 
97        general purpose Px0 and Px1, respectively, when the HSE oscillator is off. 
98        The HSE has priority over the GPIO function.
99   
100   @endverbatim
101   ******************************************************************************
102   * @attention
103   *
104   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
105   *
106   * Redistribution and use in source and binary forms, with or without modification,
107   * are permitted provided that the following conditions are met:
108   *   1. Redistributions of source code must retain the above copyright notice,
109   *      this list of conditions and the following disclaimer.
110   *   2. Redistributions in binary form must reproduce the above copyright notice,
111   *      this list of conditions and the following disclaimer in the documentation
112   *      and/or other materials provided with the distribution.
113   *   3. Neither the name of STMicroelectronics nor the names of its contributors
114   *      may be used to endorse or promote products derived from this software
115   *      without specific prior written permission.
116   *
117   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
118   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
119   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
120   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
121   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
122   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
123   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
124   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
125   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
126   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
127   *
128   ******************************************************************************  
129   */
130
131 /* Includes ------------------------------------------------------------------*/
132 #include "stm32f3xx_hal.h"
133
134 /** @addtogroup STM32F3xx_HAL_Driver
135   * @{
136   */
137
138 /** @defgroup GPIO GPIO HAL module driver
139   * @brief GPIO HAL module driver
140   * @{
141   */
142
143 #ifdef HAL_GPIO_MODULE_ENABLED
144
145 /* Private typedef -----------------------------------------------------------*/
146 /* Private define ------------------------------------------------------------*/
147 /* Private macro -------------------------------------------------------------*/
148 /** @defgroup GPIO_Private_Macros GPIO Private Macros
149  * @{
150  */
151 #define GET_GPIO_SOURCE(__GPIOx__) \
152 (((uint32_t)(__GPIOx__) == ((uint32_t)GPIOA_BASE))? 0U :\
153  ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x0400)))? 1U :\
154  ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x0800)))? 2U :\
155  ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x0C00)))? 3U :\
156  ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x1000)))? 4U :\
157  ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x1400)))? 5U : 6U)
158  
159 #define GPIO_MODE             ((uint32_t)0x00000003)
160 #define EXTI_MODE             ((uint32_t)0x10000000)
161 #define GPIO_MODE_IT          ((uint32_t)0x00010000)
162 #define GPIO_MODE_EVT         ((uint32_t)0x00020000)
163 #define RISING_EDGE           ((uint32_t)0x00100000) 
164 #define FALLING_EDGE          ((uint32_t)0x00200000) 
165 #define GPIO_OUTPUT_TYPE      ((uint32_t)0x00000010) 
166
167 #define GPIO_NUMBER           ((uint32_t)16)
168 /**
169   * @}
170   */
171
172 /* Private variables ---------------------------------------------------------*/
173 /* Private function prototypes -----------------------------------------------*/
174 /* Exported functions ---------------------------------------------------------*/
175 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
176 * @{
177 */
178
179 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
180  *  @brief    Initialization and Configuration functions 
181  *
182 @verbatim    
183  ===============================================================================
184               ##### Initialization and de-initialization functions #####
185  ===============================================================================
186  
187 @endverbatim
188   * @{
189   */
190
191 /**
192   * @brief  Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
193   * @param  GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family devices
194   * @param  GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
195   *         the configuration information for the specified GPIO peripheral.
196   * @retval None
197   */
198 void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)
199
200   uint32_t position;
201   uint32_t ioposition = 0x00;
202   uint32_t iocurrent = 0x00;
203   uint32_t temp = 0x00;
204
205   /* Check the parameters */
206   assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
207   assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
208   assert_param(IS_GPIO_PULL(GPIO_Init->Pull)); 
209
210   /* Configure the port pins */
211   for (position = 0; position < GPIO_NUMBER; position++)
212   {
213     /* Get the IO position */
214     ioposition = ((uint32_t)0x01) << position;
215     /* Get the current IO position */
216     iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
217  
218     if (iocurrent == ioposition)
219     {
220       /*--------------------- GPIO Mode Configuration ------------------------*/ 
221       /* In case of Alternate function mode selection */
222       if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD)) 
223       {
224         /* Check the Alternate function parameter */
225         assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
226         /* Configure Alternate function mapped with the current IO */ 
227         temp = GPIOx->AFR[position >> 3];
228         temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
229         temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4));
230         GPIOx->AFR[position >> 3] = temp;
231       }
232
233       /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
234       temp = GPIOx->MODER;
235       temp &= ~(GPIO_MODER_MODER0 << (position * 2));
236       temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2));
237       GPIOx->MODER = temp;
238
239       /* In case of Output or Alternate function mode selection */
240       if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
241           (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
242       {
243         /* Check the Speed parameter */
244         assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
245         /* Configure the IO Speed */
246         temp = GPIOx->OSPEEDR; 
247         temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
248         temp |= (GPIO_Init->Speed << (position * 2));
249         GPIOx->OSPEEDR = temp;
250
251         /* Configure the IO Output Type */
252         temp = GPIOx->OTYPER;
253         temp &= ~(GPIO_OTYPER_OT_0 << position) ;
254         temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4) << position);
255         GPIOx->OTYPER = temp;
256       }
257
258       /* Activate the Pull-up or Pull down resistor for the current IO */
259       temp = GPIOx->PUPDR;
260       temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
261       temp |= ((GPIO_Init->Pull) << (position * 2));
262       GPIOx->PUPDR = temp;
263
264       /*--------------------- EXTI Mode Configuration ------------------------*/
265       /* Configure the External Interrupt or event for the current IO */
266       if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE) 
267       {
268         /* Enable SYSCFG Clock */
269         __SYSCFG_CLK_ENABLE();
270   
271         temp = SYSCFG->EXTICR[position >> 2];
272         temp &= ~(((uint32_t)0x0F) << (4 * (position & 0x03)));
273         temp |= ((uint32_t)(GET_GPIO_SOURCE(GPIOx)) << (4 * (position & 0x03)));
274         SYSCFG->EXTICR[position >> 2] = temp;
275                   
276         /* Clear EXTI line configuration */
277         temp = EXTI->IMR;
278         temp &= ~((uint32_t)iocurrent);
279         if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
280         {
281           temp |= iocurrent;
282         }
283         EXTI->IMR = temp;
284
285         temp = EXTI->EMR;
286         temp &= ~((uint32_t)iocurrent);
287         if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
288         { 
289           temp |= iocurrent;
290         }
291         EXTI->EMR = temp;
292   
293         /* Clear Rising Falling edge configuration */
294         temp = EXTI->RTSR;
295         temp &= ~((uint32_t)iocurrent);
296         if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
297         {
298           temp |= iocurrent;
299         }
300         EXTI->RTSR = temp;
301
302         temp = EXTI->FTSR;
303         temp &= ~((uint32_t)iocurrent);
304         if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
305         {
306           temp |= iocurrent;
307         }
308         EXTI->FTSR = temp;
309       }
310     }
311   } 
312 }
313
314 /**
315   * @brief  De-initializes the GPIOx peripheral registers to their default reset values.
316   * @param  GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F30X device or STM32F37X device
317   * @param  GPIO_Pin: specifies the port bit to be written.
318   *          This parameter can be one of GPIO_PIN_x where x can be (0..15).
319   * @retval None
320   */
321 void HAL_GPIO_DeInit(GPIO_TypeDef  *GPIOx, uint32_t GPIO_Pin)
322 {
323   uint32_t position;
324   uint32_t ioposition = 0x00;
325   uint32_t iocurrent = 0x00;
326   uint32_t tmp = 0x00;
327
328   /* Configure the port pins */
329   for (position = 0; position < GPIO_NUMBER; position++)
330   {
331     /* Get the IO position */
332     ioposition = ((uint32_t)0x01) << position;
333     /* Get the current IO position */
334     iocurrent = (GPIO_Pin) & ioposition;
335
336     if (iocurrent == ioposition)
337     {
338       /*------------------------- GPIO Mode Configuration --------------------*/
339       /* Configure IO Direction in Input Floting Mode */
340       GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2));
341   
342       /* Configure the default Alternate Function in current IO */ 
343       GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
344   
345       /* Configure the default value for IO Speed */
346       GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
347                   
348       /* Configure the default value IO Output Type */
349       GPIOx->OTYPER  &= ~(GPIO_OTYPER_OT_0 << position) ;
350   
351       /* Deactivate the Pull-up oand Pull-down resistor for the current IO */
352       GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
353
354   
355       /*------------------------- EXTI Mode Configuration --------------------*/
356       /* Configure the External Interrupt or event for the current IO */
357       tmp = ((uint32_t)0x0F) << (4 * (position & 0x03));
358       SYSCFG->EXTICR[position >> 2] &= ~tmp;
359   
360       /* Clear EXTI line configuration */
361       EXTI->IMR &= ~((uint32_t)iocurrent);
362       EXTI->EMR &= ~((uint32_t)iocurrent);
363                   
364       /* Clear Rising Falling edge configuration */
365       EXTI->RTSR &= ~((uint32_t)iocurrent);
366       EXTI->FTSR &= ~((uint32_t)iocurrent);
367     }
368   }
369 }
370
371 /**
372   * @}
373   */
374
375 /** @defgroup GPIO_Exported_Functions_Group2 Input and Output operation functions 
376  *  @brief   GPIO Read and Write 
377  *
378 @verbatim   
379  ===============================================================================
380                        ##### IO operation functions #####
381  ===============================================================================  
382
383 @endverbatim
384   * @{
385   */
386
387 /**
388   * @brief  Reads the specified input port pin.
389   * @param  GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family
390   * @param  GPIO_Pin: specifies the port bit to read.
391   *         This parameter can be GPIO_PIN_x where x can be (0..15).
392   * @retval The input port pin value.
393   */
394 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
395 {
396   GPIO_PinState bitstatus;
397
398   /* Check the parameters */
399   assert_param(IS_GPIO_PIN(GPIO_Pin));
400
401   if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
402   {
403     bitstatus = GPIO_PIN_SET;
404   }
405   else
406   {
407     bitstatus = GPIO_PIN_RESET;
408   }
409   return bitstatus;
410   }
411
412 /**
413   * @brief  Sets or clears the selected data port bit.
414   * 
415   * @note   This function uses GPIOx_BSRR register to allow atomic read/modify 
416   *         accesses. In this way, there is no risk of an IRQ occurring between
417   *         the read and the modify access.
418   *               
419   * @param  GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family
420   * @param  GPIO_Pin: specifies the port bit to be written.
421   *          This parameter can be one of GPIO_PIN_x where x can be (0..15).
422   * @param  PinState: specifies the value to be written to the selected bit.
423   *          This parameter can be one of the GPIO_PinState enum values:
424   *            @arg GPIO_PIN_RESET: to clear the port pin
425   *            @arg GPIO_PIN_SET: to set the port pin
426   * @retval None
427   */
428 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
429 {
430   /* Check the parameters */
431   assert_param(IS_GPIO_PIN(GPIO_Pin));
432   assert_param(IS_GPIO_PIN_ACTION(PinState));
433
434   if (PinState != GPIO_PIN_RESET)
435   {
436     GPIOx->BSRRL = GPIO_Pin;
437   }
438   else
439   {
440     GPIOx->BSRRH = GPIO_Pin ;
441   }
442 }
443   
444 /**
445   * @brief  Toggles the specified GPIO pin
446   * @param  GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family
447   * @param  GPIO_Pin: specifies the pins to be toggled.
448   * @retval None
449   */
450 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
451 {
452   /* Check the parameters */
453   assert_param(IS_GPIO_PIN(GPIO_Pin));
454
455   GPIOx->ODR ^= GPIO_Pin;
456 }
457
458 /**
459 * @brief  Locks GPIO Pins configuration registers.
460 * @note   The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
461 *         GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
462 * @note   The configuration of the locked GPIO pins can no longer be modified
463 *         until the next reset.
464 * @param  GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family
465 * @param  GPIO_Pin: specifies the port bit to be locked.
466 *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
467 * @retval None
468 */
469 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
470 {
471   __IO uint32_t tmp = GPIO_LCKR_LCKK;
472
473   /* Check the parameters */
474   assert_param(IS_GPIO_PIN(GPIO_Pin));
475
476   /* Apply lock key write sequence */
477   tmp |= GPIO_Pin;
478   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
479   GPIOx->LCKR = tmp;
480   /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
481   GPIOx->LCKR = GPIO_Pin;
482   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
483   GPIOx->LCKR = tmp;
484   /* Read LCKK bit*/
485   tmp = GPIOx->LCKR;
486
487   if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
488   {
489     return HAL_OK;
490   }
491   else
492   {
493     return HAL_ERROR;
494   }
495 }
496
497 /**
498   * @brief This function handles EXTI interrupt request.
499   * @param GPIO_Pin: Specifies the pins connected EXTI line
500   * @retval None
501   */
502 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
503 {
504   /* EXTI line interrupt detected */
505   if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET) 
506   { 
507     __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
508     HAL_GPIO_EXTI_Callback(GPIO_Pin);
509   }
510 }
511
512 /**
513   * @brief  EXTI line detection callbacks.
514   * @param GPIO_Pin: Specifies the pins connected EXTI line
515   * @retval None
516   */
517 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
518 {
519   /* NOTE : This function Should not be modified, when the callback is needed,
520             the HAL_GPIO_EXTI_Callback could be implemented in the user file
521    */ 
522 }
523
524 /**
525   * @}
526   */
527
528
529 /**
530   * @}
531   */
532
533 #endif /* HAL_GPIO_MODULE_ENABLED */
534 /**
535   * @}
536   */
537
538 /**
539   * @}
540   */
541
542 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/