テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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