mbed library

Dependents:   Printf

Fork of mbed by mbed official

Committer:
bogdanm
Date:
Fri Sep 12 16:41:52 2014 +0100
Revision:
89:552587b429a1
Release 89 of the mbed library

Main changes:

- low power optimizations for Nordic targets
- code structure changes for Freescale K64F targets
- bug fixes in various backends

Who changed what in which revision?

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