BLE FOTA APP

Dependencies:   BLE_API mbed

It doesn't work with the default FOTA bootloader. It use NVIC_SystemReset() to enter a bootloader.

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

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