ISSDK  1.7
IoT Sensing Software Development Kit
gpio_driver.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36 * @file gpio_driver.c
37 * @brief The gpio_driver.c file contains Generic API Adaption to SDK 2.0 GPIO Driver.
38 * The type and variable names have been kept aligned to Kinetis family for compatibility of examples.
39 */
40 
41 #include "gpio_driver.h"
42 
43 /*******************************************************************************
44 * Definitions
45 ******************************************************************************/
46 #define GPIO_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2, 2) /* driver version */
47 #define GINT_COUNT (sizeof(gintBases) / sizeof(void *)) /* Number of GINTs*/
48 #define GINT_NUMBER_OF_PIN 32 /* Each Port has 32 Pins */
49 
50 /*******************************************************************************
51 * Prototypes
52 ******************************************************************************/
53 GINT_Type *const gintBases[] = GINT_BASE_PTRS;
54 #if defined(GINT0)
55 extern void PORT0_IRQHandler(void);
56 #endif
57 #if defined(GINT1)
58 extern void PORT1_IRQHandler(void);
59 #endif
60 #if defined(GINT2)
61 extern void PORT2_IRQHandler(void);
62 #endif
63 #if defined(GINT3)
64 extern void PORT3_IRQHandler(void);
65 #endif
66 #if defined(GINT4)
67 extern void PORT4_IRQHandler(void);
68 #endif
69 #if defined(GINT5)
70 extern void PORT5_IRQHandler(void);
71 #endif
72 #if defined(GINT6)
73 extern void PORT6_IRQHandler(void);
74 #endif
75 #if defined(GINT7)
76 extern void PORT7_IRQHandler(void);
77 #endif
78 
79 /*******************************************************************************
80 * Variables
81 ******************************************************************************/
82 
83 /* Driver Version */
84 static const GENERIC_DRIVER_VERSION DriverVersion = {GPIO_API_VERSION, GPIO_DRV_VERSION};
85 /* ISR handler array for each gpio pin in the system */
87 /* GPIO Pin characteristic */
88 static gpioConfigKSDK_t gpioConfigDefault = {
89  .pinConfig = {kGPIO_DigitalInput, 0}, .interruptMode = kGINT_TrigEdge, .interruptPolarity = kGINT_InterruptLogic_1,
90 };
91 /* GINT base specific ISR callback list */
92 static gint_cb_t gintIsrCb[GINT_COUNT] = {
93 #if defined(GINT0)
94  PORT0_IRQHandler,
95 #endif
96 #if defined(GINT1)
97  PORT1_IRQHandler,
98 #endif
99 #if defined(GINT2)
100  PORT2_IRQHandler,
101 #endif
102 #if defined(GINT3)
103  PORT3_IRQHandler,
104 #endif
105 #if defined(GINT4)
106  PORT4_IRQHandler,
107 #endif
108 #if defined(GINT5)
109  PORT5_IRQHandler,
110 #endif
111 #if defined(GINT6)
112  PORT6_IRQHandler,
113 #endif
114 #if defined(GINT7)
115  PORT7_IRQHandler,
116 #endif
117 };
118 
119 /*******************************************************************************
120  * Code
121  ******************************************************************************/
122 
123 /***********************************************************************
124  *
125  * Function Name : ksdk_gpio_get_version
126  * Description : get the driver version.
127  *
128  ***************************************************************************/
130 {
131  return DriverVersion;
132 }
133 
134 /***********************************************************************
135  *
136  * Function Name : ksdk_gpio_pin_init
137  * Description : Initialize particular GPIO pin used by board.
138  *
139  ***************************************************************************/
141  pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
142 {
143  uint32_t polarityMask, enableMask;
144 
145  gpioConfigKSDK_t *pGpioConfig;
146  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
147  if (NULL == apPinConfig)
148  {
149  pGpioConfig = &gpioConfigDefault;
150  }
151  else
152  {
153  pGpioConfig = (gpioConfigKSDK_t *)apPinConfig;
154  }
155 
156  // Configure as GPIO
157  CLOCK_EnableClock(pinHandle->clockName);
158  pGpioConfig->pinConfig.pinDirection = (dir == GPIO_DIRECTION_IN) ? kGPIO_DigitalInput : kGPIO_DigitalOutput;
159  GPIO_PinInit(pinHandle->base, pinHandle->portNumber, pinHandle->pinNumber, &pGpioConfig->pinConfig);
160 
161  // Configure is GINT if ISR requested (There should be as many GINTs as there are GPIO Ports).
162  if (aIsrHandler && pinHandle->portNumber < GINT_COUNT)
163  {
164  /* Initialize GINT device for the associated Port */
165  GINT_Init(gintBases[pinHandle->portNumber]);
166 
167  // Enable the IRQ
168  isrObj[pinHandle->portNumber][pinHandle->pinNumber].isrHandle = aIsrHandler;
169  isrObj[pinHandle->portNumber][pinHandle->pinNumber].pUserData = apUserData;
170 
171  /* Setup GINT trigger mode and "OR" mode */
172  GINT_SetCtrl(gintBases[pinHandle->portNumber], kGINT_CombineOr, pGpioConfig->interruptMode,
173  gintIsrCb[pinHandle->portNumber]);
174 
175  /* Get current pins & polarity for GINT */
176  GINT_GetConfigPins(gintBases[pinHandle->portNumber], pinHandle->portNumber, &polarityMask, &enableMask);
177 
178  /* Update Polarity for this PIN */
179  if (pGpioConfig->interruptPolarity == kGINT_InterruptLogic_1)
180  {
181  polarityMask |= pinHandle->mask; /* Logic 1 for : Level High or Rising Edge */
182  }
183  else
184  {
185  polarityMask &= ~(pinHandle->mask); /* Logic 0 for : Level Low or Falling Edge */
186  }
187  enableMask |= pinHandle->mask;
188 
189  /* Select pins & polarity for GINT */
190  GINT_ConfigPins(gintBases[pinHandle->portNumber], pinHandle->portNumber, polarityMask, enableMask);
191 
192  /* Enable callbacks for GINT */
193  GINT_EnableCallback(gintBases[pinHandle->portNumber]);
194  }
195 }
196 
197 /***********************************************************************
198  *
199  * Function Name : ksdk_gpio_set_pin
200  * Description : Set output level of individual GPIO pin to logic 1.
201  *
202  ***************************************************************************/
204 {
205  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
206  GPIO_SetPinsOutput(pinHandle->base, pinHandle->portNumber, pinHandle->mask);
207 }
208 
209 /***********************************************************************
210  *
211  * Function Name : ksdk_gpio_clr_pin
212  * Description : Set output level of individual GPIO pin to logic 0..
213  *
214  ***************************************************************************/
216 {
217  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
218  GPIO_ClearPinsOutput(pinHandle->base, pinHandle->portNumber, pinHandle->mask);
219 }
220 
221 /***********************************************************************
222  *
223  * Function Name : ksdk_gpio_toggle_pin
224  * Description : toggle the currrent output logic of individual GPIO pin.
225  *
226  ***************************************************************************/
228 {
229  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
230  GPIO_TogglePinsOutput(pinHandle->base, pinHandle->portNumber, pinHandle->mask);
231 }
232 
233 /***********************************************************************
234  *
235  * Function Name : ksdk_gpio_write_pin
236  * Description : Set output level of individual GPIO pin to desired value, ie 1 or 0.
237  *
238  ***************************************************************************/
239 void ksdk_gpio_write_pin(pinID_t aPinId, uint8_t aValue)
240 {
241  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
242  GPIO_WritePinOutput(pinHandle->base, pinHandle->portNumber, pinHandle->pinNumber, aValue);
243 }
244 
245 /***********************************************************************
246  *
247  * Function Name : ksdk_gpio_read_pin
248  * Description : Read current input value of individual GPIO pin.
249  *
250  ***************************************************************************/
251 uint32_t ksdk_gpio_read_pin(pinID_t aPinId)
252 {
253  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
254  return GPIO_ReadPinInput(pinHandle->base, pinHandle->portNumber, pinHandle->pinNumber);
255 }
256 
257 /***********************************************************************
258  *
259  * Function Name : ksdk_gpio_handle_interrupt
260  * Description : handle the gint interrupt of a port.
261  *
262  ***************************************************************************/
263 void issdk_gpio_handle_interrupt(GINT_Type *apBase, gint_port_t aPortNumber)
264 {
265  uint32_t enableMask, polarityMask;
266 
267  GINT_GetConfigPins(apBase, aPortNumber, &polarityMask, &enableMask);
268  // parse through all the pending interrupt for a PORT
269  for (uint8_t i = 0; i < GINT_NUMBER_OF_PIN; i++)
270  {
271  if (enableMask & (1 << i))
272  {
273  gpio_isr_handler_t handle = isrObj[aPortNumber][i].isrHandle;
274  if (handle == NULL)
275  {
276  continue;
277  }
278  // call user defined handler
279  handle(isrObj[aPortNumber][i].pUserData);
280  }
281  }
282 }
283 
287 };
GENERIC_DRIVER_VERSION ksdk_gpio_get_version(void)
Definition: gpio_driver.c:69
#define GPIO_DRV_VERSION
Definition: gpio_driver.c:46
#define GINT_COUNT
Definition: gpio_driver.c:47
The GPIO pin handle for KSDK.
Definition: gpio_driver.h:75
void issdk_gpio_handle_interrupt(GINT_Type *apBase, gint_port_t aPortNumber)
Definition: gpio_driver.c:263
port_interrupt_t interruptMode
Definition: gpio_driver.h:69
The GPIO Configuration KSDK.
Definition: gpio_driver.h:65
void(* gpio_isr_handler_t)(void *apUserData)
Definition: Driver_GPIO.h:60
ARM_DRIVER_VERSION GENERIC_DRIVER_VERSION
Definition: Driver_GPIO.h:58
uint32_t ksdk_gpio_read_pin(pinID_t aPinId)
Definition: gpio_driver.c:167
enum gpio_direction_en gpio_direction_t
gpio_pin_config_t pinConfig
Definition: gpio_driver.h:67
The gpio isr object.
Definition: gpio_driver.h:88
void * pinID_t
GPIO Driver direction.
Definition: Driver_GPIO.h:56
#define GPIO_API_VERSION
Definition: Driver_GPIO.h:40
clock_ip_name_t clockName
Definition: gpio_driver.h:82
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
void * pUserData
Definition: gpio_driver.h:90
uint32_t pinNumber
Definition: gpio_driver.h:79
port_number_t portNumber
Definition: gpio_driver.h:83
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
gpio_isr_handler_t isrHandle
Definition: gpio_driver.h:91
uint32_t mask
Definition: gpio_driver.h:80
void ksdk_gpio_write_pin(pinID_t aPinId, uint8_t aValue)
Definition: gpio_driver.c:155
gint_interrupt_t interruptPolarity
Definition: gpio_driver.h:63
void ksdk_gpio_pin_init(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: gpio_driver.c:80
void ksdk_gpio_clr_pin(pinID_t aPinId)
Definition: gpio_driver.c:131
GPIO_Type * base
Definition: gpio_driver.h:77
#define GINT_NUMBER_OF_PIN
Definition: gpio_driver.c:48
void ksdk_gpio_toggle_pin(pinID_t aPinId)
Definition: gpio_driver.c:143
void ksdk_gpio_set_pin(pinID_t aPinId)
Definition: gpio_driver.c:119
GINT_Type *const gintBases[]
Definition: gpio_driver.c:53