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 2017 NXP
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted (subject to the limitations in the disclaimer below) provided
8  * that the following conditions are met:
9  *
10  * o Redistributions of source code must retain the above copyright notice, this list
11  * of conditions and the following disclaimer.
12  *
13  * o Redistributions in binary form must reproduce the above copyright notice, this
14  * list of conditions and the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  *
17  * o Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
26  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /**
35 * @file gpio_driver.c
36 * @brief The gpio_driver.c file contains Generic API Adaption to i.MXRT1050 SDK 2.0 GPIO Driver.
37 */
38 
39 #include "gpio_driver.h"
40 
41 /*******************************************************************************
42 * Definitions
43 ******************************************************************************/
44 #define GPIO_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2, 3) /* driver version */
45 // ISR handler array for each gpio pin in the system
46 #define GPIO_NUMBER_OF_PIN 0x20
47 
48 /*******************************************************************************
49 * Variables
50 ******************************************************************************/
51 
52 /* Driver Version */
53 static const GENERIC_DRIVER_VERSION DriverVersion = {GPIO_API_VERSION, GPIO_DRV_VERSION};
55 static gpioConfigiMXSDK_t gpioConfigDefault = {
56  .pinConfig = {kGPIO_DigitalInput, 0}, .interruptMode = kGPIO_IntLowLevel};
57 
58 /*******************************************************************************
59  * Code
60  ******************************************************************************/
61 
62 /***********************************************************************
63  *
64  * Function Name : imxsdk_gpio_get_version
65  * Description : get the i.MXRT1050RT gpio driver version.
66  *
67  ***************************************************************************/
69 {
70  return DriverVersion;
71 }
72 
73 /***********************************************************************
74  *
75  * Function Name : imxsdk_gpio_pin_init
76  * Description : Initialize particular GPIO pin used by board.
77  *
78  ***************************************************************************/
80  pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
81 {
82  gpioConfigiMXSDK_t *pGpioConfig = (gpioConfigiMXSDK_t *)apPinConfig;
83  gpioHandleiMXSDK_t *pinHandle = (gpioHandleiMXSDK_t *)aPinId;
84  if (NULL == apPinConfig)
85  {
86  pGpioConfig = &gpioConfigDefault;
87  }
88 
89  pGpioConfig->pinConfig.direction = dir == GPIO_DIRECTION_IN ? kGPIO_DigitalInput : kGPIO_DigitalOutput;
90  /* Set the pin information */
91  GPIO_PinInit(pinHandle->base, pinHandle->pinNumber, &pGpioConfig->pinConfig);
92 
93  /* Isr is installed */
94  if (aIsrHandler)
95  {
96  /* Enable the IRQ */
97  EnableIRQ(pinHandle->irq);
98  isrObj[pinHandle->portNumber][pinHandle->pinNumber].isrHandle = aIsrHandler;
99  isrObj[pinHandle->portNumber][pinHandle->pinNumber].pUserData = apUserData;
100  /* Enable GPIO pin interrupt */
101  GPIO_PortEnableInterrupts(pinHandle->base, pinHandle->mask);
102  }
103 }
104 
105 /***********************************************************************
106  *
107  * Function Name : imxsdk_gpio_set_pin
108  * Description : Set output level of individual GPIO pin to logic 1.
109  *
110  ***************************************************************************/
112 {
113  gpioHandleiMXSDK_t *pinHandle = (gpioHandleiMXSDK_t *)aPinId;
114  pinHandle->pinStatus = PIN_SET;
115  GPIO_PinWrite(pinHandle->base, pinHandle->pinNumber, PIN_SET);
116 }
117 
118 /***********************************************************************
119  *
120  * Function Name : imxsdk_gpio_clr_pin
121  * Description : Set output level of individual GPIO pin to logic 0..
122  *
123  ***************************************************************************/
125 {
126  gpioHandleiMXSDK_t *pinHandle = (gpioHandleiMXSDK_t *)aPinId;
127  pinHandle->pinStatus = PIN_CLR;
128  GPIO_PinWrite(pinHandle->base, pinHandle->pinNumber, PIN_CLR);
129 }
130 
131 /***********************************************************************
132  *
133  * Function Name : ksdk_gpio_toggle_pin
134  * Description : toggle the currrent output logic of individual GPIO pin.
135  *
136  ***************************************************************************/
138 {
139  gpioHandleiMXSDK_t *pinHandle = (gpioHandleiMXSDK_t *)aPinId;
140 
141  /* Check PIN Status and toggle */
142  if (PIN_CLR == pinHandle->pinStatus)
143  {
144  imxsdk_gpio_set_pin(aPinId);
145  }
146  else if (PIN_SET == pinHandle->pinStatus)
147  {
148  imxsdk_gpio_clr_pin(aPinId);
149  }
150 }
151 
152 /***********************************************************************
153  *
154  * Function Name : imxsdk_gpio_write_pin
155  * Description : Set output level of individual GPIO pin to desired value, ie 1 or 0.
156  *
157  ***************************************************************************/
158 void imxsdk_gpio_write_pin(pinID_t aPinId, uint8_t aValue)
159 {
160  gpioHandleiMXSDK_t *pinHandle = (gpioHandleiMXSDK_t *)aPinId;
161  GPIO_WritePinOutput(pinHandle->base, pinHandle->pinNumber, aValue);
162 }
163 
164 /***********************************************************************
165  *
166  * Function Name : imxsdk_gpio_read_pin
167  * Description : Read current input value of individual GPIO pin.
168  *
169  ***************************************************************************/
171 {
172  gpioHandleiMXSDK_t *pinHandle = (gpioHandleiMXSDK_t *)aPinId;
173  return GPIO_ReadPinInput(pinHandle->base, pinHandle->pinNumber);
174 }
175 
176 /***********************************************************************
177  *
178  * Function Name : imxsdk_gpio_handle_interrupt
179  * Description : handle the gpio interrupt in a pin.
180  *
181  ***************************************************************************/
182 void imxsdk_gpio_handle_interrupt(GPIO_Type *apBase, port_number_t gpioPortNumber)
183 {
184  uint32_t isfr = GPIO_GetPinsInterruptFlags(apBase);
185 
186  /* Parse through all the pending interrupt for a PORT */
187  for (uint8_t i = 0; i < GPIO_NUMBER_OF_PIN; i++)
188  {
189  if (isfr & (1 << i))
190  {
191  gpio_isr_handler_t handle = isrObj[gpioPortNumber][i].isrHandle;
192  if (handle == NULL)
193  {
194  continue;
195  }
196  /* Call user defined handler */
197  handle(isrObj[gpioPortNumber][i].pUserData);
198  GPIO_ClearPinsInterruptFlags(apBase, (1 << i));
199  }
200  }
201 }
202 
206 };
gpio_pin_config_t pinConfig
Definition: gpio_driver.h:68
#define GPIO_NUMBER_OF_PIN
Definition: gpio_driver.c:46
#define PIN_SET
Definition: gpio_driver.h:47
#define PIN_CLR
Definition: gpio_driver.h:48
enum port_number port_number_t
GPIO PORT NAMES.
void(* gpio_isr_handler_t)(void *apUserData)
Definition: Driver_GPIO.h:60
void imxsdk_gpio_pin_init(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: gpio_driver.c:79
#define GPIO_DRV_VERSION
Definition: gpio_driver.c:44
void imxsdk_gpio_toggle_pin(pinID_t aPinId)
Definition: gpio_driver.c:137
The GPIO Pin Configuration i.MX SDK.
Definition: gpio_driver.h:66
ARM_DRIVER_VERSION GENERIC_DRIVER_VERSION
Definition: Driver_GPIO.h:58
void imxsdk_gpio_set_pin(pinID_t aPinId)
Definition: gpio_driver.c:111
The GPIO pin handle for i.MX SDK.
Definition: gpio_driver.h:75
uint32_t imxsdk_gpio_read_pin(pinID_t aPinId)
Definition: gpio_driver.c:170
enum gpio_direction_en gpio_direction_t
void imxsdk_gpio_handle_interrupt(GPIO_Type *apBase, port_number_t gpioPortNumber)
Definition: gpio_driver.c:182
The gpio isr object.
Definition: gpio_driver.h:88
uint8_t pinStatus
Definition: gpio_driver.h:82
void * pinID_t
GPIO Driver direction.
Definition: Driver_GPIO.h:56
#define GPIO_API_VERSION
Definition: Driver_GPIO.h:40
GENERIC_DRIVER_VERSION imxsdk_gpio_get_version(void)
Definition: gpio_driver.c:68
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
void * pUserData
Definition: gpio_driver.h:90
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
gpio_isr_handler_t isrHandle
Definition: gpio_driver.h:91
uint32_t pinNumber
Definition: gpio_driver.h:78
port_number_t portNumber
Definition: gpio_driver.h:81
void imxsdk_gpio_write_pin(pinID_t aPinId, uint8_t aValue)
Definition: gpio_driver.c:158
GPIO_Type * base
Definition: gpio_driver.h:77
IRQn_Type irq
Definition: gpio_driver.h:80
void imxsdk_gpio_clr_pin(pinID_t aPinId)
Definition: gpio_driver.c:124