cc y / mbed

Fork of mbed by mbed official

Committer:
Kojto
Date:
Wed Oct 29 11:02:04 2014 +0000
Revision:
91:031413cf7a89
Release 91 of the mbed library

Changes:

- RBLAB_NANO - new target addition
- NRF51_DK - new target addition
- NRF51_DONGLE - new target addition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 91:031413cf7a89 1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
Kojto 91:031413cf7a89 2 *
Kojto 91:031413cf7a89 3 * The information contained herein is property of Nordic Semiconductor ASA.
Kojto 91:031413cf7a89 4 * Terms and conditions of usage are described in detail in NORDIC
Kojto 91:031413cf7a89 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
Kojto 91:031413cf7a89 6 *
Kojto 91:031413cf7a89 7 * Licensees are granted free, non-transferable use of the information. NO
Kojto 91:031413cf7a89 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
Kojto 91:031413cf7a89 9 * the file.
Kojto 91:031413cf7a89 10 *
Kojto 91:031413cf7a89 11 */
Kojto 91:031413cf7a89 12
Kojto 91:031413cf7a89 13 /** @file
Kojto 91:031413cf7a89 14 *
Kojto 91:031413cf7a89 15 * @defgroup app_gpiote GPIOTE Handler
Kojto 91:031413cf7a89 16 * @{
Kojto 91:031413cf7a89 17 * @ingroup app_common
Kojto 91:031413cf7a89 18 *
Kojto 91:031413cf7a89 19 * @brief GPIOTE handler module.
Kojto 91:031413cf7a89 20 *
Kojto 91:031413cf7a89 21 * @details The GPIOTE handler allows several modules ("users") to share the GPIOTE interrupt,
Kojto 91:031413cf7a89 22 * each user defining a set of pins able to generate events to the user.
Kojto 91:031413cf7a89 23 * When a GPIOTE interrupt occurs, the GPIOTE interrupt handler will call the event handler
Kojto 91:031413cf7a89 24 * of each user for which at least one of the pins generated an event.
Kojto 91:031413cf7a89 25 *
Kojto 91:031413cf7a89 26 * The GPIOTE users are responsible for configuring all their corresponding pins, except
Kojto 91:031413cf7a89 27 * the SENSE field, which should be initialized to GPIO_PIN_CNF_SENSE_Disabled.
Kojto 91:031413cf7a89 28 * The SENSE field will be updated by the GPIOTE module when it is enabled or disabled,
Kojto 91:031413cf7a89 29 * and also while it is enabled.
Kojto 91:031413cf7a89 30 *
Kojto 91:031413cf7a89 31 * The module specifies on which pins events should be generated if the pin(s) goes
Kojto 91:031413cf7a89 32 * from low->high or high->low or both directions.
Kojto 91:031413cf7a89 33 *
Kojto 91:031413cf7a89 34 * @note Even if the application is using the @ref app_scheduler, the GPIOTE event handlers will
Kojto 91:031413cf7a89 35 * be called directly from the GPIOTE interrupt handler.
Kojto 91:031413cf7a89 36 *
Kojto 91:031413cf7a89 37 * @warning If multiple users registers for the same pins the behavior for those pins are undefined.
Kojto 91:031413cf7a89 38 */
Kojto 91:031413cf7a89 39
Kojto 91:031413cf7a89 40 #ifndef APP_GPIOTE_H__
Kojto 91:031413cf7a89 41 #define APP_GPIOTE_H__
Kojto 91:031413cf7a89 42
Kojto 91:031413cf7a89 43 #include <stdint.h>
Kojto 91:031413cf7a89 44 #include <stdbool.h>
Kojto 91:031413cf7a89 45 // #include "nrf.h"
Kojto 91:031413cf7a89 46 #include "app_error.h"
Kojto 91:031413cf7a89 47 #include "app_util.h"
Kojto 91:031413cf7a89 48
Kojto 91:031413cf7a89 49 #ifdef __cpluplus
Kojto 91:031413cf7a89 50 extern "C" {
Kojto 91:031413cf7a89 51 #endif
Kojto 91:031413cf7a89 52
Kojto 91:031413cf7a89 53 #define GPIOTE_USER_NODE_SIZE 20 /**< Size of app_gpiote.gpiote_user_t (only for use inside APP_GPIOTE_BUF_SIZE()). */
Kojto 91:031413cf7a89 54 #define NO_OF_PINS 32 /**< Number of GPIO pins on the nRF51 chip. */
Kojto 91:031413cf7a89 55
Kojto 91:031413cf7a89 56 /**@brief Compute number of bytes required to hold the GPIOTE data structures.
Kojto 91:031413cf7a89 57 *
Kojto 91:031413cf7a89 58 * @param[in] MAX_USERS Maximum number of GPIOTE users.
Kojto 91:031413cf7a89 59 *
Kojto 91:031413cf7a89 60 * @return Required buffer size (in bytes).
Kojto 91:031413cf7a89 61 */
Kojto 91:031413cf7a89 62 #define APP_GPIOTE_BUF_SIZE(MAX_USERS) ((MAX_USERS) * GPIOTE_USER_NODE_SIZE)
Kojto 91:031413cf7a89 63
Kojto 91:031413cf7a89 64 typedef uint8_t app_gpiote_user_id_t;
Kojto 91:031413cf7a89 65
Kojto 91:031413cf7a89 66 /**@brief GPIOTE event handler type. */
Kojto 91:031413cf7a89 67 typedef void (*app_gpiote_event_handler_t)(uint32_t event_pins_low_to_high,
Kojto 91:031413cf7a89 68 uint32_t event_pins_high_to_low);
Kojto 91:031413cf7a89 69
Kojto 91:031413cf7a89 70 /**@brief GPIOTE input event handler type. */
Kojto 91:031413cf7a89 71 typedef void (*app_gpiote_input_event_handler_t)(void);
Kojto 91:031413cf7a89 72
Kojto 91:031413cf7a89 73 /**@brief Macro for initializing the GPIOTE module.
Kojto 91:031413cf7a89 74 *
Kojto 91:031413cf7a89 75 * @details It will handle dimensioning and allocation of the memory buffer required by the module,
Kojto 91:031413cf7a89 76 * making sure that the buffer is correctly aligned.
Kojto 91:031413cf7a89 77 *
Kojto 91:031413cf7a89 78 * @param[in] MAX_USERS Maximum number of GPIOTE users.
Kojto 91:031413cf7a89 79 *
Kojto 91:031413cf7a89 80 * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
Kojto 91:031413cf7a89 81 * several times as long as it is from the same location, e.g. to do a reinitialization).
Kojto 91:031413cf7a89 82 */
Kojto 91:031413cf7a89 83 /*lint -emacro(506, APP_GPIOTE_INIT) */ /* Suppress "Constant value Boolean */
Kojto 91:031413cf7a89 84 #define APP_GPIOTE_INIT(MAX_USERS) \
Kojto 91:031413cf7a89 85 do \
Kojto 91:031413cf7a89 86 { \
Kojto 91:031413cf7a89 87 static uint32_t app_gpiote_buf[CEIL_DIV(APP_GPIOTE_BUF_SIZE(MAX_USERS), sizeof(uint32_t))];\
Kojto 91:031413cf7a89 88 uint32_t ERR_CODE = app_gpiote_init((MAX_USERS), app_gpiote_buf); \
Kojto 91:031413cf7a89 89 APP_ERROR_CHECK(ERR_CODE); \
Kojto 91:031413cf7a89 90 } while (0)
Kojto 91:031413cf7a89 91
Kojto 91:031413cf7a89 92 /**@brief Function for initializing the GPIOTE module.
Kojto 91:031413cf7a89 93 *
Kojto 91:031413cf7a89 94 * @note Normally initialization should be done using the APP_GPIOTE_INIT() macro, as that will
Kojto 91:031413cf7a89 95 * allocate the buffer needed by the GPIOTE module (including aligning the buffer correctly).
Kojto 91:031413cf7a89 96 *
Kojto 91:031413cf7a89 97 * @param[in] max_users Maximum number of GPIOTE users.
Kojto 91:031413cf7a89 98 * @param[in] p_buffer Pointer to memory buffer for internal use in the app_gpiote
Kojto 91:031413cf7a89 99 * module. The size of the buffer can be computed using the
Kojto 91:031413cf7a89 100 * APP_GPIOTE_BUF_SIZE() macro. The buffer must be aligned to
Kojto 91:031413cf7a89 101 * a 4 byte boundary.
Kojto 91:031413cf7a89 102 *
Kojto 91:031413cf7a89 103 * @retval NRF_SUCCESS Successful initialization.
Kojto 91:031413cf7a89 104 * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte
Kojto 91:031413cf7a89 105 * boundary).
Kojto 91:031413cf7a89 106 */
Kojto 91:031413cf7a89 107 uint32_t app_gpiote_init(uint8_t max_users, void * p_buffer);
Kojto 91:031413cf7a89 108
Kojto 91:031413cf7a89 109 /**@brief Function for registering a GPIOTE user.
Kojto 91:031413cf7a89 110 *
Kojto 91:031413cf7a89 111 * @param[out] p_user_id Id for the new GPIOTE user.
Kojto 91:031413cf7a89 112 * @param[in] pins_low_to_high_mask Mask defining which pins will generate events to this user
Kojto 91:031413cf7a89 113 * when state is changed from low->high.
Kojto 91:031413cf7a89 114 * @param[in] pins_high_to_low_mask Mask defining which pins will generate events to this user
Kojto 91:031413cf7a89 115 * when state is changed from high->low.
Kojto 91:031413cf7a89 116 * @param[in] event_handler Pointer to function to be executed when an event occurs.
Kojto 91:031413cf7a89 117 *
Kojto 91:031413cf7a89 118 * @retval NRF_SUCCESS Successful initialization.
Kojto 91:031413cf7a89 119 * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte boundary).
Kojto 91:031413cf7a89 120 * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
Kojto 91:031413cf7a89 121 * module.
Kojto 91:031413cf7a89 122 * @retval NRF_ERROR_NO_MEM Returned if the application tries to register more users
Kojto 91:031413cf7a89 123 * than defined when the GPIOTE module was initialized in
Kojto 91:031413cf7a89 124 * @ref app_gpiote_init.
Kojto 91:031413cf7a89 125 */
Kojto 91:031413cf7a89 126 uint32_t app_gpiote_user_register(app_gpiote_user_id_t * p_user_id,
Kojto 91:031413cf7a89 127 uint32_t pins_low_to_high_mask,
Kojto 91:031413cf7a89 128 uint32_t pins_high_to_low_mask,
Kojto 91:031413cf7a89 129 app_gpiote_event_handler_t event_handler);
Kojto 91:031413cf7a89 130
Kojto 91:031413cf7a89 131 /**@brief Function for informing the GPIOTE module that the specified user wants to use the GPIOTE module.
Kojto 91:031413cf7a89 132 *
Kojto 91:031413cf7a89 133 * @param[in] user_id Id of user to enable.
Kojto 91:031413cf7a89 134 *
Kojto 91:031413cf7a89 135 * @retval NRF_SUCCESS On success.
Kojto 91:031413cf7a89 136 * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user.
Kojto 91:031413cf7a89 137 * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
Kojto 91:031413cf7a89 138 * module.
Kojto 91:031413cf7a89 139 */
Kojto 91:031413cf7a89 140 uint32_t app_gpiote_user_enable(app_gpiote_user_id_t user_id);
Kojto 91:031413cf7a89 141
Kojto 91:031413cf7a89 142 /**@brief Function for informing the GPIOTE module that the specified user is done using the GPIOTE module.
Kojto 91:031413cf7a89 143 *
Kojto 91:031413cf7a89 144 * @param[in] user_id Id of user to enable.
Kojto 91:031413cf7a89 145 *
Kojto 91:031413cf7a89 146 * @return NRF_SUCCESS On success.
Kojto 91:031413cf7a89 147 * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user.
Kojto 91:031413cf7a89 148 * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
Kojto 91:031413cf7a89 149 * module.
Kojto 91:031413cf7a89 150 */
Kojto 91:031413cf7a89 151 uint32_t app_gpiote_user_disable(app_gpiote_user_id_t user_id);
Kojto 91:031413cf7a89 152
Kojto 91:031413cf7a89 153 /**@brief Function for getting the state of the pins which are registered for the specified user.
Kojto 91:031413cf7a89 154 *
Kojto 91:031413cf7a89 155 * @param[in] user_id Id of user to check.
Kojto 91:031413cf7a89 156 * @param[out] p_pins Bit mask corresponding to the pins configured to generate events to
Kojto 91:031413cf7a89 157 * the specified user. All bits corresponding to pins in the state
Kojto 91:031413cf7a89 158 * 'high' will have value '1', all others will have value '0'.
Kojto 91:031413cf7a89 159 *
Kojto 91:031413cf7a89 160 * @return NRF_SUCCESS On success.
Kojto 91:031413cf7a89 161 * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user.
Kojto 91:031413cf7a89 162 * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE
Kojto 91:031413cf7a89 163 * module.
Kojto 91:031413cf7a89 164 */
Kojto 91:031413cf7a89 165 uint32_t app_gpiote_pins_state_get(app_gpiote_user_id_t user_id, uint32_t * p_pins);
Kojto 91:031413cf7a89 166
Kojto 91:031413cf7a89 167 /**@brief Function for registering event handlers for GPIOTE IN events.
Kojto 91:031413cf7a89 168 *
Kojto 91:031413cf7a89 169 * @param[in] channel GPIOTE channel [0..3].
Kojto 91:031413cf7a89 170 * @param[in] pin Pins associated with GPIOTE channel. Changes on following pins will generate events.
Kojto 91:031413cf7a89 171 * @param[in] polarity Specify operation on input that shall trigger IN event.
Kojto 91:031413cf7a89 172 * @param[in] event_handler Event handler invoked on the IN event in the GPIOTE interrupt.
Kojto 91:031413cf7a89 173 *
Kojto 91:031413cf7a89 174 * @return NRF_SUCCESS On success.
Kojto 91:031413cf7a89 175 * @retval NRF_ERROR_INVALID_PARAM Invalid channel or pin number.
Kojto 91:031413cf7a89 176 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events.
Kojto 91:031413cf7a89 177 */
Kojto 91:031413cf7a89 178 uint32_t app_gpiote_input_event_handler_register(const uint8_t channel,
Kojto 91:031413cf7a89 179 const uint32_t pin,
Kojto 91:031413cf7a89 180 const uint32_t polarity,
Kojto 91:031413cf7a89 181 app_gpiote_input_event_handler_t event_handler);
Kojto 91:031413cf7a89 182
Kojto 91:031413cf7a89 183 /**@brief Function for unregistering event handlers for GPIOTE IN events.
Kojto 91:031413cf7a89 184 *
Kojto 91:031413cf7a89 185 * @return NRF_SUCCESS On success.
Kojto 91:031413cf7a89 186 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events.
Kojto 91:031413cf7a89 187 */
Kojto 91:031413cf7a89 188 uint32_t app_gpiote_input_event_handler_unregister(const uint8_t channel);
Kojto 91:031413cf7a89 189
Kojto 91:031413cf7a89 190 /**@brief Function for registering event handler invoked at the end of a GPIOTE interrupt.
Kojto 91:031413cf7a89 191 *
Kojto 91:031413cf7a89 192 * @param[in] event_handler Event handler invoked at the end of the GPIOTE interrupt.
Kojto 91:031413cf7a89 193 *
Kojto 91:031413cf7a89 194 * @return NRF_SUCCESS On success.
Kojto 91:031413cf7a89 195 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events.
Kojto 91:031413cf7a89 196 */
Kojto 91:031413cf7a89 197 uint32_t app_gpiote_end_irq_event_handler_register(app_gpiote_input_event_handler_t event_handler);
Kojto 91:031413cf7a89 198
Kojto 91:031413cf7a89 199 /**@brief Function for unregistering event handler invoked at the end of a GPIOTE interrupt.
Kojto 91:031413cf7a89 200 *
Kojto 91:031413cf7a89 201 * @return NRF_SUCCESS On success.
Kojto 91:031413cf7a89 202 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events.
Kojto 91:031413cf7a89 203 */
Kojto 91:031413cf7a89 204 uint32_t app_gpiote_end_irq_event_handler_unregister(void);
Kojto 91:031413cf7a89 205
Kojto 91:031413cf7a89 206 /**@brief Function for enabling interrupts in the GPIOTE driver.
Kojto 91:031413cf7a89 207 *
Kojto 91:031413cf7a89 208 * @return NRF_SUCCESS On success.
Kojto 91:031413cf7a89 209 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support.
Kojto 91:031413cf7a89 210 */
Kojto 91:031413cf7a89 211 uint32_t app_gpiote_enable_interrupts(void);
Kojto 91:031413cf7a89 212
Kojto 91:031413cf7a89 213 /**@brief Function for disabling interrupts in the GPIOTE driver.
Kojto 91:031413cf7a89 214 *
Kojto 91:031413cf7a89 215 * @return NRF_SUCCESS On success.
Kojto 91:031413cf7a89 216 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support.
Kojto 91:031413cf7a89 217 */
Kojto 91:031413cf7a89 218 uint32_t app_gpiote_disable_interrupts(void);
Kojto 91:031413cf7a89 219
Kojto 91:031413cf7a89 220 #ifdef __cpluplus
Kojto 91:031413cf7a89 221 }
Kojto 91:031413cf7a89 222 #endif
Kojto 91:031413cf7a89 223
Kojto 91:031413cf7a89 224 #endif // APP_GPIOTE_H__
Kojto 91:031413cf7a89 225
Kojto 91:031413cf7a89 226 /** @} */