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