fsl_gpio.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_gpio.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.igpio"
14 #endif
15 
16 /*******************************************************************************
17  * Variables
18  ******************************************************************************/
19 
20 /* Array of GPIO peripheral base address. */
22 
23 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
24 /* Array of GPIO clock name. */
26 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
27 
28 /*******************************************************************************
29  * Prototypes
30  ******************************************************************************/
31 
38 static uint32_t GPIO_GetInstance(GPIO_Type *base);
39 
40 /*******************************************************************************
41  * Code
42  ******************************************************************************/
43 
44 static uint32_t GPIO_GetInstance(GPIO_Type *base)
45 {
46  uint32_t instance;
47 
48  /* Find the instance index from base address mappings. */
49  for (instance = 0U; instance < ARRAY_SIZE(s_gpioBases); instance++)
50  {
51  if (s_gpioBases[instance] == base)
52  {
53  break;
54  }
55  }
56 
57  assert(instance < ARRAY_SIZE(s_gpioBases));
58 
59  return instance;
60 }
61 
71 void GPIO_PinInit(GPIO_Type *base, uint32_t pin, const gpio_pin_config_t *Config)
72 {
73 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
74  /* Enable GPIO clock. */
75  uint32_t instance = GPIO_GetInstance(base);
76 
77  /* If The clock IP is valid, enable the clock gate. */
78  if ((instance < ARRAY_SIZE(s_gpioClock)) && (kCLOCK_IpInvalid != s_gpioClock[instance]))
79  {
80  CLOCK_EnableClock(s_gpioClock[instance]);
81  }
82 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
83 
84  /* Register reset to default value */
85  base->IMR &= ~(1UL << pin);
86 
87  /* Configure GPIO pin direction */
88  if (Config->direction == kGPIO_DigitalInput)
89  {
90  base->GDIR &= ~(1UL << pin);
91  }
92  else
93  {
94  GPIO_PinWrite(base, pin, Config->outputLogic);
95  base->GDIR |= (1UL << pin);
96  }
97 
98  /* Configure GPIO pin interrupt mode */
99  GPIO_SetPinInterruptConfig(base, pin, Config->interruptMode);
100 }
101 
111 void GPIO_PinWrite(GPIO_Type *base, uint32_t pin, uint8_t output)
112 {
113  assert(pin < 32U);
114  if (output == 0U)
115  {
116  base->DR &= ~(1UL << pin); /* Set pin output to low level.*/
117  }
118  else
119  {
120  base->DR |= (1UL << pin); /* Set pin output to high level.*/
121  }
122 }
123 
132 void GPIO_PinSetInterruptConfig(GPIO_Type *base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode)
133 {
134  volatile uint32_t *icr;
135  uint32_t icrShift;
136 
137  icrShift = pin;
138 
139  /* Register reset to default value */
140  base->EDGE_SEL &= ~(1UL << pin);
141 
142  if (pin < 16U)
143  {
144  icr = &(base->ICR1);
145  }
146  else
147  {
148  icr = &(base->ICR2);
149  icrShift -= 16U;
150  }
151  switch (pinInterruptMode)
152  {
153  case (kGPIO_IntLowLevel):
154  *icr &= ~(3UL << (2UL * icrShift));
155  break;
156  case (kGPIO_IntHighLevel):
157  *icr = (*icr & (~(3UL << (2UL * icrShift)))) | (1UL << (2UL * icrShift));
158  break;
159  case (kGPIO_IntRisingEdge):
160  *icr = (*icr & (~(3UL << (2UL * icrShift)))) | (2UL << (2UL * icrShift));
161  break;
162  case (kGPIO_IntFallingEdge):
163  *icr |= (3UL << (2UL * icrShift));
164  break;
166  base->EDGE_SEL |= (1UL << pin);
167  break;
168  default:; /* Intentional empty default */
169  break;
170  }
171 }
_gpio_pin_config
GPIO Init structure definition.
Definition: fsl_gpio.h:48
s_gpioClock
static const clock_ip_name_t s_gpioClock[]
Definition: fsl_gpio.c:25
_gpio_pin_config::direction
gpio_pin_direction_t direction
Definition: fsl_gpio.h:50
GPIO_PinWrite
void GPIO_PinWrite(GPIO_Type *base, uint32_t pin, uint8_t output)
Sets the output level of the individual GPIO pin to logic 1 or 0.
Definition: fsl_gpio.c:111
GPIO_PinSetInterruptConfig
void GPIO_PinSetInterruptConfig(GPIO_Type *base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode)
Sets the current pin interrupt mode.
Definition: fsl_gpio.c:132
GPIO_Type::GDIR
__IO uint32_t GDIR
Definition: MIMXRT1052.h:19127
GPIO_Type::EDGE_SEL
__IO uint32_t EDGE_SEL
Definition: MIMXRT1052.h:19133
GPIO_Type::DR
__IO uint32_t DR
Definition: MIMXRT1052.h:19126
_gpio_pin_config::interruptMode
gpio_interrupt_mode_t interruptMode
Definition: fsl_gpio.h:53
GPIO_GetInstance
static uint32_t GPIO_GetInstance(GPIO_Type *base)
Gets the GPIO instance according to the GPIO base.
Definition: fsl_gpio.c:44
gpio_interrupt_mode_t
enum _gpio_interrupt_mode gpio_interrupt_mode_t
GPIO interrupt mode definition.
GPIO_CLOCKS
#define GPIO_CLOCKS
Clock ip name array for GPIO.
Definition: fsl_clock.h:247
kGPIO_IntRisingOrFallingEdge
@ kGPIO_IntRisingOrFallingEdge
Definition: fsl_gpio.h:44
GPIO_Type::IMR
__IO uint32_t IMR
Definition: MIMXRT1052.h:19131
ARRAY_SIZE
#define ARRAY_SIZE(x)
Computes the number of elements in an array.
Definition: fsl_common.h:211
kCLOCK_IpInvalid
@ kCLOCK_IpInvalid
Definition: fsl_clock.h:446
kGPIO_DigitalInput
@ kGPIO_DigitalInput
Definition: fsl_gpio.h:32
GPIO_PinInit
void GPIO_PinInit(GPIO_Type *base, uint32_t pin, const gpio_pin_config_t *Config)
Initializes the GPIO peripheral according to the specified parameters in the initConfig.
Definition: fsl_gpio.c:71
CLOCK_EnableClock
static void CLOCK_EnableClock(clock_ip_name_t name)
Enable the clock for specific IP.
Definition: fsl_clock.h:1059
GPIO_BASE_PTRS
#define GPIO_BASE_PTRS
Definition: MIMXRT1052.h:19556
kGPIO_IntLowLevel
@ kGPIO_IntLowLevel
Definition: fsl_gpio.h:40
fsl_gpio.h
kGPIO_IntHighLevel
@ kGPIO_IntHighLevel
Definition: fsl_gpio.h:41
GPIO_SetPinInterruptConfig
static void GPIO_SetPinInterruptConfig(GPIO_Type *base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode)
Sets the current pin interrupt mode.
Definition: fsl_gpio.h:240
kGPIO_IntFallingEdge
@ kGPIO_IntFallingEdge
Definition: fsl_gpio.h:43
s_gpioBases
static GPIO_Type *const s_gpioBases[]
Definition: fsl_gpio.c:21
clock_ip_name_t
enum _clock_ip_name clock_ip_name_t
CCM CCGR gate control for each module independently.
GPIO_Type
Definition: MIMXRT1052.h:19125
GPIO_Type::ICR1
__IO uint32_t ICR1
Definition: MIMXRT1052.h:19129
kGPIO_IntRisingEdge
@ kGPIO_IntRisingEdge
Definition: fsl_gpio.h:42
GPIO_Type::ICR2
__IO uint32_t ICR2
Definition: MIMXRT1052.h:19130
_gpio_pin_config::outputLogic
uint8_t outputLogic
Definition: fsl_gpio.h:51


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:56