Donal Morrissey / BLE_API_Native_blog

Dependents:   BLE_iBeacon_Exercise

Fork of BLE_API_Native by Kevin Townsend

Committer:
ktownsend
Date:
Fri Feb 07 16:04:09 2014 +0000
Revision:
4:2f1f20c755ed
Parent:
0:4c3097c65247
Added nordic_global.h to solve conditional compilation issues

Who changed what in which revision?

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