Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of nRF51822 by
app_gpiote.h
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 #define GPIOTE_USER_NODE_SIZE 20 /**< Size of app_gpiote.gpiote_user_t (only for use inside APP_GPIOTE_BUF_SIZE()). */ 00050 #define NO_OF_PINS 32 /**< Number of GPIO pins on the nRF51 chip. */ 00051 00052 /**@brief Compute number of bytes required to hold the GPIOTE data structures. 00053 * 00054 * @param[in] MAX_USERS Maximum number of GPIOTE users. 00055 * 00056 * @return Required buffer size (in bytes). 00057 */ 00058 #define APP_GPIOTE_BUF_SIZE(MAX_USERS) ((MAX_USERS) * GPIOTE_USER_NODE_SIZE) 00059 00060 typedef uint8_t app_gpiote_user_id_t; 00061 00062 /**@brief GPIOTE event handler type. */ 00063 typedef void (*app_gpiote_event_handler_t)(uint32_t event_pins_low_to_high, 00064 uint32_t event_pins_high_to_low); 00065 00066 /**@brief GPIOTE input event handler type. */ 00067 typedef void (*app_gpiote_input_event_handler_t)(void); 00068 00069 /**@brief Macro for initializing the GPIOTE module. 00070 * 00071 * @details It will handle dimensioning and allocation of the memory buffer required by the module, 00072 * making sure that the buffer is correctly aligned. 00073 * 00074 * @param[in] MAX_USERS Maximum number of GPIOTE users. 00075 * 00076 * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it 00077 * several times as long as it is from the same location, e.g. to do a reinitialization). 00078 */ 00079 /*lint -emacro(506, APP_GPIOTE_INIT) */ /* Suppress "Constant value Boolean */ 00080 #define APP_GPIOTE_INIT(MAX_USERS) \ 00081 do \ 00082 { \ 00083 static uint32_t app_gpiote_buf[CEIL_DIV(APP_GPIOTE_BUF_SIZE(MAX_USERS), sizeof(uint32_t))];\ 00084 uint32_t ERR_CODE = app_gpiote_init((MAX_USERS), app_gpiote_buf); \ 00085 APP_ERROR_CHECK(ERR_CODE); \ 00086 } while (0) 00087 00088 /**@brief Function for initializing the GPIOTE module. 00089 * 00090 * @note Normally initialization should be done using the APP_GPIOTE_INIT() macro, as that will 00091 * allocate the buffer needed by the GPIOTE module (including aligning the buffer correctly). 00092 * 00093 * @param[in] max_users Maximum number of GPIOTE users. 00094 * @param[in] p_buffer Pointer to memory buffer for internal use in the app_gpiote 00095 * module. The size of the buffer can be computed using the 00096 * APP_GPIOTE_BUF_SIZE() macro. The buffer must be aligned to 00097 * a 4 byte boundary. 00098 * 00099 * @retval NRF_SUCCESS Successful initialization. 00100 * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte 00101 * boundary). 00102 */ 00103 uint32_t app_gpiote_init(uint8_t max_users, void * p_buffer); 00104 00105 /**@brief Function for registering a GPIOTE user. 00106 * 00107 * @param[out] p_user_id Id for the new GPIOTE user. 00108 * @param[in] pins_low_to_high_mask Mask defining which pins will generate events to this user 00109 * when state is changed from low->high. 00110 * @param[in] pins_high_to_low_mask Mask defining which pins will generate events to this user 00111 * when state is changed from high->low. 00112 * @param[in] event_handler Pointer to function to be executed when an event occurs. 00113 * 00114 * @retval NRF_SUCCESS Successful initialization. 00115 * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte boundary). 00116 * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE 00117 * module. 00118 * @retval NRF_ERROR_NO_MEM Returned if the application tries to register more users 00119 * than defined when the GPIOTE module was initialized in 00120 * @ref app_gpiote_init. 00121 */ 00122 uint32_t app_gpiote_user_register(app_gpiote_user_id_t * p_user_id, 00123 uint32_t pins_low_to_high_mask, 00124 uint32_t pins_high_to_low_mask, 00125 app_gpiote_event_handler_t event_handler); 00126 00127 /**@brief Function for informing the GPIOTE module that the specified user wants to use the GPIOTE module. 00128 * 00129 * @param[in] user_id Id of user to enable. 00130 * 00131 * @retval NRF_SUCCESS On success. 00132 * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user. 00133 * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE 00134 * module. 00135 */ 00136 uint32_t app_gpiote_user_enable(app_gpiote_user_id_t user_id); 00137 00138 /**@brief Function for informing the GPIOTE module that the specified user is done using the GPIOTE module. 00139 * 00140 * @param[in] user_id Id of user to enable. 00141 * 00142 * @return NRF_SUCCESS On success. 00143 * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user. 00144 * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE 00145 * module. 00146 */ 00147 uint32_t app_gpiote_user_disable(app_gpiote_user_id_t user_id); 00148 00149 /**@brief Function for getting the state of the pins which are registered for the specified user. 00150 * 00151 * @param[in] user_id Id of user to check. 00152 * @param[out] p_pins Bit mask corresponding to the pins configured to generate events to 00153 * the specified user. All bits corresponding to pins in the state 00154 * 'high' will have value '1', all others will have value '0'. 00155 * 00156 * @return NRF_SUCCESS On success. 00157 * @retval NRF_ERROR_INVALID_PARAM Invalid user_id provided, No a valid user. 00158 * @retval NRF_ERROR_INALID_STATE If @ref app_gpiote_init has not been called on the GPIOTE 00159 * module. 00160 */ 00161 uint32_t app_gpiote_pins_state_get(app_gpiote_user_id_t user_id, uint32_t * p_pins); 00162 00163 /**@brief Function for registering event handlers for GPIOTE IN events. 00164 * 00165 * @param[in] channel GPIOTE channel [0..3]. 00166 * @param[in] pin Pins associated with GPIOTE channel. Changes on following pins will generate events. 00167 * @param[in] polarity Specify operation on input that shall trigger IN event. 00168 * @param[in] event_handler Event handler invoked on the IN event in the GPIOTE interrupt. 00169 * 00170 * @return NRF_SUCCESS On success. 00171 * @retval NRF_ERROR_INVALID_PARAM Invalid channel or pin number. 00172 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events. 00173 */ 00174 uint32_t app_gpiote_input_event_handler_register(const uint8_t channel, 00175 const uint32_t pin, 00176 const uint32_t polarity, 00177 app_gpiote_input_event_handler_t event_handler); 00178 00179 /**@brief Function for unregistering event handlers for GPIOTE IN events. 00180 * 00181 * @return NRF_SUCCESS On success. 00182 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events. 00183 */ 00184 uint32_t app_gpiote_input_event_handler_unregister(const uint8_t channel); 00185 00186 /**@brief Function for registering event handler invoked at the end of a GPIOTE interrupt. 00187 * 00188 * @param[in] event_handler Event handler invoked at the end of the GPIOTE interrupt. 00189 * 00190 * @return NRF_SUCCESS On success. 00191 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events. 00192 */ 00193 uint32_t app_gpiote_end_irq_event_handler_register(app_gpiote_input_event_handler_t event_handler); 00194 00195 /**@brief Function for unregistering event handler invoked at the end of a GPIOTE interrupt. 00196 * 00197 * @return NRF_SUCCESS On success. 00198 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support IN events. 00199 */ 00200 uint32_t app_gpiote_end_irq_event_handler_unregister(void); 00201 00202 /**@brief Function for enabling interrupts in the GPIOTE driver. 00203 * 00204 * @return NRF_SUCCESS On success. 00205 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support. 00206 */ 00207 uint32_t app_gpiote_enable_interrupts(void); 00208 00209 /**@brief Function for disabling interrupts in the GPIOTE driver. 00210 * 00211 * @return NRF_SUCCESS On success. 00212 * @retval NRF_ERROR_NOT_SUPPORTED Driver doesn't support. 00213 */ 00214 uint32_t app_gpiote_disable_interrupts(void); 00215 00216 00217 #endif // APP_GPIOTE_H__ 00218 00219 /** @} */
Generated on Tue Jul 12 2022 16:21:02 by
