![](/media/cache/profiles/312e53ccba0b2ddfefd4a1f05b55bcdd.jpg.50x50_q85.jpg)
BLE FOTA APP
It doesn't work with the default FOTA bootloader. It use NVIC_SystemReset() to enter a bootloader.
nRF51822/nordic/app_common/app_gpiote.c@1:a607cd9655d7, 2014-10-10 (annotated)
- 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?
User | Revision | Line number | New 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 | #if NEED_APP_GPIOTE /* disabled by default */ |
yihui | 1:a607cd9655d7 | 14 | |
yihui | 1:a607cd9655d7 | 15 | #include "app_gpiote.h" |
yihui | 1:a607cd9655d7 | 16 | #include <stdlib.h> |
yihui | 1:a607cd9655d7 | 17 | #include <string.h> |
yihui | 1:a607cd9655d7 | 18 | #include "app_util.h" |
yihui | 1:a607cd9655d7 | 19 | #include "app_util_platform.h" |
yihui | 1:a607cd9655d7 | 20 | #include "nrf_error.h" |
yihui | 1:a607cd9655d7 | 21 | #include "nrf_gpio.h" |
yihui | 1:a607cd9655d7 | 22 | |
yihui | 1:a607cd9655d7 | 23 | |
yihui | 1:a607cd9655d7 | 24 | /**@brief GPIOTE user type. */ |
yihui | 1:a607cd9655d7 | 25 | typedef struct |
yihui | 1:a607cd9655d7 | 26 | { |
yihui | 1:a607cd9655d7 | 27 | uint32_t pins_mask; /**< Mask defining which pins user wants to monitor. */ |
yihui | 1:a607cd9655d7 | 28 | uint32_t pins_low_to_high_mask; /**< Mask defining which pins will generate events to this user when toggling low->high. */ |
yihui | 1:a607cd9655d7 | 29 | uint32_t pins_high_to_low_mask; /**< Mask defining which pins will generate events to this user when toggling high->low. */ |
yihui | 1:a607cd9655d7 | 30 | uint32_t sense_high_pins; /**< Mask defining which pins are configured to generate GPIOTE interrupt on transition to high level. */ |
yihui | 1:a607cd9655d7 | 31 | app_gpiote_event_handler_t event_handler; /**< Pointer to function to be executed when an event occurs. */ |
yihui | 1:a607cd9655d7 | 32 | } gpiote_user_t; |
yihui | 1:a607cd9655d7 | 33 | |
yihui | 1:a607cd9655d7 | 34 | STATIC_ASSERT(sizeof(gpiote_user_t) <= GPIOTE_USER_NODE_SIZE); |
yihui | 1:a607cd9655d7 | 35 | STATIC_ASSERT(sizeof(gpiote_user_t) % 4 == 0); |
yihui | 1:a607cd9655d7 | 36 | |
yihui | 1:a607cd9655d7 | 37 | static uint32_t m_enabled_users_mask; /**< Mask for tracking which users are enabled. */ |
yihui | 1:a607cd9655d7 | 38 | static uint8_t m_user_array_size; /**< Size of user array. */ |
yihui | 1:a607cd9655d7 | 39 | static uint8_t m_user_count; /**< Number of registered users. */ |
yihui | 1:a607cd9655d7 | 40 | static gpiote_user_t * mp_users = NULL; /**< Array of GPIOTE users. */ |
yihui | 1:a607cd9655d7 | 41 | |
yihui | 1:a607cd9655d7 | 42 | |
yihui | 1:a607cd9655d7 | 43 | /**@brief Function for toggling sense level for specified pins. |
yihui | 1:a607cd9655d7 | 44 | * |
yihui | 1:a607cd9655d7 | 45 | * @param[in] p_user Pointer to user structure. |
yihui | 1:a607cd9655d7 | 46 | * @param[in] pins Bitmask specifying for which pins the sense level is to be toggled. |
yihui | 1:a607cd9655d7 | 47 | */ |
yihui | 1:a607cd9655d7 | 48 | static void sense_level_toggle(gpiote_user_t * p_user, uint32_t pins) |
yihui | 1:a607cd9655d7 | 49 | { |
yihui | 1:a607cd9655d7 | 50 | uint32_t pin_no; |
yihui | 1:a607cd9655d7 | 51 | |
yihui | 1:a607cd9655d7 | 52 | for (pin_no = 0; pin_no < NO_OF_PINS; pin_no++) |
yihui | 1:a607cd9655d7 | 53 | { |
yihui | 1:a607cd9655d7 | 54 | uint32_t pin_mask = (1 << pin_no); |
yihui | 1:a607cd9655d7 | 55 | |
yihui | 1:a607cd9655d7 | 56 | if ((pins & pin_mask) != 0) |
yihui | 1:a607cd9655d7 | 57 | { |
yihui | 1:a607cd9655d7 | 58 | uint32_t sense; |
yihui | 1:a607cd9655d7 | 59 | |
yihui | 1:a607cd9655d7 | 60 | // Invert sensing. |
yihui | 1:a607cd9655d7 | 61 | if ((p_user->sense_high_pins & pin_mask) == 0) |
yihui | 1:a607cd9655d7 | 62 | { |
yihui | 1:a607cd9655d7 | 63 | sense = GPIO_PIN_CNF_SENSE_High << GPIO_PIN_CNF_SENSE_Pos; |
yihui | 1:a607cd9655d7 | 64 | p_user->sense_high_pins |= pin_mask; |
yihui | 1:a607cd9655d7 | 65 | } |
yihui | 1:a607cd9655d7 | 66 | else |
yihui | 1:a607cd9655d7 | 67 | { |
yihui | 1:a607cd9655d7 | 68 | sense = GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos; |
yihui | 1:a607cd9655d7 | 69 | p_user->sense_high_pins &= ~pin_mask; |
yihui | 1:a607cd9655d7 | 70 | } |
yihui | 1:a607cd9655d7 | 71 | |
yihui | 1:a607cd9655d7 | 72 | NRF_GPIO->PIN_CNF[pin_no] &= ~GPIO_PIN_CNF_SENSE_Msk; |
yihui | 1:a607cd9655d7 | 73 | NRF_GPIO->PIN_CNF[pin_no] |= sense; |
yihui | 1:a607cd9655d7 | 74 | } |
yihui | 1:a607cd9655d7 | 75 | } |
yihui | 1:a607cd9655d7 | 76 | } |
yihui | 1:a607cd9655d7 | 77 | |
yihui | 1:a607cd9655d7 | 78 | |
yihui | 1:a607cd9655d7 | 79 | /**@brief Function for handling the GPIOTE interrupt. |
yihui | 1:a607cd9655d7 | 80 | */ |
yihui | 1:a607cd9655d7 | 81 | void GPIOTE_IRQHandler(void) |
yihui | 1:a607cd9655d7 | 82 | { |
yihui | 1:a607cd9655d7 | 83 | uint8_t i; |
yihui | 1:a607cd9655d7 | 84 | uint32_t pins_changed; |
yihui | 1:a607cd9655d7 | 85 | uint32_t pins_state = NRF_GPIO->IN; |
yihui | 1:a607cd9655d7 | 86 | |
yihui | 1:a607cd9655d7 | 87 | // Clear event. |
yihui | 1:a607cd9655d7 | 88 | NRF_GPIOTE->EVENTS_PORT = 0; |
yihui | 1:a607cd9655d7 | 89 | |
yihui | 1:a607cd9655d7 | 90 | // Check all users. |
yihui | 1:a607cd9655d7 | 91 | for (i = 0; i < m_user_count; i++) |
yihui | 1:a607cd9655d7 | 92 | { |
yihui | 1:a607cd9655d7 | 93 | gpiote_user_t * p_user = &mp_users[i]; |
yihui | 1:a607cd9655d7 | 94 | |
yihui | 1:a607cd9655d7 | 95 | // Check if user is enabled. |
yihui | 1:a607cd9655d7 | 96 | if (((1 << i) & m_enabled_users_mask) != 0) |
yihui | 1:a607cd9655d7 | 97 | { |
yihui | 1:a607cd9655d7 | 98 | uint32_t transition_pins; |
yihui | 1:a607cd9655d7 | 99 | uint32_t event_low_to_high; |
yihui | 1:a607cd9655d7 | 100 | uint32_t event_high_to_low; |
yihui | 1:a607cd9655d7 | 101 | |
yihui | 1:a607cd9655d7 | 102 | // Find set of pins on which there has been a transition. |
yihui | 1:a607cd9655d7 | 103 | transition_pins = (pins_state ^ ~p_user->sense_high_pins) & p_user->pins_mask; |
yihui | 1:a607cd9655d7 | 104 | |
yihui | 1:a607cd9655d7 | 105 | // Toggle SENSE level for all pins that have changed state. |
yihui | 1:a607cd9655d7 | 106 | sense_level_toggle(p_user, transition_pins); |
yihui | 1:a607cd9655d7 | 107 | |
yihui | 1:a607cd9655d7 | 108 | // Second read after setting sense. |
yihui | 1:a607cd9655d7 | 109 | // Check if any pins have changed while serving this interrupt. |
yihui | 1:a607cd9655d7 | 110 | pins_changed = NRF_GPIO->IN ^ pins_state; |
yihui | 1:a607cd9655d7 | 111 | if (pins_changed) |
yihui | 1:a607cd9655d7 | 112 | { |
yihui | 1:a607cd9655d7 | 113 | // Transition pins detected in late stage. |
yihui | 1:a607cd9655d7 | 114 | uint32_t late_transition_pins; |
yihui | 1:a607cd9655d7 | 115 | |
yihui | 1:a607cd9655d7 | 116 | pins_state |= pins_changed; |
yihui | 1:a607cd9655d7 | 117 | |
yihui | 1:a607cd9655d7 | 118 | // Find set of pins on which there has been a transition. |
yihui | 1:a607cd9655d7 | 119 | late_transition_pins = (pins_state ^ ~p_user->sense_high_pins) & p_user->pins_mask; |
yihui | 1:a607cd9655d7 | 120 | |
yihui | 1:a607cd9655d7 | 121 | // Toggle SENSE level for all pins that have changed state in last phase. |
yihui | 1:a607cd9655d7 | 122 | sense_level_toggle(p_user, late_transition_pins); |
yihui | 1:a607cd9655d7 | 123 | |
yihui | 1:a607cd9655d7 | 124 | // Update pins that has changed state since the interrupt occurred. |
yihui | 1:a607cd9655d7 | 125 | transition_pins |= late_transition_pins; |
yihui | 1:a607cd9655d7 | 126 | } |
yihui | 1:a607cd9655d7 | 127 | |
yihui | 1:a607cd9655d7 | 128 | // Call user event handler if an event has occurred. |
yihui | 1:a607cd9655d7 | 129 | event_high_to_low = (~pins_state & p_user->pins_high_to_low_mask) & transition_pins; |
yihui | 1:a607cd9655d7 | 130 | event_low_to_high = (pins_state & p_user->pins_low_to_high_mask) & transition_pins; |
yihui | 1:a607cd9655d7 | 131 | |
yihui | 1:a607cd9655d7 | 132 | if ((event_low_to_high | event_high_to_low) != 0) |
yihui | 1:a607cd9655d7 | 133 | { |
yihui | 1:a607cd9655d7 | 134 | p_user->event_handler(event_low_to_high, event_high_to_low); |
yihui | 1:a607cd9655d7 | 135 | } |
yihui | 1:a607cd9655d7 | 136 | } |
yihui | 1:a607cd9655d7 | 137 | } |
yihui | 1:a607cd9655d7 | 138 | } |
yihui | 1:a607cd9655d7 | 139 | |
yihui | 1:a607cd9655d7 | 140 | |
yihui | 1:a607cd9655d7 | 141 | /**@brief Function for sense disabling for all pins for specified user. |
yihui | 1:a607cd9655d7 | 142 | * |
yihui | 1:a607cd9655d7 | 143 | * @param[in] user_id User id. |
yihui | 1:a607cd9655d7 | 144 | */ |
yihui | 1:a607cd9655d7 | 145 | static void pins_sense_disable(app_gpiote_user_id_t user_id) |
yihui | 1:a607cd9655d7 | 146 | { |
yihui | 1:a607cd9655d7 | 147 | uint32_t pin_no; |
yihui | 1:a607cd9655d7 | 148 | |
yihui | 1:a607cd9655d7 | 149 | for (pin_no = 0; pin_no < 32; pin_no++) |
yihui | 1:a607cd9655d7 | 150 | { |
yihui | 1:a607cd9655d7 | 151 | if ((mp_users[user_id].pins_mask & (1 << pin_no)) != 0) |
yihui | 1:a607cd9655d7 | 152 | { |
yihui | 1:a607cd9655d7 | 153 | NRF_GPIO->PIN_CNF[pin_no] &= ~GPIO_PIN_CNF_SENSE_Msk; |
yihui | 1:a607cd9655d7 | 154 | NRF_GPIO->PIN_CNF[pin_no] |= GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos; |
yihui | 1:a607cd9655d7 | 155 | } |
yihui | 1:a607cd9655d7 | 156 | } |
yihui | 1:a607cd9655d7 | 157 | } |
yihui | 1:a607cd9655d7 | 158 | |
yihui | 1:a607cd9655d7 | 159 | |
yihui | 1:a607cd9655d7 | 160 | uint32_t app_gpiote_init(uint8_t max_users, void * p_buffer) |
yihui | 1:a607cd9655d7 | 161 | { |
yihui | 1:a607cd9655d7 | 162 | if (p_buffer == NULL) |
yihui | 1:a607cd9655d7 | 163 | { |
yihui | 1:a607cd9655d7 | 164 | return NRF_ERROR_INVALID_PARAM; |
yihui | 1:a607cd9655d7 | 165 | } |
yihui | 1:a607cd9655d7 | 166 | |
yihui | 1:a607cd9655d7 | 167 | // Check that buffer is correctly aligned. |
yihui | 1:a607cd9655d7 | 168 | if (!is_word_aligned(p_buffer)) |
yihui | 1:a607cd9655d7 | 169 | { |
yihui | 1:a607cd9655d7 | 170 | return NRF_ERROR_INVALID_PARAM; |
yihui | 1:a607cd9655d7 | 171 | } |
yihui | 1:a607cd9655d7 | 172 | |
yihui | 1:a607cd9655d7 | 173 | // Initialize file globals. |
yihui | 1:a607cd9655d7 | 174 | mp_users = (gpiote_user_t *)p_buffer; |
yihui | 1:a607cd9655d7 | 175 | m_user_array_size = max_users; |
yihui | 1:a607cd9655d7 | 176 | m_user_count = 0; |
yihui | 1:a607cd9655d7 | 177 | m_enabled_users_mask = 0; |
yihui | 1:a607cd9655d7 | 178 | |
yihui | 1:a607cd9655d7 | 179 | memset(mp_users, 0, m_user_array_size * sizeof(gpiote_user_t)); |
yihui | 1:a607cd9655d7 | 180 | |
yihui | 1:a607cd9655d7 | 181 | // Initialize GPIOTE interrupt (will not be enabled until app_gpiote_user_enable() is called). |
yihui | 1:a607cd9655d7 | 182 | NRF_GPIOTE->INTENCLR = 0xFFFFFFFF; |
yihui | 1:a607cd9655d7 | 183 | |
yihui | 1:a607cd9655d7 | 184 | NVIC_ClearPendingIRQ(GPIOTE_IRQn); |
yihui | 1:a607cd9655d7 | 185 | NVIC_SetPriority(GPIOTE_IRQn, APP_IRQ_PRIORITY_HIGH); |
yihui | 1:a607cd9655d7 | 186 | NVIC_EnableIRQ(GPIOTE_IRQn); |
yihui | 1:a607cd9655d7 | 187 | |
yihui | 1:a607cd9655d7 | 188 | return NRF_SUCCESS; |
yihui | 1:a607cd9655d7 | 189 | } |
yihui | 1:a607cd9655d7 | 190 | |
yihui | 1:a607cd9655d7 | 191 | |
yihui | 1:a607cd9655d7 | 192 | uint32_t app_gpiote_user_register(app_gpiote_user_id_t * p_user_id, |
yihui | 1:a607cd9655d7 | 193 | uint32_t pins_low_to_high_mask, |
yihui | 1:a607cd9655d7 | 194 | uint32_t pins_high_to_low_mask, |
yihui | 1:a607cd9655d7 | 195 | app_gpiote_event_handler_t event_handler) |
yihui | 1:a607cd9655d7 | 196 | { |
yihui | 1:a607cd9655d7 | 197 | // Check state and parameters. |
yihui | 1:a607cd9655d7 | 198 | if (mp_users == NULL) |
yihui | 1:a607cd9655d7 | 199 | { |
yihui | 1:a607cd9655d7 | 200 | return NRF_ERROR_INVALID_STATE; |
yihui | 1:a607cd9655d7 | 201 | } |
yihui | 1:a607cd9655d7 | 202 | if (event_handler == NULL) |
yihui | 1:a607cd9655d7 | 203 | { |
yihui | 1:a607cd9655d7 | 204 | return NRF_ERROR_INVALID_PARAM; |
yihui | 1:a607cd9655d7 | 205 | } |
yihui | 1:a607cd9655d7 | 206 | if (m_user_count >= m_user_array_size) |
yihui | 1:a607cd9655d7 | 207 | { |
yihui | 1:a607cd9655d7 | 208 | return NRF_ERROR_NO_MEM; |
yihui | 1:a607cd9655d7 | 209 | } |
yihui | 1:a607cd9655d7 | 210 | |
yihui | 1:a607cd9655d7 | 211 | // Allocate new user. |
yihui | 1:a607cd9655d7 | 212 | mp_users[m_user_count].pins_mask = pins_low_to_high_mask | pins_high_to_low_mask; |
yihui | 1:a607cd9655d7 | 213 | mp_users[m_user_count].pins_low_to_high_mask = pins_low_to_high_mask; |
yihui | 1:a607cd9655d7 | 214 | mp_users[m_user_count].pins_high_to_low_mask = pins_high_to_low_mask; |
yihui | 1:a607cd9655d7 | 215 | mp_users[m_user_count].event_handler = event_handler; |
yihui | 1:a607cd9655d7 | 216 | |
yihui | 1:a607cd9655d7 | 217 | *p_user_id = m_user_count++; |
yihui | 1:a607cd9655d7 | 218 | |
yihui | 1:a607cd9655d7 | 219 | // Make sure SENSE is disabled for all pins. |
yihui | 1:a607cd9655d7 | 220 | pins_sense_disable(*p_user_id); |
yihui | 1:a607cd9655d7 | 221 | |
yihui | 1:a607cd9655d7 | 222 | return NRF_SUCCESS; |
yihui | 1:a607cd9655d7 | 223 | } |
yihui | 1:a607cd9655d7 | 224 | |
yihui | 1:a607cd9655d7 | 225 | |
yihui | 1:a607cd9655d7 | 226 | uint32_t app_gpiote_user_enable(app_gpiote_user_id_t user_id) |
yihui | 1:a607cd9655d7 | 227 | { |
yihui | 1:a607cd9655d7 | 228 | uint32_t pin_no; |
yihui | 1:a607cd9655d7 | 229 | uint32_t pins_state; |
yihui | 1:a607cd9655d7 | 230 | |
yihui | 1:a607cd9655d7 | 231 | // Check state and parameters. |
yihui | 1:a607cd9655d7 | 232 | if (mp_users == NULL) |
yihui | 1:a607cd9655d7 | 233 | { |
yihui | 1:a607cd9655d7 | 234 | return NRF_ERROR_INVALID_STATE; |
yihui | 1:a607cd9655d7 | 235 | } |
yihui | 1:a607cd9655d7 | 236 | if (user_id >= m_user_count) |
yihui | 1:a607cd9655d7 | 237 | { |
yihui | 1:a607cd9655d7 | 238 | return NRF_ERROR_INVALID_PARAM; |
yihui | 1:a607cd9655d7 | 239 | } |
yihui | 1:a607cd9655d7 | 240 | |
yihui | 1:a607cd9655d7 | 241 | // Clear any pending event. |
yihui | 1:a607cd9655d7 | 242 | NRF_GPIOTE->EVENTS_PORT = 0; |
yihui | 1:a607cd9655d7 | 243 | pins_state = NRF_GPIO->IN; |
yihui | 1:a607cd9655d7 | 244 | |
yihui | 1:a607cd9655d7 | 245 | // Enable user. |
yihui | 1:a607cd9655d7 | 246 | if (m_enabled_users_mask == 0) |
yihui | 1:a607cd9655d7 | 247 | { |
yihui | 1:a607cd9655d7 | 248 | NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_PORT_Msk; |
yihui | 1:a607cd9655d7 | 249 | } |
yihui | 1:a607cd9655d7 | 250 | m_enabled_users_mask |= (1 << user_id); |
yihui | 1:a607cd9655d7 | 251 | |
yihui | 1:a607cd9655d7 | 252 | // Enable sensing for all pins for specified user. |
yihui | 1:a607cd9655d7 | 253 | mp_users[user_id].sense_high_pins = 0; |
yihui | 1:a607cd9655d7 | 254 | for (pin_no = 0; pin_no < 32; pin_no++) |
yihui | 1:a607cd9655d7 | 255 | { |
yihui | 1:a607cd9655d7 | 256 | uint32_t pin_mask = (1 << pin_no); |
yihui | 1:a607cd9655d7 | 257 | |
yihui | 1:a607cd9655d7 | 258 | if ((mp_users[user_id].pins_mask & pin_mask) != 0) |
yihui | 1:a607cd9655d7 | 259 | { |
yihui | 1:a607cd9655d7 | 260 | uint32_t sense; |
yihui | 1:a607cd9655d7 | 261 | |
yihui | 1:a607cd9655d7 | 262 | if ((pins_state & pin_mask) != 0) |
yihui | 1:a607cd9655d7 | 263 | { |
yihui | 1:a607cd9655d7 | 264 | sense = GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos; |
yihui | 1:a607cd9655d7 | 265 | } |
yihui | 1:a607cd9655d7 | 266 | else |
yihui | 1:a607cd9655d7 | 267 | { |
yihui | 1:a607cd9655d7 | 268 | sense = GPIO_PIN_CNF_SENSE_High << GPIO_PIN_CNF_SENSE_Pos; |
yihui | 1:a607cd9655d7 | 269 | mp_users[user_id].sense_high_pins |= pin_mask; |
yihui | 1:a607cd9655d7 | 270 | } |
yihui | 1:a607cd9655d7 | 271 | |
yihui | 1:a607cd9655d7 | 272 | NRF_GPIO->PIN_CNF[pin_no] &= ~GPIO_PIN_CNF_SENSE_Msk; |
yihui | 1:a607cd9655d7 | 273 | NRF_GPIO->PIN_CNF[pin_no] |= sense; |
yihui | 1:a607cd9655d7 | 274 | } |
yihui | 1:a607cd9655d7 | 275 | } |
yihui | 1:a607cd9655d7 | 276 | |
yihui | 1:a607cd9655d7 | 277 | return NRF_SUCCESS; |
yihui | 1:a607cd9655d7 | 278 | } |
yihui | 1:a607cd9655d7 | 279 | |
yihui | 1:a607cd9655d7 | 280 | |
yihui | 1:a607cd9655d7 | 281 | uint32_t app_gpiote_user_disable(app_gpiote_user_id_t user_id) |
yihui | 1:a607cd9655d7 | 282 | { |
yihui | 1:a607cd9655d7 | 283 | // Check state and parameters. |
yihui | 1:a607cd9655d7 | 284 | if (mp_users == NULL) |
yihui | 1:a607cd9655d7 | 285 | { |
yihui | 1:a607cd9655d7 | 286 | return NRF_ERROR_INVALID_STATE; |
yihui | 1:a607cd9655d7 | 287 | } |
yihui | 1:a607cd9655d7 | 288 | if (user_id >= m_user_count) |
yihui | 1:a607cd9655d7 | 289 | { |
yihui | 1:a607cd9655d7 | 290 | return NRF_ERROR_INVALID_PARAM; |
yihui | 1:a607cd9655d7 | 291 | } |
yihui | 1:a607cd9655d7 | 292 | |
yihui | 1:a607cd9655d7 | 293 | // Disable sensing for all pins for specified user. |
yihui | 1:a607cd9655d7 | 294 | pins_sense_disable(user_id); |
yihui | 1:a607cd9655d7 | 295 | |
yihui | 1:a607cd9655d7 | 296 | // Disable user. |
yihui | 1:a607cd9655d7 | 297 | m_enabled_users_mask &= ~(1UL << user_id); |
yihui | 1:a607cd9655d7 | 298 | if (m_enabled_users_mask == 0) |
yihui | 1:a607cd9655d7 | 299 | { |
yihui | 1:a607cd9655d7 | 300 | NRF_GPIOTE->INTENCLR = GPIOTE_INTENSET_PORT_Msk; |
yihui | 1:a607cd9655d7 | 301 | } |
yihui | 1:a607cd9655d7 | 302 | |
yihui | 1:a607cd9655d7 | 303 | return NRF_SUCCESS; |
yihui | 1:a607cd9655d7 | 304 | } |
yihui | 1:a607cd9655d7 | 305 | |
yihui | 1:a607cd9655d7 | 306 | |
yihui | 1:a607cd9655d7 | 307 | uint32_t app_gpiote_pins_state_get(app_gpiote_user_id_t user_id, uint32_t * p_pins) |
yihui | 1:a607cd9655d7 | 308 | { |
yihui | 1:a607cd9655d7 | 309 | gpiote_user_t * p_user; |
yihui | 1:a607cd9655d7 | 310 | |
yihui | 1:a607cd9655d7 | 311 | // Check state and parameters. |
yihui | 1:a607cd9655d7 | 312 | if (mp_users == NULL) |
yihui | 1:a607cd9655d7 | 313 | { |
yihui | 1:a607cd9655d7 | 314 | return NRF_ERROR_INVALID_STATE; |
yihui | 1:a607cd9655d7 | 315 | } |
yihui | 1:a607cd9655d7 | 316 | if (user_id >= m_user_count) |
yihui | 1:a607cd9655d7 | 317 | { |
yihui | 1:a607cd9655d7 | 318 | return NRF_ERROR_INVALID_PARAM; |
yihui | 1:a607cd9655d7 | 319 | } |
yihui | 1:a607cd9655d7 | 320 | |
yihui | 1:a607cd9655d7 | 321 | // Get pins. |
yihui | 1:a607cd9655d7 | 322 | p_user = &mp_users[user_id]; |
yihui | 1:a607cd9655d7 | 323 | *p_pins = NRF_GPIO->IN & p_user->pins_mask; |
yihui | 1:a607cd9655d7 | 324 | |
yihui | 1:a607cd9655d7 | 325 | return NRF_SUCCESS; |
yihui | 1:a607cd9655d7 | 326 | } |
yihui | 1:a607cd9655d7 | 327 | |
yihui | 1:a607cd9655d7 | 328 | #if defined(SVCALL_AS_NORMAL_FUNCTION) || defined(SER_CONNECTIVITY) |
yihui | 1:a607cd9655d7 | 329 | uint32_t app_gpiote_input_event_handler_register(const uint8_t channel, |
yihui | 1:a607cd9655d7 | 330 | const uint32_t pin, |
yihui | 1:a607cd9655d7 | 331 | const uint32_t polarity, |
yihui | 1:a607cd9655d7 | 332 | app_gpiote_input_event_handler_t event_handler) |
yihui | 1:a607cd9655d7 | 333 | { |
yihui | 1:a607cd9655d7 | 334 | (void)sense_level_toggle(NULL, pin); |
yihui | 1:a607cd9655d7 | 335 | return NRF_ERROR_NOT_SUPPORTED; |
yihui | 1:a607cd9655d7 | 336 | } |
yihui | 1:a607cd9655d7 | 337 | |
yihui | 1:a607cd9655d7 | 338 | uint32_t app_gpiote_input_event_handler_unregister(const uint8_t channel) |
yihui | 1:a607cd9655d7 | 339 | { |
yihui | 1:a607cd9655d7 | 340 | return NRF_ERROR_NOT_SUPPORTED; |
yihui | 1:a607cd9655d7 | 341 | } |
yihui | 1:a607cd9655d7 | 342 | |
yihui | 1:a607cd9655d7 | 343 | uint32_t app_gpiote_end_irq_event_handler_register(app_gpiote_input_event_handler_t event_handler) |
yihui | 1:a607cd9655d7 | 344 | { |
yihui | 1:a607cd9655d7 | 345 | return NRF_ERROR_NOT_SUPPORTED; |
yihui | 1:a607cd9655d7 | 346 | } |
yihui | 1:a607cd9655d7 | 347 | |
yihui | 1:a607cd9655d7 | 348 | uint32_t app_gpiote_end_irq_event_handler_unregister(void) |
yihui | 1:a607cd9655d7 | 349 | { |
yihui | 1:a607cd9655d7 | 350 | return NRF_ERROR_NOT_SUPPORTED; |
yihui | 1:a607cd9655d7 | 351 | } |
yihui | 1:a607cd9655d7 | 352 | |
yihui | 1:a607cd9655d7 | 353 | uint32_t app_gpiote_enable_interrupts(void) |
yihui | 1:a607cd9655d7 | 354 | { |
yihui | 1:a607cd9655d7 | 355 | return NRF_ERROR_NOT_SUPPORTED; |
yihui | 1:a607cd9655d7 | 356 | } |
yihui | 1:a607cd9655d7 | 357 | |
yihui | 1:a607cd9655d7 | 358 | uint32_t app_gpiote_disable_interrupts(void) |
yihui | 1:a607cd9655d7 | 359 | { |
yihui | 1:a607cd9655d7 | 360 | return NRF_ERROR_NOT_SUPPORTED; |
yihui | 1:a607cd9655d7 | 361 | } |
yihui | 1:a607cd9655d7 | 362 | #endif // SVCALL_AS_NORMAL_FUNCTION || SER_CONNECTIVITY |
yihui | 1:a607cd9655d7 | 363 | |
yihui | 1:a607cd9655d7 | 364 | #endif // #if NEED_APP_GPIOTE |