Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:42

0001 /**
0002  * @file
0003  *
0004  * @ingroup rtems_gpio
0005  *
0006  * @brief RTEMS GPIO API definition.
0007  */
0008 
0009 /*
0010  *  Copyright (c) 2014-2015 Andre Marques <andre.lousa.marques at gmail.com>
0011  *
0012  *  The license and distribution terms for this file may be
0013  *  found in the file LICENSE in this distribution or at
0014  *  http://www.rtems.org/license/LICENSE.
0015  */
0016 
0017 #ifndef LIBBSP_SHARED_GPIO_H
0018 #define LIBBSP_SHARED_GPIO_H
0019 
0020 #include <bsp.h>
0021 #include <rtems.h>
0022 
0023 #ifdef __cplusplus
0024 extern "C" {
0025 #endif /* __cplusplus */
0026 
0027 #if !defined(BSP_GPIO_PIN_COUNT) || !defined(BSP_GPIO_PINS_PER_BANK)
0028   #error "BSP_GPIO_PIN_COUNT or BSP_GPIO_PINS_PER_BANK is not defined."
0029 #endif
0030 
0031 #if BSP_GPIO_PIN_COUNT <= 0 || BSP_GPIO_PINS_PER_BANK <= 0
0032   #error "Invalid BSP_GPIO_PIN_COUNT or BSP_GPIO_PINS_PER_BANK."
0033 #endif
0034 
0035 #if BSP_GPIO_PINS_PER_BANK > 32
0036   #error "Invalid BSP_GPIO_PINS_PER_BANK. Must be in the range of 1 to 32."
0037 #endif
0038 
0039 #define GPIO_LAST_BANK_PINS BSP_GPIO_PIN_COUNT % BSP_GPIO_PINS_PER_BANK
0040 
0041 #if GPIO_LAST_BANK_PINS > 0
0042   #define GPIO_BANK_COUNT (BSP_GPIO_PIN_COUNT / BSP_GPIO_PINS_PER_BANK) + 1
0043 #else
0044   #define GPIO_BANK_COUNT BSP_GPIO_PIN_COUNT / BSP_GPIO_PINS_PER_BANK
0045   #undef GPIO_LAST_BANK_PINS
0046   #define GPIO_LAST_BANK_PINS BSP_GPIO_PINS_PER_BANK
0047 #endif
0048 
0049 #if defined(BSP_GPIO_PINS_PER_SELECT_BANK) && BSP_GPIO_PINS_PER_SELECT_BANK > 32
0050   #error "Invalid BSP_GPIO_PINS_PER_SELECT_BANK. Must under and including 32."
0051 #elif defined(BSP_GPIO_PINS_PER_SELECT_BANK) <= 32
0052   #define GPIO_SELECT_BANK_COUNT \
0053     BSP_GPIO_PINS_PER_BANK / BSP_GPIO_PINS_PER_SELECT_BANK
0054 #endif
0055 
0056 #define INTERRUPT_SERVER_PRIORITY 1
0057 #define INTERRUPT_SERVER_STACK_SIZE 2 * RTEMS_MINIMUM_STACK_SIZE
0058 #define INTERRUPT_SERVER_MODES RTEMS_TIMESLICE | RTEMS_PREEMPT
0059 #define INTERRUPT_SERVER_ATTRIBUTES RTEMS_DEFAULT_ATTRIBUTES
0060 
0061 #define GPIO_INPUT_ERROR ~0
0062 
0063 /**
0064  * @name GPIO data structures
0065  *
0066  * @{
0067  */
0068 
0069 /**
0070  * @brief The set of possible configurations for a GPIO pull-up resistor.
0071  *
0072  * Enumerated type to define the possible pull-up resistor configurations
0073  * for a GPIO pin.
0074  */
0075 typedef enum
0076 {
0077   PULL_UP = 1,
0078   PULL_DOWN,
0079   NO_PULL_RESISTOR
0080 } rtems_gpio_pull_mode;
0081 
0082 /**
0083  * @brief The set of possible functions a pin can have.
0084  *
0085  * Enumerated type to define a pin function.
0086  */
0087 typedef enum
0088 {
0089   DIGITAL_INPUT = 0,
0090   DIGITAL_OUTPUT,
0091   BSP_SPECIFIC,
0092   NOT_USED
0093 } rtems_gpio_function;
0094 
0095 /**
0096  * @brief The set of possible interrupts a GPIO pin can generate.
0097  *
0098  * Enumerated type to define a GPIO pin interrupt.
0099  */
0100 typedef enum
0101 {
0102   FALLING_EDGE = 0,
0103   RISING_EDGE,
0104   LOW_LEVEL,
0105   HIGH_LEVEL,
0106   BOTH_EDGES,
0107   BOTH_LEVELS,
0108   NONE
0109 } rtems_gpio_interrupt;
0110 
0111 /**
0112  * @brief The set of possible handled states an user-defined interrupt
0113  *        handler can return.
0114  *
0115  * Enumerated type to define an interrupt handler handled state.
0116  */
0117 typedef enum
0118 {
0119   IRQ_HANDLED,
0120   IRQ_NONE
0121 } rtems_gpio_irq_state;
0122 
0123 /**
0124  * @brief The set of flags to specify an user-defined interrupt handler
0125  *        uniqueness on a GPIO pin.
0126  *
0127  * Enumerated type to define an interrupt handler shared flag.
0128  */
0129 typedef enum
0130 {
0131   SHARED_HANDLER,
0132   UNIQUE_HANDLER
0133 } rtems_gpio_handler_flag;
0134 
0135 /**
0136  * @brief Object containing relevant information for assigning a BSP specific
0137  *        function to a pin.
0138  *
0139  * Encapsulates relevant data for a BSP specific GPIO function.
0140  */
0141 typedef struct
0142 {
0143   /* The BSP defined function code. */
0144   uint32_t io_function;
0145 
0146   void *pin_data;
0147 } rtems_gpio_specific_data;
0148 
0149 /**
0150  * @brief Object containing configuration information
0151  *        regarding interrupts.
0152  */
0153 typedef struct
0154 {
0155   rtems_gpio_interrupt active_interrupt;
0156 
0157   rtems_gpio_handler_flag handler_flag;
0158 
0159   bool threaded_interrupts;
0160 
0161   /* Interrupt handler function. */
0162   rtems_gpio_irq_state (*handler) (void *arg);
0163 
0164   /* Interrupt handler function arguments. */
0165   void *arg;
0166 
0167   /* Software switch debounce settings. It should contain the amount of clock
0168    * ticks that must pass between interrupts to ensure that the interrupt
0169    * was not caused by a switch bounce.
0170    * If set to 0 this feature is disabled . */
0171   uint32_t debounce_clock_tick_interval;
0172 } rtems_gpio_interrupt_configuration;
0173 
0174 /**
0175  * @brief Object containing configuration information
0176  *        to request/update a GPIO pin.
0177  */
0178 typedef struct
0179 {
0180   /* Processor pin number. */
0181   uint32_t pin_number;
0182   rtems_gpio_function function;
0183 
0184   /* Pull resistor setting. */
0185   rtems_gpio_pull_mode pull_mode;
0186 
0187   /* If digital out pin, set to TRUE to set the pin to logical high,
0188    * or FALSE for logical low. If not a digital out then this
0189    * is ignored. */
0190   bool output_enabled;
0191 
0192   /* If true inverts digital in/out applicational logic. */
0193   bool logic_invert;
0194 
0195   /* Pin interrupt configuration. Should be NULL if not used. */
0196   rtems_gpio_interrupt_configuration *interrupt;
0197 
0198   /* Structure with BSP specific data, to use during the pin request.
0199    * If function == BSP_SPECIFIC this should have a pointer to
0200    * a rtems_gpio_specific_data structure.
0201    *
0202    * If not this field may be NULL. This is passed to the BSP function
0203    * so any BSP specific data can be passed to it through this pointer. */
0204   void *bsp_specific;
0205 } rtems_gpio_pin_conf;
0206 
0207 /**
0208  * @brief Object containing configuration information
0209  *        to assign GPIO functions to multiple pins
0210  *        at the same time. To be used by BSP code only.
0211  */
0212 typedef struct
0213 {
0214   /* Global GPIO pin number. */
0215   uint32_t pin_number;
0216 
0217   /* RTEMS GPIO pin function code. */
0218   rtems_gpio_function function;
0219 
0220   /* BSP specific function code. Only used if function == BSP_SPECIFIC */
0221   uint32_t io_function;
0222 
0223   /* BSP specific data. */
0224   void *bsp_specific;
0225 } rtems_gpio_multiple_pin_select;
0226 
0227 /**
0228  * @brief Object containing configuration information
0229  *        to request a GPIO pin group.
0230  */
0231 typedef struct
0232 {
0233   const rtems_gpio_pin_conf *digital_inputs;
0234   uint32_t input_count;
0235 
0236   const rtems_gpio_pin_conf *digital_outputs;
0237   uint32_t output_count;
0238 
0239   const rtems_gpio_pin_conf *bsp_specifics;
0240   uint32_t bsp_specific_pin_count;
0241 } rtems_gpio_group_definition;
0242 
0243 /**
0244  * @brief Opaque type for a GPIO pin group.
0245  */
0246 typedef struct rtems_gpio_group rtems_gpio_group;
0247 
0248 /** @} */
0249 
0250 /**
0251  * @name gpio Usage
0252  *
0253  * @{
0254  */
0255 
0256 /**
0257  * @brief Initializes the GPIO API.
0258  *
0259  * @retval RTEMS_SUCCESSFUL API successfully initialized.
0260  * @retval * @see rtems_semaphore_create().
0261  */
0262 extern rtems_status_code rtems_gpio_initialize(void);
0263 
0264 /**
0265  * @brief Instantiates a GPIO pin group.
0266  *        To define the group @see rtems_gpio_define_pin_group().
0267  *
0268  * @retval rtems_gpio_group pointer.
0269  */
0270 extern rtems_gpio_group *rtems_gpio_create_pin_group(void);
0271 
0272 /**
0273  * @brief Requests a GPIO pin group configuration.
0274  *
0275  * @param[in] group_definition rtems_gpio_group_definition structure filled with
0276  *                             the group pins configurations.
0277  * @param[out] group Reference to the created group.
0278  *
0279  * @retval RTEMS_SUCCESSFUL Pin group was configured successfully.
0280  * @retval RTEMS_UNSATISFIED @var group_definition or @var group is NULL,
0281  *                           the @var pins are not from the same bank,
0282  *                           no pins were defined or could not satisfy at
0283  *                           least one given configuration.
0284  * @retval RTEMS_RESOURCE_IN_USE At least one pin is already being used.
0285  * @retval * @see rtems_semaphore_create().
0286  */
0287 extern rtems_status_code rtems_gpio_define_pin_group(
0288   const rtems_gpio_group_definition *group_definition,
0289   rtems_gpio_group *group
0290 );
0291 
0292 /**
0293  * @brief Writes a value to the group's digital outputs. The pins order
0294  *        is as defined in the group definition.
0295  *
0296  * @param[in] data Data to write/send.
0297  * @param[in] group Reference to the group.
0298  *
0299  * @retval RTEMS_SUCCESSFUL Data successfully written.
0300  * @retval RTEMS_NOT_DEFINED Group has no output pins.
0301  * @retval RTEMS_UNSATISFIED Could not operate on at least one of the pins.
0302  */
0303 extern rtems_status_code rtems_gpio_write_group(
0304   uint32_t data,
0305   rtems_gpio_group *group
0306 );
0307 
0308 /**
0309  * @brief Reads the value/level of the group's digital inputs. The pins order
0310  *        is as defined in the group definition.
0311  *
0312  * @param[in] group Reference to the group.
0313  *
0314  * @retval The function returns a 32-bit bitmask with the group's input pins
0315  *         current logical values.
0316  * @retval GPIO_INPUT_ERROR Group has no input pins.
0317  */
0318 extern uint32_t rtems_gpio_read_group(rtems_gpio_group *group);
0319 
0320 /**
0321  * @brief Performs a BSP specific operation on a group of pins. The pins order
0322  *        is as defined in the group definition.
0323  *
0324  * @param[in] group Reference to the group.
0325  * @param[in] arg Pointer to a BSP defined structure with BSP-specific
0326  *                data. This field is handled by the BSP.
0327  *
0328  * @retval RTEMS_SUCCESSFUL Operation completed with success.
0329  * @retval RTEMS_NOT_DEFINED Group has no BSP specific pins, or the BSP does not
0330  *                           support BSP specific operations for groups.
0331  * @retval RTEMS_UNSATISFIED Could not operate on at least one of the pins.
0332  */
0333 extern rtems_status_code rtems_gpio_group_bsp_specific_operation(
0334   rtems_gpio_group *group,
0335   void *arg
0336 );
0337 
0338 /**
0339  * @brief Requests a GPIO pin configuration.
0340  *
0341  * @param[in] conf rtems_gpio_pin_conf structure filled with the pin information
0342  *                 and desired configurations.
0343  *
0344  * @retval RTEMS_SUCCESSFUL Pin was configured successfully.
0345  * @retval RTEMS_UNSATISFIED Could not satisfy the given configuration.
0346  */
0347 extern rtems_status_code rtems_gpio_request_configuration(
0348   const rtems_gpio_pin_conf *conf
0349 );
0350 
0351 /**
0352  * @brief Updates the current configuration of a GPIO pin .
0353  *
0354  * @param[in] conf rtems_gpio_pin_conf structure filled with the pin information
0355  *                 and desired configurations.
0356  *
0357  * @retval RTEMS_SUCCESSFUL Pin configuration was updated successfully.
0358  * @retval RTEMS_INVALID_ID Pin number is invalid.
0359  * @retval RTEMS_NOT_CONFIGURED The pin is not being used.
0360  * @retval RTEMS_UNSATISFIED Could not update the pin's configuration.
0361  */
0362 extern rtems_status_code rtems_gpio_update_configuration(
0363   const rtems_gpio_pin_conf *conf
0364 );
0365 
0366 /**
0367  * @brief Sets multiple output GPIO pins with the logical high.
0368  *
0369  * @param[in] pin_numbers Array with the GPIO pin numbers to set.
0370  * @param[in] count Number of GPIO pins to set.
0371  *
0372  * @retval RTEMS_SUCCESSFUL All pins were set successfully.
0373  * @retval RTEMS_INVALID_ID At least one pin number is invalid.
0374  * @retval RTEMS_NOT_CONFIGURED At least one of the received pins
0375  *                              is not configured as a digital output.
0376  * @retval RTEMS_UNSATISFIED Could not set the GPIO pins.
0377  */
0378 extern rtems_status_code rtems_gpio_multi_set(
0379   uint32_t *pin_numbers,
0380   uint32_t pin_count
0381 );
0382 
0383 /**
0384  * @brief Sets multiple output GPIO pins with the logical low.
0385  *
0386  * @param[in] pin_numbers Array with the GPIO pin numbers to clear.
0387  * @param[in] count Number of GPIO pins to clear.
0388  *
0389  * @retval RTEMS_SUCCESSFUL All pins were cleared successfully.
0390  * @retval RTEMS_INVALID_ID At least one pin number is invalid.
0391  * @retval RTEMS_NOT_CONFIGURED At least one of the received pins
0392  *                              is not configured as a digital output.
0393  * @retval RTEMS_UNSATISFIED Could not clear the GPIO pins.
0394  */
0395 extern rtems_status_code rtems_gpio_multi_clear(
0396   uint32_t *pin_numbers,
0397   uint32_t pin_count
0398 );
0399 
0400 /**
0401  * @brief Returns the value (level) of multiple GPIO input pins.
0402  *
0403  * @param[in] pin_numbers Array with the GPIO pin numbers to read.
0404  * @param[in] count Number of GPIO pins to read.
0405  *
0406  * @retval Bitmask with the values of the corresponding pins.
0407  *         0 for logical low and 1 for logical high.
0408  * @retval GPIO_INPUT_ERROR Could not read at least one pin level.
0409  */
0410 extern uint32_t rtems_gpio_multi_read(
0411   uint32_t *pin_numbers,
0412   uint32_t pin_count
0413 );
0414 
0415 /**
0416  * @brief Sets an output GPIO pin with the logical high.
0417  *
0418  * @param[in] pin_number GPIO pin number.
0419  *
0420  * @retval RTEMS_SUCCESSFUL Pin was set successfully.
0421  * @retval RTEMS_INVALID_ID Pin number is invalid.
0422  * @retval RTEMS_NOT_CONFIGURED The received pin is not configured
0423  *                              as a digital output.
0424  * @retval RTEMS_UNSATISFIED Could not set the GPIO pin.
0425  */
0426 extern rtems_status_code rtems_gpio_set(uint32_t pin_number);
0427 
0428 /**
0429  * @brief Sets an output GPIO pin with the logical low.
0430  *
0431  * @param[in] pin_number GPIO pin number.
0432  *
0433  * @retval RTEMS_SUCCESSFUL Pin was cleared successfully.
0434  * @retval RTEMS_INVALID_ID Pin number is invalid.
0435  * @retval RTEMS_NOT_CONFIGURED The received pin is not configured
0436  *                              as a digital output.
0437  * @retval RTEMS_UNSATISFIED Could not clear the GPIO pin.
0438  */
0439 extern rtems_status_code rtems_gpio_clear(uint32_t pin_number);
0440 
0441 /**
0442  * @brief Returns the value (level) of a GPIO input pin.
0443  *
0444  * @param[in] pin_number GPIO pin number.
0445  *
0446  * @retval The function returns 0 or 1 depending on the pin current
0447  *         logical value.
0448  * @retval -1 Pin number is invalid, or not a digital input pin.
0449  */
0450 extern int rtems_gpio_get_value(uint32_t pin_number);
0451 
0452 /**
0453  * @brief Requests multiple GPIO pin configurations. If the BSP provides
0454  *        support for parallel selection each call to this function will
0455  *        result in a single call to the GPIO hardware, else each pin
0456  *        configuration will be done in individual and sequential calls.
0457  *        All pins must belong to the same GPIO bank.
0458  *
0459  * @param[in] pins Array of rtems_gpio_pin_conf structures filled with the pins
0460  *                 information and desired configurations. All pins must belong
0461  *                 to the same GPIO bank.
0462  * @param[in] pin_count Number of pin configurations in the @var pins array.
0463  *
0464  * @retval RTEMS_SUCCESSFUL All pins were configured successfully.
0465  * @retval RTEMS_INVALID_ID At least one pin number in the @var pins array
0466  *                          is invalid.
0467  * @retval RTEMS_RESOURCE_IN_USE At least one pin is already being used.
0468  * @retval RTEMS_UNSATISFIED Could not satisfy at least one given configuration.
0469  */
0470 extern rtems_status_code rtems_gpio_multi_select(
0471   const rtems_gpio_pin_conf *pins,
0472   uint8_t pin_count
0473 );
0474 
0475 /**
0476  * @brief Assigns a certain function to a GPIO pin.
0477  *
0478  * @param[in] pin_number GPIO pin number.
0479  * @param[in] function The new function for the pin.
0480  * @param[in] output_enabled If TRUE and @var function is DIGITAL_OUTPUT,
0481  *                           then the pin is set with the logical high.
0482  *                           Otherwise it is set with logical low.
0483  * @param[in] logic_invert Reverses the digital I/O logic for DIGITAL_INPUT
0484  *                         and DIGITAL_OUTPUT pins.
0485  * @param[in] bsp_specific Pointer to a BSP defined structure with BSP-specific
0486  *                         data. This field is handled by the BSP.
0487  *
0488  * @retval RTEMS_SUCCESSFUL Pin was configured successfully.
0489  * @retval RTEMS_INVALID_ID Pin number is invalid.
0490  * @retval RTEMS_RESOURCE_IN_USE The received pin is already being used.
0491  * @retval RTEMS_UNSATISFIED Could not assign the GPIO function.
0492  * @retval RTEMS_NOT_DEFINED GPIO function not defined, or NOT_USED.
0493  */
0494 extern rtems_status_code rtems_gpio_request_pin(
0495   uint32_t pin_number,
0496   rtems_gpio_function function,
0497   bool output_enable,
0498   bool logic_invert,
0499   void *bsp_specific
0500 );
0501 
0502 /**
0503  * @brief Configures a single GPIO pin pull resistor.
0504  *
0505  * @param[in] pin_number GPIO pin number.
0506  * @param[in] mode The pull resistor mode.
0507  *
0508  * @retval RTEMS_SUCCESSFUL Pull resistor successfully configured.
0509  * @retval RTEMS_INVALID_ID Pin number is invalid.
0510  * @retval RTEMS_UNSATISFIED Could not set the pull mode.
0511  */
0512 extern rtems_status_code rtems_gpio_resistor_mode(
0513   uint32_t pin_number,
0514   rtems_gpio_pull_mode mode
0515 );
0516 
0517 /**
0518  * @brief Releases a GPIO pin, making it available to be used again.
0519  *
0520  * @param[in] pin_number GPIO pin number.
0521  *
0522  * @retval RTEMS_SUCCESSFUL Pin successfully disabled.
0523  * @retval RTEMS_INVALID_ID Pin number is invalid.
0524  * @retval * Could not disable an active interrupt on this pin,
0525  *           @see rtems_gpio_disable_interrupt().
0526  */
0527 extern rtems_status_code rtems_gpio_release_pin(uint32_t pin_number);
0528 
0529 /**
0530  * @brief Releases a GPIO pin, making it available to be used again.
0531  *
0532  * @param[in] conf GPIO pin configuration to be released.
0533  *
0534  * @retval RTEMS_SUCCESSFUL Pin successfully disabled.
0535  * @retval RTEMS_UNSATISFIED Pin configuration is NULL.
0536  * @retval * @see rtems_gpio_release_pin().
0537  */
0538 extern rtems_status_code rtems_gpio_release_configuration(
0539   const rtems_gpio_pin_conf *conf
0540 );
0541 
0542 /**
0543  * @brief Releases multiple GPIO pins, making them available to be used again.
0544  *
0545  * @param[in] pins Array of rtems_gpio_pin_conf structures.
0546  * @param[in] pin_count Number of pin configurations in the @var pins array.
0547  *
0548  * @retval RTEMS_SUCCESSFUL Pins successfully disabled.
0549  * @retval RTEMS_UNSATISFIED @var pins array is NULL.
0550  * @retval * @see rtems_gpio_release_pin().
0551  */
0552 extern rtems_status_code rtems_gpio_release_multiple_pins(
0553   const rtems_gpio_pin_conf *pins,
0554   uint32_t pin_count
0555 );
0556 
0557 /**
0558  * @brief Releases a GPIO pin group, making the pins used available to be
0559  *        repurposed.
0560  *
0561  * @param[in] conf GPIO pin configuration to be released.
0562  *
0563  * @retval RTEMS_SUCCESSFUL Pins successfully disabled.
0564  * @retval * @see rtems_gpio_release_pin(), @see rtems_semaphore_delete() or
0565  *           @see rtems_semaphore_flush().
0566  */
0567 extern rtems_status_code rtems_gpio_release_pin_group(
0568   rtems_gpio_group *group
0569 );
0570 
0571 /**
0572  * @brief Attaches a debouncing function to a given pin/switch.
0573  *        Debouncing is done by requiring a certain number of clock ticks to
0574  *        pass between interrupts. Any interrupt fired too close to the last
0575  *        will be ignored as it is probably the result of an involuntary
0576  *        switch/button bounce after being released.
0577  *
0578  * @param[in] pin_number GPIO pin number.
0579  * @param[in] ticks Minimum number of clock ticks that must pass between
0580  *                  interrupts so it can be considered a legitimate
0581  *                  interrupt.
0582  *
0583  * @retval RTEMS_SUCCESSFUL Debounce function successfully attached to the pin.
0584  * @retval RTEMS_INVALID_ID Pin number is invalid.
0585  * @retval RTEMS_NOT_CONFIGURED The current pin is not configured as a digital
0586  *                              input, hence it can not be connected to a switch,
0587  *                              or interrupts are not enabled for this pin.
0588  */
0589 extern rtems_status_code rtems_gpio_debounce_switch(
0590   uint32_t pin_number,
0591   int ticks
0592 );
0593 
0594 /**
0595  * @brief Connects a new user-defined interrupt handler to a given pin.
0596  *
0597  * @param[in] pin_number GPIO pin number.
0598  * @param[in] handler Pointer to a function that will be called every time
0599  *                    the enabled interrupt for the given pin is generated.
0600  *                    This function must return information about its
0601  *                    handled/unhandled state.
0602  * @param[in] arg Void pointer to the arguments of the user-defined handler.
0603  *
0604  * @retval RTEMS_SUCCESSFUL Handler successfully connected to this pin.
0605  * @retval RTEMS_NO_MEMORY Could not connect more user-defined handlers to
0606  *                         the given pin.
0607  * @retval RTEMS_NOT_CONFIGURED The given pin has no interrupt configured.
0608  * @retval RTEMS_INVALID_ID Pin number is invalid.
0609  * @retval RTEMS_TOO_MANY The pin's current handler is set as unique.
0610  * @retval RTEMS_RESOURCE_IN_USE The current user-defined handler for this pin
0611  *                               is unique.
0612  */
0613 extern rtems_status_code rtems_gpio_interrupt_handler_install(
0614   uint32_t pin_number,
0615   rtems_gpio_irq_state (*handler) (void *arg),
0616   void *arg
0617 );
0618 
0619 /**
0620  * @brief Enables interrupts to be generated on a given GPIO pin.
0621  *        When fired that interrupt will call the given handler.
0622  *
0623  * @param[in] pin_number GPIO pin number.
0624  * @param[in] interrupt Type of interrupt to enable for the pin.
0625  * @param[in] flag Defines the uniqueness of the interrupt handler for the pin.
0626  * @param[in] threaded_handling Defines if the handler should be called from a
0627  *                              thread/task or from normal ISR contex.
0628  * @param[in] handler Pointer to a function that will be called every time
0629  *                    @var interrupt is generated. This function must return
0630  *                    information about its handled/unhandled state.
0631  * @param[in] arg Void pointer to the arguments of the user-defined handler.
0632  *
0633  * @retval RTEMS_SUCCESSFUL Interrupt successfully enabled for this pin.
0634  * @retval RTEMS_UNSATISFIED Could not install the GPIO ISR, create/start
0635  *                           the handler task, or enable the interrupt
0636  *                           on the pin.
0637  * @retval RTEMS_INVALID_ID Pin number is invalid.
0638  * @retval RTEMS_NOT_CONFIGURED The received pin is not configured
0639  *                              as a digital input, the pin is on a
0640  *                              pin grouping.
0641  * @retval RTEMS_RESOURCE_IN_USE The pin already has an enabled interrupt,
0642  *                               or the handler threading policy does not match
0643  *                               the bank's policy.
0644  * @retval RTEMS_NO_MEMORY Could not store the pin's interrupt configuration.
0645  */
0646 extern rtems_status_code rtems_gpio_enable_interrupt(
0647   uint32_t pin_number,
0648   rtems_gpio_interrupt interrupt,
0649   rtems_gpio_handler_flag flag,
0650   bool threaded_handling,
0651   rtems_gpio_irq_state (*handler) (void *arg),
0652   void *arg
0653 );
0654 
0655 /**
0656  * @brief Disconnects an user-defined interrupt handler from the given pin.
0657  *        If in the end there are no more user-defined handlers connected
0658  *        to the pin, interrupts are disabled on the given pin.
0659  *
0660  * @param[in] pin_number GPIO pin number.
0661  * @param[in] handler Pointer to the user-defined handler
0662  * @param[in] arg Void pointer to the arguments of the user-defined handler.
0663  *
0664  * @retval RTEMS_SUCCESSFUL Handler successfully disconnected from this pin.
0665  * @retval RTEMS_INVALID_ID Pin number is invalid.
0666  * @retval RTEMS_NOT_CONFIGURED Pin has no active interrupts.
0667  * @retval * @see rtems_gpio_disable_interrupt()
0668  */
0669 extern rtems_status_code rtems_gpio_interrupt_handler_remove(
0670   uint32_t pin_number,
0671   rtems_gpio_irq_state (*handler) (void *arg),
0672   void *arg
0673 );
0674 
0675 /**
0676  * @brief Stops interrupts from being generated on a given GPIO pin
0677  *        and removes the corresponding handler.
0678  *
0679  * @param[in] pin_number GPIO pin number.
0680  *
0681  * @retval RTEMS_SUCCESSFUL Interrupt successfully disabled for this pin.
0682  * @retval RTEMS_INVALID_ID Pin number is invalid.
0683  * @retval RTEMS_NOT_CONFIGURED Pin has no active interrupts.
0684  * @retval RTEMS_UNSATISFIED Could not remove the current interrupt handler,
0685  *                           could not recognize the current active interrupt
0686  *                           on this pin or could not disable interrupts on
0687  *                           this pin.
0688  */
0689 extern rtems_status_code rtems_gpio_disable_interrupt(uint32_t pin_number);
0690 
0691 /**
0692  * @brief Sets multiple output GPIO pins with the logical high.
0693  *        This must be implemented by each BSP.
0694  *
0695  * @param[in] bank GPIO bank number.
0696  * @param[in] bitmask Bitmask of GPIO pins to set in the given bank.
0697  *
0698  * @retval RTEMS_SUCCESSFUL All pins were set successfully.
0699  * @retval RTEMS_UNSATISFIED Could not set at least one of the pins.
0700  */
0701 extern rtems_status_code rtems_gpio_bsp_multi_set(
0702   uint32_t bank,
0703   uint32_t bitmask
0704 );
0705 
0706 /**
0707  * @brief Sets multiple output GPIO pins with the logical low.
0708  *        This must be implemented by each BSP.
0709  *
0710  * @param[in] bank GPIO bank number.
0711  * @param[in] bitmask Bitmask of GPIO pins to clear in the given bank.
0712  *
0713  * @retval RTEMS_SUCCESSFUL All pins were cleared successfully.
0714  * @retval RTEMS_UNSATISFIED Could not clear at least one of the pins.
0715  */
0716 extern rtems_status_code rtems_gpio_bsp_multi_clear(
0717   uint32_t bank,
0718   uint32_t bitmask
0719 );
0720 
0721 /**
0722  * @brief Returns the value (level) of multiple GPIO input pins.
0723  *        This must be implemented by each BSP.
0724  *
0725  * @param[in] bank GPIO bank number.
0726  * @param[in] bitmask Bitmask of GPIO pins to read in the given bank.
0727  *
0728  * @retval The function must return a bitmask with the values of the
0729  *         corresponding pins. 0 for logical low and 1 for logical high.
0730  * @retval GPIO_INPUT_ERROR Could not read at least one pin level.
0731  */
0732 extern uint32_t rtems_gpio_bsp_multi_read(uint32_t bank, uint32_t bitmask);
0733 
0734 /**
0735  * @brief Performs a BSP specific operation on a group of pins.
0736  *        The implementation for this function may be omitted if the target
0737  *        does not support the feature, by returning RTEMS_NOT_DEFINED.
0738  *
0739  * @param[in] bank GPIO bank number.
0740  * @param[in] pins Array filled with BSP specific pin numbers. All pins belong
0741  *                 to the same select bank.
0742  * @param[in] pin_count Number of pin configurations in the @var pins array.
0743  * @param[in] arg Pointer to a BSP defined structure with BSP-specific
0744  *                data. This field is handled by the BSP.
0745  *
0746  * @retval RTEMS_SUCCESSFUL Operation completed with success.
0747  * @retval RTEMS_NOT_DEFINED Group has no BSP specific pins, or the BSP does not
0748  *                           support BSP specific operations for groups.
0749  * @retval RTEMS_UNSATISFIED Could not operate on at least one of the pins.
0750  */
0751 extern rtems_status_code rtems_gpio_bsp_specific_group_operation(
0752   uint32_t bank,
0753   uint32_t *pins,
0754   uint32_t pin_count,
0755   void *arg
0756 );
0757 
0758 /**
0759  * @brief Assigns GPIO functions to all the given pins in a single register
0760  *        operation.
0761  *        The implementation for this function may be omitted if the target
0762  *        does not support the feature, by returning RTEMS_NOT_DEFINED.
0763  *
0764  * @param[in] pins Array of rtems_gpio_multiple_pin_select structures filled
0765  *                 with the pins desired functions. All pins belong to the
0766  *                 same select bank.
0767  * @param[in] pin_count Number of pin configurations in the @var pins array.
0768  * @param[in] select_bank Select bank number of the received pins.
0769  *
0770  * @retval RTEMS_SUCCESSFUL Functions were assigned successfully.
0771  * @retval RTEMS_NOT_DEFINED The BSP does not support multiple pin function
0772  *                           assignment.
0773  * @retval RTEMS_UNSATISFIED Could not assign the functions to the pins.
0774  */
0775 extern rtems_status_code rtems_gpio_bsp_multi_select(
0776   rtems_gpio_multiple_pin_select *pins,
0777   uint32_t pin_count,
0778   uint32_t select_bank
0779 );
0780 
0781 /**
0782  * @brief Sets an output GPIO pin with the logical high.
0783  *        This must be implemented by each BSP.
0784  *
0785  * @param[in] bank GPIO bank number.
0786  * @param[in] pin GPIO pin number within the given bank.
0787  *
0788  * @retval RTEMS_SUCCESSFUL Pin was set successfully.
0789  * @retval RTEMS_UNSATISFIED Could not set the given pin.
0790  */
0791 extern rtems_status_code rtems_gpio_bsp_set(uint32_t bank, uint32_t pin);
0792 
0793 /**
0794  * @brief Sets an output GPIO pin with the logical low.
0795  *        This must be implemented by each BSP.
0796  *
0797  * @param[in] bank GPIO bank number.
0798  * @param[in] pin GPIO pin number within the given bank.
0799  *
0800  * @retval RTEMS_SUCCESSFUL Pin was cleared successfully.
0801  * @retval RTEMS_UNSATISFIED Could not clear the given pin.
0802  */
0803 extern rtems_status_code rtems_gpio_bsp_clear(uint32_t bank, uint32_t pin);
0804 
0805 /**
0806  * @brief Returns the value (level) of a GPIO input pin.
0807  *        This must be implemented by each BSP.
0808  *
0809  * @param[in] bank GPIO bank number.
0810  * @param[in] pin GPIO pin number within the given bank.
0811  *
0812  * @retval The function must return 0 if the pin level is a logical low,
0813  *         or non zero if it has a logical high.
0814  * @retval GPIO_INPUT_ERROR Could not read the pin level.
0815  */
0816 extern uint32_t rtems_gpio_bsp_get_value(uint32_t bank, uint32_t pin);
0817 
0818 /**
0819  * @brief Assigns the digital input function to the given pin.
0820  *        This must be implemented by each BSP.
0821  *
0822  * @param[in] bank GPIO bank number.
0823  * @param[in] pin GPIO pin number within the given bank.
0824  * @param[in] bsp_specific Pointer to a BSP defined structure with BSP-specific
0825  *                         data.
0826  *
0827  * @retval RTEMS_SUCCESSFUL Function was assigned successfully.
0828  * @retval RTEMS_UNSATISFIED Could not assign the function to the pin.
0829  */
0830 extern rtems_status_code rtems_gpio_bsp_select_input(
0831   uint32_t bank,
0832   uint32_t pin,
0833   void *bsp_specific
0834 );
0835 
0836 /**
0837  * @brief Assigns the digital output function to the given pin.
0838  *        This must be implemented by each BSP.
0839  *
0840  * @param[in] bank GPIO bank number.
0841  * @param[in] pin GPIO pin number within the given bank.
0842  * @param[in] bsp_specific Pointer to a BSP defined structure with BSP-specific
0843  *                         data.
0844  *
0845  * @retval RTEMS_SUCCESSFUL Function was assigned successfully.
0846  * @retval RTEMS_UNSATISFIED Could not assign the function to the pin.
0847  */
0848 extern rtems_status_code rtems_gpio_bsp_select_output(
0849   uint32_t bank,
0850   uint32_t pin,
0851   void *bsp_specific
0852 );
0853 
0854 /**
0855  * @brief Assigns a BSP specific function to the given pin.
0856  *        This must be implemented by each BSP.
0857  *
0858  * @param[in] bank GPIO bank number.
0859  * @param[in] pin GPIO pin number within the given bank.
0860  * @param[in] function BSP defined GPIO function.
0861  * @param[in] pin_data Pointer to a BSP defined structure with BSP-specific
0862  *                     data.
0863  *
0864  * @retval RTEMS_SUCCESSFUL Function was assigned successfully.
0865  * @retval RTEMS_UNSATISFIED Could not assign the function to the pin.
0866  */
0867 extern rtems_status_code rtems_gpio_bsp_select_specific_io(
0868   uint32_t bank,
0869   uint32_t pin,
0870   uint32_t function,
0871   void *pin_data
0872 );
0873 
0874 /**
0875  * @brief Configures a single GPIO pin pull resistor.
0876  *        This must be implemented by each BSP.
0877  *
0878  * @param[in] bank GPIO bank number.
0879  * @param[in] pin GPIO pin number within the given bank.
0880  * @param[in] mode The pull resistor mode.
0881  *
0882  * @retval RTEMS_SUCCESSFUL Pull resistor successfully configured.
0883  * @retval RTEMS_UNSATISFIED Could not set the pull mode.
0884  */
0885 extern rtems_status_code rtems_gpio_bsp_set_resistor_mode(
0886   uint32_t bank,
0887   uint32_t pin,
0888   rtems_gpio_pull_mode mode
0889 );
0890 
0891 /**
0892  * @brief Reads and returns a vector/bank interrupt event line.
0893  *        The bitmask should indicate with a 1 if the corresponding pin
0894  *        as a pending interrupt, or 0 if otherwise. The function
0895  *        should clear the interrupt event line before returning.
0896  *        This must be implemented by each BSP.
0897  *
0898  * @param[in] vector GPIO vector/bank.
0899  *
0900  * @retval Bitmask (max 32-bit) representing a GPIO bank, where a bit set
0901  *         indicates an active interrupt on that pin.
0902  */
0903 extern uint32_t rtems_gpio_bsp_interrupt_line(rtems_vector_number vector);
0904 
0905 /**
0906  * @brief Calculates a vector number for a given GPIO bank.
0907  *        This must be implemented by each BSP.
0908  *
0909  * @param[in] bank GPIO bank number.
0910  *
0911  * @retval The corresponding rtems_vector_number.
0912  */
0913 extern rtems_vector_number rtems_gpio_bsp_get_vector(uint32_t bank);
0914 
0915 /**
0916  * @brief Enables interrupts to be generated on a given GPIO pin.
0917  *        This must be implemented by each BSP.
0918  *
0919  * @param[in] bank GPIO bank number.
0920  * @param[in] pin GPIO pin number within the given bank.
0921  * @param[in] interrupt Type of interrupt to enable for the pin.
0922  *
0923  * @retval RTEMS_SUCCESSFUL Interrupt successfully enabled for this pin.
0924  * @retval RTEMS_UNSATISFIED Could not enable the interrupt on the pin.
0925  */
0926 extern rtems_status_code rtems_gpio_bsp_enable_interrupt(
0927   uint32_t bank,
0928   uint32_t pin,
0929   rtems_gpio_interrupt interrupt
0930 );
0931 
0932 /**
0933  * @brief Stops interrupts from being generated on a given GPIO pin.
0934  *        This must be implemented by each BSP.
0935  *
0936  * @param[in] bank GPIO bank number.
0937  * @param[in] pin GPIO pin number within the given bank.
0938  * @param[in] active_interrupt Interrupt type currently active on this pin.
0939  *
0940  * @retval RTEMS_SUCCESSFUL Interrupt successfully disabled for this pin.
0941  * @retval RTEMS_UNSATISFIED Could not disable interrupts on this pin.
0942  */
0943 extern rtems_status_code rtems_gpio_bsp_disable_interrupt(
0944   uint32_t bank,
0945   uint32_t pin,
0946   rtems_gpio_interrupt interrupt
0947 );
0948 
0949 /** @} */
0950 
0951 #ifdef __cplusplus
0952 }
0953 #endif /* __cplusplus */
0954 
0955 #endif /* LIBBSP_SHARED_GPIO_H */