PIN.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2017, 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 /*!*****************************************************************************
34  * @file PIN.h
35  * @brief Generic PIN & GPIO driver
36  *
37  * To use the PIN driver ensure that the correct TI-RTOS driver library for your
38  * device is linked in and include this header file:
39  * @code
40  * #include <ti/drivers/PIN.h>
41  * @endcode
42  *
43  * In order to use device-specific functionality or to use the size/speed-
44  * optimized versions of some of the PIN driver functions that circumvent error
45  * and resource checking, link in the correct TI-RTOS driver library for your
46  * device and include the device-specific PIN driver header file (which in turn
47  * includes PIN.h). As an example for the CC26xx family of devices:
48  * @code
49  * #include <ti/drivers/pin/PINCC26xx.h>
50  * @endcode
51  *
52  * # Overview #
53  * The PIN driver allows clients (applications or other drivers) to allocate
54  * and control the I/O pins on the device. The pins can either be software-
55  * controlled general-purpose I/O (GPIO) or connected to hardware peripherals.
56  * Furthermore, the PIN driver allows clients to configure interrupt
57  * functionality on the pins to receive callbacks (and potentially wake up from
58  * the standby or idle power modes) on configurable signal edges.
59  *
60  * Most other drivers rely on functionality in the PIN driver.
61  *
62  * ## Structure ##
63  * In order to provide a generic driver interface, this file (PIN.h) only
64  * defines the API and some common data types and macros of the driver. A PIN
65  * client (application or driver) can in most cases only use the generic PIN
66  * API, however, for more advanced usage where device-specific pin
67  * configuration is used or device-specific PIN driver API extensions are
68  * used must use the device-specific PIN driver API.
69  *
70  * The device-independent API is implemented as function calls with pin
71  * access control based on the PIN client handle. For time-critical
72  * applications the device-specific API can be used directly, as these
73  * API functions are implemented as inlined functions without access control.
74  *
75  * ## Functionality ##
76  * The PIN module provides the following functionality:
77  * - Initialize I/O pins upon boot to a default configuration (possibly
78  * user-generated)
79  * - Provides atomic manipulation of I/O pin hardware registers to allow safe
80  * simultaneous use of I/O pin resources
81  * - I/O pin allocation
82  * - A set of pins can be allocated receiving a pin set handle.
83  * Typically each peripheral driver will allocate a set of pins and an
84  * application must allocate the pins it uses too
85  * - When a pin set is deallocated all the pins in it revert to the state
86  * they were initialized to at boot
87  * - General-purpose I/O (GPIO) services
88  * - Read input buffer value
89  * - Read and set output buffer value
90  * - Read and set output buffer enable
91  * - Access as single pin or port (muliple pins simultaneously)
92  * - Protect pin manipulation
93  * - Pins in an allocated set can only be manipulated using the corresponding
94  * handle.
95  * - No handle is needed to read input and output buffer values
96  * - I/O buffer/driver control
97  * - Input mode (detached, hysteresis, pull-up, pull-down)
98  * - Output mode (tristated, push-pull, open drain, open source)
99  * - Output driver strength control
100  * - Output driver slew rate control
101  * - I/O source/target selection (device-specific driver only)
102  * - Map pin to GPIO, peripheral or HW observation signal
103  * - Configuration of I/O interrupt and wakeup from standby
104  * - Interrupt configuration: signal edge to interrupt on, interrupt mask,
105  * callback function registration
106  * - Pins that have enabled interrupts will also wake up the device from low-
107  * power modes like standby and idle upon events
108  * - Provides data types and enums/defines for use in pin configurations
109  * definitions in board files, drivers and applications
110  *
111  * ## Pin Allocation ##
112  * The purpose of being able to allocate pins to a pin set is to:
113  * - Manage pin resources
114  * - Give exclusive, protected access to these pins
115  * - Establish a driver state in connection with these pins that allow
116  * functionality such as I/O interrupt callback and I/O port operations
117  * in a safe manner
118  *
119  * | API function | Description |
120  * |--------------------|------------------------------------------------------|
121  * | PIN_open() | Allocate pins to a set, returns handle |
122  * | PIN_add() | Add pin to pin set for open PIN handle |
123  * | PIN_remove() | Removes pin from pin set for open PIN handle |
124  * | PIN_close() | Deallocate pin set, revert to original GPIO state |
125  *
126  * ## GPIO ##
127  * Pins that are to be used as software-controlled general-purpose I/O (GPIO)
128  * need to be allocated in the same manner as for pins that will be mapped to
129  * hardware peripheral ports. A pin set requested with a PIN_open() call may
130  * contain a mix of pins to be used for GPIO and hardware-mapped pins.
131  *
132  * When a pin is deallocated using PIN_close() it reverts to the GPIO
133  * configuration it was given in the initial call to PIN_init().
134  *
135  * | API function | Description |
136  * |----------------------|---------------------------------------------------|
137  * | PIN_init() | Initialize I/O pins to a safe GPIO state |
138  * | PIN_open() | Allocate pins to a set, returns handle |
139  * | PIN_close() | Deallocate pin set, revert to original GPIO state |
140  * | PIN_setConfig() | Sets parts of or complete pin configuration |
141  * | PIN_getConfig() | Returns pin configuration |
142  * | PIN_setOutputEnable()| Control output enable of GPIO pin |
143  * | PIN_getInputValue() | Read input value on pin |
144  * | PIN_setOutputValue() | Set output value of GPIO pin |
145  * | PIN_getOutputValue() | Get current output value of GPIO pin |
146  *
147  * ## GPIO Ports ##
148  * Sometimes it is necessary to be able to read from, write to or control
149  * multiple pins simultaneously (in time). The PIN driver allows a set of
150  * allocated pins, if they reside on the same GPIO port in the underlying
151  * hardware, to be manipulated simultaneously.
152  *
153  * | API function | Description |
154  * |--------------------------|---------------------------------------------------|
155  * | PIN_open() | Allocate pins to a set, returns handle |
156  * | PIN_close() | Deallocate pin set, revert to original GPIO state |
157  * | PIN_getPortMask() | Returns bitmask for allocated pins in GPIO port |
158  * | PIN_getPortInputValue() | Returns input value of whole GPIO port |
159  * | PIN_setPortOutputValue() | Sets output value of whole GPIO port (masked) |
160  * | PIN_getPortOutputValue() | Get current output value of whole GPIO port |
161  * | PIN_setPortOutputValue() | Sets output value of whole GPIO port (masked) |
162  * | PIN_setPortOutputEnable()| Sets output enable of whole GPIO port (masked) |
163  *
164  * ## I/O Pin Configuration ##
165  * Different devices provide different levels of configurability of I/O pins.
166  * The PIN driver provides a fairly extensive set of @ref PIN_GENERIC_FLAGS
167  * "generic IO configuration options" that are device-independent, all of which
168  * might not be supported by the underlying device-specific PIN driver and
169  * hardware. Likewise, the underlying device-specific PIN driver and hardware
170  * might support additional configuration options not covered by the generic
171  * options.
172  *
173  * To allow both independence from and flexibility to use features on the target
174  * device, the #PIN_Config entries used by the PIN driver allows use of either
175  * a set of @ref PIN_GENERIC_FLAGS "generic PIN configuration options" or a
176  * device-specific set of PIN configuration options defined in the underlying
177  * device-specific PIN driver (e.g. PINCC26XX.h)
178  *
179  * ### Mapping to GPIO or Peripheral ###
180  * Since the amount of flexibilty in which peripherals can be mapped to which
181  * pins and the manner in which this needs to be set up is highly
182  * device-specific, functions for configuring this is not part of the generic
183  * PIN driver API but is left to be implemented by device-specific PIN drivers.
184  * See the relevant device-specific PIN driver (e.g. PINCC26XX.h) for details.
185  *
186  * ### Input Mode ###
187  * The input mode of a pin controls:
188  * - Input buffer enable
189  * - Pull-ups or pull-downs
190  * - Hysteresis of input buffer
191  * - Inversion of logical input level
192  * - Potentially, device-specific options
193  * The input mode is set initially with PIN_init() or at a later stage with
194  * PIN_setConfig() and a bitmask with the relevant options
195  *
196  * | API function | Description |
197  * |------------------|-------------------------------------------------------|
198  * | PIN_init() | Initialize IOs to a safe GPIO state |
199  * | PIN_getConfig() | Returns pin configuration |
200  * | PIN_setConfig() | Sets parts of or complete pin configuration |
201  *
202  * ### Output Mode ###
203  * The output mode of a pin controls:
204  * - Output buffer enable
205  * - Output driver mode (push-pull, open-drain, open-source)
206  * - Output driver slew control
207  * - Output driver current (drive strength)
208  * - Inversion of logical output level
209  * - Potentially, device-specific options
210  *
211  * | API function | Description |
212  * |----------------------|---------------------------------------------------|
213  * | PIN_init() | Initialize IOs to a safe GPIO state |
214  * | PIN_setOutputEnable()| Control output enable of GPIO pins |
215  * | PIN_getConfig() | Returns pin configuration |
216  * | PIN_setConfig() | Sets parts of or complete pin configuration |
217  *
218  * ### Pin Interrupt and Pin Wakeup ###
219  * Pin interrupts are used to process asynchronous signal edge events on pins
220  * and potentially wake the device up from low power sleep modes. To use pin
221  * interrupts the relevant pins must be allocated and a interrupt callback
222  * registered by the client. The callback function will be called in a SWI
223  * context.
224  *
225  * | API function | Description |
226  * |---------------------|----------------------------------------------------|
227  * | PIN_init() | Initialize IOs to a safe GPIO state |
228  * | PIN_getConfig() | Returns pin configuration |
229  * | PIN_setConfig() | Sets parts of or complete pin configuration |
230  * | PIN_setInterrupt() | Control interrupt enable and edge for pin |
231  * | PIN_registerIntCb() | Register callback function for a set of pins |
232  * | PIN_setUserArg() | Sets a user argument associated with the handle |
233  * | PIN_getUserArg() | Gets a user argument associated with the handle |
234  *
235  * ## PIN Data Types ##
236  * The PIN driver defines the following data types:
237  * - #PIN_Id: identifies a pin in arguments or lists
238  * - #PIN_Config: provides I/O configuration options for a pin and also embeds
239  * a #PIN_Id identifier. See @ref PIN_GENERIC_FLAGS "available flags/fields"
240  *
241  * ## PIN Config Flags/Fields and Bitmasks ##
242  * The PIN driver uses the #PIN_Config data type many places and it merits some
243  * additional attention. A #PIN_Config value consists of a collection of flags
244  * and fields that define how an I/O pin and its attached GPIO interface should
245  * behave electrically and logically. In addition a #PIN_Config value also
246  * embeds a #PIN_Id pin ID, identifying which pin it refers to.
247  *
248  * A #PIN_Config value can use one of two mutually exclusive sets of flags and
249  * fields: @ref PIN_GENERIC_FLAGS "device-independent options" defined in
250  * PIN.h or device-dependent options defined in the device-specific
251  * implementation of the PIN driver interface. Any function that uses
252  * #PIN_Config will accept both option types, just not at the same time.
253  * PIN_getConfig() always returns device-independent options, an additional
254  * device-specific version (e.g. PINCC26XX_getConfig()) might return
255  * device-specific options.
256  *
257  * The bitmask argument for PIN_setConfig() decides which of the options the
258  * call should affect. All other options are kept at their current values in
259  * hardware. Thus PIN_setConfig(hPins, PIN_BM_PULLING, PIN_BM_PULLUP) will only
260  * change the pullup/pulldown configuration of the pin, leaving everything
261  * else, such as for instance output enable, input hysteresis or output value,
262  * untouched. For #PIN_Config lists (as supplied to PIN_init() for instance)
263  * there is no mask, so all options will affect the pin.
264  *
265  * Some of the options affect the pin regardless of whether it is mapped to
266  * a hardware peripheral or GPIO and some options only take effect when it is
267  * mapped to GPIO. These latter options have \_GPIO_ in their names.
268  *
269  * The default value for a flag/field is indicated with a star (*) in the
270  * description of the options and will be applied if any explicit value is
271  * not supplied for a flag/field that is masked.
272  *
273  * The available options can be grouped into categories as follows:
274  *
275  * ### Input Mode Options ###
276  * | Option | Option bitmask | HW/GPIO | Description |
277  * |--------------------|-----------------------|---------|--------------------------------|
278  * |#PIN_INPUT_EN (*) |#PIN_BM_INPUT_EN | Both | Enable pin input buffer |
279  * |#PIN_INPUT_DIS |#PIN_BM_INPUT_EN | Both | Disable pin input buffer |
280  * |#PIN_HYSTERESIS |#PIN_BM_HYSTERESIS | Both | Enable hysteresis on input |
281  * |#PIN_NOPULL (*) |#PIN_BM_PULLING | Both | No pullup/pulldown |
282  * |#PIN_PULLUP |#PIN_BM_PULLING | Both | Enable pullup |
283  * |#PIN_PULLDOWN |#PIN_BM_PULLING | Both | Enable pulldown |
284  * | |#PIN_BM_INPUT_MODE | | Mask for all input mode options|
285  *
286  * ### Output Mode Options ###
287  * | Option | Option bitmask | HW/GPIO | Description |
288  * |------------------------|------------------------|---------|----------------------------------|
289  * |#PIN_GPIO_OUTPUT_DIS (*)|#PIN_BM_GPIO_OUTPUT_EN | GPIO | Disable GPIO output buffer |
290  * |#PIN_GPIO_OUTPUT_EN |#PIN_BM_GPIO_OUTPUT_EN | GPIO | Enable GPIO output buffer |
291  * |#PIN_GPIO_LOW (*) |#PIN_BM_GPIO_OUTPUT_VAL | GPIO | Output 0 when GPIO |
292  * |#PIN_GPIO_HIGH |#PIN_BM_GPIO_OUTPUT_VAL | GPIO | Output 1 when GPIO |
293  * |#PIN_PUSHPULL (*) |#PIN_BM_OUTPUT_BUF | Both | Use push-pull output buffer |
294  * |#PIN_OPENDRAIN |#PIN_BM_OUTPUT_BUF | Both | Use open drain output buffer |
295  * |#PIN_OPENSOURCE |#PIN_BM_OUTPUT_BUF | Both | Use open source output buffer |
296  * |#PIN_SLEWCTRL |#PIN_BM_SLEWCTRL | Both | Enable output buffer slew control|
297  * |#PIN_DRVSTR_MIN (*) |#PIN_BM_DRVSTR | Both | Output buffer uses min drive |
298  * |#PIN_DRVSTR_MED |#PIN_BM_DRVSTR | Both | Output buffer uses medium drive |
299  * |#PIN_DRVSTR_MAX |#PIN_BM_DRVSTR | Both | Output buffer uses max drive |
300  * | |#PIN_BM_OUTPUT_MODE | | Mask for all output mode options |
301  *
302  * ### Misc Options ###
303  * | Option | Option bitmask | HW/GPIO | Description |
304  * |-------------------|------------------|---------|----------------------------------|
305  * |#PIN_INV_INOUT |#PIN_BM_INV_INOUT | Both | Invert input/output |
306  * |#PIN_IRQ_DIS (*) |#PIN_BM_IRQ | Both | Disable pin interrupts |
307  * |#PIN_IRQ_NEGEDGE |#PIN_BM_IRQ | Both | Pin interrupts on negative edges |
308  * |#PIN_IRQ_POSEDGE |#PIN_BM_IRQ | Both | Pin interrupts on negative edges |
309  * |#PIN_IRQ_BOTHEDGES |#PIN_BM_IRQ | Both | Pin interrupts on both edges |
310  * | |#PIN_BM_ALL | | Mask for *all* options |
311  *
312  * ## Initialization ##
313  * The PIN driver must be initialized before any other drivers are initialized.
314  * In order for IO pins to get a safe value as soon as possible PIN_init()
315  * should be called as early as possible in the boot sequence. Typically,
316  * PIN_init() is called at the start of main() before TI-RTOS is started with
317  * BIOS_start().
318  *
319  * PIN_init() takes as an argument a #PIN_Config list containing default pin
320  * configurations. Typically the #PIN_Config list defined in the board files
321  * is used:
322  * @code
323  * PIN_init(BoardGpioInitTable);
324  * @endcode
325  * It is possible, however, to use another #PIN_Config list if desired.
326  *
327  * ## Power Management Interaction ##
328  * No specific interaction with power management module, as PIN is independent
329  * of power mode.
330  *
331  * ## Functionality Not Supported ##
332  * There is no known unsupported functionality.
333  *
334  * ## Instrumentation ##
335  * The pin driver does not use any of the instrumentation facilities.
336  *
337  * # Usage Examples #
338  *
339  * ## Initialization and Pin Allocation ##
340  * Example that illustrates when and how to call PIN_init(), PIN_open(), PIN_add(), PIN_close()
341  * @code
342  * // Default pin configuration. Typically resides in Board.c file.
343  * // IOs not mentioned here configured to default: input/output/pull disabled
344  * PIN_Config BoardGpioInitTable[] = {
345  * // DIO11: LED A (initially off)
346  * PIN_ID(11) | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
347  * // DIO10: LED B (initially off)
348  * PIN_ID(10) | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
349  * // DIO23: BUTTON A (ensure pull-up as button A is also used by other ICs)
350  * PIN_ID(23) | PIN_INPUT_EN | PIN_PULLUP | PIN_HYSTERESIS,
351  * // DIO3: LCD controller reset line (make sure LCD is in reset)
352  * PIN_ID(3) | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL,
353  * // Terminate list
354  * PIN_TERMINATE
355  * };
356  *
357  * //Stack size in bytes
358  * #define THREADSTACKSIZE 1024
359  *
360  * // PIN_init() should be called as early as possible in boot
361  * void main() {
362  *
363  * pthread_t thread;
364  * pthread_attr_t attrs;
365  * struct sched_param priParam;
366  * int retc;
367  * int detachState;
368  *
369  * //Board_initGeneral() will call PIN_init(BoardGpioInitTable)
370  * Board_initGeneral();
371  *
372  * // Set priority and stack size attributes
373  * pthread_attr_init(&attrs);
374  * priParam.sched_priority = 1;
375  *
376  * detachState = PTHREAD_CREATE_DETACHED;
377  * retc = pthread_attr_setdetachstate(&attrs, detachState);
378  * if (retc != 0) {
379  * // pthread_attr_setdetachstate() failed
380  * while (1);
381  * }
382  *
383  * pthread_attr_setschedparam(&attrs, &priParam);
384  *
385  * retc |= pthread_attr_setstacksize(&attrs, THREADSTACKSIZE);
386  * if (retc != 0) {
387  * // pthread_attr_setstacksize() failed
388  * while (1);
389  * }
390  *
391  * retc = pthread_create(&thread, &attrs, mainThread, NULL);
392  * if (retc != 0) {
393  * // pthread_create() failed
394  * while (1);
395  * }
396  *
397  * // Start kernel
398  * Add_Kernel_Start_Call();
399  *
400  * return (0);
401  * }
402  *
403  * // Human user interface PIN state/handle
404  * PIN_State hStateHui;
405  * #define HUI_LED_A PIN_ID(11)
406  * #define HUI_LED_B PIN_ID(10)
407  * #define HUI_LED_C PIN_ID(9)
408  * #define HUI_BUTTON_A PIN_ID(23)
409  * #define HUI_BUTTON_B PIN_ID(24)
410  *
411  * static void taskStartFxn(UArg a0, UArg a1) {
412  * // Define pins used by Human user interface and initial configuration
413  * const PIN_Config pinListHui[] = {
414  * HUI_LED_A | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
415  * HUI_LED_B | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
416  * HUI_BUTTON_A | PIN_INPUT_EN | PIN_PULLUP | PIN_HYSTERESIS,
417  * HUI_BUTTON_B | PIN_INPUT_EN | PIN_PULLUP | PIN_HYSTERESIS,
418  * PIN_TERMINATE
419  * };
420  *
421  * // Get handle to this collection of pins
422  * if (!PIN_open(&hStateHui, pinListHui)) {
423  * // Handle allocation error
424  * }
425  *
426  * // ...
427  *
428  * // We can also add (and remove) pins to a set at run time
429  * PIN_Status status = PIN_add(
430  * &hStateHui,
431  * HUI_LED_C | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
432  * );
433  * if (status != PIN_SUCCESS) {
434  * // Handling allocation error is especially important with PIN_add()
435  * }
436  *
437  * // ...
438  * huiDoSomething();
439  *
440  * // Before ending task, make sure to deallocate pins. They will return
441  * // to the default configurations provided in PIN_init()
442  * PIN_close(&hStateHui);
443  * }
444  * @endcode
445  *
446  * ## Application use of GPIO ##
447  * An example of using GPIO that builds on the previous example. Illustrates how
448  * to read input values, set output values and control output enable
449  * @code
450  * void huiDoSomething() {
451  * // Running lights on LEDs A-B-C (left to right). Button A causes left
452  * // movement, button B causes right movement, both simultaneously aborts
453  * // and disables LED output drivers
454  *
455  * // LED initial state (A off, B off, C on). Only our outputs are affected
456  * PIN_setPortOutputValue(&hStateHui, (1<<HUI_LED_C));
457  *
458  * int32_t moveDir = -1; // <0: left, 0: stop, >0 right
459  * while (moveDir) {
460  * // Update LEDs
461  * if (moveDir<0) {
462  * // Left movement
463  * uint32_t t = PIN_getOutputValue(HUI_LED_A);
464  * PIN_setOutputValue(&hStateHui, HUI_LED_A, PIN_getOutputValue(HUI_LED_B));
465  * PIN_setOutputValue(&hStateHui, HUI_LED_B, PIN_getOutputValue(HUI_LED_C));
466  * PIN_setOutputValue(&hStateHui, HUI_LED_C, t);
467  * } else {
468  * // Right movement
469  * uint32_t t = PIN_getOutputValue(HUI_LED_C);
470  * PIN_setOutputValue(&hStateHui, HUI_LED_C, PIN_getOutputValue(HUI_LED_B));
471  * PIN_setOutputValue(&hStateHui, HUI_LED_B, PIN_getOutputValue(HUI_LED_A));
472  * PIN_setOutputValue(&hStateHui, HUI_LED_A, t);
473  * }
474  *
475  * // Sleep for 333 ms
476  * Task_sleep(333000/10);
477  *
478  * // Read input from both buttons simultaneously
479  * uint32_t buttons = PIN_getPortInputValue(&hStateHui);
480  * if (buttons&(1<<HUI_BUTTON_A) == 0) {
481  * moveDir = -1;
482  * } else if (buttons&(1<<HUI_BUTTON_A) == 0) {
483  * moveDir = 1;
484  * } else if (buttons&((1<<HUI_BUTTON_A)|(1<<HUI_BUTTON_A))) {
485  * moveDir = 0;
486  * }
487  * }
488  * // Disable output enable for all pins (only our pins affected)
489  * PIN_setPortOutputEnable(&hStateHui, 0);
490  * }
491  * @endcode
492  *
493  * ## Pin Interrupt ##
494  * An example that handles pin inputs in the GPIO example above using PIN interrupts
495  * instead:
496  * @code
497  * // volatile variable used to communicate between callback and task
498  * static volatile int32_t moveDir = -1; // <0: left, 0: stop, >0 right
499  *
500  * // Pin interrupt callback
501  * void huiPinIntCb(PIN_Handle handle, PIN_Id pinId) {
502  * // Ignore pinId and read input from both buttons simultaneously
503  * uint32_t buttons = PIN_getPortInputValue(&hStateHui);
504  * if (buttons&(1<<HUI_BUTTON_A) == 0) {
505  * moveDir = -1;
506  * } else if (buttons&(1<<HUI_BUTTON_A) == 0) {
507  * moveDir = 1;
508  * } else if (buttons&((1<<HUI_BUTTON_A)|(1<<HUI_BUTTON_A))) {
509  * moveDir = 0;
510  * }
511  * }
512  *
513  * void huiDoSomething() {
514  * // Running lights on LEDs A-B-C (left to right). Button A causes left
515  * // movement, button B causes right movement, both simultaneously aborts
516  * // and disables LED output drivers
517  *
518  * // LED initial state (A off, B off, C on). Only our outputs are affected
519  * PIN_setPortOutputValue(&hStateHui, (1<<HUI_LED_C));
520  * moveDir = -1; // <0: left, 0: stop, >0 right
521  *
522  * // Setup pin interrupts and register callback
523  * PIN_registerIntCb(&hStateHui, huiPinIntCb);
524  * PIN_setInterrupt(&hStateHui, HUI_BUTTON_A | PIN_IRQ_NEGEDGE);
525  * PIN_setInterrupt(&hStateHui, HUI_BUTTON_B | PIN_IRQ_NEGEDGE);
526  *
527  * while (moveDir) {
528  * // Update LEDs
529  * if (moveDir<0) {
530  * // Left movement
531  * uint32_t t = PIN_getOutputValue(HUI_LED_A);
532  * PIN_setOutputValue(&hStateHui, HUI_LED_A, PIN_getOutputValue(HUI_LED_B));
533  * PIN_setOutputValue(&hStateHui, HUI_LED_B, PIN_getOutputValue(HUI_LED_C));
534  * PIN_setOutputValue(&hStateHui, HUI_LED_C, t);
535  * } else {
536  * // Right movement
537  * uint32_t t = PIN_getOutputValue(HUI_LED_C);
538  * PIN_setOutputValue(&hStateHui, HUI_LED_C, PIN_getOutputValue(HUI_LED_B));
539  * PIN_setOutputValue(&hStateHui, HUI_LED_B, PIN_getOutputValue(HUI_LED_A));
540  * PIN_setOutputValue(&hStateHui, HUI_LED_A, t);
541  * }
542  *
543  * // Sleep for 333 ms (we will likely go into standby)
544  * Task_sleep(333000/10);
545  * }
546  * // Disable output enable for all pins (only our pins affected)
547  * PIN_setPortOutputEnable(&hStateHui, 0);
548  * // Disable pin interrupts
549  * PIN_setInterrupt(&hStateHui, HUI_BUTTON_A | PIN_IRQ_DIS);
550  * PIN_setInterrupt(&hStateHui, HUI_BUTTON_B | PIN_IRQ_DIS);
551  * }
552  * @endcode
553  *
554  *******************************************************************************
555  */
556 
557 #ifndef ti_drivers_PIN__include
558 #define ti_drivers_PIN__include
559 #ifdef __cplusplus
560 extern "C" {
561 #endif
562 
563 #include <stdbool.h>
564 #include <stdint.h>
565 
577 typedef uint8_t PIN_Id;
578 
580 #define PIN_UNASSIGNED 0xFF
581 #define PIN_TERMINATE 0xFE
583 
597 typedef uint32_t PIN_Config;
598 
609 #define PIN_ID(x) ((x)&0xFF)
610 
611 
628 #define PIN_GEN (((uint32_t)1) << 31)
629 
630 #define PIN_INPUT_EN (PIN_GEN | (0 << 29))
631 #define PIN_INPUT_DIS (PIN_GEN | (1 << 29))
632 #define PIN_HYSTERESIS (PIN_GEN | (1 << 30))
633 #define PIN_NOPULL (PIN_GEN | (0 << 13))
634 #define PIN_PULLUP (PIN_GEN | (1 << 13))
635 #define PIN_PULLDOWN (PIN_GEN | (2 << 13))
636 #define PIN_BM_INPUT_EN (1 << 29)
637 #define PIN_BM_HYSTERESIS (1 << 30)
638 #define PIN_BM_PULLING (0x3 << 13)
639 
640 #define PIN_BM_INPUT_MODE (PIN_BM_INPUT_EN|PIN_BM_HYSTERESIS|PIN_BM_PULLING)
642 
643 #define PIN_GPIO_OUTPUT_DIS (PIN_GEN | (0 << 23))
644 #define PIN_GPIO_OUTPUT_EN (PIN_GEN | (1 << 23))
645 #define PIN_GPIO_LOW (PIN_GEN | (0 << 22))
646 #define PIN_GPIO_HIGH (PIN_GEN | (1 << 22))
647 #define PIN_PUSHPULL (PIN_GEN | (0 << 25))
648 #define PIN_OPENDRAIN (PIN_GEN | (2 << 25))
649 #define PIN_OPENSOURCE (PIN_GEN | (3 << 25))
650 #define PIN_SLEWCTRL (PIN_GEN | (1 << 12))
651 #define PIN_DRVSTR_MIN (PIN_GEN | (0x0 << 8))
652 #define PIN_DRVSTR_MED (PIN_GEN | (0x4 << 8))
653 #define PIN_DRVSTR_MAX (PIN_GEN | (0x8 << 8))
654 #define PIN_BM_GPIO_OUTPUT_EN (1 << 23)
655 #define PIN_BM_GPIO_OUTPUT_VAL (1 << 22)
656 #define PIN_BM_OUTPUT_BUF (0x3 << 25)
657 #define PIN_BM_SLEWCTRL (0x1 << 12)
658 #define PIN_BM_DRVSTR (0xF << 8)
659 
660 #define PIN_BM_OUTPUT_MODE (PIN_BM_GPIO_OUTPUT_VAL | PIN_BM_GPIO_OUTPUT_EN | \
662  PIN_BM_OUTPUT_BUF | PIN_BM_SLEWCTRL | PIN_BM_DRVSTR)
663 
664 #define PIN_INV_INOUT (PIN_GEN | (1 << 24))
665 #define PIN_BM_INV_INOUT (1 << 24)
666 
667 #define PIN_IRQ_DIS (PIN_GEN | (0x0 << 16))
668 #define PIN_IRQ_NEGEDGE (PIN_GEN | (0x5 << 16))
669 #define PIN_IRQ_POSEDGE (PIN_GEN | (0x6 << 16))
670 #define PIN_IRQ_BOTHEDGES (PIN_GEN | (0x7 << 16))
671 #define PIN_BM_IRQ (0x7 << 16)
672 
673 #define PIN_BM_ALL (PIN_BM_INPUT_MODE | PIN_BM_OUTPUT_MODE | PIN_BM_INV_INOUT | PIN_BM_IRQ)
675 
685 typedef struct PIN_State_s PIN_State;
686 
687 
691 typedef PIN_State *PIN_Handle;
692 
693 
703 typedef void (*PIN_IntCb)(PIN_Handle handle, PIN_Id pinId);
704 
705 
708 struct PIN_State_s {
710  uint32_t portMask;
711  uintptr_t userArg;
712  // TODO: add driver-specific field for extensions?
713 };
714 
716 typedef enum {
720  PIN_UNSUPPORTED = 3
721 } PIN_Status;
722 
723 
739 extern PIN_Status PIN_init(const PIN_Config aPinCfg[]);
740 
741 
757 extern PIN_Handle PIN_open(PIN_State *state, const PIN_Config pinList[]);
758 
759 
768 extern PIN_Status PIN_add(PIN_Handle handle, PIN_Config pinCfg);
769 
770 
779 extern PIN_Status PIN_remove(PIN_Handle handle, PIN_Id pinId);
780 
781 
789 extern void PIN_close(PIN_Handle handle);
790 
791 
799 static inline void PIN_setUserArg(PIN_Handle handle, uintptr_t arg) {
800  if (handle) {
801  handle->userArg = arg;
802  }
803 }
804 
805 
813 static inline uintptr_t PIN_getUserArg(PIN_Handle handle) {
814  return handle->userArg;
815 }
816 
817 
836 extern uint32_t PIN_getInputValue(PIN_Id pinId);
837 
838 
855 extern PIN_Status PIN_setOutputEnable(PIN_Handle handle, PIN_Id pinId, bool outputEnable);
856 
857 
871 extern PIN_Status PIN_setOutputValue(PIN_Handle handle, PIN_Id pinId, uint32_t val);
872 
873 
886 extern uint32_t PIN_getOutputValue(PIN_Id pinId);
887 
888 
906 extern PIN_Status PIN_setInterrupt(PIN_Handle handle, PIN_Config pinCfg);
907 
908 
919 extern PIN_Status PIN_clrPendInterrupt(PIN_Handle handle, PIN_Id pinId);
920 
921 
942 extern PIN_Status PIN_registerIntCb(PIN_Handle handle, PIN_IntCb callbackFxn);
943 
944 
945 
965 extern PIN_Config PIN_getConfig(PIN_Id pinId);
966 
967 
981 extern PIN_Status PIN_setConfig(PIN_Handle handle, PIN_Config updateMask, PIN_Config pinCfg);
982 
983 
1006 extern uint32_t PIN_getPortMask(PIN_Handle handle);
1007 
1008 
1018 extern uint32_t PIN_getPortInputValue(PIN_Handle handle);
1019 
1020 
1031 extern uint32_t PIN_getPortOutputValue(PIN_Handle handle);
1032 
1033 
1050 extern PIN_Status PIN_setPortOutputValue(PIN_Handle handle, uint32_t outputValueMask);
1051 
1052 
1070 extern PIN_Status PIN_setPortOutputEnable(PIN_Handle handle, uint32_t outputEnableMask);
1071 
1072 
1076 #ifdef __cplusplus
1077 }
1078 #endif
1079 #endif /* ti_drivers_PIN__include */
uint8_t PIN_Id
Pin identifier data type.
Definition: PIN.h:577
PIN_Status PIN_setInterrupt(PIN_Handle handle, PIN_Config pinCfg)
Control interrupt enable and edge for pin.
Operation not supported.
Definition: PIN.h:719
PIN_Status PIN_setOutputValue(PIN_Handle handle, PIN_Id pinId, uint32_t val)
Control output value for GPIO pin.
PIN_Status PIN_setPortOutputValue(PIN_Handle handle, uint32_t outputValueMask)
Simultaneous write output buffer values of all allocated pins in GPIO port.
PIN_Status PIN_setConfig(PIN_Handle handle, PIN_Config updateMask, PIN_Config pinCfg)
Sets complete pin configuration.
PIN_Status PIN_add(PIN_Handle handle, PIN_Config pinCfg)
Add pin to pin set for open PIN handle.
PIN_Status PIN_init(const PIN_Config aPinCfg[])
PIN module initialization.
uint32_t PIN_getInputValue(PIN_Id pinId)
Get pin input value (0/1)
uint32_t portMask
Bitmask for pins allocated in port.
Definition: PIN.h:709
void(* PIN_IntCb)(PIN_Handle handle, PIN_Id pinId)
I/O Interrupt callback function pointer type One PIN Interrupt callback can be registered by each PIN...
Definition: PIN.h:702
PIN_Handle PIN_open(PIN_State *state, const PIN_Config pinList[])
Allocate one or more pins for a driver or an application.
uintptr_t userArg
User argument for whole handle.
Definition: PIN.h:710
PIN_Config PIN_getConfig(PIN_Id pinId)
Returns pin configuration.
PIN_State * PIN_Handle
A handle that is returned from a PIN_open() call Used for further PIN client interaction with the PIN...
Definition: PIN.h:690
PIN_Status PIN_remove(PIN_Handle handle, PIN_Id pinId)
Removes pin from pin set foropen PIN handle.
uint32_t PIN_getPortInputValue(PIN_Handle handle)
Read input value of whole GPIO port.
underlying data structure for type PIN_State
Definition: PIN.h:707
PIN_Status PIN_setPortOutputEnable(PIN_Handle handle, uint32_t outputEnableMask)
Set output enable for all pins allocated to client in GPIO port.
PIN_Status PIN_setOutputEnable(PIN_Handle handle, PIN_Id pinId, bool outputEnable)
Control output enable for GPIO pin.
PIN_Status
Return value for many functions in the PIN driver interface.
Definition: PIN.h:715
Operation succeeded.
Definition: PIN.h:716
void PIN_close(PIN_Handle handle)
Deallocate all pins previously allocated with a call to PIN_open().
uint32_t PIN_getOutputValue(PIN_Id pinId)
Get value of GPIO pin output buffer.
uint32_t PIN_getPortOutputValue(PIN_Handle handle)
Returns value of whole GPIO port&#39;s output buffers.
PIN_Status PIN_clrPendInterrupt(PIN_Handle handle, PIN_Id pinId)
Clear pending interrupt for pin, if any.
PIN_Status PIN_registerIntCb(PIN_Handle handle, PIN_IntCb callbackFxn)
Register callback function for a set of pins.
Operation failed, client does not have access to pin.
Definition: PIN.h:718
Operation failed, some pin already allocated.
Definition: PIN.h:717
uint32_t PIN_getPortMask(PIN_Handle handle)
Returns bitmask indicating pins allocated to client in GPIO port.
PIN_IntCb callbackFxn
Pointer to interrupt callback function.
Definition: PIN.h:708
uint32_t PIN_Config
Pin configuration data type with embedded pin identifier.
Definition: PIN.h:597
static void PIN_setUserArg(PIN_Handle handle, uintptr_t arg)
Sets a user argument associated with the handle.
Definition: PIN.h:798
static uintptr_t PIN_getUserArg(PIN_Handle handle)
Gets a user argument associated with the handle.
Definition: PIN.h:812
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale