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_button Button Handler
bogdanm 89:552587b429a1 16 * @{
bogdanm 89:552587b429a1 17 * @ingroup app_common
bogdanm 89:552587b429a1 18 *
bogdanm 89:552587b429a1 19 * @brief Buttons handling module.
bogdanm 89:552587b429a1 20 *
bogdanm 89:552587b429a1 21 * @details The button handler uses the @ref app_gpiote to detect that a button has been
bogdanm 89:552587b429a1 22 * pushed. To handle debouncing, it will start a timer in the GPIOTE event handler.
bogdanm 89:552587b429a1 23 * The button will only be reported as pushed if the corresponding pin is still active when
bogdanm 89:552587b429a1 24 * the timer expires. If there is a new GPIOTE event while the timer is running, the timer
bogdanm 89:552587b429a1 25 * is restarted.
bogdanm 89:552587b429a1 26 * Use the USE_SCHEDULER parameter of the APP_BUTTON_INIT() macro to select if the
bogdanm 89:552587b429a1 27 * @ref app_scheduler is to be used or not.
bogdanm 89:552587b429a1 28 *
bogdanm 89:552587b429a1 29 * @note The app_button module uses the app_timer module. The user must ensure that the queue in
bogdanm 89:552587b429a1 30 * app_timer is large enough to hold the app_timer_stop() / app_timer_start() operations
bogdanm 89:552587b429a1 31 * which will be executed on each event from GPIOTE module (2 operations), as well as other
bogdanm 89:552587b429a1 32 * app_timer operations queued simultaneously in the application.
bogdanm 89:552587b429a1 33 *
bogdanm 89:552587b429a1 34 * @note Even if the scheduler is not used, app_button.h will include app_scheduler.h, so when
bogdanm 89:552587b429a1 35 * compiling, app_scheduler.h must be available in one of the compiler include paths.
bogdanm 89:552587b429a1 36 */
bogdanm 89:552587b429a1 37
bogdanm 89:552587b429a1 38 #ifndef APP_BUTTON_H__
bogdanm 89:552587b429a1 39 #define APP_BUTTON_H__
bogdanm 89:552587b429a1 40
bogdanm 89:552587b429a1 41 #include <stdint.h>
bogdanm 89:552587b429a1 42 #include <stdbool.h>
bogdanm 89:552587b429a1 43 #include "app_error.h"
bogdanm 89:552587b429a1 44 #include "app_scheduler.h"
bogdanm 89:552587b429a1 45 #include "nrf_gpio.h"
bogdanm 89:552587b429a1 46
bogdanm 89:552587b429a1 47 #define APP_BUTTON_SCHED_EVT_SIZE sizeof(app_button_event_t) /**< Size of button events being passed through the scheduler (is to be used for computing the maximum size of scheduler events). */
bogdanm 89:552587b429a1 48 #define APP_BUTTON_PUSH 1 /**< Indicates that a button is pushed. */
bogdanm 89:552587b429a1 49 #define APP_BUTTON_RELEASE 0 /**< Indicates that a button is released. */
bogdanm 89:552587b429a1 50 #define APP_BUTTON_ACTIVE_HIGH 1 /**< Indicates that a button is active high. */
bogdanm 89:552587b429a1 51 #define APP_BUTTON_ACTIVE_LOW 0 /**< Indicates that a button is active low. */
bogdanm 89:552587b429a1 52
bogdanm 89:552587b429a1 53 /**@brief Button event handler type. */
bogdanm 89:552587b429a1 54 typedef void (*app_button_handler_t)(uint8_t pin_no, uint8_t button_action);
bogdanm 89:552587b429a1 55
bogdanm 89:552587b429a1 56 /**@brief Type of function for passing events from the Button Handler module to the scheduler. */
bogdanm 89:552587b429a1 57 typedef uint32_t (*app_button_evt_schedule_func_t) (app_button_handler_t button_handler,
bogdanm 89:552587b429a1 58 uint8_t pin_no,
bogdanm 89:552587b429a1 59 uint8_t button_action);
bogdanm 89:552587b429a1 60
bogdanm 89:552587b429a1 61 /**@brief Button configuration structure. */
bogdanm 89:552587b429a1 62 typedef struct
bogdanm 89:552587b429a1 63 {
bogdanm 89:552587b429a1 64 uint8_t pin_no; /**< Pin to be used as a button. */
bogdanm 89:552587b429a1 65 uint8_t active_state; /**< APP_BUTTON_ACTIVE_HIGH or APP_BUTTON_ACTIVE_LOW. */
bogdanm 89:552587b429a1 66 nrf_gpio_pin_pull_t pull_cfg; /**< Pull-up or -down configuration. */
bogdanm 89:552587b429a1 67 app_button_handler_t button_handler; /**< Handler to be called when button is pushed. */
bogdanm 89:552587b429a1 68 } app_button_cfg_t;
bogdanm 89:552587b429a1 69
bogdanm 89:552587b429a1 70 /**@brief Pin transition direction struct. */
bogdanm 89:552587b429a1 71 typedef struct
bogdanm 89:552587b429a1 72 {
bogdanm 89:552587b429a1 73 uint32_t high_to_low; /**Pin went from high to low */
bogdanm 89:552587b429a1 74 uint32_t low_to_high; /**Pin went from low to high */
bogdanm 89:552587b429a1 75 } pin_transition_t;
bogdanm 89:552587b429a1 76
bogdanm 89:552587b429a1 77 /**@brief Macro for initializing the Button Handler module.
bogdanm 89:552587b429a1 78 *
bogdanm 89:552587b429a1 79 * @details It will initialize the specified pins as buttons, and configure the Button Handler
bogdanm 89:552587b429a1 80 * module as a GPIOTE user (but it will not enable button detection). It will also connect
bogdanm 89:552587b429a1 81 * the Button Handler module to the scheduler (if specified).
bogdanm 89:552587b429a1 82 *
bogdanm 89:552587b429a1 83 * @param[in] BUTTONS Array of buttons to be used (type app_button_cfg_t, must be
bogdanm 89:552587b429a1 84 * static!).
bogdanm 89:552587b429a1 85 * @param[in] BUTTON_COUNT Number of buttons.
bogdanm 89:552587b429a1 86 * @param[in] DETECTION_DELAY Delay from a GPIOTE event until a button is reported as pushed.
bogdanm 89:552587b429a1 87 * @param[in] USE_SCHEDULER TRUE if the application is using the event scheduler,
bogdanm 89:552587b429a1 88 * FALSE otherwise.
bogdanm 89:552587b429a1 89 */
bogdanm 89:552587b429a1 90 /*lint -emacro(506, APP_BUTTON_INIT) */ /* Suppress "Constant value Boolean */
bogdanm 89:552587b429a1 91 #define APP_BUTTON_INIT(BUTTONS, BUTTON_COUNT, DETECTION_DELAY, USE_SCHEDULER) \
bogdanm 89:552587b429a1 92 do \
bogdanm 89:552587b429a1 93 { \
bogdanm 89:552587b429a1 94 uint32_t ERR_CODE = app_button_init((BUTTONS), \
bogdanm 89:552587b429a1 95 (BUTTON_COUNT), \
bogdanm 89:552587b429a1 96 (DETECTION_DELAY), \
bogdanm 89:552587b429a1 97 (USE_SCHEDULER) ? app_button_evt_schedule : NULL); \
bogdanm 89:552587b429a1 98 APP_ERROR_CHECK(ERR_CODE); \
bogdanm 89:552587b429a1 99 } while (0)
bogdanm 89:552587b429a1 100
bogdanm 89:552587b429a1 101 /**@brief Function for initializing the Buttons.
bogdanm 89:552587b429a1 102 *
bogdanm 89:552587b429a1 103 * @details This function will initialize the specified pins as buttons, and configure the Button
bogdanm 89:552587b429a1 104 * Handler module as a GPIOTE user (but it will not enable button detection).
bogdanm 89:552587b429a1 105 *
bogdanm 89:552587b429a1 106 * @note Normally initialization should be done using the APP_BUTTON_INIT() macro, as that will take
bogdanm 89:552587b429a1 107 * care of connecting the Buttons module to the scheduler (if specified).
bogdanm 89:552587b429a1 108 *
bogdanm 89:552587b429a1 109 * @note app_button_enable() function must be called in order to enable the button detection.
bogdanm 89:552587b429a1 110 *
bogdanm 89:552587b429a1 111 * @param[in] p_buttons Array of buttons to be used (NOTE: Must be static!).
bogdanm 89:552587b429a1 112 * @param[in] button_count Number of buttons.
bogdanm 89:552587b429a1 113 * @param[in] detection_delay Delay from a GPIOTE event until a button is reported as pushed.
bogdanm 89:552587b429a1 114 * @param[in] evt_schedule_func Function for passing button events to the scheduler. Point to
bogdanm 89:552587b429a1 115 * app_button_evt_schedule() to connect to the scheduler. Set to
bogdanm 89:552587b429a1 116 * NULL to make the Buttons module call the event handler directly
bogdanm 89:552587b429a1 117 * from the delayed button push detection timeout handler.
bogdanm 89:552587b429a1 118 *
bogdanm 89:552587b429a1 119 * @return NRF_SUCCESS on success, otherwise an error code.
bogdanm 89:552587b429a1 120 */
bogdanm 89:552587b429a1 121 uint32_t app_button_init(app_button_cfg_t * p_buttons,
bogdanm 89:552587b429a1 122 uint8_t button_count,
bogdanm 89:552587b429a1 123 uint32_t detection_delay,
bogdanm 89:552587b429a1 124 app_button_evt_schedule_func_t evt_schedule_func);
bogdanm 89:552587b429a1 125
bogdanm 89:552587b429a1 126 /**@brief Function for enabling button detection.
bogdanm 89:552587b429a1 127 *
bogdanm 89:552587b429a1 128 * @retval NRF_ERROR_INVALID_PARAM GPIOTE has to many users.
bogdanm 89:552587b429a1 129 * @retval NRF_ERROR_INVALID_STATE Button or GPIOTE not initialized.
bogdanm 89:552587b429a1 130 * @retval NRF_SUCCESS Button detection successfully enabled.
bogdanm 89:552587b429a1 131 */
bogdanm 89:552587b429a1 132 uint32_t app_button_enable(void);
bogdanm 89:552587b429a1 133
bogdanm 89:552587b429a1 134 /**@brief Function for disabling button detection.
bogdanm 89:552587b429a1 135 *
bogdanm 89:552587b429a1 136 * @retval NRF_ERROR_INVALID_PARAM GPIOTE has to many users.
bogdanm 89:552587b429a1 137 * @retval NRF_ERROR_INVALID_STATE Button or GPIOTE not initialized.
bogdanm 89:552587b429a1 138 * @retval NRF_SUCCESS Button detection successfully enabled.
bogdanm 89:552587b429a1 139 */
bogdanm 89:552587b429a1 140 uint32_t app_button_disable(void);
bogdanm 89:552587b429a1 141
bogdanm 89:552587b429a1 142 /**@brief Function for checking if a button is currently being pushed.
bogdanm 89:552587b429a1 143 *
bogdanm 89:552587b429a1 144 * @param[in] pin_no Button pin to be checked.
bogdanm 89:552587b429a1 145 * @param[out] p_is_pushed Button state.
bogdanm 89:552587b429a1 146 *
bogdanm 89:552587b429a1 147 * @retval NRF_SUCCESS State successfully read.
bogdanm 89:552587b429a1 148 * @retval NRF_ERROR_INVALID_PARAM Invalid pin_no.
bogdanm 89:552587b429a1 149 */
bogdanm 89:552587b429a1 150 uint32_t app_button_is_pushed(uint8_t pin_no, bool * p_is_pushed);
bogdanm 89:552587b429a1 151
bogdanm 89:552587b429a1 152
bogdanm 89:552587b429a1 153 // Type and functions for connecting the Buttons module to the scheduler:
bogdanm 89:552587b429a1 154
bogdanm 89:552587b429a1 155 /**@cond NO_DOXYGEN */
bogdanm 89:552587b429a1 156 typedef struct
bogdanm 89:552587b429a1 157 {
bogdanm 89:552587b429a1 158 app_button_handler_t button_handler;
bogdanm 89:552587b429a1 159 uint8_t pin_no;
bogdanm 89:552587b429a1 160 uint8_t button_action;
bogdanm 89:552587b429a1 161 } app_button_event_t;
bogdanm 89:552587b429a1 162
bogdanm 89:552587b429a1 163 static __INLINE void app_button_evt_get(void * p_event_data, uint16_t event_size)
bogdanm 89:552587b429a1 164 {
bogdanm 89:552587b429a1 165 app_button_event_t * p_buttons_event = (app_button_event_t *)p_event_data;
bogdanm 89:552587b429a1 166
bogdanm 89:552587b429a1 167 APP_ERROR_CHECK_BOOL(event_size == sizeof(app_button_event_t));
bogdanm 89:552587b429a1 168 p_buttons_event->button_handler(p_buttons_event->pin_no, p_buttons_event->button_action);
bogdanm 89:552587b429a1 169 }
bogdanm 89:552587b429a1 170
bogdanm 89:552587b429a1 171 static __INLINE uint32_t app_button_evt_schedule(app_button_handler_t button_handler,
bogdanm 89:552587b429a1 172 uint8_t pin_no,
bogdanm 89:552587b429a1 173 uint8_t button_action)
bogdanm 89:552587b429a1 174 {
bogdanm 89:552587b429a1 175 app_button_event_t buttons_event;
bogdanm 89:552587b429a1 176
bogdanm 89:552587b429a1 177 buttons_event.button_handler = button_handler;
bogdanm 89:552587b429a1 178 buttons_event.pin_no = pin_no;
bogdanm 89:552587b429a1 179 buttons_event.button_action = button_action;
bogdanm 89:552587b429a1 180
bogdanm 89:552587b429a1 181 return app_sched_event_put(&buttons_event, sizeof(buttons_event), app_button_evt_get);
bogdanm 89:552587b429a1 182 }
bogdanm 89:552587b429a1 183 /**@endcond */
bogdanm 89:552587b429a1 184
bogdanm 89:552587b429a1 185 #endif // APP_BUTTON_H__
bogdanm 89:552587b429a1 186
bogdanm 89:552587b429a1 187 /** @} */