Nordic stack and drivers for the mbed BLE API
Fork of nRF51822 by
nordic/nrf-sdk/app_common/app_gpiote.h@0:eff01767de02, 2014-03-26 (annotated)
- Committer:
- bogdanm
- Date:
- Wed Mar 26 14:38:17 2014 +0000
- Revision:
- 0:eff01767de02
- Child:
- 37:c29c330d942c
Initial import of the nRF51822 code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:eff01767de02 | 1 | /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. |
bogdanm | 0:eff01767de02 | 2 | * |
bogdanm | 0:eff01767de02 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
bogdanm | 0:eff01767de02 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
bogdanm | 0:eff01767de02 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
bogdanm | 0:eff01767de02 | 6 | * |
bogdanm | 0:eff01767de02 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
bogdanm | 0:eff01767de02 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
bogdanm | 0:eff01767de02 | 9 | * the file. |
bogdanm | 0:eff01767de02 | 10 | * |
bogdanm | 0:eff01767de02 | 11 | */ |
bogdanm | 0:eff01767de02 | 12 | |
bogdanm | 0:eff01767de02 | 13 | /** @file |
bogdanm | 0:eff01767de02 | 14 | * |
bogdanm | 0:eff01767de02 | 15 | * @defgroup app_gpiote GPIOTE Handler |
bogdanm | 0:eff01767de02 | 16 | * @{ |
bogdanm | 0:eff01767de02 | 17 | * @ingroup app_common |
bogdanm | 0:eff01767de02 | 18 | * |
bogdanm | 0:eff01767de02 | 19 | * @brief GPIOTE handler module. |
bogdanm | 0:eff01767de02 | 20 | * |
bogdanm | 0:eff01767de02 | 21 | * @details The GPIOTE handler allows several modules ("users") to share the GPIOTE interrupt, |
bogdanm | 0:eff01767de02 | 22 | * each user defining a set of pins able to generate events to the user. |
bogdanm | 0:eff01767de02 | 23 | * When a GPIOTE interrupt occurs, the GPIOTE interrupt handler will call the event handler |
bogdanm | 0:eff01767de02 | 24 | * of each user for which at least one of the pins generated an event. |
bogdanm | 0:eff01767de02 | 25 | * |
bogdanm | 0:eff01767de02 | 26 | * The GPIOTE users are responsible for configuring all their corresponding pins, except |
bogdanm | 0:eff01767de02 | 27 | * the SENSE field, which should be initialized to GPIO_PIN_CNF_SENSE_Disabled. |
bogdanm | 0:eff01767de02 | 28 | * The SENSE field will be updated by the GPIOTE module when it is enabled or disabled, |
bogdanm | 0:eff01767de02 | 29 | * and also while it is enabled. |
bogdanm | 0:eff01767de02 | 30 | * |
bogdanm | 0:eff01767de02 | 31 | * The module specifies on which pins events should be generated if the pin(s) goes |
bogdanm | 0:eff01767de02 | 32 | * from low->high or high->low or both directions. |
bogdanm | 0:eff01767de02 | 33 | * |
bogdanm | 0:eff01767de02 | 34 | * @note Even if the application is using the @ref app_scheduler, the GPIOTE event handlers will |
bogdanm | 0:eff01767de02 | 35 | * be called directly from the GPIOTE interrupt handler. |
bogdanm | 0:eff01767de02 | 36 | */ |
bogdanm | 0:eff01767de02 | 37 | |
bogdanm | 0:eff01767de02 | 38 | #ifndef APP_GPIOTE_H__ |
bogdanm | 0:eff01767de02 | 39 | #define APP_GPIOTE_H__ |
bogdanm | 0:eff01767de02 | 40 | |
bogdanm | 0:eff01767de02 | 41 | #include <stdint.h> |
bogdanm | 0:eff01767de02 | 42 | #include <stdbool.h> |
bogdanm | 0:eff01767de02 | 43 | #include "nordic_global.h" |
bogdanm | 0:eff01767de02 | 44 | #include "nrf.h" |
bogdanm | 0:eff01767de02 | 45 | #include "app_error.h" |
bogdanm | 0:eff01767de02 | 46 | #include "app_util.h" |
bogdanm | 0:eff01767de02 | 47 | |
bogdanm | 0:eff01767de02 | 48 | #define GPIOTE_USER_NODE_SIZE 20 /**< Size of app_gpiote.gpiote_user_t (only for use inside APP_GPIOTE_BUF_SIZE()). */ |
bogdanm | 0:eff01767de02 | 49 | #define NO_OF_PINS 32 /**< Number of GPIO pins on the nRF51 chip. */ |
bogdanm | 0:eff01767de02 | 50 | |
bogdanm | 0:eff01767de02 | 51 | /**@brief Compute number of bytes required to hold the GPIOTE data structures. |
bogdanm | 0:eff01767de02 | 52 | * |
bogdanm | 0:eff01767de02 | 53 | * @param[in] MAX_USERS Maximum number of GPIOTE users. |
bogdanm | 0:eff01767de02 | 54 | * |
bogdanm | 0:eff01767de02 | 55 | * @return Required buffer size (in bytes). |
bogdanm | 0:eff01767de02 | 56 | */ |
bogdanm | 0:eff01767de02 | 57 | #define APP_GPIOTE_BUF_SIZE(MAX_USERS) ((MAX_USERS) * GPIOTE_USER_NODE_SIZE) |
bogdanm | 0:eff01767de02 | 58 | |
bogdanm | 0:eff01767de02 | 59 | typedef uint8_t app_gpiote_user_id_t; |
bogdanm | 0:eff01767de02 | 60 | |
bogdanm | 0:eff01767de02 | 61 | /**@brief GPIOTE event handler type. */ |
bogdanm | 0:eff01767de02 | 62 | typedef void (*app_gpiote_event_handler_t)(uint32_t event_pins_low_to_high, |
bogdanm | 0:eff01767de02 | 63 | uint32_t event_pins_high_to_low); |
bogdanm | 0:eff01767de02 | 64 | |
bogdanm | 0:eff01767de02 | 65 | /**@brief Macro for initializing the GPIOTE module. |
bogdanm | 0:eff01767de02 | 66 | * |
bogdanm | 0:eff01767de02 | 67 | * @details It will handle dimensioning and allocation of the memory buffer required by the module, |
bogdanm | 0:eff01767de02 | 68 | * making sure that the buffer is correctly aligned. |
bogdanm | 0:eff01767de02 | 69 | * |
bogdanm | 0:eff01767de02 | 70 | * @param[in] MAX_USERS Maximum number of GPIOTE users. |
bogdanm | 0:eff01767de02 | 71 | * |
bogdanm | 0:eff01767de02 | 72 | * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it |
bogdanm | 0:eff01767de02 | 73 | * several times as long as it is from the same location, e.g. to do a reinitialization). |
bogdanm | 0:eff01767de02 | 74 | */ |
bogdanm | 0:eff01767de02 | 75 | /*lint -emacro(506, APP_GPIOTE_INIT) */ /* Suppress "Constant value Boolean */ |
bogdanm | 0:eff01767de02 | 76 | #define APP_GPIOTE_INIT(MAX_USERS) \ |
bogdanm | 0:eff01767de02 | 77 | do \ |
bogdanm | 0:eff01767de02 | 78 | { \ |
bogdanm | 0:eff01767de02 | 79 | static uint32_t app_gpiote_buf[CEIL_DIV(APP_GPIOTE_BUF_SIZE(MAX_USERS), sizeof(uint32_t))];\ |
bogdanm | 0:eff01767de02 | 80 | uint32_t ERR_CODE = app_gpiote_init((MAX_USERS), app_gpiote_buf); \ |
bogdanm | 0:eff01767de02 | 81 | APP_ERROR_CHECK(ERR_CODE); \ |
bogdanm | 0:eff01767de02 | 82 | } while (0) |
bogdanm | 0:eff01767de02 | 83 | |
bogdanm | 0:eff01767de02 | 84 | /**@brief Function for initializing the GPIOTE module. |
bogdanm | 0:eff01767de02 | 85 | * |
bogdanm | 0:eff01767de02 | 86 | * @note Normally initialization should be done using the APP_GPIOTE_INIT() macro, as that will |
bogdanm | 0:eff01767de02 | 87 | * allocate the buffer needed by the GPIOTE module (including aligning the buffer correctly). |
bogdanm | 0:eff01767de02 | 88 | * |
bogdanm | 0:eff01767de02 | 89 | * @param[in] max_users Maximum number of GPIOTE users. |
bogdanm | 0:eff01767de02 | 90 | * @param[in] p_buffer Pointer to memory buffer for internal use in the app_gpiote |
bogdanm | 0:eff01767de02 | 91 | * module. The size of the buffer can be computed using the |
bogdanm | 0:eff01767de02 | 92 | * APP_GPIOTE_BUF_SIZE() macro. The buffer must be aligned to |
bogdanm | 0:eff01767de02 | 93 | * a 4 byte boundary. |
bogdanm | 0:eff01767de02 | 94 | * |
bogdanm | 0:eff01767de02 | 95 | * @retval NRF_SUCCESS Successful initialization. |
bogdanm | 0:eff01767de02 | 96 | * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte |
bogdanm | 0:eff01767de02 | 97 | * boundary). |
bogdanm | 0:eff01767de02 | 98 | */ |
bogdanm | 0:eff01767de02 | 99 | uint32_t app_gpiote_init(uint8_t max_users, void * p_buffer); |
bogdanm | 0:eff01767de02 | 100 | |
bogdanm | 0:eff01767de02 | 101 | /**@brief Function for registering a GPIOTE user. |
bogdanm | 0:eff01767de02 | 102 | * |
bogdanm | 0:eff01767de02 | 103 | * @param[out] p_user_id Id for the new GPIOTE user. |
bogdanm | 0:eff01767de02 | 104 | * @param[in] pins_low_to_high_mask Mask defining which pins will generate events to this user |
bogdanm | 0:eff01767de02 | 105 | * when state is changed from low->high. |
bogdanm | 0:eff01767de02 | 106 | * @param[in] pins_high_to_low_mask Mask defining which pins will generate events to this user |
bogdanm | 0:eff01767de02 | 107 | * when state is changed from high->low. |
bogdanm | 0:eff01767de02 | 108 | * @param[in] event_handler Pointer to function to be executed when an event occurs. |
bogdanm | 0:eff01767de02 | 109 | * |
bogdanm | 0:eff01767de02 | 110 | * @retval NRF_SUCCESS Successful initialization. |
bogdanm | 0:eff01767de02 | 111 | * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte boundary). |
bogdanm | 0:eff01767de02 | 112 | * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE |
bogdanm | 0:eff01767de02 | 113 | * module. |
bogdanm | 0:eff01767de02 | 114 | * @retval NRF_ERROR_NO_MEM Returned if the application tries to register more users |
bogdanm | 0:eff01767de02 | 115 | * than defined when the GPIOTE module was initialized in |
bogdanm | 0:eff01767de02 | 116 | * @ref app_gpiote_init. |
bogdanm | 0:eff01767de02 | 117 | */ |
bogdanm | 0:eff01767de02 | 118 | uint32_t app_gpiote_user_register(app_gpiote_user_id_t * p_user_id, |
bogdanm | 0:eff01767de02 | 119 | uint32_t pins_low_to_high_mask, |
bogdanm | 0:eff01767de02 | 120 | uint32_t pins_high_to_low_mask, |
bogdanm | 0:eff01767de02 | 121 | app_gpiote_event_handler_t event_handler); |
bogdanm | 0:eff01767de02 | 122 | |
bogdanm | 0:eff01767de02 | 123 | /**@brief Function for informing the GPIOTE module that the specified user wants to use the GPIOTE module. |
bogdanm | 0:eff01767de02 | 124 | * |
bogdanm | 0:eff01767de02 | 125 | * @param[in] user_id Id of user to enable. |
bogdanm | 0:eff01767de02 | 126 | * |
bogdanm | 0:eff01767de02 | 127 | * @retval NRF_SUCCESS On success. |
bogdanm | 0:eff01767de02 | 128 | * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user. |
bogdanm | 0:eff01767de02 | 129 | * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE |
bogdanm | 0:eff01767de02 | 130 | * module. |
bogdanm | 0:eff01767de02 | 131 | */ |
bogdanm | 0:eff01767de02 | 132 | uint32_t app_gpiote_user_enable(app_gpiote_user_id_t user_id); |
bogdanm | 0:eff01767de02 | 133 | |
bogdanm | 0:eff01767de02 | 134 | /**@brief Function for informing the GPIOTE module that the specified user is done using the GPIOTE module. |
bogdanm | 0:eff01767de02 | 135 | * |
bogdanm | 0:eff01767de02 | 136 | * @param[in] user_id Id of user to enable. |
bogdanm | 0:eff01767de02 | 137 | * |
bogdanm | 0:eff01767de02 | 138 | * @return NRF_SUCCESS On success. |
bogdanm | 0:eff01767de02 | 139 | * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user. |
bogdanm | 0:eff01767de02 | 140 | * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE |
bogdanm | 0:eff01767de02 | 141 | * module. |
bogdanm | 0:eff01767de02 | 142 | */ |
bogdanm | 0:eff01767de02 | 143 | uint32_t app_gpiote_user_disable(app_gpiote_user_id_t user_id); |
bogdanm | 0:eff01767de02 | 144 | |
bogdanm | 0:eff01767de02 | 145 | /**@brief Function for getting the state of the pins which are registered for the specified user. |
bogdanm | 0:eff01767de02 | 146 | * |
bogdanm | 0:eff01767de02 | 147 | * @param[in] user_id Id of user to check. |
bogdanm | 0:eff01767de02 | 148 | * @param[out] p_pins Bit mask corresponding to the pins configured to generate events to |
bogdanm | 0:eff01767de02 | 149 | * the specified user. All bits corresponding to pins in the state |
bogdanm | 0:eff01767de02 | 150 | * 'high' will have value '1', all others will have value '0'. |
bogdanm | 0:eff01767de02 | 151 | * |
bogdanm | 0:eff01767de02 | 152 | * @return NRF_SUCCESS On success. |
bogdanm | 0:eff01767de02 | 153 | * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user. |
bogdanm | 0:eff01767de02 | 154 | * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE |
bogdanm | 0:eff01767de02 | 155 | * module. |
bogdanm | 0:eff01767de02 | 156 | */ |
bogdanm | 0:eff01767de02 | 157 | uint32_t app_gpiote_pins_state_get(app_gpiote_user_id_t user_id, uint32_t * p_pins); |
bogdanm | 0:eff01767de02 | 158 | |
bogdanm | 0:eff01767de02 | 159 | #endif // APP_GPIOTE_H__ |
bogdanm | 0:eff01767de02 | 160 | |
bogdanm | 0:eff01767de02 | 161 | /** @} */ |