Copy of nRF51822 library

Fork of nRF51822 by Nordic Semiconductor

Committer:
wd5gnr
Date:
Sun Sep 21 19:21:08 2014 +0000
Revision:
61:6fb5c2c43d35
Parent:
45:3c4df37ed83e
First commit

Who changed what in which revision?

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