Openwear requires RC oscillator to be used
Fork of nRF51822 by
nordic/app_common/app_gpiote.c@56:a1071b629aa3, 2014-09-02 (annotated)
- Committer:
- Rohit Grover
- Date:
- Tue Sep 02 15:50:05 2014 +0100
- Revision:
- 56:a1071b629aa3
- Parent:
- 46:2bfbbe290083
Release 0.1.0
=============
We've achieved significant gains in power consumption: the BLE_Beacon demo now
runs at around 35uA of average current broadcasting once a second at 0dB; when
not using the radio, this demo consumes around 7uA.
Features
~~~~~~~~
- Replace initialization of high-frequency external crystal clock-source with
the use of low-frequency clock. This brings in significant gains in power
consumption.
- Re-implement the micro-second timer on nRF51 using the app_timer module
(which internally uses RTC). This limits the precision of the us_Timer to
30uS; but brings in significant gains in power consumption.
- Reduce the number of available app_timers and the event depths for app-timer
events; this will reduce memory consumption for zero-initialized data by
around 1K.
- Remove the call to conn_params_init() at startup. This is not mandatory; and
was causing an unnecessary re-negotiation of connection parameters a few
seconds into every connection.
- Reduce default transmission power level to 0dbB (was 4dbB before).
- Reduce min connection interval to 50ms and max to 500ms (previous values
were much larger).
- Replace a few instances of use of wait() with nrf_delay_us().
- onConnection() callback now receives connection-parameters applicable to the
new connection.
- onDataSent() callback now receives a count parameter containing the number of
times notifications were sent out since the last callback.
- A 'reason' parameter has been added to Gap::disconnect() to indicate the
reason for disconnection; and also to the onDisconnection callback to
receive a reason from the remote host.
- disable the app_gpiote module by default.
Bugfixes
~~~~~~~~
- onDataWritten() callback now passes an additional parameter
(GattServer::WriteEventCallback_t) encapsulating the update. This avoids
having to re-fetch the updated characteristic's value attribute. It also
fixes a bug where multiple updates to the characteristic's value-attribute
could get clobbered if they occurred in quick succession before the
callbacks could be processed.
Compatibility
~~~~~~~~~~~~~
Compatible with revision 0.1.0 of the BLE_API.
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 |
56:a1071b629aa3 | 13 | #if NEED_APP_GPIOTE /* disabled by default */ |
Rohit Grover |
56:a1071b629aa3 | 14 | |
Rohit Grover |
46:2bfbbe290083 | 15 | #include "app_gpiote.h" |
Rohit Grover |
46:2bfbbe290083 | 16 | #include <stdlib.h> |
Rohit Grover |
46:2bfbbe290083 | 17 | #include <string.h> |
Rohit Grover |
46:2bfbbe290083 | 18 | #include "app_util.h" |
Rohit Grover |
46:2bfbbe290083 | 19 | #include "app_util_platform.h" |
Rohit Grover |
46:2bfbbe290083 | 20 | #include "nrf_error.h" |
Rohit Grover |
46:2bfbbe290083 | 21 | #include "nrf_gpio.h" |
Rohit Grover |
46:2bfbbe290083 | 22 | |
Rohit Grover |
46:2bfbbe290083 | 23 | |
Rohit Grover |
46:2bfbbe290083 | 24 | /**@brief GPIOTE user type. */ |
Rohit Grover |
46:2bfbbe290083 | 25 | typedef struct |
Rohit Grover |
46:2bfbbe290083 | 26 | { |
Rohit Grover |
46:2bfbbe290083 | 27 | uint32_t pins_mask; /**< Mask defining which pins user wants to monitor. */ |
Rohit Grover |
46:2bfbbe290083 | 28 | 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 | 29 | 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 | 30 | uint32_t sense_high_pins; /**< Mask defining which pins are configured to generate GPIOTE interrupt on transition to high level. */ |
Rohit Grover |
46:2bfbbe290083 | 31 | app_gpiote_event_handler_t event_handler; /**< Pointer to function to be executed when an event occurs. */ |
Rohit Grover |
46:2bfbbe290083 | 32 | } gpiote_user_t; |
Rohit Grover |
46:2bfbbe290083 | 33 | |
Rohit Grover |
46:2bfbbe290083 | 34 | STATIC_ASSERT(sizeof(gpiote_user_t) <= GPIOTE_USER_NODE_SIZE); |
Rohit Grover |
46:2bfbbe290083 | 35 | STATIC_ASSERT(sizeof(gpiote_user_t) % 4 == 0); |
Rohit Grover |
46:2bfbbe290083 | 36 | |
Rohit Grover |
46:2bfbbe290083 | 37 | static uint32_t m_enabled_users_mask; /**< Mask for tracking which users are enabled. */ |
Rohit Grover |
46:2bfbbe290083 | 38 | static uint8_t m_user_array_size; /**< Size of user array. */ |
Rohit Grover |
46:2bfbbe290083 | 39 | static uint8_t m_user_count; /**< Number of registered users. */ |
Rohit Grover |
46:2bfbbe290083 | 40 | static gpiote_user_t * mp_users = NULL; /**< Array of GPIOTE users. */ |
Rohit Grover |
46:2bfbbe290083 | 41 | |
Rohit Grover |
46:2bfbbe290083 | 42 | |
Rohit Grover |
46:2bfbbe290083 | 43 | /**@brief Function for toggling sense level for specified pins. |
Rohit Grover |
46:2bfbbe290083 | 44 | * |
Rohit Grover |
46:2bfbbe290083 | 45 | * @param[in] p_user Pointer to user structure. |
Rohit Grover |
46:2bfbbe290083 | 46 | * @param[in] pins Bitmask specifying for which pins the sense level is to be toggled. |
Rohit Grover |
46:2bfbbe290083 | 47 | */ |
Rohit Grover |
46:2bfbbe290083 | 48 | static void sense_level_toggle(gpiote_user_t * p_user, uint32_t pins) |
Rohit Grover |
46:2bfbbe290083 | 49 | { |
Rohit Grover |
46:2bfbbe290083 | 50 | uint32_t pin_no; |
Rohit Grover |
46:2bfbbe290083 | 51 | |
Rohit Grover |
46:2bfbbe290083 | 52 | for (pin_no = 0; pin_no < NO_OF_PINS; pin_no++) |
Rohit Grover |
46:2bfbbe290083 | 53 | { |
Rohit Grover |
46:2bfbbe290083 | 54 | uint32_t pin_mask = (1 << pin_no); |
Rohit Grover |
46:2bfbbe290083 | 55 | |
Rohit Grover |
46:2bfbbe290083 | 56 | if ((pins & pin_mask) != 0) |
Rohit Grover |
46:2bfbbe290083 | 57 | { |
Rohit Grover |
46:2bfbbe290083 | 58 | uint32_t sense; |
Rohit Grover |
46:2bfbbe290083 | 59 | |
Rohit Grover |
46:2bfbbe290083 | 60 | // Invert sensing. |
Rohit Grover |
46:2bfbbe290083 | 61 | if ((p_user->sense_high_pins & pin_mask) == 0) |
Rohit Grover |
46:2bfbbe290083 | 62 | { |
Rohit Grover |
46:2bfbbe290083 | 63 | sense = GPIO_PIN_CNF_SENSE_High << GPIO_PIN_CNF_SENSE_Pos; |
Rohit Grover |
46:2bfbbe290083 | 64 | p_user->sense_high_pins |= pin_mask; |
Rohit Grover |
46:2bfbbe290083 | 65 | } |
Rohit Grover |
46:2bfbbe290083 | 66 | else |
Rohit Grover |
46:2bfbbe290083 | 67 | { |
Rohit Grover |
46:2bfbbe290083 | 68 | sense = GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos; |
Rohit Grover |
46:2bfbbe290083 | 69 | p_user->sense_high_pins &= ~pin_mask; |
Rohit Grover |
46:2bfbbe290083 | 70 | } |
Rohit Grover |
46:2bfbbe290083 | 71 | |
Rohit Grover |
46:2bfbbe290083 | 72 | NRF_GPIO->PIN_CNF[pin_no] &= ~GPIO_PIN_CNF_SENSE_Msk; |
Rohit Grover |
46:2bfbbe290083 | 73 | NRF_GPIO->PIN_CNF[pin_no] |= sense; |
Rohit Grover |
46:2bfbbe290083 | 74 | } |
Rohit Grover |
46:2bfbbe290083 | 75 | } |
Rohit Grover |
46:2bfbbe290083 | 76 | } |
Rohit Grover |
46:2bfbbe290083 | 77 | |
Rohit Grover |
46:2bfbbe290083 | 78 | |
Rohit Grover |
46:2bfbbe290083 | 79 | /**@brief Function for handling the GPIOTE interrupt. |
Rohit Grover |
46:2bfbbe290083 | 80 | */ |
Rohit Grover |
46:2bfbbe290083 | 81 | void GPIOTE_IRQHandler(void) |
Rohit Grover |
46:2bfbbe290083 | 82 | { |
Rohit Grover |
46:2bfbbe290083 | 83 | uint8_t i; |
Rohit Grover |
46:2bfbbe290083 | 84 | uint32_t pins_changed; |
Rohit Grover |
46:2bfbbe290083 | 85 | uint32_t pins_state = NRF_GPIO->IN; |
Rohit Grover |
46:2bfbbe290083 | 86 | |
Rohit Grover |
46:2bfbbe290083 | 87 | // Clear event. |
Rohit Grover |
46:2bfbbe290083 | 88 | NRF_GPIOTE->EVENTS_PORT = 0; |
Rohit Grover |
46:2bfbbe290083 | 89 | |
Rohit Grover |
46:2bfbbe290083 | 90 | // Check all users. |
Rohit Grover |
46:2bfbbe290083 | 91 | for (i = 0; i < m_user_count; i++) |
Rohit Grover |
46:2bfbbe290083 | 92 | { |
Rohit Grover |
46:2bfbbe290083 | 93 | gpiote_user_t * p_user = &mp_users[i]; |
Rohit Grover |
46:2bfbbe290083 | 94 | |
Rohit Grover |
46:2bfbbe290083 | 95 | // Check if user is enabled. |
Rohit Grover |
46:2bfbbe290083 | 96 | if (((1 << i) & m_enabled_users_mask) != 0) |
Rohit Grover |
46:2bfbbe290083 | 97 | { |
Rohit Grover |
46:2bfbbe290083 | 98 | uint32_t transition_pins; |
Rohit Grover |
46:2bfbbe290083 | 99 | uint32_t event_low_to_high; |
Rohit Grover |
46:2bfbbe290083 | 100 | uint32_t event_high_to_low; |
Rohit Grover |
46:2bfbbe290083 | 101 | |
Rohit Grover |
46:2bfbbe290083 | 102 | // Find set of pins on which there has been a transition. |
Rohit Grover |
46:2bfbbe290083 | 103 | transition_pins = (pins_state ^ ~p_user->sense_high_pins) & p_user->pins_mask; |
Rohit Grover |
46:2bfbbe290083 | 104 | |
Rohit Grover |
46:2bfbbe290083 | 105 | // Toggle SENSE level for all pins that have changed state. |
Rohit Grover |
46:2bfbbe290083 | 106 | sense_level_toggle(p_user, transition_pins); |
Rohit Grover |
46:2bfbbe290083 | 107 | |
Rohit Grover |
46:2bfbbe290083 | 108 | // Second read after setting sense. |
Rohit Grover |
46:2bfbbe290083 | 109 | // Check if any pins have changed while serving this interrupt. |
Rohit Grover |
46:2bfbbe290083 | 110 | pins_changed = NRF_GPIO->IN ^ pins_state; |
Rohit Grover |
46:2bfbbe290083 | 111 | if (pins_changed) |
Rohit Grover |
46:2bfbbe290083 | 112 | { |
Rohit Grover |
46:2bfbbe290083 | 113 | // Transition pins detected in late stage. |
Rohit Grover |
46:2bfbbe290083 | 114 | uint32_t late_transition_pins; |
Rohit Grover |
46:2bfbbe290083 | 115 | |
Rohit Grover |
46:2bfbbe290083 | 116 | pins_state |= pins_changed; |
Rohit Grover |
46:2bfbbe290083 | 117 | |
Rohit Grover |
46:2bfbbe290083 | 118 | // Find set of pins on which there has been a transition. |
Rohit Grover |
46:2bfbbe290083 | 119 | late_transition_pins = (pins_state ^ ~p_user->sense_high_pins) & p_user->pins_mask; |
Rohit Grover |
46:2bfbbe290083 | 120 | |
Rohit Grover |
46:2bfbbe290083 | 121 | // Toggle SENSE level for all pins that have changed state in last phase. |
Rohit Grover |
46:2bfbbe290083 | 122 | sense_level_toggle(p_user, late_transition_pins); |
Rohit Grover |
46:2bfbbe290083 | 123 | |
Rohit Grover |
46:2bfbbe290083 | 124 | // Update pins that has changed state since the interrupt occurred. |
Rohit Grover |
46:2bfbbe290083 | 125 | transition_pins |= late_transition_pins; |
Rohit Grover |
46:2bfbbe290083 | 126 | } |
Rohit Grover |
46:2bfbbe290083 | 127 | |
Rohit Grover |
46:2bfbbe290083 | 128 | // Call user event handler if an event has occurred. |
Rohit Grover |
46:2bfbbe290083 | 129 | event_high_to_low = (~pins_state & p_user->pins_high_to_low_mask) & transition_pins; |
Rohit Grover |
46:2bfbbe290083 | 130 | event_low_to_high = (pins_state & p_user->pins_low_to_high_mask) & transition_pins; |
Rohit Grover |
46:2bfbbe290083 | 131 | |
Rohit Grover |
46:2bfbbe290083 | 132 | if ((event_low_to_high | event_high_to_low) != 0) |
Rohit Grover |
46:2bfbbe290083 | 133 | { |
Rohit Grover |
46:2bfbbe290083 | 134 | p_user->event_handler(event_low_to_high, event_high_to_low); |
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 | |
Rohit Grover |
46:2bfbbe290083 | 140 | |
Rohit Grover |
46:2bfbbe290083 | 141 | /**@brief Function for sense disabling for all pins for specified user. |
Rohit Grover |
46:2bfbbe290083 | 142 | * |
Rohit Grover |
46:2bfbbe290083 | 143 | * @param[in] user_id User id. |
Rohit Grover |
46:2bfbbe290083 | 144 | */ |
Rohit Grover |
46:2bfbbe290083 | 145 | static void pins_sense_disable(app_gpiote_user_id_t user_id) |
Rohit Grover |
46:2bfbbe290083 | 146 | { |
Rohit Grover |
46:2bfbbe290083 | 147 | uint32_t pin_no; |
Rohit Grover |
46:2bfbbe290083 | 148 | |
Rohit Grover |
46:2bfbbe290083 | 149 | for (pin_no = 0; pin_no < 32; pin_no++) |
Rohit Grover |
46:2bfbbe290083 | 150 | { |
Rohit Grover |
46:2bfbbe290083 | 151 | if ((mp_users[user_id].pins_mask & (1 << pin_no)) != 0) |
Rohit Grover |
46:2bfbbe290083 | 152 | { |
Rohit Grover |
46:2bfbbe290083 | 153 | NRF_GPIO->PIN_CNF[pin_no] &= ~GPIO_PIN_CNF_SENSE_Msk; |
Rohit Grover |
46:2bfbbe290083 | 154 | NRF_GPIO->PIN_CNF[pin_no] |= GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos; |
Rohit Grover |
46:2bfbbe290083 | 155 | } |
Rohit Grover |
46:2bfbbe290083 | 156 | } |
Rohit Grover |
46:2bfbbe290083 | 157 | } |
Rohit Grover |
46:2bfbbe290083 | 158 | |
Rohit Grover |
46:2bfbbe290083 | 159 | |
Rohit Grover |
46:2bfbbe290083 | 160 | uint32_t app_gpiote_init(uint8_t max_users, void * p_buffer) |
Rohit Grover |
46:2bfbbe290083 | 161 | { |
Rohit Grover |
46:2bfbbe290083 | 162 | if (p_buffer == NULL) |
Rohit Grover |
46:2bfbbe290083 | 163 | { |
Rohit Grover |
46:2bfbbe290083 | 164 | return NRF_ERROR_INVALID_PARAM; |
Rohit Grover |
46:2bfbbe290083 | 165 | } |
Rohit Grover |
46:2bfbbe290083 | 166 | |
Rohit Grover |
46:2bfbbe290083 | 167 | // Check that buffer is correctly aligned. |
Rohit Grover |
46:2bfbbe290083 | 168 | if (!is_word_aligned(p_buffer)) |
Rohit Grover |
46:2bfbbe290083 | 169 | { |
Rohit Grover |
46:2bfbbe290083 | 170 | return NRF_ERROR_INVALID_PARAM; |
Rohit Grover |
46:2bfbbe290083 | 171 | } |
Rohit Grover |
46:2bfbbe290083 | 172 | |
Rohit Grover |
46:2bfbbe290083 | 173 | // Initialize file globals. |
Rohit Grover |
46:2bfbbe290083 | 174 | mp_users = (gpiote_user_t *)p_buffer; |
Rohit Grover |
46:2bfbbe290083 | 175 | m_user_array_size = max_users; |
Rohit Grover |
46:2bfbbe290083 | 176 | m_user_count = 0; |
Rohit Grover |
46:2bfbbe290083 | 177 | m_enabled_users_mask = 0; |
Rohit Grover |
46:2bfbbe290083 | 178 | |
Rohit Grover |
46:2bfbbe290083 | 179 | memset(mp_users, 0, m_user_array_size * sizeof(gpiote_user_t)); |
Rohit Grover |
46:2bfbbe290083 | 180 | |
Rohit Grover |
46:2bfbbe290083 | 181 | // Initialize GPIOTE interrupt (will not be enabled until app_gpiote_user_enable() is called). |
Rohit Grover |
46:2bfbbe290083 | 182 | NRF_GPIOTE->INTENCLR = 0xFFFFFFFF; |
Rohit Grover |
46:2bfbbe290083 | 183 | |
Rohit Grover |
46:2bfbbe290083 | 184 | NVIC_ClearPendingIRQ(GPIOTE_IRQn); |
Rohit Grover |
46:2bfbbe290083 | 185 | NVIC_SetPriority(GPIOTE_IRQn, APP_IRQ_PRIORITY_HIGH); |
Rohit Grover |
46:2bfbbe290083 | 186 | NVIC_EnableIRQ(GPIOTE_IRQn); |
Rohit Grover |
46:2bfbbe290083 | 187 | |
Rohit Grover |
46:2bfbbe290083 | 188 | return NRF_SUCCESS; |
Rohit Grover |
46:2bfbbe290083 | 189 | } |
Rohit Grover |
46:2bfbbe290083 | 190 | |
Rohit Grover |
46:2bfbbe290083 | 191 | |
Rohit Grover |
46:2bfbbe290083 | 192 | uint32_t app_gpiote_user_register(app_gpiote_user_id_t * p_user_id, |
Rohit Grover |
46:2bfbbe290083 | 193 | uint32_t pins_low_to_high_mask, |
Rohit Grover |
46:2bfbbe290083 | 194 | uint32_t pins_high_to_low_mask, |
Rohit Grover |
46:2bfbbe290083 | 195 | app_gpiote_event_handler_t event_handler) |
Rohit Grover |
46:2bfbbe290083 | 196 | { |
Rohit Grover |
46:2bfbbe290083 | 197 | // Check state and parameters. |
Rohit Grover |
46:2bfbbe290083 | 198 | if (mp_users == NULL) |
Rohit Grover |
46:2bfbbe290083 | 199 | { |
Rohit Grover |
46:2bfbbe290083 | 200 | return NRF_ERROR_INVALID_STATE; |
Rohit Grover |
46:2bfbbe290083 | 201 | } |
Rohit Grover |
46:2bfbbe290083 | 202 | if (event_handler == NULL) |
Rohit Grover |
46:2bfbbe290083 | 203 | { |
Rohit Grover |
46:2bfbbe290083 | 204 | return NRF_ERROR_INVALID_PARAM; |
Rohit Grover |
46:2bfbbe290083 | 205 | } |
Rohit Grover |
46:2bfbbe290083 | 206 | if (m_user_count >= m_user_array_size) |
Rohit Grover |
46:2bfbbe290083 | 207 | { |
Rohit Grover |
46:2bfbbe290083 | 208 | return NRF_ERROR_NO_MEM; |
Rohit Grover |
46:2bfbbe290083 | 209 | } |
Rohit Grover |
46:2bfbbe290083 | 210 | |
Rohit Grover |
46:2bfbbe290083 | 211 | // Allocate new user. |
Rohit Grover |
46:2bfbbe290083 | 212 | mp_users[m_user_count].pins_mask = pins_low_to_high_mask | pins_high_to_low_mask; |
Rohit Grover |
46:2bfbbe290083 | 213 | mp_users[m_user_count].pins_low_to_high_mask = pins_low_to_high_mask; |
Rohit Grover |
46:2bfbbe290083 | 214 | mp_users[m_user_count].pins_high_to_low_mask = pins_high_to_low_mask; |
Rohit Grover |
46:2bfbbe290083 | 215 | mp_users[m_user_count].event_handler = event_handler; |
Rohit Grover |
46:2bfbbe290083 | 216 | |
Rohit Grover |
46:2bfbbe290083 | 217 | *p_user_id = m_user_count++; |
Rohit Grover |
46:2bfbbe290083 | 218 | |
Rohit Grover |
46:2bfbbe290083 | 219 | // Make sure SENSE is disabled for all pins. |
Rohit Grover |
46:2bfbbe290083 | 220 | pins_sense_disable(*p_user_id); |
Rohit Grover |
46:2bfbbe290083 | 221 | |
Rohit Grover |
46:2bfbbe290083 | 222 | return NRF_SUCCESS; |
Rohit Grover |
46:2bfbbe290083 | 223 | } |
Rohit Grover |
46:2bfbbe290083 | 224 | |
Rohit Grover |
46:2bfbbe290083 | 225 | |
Rohit Grover |
46:2bfbbe290083 | 226 | uint32_t app_gpiote_user_enable(app_gpiote_user_id_t user_id) |
Rohit Grover |
46:2bfbbe290083 | 227 | { |
Rohit Grover |
46:2bfbbe290083 | 228 | uint32_t pin_no; |
Rohit Grover |
46:2bfbbe290083 | 229 | uint32_t pins_state; |
Rohit Grover |
46:2bfbbe290083 | 230 | |
Rohit Grover |
46:2bfbbe290083 | 231 | // Check state and parameters. |
Rohit Grover |
46:2bfbbe290083 | 232 | if (mp_users == NULL) |
Rohit Grover |
46:2bfbbe290083 | 233 | { |
Rohit Grover |
46:2bfbbe290083 | 234 | return NRF_ERROR_INVALID_STATE; |
Rohit Grover |
46:2bfbbe290083 | 235 | } |
Rohit Grover |
46:2bfbbe290083 | 236 | if (user_id >= m_user_count) |
Rohit Grover |
46:2bfbbe290083 | 237 | { |
Rohit Grover |
46:2bfbbe290083 | 238 | return NRF_ERROR_INVALID_PARAM; |
Rohit Grover |
46:2bfbbe290083 | 239 | } |
Rohit Grover |
46:2bfbbe290083 | 240 | |
Rohit Grover |
46:2bfbbe290083 | 241 | // Clear any pending event. |
Rohit Grover |
46:2bfbbe290083 | 242 | NRF_GPIOTE->EVENTS_PORT = 0; |
Rohit Grover |
46:2bfbbe290083 | 243 | pins_state = NRF_GPIO->IN; |
Rohit Grover |
46:2bfbbe290083 | 244 | |
Rohit Grover |
46:2bfbbe290083 | 245 | // Enable user. |
Rohit Grover |
46:2bfbbe290083 | 246 | if (m_enabled_users_mask == 0) |
Rohit Grover |
46:2bfbbe290083 | 247 | { |
Rohit Grover |
46:2bfbbe290083 | 248 | NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_PORT_Msk; |
Rohit Grover |
46:2bfbbe290083 | 249 | } |
Rohit Grover |
46:2bfbbe290083 | 250 | m_enabled_users_mask |= (1 << user_id); |
Rohit Grover |
46:2bfbbe290083 | 251 | |
Rohit Grover |
46:2bfbbe290083 | 252 | // Enable sensing for all pins for specified user. |
Rohit Grover |
46:2bfbbe290083 | 253 | mp_users[user_id].sense_high_pins = 0; |
Rohit Grover |
46:2bfbbe290083 | 254 | for (pin_no = 0; pin_no < 32; pin_no++) |
Rohit Grover |
46:2bfbbe290083 | 255 | { |
Rohit Grover |
46:2bfbbe290083 | 256 | uint32_t pin_mask = (1 << pin_no); |
Rohit Grover |
46:2bfbbe290083 | 257 | |
Rohit Grover |
46:2bfbbe290083 | 258 | if ((mp_users[user_id].pins_mask & pin_mask) != 0) |
Rohit Grover |
46:2bfbbe290083 | 259 | { |
Rohit Grover |
46:2bfbbe290083 | 260 | uint32_t sense; |
Rohit Grover |
46:2bfbbe290083 | 261 | |
Rohit Grover |
46:2bfbbe290083 | 262 | if ((pins_state & pin_mask) != 0) |
Rohit Grover |
46:2bfbbe290083 | 263 | { |
Rohit Grover |
46:2bfbbe290083 | 264 | sense = GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos; |
Rohit Grover |
46:2bfbbe290083 | 265 | } |
Rohit Grover |
46:2bfbbe290083 | 266 | else |
Rohit Grover |
46:2bfbbe290083 | 267 | { |
Rohit Grover |
46:2bfbbe290083 | 268 | sense = GPIO_PIN_CNF_SENSE_High << GPIO_PIN_CNF_SENSE_Pos; |
Rohit Grover |
46:2bfbbe290083 | 269 | mp_users[user_id].sense_high_pins |= pin_mask; |
Rohit Grover |
46:2bfbbe290083 | 270 | } |
Rohit Grover |
46:2bfbbe290083 | 271 | |
Rohit Grover |
46:2bfbbe290083 | 272 | NRF_GPIO->PIN_CNF[pin_no] &= ~GPIO_PIN_CNF_SENSE_Msk; |
Rohit Grover |
46:2bfbbe290083 | 273 | NRF_GPIO->PIN_CNF[pin_no] |= sense; |
Rohit Grover |
46:2bfbbe290083 | 274 | } |
Rohit Grover |
46:2bfbbe290083 | 275 | } |
Rohit Grover |
46:2bfbbe290083 | 276 | |
Rohit Grover |
46:2bfbbe290083 | 277 | return NRF_SUCCESS; |
Rohit Grover |
46:2bfbbe290083 | 278 | } |
Rohit Grover |
46:2bfbbe290083 | 279 | |
Rohit Grover |
46:2bfbbe290083 | 280 | |
Rohit Grover |
46:2bfbbe290083 | 281 | uint32_t app_gpiote_user_disable(app_gpiote_user_id_t user_id) |
Rohit Grover |
46:2bfbbe290083 | 282 | { |
Rohit Grover |
46:2bfbbe290083 | 283 | // Check state and parameters. |
Rohit Grover |
46:2bfbbe290083 | 284 | if (mp_users == NULL) |
Rohit Grover |
46:2bfbbe290083 | 285 | { |
Rohit Grover |
46:2bfbbe290083 | 286 | return NRF_ERROR_INVALID_STATE; |
Rohit Grover |
46:2bfbbe290083 | 287 | } |
Rohit Grover |
46:2bfbbe290083 | 288 | if (user_id >= m_user_count) |
Rohit Grover |
46:2bfbbe290083 | 289 | { |
Rohit Grover |
46:2bfbbe290083 | 290 | return NRF_ERROR_INVALID_PARAM; |
Rohit Grover |
46:2bfbbe290083 | 291 | } |
Rohit Grover |
46:2bfbbe290083 | 292 | |
Rohit Grover |
46:2bfbbe290083 | 293 | // Disable sensing for all pins for specified user. |
Rohit Grover |
46:2bfbbe290083 | 294 | pins_sense_disable(user_id); |
Rohit Grover |
46:2bfbbe290083 | 295 | |
Rohit Grover |
46:2bfbbe290083 | 296 | // Disable user. |
Rohit Grover |
46:2bfbbe290083 | 297 | m_enabled_users_mask &= ~(1UL << user_id); |
Rohit Grover |
46:2bfbbe290083 | 298 | if (m_enabled_users_mask == 0) |
Rohit Grover |
46:2bfbbe290083 | 299 | { |
Rohit Grover |
46:2bfbbe290083 | 300 | NRF_GPIOTE->INTENCLR = GPIOTE_INTENSET_PORT_Msk; |
Rohit Grover |
46:2bfbbe290083 | 301 | } |
Rohit Grover |
46:2bfbbe290083 | 302 | |
Rohit Grover |
46:2bfbbe290083 | 303 | return NRF_SUCCESS; |
Rohit Grover |
46:2bfbbe290083 | 304 | } |
Rohit Grover |
46:2bfbbe290083 | 305 | |
Rohit Grover |
46:2bfbbe290083 | 306 | |
Rohit Grover |
46:2bfbbe290083 | 307 | uint32_t app_gpiote_pins_state_get(app_gpiote_user_id_t user_id, uint32_t * p_pins) |
Rohit Grover |
46:2bfbbe290083 | 308 | { |
Rohit Grover |
46:2bfbbe290083 | 309 | gpiote_user_t * p_user; |
Rohit Grover |
46:2bfbbe290083 | 310 | |
Rohit Grover |
46:2bfbbe290083 | 311 | // Check state and parameters. |
Rohit Grover |
46:2bfbbe290083 | 312 | if (mp_users == NULL) |
Rohit Grover |
46:2bfbbe290083 | 313 | { |
Rohit Grover |
46:2bfbbe290083 | 314 | return NRF_ERROR_INVALID_STATE; |
Rohit Grover |
46:2bfbbe290083 | 315 | } |
Rohit Grover |
46:2bfbbe290083 | 316 | if (user_id >= m_user_count) |
Rohit Grover |
46:2bfbbe290083 | 317 | { |
Rohit Grover |
46:2bfbbe290083 | 318 | return NRF_ERROR_INVALID_PARAM; |
Rohit Grover |
46:2bfbbe290083 | 319 | } |
Rohit Grover |
46:2bfbbe290083 | 320 | |
Rohit Grover |
46:2bfbbe290083 | 321 | // Get pins. |
Rohit Grover |
46:2bfbbe290083 | 322 | p_user = &mp_users[user_id]; |
Rohit Grover |
46:2bfbbe290083 | 323 | *p_pins = NRF_GPIO->IN & p_user->pins_mask; |
Rohit Grover |
46:2bfbbe290083 | 324 | |
Rohit Grover |
46:2bfbbe290083 | 325 | return NRF_SUCCESS; |
Rohit Grover |
46:2bfbbe290083 | 326 | } |
Rohit Grover |
46:2bfbbe290083 | 327 | |
Rohit Grover |
46:2bfbbe290083 | 328 | #if defined(SVCALL_AS_NORMAL_FUNCTION) || defined(SER_CONNECTIVITY) |
Rohit Grover |
46:2bfbbe290083 | 329 | uint32_t app_gpiote_input_event_handler_register(const uint8_t channel, |
Rohit Grover |
46:2bfbbe290083 | 330 | const uint32_t pin, |
Rohit Grover |
46:2bfbbe290083 | 331 | const uint32_t polarity, |
Rohit Grover |
46:2bfbbe290083 | 332 | app_gpiote_input_event_handler_t event_handler) |
Rohit Grover |
46:2bfbbe290083 | 333 | { |
Rohit Grover |
46:2bfbbe290083 | 334 | (void)sense_level_toggle(NULL, pin); |
Rohit Grover |
46:2bfbbe290083 | 335 | return NRF_ERROR_NOT_SUPPORTED; |
Rohit Grover |
46:2bfbbe290083 | 336 | } |
Rohit Grover |
46:2bfbbe290083 | 337 | |
Rohit Grover |
46:2bfbbe290083 | 338 | uint32_t app_gpiote_input_event_handler_unregister(const uint8_t channel) |
Rohit Grover |
46:2bfbbe290083 | 339 | { |
Rohit Grover |
46:2bfbbe290083 | 340 | return NRF_ERROR_NOT_SUPPORTED; |
Rohit Grover |
46:2bfbbe290083 | 341 | } |
Rohit Grover |
46:2bfbbe290083 | 342 | |
Rohit Grover |
46:2bfbbe290083 | 343 | uint32_t app_gpiote_end_irq_event_handler_register(app_gpiote_input_event_handler_t event_handler) |
Rohit Grover |
46:2bfbbe290083 | 344 | { |
Rohit Grover |
46:2bfbbe290083 | 345 | return NRF_ERROR_NOT_SUPPORTED; |
Rohit Grover |
46:2bfbbe290083 | 346 | } |
Rohit Grover |
46:2bfbbe290083 | 347 | |
Rohit Grover |
46:2bfbbe290083 | 348 | uint32_t app_gpiote_end_irq_event_handler_unregister(void) |
Rohit Grover |
46:2bfbbe290083 | 349 | { |
Rohit Grover |
46:2bfbbe290083 | 350 | return NRF_ERROR_NOT_SUPPORTED; |
Rohit Grover |
46:2bfbbe290083 | 351 | } |
Rohit Grover |
46:2bfbbe290083 | 352 | |
Rohit Grover |
46:2bfbbe290083 | 353 | uint32_t app_gpiote_enable_interrupts(void) |
Rohit Grover |
46:2bfbbe290083 | 354 | { |
Rohit Grover |
46:2bfbbe290083 | 355 | return NRF_ERROR_NOT_SUPPORTED; |
Rohit Grover |
46:2bfbbe290083 | 356 | } |
Rohit Grover |
46:2bfbbe290083 | 357 | |
Rohit Grover |
46:2bfbbe290083 | 358 | uint32_t app_gpiote_disable_interrupts(void) |
Rohit Grover |
46:2bfbbe290083 | 359 | { |
Rohit Grover |
46:2bfbbe290083 | 360 | return NRF_ERROR_NOT_SUPPORTED; |
Rohit Grover |
46:2bfbbe290083 | 361 | } |
Rohit Grover |
46:2bfbbe290083 | 362 | #endif // SVCALL_AS_NORMAL_FUNCTION || SER_CONNECTIVITY |
Rohit Grover |
56:a1071b629aa3 | 363 | |
Rohit Grover |
56:a1071b629aa3 | 364 | #endif // #if NEED_APP_GPIOTE |