SG RFID nRF51822 fork

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers app_gpiote.h Source File

app_gpiote.h

Go to the documentation of this file.
00001 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
00002  *
00003  * The information contained herein is property of Nordic Semiconductor ASA.
00004  * Terms and conditions of usage are described in detail in NORDIC
00005  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
00006  *
00007  * Licensees are granted free, non-transferable use of the information. NO
00008  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
00009  * the file.
00010  *
00011  */
00012 
00013 /** @file
00014  *
00015  * @defgroup app_gpiote GPIOTE Handler
00016  * @{
00017  * @ingroup app_common
00018  *
00019  * @brief GPIOTE handler module.
00020  *
00021  * @details The GPIOTE handler allows several modules ("users") to share the GPIOTE interrupt,
00022  *          each user defining a set of pins able to generate events to the user.
00023  *          When a GPIOTE interrupt occurs, the GPIOTE interrupt handler will call the event handler
00024  *          of each user for which at least one of the pins generated an event.
00025  *
00026  *          The GPIOTE users are responsible for configuring all their corresponding pins, except
00027  *          the SENSE field, which should be initialized to GPIO_PIN_CNF_SENSE_Disabled.
00028  *          The SENSE field will be updated by the GPIOTE module when it is enabled or disabled,
00029  *          and also while it is enabled.
00030  *
00031  *          The module specifies on which pins events should be generated if the pin(s) goes
00032  *          from low->high or high->low or both directions.
00033  *
00034  * @note    Even if the application is using the @ref app_scheduler, the GPIOTE event handlers will
00035  *          be called directly from the GPIOTE interrupt handler.
00036  *
00037  * @warning If multiple users registers for the same pins the behavior for those pins are undefined.
00038  */
00039 
00040 #ifndef APP_GPIOTE_H__
00041 #define APP_GPIOTE_H__
00042 
00043 #include <stdint.h>
00044 #include <stdbool.h>
00045 #include "nrf.h"
00046 #include "app_error.h "
00047 #include "app_util.h "
00048 
00049 #ifdef __cpluplus
00050 extern "C" {
00051 #endif
00052 
00053 #define GPIOTE_USER_NODE_SIZE   20          /**< Size of app_gpiote.gpiote_user_t (only for use inside APP_GPIOTE_BUF_SIZE()). */
00054 #define NO_OF_PINS              32          /**< Number of GPIO pins on the nRF51 chip. */
00055 
00056 /**@brief Compute number of bytes required to hold the GPIOTE data structures.
00057  *
00058  * @param[in]   MAX_USERS   Maximum number of GPIOTE users.
00059  *
00060  * @return      Required buffer size (in bytes).
00061  */
00062 #define APP_GPIOTE_BUF_SIZE(MAX_USERS)  ((MAX_USERS) * GPIOTE_USER_NODE_SIZE)
00063 
00064 typedef uint8_t app_gpiote_user_id_t;
00065 
00066 /**@brief GPIOTE event handler type. */
00067 typedef void (*app_gpiote_event_handler_t)(uint32_t event_pins_low_to_high,
00068                                            uint32_t event_pins_high_to_low);
00069 
00070 /**@brief GPIOTE input event handler type. */
00071 typedef void (*app_gpiote_input_event_handler_t)(void);
00072 
00073 /**@brief Macro for initializing the GPIOTE module.
00074  *
00075  * @details It will handle dimensioning and allocation of the memory buffer required by the module,
00076  *          making sure that the buffer is correctly aligned.
00077  *
00078  * @param[in]   MAX_USERS   Maximum number of GPIOTE users.
00079  *
00080  * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
00081  *       several times as long as it is from the same location, e.g. to do a reinitialization).
00082  */
00083 /*lint -emacro(506, APP_GPIOTE_INIT) */ /* Suppress "Constant value Boolean */
00084 #define APP_GPIOTE_INIT(MAX_USERS)                                                                 \
00085     do                                                                                             \
00086     {                                                                                              \
00087         static uint32_t app_gpiote_buf[CEIL_DIV(APP_GPIOTE_BUF_SIZE(MAX_USERS), sizeof(uint32_t))];\
00088         uint32_t ERR_CODE = app_gpiote_init((MAX_USERS), app_gpiote_buf);                          \
00089         APP_ERROR_CHECK(ERR_CODE);                                                                 \
00090     } while (0)
00091 
00092 /**@brief Function for initializing the GPIOTE module.
00093  *
00094  * @note Normally initialization should be done using the APP_GPIOTE_INIT() macro, as that will
00095  *       allocate the buffer needed by the GPIOTE module (including aligning the buffer correctly).
00096  *
00097  * @param[in]   max_users               Maximum number of GPIOTE users.
00098  * @param[in]   p_buffer                Pointer to memory buffer for internal use in the app_gpiote
00099  *                                      module. The size of the buffer can be computed using the
00100  *                                      APP_GPIOTE_BUF_SIZE() macro. The buffer must be aligned to 
00101  *                                      a 4 byte boundary.
00102  *
00103  * @retval      NRF_SUCCESS             Successful initialization.
00104  * @retval      NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte
00105  *                                      boundary).
00106  */
00107 uint32_t app_gpiote_init(uint8_t max_users, void * p_buffer);
00108 
00109 /**@brief Function for registering a GPIOTE user.
00110  *
00111  * @param[out]  p_user_id               Id for the new GPIOTE user.
00112  * @param[in]   pins_low_to_high_mask   Mask defining which pins will generate events to this user 
00113  *                                      when state is changed from low->high.
00114  * @param[in]   pins_high_to_low_mask   Mask defining which pins will generate events to this user
00115  *                                      when state is changed from high->low.
00116  * @param[in]   event_handler           Pointer to function to be executed when an event occurs.
00117  *
00118  * @retval      NRF_SUCCESS             Successful initialization.
00119  * @retval      NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte boundary).
00120  * @retval      NRF_ERROR_INALID_STATE  If @ref app_gpiote_init has not been called on the GPIOTE
00121  *                                      module.
00122  * @retval      NRF_ERROR_NO_MEM        Returned if the application tries to register more users
00123  *                                      than defined when the GPIOTE module was initialized in
00124  *                                      @ref app_gpiote_init.
00125  */
00126 uint32_t app_gpiote_user_register(app_gpiote_user_id_t *     p_user_id,
00127                                   uint32_t                   pins_low_to_high_mask,
00128                                   uint32_t                   pins_high_to_low_mask,
00129                                   app_gpiote_event_handler_t event_handler);
00130 
00131 /**@brief Function for informing the GPIOTE module that the specified user wants to use the GPIOTE module.
00132  *
00133  * @param[in]   user_id                 Id of user to enable.
00134  *
00135  * @retval      NRF_SUCCESS             On success.
00136  * @retval      NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user.
00137  * @retval      NRF_ERROR_INALID_STATE  If @ref app_gpiote_init has not been called on the GPIOTE
00138  *                                      module.
00139  */
00140 uint32_t app_gpiote_user_enable(app_gpiote_user_id_t user_id);
00141 
00142 /**@brief Function for informing the GPIOTE module that the specified user is done using the GPIOTE module.
00143  *
00144  * @param[in]   user_id                   Id of user to enable.
00145  *
00146  * @return      NRF_SUCCESS               On success.
00147  * @retval      NRF_ERROR_INVALID_PARAM   Invalid user_id provided, No a valid user.
00148  * @retval      NRF_ERROR_INALID_STATE    If @ref app_gpiote_init has not been called on the GPIOTE
00149  *                                        module.
00150  */
00151 uint32_t app_gpiote_user_disable(app_gpiote_user_id_t user_id);
00152 
00153 /**@brief Function for getting the state of the pins which are registered for the specified user.
00154  *
00155  * @param[in]   user_id         Id of user to check.
00156  * @param[out]  p_pins          Bit mask corresponding to the pins configured to generate events to
00157  *                              the specified user. All bits corresponding to pins in the state
00158  *                              'high' will have value '1', all others will have value '0'.
00159  *
00160  * @return      NRF_SUCCESS               On success.
00161  * @retval      NRF_ERROR_INVALID_PARAM   Invalid user_id provided, No a valid user.
00162  * @retval      NRF_ERROR_INALID_STATE    If @ref app_gpiote_init has not been called on the GPIOTE
00163  *                                        module.
00164  */
00165 uint32_t app_gpiote_pins_state_get(app_gpiote_user_id_t user_id, uint32_t * p_pins);
00166 
00167 /**@brief Function for registering event handlers for GPIOTE IN events.
00168  *
00169  * @param[in] channel         GPIOTE channel [0..3].
00170  * @param[in] pin             Pins associated with GPIOTE channel. Changes on following pins will generate events.
00171  * @param[in] polarity        Specify operation on input that shall trigger IN event.
00172  * @param[in] event_handler   Event handler invoked on the IN event in the GPIOTE interrupt.
00173  *
00174  * @return   NRF_SUCCESS                 On success.
00175  * @retval   NRF_ERROR_INVALID_PARAM     Invalid channel or pin number.
00176  * @retval   NRF_ERROR_NOT_SUPPORTED     Driver doesn't support IN events.
00177  */
00178 uint32_t app_gpiote_input_event_handler_register(const uint8_t channel,
00179                                                  const uint32_t pin,
00180                                                  const uint32_t polarity,
00181                                                  app_gpiote_input_event_handler_t event_handler);
00182 
00183 /**@brief Function for unregistering event handlers for GPIOTE IN events.
00184  *
00185  * @return   NRF_SUCCESS                 On success.
00186  * @retval   NRF_ERROR_NOT_SUPPORTED     Driver doesn't support IN events.
00187  */
00188 uint32_t app_gpiote_input_event_handler_unregister(const uint8_t channel);
00189 
00190 /**@brief Function for registering event handler invoked at the end of a GPIOTE interrupt.
00191  *
00192  * @param[in] event_handler    Event handler invoked at the end of the GPIOTE interrupt.
00193  *
00194  * @return   NRF_SUCCESS                 On success.
00195  * @retval   NRF_ERROR_NOT_SUPPORTED     Driver doesn't support IN events.
00196  */
00197 uint32_t app_gpiote_end_irq_event_handler_register(app_gpiote_input_event_handler_t event_handler);
00198 
00199 /**@brief Function for unregistering event handler invoked at the end of a GPIOTE interrupt.
00200  *
00201  * @return   NRF_SUCCESS                 On success.
00202  * @retval   NRF_ERROR_NOT_SUPPORTED     Driver doesn't support IN events.
00203  */
00204 uint32_t app_gpiote_end_irq_event_handler_unregister(void);
00205 
00206 /**@brief Function for enabling interrupts in the GPIOTE driver.
00207  *
00208  * @return   NRF_SUCCESS                 On success.
00209  * @retval   NRF_ERROR_NOT_SUPPORTED     Driver doesn't support.
00210  */
00211 uint32_t app_gpiote_enable_interrupts(void);
00212 
00213 /**@brief Function for disabling interrupts in the GPIOTE driver.
00214  *
00215  * @return   NRF_SUCCESS                 On success.
00216  * @retval   NRF_ERROR_NOT_SUPPORTED     Driver doesn't support.
00217  */
00218 uint32_t app_gpiote_disable_interrupts(void);
00219 
00220 #ifdef __cpluplus
00221 }
00222 #endif
00223 
00224 #endif // APP_GPIOTE_H__
00225 
00226 /** @} */