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}, .portPinConfig = {0}, .interruptMode = kPORT_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_pin_init
77  * Description : Initialize particular GPIO pin used by board.
78  *
79  ***************************************************************************/
81  pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
82 {
83  gpioConfigKSDK_t *pGpioConfig = (gpioConfigKSDK_t *)apPinConfig;
84  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
85  if (NULL == apPinConfig)
86  {
87  pGpioConfig = &gpioConfigDefault;
88  pGpioConfig->portPinConfig.pullSelect = kPORT_PullUp;
89  pGpioConfig->portPinConfig.mux = kPORT_MuxAsGpio;
90  }
91  // Configure the clock
92  CLOCK_EnableClock(pinHandle->clockName);
93 
94  // Set the port pin configuration value
95  PORT_SetPinConfig(pinHandle->portBase, pinHandle->pinNumber, &pGpioConfig->portPinConfig);
96 
97  pGpioConfig->pinConfig.pinDirection = dir == GPIO_DIRECTION_IN ? kGPIO_DigitalInput : kGPIO_DigitalOutput;
98  // Set the pin information
99  GPIO_PinInit(pinHandle->base, pinHandle->pinNumber, &pGpioConfig->pinConfig);
100 
101  // Isr is installed
102  if (aIsrHandler)
103  {
104  // Enable the IRQ
105  EnableIRQ(pinHandle->irq);
106  isrObj[pinHandle->portNumber][pinHandle->pinNumber].isrHandle = aIsrHandler;
107  isrObj[pinHandle->portNumber][pinHandle->pinNumber].pUserData = apUserData;
108  // Enable the interrupt on a pin.
109  PORT_SetPinInterruptConfig(pinHandle->portBase, pinHandle->pinNumber, pGpioConfig->interruptMode);
110  }
111 }
112 
113 /***********************************************************************
114  *
115  * Function Name : ksdk_gpio_set_pin
116  * Description : Set output level of individual GPIO pin to logic 1.
117  *
118  ***************************************************************************/
120 {
121  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
122  GPIO_SetPinsOutput(pinHandle->base, pinHandle->mask);
123 }
124 
125 /***********************************************************************
126  *
127  * Function Name : ksdk_gpio_clr_pin
128  * Description : Set output level of individual GPIO pin to logic 0..
129  *
130  ***************************************************************************/
132 {
133  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
134  GPIO_ClearPinsOutput(pinHandle->base, pinHandle->mask);
135 }
136 
137 /***********************************************************************
138  *
139  * Function Name : ksdk_gpio_toggle_pin
140  * Description : toggle the currrent output logic of individual GPIO pin.
141  *
142  ***************************************************************************/
144 {
145  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
146  GPIO_TogglePinsOutput(pinHandle->base, pinHandle->mask);
147 }
148 
149 /***********************************************************************
150  *
151  * Function Name : ksdk_gpio_write_pin
152  * Description : Set output level of individual GPIO pin to desired value, ie 1 or 0.
153  *
154  ***************************************************************************/
155 void ksdk_gpio_write_pin(pinID_t aPinId, uint8_t aValue)
156 {
157  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
158  GPIO_WritePinOutput(pinHandle->base, pinHandle->pinNumber, aValue);
159 }
160 
161 /***********************************************************************
162  *
163  * Function Name : ksdk_gpio_read_pin
164  * Description : Read current input value of individual GPIO pin.
165  *
166  ***************************************************************************/
167 uint32_t ksdk_gpio_read_pin(pinID_t aPinId)
168 {
169  gpioHandleKSDK_t *pinHandle = (gpioHandleKSDK_t *)aPinId;
170  return GPIO_ReadPinInput(pinHandle->base, pinHandle->pinNumber);
171 }
172 
173 /***********************************************************************
174  *
175  * Function Name : ksdk_gpio_handle_interrupt
176  * Description : handle the gpio interrupt in a pin.
177  *
178  ***************************************************************************/
179 void ksdk_gpio_handle_interrupt(GPIO_Type *apBase, port_number_t aPortNumber)
180 {
181  uint32_t isfr = GPIO_GetPinsInterruptFlags(apBase);
182 
183  // parse through all the pending interrupt for a PORT
184  for (uint8_t i = 0; i < GPIO_NUMBER_OF_PIN; i++)
185  {
186  if (isfr & (1 << i))
187  {
188  gpio_isr_handler_t handle = isrObj[aPortNumber][i].isrHandle;
189  if (handle == NULL)
190  {
191  continue;
192  }
193  // call user defined handler
194  handle(isrObj[aPortNumber][i].pUserData);
195  GPIO_ClearPinsInterruptFlags(apBase, (1 << i));
196  }
197  }
198 }
199 
203 };
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
The GPIO Configuration KSDK.
Definition: gpio_driver.h:65
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
#define GPIO_DRV_VERSION
Definition: gpio_driver.c:45
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
port_pin_config_t portPinConfig
Definition: gpio_driver.h:68
gpio_isr_handler_t isrHandle
Definition: gpio_driver.h:91
PORT_Type * portBase
Definition: gpio_driver.h:78
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