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 */
39 
40 #include "gpio_driver.h"
41 
42 /*******************************************************************************
43 * Definitions
44 ******************************************************************************/
45 #define GPIO_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2, 0) /* driver version */
46 // ISR handler array for each gpio pin in the system
47 #define GPIO_NUMBER_OF_PIN 0x20
48 
49 /*******************************************************************************
50 * Variables
51 ******************************************************************************/
52 
53 /* Driver Version */
54 static const GENERIC_DRIVER_VERSION DriverVersion = {GPIO_API_VERSION, GPIO_DRV_VERSION};
56 static gpioConfigKSDK_t gpioConfigDefault = {
57  .pinConfig = {kGPIO_DigitalInput, 0}, .modeFunc = 0, .interruptMode = kGPIO_InterruptRisingEdge};
58 
59 /*******************************************************************************
60  * Code
61  ******************************************************************************/
62 
63 /***********************************************************************
64  *
65  * Function Name : ksdk_gpio_get_version
66  * Description : get the driver version.
67  *
68  ***************************************************************************/
70 {
71  return DriverVersion;
72 }
73 
74 /***********************************************************************
75  *
76  * Function Name : ksdk_gpio_set_interrupt
77  * Description : Set the Interrupt.
78  *
79  ***************************************************************************/
80 void ksdk_gpio_set_interrupt(GPIO_Type *base, uint32_t mask, uint8_t interruptMode)
81 {
82  switch (interruptMode)
83  {
85  GPIO_SetLowLevelInterrupt(base, mask);
86  break;
88  GPIO_SetHighLevelInterrupt(base, mask);
89  break;
91  GPIO_SetFallingEdgeInterrupt(base, mask);
92  break;
94  GPIO_SetRisingEdgeInterrupt(base, mask);
95  break;
96  default:
97  break;
98  }
99 
100  GPIO_EnableInterrupt(base, mask);
101 }
102 
103 /***********************************************************************
104  *
105  * Function Name : ksdk_gpio_pin_init
106  * Description : Initialize particular GPIO pin used by board.
107  *
108  ***************************************************************************/
110  pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
111 {
112  gpioConfigKSDK_t *pGpioConfig = (gpioConfigKSDK_t *)apPinConfig;
113  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
114  if (NULL == apPinConfig)
115  {
116  pGpioConfig = &gpioConfigDefault;
117  pGpioConfig->modeFunc = (IOCON_FUNC0 | IOCON_MODE_PULLUP);
118  }
119  // Configure the clock
120  CLOCK_EnableClock(pinHandle->clockName);
121 
122  // Set the ICON pin mux
123  IOCON_PinMuxSet(IOCON, pinHandle->portNumber, pinHandle->pinNumber, pGpioConfig->modeFunc);
124 
125  pGpioConfig->pinConfig.pinDirection = dir == GPIO_DIRECTION_IN ? kGPIO_DigitalInput : kGPIO_DigitalOutput;
126  // Set the pin information
127  GPIO_PinInit(pinHandle->base, pinHandle->pinNumber, &pGpioConfig->pinConfig);
128 
129  // Isr is installed
130  if (aIsrHandler)
131  {
132  // Enable the IRQ
133  EnableIRQ(pinHandle->irq);
134  isrObj[pinHandle->portNumber][pinHandle->pinNumber].isrHandle = aIsrHandler;
135  isrObj[pinHandle->portNumber][pinHandle->pinNumber].pUserData = apUserData;
136  // Enable the interrupt on a pin.
137  ksdk_gpio_set_interrupt(pinHandle->base, pinHandle->mask, pGpioConfig->interruptMode);
138  }
139 }
140 
141 /***********************************************************************
142  *
143  * Function Name : ksdk_gpio_set_pin
144  * Description : Set output level of individual GPIO pin to logic 1.
145  *
146  ***************************************************************************/
148 {
149  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
150  GPIO_SetPinsOutput(pinHandle->base, pinHandle->mask);
151 }
152 
153 /***********************************************************************
154  *
155  * Function Name : ksdk_gpio_clr_pin
156  * Description : Set output level of individual GPIO pin to logic 0..
157  *
158  ***************************************************************************/
160 {
161  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
162  GPIO_ClearPinsOutput(pinHandle->base, pinHandle->mask);
163 }
164 
165 /***********************************************************************
166  *
167  * Function Name : ksdk_gpio_toggle_pin
168  * Description : toggle the currrent output logic of individual GPIO pin.
169  *
170  ***************************************************************************/
172 {
173  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
174  GPIO_TogglePinsOutput(pinHandle->base, pinHandle->mask);
175 }
176 
177 /***********************************************************************
178  *
179  * Function Name : ksdk_gpio_write_pin
180  * Description : Set output level of individual GPIO pin to desired value, ie 1 or 0.
181  *
182  ***************************************************************************/
183 void ksdk_gpio_write_pin(pinID_t aPinId, uint8_t aValue)
184 {
185  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
186  GPIO_WritePinOutput(pinHandle->base, pinHandle->pinNumber, aValue);
187 }
188 
189 /***********************************************************************
190  *
191  * Function Name : ksdk_gpio_read_pin
192  * Description : Read current input value of individual GPIO pin.
193  *
194  ***************************************************************************/
195 uint32_t ksdk_gpio_read_pin(pinID_t aPinId)
196 {
197  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
198  return GPIO_ReadPinInput(pinHandle->base, pinHandle->pinNumber);
199 }
200 
201 /***********************************************************************
202  *
203  * Function Name : ksdk_gpio_handle_interrupt
204  * Description : handle the gpio interrupt in a pin.
205  *
206  ***************************************************************************/
207 void ksdk_gpio_handle_interrupt(GPIO_Type *apBase, port_number_t aPortNumber)
208 {
209  uint32_t isfr = GPIO_GetPinsInterruptFlags(apBase);
210 
211  // parse through all the pending interrupt for a PORT
212  for (uint8_t i = 0; i < GPIO_NUMBER_OF_PIN; i++)
213  {
214  if (isfr & (1 << i))
215  {
216  gpio_isr_handler_t handle = isrObj[aPortNumber][i].isrHandle;
217  if (handle == NULL)
218  {
219  continue;
220  }
221  // call user defined handler
222  handle(isrObj[aPortNumber][i].pUserData);
223  GPIO_ClearPinsInterruptFlags(apBase, (1 << i));
224  }
225  }
226 }
227 
231 };
GENERIC_DRIVER_VERSION ksdk_gpio_get_version(void)
Definition: gpio_driver.c:69
The GPIO pin handle for KSDK.
Definition: gpio_driver.h:75
void ksdk_gpio_handle_interrupt(GPIO_Type *apBase, port_number_t aPortNumber)
Definition: gpio_driver.c:179
port_interrupt_t interruptMode
Definition: gpio_driver.h:69
void ksdk_gpio_set_interrupt(GPIO_Type *base, uint32_t mask, uint8_t interruptMode)
Definition: gpio_driver.c:80
The GPIO Configuration KSDK.
Definition: gpio_driver.h:65
uint32_t modeFunc
Definition: gpio_driver.h:73
enum port_number port_number_t
GPIO PORT NAMES.
void(* gpio_isr_handler_t)(void *apUserData)
Definition: Driver_GPIO.h:60
IRQn_Type irq
Definition: gpio_driver.h:81
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
#define GPIO_DRV_VERSION
Definition: gpio_driver.c:45
uint32_t mask
Definition: gpio_driver.h:80
void ksdk_gpio_write_pin(pinID_t aPinId, uint8_t aValue)
Definition: gpio_driver.c:155
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 GPIO_NUMBER_OF_PIN
Definition: gpio_driver.c:47
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