GPIO.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2022, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  * @file GPIO.h
34  *
35  * @brief General Purpose I/O driver interface.
36  *
37  * The GPIO header file should be included in an application as follows:
38  * @code
39  * #include <ti/drivers/GPIO.h>
40  * @endcode
41  *
42  * # Overview #
43  * The GPIO module allows you to manage General Purpose I/O pins via simple
44  * and portable APIs. GPIO pin behavior is usually configured statically,
45  * but can also be configured or reconfigured at runtime.
46  *
47  * Because of its simplicity, the GPIO driver does not follow the model of
48  * other TI-RTOS drivers in which a driver application interface has
49  * separate device-specific implementations. This difference is most
50  * apparent in the GPIOxxx_Config structure, which does not require you to
51  * specify a particular function table or object.
52  *
53  * # Usage #
54  * This section provides a basic \ref ti_drivers_GPIO_Synopsis
55  * "usage summary" and a set of \ref ti_drivers_GPIO_Examples "examples"
56  * in the form of commented code fragments. Detailed descriptions of the
57  * GPIO APIs and their effect are provided in subsequent sections.
58  *
59  * @anchor ti_drivers_GPIO_Synopsis
60  * ### Synopsis #
61  * @anchor ti_drivers_GPIO_Synopsis_Code
62  * @code
63  * // Import GPIO Driver definitions
64  * #include <ti/drivers/GPIO.h>
65  *
66  * // Define names for GPIO pin indexes
67  * #define BUTTON 0
68  * #define LED 1
69  *
70  * // One-time init of GPIO driver
71  * GPIO_init();
72  *
73  * // Read GPIO pin
74  * unsigned int state = GPIO_read(BUTTON);
75  *
76  * // Write to GPIO pin
77  * GPIO_write(LED, state);
78  * @endcode
79  *
80  * @anchor ti_drivers_GPIO_Examples
81  * ### Examples #
82  * * @ref ti_drivers_GPIO_Example_callback "Creating an input callback"
83  * * @ref ti_drivers_GPIO_Example_reconfigure "Runtime pin configuration"
84  *
85  * @anchor ti_drivers_GPIO_Example_callback
86  * **Creating an input callback**: The following example demonstrates how
87  * to configure a GPIO pin to generate an interrupt and how to toggle an
88  * an LED on and off within the registered interrupt callback function. Pin
89  * configuration is handled within Sysconfig for this example.
90  * @code
91  * // Driver header file
92  * #include <ti/drivers/GPIO.h>
93  *
94  * // TI Drivers Configuration
95  * #include "ti_drivers_config.h"
96  *
97  * // GPIO button call back function
98  * void gpioButton0Fxn(uint_least8_t index);
99  *
100  * main()
101  * {
102  * // Turn on user LED
103  * GPIO_write(CONFIG_GPIO_LED0, CONFIG_GPIO_LED_ON);
104  *
105  * // install Button callback and enable interrupts
106  * GPIO_setCallback(CONFIG_GPIO_BUTTON0, gpioButton0Fxn);
107  * GPIO_enableInt(CONFIG_GPIO_BUTTON0);
108  * }
109  *
110  * //
111  * // ======== gpioButton0Fxn ========
112  * // Callback function for the GPIO interrupt on CONFIG_GPIO_BUTTON0
113  * //
114  * // Note: index is the GPIO id for the button which is not used here
115  * //
116  * void gpioButton0Fxn(uint_least8_t index)
117  * {
118  * // Toggle the LED
119  * GPIO_toggle(CONFIG_GPIO_LED0);
120  * }
121  * @endcode
122  *
123  * @anchor ti_drivers_GPIO_Example_reconfigure
124  * **Runtime pin configuration**: The following example demonstrates how
125  * to (re)configure GPIO pins.
126  * @code
127  * // Driver header file
128  * #include <ti/drivers/GPIO.h>
129  *
130  * // TI Driver configuration
131  * #include "ti_drivers_config.h"
132  *
133  * void main()
134  * {
135  * // One-time init of GPIO driver
136  * GPIO_init();
137  *
138  * // Configure a button pin as input and configure its interrupt
139  * // Passing INT_ENABLE means you do not need to also call GPIO_enableInt()
140  * GPIO_setConfig(
141  * CONFIG_GPIO_BUTTON0,
142  * GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING | GPIO_CFG_INT_ENABLE
143  * );
144  *
145  * // Configure an LED output pin
146  * GPIO_setConfig(CONFIG_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
147  * }
148  * @endcode
149  *
150  * ### GPIO Driver Configuration #
151  *
152  * In order to use the GPIO APIs, the application is required
153  * to provide 3 structures in the ti_drivers_config.c file:
154  * 1. An array of @ref GPIO_PinConfig elements that defines the
155  * initial configuration of each pin on the device. A pin is then
156  * referenced in the application by its corresponding index in this
157  * array. The pin type (that is, INPUT/OUTPUT), its initial state (that is
158  * OUTPUT_HIGH or LOW), interrupt behavior (RISING/FALLING edge, etc.), and
159  * device specific pin identification are configured in each element
160  * of this array (see @ref GPIO_PinConfigSettings).
161  * Below is a device specific example of the GPIO_PinConfig array:
162  * @code
163  * //
164  * // Array of Pin configurations
165  * //
166  * GPIO_PinConfig gpioPinConfigs[31] = {
167  * GPIO_CFG_INPUT, // DIO_0
168  * GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_NONE, // CONFIG_GPIO_LP19
169  * GPIO_CFG_INPUT, // DIO_2
170  * GPIO_CFG_INPUT, // DIO_3
171  * ...
172  * };
173  * @endcode
174  *
175  * 2. An array of @ref GPIO_CallbackFxn elements that is used to store
176  * callback function pointers for GPIO pins. The indexes for these array
177  * elements correspond to the pins defined in the GPIO_pinConfig array.
178  * These function pointers can be defined statically by referencing the
179  * callback function name in the array element, or dynamically, by setting
180  * the array element to NULL and using GPIO_setCallback() at runtime to
181  * plug the callback entry. The callback function syntax should match the
182  * following:
183  * @code
184  * void (*GPIO_CallbackFxn)(uint_least8_t index);
185  * @endcode
186  * The index parameter is the same index that was passed to
187  * GPIO_setCallback(). This allows the same callback function to be used
188  * for multiple GPIO interrupts, by using the index to identify the GPIO
189  * that caused the interrupt.
190  * @remark Callback functions are called in the context of an interrupt
191  * service routine and should be designed accordingly.
192  *
193  * When an interrupt is triggered, the interrupt status of all
194  * (interrupt enabled) pins on a port will be read, cleared, and the
195  * respective callbacks will be executed. Callbacks will be called in order
196  * from least significant bit to most significant bit.
197  * Below is an MSP432 device specific example of the GPIO_CallbackFxn array:
198  * @code
199  * //
200  * // Array of callback function pointers
201  * //
202  * GPIO_CallbackFxn gpioCallbackFunctions[31] = {
203  * NULL, // DIO_0
204  * NULL, // DIO_1
205  * myGpioCallback, // CONFIG_GPIO_LP19
206  * NULL, // DIO_3
207  * ...
208  * };
209  * @endcode
210  *
211  * 3. A device specific GPIO_Config structure that tells the GPIO
212  * driver where the two aforementioned arrays are and the number of elements
213  * in each. The interrupt priority of all pins configured to generate
214  * interrupts is also specified here. Values for the interrupt priority are
215  * device-specific. You should be well-acquainted with the interrupt
216  * controller used in your device before setting this parameter to a
217  * non-default value. The sentinel value of (~0) (the default value) is
218  * used to indicate that the lowest possible priority should be used.
219  * Below is a device specific example of a GPIO_Config structure:
220  * @code
221  * //
222  * // ======== GPIO_config ========
223  * //
224  * const GPIO_Config GPIO_config = {
225  * .configs = (GPIO_PinConfig *)gpioPinConfigs,
226  * .callbacks = (GPIO_CallbackFxn *)gpioCallbackFunctions,
227  * .intPriority = (~0)
228  * };
229  * @endcode
230  *
231  * ### Initializing the GPIO Driver #
232  *
233  * GPIO_init() must be called before any other GPIO APIs. This function
234  * configures each GPIO pin in the user-provided @ref GPIO_PinConfig
235  * array according to the defined settings. The user can also reconfigure
236  * a pin dynamically after GPIO_init() is called by using the
237  * GPIO_setConfig(), and GPIO_setCallback() APIs.
238  *
239  * GPIO_init() is called from Board_init() by default. Calling GPIO_init()
240  * multiple times is safe.
241  *
242  * # Implementation #
243  *
244  * Unlike most other TI-RTOS drivers, there is no notion of an instance
245  * 'handle' with the GPIO driver. This allows lightweight pin control with
246  * minimal runtime and memory overhead.
247  *
248  * GPIO pins are always referenced by device DIO index.
249  *
250  ******************************************************************************
251  */
252 
253 #ifndef ti_drivers_GPIO__include
254 #define ti_drivers_GPIO__include
255 
256 #include <stdint.h>
257 
258 #include <ti/devices/DeviceFamily.h>
259 
260 /* The device-specific header is used to map GPIO_CFG_X_INTERNAL definitions
261  * directly to device-specific configuration values, allowing efficient runtime
262  * reconfiguration without the need for bit twiddling.
263  */
264 #if (DeviceFamily_PARENT == DeviceFamily_PARENT_CC13X0_CC26X0 \
265  || DeviceFamily_PARENT == DeviceFamily_PARENT_CC13X1_CC26X1 \
266  || DeviceFamily_PARENT == DeviceFamily_PARENT_CC13X2_CC26X2 \
267  || DeviceFamily_PARENT == DeviceFamily_PARENT_CC13X4_CC26X3_CC26X4)
268 #include <ti/drivers/gpio/GPIOCC26XX.h>
269 #elif (DeviceFamily_ID == DeviceFamily_ID_CC3220 || DeviceFamily_ID == DeviceFamily_ID_CC3200)
271 #elif (DeviceFamily_PARENT == DeviceFamily_PARENT_CC23X0)
272 #include <ti/drivers/gpio/GPIOCC23XX.h>
273 #endif
274 
275 /* Generic functions for converting pin indexes to and from masks. Internal use
276  * only. CLZ is an ARM instruction for `count leading zeroes`, so if multiple
277  * bits in the pinmask are set MASK_TO_PIN will only return the highest set
278  * bit. PIN_TO_MASK is used for setting registers.
279  */
280 #if defined(__IAR_SYSTEMS_ICC__)
281 #include <intrinsics.h>
282 #define GPIO_MASK_TO_PIN(pinmask) (31 - __CLZ(pinmask))
283 #elif defined(__TI_COMPILER_VERSION__)
284 #include <arm_acle.h>
285 #define GPIO_MASK_TO_PIN(pinmask) (31 - __clz(pinmask))
286 #elif defined(__GNUC__) && !defined(__TI_COMPILER_VERSION__)
287 #include <arm_acle.h>
288 #define GPIO_MASK_TO_PIN(pinmask) (31 - __builtin_clz(pinmask))
289 #endif
290 
291 #define GPIO_PIN_TO_MASK(pin) (1 << (pin))
292 
293 #ifdef __cplusplus
294 extern "C" {
295 #endif
296 
308 #define GPIO_STATUS_SUCCESS (0)
309 
316 #define GPIO_STATUS_ERROR (-1)
317 
328 typedef uint32_t GPIO_PinConfig;
329 
338 #define GPIO_INVALID_INDEX 0xFF
339 
347 #define GPIO_CFG_OUTPUT GPIO_CFG_OUTPUT_INTERNAL | GPIO_CFG_PULL_NONE_INTERNAL
348 #define GPIO_CFG_OUT_STD GPIO_CFG_OUTPUT_INTERNAL | GPIO_CFG_PULL_NONE_INTERNAL
349 #define GPIO_CFG_OUT_OD_NOPULL GPIO_CFG_OUTPUT_OPEN_DRAIN_INTERNAL | GPIO_CFG_PULL_NONE_INTERNAL
350 #define GPIO_CFG_OUT_OD_PU GPIO_CFG_OUTPUT_OPEN_DRAIN_INTERNAL | GPIO_CFG_PULL_UP_INTERNAL
351 #define GPIO_CFG_OUT_OD_PD GPIO_CFG_OUTPUT_OPEN_DRAIN_INTERNAL | GPIO_CFG_PULL_DOWN_INTERNAL
353 #define GPIO_CFG_OUT_STR_LOW GPIO_CFG_DRVSTR_LOW_INTERNAL
354 #define GPIO_CFG_OUT_STR_MED GPIO_CFG_DRVSTR_MED_INTERNAL
355 #define GPIO_CFG_OUT_STR_HIGH GPIO_CFG_DRVSTR_HIGH_INTERNAL
357 #define GPIO_CFG_OUT_HIGH GPIO_CFG_OUTPUT_DEFAULT_HIGH_INTERNAL
358 #define GPIO_CFG_OUT_LOW GPIO_CFG_OUTPUT_DEFAULT_LOW_INTERNAL
364 #define GPIO_CFG_INPUT GPIO_CFG_INPUT_INTERNAL | GPIO_CFG_PULL_NONE_INTERNAL
365 #define GPIO_CFG_IN_NOPULL GPIO_CFG_INPUT_INTERNAL | GPIO_CFG_PULL_NONE_INTERNAL
366 #define GPIO_CFG_IN_PU GPIO_CFG_INPUT_INTERNAL | GPIO_CFG_PULL_UP_INTERNAL
367 #define GPIO_CFG_IN_PD GPIO_CFG_INPUT_INTERNAL | GPIO_CFG_PULL_DOWN_INTERNAL
374 #define GPIO_CFG_NO_DIR GPIO_CFG_NO_DIR_INTERNAL | GPIO_CFG_PULL_NONE_INTERNAL
375 
380 #define GPIO_CFG_INVERT_OFF GPIO_CFG_INVERT_OFF_INTERNAL
381 #define GPIO_CFG_INVERT_ON GPIO_CFG_INVERT_ON_INTERNAL
387 #define GPIO_CFG_HYSTERESIS_OFF GPIO_CFG_HYSTERESIS_OFF_INTERNAL
388 #define GPIO_CFG_HYSTERESIS_ON GPIO_CFG_HYSTERESIS_ON_INTERNAL
394 #define GPIO_CFG_SLEW_NORMAL GPIO_CFG_SLEW_NORMAL_INTERNAL
395 #define GPIO_CFG_SLEW_REDUCED GPIO_CFG_SLEW_REDUCED_INTERNAL
401 #define GPIO_CFG_IN_INT_NONE GPIO_CFG_INT_NONE_INTERNAL
402 #define GPIO_CFG_IN_INT_FALLING GPIO_CFG_INT_FALLING_INTERNAL
403 #define GPIO_CFG_IN_INT_RISING GPIO_CFG_INT_RISING_INTERNAL
404 #define GPIO_CFG_IN_INT_BOTH_EDGES GPIO_CFG_INT_BOTH_EDGES_INTERNAL
405 #define GPIO_CFG_IN_INT_LOW GPIO_CFG_INT_LOW_INTERNAL
406 #define GPIO_CFG_IN_INT_HIGH GPIO_CFG_INT_HIGH_INTERNAL
408 #define GPIO_CFG_INT_DISABLE GPIO_CFG_INT_DISABLE_INTERNAL
409 #define GPIO_CFG_INT_ENABLE GPIO_CFG_INT_ENABLE_INTERNAL
417 #define GPIO_MUX_GPIO GPIO_MUX_GPIO_INTERNAL
430 typedef void (*GPIO_CallbackFxn)(uint_least8_t index);
431 
444 typedef struct {
445  GPIO_PinConfig* configs;
447  void** userArgs;
448  uint32_t intPriority;
449 } GPIO_Config;
450 
461 extern void GPIO_clearInt(uint_least8_t index);
462 
470 extern void GPIO_disableInt(uint_least8_t index);
471 
485 extern void GPIO_enableInt(uint_least8_t index);
486 
497 extern void GPIO_init(void);
498 
509 extern uint_fast8_t GPIO_read(uint_least8_t index);
510 
516 extern void GPIO_toggle(uint_least8_t index);
517 
524 extern void GPIO_write(uint_least8_t index, unsigned int value);
525 
547 extern void GPIO_setCallback(uint_least8_t index, GPIO_CallbackFxn callback);
548 
556 extern GPIO_CallbackFxn GPIO_getCallback(uint_least8_t index);
557 
573 extern int_fast16_t GPIO_setConfig(uint_least8_t index, GPIO_PinConfig pinConfig);
574 
589 extern void GPIO_setInterruptConfig(uint_least8_t index, GPIO_PinConfig config);
590 
604 extern void GPIO_getConfig(uint_least8_t index, GPIO_PinConfig *pinConfig);
605 
617 extern void GPIO_resetConfig(uint_least8_t index);
618 
633 extern void GPIO_setMux(uint_least8_t index, uint32_t mux);
634 
644 extern uint32_t GPIO_getMux(uint_least8_t index);
645 
655 void GPIO_setUserArg(uint_least8_t index, void* arg);
656 
664 void* GPIO_getUserArg(uint_least8_t index);
665 
666 #ifdef __cplusplus
667 }
668 #endif
669 
670 #endif /* ti_drivers_GPIO__include */
uint32_t intPriority
Definition: GPIO.h:448
void GPIO_write(uint_least8_t index, unsigned int value)
Writes the value to a GPIO pin.
GPIO_CallbackFxn * callbacks
Definition: GPIO.h:446
uint32_t GPIO_getMux(uint_least8_t index)
Get the current mux for a gpio pin.
GPIO driver configuration structure.
Definition: GPIO.h:444
void GPIO_init(void)
Initializes the GPIO module.
GPIO driver implementation for CC32xx devices.
void GPIO_resetConfig(uint_least8_t index)
Resets the configuration for a gpio pin to the default value.
void GPIO_setMux(uint_least8_t index, uint32_t mux)
Sets the mux for a gpio pin.
GPIO_PinConfig * configs
Definition: GPIO.h:445
void GPIO_clearInt(uint_least8_t index)
Clear a GPIO pin interrupt flag.
void GPIO_toggle(uint_least8_t index)
Toggles the current state of a GPIO.
void GPIO_enableInt(uint_least8_t index)
Enable a GPIO pin interrupt.
void * GPIO_getUserArg(uint_least8_t index)
Get the user argument for a gpio pin.
GPIO_CallbackFxn GPIO_getCallback(uint_least8_t index)
Gets the callback associated with a GPIO pin.
void GPIO_setInterruptConfig(uint_least8_t index, GPIO_PinConfig config)
Configure the gpio pin.
uint32_t GPIO_PinConfig
GPIO pin configuration settings.
Definition: GPIO.h:328
void GPIO_getConfig(uint_least8_t index, GPIO_PinConfig *pinConfig)
Get the current configuration for a gpio pin.
void GPIO_setUserArg(uint_least8_t index, void *arg)
Set the user argument for a gpio pin.
int_fast16_t GPIO_setConfig(uint_least8_t index, GPIO_PinConfig pinConfig)
Configure the gpio pin.
void GPIO_setCallback(uint_least8_t index, GPIO_CallbackFxn callback)
Bind a callback function to a GPIO pin interrupt.
void ** userArgs
Definition: GPIO.h:447
uint_fast8_t GPIO_read(uint_least8_t index)
Reads the value of a GPIO pin.
void GPIO_disableInt(uint_least8_t index)
Disable a GPIO pin interrupt.
void(* GPIO_CallbackFxn)(uint_least8_t index)
GPIO callback function type.
Definition: GPIO.h:430
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale