GPIO.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2018, 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.
89  * @code
90  * // Driver header file
91  * #include <ti/drivers/GPIO.h>
92  *
93  * // Portable user-defined board-level symbols
94  * #include "Board.h"
95  *
96  * // GPIO button call back function
97  * void gpioButton0Fxn(uint_least8_t index);
98  *
99  * main()
100  * {
101  * // One-time TI-DRIVERS Board initialization
102  * Board_init();
103  *
104  * // One-time init of GPIO driver
105  * GPIO_init();
106  *
107  * // Turn on user LED
108  * GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
109  *
110  * // install Button callback
111  * GPIO_setCallback(Board_GPIO_BUTTON0, gpioButton0Fxn);
112  *
113  * // Enable interrupts
114  * GPIO_enableInt(Board_GPIO_BUTTON0);
115  * }
116  *
117  * //
118  * // ======== gpioButton0Fxn ========
119  * // Callback function for the GPIO interrupt on Board_GPIO_BUTTON0
120  * //
121  * // Note: index is the GPIO id for the button which is not used here
122  * //
123  * void gpioButton0Fxn(uint_least8_t index)
124  * {
125  * // Toggle the LED
126  * GPIO_toggle(Board_GPIO_LED0);
127  * }
128  * @endcode
129  *
130  * @anchor ti_drivers_GPIO_Example_reconfigure
131  * **Runtime pin configuration**: The following example demonstrates how
132  * to (re)configure GPIO pins.
133  * @code
134  * // Driver header file
135  * #include <ti/drivers/GPIO.h>
136  *
137  * // Portable board-level symbols
138  * #include "Board.h"
139  *
140  * #define LED Board_GPIO_LED0
141  * #define BUTTON Board_GPIO_BUTTON0
142  *
143  * void main()
144  * {
145  * // One-time init of GPIO driver
146  * GPIO_init();
147  *
148  * // Configure a button input pin
149  * GPIO_setConfig(BUTTON, GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING);
150  *
151  * // Configure an LED output pin
152  * GPIO_setConfig(LED, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
153  * }
154  * @endcode
155  *
156  * ### GPIO Driver Configuration #
157  *
158  * In order to use the GPIO APIs, the application is required
159  * to provide 3 structures in the Board.c file:
160  * 1. An array of @ref GPIO_PinConfig elements that defines the
161  * initial configuration of each pin used by the application. A
162  * pin is referenced in the application by its corresponding index in this
163  * array. The pin type (that is, INPUT/OUTPUT), its initial state (that is
164  * OUTPUT_HIGH or LOW), interrupt behavior (RISING/FALLING edge, etc.), and
165  * device specific pin identification are configured in each element
166  * of this array (see @ref GPIO_PinConfigSettings).
167  * Below is an MSP432 device specific example of the GPIO_PinConfig array:
168  * @code
169  * //
170  * // Array of Pin configurations
171  * // NOTE: The order of the pin configurations must coincide with what was
172  * // defined in MSP_EXP432P401R.h
173  * // NOTE: Pins not used for interrupts should be placed at the end of the
174  * // array. Callback entries can be omitted from callbacks array to
175  * // reduce memory usage.
176  * //
177  * GPIO_PinConfig gpioPinConfigs[] = {
178  * // Input pins
179  * // MSP_EXP432P401R_GPIO_S1
180  * GPIOMSP432_P1_1 | GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING,
181  * // MSP_EXP432P401R_GPIO_S2
182  * GPIOMSP432_P1_4 | GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_FALLING,
183  *
184  * // Output pins
185  * // MSP_EXP432P401R_GPIO_LED1
186  * GPIOMSP432_P1_0 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
187  * // MSP_EXP432P401R_GPIO_LED_RED
188  * GPIOMSP432_P2_0 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
189  * };
190  * @endcode
191  *
192  * 2. An array of @ref GPIO_CallbackFxn elements that is used to store
193  * callback function pointers for GPIO pins configured with interrupts.
194  * The indexes for these array elements correspond to the pins defined
195  * in the GPIO_pinConfig array. These function pointers can be defined
196  * statically by referencing the callback function name in the array
197  * element, or dynamically, by setting the array element to NULL and using
198  * GPIO_setCallback() at runtime to plug the callback entry.
199  * Pins not used for interrupts can be omitted from the callback array to
200  * reduce memory usage (if they are placed at the end of GPIO_pinConfig
201  * array). The callback function syntax should match the following:
202  * @code
203  * void (*GPIO_CallbackFxn)(uint_least8_t index);
204  * @endcode
205  * The index parameter is the same index that was passed to
206  * GPIO_setCallback(). This allows the same callback function to be used
207  * for multiple GPIO interrupts, by using the index to identify the GPIO
208  * that caused the interrupt.
209  * Keep in mind that the callback functions will be called in the context of
210  * an interrupt service routine and should be designed accordingly. When an
211  * interrupt is triggered, the interrupt status of all (interrupt enabled) pins
212  * on a port will be read, cleared, and the respective callbacks will be
213  * executed. Callbacks will be called in order from least significant bit to
214  * most significant bit.
215  * Below is an MSP432 device specific example of the GPIO_CallbackFxn array:
216  * @code
217  * //
218  * // Array of callback function pointers
219  * // NOTE: The order of the pin configurations must coincide with what was
220  * // defined in MSP_EXP432P401R.h
221  * // NOTE: Pins not used for interrupts can be omitted from callbacks array
222  * // to reduce memory usage (if placed at end of gpioPinConfigs
223  * // array).
224  * //
225  * GPIO_CallbackFxn gpioCallbackFunctions[] = {
226  * // MSP_EXP432P401R_GPIO_S1
227  * NULL,
228  * // MSP_EXP432P401R_GPIO_S2
229  * NULL
230  * };
231  * @endcode
232  *
233  * 3. A device specific GPIOxxx_Config structure that tells the GPIO
234  * driver where the two aforementioned arrays are and the number of elements
235  * in each. The interrupt priority of all pins configured to generate
236  * interrupts is also specified here. Values for the interrupt priority are
237  * device-specific. You should be well-acquainted with the interrupt
238  * controller used in your device before setting this parameter to a
239  * non-default value. The sentinel value of (~0) (the default value) is
240  * used to indicate that the lowest possible priority should be used.
241  * Below is an MSP432 device specific example of a GPIOxxx_Config
242  * structure:
243  * @code
244  * //
245  * // MSP432 specific GPIOxxx_Config structure
246  * //
247  * const GPIOMSP432_Config GPIOMSP432_config = {
248  * .pinConfigs = (GPIO_PinConfig *)gpioPinConfigs,
249  * .callbacks = (GPIO_CallbackFxn *)gpioCallbackFunctions,
250  * .numberOfPinConfigs = sizeof(gpioPinConfigs)/sizeof(GPIO_PinConfig),
251  * .numberOfCallbacks = sizeof(gpioCallbackFunctions)/sizeof(GPIO_CallbackFxn),
252  * .intPriority = (~0)
253  * };
254  * @endcode
255  *
256  * ### Initializing the GPIO Driver #
257  *
258  * GPIO_init() must be called before any other GPIO APIs. This function
259  * configures each GPIO pin in the user-provided @ref GPIO_PinConfig
260  * array according to the defined settings. The user can also reconfigure
261  * a pin dynamically after GPIO_init() is called by using the
262  * GPIO_setConfig(), and GPIO_setCallback() APIs.
263  *
264  * # Implementation #
265  *
266  * Unlike most other TI-RTOS drivers, the GPIO driver has no generic function
267  * table with pointers to device-specific API implementations. All the generic
268  * GPIO APIs are implemented by the device-specific GPIO driver module.
269  * Additionally, there is no notion of an instance 'handle' with the GPIO
270  * driver.
271  *
272  * GPIO pins are referenced by their numeric index in the GPIO_PinConfig
273  * array. This design approach was used to enhance runtime and memory
274  * efficiency.
275  *
276  ******************************************************************************
277  */
278 
279 #ifndef ti_drivers_GPIO__include
280 #define ti_drivers_GPIO__include
281 
282 #ifdef __cplusplus
283 extern "C" {
284 #endif
285 
286 #include <stdint.h>
287 
306 #define GPIO_STATUS_RESERVED (-32)
307 
314 #define GPIO_STATUS_SUCCESS (0)
315 
322 #define GPIO_STATUS_ERROR (-1)
323 
334 typedef uint32_t GPIO_PinConfig;
335 
340 #define GPIO_CFG_IO_MASK 0x00ff0000
341 #define GPIO_CFG_IO_LSB 16
342 #define GPIO_CFG_OUT_TYPE_MASK 0x00060000
343 #define GPIO_CFG_OUT_TYPE_LSB 17
344 #define GPIO_CFG_IN_TYPE_MASK 0x00060000
345 #define GPIO_CFG_IN_TYPE_LSB 17
346 #define GPIO_CFG_OUT_STRENGTH_MASK 0x00f00000
347 #define GPIO_CFG_OUT_STRENGTH_LSB 20
348 #define GPIO_CFG_INT_MASK 0x07000000
349 #define GPIO_CFG_INT_LSB 24
350 #define GPIO_CFG_OUT_BIT 19
351 
360 #define GPIO_CFG_OUTPUT (((uint32_t) 0) << GPIO_CFG_IO_LSB)
361 #define GPIO_CFG_OUT_STD (((uint32_t) 0) << GPIO_CFG_IO_LSB)
362 #define GPIO_CFG_OUT_OD_NOPULL (((uint32_t) 2) << GPIO_CFG_IO_LSB)
363 #define GPIO_CFG_OUT_OD_PU (((uint32_t) 4) << GPIO_CFG_IO_LSB)
364 #define GPIO_CFG_OUT_OD_PD (((uint32_t) 6) << GPIO_CFG_IO_LSB)
366 #define GPIO_CFG_OUT_STR_LOW (((uint32_t) 0) << GPIO_CFG_OUT_STRENGTH_LSB)
367 #define GPIO_CFG_OUT_STR_MED (((uint32_t) 1) << GPIO_CFG_OUT_STRENGTH_LSB)
368 #define GPIO_CFG_OUT_STR_HIGH (((uint32_t) 2) << GPIO_CFG_OUT_STRENGTH_LSB)
370 #define GPIO_CFG_OUT_HIGH (((uint32_t) 1) << GPIO_CFG_OUT_BIT)
371 #define GPIO_CFG_OUT_LOW (((uint32_t) 0) << GPIO_CFG_OUT_BIT)
377 #define GPIO_CFG_INPUT (((uint32_t) 1) << GPIO_CFG_IO_LSB)
378 #define GPIO_CFG_IN_NOPULL (((uint32_t) 1) << GPIO_CFG_IO_LSB)
379 #define GPIO_CFG_IN_PU (((uint32_t) 3) << GPIO_CFG_IO_LSB)
380 #define GPIO_CFG_IN_PD (((uint32_t) 5) << GPIO_CFG_IO_LSB)
386 #define GPIO_CFG_IN_INT_NONE (((uint32_t) 0) << GPIO_CFG_INT_LSB)
387 #define GPIO_CFG_IN_INT_FALLING (((uint32_t) 1) << GPIO_CFG_INT_LSB)
388 #define GPIO_CFG_IN_INT_RISING (((uint32_t) 2) << GPIO_CFG_INT_LSB)
389 #define GPIO_CFG_IN_INT_BOTH_EDGES (((uint32_t) 3) << GPIO_CFG_INT_LSB)
390 #define GPIO_CFG_IN_INT_LOW (((uint32_t) 4) << GPIO_CFG_INT_LSB)
391 #define GPIO_CFG_IN_INT_HIGH (((uint32_t) 5) << GPIO_CFG_INT_LSB)
402 #define GPIO_CFG_IN_INT_ONLY (((uint32_t) 1) << 27)
408 #define GPIO_DO_NOT_CONFIG 0x40000000
422 typedef void (*GPIO_CallbackFxn)(uint_least8_t index);
423 
434 extern void GPIO_clearInt(uint_least8_t index);
435 
443 extern void GPIO_disableInt(uint_least8_t index);
444 
458 extern void GPIO_enableInt(uint_least8_t index);
459 
471 extern void GPIO_getConfig(uint_least8_t index, GPIO_PinConfig *pinConfig);
472 
483 extern void GPIO_init();
484 
495 extern uint_fast8_t GPIO_read(uint_least8_t index);
496 
518 extern void GPIO_setCallback(uint_least8_t index, GPIO_CallbackFxn callback);
519 
533 extern int_fast16_t GPIO_setConfig(uint_least8_t index,
534  GPIO_PinConfig pinConfig);
535 
541 extern void GPIO_toggle(uint_least8_t index);
542 
549 extern void GPIO_write(uint_least8_t index, unsigned int value);
550 
551 #ifdef __cplusplus
552 }
553 #endif
554 
555 #endif /* ti_drivers_GPIO__include */
void GPIO_write(uint_least8_t index, unsigned int value)
Writes the value to a GPIO pin.
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.
uint32_t GPIO_PinConfig
GPIO pin configuration settings.
Definition: GPIO.h:334
void GPIO_getConfig(uint_least8_t index, GPIO_PinConfig *pinConfig)
Get the current configuration for a gpio pin.
void GPIO_init()
Initializes the GPIO module.
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.
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:422
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale