Subversion Repositories FuelGauge

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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