RTEMS 6.1-rc1
gpio.h
Go to the documentation of this file.
1
9/*
10 * Copyright (c) 2014-2015 Andre Marques <andre.lousa.marques at gmail.com>
11 *
12 * The license and distribution terms for this file may be
13 * found in the file LICENSE in this distribution or at
14 * http://www.rtems.org/license/LICENSE.
15 */
16
17#ifndef LIBBSP_SHARED_GPIO_H
18#define LIBBSP_SHARED_GPIO_H
19
20#include <bsp.h>
21#include <rtems.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif /* __cplusplus */
26
27#if !defined(BSP_GPIO_PIN_COUNT) || !defined(BSP_GPIO_PINS_PER_BANK)
28 #error "BSP_GPIO_PIN_COUNT or BSP_GPIO_PINS_PER_BANK is not defined."
29#endif
30
31#if BSP_GPIO_PIN_COUNT <= 0 || BSP_GPIO_PINS_PER_BANK <= 0
32 #error "Invalid BSP_GPIO_PIN_COUNT or BSP_GPIO_PINS_PER_BANK."
33#endif
34
35#if BSP_GPIO_PINS_PER_BANK > 32
36 #error "Invalid BSP_GPIO_PINS_PER_BANK. Must be in the range of 1 to 32."
37#endif
38
39#define GPIO_LAST_BANK_PINS BSP_GPIO_PIN_COUNT % BSP_GPIO_PINS_PER_BANK
40
41#if GPIO_LAST_BANK_PINS > 0
42 #define GPIO_BANK_COUNT (BSP_GPIO_PIN_COUNT / BSP_GPIO_PINS_PER_BANK) + 1
43#else
44 #define GPIO_BANK_COUNT BSP_GPIO_PIN_COUNT / BSP_GPIO_PINS_PER_BANK
45 #undef GPIO_LAST_BANK_PINS
46 #define GPIO_LAST_BANK_PINS BSP_GPIO_PINS_PER_BANK
47#endif
48
49#if defined(BSP_GPIO_PINS_PER_SELECT_BANK) && BSP_GPIO_PINS_PER_SELECT_BANK > 32
50 #error "Invalid BSP_GPIO_PINS_PER_SELECT_BANK. Must under and including 32."
51#elif defined(BSP_GPIO_PINS_PER_SELECT_BANK) <= 32
52 #define GPIO_SELECT_BANK_COUNT \
53 BSP_GPIO_PINS_PER_BANK / BSP_GPIO_PINS_PER_SELECT_BANK
54#endif
55
56#define INTERRUPT_SERVER_PRIORITY 1
57#define INTERRUPT_SERVER_STACK_SIZE 2 * RTEMS_MINIMUM_STACK_SIZE
58#define INTERRUPT_SERVER_MODES RTEMS_TIMESLICE | RTEMS_PREEMPT
59#define INTERRUPT_SERVER_ATTRIBUTES RTEMS_DEFAULT_ATTRIBUTES
60
61#define GPIO_INPUT_ERROR ~0
62
75typedef enum
76{
77 PULL_UP = 1,
78 PULL_DOWN,
79 NO_PULL_RESISTOR
81
87typedef enum
88{
89 DIGITAL_INPUT = 0,
91 BSP_SPECIFIC,
92 NOT_USED
94
100typedef enum
101{
102 FALLING_EDGE = 0,
103 RISING_EDGE,
104 LOW_LEVEL,
105 HIGH_LEVEL,
106 BOTH_EDGES,
107 BOTH_LEVELS,
108 NONE
110
117typedef enum
118{
119 IRQ_HANDLED,
120 IRQ_NONE
122
129typedef enum
130{
131 SHARED_HANDLER,
132 UNIQUE_HANDLER
134
141typedef struct
142{
143 /* The BSP defined function code. */
144 uint32_t io_function;
145
146 void *pin_data;
148
153typedef struct
154{
155 rtems_gpio_interrupt active_interrupt;
156
157 rtems_gpio_handler_flag handler_flag;
158
159 bool threaded_interrupts;
160
161 /* Interrupt handler function. */
162 rtems_gpio_irq_state (*handler) (void *arg);
163
164 /* Interrupt handler function arguments. */
165 void *arg;
166
167 /* Software switch debounce settings. It should contain the amount of clock
168 * ticks that must pass between interrupts to ensure that the interrupt
169 * was not caused by a switch bounce.
170 * If set to 0 this feature is disabled . */
171 uint32_t debounce_clock_tick_interval;
173
178typedef struct
179{
180 /* Processor pin number. */
181 uint32_t pin_number;
182 rtems_gpio_function function;
183
184 /* Pull resistor setting. */
185 rtems_gpio_pull_mode pull_mode;
186
187 /* If digital out pin, set to TRUE to set the pin to logical high,
188 * or FALSE for logical low. If not a digital out then this
189 * is ignored. */
190 bool output_enabled;
191
192 /* If true inverts digital in/out applicational logic. */
193 bool logic_invert;
194
195 /* Pin interrupt configuration. Should be NULL if not used. */
197
198 /* Structure with BSP specific data, to use during the pin request.
199 * If function == BSP_SPECIFIC this should have a pointer to
200 * a rtems_gpio_specific_data structure.
201 *
202 * If not this field may be NULL. This is passed to the BSP function
203 * so any BSP specific data can be passed to it through this pointer. */
204 void *bsp_specific;
206
212typedef struct
213{
214 /* Global GPIO pin number. */
215 uint32_t pin_number;
216
217 /* RTEMS GPIO pin function code. */
218 rtems_gpio_function function;
219
220 /* BSP specific function code. Only used if function == BSP_SPECIFIC */
221 uint32_t io_function;
222
223 /* BSP specific data. */
224 void *bsp_specific;
226
231typedef struct
232{
233 const rtems_gpio_pin_conf *digital_inputs;
234 uint32_t input_count;
235
236 const rtems_gpio_pin_conf *digital_outputs;
237 uint32_t output_count;
238
239 const rtems_gpio_pin_conf *bsp_specifics;
240 uint32_t bsp_specific_pin_count;
242
247
263
271
287extern rtems_status_code rtems_gpio_define_pin_group(
288 const rtems_gpio_group_definition *group_definition,
289 rtems_gpio_group *group
290);
291
304 uint32_t data,
305 rtems_gpio_group *group
306);
307
318extern uint32_t rtems_gpio_read_group(rtems_gpio_group *group);
319
334 rtems_gpio_group *group,
335 void *arg
336);
337
348 const rtems_gpio_pin_conf *conf
349);
350
363 const rtems_gpio_pin_conf *conf
364);
365
379 uint32_t *pin_numbers,
380 uint32_t pin_count
381);
382
396 uint32_t *pin_numbers,
397 uint32_t pin_count
398);
399
410extern uint32_t rtems_gpio_multi_read(
411 uint32_t *pin_numbers,
412 uint32_t pin_count
413);
414
426extern rtems_status_code rtems_gpio_set(uint32_t pin_number);
427
439extern rtems_status_code rtems_gpio_clear(uint32_t pin_number);
440
450extern int rtems_gpio_get_value(uint32_t pin_number);
451
470extern rtems_status_code rtems_gpio_multi_select(
471 const rtems_gpio_pin_conf *pins,
472 uint8_t pin_count
473);
474
494extern rtems_status_code rtems_gpio_request_pin(
495 uint32_t pin_number,
496 rtems_gpio_function function,
497 bool output_enable,
498 bool logic_invert,
499 void *bsp_specific
500);
501
513 uint32_t pin_number,
515);
516
527extern rtems_status_code rtems_gpio_release_pin(uint32_t pin_number);
528
539 const rtems_gpio_pin_conf *conf
540);
541
552extern rtems_status_code rtems_gpio_release_multiple_pins(
553 const rtems_gpio_pin_conf *pins,
554 uint32_t pin_count
555);
556
568 rtems_gpio_group *group
569);
570
590 uint32_t pin_number,
591 int ticks
592);
593
614 uint32_t pin_number,
615 rtems_gpio_irq_state (*handler) (void *arg),
616 void *arg
617);
618
646extern rtems_status_code rtems_gpio_enable_interrupt(
647 uint32_t pin_number,
648 rtems_gpio_interrupt interrupt,
650 bool threaded_handling,
651 rtems_gpio_irq_state (*handler) (void *arg),
652 void *arg
653);
654
670 uint32_t pin_number,
671 rtems_gpio_irq_state (*handler) (void *arg),
672 void *arg
673);
674
689extern rtems_status_code rtems_gpio_disable_interrupt(uint32_t pin_number);
690
702 uint32_t bank,
703 uint32_t bitmask
704);
705
717 uint32_t bank,
718 uint32_t bitmask
719);
720
732extern uint32_t rtems_gpio_bsp_multi_read(uint32_t bank, uint32_t bitmask);
733
751extern rtems_status_code rtems_gpio_bsp_specific_group_operation(
752 uint32_t bank,
753 uint32_t *pins,
754 uint32_t pin_count,
755 void *arg
756);
757
775extern rtems_status_code rtems_gpio_bsp_multi_select(
777 uint32_t pin_count,
778 uint32_t select_bank
779);
780
791extern rtems_status_code rtems_gpio_bsp_set(uint32_t bank, uint32_t pin);
792
803extern rtems_status_code rtems_gpio_bsp_clear(uint32_t bank, uint32_t pin);
804
816extern uint32_t rtems_gpio_bsp_get_value(uint32_t bank, uint32_t pin);
817
831 uint32_t bank,
832 uint32_t pin,
833 void *bsp_specific
834);
835
849 uint32_t bank,
850 uint32_t pin,
851 void *bsp_specific
852);
853
868 uint32_t bank,
869 uint32_t pin,
870 uint32_t function,
871 void *pin_data
872);
873
886 uint32_t bank,
887 uint32_t pin,
889);
890
904
914
927 uint32_t bank,
928 uint32_t pin,
929 rtems_gpio_interrupt interrupt
930);
931
944 uint32_t bank,
945 uint32_t pin,
946 rtems_gpio_interrupt interrupt
947);
948
951#ifdef __cplusplus
952}
953#endif /* __cplusplus */
954
955#endif /* LIBBSP_SHARED_GPIO_H */
rtems_status_code rtems_gpio_resistor_mode(uint32_t pin_number, rtems_gpio_pull_mode mode)
Configures a single GPIO pin pull resistor.
Definition: gpio-support.c:1383
rtems_gpio_function
The set of possible functions a pin can have.
Definition: gpio.h:88
@ DIGITAL_OUTPUT
Assigns a certain function to a GPIO pin.
Definition: gpio.h:90
rtems_status_code rtems_gpio_group_bsp_specific_operation(rtems_gpio_group *group, void *arg)
Performs a BSP specific operation on a group of pins. The pins order is as defined in the group defin...
Definition: gpio-support.c:941
rtems_status_code rtems_gpio_bsp_select_specific_io(uint32_t bank, uint32_t pin, uint32_t function, void *pin_data)
Assigns a BSP specific function to the given pin. This must be implemented by each BSP.
Definition: rpi-gpio.c:128
rtems_gpio_irq_state
The set of possible handled states an user-defined interrupt handler can return.
Definition: gpio.h:118
rtems_status_code rtems_gpio_bsp_select_input(uint32_t bank, uint32_t pin, void *bsp_specific)
Assigns the digital input function to the given pin. This must be implemented by each BSP.
Definition: rpi-gpio.c:112
uint32_t rtems_gpio_read_group(rtems_gpio_group *group)
Reads the value/level of the group's digital inputs. The pins order is as defined in the group defini...
Definition: gpio-support.c:905
rtems_status_code rtems_gpio_release_pin(uint32_t pin_number)
Releases a GPIO pin, making it available to be used again.
Definition: gpio-support.c:1428
rtems_status_code rtems_gpio_disable_interrupt(uint32_t pin_number)
Stops interrupts from being generated on a given GPIO pin and removes the corresponding handler.
Definition: gpio-support.c:1888
rtems_status_code rtems_gpio_multi_set(uint32_t *pin_numbers, uint32_t pin_count)
Sets multiple output GPIO pins with the logical high.
Definition: gpio-support.c:1103
rtems_status_code rtems_gpio_bsp_enable_interrupt(uint32_t bank, uint32_t pin, rtems_gpio_interrupt interrupt)
Enables interrupts to be generated on a given GPIO pin. This must be implemented by each BSP.
Definition: rpi-gpio.c:192
rtems_status_code rtems_gpio_initialize(void)
Initializes the GPIO API.
Definition: gpio-support.c:640
rtems_status_code rtems_gpio_bsp_clear(uint32_t bank, uint32_t pin)
Sets an output GPIO pin with the logical low. This must be implemented by each BSP.
Definition: rpi-gpio.c:100
rtems_gpio_handler_flag
The set of flags to specify an user-defined interrupt handler uniqueness on a GPIO pin.
Definition: gpio.h:130
rtems_status_code rtems_gpio_interrupt_handler_remove(uint32_t pin_number, rtems_gpio_irq_state(*handler)(void *arg), void *arg)
Disconnects an user-defined interrupt handler from the given pin. If in the end there are no more use...
Definition: gpio-support.c:1826
int rtems_gpio_get_value(uint32_t pin_number)
Returns the value (level) of a GPIO input pin.
Definition: gpio-support.c:1253
uint32_t rtems_gpio_bsp_get_value(uint32_t bank, uint32_t pin)
Returns the value (level) of a GPIO input pin. This must be implemented by each BSP.
Definition: rpi-gpio.c:107
rtems_status_code rtems_gpio_bsp_multi_set(uint32_t bank, uint32_t bitmask)
Sets multiple output GPIO pins with the logical high. This must be implemented by each BSP.
Definition: rpi-gpio.c:74
rtems_status_code rtems_gpio_update_configuration(const rtems_gpio_pin_conf *conf)
Updates the current configuration of a GPIO pin .
Definition: gpio-support.c:1008
rtems_status_code rtems_gpio_bsp_set_resistor_mode(uint32_t bank, uint32_t pin, rtems_gpio_pull_mode mode)
Configures a single GPIO pin pull resistor. This must be implemented by each BSP.
Definition: rpi-gpio.c:137
rtems_gpio_group * rtems_gpio_create_pin_group(void)
Instantiates a GPIO pin group. To define the group.
Definition: gpio-support.c:680
rtems_status_code rtems_gpio_bsp_disable_interrupt(uint32_t bank, uint32_t pin, rtems_gpio_interrupt interrupt)
Stops interrupts from being generated on a given GPIO pin. This must be implemented by each BSP.
Definition: rpi-gpio.c:236
rtems_gpio_interrupt
The set of possible interrupts a GPIO pin can generate.
Definition: gpio.h:101
rtems_status_code rtems_gpio_bsp_select_output(uint32_t bank, uint32_t pin, void *bsp_specific)
Assigns the digital output function to the given pin. This must be implemented by each BSP.
Definition: rpi-gpio.c:120
uint32_t rtems_gpio_bsp_multi_read(uint32_t bank, uint32_t bitmask)
Returns the value (level) of multiple GPIO input pins. This must be implemented by each BSP.
Definition: rpi-gpio.c:88
rtems_status_code rtems_gpio_write_group(uint32_t data, rtems_gpio_group *group)
Writes a value to the group's digital outputs. The pins order is as defined in the group definition.
Definition: gpio-support.c:840
uint32_t rtems_gpio_bsp_interrupt_line(rtems_vector_number vector)
Reads and returns a vector/bank interrupt event line. The bitmask should indicate with a 1 if the cor...
Definition: rpi-gpio.c:179
rtems_status_code rtems_gpio_debounce_switch(uint32_t pin_number, int ticks)
Attaches a debouncing function to a given pin/switch. Debouncing is done by requiring a certain numbe...
Definition: gpio-support.c:1567
rtems_status_code rtems_gpio_release_pin_group(rtems_gpio_group *group)
Releases a GPIO pin group, making the pins used available to be repurposed.
Definition: gpio-support.c:1498
rtems_status_code rtems_gpio_request_configuration(const rtems_gpio_pin_conf *conf)
Requests a GPIO pin configuration.
Definition: gpio-support.c:980
uint32_t rtems_gpio_multi_read(uint32_t *pin_numbers, uint32_t pin_count)
Returns the value (level) of multiple GPIO input pins.
Definition: gpio-support.c:1149
rtems_status_code rtems_gpio_clear(uint32_t pin_number)
Sets an output GPIO pin with the logical low.
Definition: gpio-support.c:1213
rtems_status_code rtems_gpio_interrupt_handler_install(uint32_t pin_number, rtems_gpio_irq_state(*handler)(void *arg), void *arg)
Connects a new user-defined interrupt handler to a given pin.
Definition: gpio-support.c:1602
rtems_status_code rtems_gpio_bsp_set(uint32_t bank, uint32_t pin)
Sets an output GPIO pin with the logical high. This must be implemented by each BSP.
Definition: rpi-gpio.c:93
rtems_gpio_pull_mode
The set of possible configurations for a GPIO pull-up resistor.
Definition: gpio.h:76
rtems_status_code rtems_gpio_release_configuration(const rtems_gpio_pin_conf *conf)
Releases a GPIO pin, making it available to be used again.
Definition: gpio-support.c:1466
rtems_status_code rtems_gpio_bsp_multi_clear(uint32_t bank, uint32_t bitmask)
Sets multiple output GPIO pins with the logical low. This must be implemented by each BSP.
Definition: rpi-gpio.c:81
rtems_status_code rtems_gpio_set(uint32_t pin_number)
Sets an output GPIO pin with the logical high.
Definition: gpio-support.c:1173
rtems_vector_number rtems_gpio_bsp_get_vector(uint32_t bank)
Calculates a vector number for a given GPIO bank. This must be implemented by each BSP.
Definition: rpi-gpio.c:174
rtems_status_code rtems_gpio_multi_clear(uint32_t *pin_numbers, uint32_t pin_count)
Sets multiple output GPIO pins with the logical low.
Definition: gpio-support.c:1126
ISR_Vector_number rtems_vector_number
This integer type represents interrupt vector numbers.
Definition: intr.h:102
rtems_status_code
This enumeration provides status codes for directives of the Classic API.
Definition: status.h:85
This header file defines the RTEMS Classic API.
Object containing configuration information to request a GPIO pin group.
Definition: gpio.h:232
Object containing relevant information about a GPIO group.
Definition: gpio-support.c:56
Object containing configuration information regarding interrupts.
Definition: gpio.h:154
Object containing configuration information to assign GPIO functions to multiple pins at the same tim...
Definition: gpio.h:213
Object containing configuration information to request/update a GPIO pin.
Definition: gpio.h:179
Object containing relevant information for assigning a BSP specific function to a pin.
Definition: gpio.h:142