MSPM0G1X0X_G3X0X TI-Driver Library  1.20.01.06
GPIO.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2023, 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  * @defgroup GPIO General Purpose Input Output (GPIO)
38  *
39  * The GPIO header file should be included in an application as follows:
40  * @code
41  * #include <ti/drivers/GPIO.h>
42  * @endcode
43  *
44  * @anchor ti_drivers_GPIO_Overview
45  * # Overview
46  * The GPIO module allows you to manage General Purpose I/O pins via simple
47  * and portable APIs. GPIO pin behavior is usually configured statically,
48  * but can also be configured or reconfigured at runtime.
49  *
50  * Because of its simplicity, the GPIO driver does not follow the model of
51  * other RTOS drivers in which a driver application interface has
52  * separate device-specific implementations. This difference is most
53  * apparent in the GPIOxxx_Config structure, which does not require you to
54  * specify a particular function table or object.
55  *
56  * # Usage #
57  * This section provides a basic \ref ti_drivers_GPIO_Synopsis
58  * "usage summary" and a set of \ref ti_drivers_GPIO_Examples "examples"
59  * in the form of commented code fragments. Detailed descriptions of the
60  * GPIO APIs and their effect are provided in subsequent sections.
61  *
62  * @anchor ti_drivers_GPIO_Synopsis
63  * ### Synopsis #
64  * @anchor ti_drivers_GPIO_Synopsis_Code
65  * @code
66  * // Import GPIO Driver definitions.
67  * #include <ti/drivers/GPIO.h>
68  *
69  * // Define names for GPIO pin indexes.
70  * #define BUTTON 0
71  * #define LED 1
72  *
73  * // One-time init of GPIO driver.
74  * GPIO_init();
75  *
76  * // Read GPIO pin
77  * unsigned int state = GPIO_read(BUTTON);
78  *
79  * // Write to GPIO pin
80  * GPIO_write(LED, state);
81  * @endcode
82  *
83  * @anchor ti_drivers_GPIO_Examples
84  * ### Examples #
85  * * @ref ti_drivers_GPIO_Example_callback "Creating an input callback"
86  * * @ref ti_drivers_GPIO_Example_reconfigure "Runtime pin configuration"
87  *
88  * @anchor ti_drivers_GPIO_Example_callback
89  * **Creating an input callback**: The following example demonstrates how
90  * to configure a GPIO pin to generate an interrupt and how to toggle an
91  * an LED on and off within the registered interrupt callback function.
92  * @code
93  * // Driver header file
94  * #include <ti/drivers/GPIO.h>
95  *
96  * // TI Drivers Configuration
97  * #include "ti_drivers_config.h"
98  * // Board file
99  * #include <ti/drivers/Board.h>
100  *
101  * // GPIO button call back function
102  * void gpioButton0Fxn(uint_least8_t index);
103  *
104  * main()
105  * {
106  * // One-time Board initialization.
107  * Board_init();
108  *
109  * // One-time init of GPIO driver.
110  * GPIO_init();
111  *
112  * // Turn on user LED.
113  * GPIO_write(CONFIG_GPIO_LED0, CONFIG_GPIO_LED_ON);
114  *
115  * // install Button callback.
116  * GPIO_setCallback(CONFIG_GPIO_BUTTON0, gpioButton0Fxn);
117  *
118  * // Enable interrupts.
119  * GPIO_enableInt(CONFIG_GPIO_BUTTON0);
120  * }
121  *
122  * //
123  * // ======== gpioButton0Fxn ========
124  * // Callback function for the GPIO interrupt on CONFIG_GPIO_BUTTON0
125  * //
126  * // Note: index is the GPIO id for the button which is not used here
127  * //
128  * void gpioButton0Fxn(uint_least8_t index)
129  * {
130  * // Toggle the LED.
131  * GPIO_toggle(CONFIG_GPIO_LED0);
132  * }
133  * @endcode
134  *
135  * @anchor ti_drivers_GPIO_Example_reconfigure
136  * **Runtime pin configuration**: The following example demonstrates how
137  * to (re)configure GPIO pins.
138  * @code
139  * // Driver header file.
140  * #include <ti/drivers/GPIO.h>
141  *
142  * // TI Driver configuration.
143  * #include "ti_drivers_config.h"
144  *
145  * #define LED CONFIG_GPIO_LED0
146  * #define BUTTON CONFIG_GPIO_BUTTON0
147  *
148  * void main()
149  * {
150  * // One-time init of GPIO driver.
151  * GPIO_init();
152  *
153  * // Configure a button input pin.
154  * GPIO_setConfig(BUTTON, GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING |
155  * BUTTON_IOMUX);
156  *
157  * // Configure an LED output pin.
158  * GPIO_setConfig(LED, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW | LED_IOMUX);
159  * }
160  * @endcode
161  *
162  * ### GPIO Driver Configuration #
163  *
164  * In order to use the GPIO APIs, the application is required
165  * to provide 3 structures in the ti_drivers_config.c file:
166  * 1. An array of @ref GPIO_PinConfig elements that defines the
167  * initial configuration of each pin used by the application. A
168  * pin is referenced in the application by its corresponding index in this
169  * array. The pin type (that is, INPUT/OUTPUT), its initial state (that is
170  * OUTPUT_HIGH or LOW), interrupt behavior (RISING/FALLING edge, etc.), and
171  * device specific pin identification are configured in each element
172  * of this array (see @ref GPIO_PinConfigSettings).
173  * Below is a device specific example of the GPIO_PinConfig array:
174  * @code
175  * //
176  * // Array of Pin configurations.
177  * //
178  * GPIO_PinConfig gpioPinConfigs[31] = {
179  * GPIO_CFG_INPUT | PA0_IOMUX, // PA0
180  * GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_NONE | PA1_IOMUX, // PA1
181  * GPIO_CFG_INPUT | PA2_IOMUX, // PA2
182  * GPIO_CFG_INPUT | PA3_IOMUX, // PA3
183  * ...
184  * };
185  * @endcode
186  *
187  * 2. An array of @ref GPIO_CallbackFxn elements that is used to store
188  * callback function pointers for GPIO pins configured with interrupts.
189  * The indexes for these array elements correspond to the pins defined
190  * in the GPIO_pinConfig array. These function pointers can be defined
191  * statically by referencing the callback function name in the array
192  * element, or dynamically, by setting the array element to NULL and using
193  * GPIO_setCallback() at runtime to plug the callback entry.
194  * Pins not used for interrupts can be omitted from the callback array to
195  * reduce memory usage (if they are placed at the end of GPIO_pinConfig
196  * array). The callback function syntax should match the following:
197  * @code
198  * void (*GPIO_CallbackFxn)(uint_least8_t index);
199  * @endcode
200  * The index parameter is the same index that was passed to
201  * GPIO_setCallback(). This allows the same callback function to be used
202  * for multiple GPIO interrupts, by using the index to identify the GPIO
203  * that caused the interrupt.
204  * @remark Callback functions are called in the context of an interrupt
205  * service routine and should be designed accordingly.
206  *
207  * When an interrupt is triggered, the interrupt status of all
208  * (interrupt enabled) pins on a port will be read, cleared, and the
209  * respective callbacks will be executed. Callbacks will be called in order
210  * from least significant bit to most significant bit.
211  * Below is a device specific example of the GPIO_CallbackFxn array:
212  * @code
213  * //
214  * // Array of callback function pointers.
215  * //
216  * GPIO_CallbackFxn gpioCallbackFunctions[31] = {
217  * NULL, // PA0
218  * NULL, // PA1
219  * myGpioCallback, // PA2
220  * NULL, // PA3
221  * ...
222  * };
223  * @endcode
224  *
225  * 3. A device specific GPIO_Config structure that tells the GPIO
226  * driver where the two aforementioned arrays are. The interrupt priority of
227  * all pins configured to generate interrupts is also specified here.
228  * Values for the interrupt priority are device-specific. You should be
229  * well-acquainted with the interrupt controller used in your device before
230  * setting this parameter to a non-default value. The sentinel value of
231  * (~0) (the default value) is used to indicate that the lowest possible
232  * priority should be used. Below is a device specific example of a
233  * GPIO_Config structure:
234  * @code
235  * //
236  * // ======== GPIO_config ========
237  * //
238  * const GPIO_Config GPIO_config = {
239  * .configs = (GPIO_PinConfig *)gpioPinConfigs,
240  * .callbacks = (GPIO_CallbackFxn *)gpioCallbackFunctions,
241  * .intPriority = (~0)
242  * };
243  * @endcode
244  *
245  * ### Initializing the GPIO Driver #
246  *
247  * GPIO_init() must be called before any other GPIO APIs. This function
248  * configures each GPIO pin in the user-provided @ref GPIO_PinConfig
249  * array according to the defined settings. The user can also reconfigure
250  * a pin dynamically after GPIO_init() is called by using the
251  * GPIO_setConfig(), and GPIO_setCallback() APIs.
252  *
253  * # Implementation #
254  *
255  * Unlike most other RTOS drivers, the GPIO driver has no generic function
256  * table with pointers to device-specific API implementations. All the generic
257  * GPIO APIs are implemented by the device-specific GPIO driver module.
258  * Additionally, there is no notion of an instance 'handle' with the GPIO
259  * driver.
260  *
261  * GPIO pins are referenced by their numeric index in the GPIO_PinConfig
262  * array. This design approach was used to enhance runtime and memory
263  * efficiency.
264  *
265  ******************************************************************************
266  */
270 #ifndef ti_drivers_GPIO__include
271 #define ti_drivers_GPIO__include
272 
273 #include <stdint.h>
274 
276 
277 /* clang-format off */
278 
279 /* Generic functions for converting pin indexes to and from masks. Internal use
280  * only. CLZ is an ARM instruction for `count leading zeroes`, so if multiple
281  * bits in the pinmask are set MASK_TO_PIN will only return the highest set
282  * bit. PIN_TO_MASK is used for setting registers.
283  */
284 #if defined(__IAR_SYSTEMS_ICC__)
285  #include <intrinsics.h>
286  #define GPIO_MASK_TO_PIN(pinmask) (31 - __CLZ(pinmask))
287 #elif defined(__TI_COMPILER_VERSION__)
288  #include <arm_acle.h>
289  #define GPIO_MASK_TO_PIN(pinmask) (31 - __clz(pinmask))
290 #elif defined(__GNUC__) && !defined(__TI_COMPILER_VERSION__)
291  #include <arm_acle.h>
292  #define GPIO_MASK_TO_PIN(pinmask) (31 - __builtin_clz(pinmask))
293 #endif
294 
298 #define GPIO_PIN_TO_MASK(pin) (1 << (pin))
299 
300 #ifdef __cplusplus
301 extern "C" {
302 #endif
303 
324 #define GPIO_STATUS_RESERVED (-32)
325 
333 #define GPIO_STATUS_SUCCESS (0)
334 
341 #define GPIO_STATUS_ERROR (-1)
342 
359 typedef uint32_t GPIO_PinConfig;
360 
369 #define GPIO_INVALID_INDEX 0xFF
370 
375 #define GPIO_CFG_IO_MASK (0x07ff0000)
376 #define GPIO_CFG_IO_LSB (16)
377 #define GPIO_CFG_OUT_TYPE_MASK (0x00060000)
378 #define GPIO_CFG_OUT_TYPE_LSB (17)
379 #define GPIO_CFG_IN_TYPE_MASK (0x00060000)
380 #define GPIO_CFG_IN_TYPE_LSB (17)
381 #define GPIO_CFG_OUT_BIT (19)
382 #define GPIO_CFG_OUT_STRENGTH_MASK (0x00300000)
383 #define GPIO_CFG_OUT_STRENGTH_LSB (20)
384 
385 #define GPIO_CFG_INVERSION_BIT (22)
386 #define GPIO_CFG_INVERSION_MASK (0x00400000)
387 
388 #define GPIO_CFG_HYSTERESIS_BIT (23)
389 #define GPIO_CFG_HYSTERESIS_MASK (0x00800000)
390 
391 #define GPIO_CFG_HIZ_BIT (24)
392 #define GPIO_CFG_HIZ_MASK (0x01000000)
393 
394 #define GPIO_CFG_WAKEUP_LSB (25)
395 #define GPIO_CFG_WAKEUP_MASK (0x06000000)
396 
397 #define GPIO_CFG_INT_LSB (27)
398 #define GPIO_CFG_INT_MASK (0x38000000)
399 
400 /* Bit 30 of GPIO_PinConfig is unused */
411 #define GPIO_CFG_OUTPUT (((uint32_t) 0) << GPIO_CFG_IO_LSB)
412 
413 #define GPIO_CFG_OUT_STD (((uint32_t) 0) << GPIO_CFG_IO_LSB)
414 
415 #define GPIO_CFG_OUT_OD_NOPULL (((uint32_t) 2) << GPIO_CFG_IO_LSB)
416 
417 #define GPIO_CFG_OUT_OD_PU (((uint32_t) 4) << GPIO_CFG_IO_LSB)
418 
419 #define GPIO_CFG_OUT_OD_PD (((uint32_t) 6) << GPIO_CFG_IO_LSB)
420 
422 #define GPIO_CFG_OUT_STR_LOW (((uint32_t) 0) << GPIO_CFG_OUT_STRENGTH_LSB)
423 
424 #define GPIO_CFG_OUT_STR_MED GPIO_CFG_DRVSTR_MED_INTERNAL
425 
426 #define GPIO_CFG_OUT_STR_HIGH (((uint32_t) 2) << GPIO_CFG_OUT_STRENGTH_LSB)
427 
429 #define GPIO_CFG_OUT_HIGH (((uint32_t) 1) << GPIO_CFG_OUT_BIT)
430 
431 #define GPIO_CFG_OUT_LOW (((uint32_t) 0) << GPIO_CFG_OUT_BIT)
432 
438 #define GPIO_CFG_INPUT (((uint32_t) 1) << GPIO_CFG_IO_LSB)
439 
440 #define GPIO_CFG_IN_NOPULL (((uint32_t) 1) << GPIO_CFG_IO_LSB)
441 
442 #define GPIO_CFG_IN_PU (((uint32_t) 3) << GPIO_CFG_IO_LSB)
443 
444 #define GPIO_CFG_IN_PD (((uint32_t) 5) << GPIO_CFG_IO_LSB)
445 
451 #define GPIO_CFG_INVERT_OFF (((uint32_t) 0) << GPIO_CFG_INVERSION_BIT)
452 
453 #define GPIO_CFG_INVERT_ON (((uint32_t) 1) << GPIO_CFG_INVERSION_BIT)
454 
460 #define GPIO_CFG_HYSTERESIS_OFF (((uint32_t) 0) << GPIO_CFG_HYSTERESIS_BIT)
461 
462 #define GPIO_CFG_HYSTERESIS_ON (((uint32_t) 1) << GPIO_CFG_HYSTERESIS_BIT)
463 
469 #define GPIO_CFG_HIZ_OFF (((uint32_t) 0) << GPIO_CFG_HIZ_BIT)
470 
471 #define GPIO_CFG_HIZ_ON (((uint32_t) 1) << GPIO_CFG_HIZ_BIT)
472 
478 #define GPIO_CFG_IN_INT_NONE (((uint32_t) 0) << GPIO_CFG_INT_LSB)
479 
480 #define GPIO_CFG_IN_INT_RISING (((uint32_t) 1) << GPIO_CFG_INT_LSB)
481 
482 #define GPIO_CFG_IN_INT_FALLING (((uint32_t) 2) << GPIO_CFG_INT_LSB)
483 
484 #define GPIO_CFG_IN_INT_BOTH_EDGES (((uint32_t) 3) << GPIO_CFG_INT_LSB)
485 
486 #define GPIO_CFG_IN_INT_LOW (GPIO_CFG_INT_LOW_INTERNAL)
487 
488 #define GPIO_CFG_IN_INT_HIGH (GPIO_CFG_INT_HIGH_INTERNAL)
489 
495 #define GPIO_CFG_WAKEUP_DISABLE (((uint32_t) 0) << GPIO_CFG_WAKEUP_LSB)
496 
497 #define GPIO_CFG_WAKEUP_ON_0 (((uint32_t) 1) << GPIO_CFG_WAKEUP_LSB)
498 
499 #define GPIO_CFG_WAKEUP_ON_1 (((uint32_t) 2) << GPIO_CFG_WAKEUP_LSB)
500 
510 #define GPIO_DO_NOT_CONFIG (0x80000000)
511 
519 #define GPIO_MUX_GPIO (0x00000001)
520 
523 /* clang-format on */
524 
534 typedef void (*GPIO_CallbackFxn)(uint_least8_t index);
535 
548 typedef struct {
549  GPIO_PinConfig *configs;
552  void **userArgs;
553  uint32_t intPriority;
554 } GPIO_Config;
555 
566 extern void GPIO_clearInt(uint_least8_t index);
567 
575 extern void GPIO_disableInt(uint_least8_t index);
576 
590 extern void GPIO_enableInt(uint_least8_t index);
591 
602 extern void GPIO_init();
603 
616 extern uint_fast8_t GPIO_read(uint_least8_t index);
617 
623 extern void GPIO_toggle(uint_least8_t index);
624 
631 extern void GPIO_write(uint_least8_t index, unsigned int value);
632 
654 extern void GPIO_setCallback(uint_least8_t index, GPIO_CallbackFxn callback);
655 
663 extern GPIO_CallbackFxn GPIO_getCallback(uint_least8_t index);
664 
680 extern int_fast16_t GPIO_setConfig(
681  uint_least8_t index, GPIO_PinConfig pinConfig);
682 
697 extern void GPIO_setInterruptConfig(
698  uint_least8_t index, GPIO_PinConfig config);
699 
711 extern void GPIO_getConfig(uint_least8_t index, GPIO_PinConfig *pinConfig);
712 
722 extern void GPIO_resetConfig(uint_least8_t index);
723 
733 extern uint32_t GPIO_getMux(uint_least8_t index);
734 
753 extern int_fast16_t GPIO_setConfigAndMux(
754  uint_least8_t index, GPIO_PinConfig pinConfig, uint32_t mux);
755 
765 void GPIO_setUserArg(uint_least8_t index, void *arg);
766 
774 void *GPIO_getUserArg(uint_least8_t index);
775 
776 #ifdef __cplusplus
777 }
778 #endif
779 
780 #endif /* ti_drivers_GPIO__include */
781 
uint32_t intPriority
Definition: GPIO.h:553
GPIO_CallbackFxn * callbacks
Definition: GPIO.h:551
void GPIO_enableInt(uint_least8_t index)
Enable a GPIO pin interrupt.
void GPIO_setCallback(uint_least8_t index, GPIO_CallbackFxn callback)
Bind a callback function to a GPIO pin interrupt.
GPIO driver configuration structure.
Definition: GPIO.h:548
GPIO_CallbackFxn GPIO_getCallback(uint_least8_t index)
Gets the callback associated with a GPIO pin.
void * GPIO_getUserArg(uint_least8_t index)
Get the user argument for a gpio pin.
GPIO_PinConfig * configs
Definition: GPIO.h:549
void GPIO_toggle(uint_least8_t index)
Toggles the current state of a GPIO.
void(* GPIO_CallbackFxn)(uint_least8_t index)
GPIO callback function type.
Definition: GPIO.h:534
void GPIO_setInterruptConfig(uint_least8_t index, GPIO_PinConfig config)
Configure the gpio pin.
GPIO driver implementation for MSPM0 devices.
uint_fast8_t GPIO_read(uint_least8_t index)
Reads the value of a GPIO pin.
int_fast16_t GPIO_setConfigAndMux(uint_least8_t index, GPIO_PinConfig pinConfig, uint32_t mux)
Configure the gpio pin&#39;s config and mux in a single write.
int_fast16_t GPIO_setConfig(uint_least8_t index, GPIO_PinConfig pinConfig)
Configure the gpio pin.
uint32_t GPIO_getMux(uint_least8_t index)
Get the current mux for a gpio pin.
void GPIO_write(uint_least8_t index, unsigned int value)
Writes the value to a GPIO pin.
void ** userArgs
Definition: GPIO.h:552
void GPIO_resetConfig(uint_least8_t index)
Resets the configuration for a gpio pin to the default value.
void GPIO_setUserArg(uint_least8_t index, void *arg)
Set the user argument for a gpio pin.
void GPIO_clearInt(uint_least8_t index)
Clear a GPIO pin interrupt flag.
void GPIO_disableInt(uint_least8_t index)
Disable a GPIO pin interrupt.
void GPIO_getConfig(uint_least8_t index, GPIO_PinConfig *pinConfig)
Get the current configuration for a gpio pin.
uint32_t GPIO_PinConfig
GPIO pin configuration settings.
Definition: GPIO.h:359
void GPIO_init()
Initializes the GPIO module.
© Copyright 1995-2023, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale