Forked.

Fork of mbed-dev by mbed official

Committer:
Dollyparton
Date:
Tue Dec 19 12:50:13 2017 +0000
Revision:
174:ed647f63e28d
Parent:
169:e3b6fe271b81
Added RAW socket.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /*
<> 160:d5399cc887bb 2 * Copyright (c) 2017 Nordic Semiconductor ASA
<> 144:ef7eb2e8f9f7 3 * All rights reserved.
<> 144:ef7eb2e8f9f7 4 *
<> 144:ef7eb2e8f9f7 5 * Redistribution and use in source and binary forms, with or without modification,
<> 144:ef7eb2e8f9f7 6 * are permitted provided that the following conditions are met:
<> 144:ef7eb2e8f9f7 7 *
<> 144:ef7eb2e8f9f7 8 * 1. Redistributions of source code must retain the above copyright notice, this list
<> 144:ef7eb2e8f9f7 9 * of conditions and the following disclaimer.
<> 144:ef7eb2e8f9f7 10 *
<> 144:ef7eb2e8f9f7 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
<> 144:ef7eb2e8f9f7 12 * integrated circuit in a product or a software update for such product, must reproduce
<> 144:ef7eb2e8f9f7 13 * the above copyright notice, this list of conditions and the following disclaimer in
<> 144:ef7eb2e8f9f7 14 * the documentation and/or other materials provided with the distribution.
<> 144:ef7eb2e8f9f7 15 *
<> 144:ef7eb2e8f9f7 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
<> 144:ef7eb2e8f9f7 17 * used to endorse or promote products derived from this software without specific prior
<> 144:ef7eb2e8f9f7 18 * written permission.
<> 144:ef7eb2e8f9f7 19 *
<> 144:ef7eb2e8f9f7 20 * 4. This software, with or without modification, must only be used with a
<> 144:ef7eb2e8f9f7 21 * Nordic Semiconductor ASA integrated circuit.
<> 144:ef7eb2e8f9f7 22 *
<> 144:ef7eb2e8f9f7 23 * 5. Any software provided in binary or object form under this license must not be reverse
<> 144:ef7eb2e8f9f7 24 * engineered, decompiled, modified and/or disassembled.
<> 144:ef7eb2e8f9f7 25 *
<> 144:ef7eb2e8f9f7 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
<> 144:ef7eb2e8f9f7 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
<> 144:ef7eb2e8f9f7 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 144:ef7eb2e8f9f7 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
<> 144:ef7eb2e8f9f7 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
<> 144:ef7eb2e8f9f7 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
<> 144:ef7eb2e8f9f7 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
<> 144:ef7eb2e8f9f7 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
<> 144:ef7eb2e8f9f7 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
<> 144:ef7eb2e8f9f7 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 144:ef7eb2e8f9f7 36 *
<> 144:ef7eb2e8f9f7 37 */
<> 144:ef7eb2e8f9f7 38
<> 144:ef7eb2e8f9f7 39
<> 144:ef7eb2e8f9f7 40 #include "i2c_api.h"
<> 144:ef7eb2e8f9f7 41
<> 144:ef7eb2e8f9f7 42 #if DEVICE_I2C
<> 144:ef7eb2e8f9f7 43
<> 144:ef7eb2e8f9f7 44 #include "mbed_assert.h"
<> 144:ef7eb2e8f9f7 45 #include "mbed_error.h"
<> 160:d5399cc887bb 46 #include "nrf_twi.h"
<> 160:d5399cc887bb 47 #include "nrf_drv_common.h"
AnnaBridge 165:e614a9f1c9e2 48 #include "sdk_config.h"
<> 144:ef7eb2e8f9f7 49 #include "app_util_platform.h"
<> 160:d5399cc887bb 50 #include "nrf_gpio.h"
<> 160:d5399cc887bb 51 #include "nrf_delay.h"
<> 160:d5399cc887bb 52
<> 160:d5399cc887bb 53 // An arbitrary value used as the counter in loops waiting for given event
<> 160:d5399cc887bb 54 // (e.g. STOPPED), needed to avoid infinite loops (and not involve any timers
<> 160:d5399cc887bb 55 // or tickers).
<> 160:d5399cc887bb 56 #define TIMEOUT_VALUE 1000
<> 144:ef7eb2e8f9f7 57
<> 144:ef7eb2e8f9f7 58 #if DEVICE_I2C_ASYNCH
<> 144:ef7eb2e8f9f7 59 #define TWI_IDX(obj) ((obj)->i2c.twi_idx)
<> 144:ef7eb2e8f9f7 60 #else
<> 144:ef7eb2e8f9f7 61 #define TWI_IDX(obj) ((obj)->twi_idx)
<> 144:ef7eb2e8f9f7 62 #endif
<> 144:ef7eb2e8f9f7 63 #define TWI_INFO(obj) (&m_twi_info[TWI_IDX(obj)])
<> 144:ef7eb2e8f9f7 64
AnnaBridge 165:e614a9f1c9e2 65 #ifdef TARGET_SDK13
AnnaBridge 165:e614a9f1c9e2 66 #define TWI0_INSTANCE_INDEX 0
AnnaBridge 165:e614a9f1c9e2 67 #define TWI1_INSTANCE_INDEX TWI0_INSTANCE_INDEX+TWI0_ENABLED
AnnaBridge 165:e614a9f1c9e2 68 #endif
AnnaBridge 165:e614a9f1c9e2 69
<> 144:ef7eb2e8f9f7 70 typedef struct {
<> 160:d5399cc887bb 71 bool initialized;
<> 160:d5399cc887bb 72 uint32_t pselsda;
<> 160:d5399cc887bb 73 uint32_t pselscl;
<> 160:d5399cc887bb 74 nrf_twi_frequency_t frequency;
<> 160:d5399cc887bb 75 bool start_twi;
<> 144:ef7eb2e8f9f7 76
<> 160:d5399cc887bb 77 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 78 volatile bool active;
<> 160:d5399cc887bb 79 uint8_t const *tx;
<> 160:d5399cc887bb 80 size_t tx_length;
<> 160:d5399cc887bb 81 uint8_t *rx;
<> 160:d5399cc887bb 82 size_t rx_length;
<> 160:d5399cc887bb 83 bool stop;
<> 160:d5399cc887bb 84
<> 160:d5399cc887bb 85 volatile uint32_t events;
<> 160:d5399cc887bb 86 void (*handler)(void);
<> 160:d5399cc887bb 87 uint32_t evt_mask;
<> 160:d5399cc887bb 88 #endif // DEVICE_I2C_ASYNCH
<> 144:ef7eb2e8f9f7 89 } twi_info_t;
<> 144:ef7eb2e8f9f7 90 static twi_info_t m_twi_info[TWI_COUNT];
<> 144:ef7eb2e8f9f7 91
<> 160:d5399cc887bb 92 static NRF_TWI_Type * const m_twi_instances[TWI_COUNT] = {
<> 144:ef7eb2e8f9f7 93 #if TWI0_ENABLED
<> 160:d5399cc887bb 94 NRF_TWI0,
<> 144:ef7eb2e8f9f7 95 #endif
<> 144:ef7eb2e8f9f7 96 #if TWI1_ENABLED
<> 160:d5399cc887bb 97 NRF_TWI1,
<> 144:ef7eb2e8f9f7 98 #endif
<> 144:ef7eb2e8f9f7 99 };
<> 144:ef7eb2e8f9f7 100
<> 150:02e0a0aed4ec 101 void SPI0_TWI0_IRQHandler(void);
<> 150:02e0a0aed4ec 102 void SPI1_TWI1_IRQHandler(void);
<> 150:02e0a0aed4ec 103
<> 150:02e0a0aed4ec 104 static const peripheral_handler_desc_t twi_handlers[TWI_COUNT] =
<> 150:02e0a0aed4ec 105 {
AnnaBridge 165:e614a9f1c9e2 106 #if TWI0_ENABLED
<> 150:02e0a0aed4ec 107 {
<> 150:02e0a0aed4ec 108 SPI0_TWI0_IRQn,
<> 150:02e0a0aed4ec 109 (uint32_t) SPI0_TWI0_IRQHandler
<> 150:02e0a0aed4ec 110 },
AnnaBridge 165:e614a9f1c9e2 111 #endif
AnnaBridge 165:e614a9f1c9e2 112 #if TWI1_ENABLED
<> 150:02e0a0aed4ec 113 {
<> 150:02e0a0aed4ec 114 SPI1_TWI1_IRQn,
<> 150:02e0a0aed4ec 115 (uint32_t) SPI1_TWI1_IRQHandler
<> 150:02e0a0aed4ec 116 }
AnnaBridge 165:e614a9f1c9e2 117 #endif
<> 150:02e0a0aed4ec 118 };
<> 160:d5399cc887bb 119 #ifdef NRF51
<> 160:d5399cc887bb 120 #define TWI_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
AnnaBridge 165:e614a9f1c9e2 121 #elif defined(NRF52) || defined(NRF52840_XXAA)
<> 160:d5399cc887bb 122 #define TWI_IRQ_PRIORITY APP_IRQ_PRIORITY_LOWEST
<> 160:d5399cc887bb 123 #endif
<> 160:d5399cc887bb 124
<> 160:d5399cc887bb 125
<> 160:d5399cc887bb 126 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 127 static void start_asynch_rx(twi_info_t *twi_info, NRF_TWI_Type *twi)
<> 160:d5399cc887bb 128 {
<> 160:d5399cc887bb 129 if (twi_info->rx_length == 1 && twi_info->stop) {
<> 160:d5399cc887bb 130 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_STOP_MASK);
<> 160:d5399cc887bb 131 } else {
<> 160:d5399cc887bb 132 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_SUSPEND_MASK);
<> 160:d5399cc887bb 133 }
<> 160:d5399cc887bb 134 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTRX);
<> 160:d5399cc887bb 135 }
<> 160:d5399cc887bb 136
<> 160:d5399cc887bb 137 static void twi_irq_handler(uint8_t instance_idx)
<> 160:d5399cc887bb 138 {
<> 160:d5399cc887bb 139 twi_info_t *twi_info = &m_twi_info[instance_idx];
<> 160:d5399cc887bb 140
<> 160:d5399cc887bb 141 NRF_TWI_Type *twi = m_twi_instances[instance_idx];
<> 160:d5399cc887bb 142 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
<> 160:d5399cc887bb 143 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 144
<> 160:d5399cc887bb 145 // In case of an error, force STOP.
<> 160:d5399cc887bb 146 // The current transfer may be suspended (if it is RX), so it must be
<> 160:d5399cc887bb 147 // resumed before the STOP task is triggered.
<> 160:d5399cc887bb 148 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 149 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 150
<> 160:d5399cc887bb 151 uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 152 twi_info->events |= I2C_EVENT_ERROR;
<> 160:d5399cc887bb 153 if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
<> 160:d5399cc887bb 154 twi_info->events |= I2C_EVENT_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 155 }
<> 160:d5399cc887bb 156 if (errorsrc & NRF_TWI_ERROR_DATA_NACK) {
<> 160:d5399cc887bb 157 twi_info->events |= I2C_EVENT_TRANSFER_EARLY_NACK;
<> 160:d5399cc887bb 158 }
<> 160:d5399cc887bb 159 }
<> 160:d5399cc887bb 160
<> 160:d5399cc887bb 161 bool finished = false;
<> 160:d5399cc887bb 162
<> 160:d5399cc887bb 163 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_TXDSENT)) {
<> 160:d5399cc887bb 164 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 165
<> 160:d5399cc887bb 166 MBED_ASSERT(twi_info->tx_length > 0);
<> 160:d5399cc887bb 167 --(twi_info->tx_length);
<> 160:d5399cc887bb 168 // Send next byte if there is still something to be sent.
<> 160:d5399cc887bb 169 if (twi_info->tx_length > 0) {
<> 160:d5399cc887bb 170 nrf_twi_txd_set(twi, *(twi_info->tx));
<> 160:d5399cc887bb 171 ++(twi_info->tx);
<> 160:d5399cc887bb 172 // It TX is done, start RX if requested.
<> 160:d5399cc887bb 173 } else if (twi_info->rx_length > 0) {
<> 160:d5399cc887bb 174 start_asynch_rx(twi_info, twi);
<> 160:d5399cc887bb 175 // If there is nothing more to do, finalize the transfer.
<> 160:d5399cc887bb 176 } else {
<> 160:d5399cc887bb 177 if (twi_info->stop) {
<> 160:d5399cc887bb 178 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 179 } else {
<> 160:d5399cc887bb 180 nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
<> 160:d5399cc887bb 181 finished = true;
<> 160:d5399cc887bb 182 }
<> 160:d5399cc887bb 183 twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
<> 160:d5399cc887bb 184 }
<> 160:d5399cc887bb 185 }
<> 160:d5399cc887bb 186
<> 160:d5399cc887bb 187 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_RXDREADY)) {
<> 160:d5399cc887bb 188 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 189
<> 160:d5399cc887bb 190 MBED_ASSERT(twi_info->rx_length > 0);
<> 160:d5399cc887bb 191 *(twi_info->rx) = nrf_twi_rxd_get(twi);
<> 160:d5399cc887bb 192 ++(twi_info->rx);
<> 160:d5399cc887bb 193 --(twi_info->rx_length);
<> 160:d5399cc887bb 194
<> 160:d5399cc887bb 195 if (twi_info->rx_length > 0) {
<> 160:d5399cc887bb 196 // If more bytes should be received, resume the transfer
<> 160:d5399cc887bb 197 // (in case the stop condition should be generated after the next
<> 160:d5399cc887bb 198 // byte, change the shortcuts configuration first).
<> 160:d5399cc887bb 199 if (twi_info->rx_length == 1 && twi_info->stop) {
<> 160:d5399cc887bb 200 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_STOP_MASK);
<> 160:d5399cc887bb 201 }
<> 160:d5399cc887bb 202 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 203 } else {
<> 160:d5399cc887bb 204 // If all requested bytes were received, finalize the transfer.
<> 160:d5399cc887bb 205 finished = true;
<> 160:d5399cc887bb 206 twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
<> 160:d5399cc887bb 207 }
<> 160:d5399cc887bb 208 }
<> 160:d5399cc887bb 209
<> 160:d5399cc887bb 210 if (finished ||
<> 160:d5399cc887bb 211 nrf_twi_event_check(twi, NRF_TWI_EVENT_STOPPED) ||
<> 160:d5399cc887bb 212 (nrf_twi_int_enable_check(twi, NRF_TWI_INT_SUSPENDED_MASK) &&
<> 160:d5399cc887bb 213 nrf_twi_event_check(twi, NRF_TWI_EVENT_SUSPENDED))) {
<> 160:d5399cc887bb 214 // There is no need to clear the STOPPED and SUSPENDED events here,
<> 160:d5399cc887bb 215 // they will no longer generate the interrupt - see below.
<> 160:d5399cc887bb 216
<> 160:d5399cc887bb 217 nrf_twi_shorts_set(twi, 0);
<> 160:d5399cc887bb 218 // Disable all interrupt sources.
<> 160:d5399cc887bb 219 nrf_twi_int_disable(twi, UINT32_MAX);
<> 160:d5399cc887bb 220 twi_info->active = false;
<> 160:d5399cc887bb 221
<> 160:d5399cc887bb 222 if (twi_info->handler) {
<> 160:d5399cc887bb 223 twi_info->handler();
<> 160:d5399cc887bb 224 }
<> 160:d5399cc887bb 225 }
<> 160:d5399cc887bb 226 }
<> 160:d5399cc887bb 227
<> 160:d5399cc887bb 228 #if TWI0_ENABLED
<> 160:d5399cc887bb 229 static void irq_handler_twi0(void)
<> 160:d5399cc887bb 230 {
<> 160:d5399cc887bb 231 twi_irq_handler(TWI0_INSTANCE_INDEX);
<> 160:d5399cc887bb 232 }
<> 160:d5399cc887bb 233 #endif
<> 160:d5399cc887bb 234 #if TWI1_ENABLED
<> 160:d5399cc887bb 235 static void irq_handler_twi1(void)
<> 160:d5399cc887bb 236 {
<> 160:d5399cc887bb 237 twi_irq_handler(TWI1_INSTANCE_INDEX);
<> 160:d5399cc887bb 238 }
<> 160:d5399cc887bb 239 #endif
<> 160:d5399cc887bb 240 static nrf_drv_irq_handler_t const m_twi_irq_handlers[TWI_COUNT] =
<> 160:d5399cc887bb 241 {
<> 160:d5399cc887bb 242 #if TWI0_ENABLED
<> 160:d5399cc887bb 243 irq_handler_twi0,
<> 160:d5399cc887bb 244 #endif
<> 160:d5399cc887bb 245 #if TWI1_ENABLED
<> 160:d5399cc887bb 246 irq_handler_twi1,
<> 160:d5399cc887bb 247 #endif
<> 160:d5399cc887bb 248 };
<> 160:d5399cc887bb 249 #endif // DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 250
<> 160:d5399cc887bb 251
<> 160:d5399cc887bb 252 static void configure_twi_pin(uint32_t pin, nrf_gpio_pin_dir_t dir)
<> 160:d5399cc887bb 253 {
<> 160:d5399cc887bb 254 nrf_gpio_cfg(pin,
<> 160:d5399cc887bb 255 dir,
<> 160:d5399cc887bb 256 NRF_GPIO_PIN_INPUT_CONNECT,
<> 160:d5399cc887bb 257 NRF_GPIO_PIN_PULLUP,
<> 160:d5399cc887bb 258 NRF_GPIO_PIN_S0D1,
<> 160:d5399cc887bb 259 NRF_GPIO_PIN_NOSENSE);
<> 160:d5399cc887bb 260 }
<> 160:d5399cc887bb 261
<> 160:d5399cc887bb 262 static void twi_clear_bus(twi_info_t *twi_info)
<> 160:d5399cc887bb 263 {
<> 160:d5399cc887bb 264 // Try to set SDA high, and check if no slave tries to drive it low.
<> 160:d5399cc887bb 265 nrf_gpio_pin_set(twi_info->pselsda);
<> 160:d5399cc887bb 266 configure_twi_pin(twi_info->pselsda, NRF_GPIO_PIN_DIR_OUTPUT);
<> 160:d5399cc887bb 267 // In case SDA is low, make up to 9 cycles on SCL line to help the slave
<> 160:d5399cc887bb 268 // that pulls SDA low release it.
<> 160:d5399cc887bb 269 if (!nrf_gpio_pin_read(twi_info->pselsda)) {
<> 160:d5399cc887bb 270 nrf_gpio_pin_set(twi_info->pselscl);
<> 160:d5399cc887bb 271 configure_twi_pin(twi_info->pselscl, NRF_GPIO_PIN_DIR_OUTPUT);
<> 160:d5399cc887bb 272 nrf_delay_us(4);
<> 160:d5399cc887bb 273
<> 160:d5399cc887bb 274 for (int i = 0; i < 9; i++) {
<> 160:d5399cc887bb 275 if (nrf_gpio_pin_read(twi_info->pselsda)) {
<> 160:d5399cc887bb 276 break;
<> 160:d5399cc887bb 277 }
<> 160:d5399cc887bb 278 nrf_gpio_pin_clear(twi_info->pselscl);
<> 160:d5399cc887bb 279 nrf_delay_us(4);
<> 160:d5399cc887bb 280 nrf_gpio_pin_set(twi_info->pselscl);
<> 160:d5399cc887bb 281 nrf_delay_us(4);
<> 160:d5399cc887bb 282 }
<> 160:d5399cc887bb 283
<> 160:d5399cc887bb 284 // Finally, generate STOP condition to put the bus into initial state.
<> 160:d5399cc887bb 285 nrf_gpio_pin_clear(twi_info->pselsda);
<> 160:d5399cc887bb 286 nrf_delay_us(4);
<> 160:d5399cc887bb 287 nrf_gpio_pin_set(twi_info->pselsda);
<> 160:d5399cc887bb 288 }
<> 160:d5399cc887bb 289 }
<> 150:02e0a0aed4ec 290
<> 144:ef7eb2e8f9f7 291 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
<> 144:ef7eb2e8f9f7 292 {
Kojto 169:e3b6fe271b81 293 ret_code_t ret;
<> 144:ef7eb2e8f9f7 294 int i;
Kojto 169:e3b6fe271b81 295
<> 144:ef7eb2e8f9f7 296 for (i = 0; i < TWI_COUNT; ++i) {
<> 144:ef7eb2e8f9f7 297 if (m_twi_info[i].initialized &&
<> 160:d5399cc887bb 298 m_twi_info[i].pselsda == (uint32_t)sda &&
<> 160:d5399cc887bb 299 m_twi_info[i].pselscl == (uint32_t)scl) {
<> 144:ef7eb2e8f9f7 300 TWI_IDX(obj) = i;
<> 160:d5399cc887bb 301 TWI_INFO(obj)->frequency = NRF_TWI_FREQ_100K;
<> 144:ef7eb2e8f9f7 302 i2c_reset(obj);
<> 144:ef7eb2e8f9f7 303 return;
<> 144:ef7eb2e8f9f7 304 }
<> 144:ef7eb2e8f9f7 305 }
<> 144:ef7eb2e8f9f7 306
<> 144:ef7eb2e8f9f7 307 for (i = 0; i < TWI_COUNT; ++i) {
<> 144:ef7eb2e8f9f7 308 if (!m_twi_info[i].initialized) {
Kojto 169:e3b6fe271b81 309 ret = nrf_drv_common_per_res_acquire(m_twi_instances[i],
Kojto 169:e3b6fe271b81 310 m_twi_irq_handlers[i]);
Kojto 169:e3b6fe271b81 311
Kojto 169:e3b6fe271b81 312 if (ret != NRF_SUCCESS) {
Kojto 169:e3b6fe271b81 313 continue; /* the hw resource is busy - test another one */
Kojto 169:e3b6fe271b81 314 }
Kojto 169:e3b6fe271b81 315
<> 160:d5399cc887bb 316 TWI_IDX(obj) = i;
<> 150:02e0a0aed4ec 317
<> 160:d5399cc887bb 318 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 319 twi_info->initialized = true;
<> 160:d5399cc887bb 320 twi_info->pselsda = (uint32_t)sda;
<> 160:d5399cc887bb 321 twi_info->pselscl = (uint32_t)scl;
<> 160:d5399cc887bb 322 twi_info->frequency = NRF_TWI_FREQ_100K;
<> 160:d5399cc887bb 323 twi_info->start_twi = false;
<> 160:d5399cc887bb 324 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 325 twi_info->active = false;
<> 160:d5399cc887bb 326 #endif
<> 160:d5399cc887bb 327
<> 160:d5399cc887bb 328 twi_clear_bus(twi_info);
<> 150:02e0a0aed4ec 329
<> 160:d5399cc887bb 330 configure_twi_pin(twi_info->pselsda, NRF_GPIO_PIN_DIR_INPUT);
<> 160:d5399cc887bb 331 configure_twi_pin(twi_info->pselscl, NRF_GPIO_PIN_DIR_INPUT);
<> 160:d5399cc887bb 332
<> 160:d5399cc887bb 333 i2c_reset(obj);
<> 144:ef7eb2e8f9f7 334
<> 160:d5399cc887bb 335 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 336 NVIC_SetVector(twi_handlers[i].IRQn, twi_handlers[i].vector);
<> 160:d5399cc887bb 337 nrf_drv_common_irq_enable(twi_handlers[i].IRQn, TWI_IRQ_PRIORITY);
<> 160:d5399cc887bb 338 #endif
<> 160:d5399cc887bb 339
<> 160:d5399cc887bb 340 return;
<> 144:ef7eb2e8f9f7 341 }
<> 144:ef7eb2e8f9f7 342 }
<> 144:ef7eb2e8f9f7 343
<> 144:ef7eb2e8f9f7 344 error("No available I2C peripheral\r\n");
<> 144:ef7eb2e8f9f7 345 }
<> 144:ef7eb2e8f9f7 346
<> 144:ef7eb2e8f9f7 347 void i2c_reset(i2c_t *obj)
<> 144:ef7eb2e8f9f7 348 {
<> 144:ef7eb2e8f9f7 349 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 350 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 144:ef7eb2e8f9f7 351
<> 160:d5399cc887bb 352 nrf_twi_disable(twi);
<> 160:d5399cc887bb 353 nrf_twi_pins_set(twi, twi_info->pselscl, twi_info->pselsda);
<> 160:d5399cc887bb 354 nrf_twi_frequency_set(twi, twi_info->frequency);
<> 160:d5399cc887bb 355 nrf_twi_enable(twi);
<> 144:ef7eb2e8f9f7 356 }
<> 144:ef7eb2e8f9f7 357
<> 144:ef7eb2e8f9f7 358 int i2c_start(i2c_t *obj)
<> 144:ef7eb2e8f9f7 359 {
<> 160:d5399cc887bb 360 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 361 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 362 if (twi_info->active) {
<> 160:d5399cc887bb 363 return I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 364 }
<> 160:d5399cc887bb 365 #endif
<> 160:d5399cc887bb 366 twi_info->start_twi = true;
<> 144:ef7eb2e8f9f7 367
<> 160:d5399cc887bb 368 return 0;
<> 144:ef7eb2e8f9f7 369 }
<> 144:ef7eb2e8f9f7 370
<> 144:ef7eb2e8f9f7 371 int i2c_stop(i2c_t *obj)
<> 144:ef7eb2e8f9f7 372 {
<> 160:d5399cc887bb 373 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 144:ef7eb2e8f9f7 374
<> 160:d5399cc887bb 375 // The current transfer may be suspended (if it is RX), so it must be
<> 160:d5399cc887bb 376 // resumed before the STOP task is triggered.
<> 160:d5399cc887bb 377 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 378 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 379 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 380 do {
<> 160:d5399cc887bb 381 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_STOPPED)) {
<> 160:d5399cc887bb 382 return 0;
<> 160:d5399cc887bb 383 }
<> 160:d5399cc887bb 384 } while (--remaining_time);
<> 160:d5399cc887bb 385
<> 160:d5399cc887bb 386 return 1;
<> 144:ef7eb2e8f9f7 387 }
<> 144:ef7eb2e8f9f7 388
<> 144:ef7eb2e8f9f7 389 void i2c_frequency(i2c_t *obj, int hz)
<> 144:ef7eb2e8f9f7 390 {
<> 144:ef7eb2e8f9f7 391 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 392 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 144:ef7eb2e8f9f7 393
<> 144:ef7eb2e8f9f7 394 if (hz < 250000) {
<> 160:d5399cc887bb 395 twi_info->frequency = NRF_TWI_FREQ_100K;
<> 144:ef7eb2e8f9f7 396 } else if (hz < 400000) {
<> 160:d5399cc887bb 397 twi_info->frequency = NRF_TWI_FREQ_250K;
<> 144:ef7eb2e8f9f7 398 } else {
<> 160:d5399cc887bb 399 twi_info->frequency = NRF_TWI_FREQ_400K;
<> 144:ef7eb2e8f9f7 400 }
<> 160:d5399cc887bb 401 nrf_twi_frequency_set(twi, twi_info->frequency);
<> 160:d5399cc887bb 402 }
<> 160:d5399cc887bb 403
<> 160:d5399cc887bb 404 static uint8_t twi_address(int i2c_address)
<> 160:d5399cc887bb 405 {
<> 160:d5399cc887bb 406 // The TWI peripheral requires 7-bit slave address (without R/W bit).
<> 160:d5399cc887bb 407 return (i2c_address >> 1);
<> 160:d5399cc887bb 408 }
<> 160:d5399cc887bb 409
<> 160:d5399cc887bb 410 static void start_twi_read(NRF_TWI_Type *twi, int address)
<> 160:d5399cc887bb 411 {
<> 160:d5399cc887bb 412 nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
<> 160:d5399cc887bb 413 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 414 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 415 (void)nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 416
<> 160:d5399cc887bb 417 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_SUSPEND_MASK);
<> 160:d5399cc887bb 418
<> 160:d5399cc887bb 419 nrf_twi_address_set(twi, twi_address(address));
<> 160:d5399cc887bb 420 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 421 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTRX);
<> 144:ef7eb2e8f9f7 422 }
<> 144:ef7eb2e8f9f7 423
<> 144:ef7eb2e8f9f7 424 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
<> 144:ef7eb2e8f9f7 425 {
<> 160:d5399cc887bb 426 // Zero-length RX transfers are not supported. Such transfers cannot
<> 160:d5399cc887bb 427 // be easily achieved with TWI peripheral (some dirty tricks would be
<> 160:d5399cc887bb 428 // required for this), and they are actually useless (TX can be used
<> 160:d5399cc887bb 429 // to check if the address is acknowledged by a slave).
<> 160:d5399cc887bb 430 MBED_ASSERT(length > 0);
<> 144:ef7eb2e8f9f7 431
<> 144:ef7eb2e8f9f7 432 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 433 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 434 if (twi_info->active) {
<> 160:d5399cc887bb 435 return I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 436 }
<> 160:d5399cc887bb 437 #endif
<> 160:d5399cc887bb 438 twi_info->start_twi = false;
<> 160:d5399cc887bb 439
<> 160:d5399cc887bb 440 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 441 start_twi_read(twi, address);
<> 160:d5399cc887bb 442
<> 160:d5399cc887bb 443 int result = length;
<> 160:d5399cc887bb 444 while (length > 0) {
<> 160:d5399cc887bb 445 int byte_read_result = i2c_byte_read(obj, (stop && length == 1));
<> 160:d5399cc887bb 446 if (byte_read_result < 0) {
<> 160:d5399cc887bb 447 // When an error occurs, return the number of bytes that have been
<> 160:d5399cc887bb 448 // received successfully.
<> 160:d5399cc887bb 449 result -= length;
<> 160:d5399cc887bb 450 // Force STOP condition.
<> 160:d5399cc887bb 451 stop = 1;
<> 160:d5399cc887bb 452 break;
<> 160:d5399cc887bb 453 }
<> 160:d5399cc887bb 454 *data++ = (uint8_t)byte_read_result;
<> 160:d5399cc887bb 455 --length;
<> 160:d5399cc887bb 456 }
<> 160:d5399cc887bb 457
<> 160:d5399cc887bb 458 if (stop) {
<> 160:d5399cc887bb 459 (void)i2c_stop(obj);
<> 160:d5399cc887bb 460 }
<> 160:d5399cc887bb 461
<> 160:d5399cc887bb 462 return result;
<> 160:d5399cc887bb 463 }
<> 144:ef7eb2e8f9f7 464
<> 160:d5399cc887bb 465 static uint8_t twi_byte_write(NRF_TWI_Type *twi, uint8_t data)
<> 160:d5399cc887bb 466 {
<> 160:d5399cc887bb 467 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 468 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 469
<> 160:d5399cc887bb 470 nrf_twi_txd_set(twi, data);
<> 160:d5399cc887bb 471 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 472 do {
<> 160:d5399cc887bb 473 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_TXDSENT)) {
<> 160:d5399cc887bb 474 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 475 return 1; // ACK received
<> 160:d5399cc887bb 476 }
<> 160:d5399cc887bb 477 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
<> 160:d5399cc887bb 478 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 479 return 0; // some error occurred
<> 160:d5399cc887bb 480 }
<> 160:d5399cc887bb 481 } while (--remaining_time);
<> 160:d5399cc887bb 482
<> 160:d5399cc887bb 483 return 2; // timeout;
<> 160:d5399cc887bb 484 }
<> 160:d5399cc887bb 485
<> 160:d5399cc887bb 486 static void start_twi_write(NRF_TWI_Type *twi, int address)
<> 160:d5399cc887bb 487 {
<> 160:d5399cc887bb 488 nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
<> 160:d5399cc887bb 489 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 490 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 491 (void)nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 492
<> 160:d5399cc887bb 493 nrf_twi_shorts_set(twi, 0);
<> 160:d5399cc887bb 494
<> 160:d5399cc887bb 495 nrf_twi_address_set(twi, twi_address(address));
<> 160:d5399cc887bb 496 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 497 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
<> 144:ef7eb2e8f9f7 498 }
<> 144:ef7eb2e8f9f7 499
<> 144:ef7eb2e8f9f7 500 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
<> 144:ef7eb2e8f9f7 501 {
<> 144:ef7eb2e8f9f7 502 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 503 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 504 if (twi_info->active) {
<> 160:d5399cc887bb 505 return I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 506 }
<> 160:d5399cc887bb 507 #endif
<> 160:d5399cc887bb 508 twi_info->start_twi = false;
<> 160:d5399cc887bb 509
<> 160:d5399cc887bb 510 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 511 start_twi_write(twi, address);
<> 160:d5399cc887bb 512
<> 160:d5399cc887bb 513 // Special case - transaction with no data.
<> 160:d5399cc887bb 514 // It can be used to check if a slave acknowledges the address.
<> 160:d5399cc887bb 515 if (length == 0) {
<> 160:d5399cc887bb 516 nrf_twi_event_t event;
<> 160:d5399cc887bb 517 if (stop) {
<> 160:d5399cc887bb 518 event = NRF_TWI_EVENT_STOPPED;
<> 160:d5399cc887bb 519 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 520 } else {
<> 160:d5399cc887bb 521 event = NRF_TWI_EVENT_SUSPENDED;
<> 160:d5399cc887bb 522 nrf_twi_event_clear(twi, event);
<> 160:d5399cc887bb 523 nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
<> 160:d5399cc887bb 524 }
<> 160:d5399cc887bb 525 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 526 do {
<> 160:d5399cc887bb 527 if (nrf_twi_event_check(twi, event)) {
<> 160:d5399cc887bb 528 break;
<> 160:d5399cc887bb 529 }
<> 160:d5399cc887bb 530 } while (--remaining_time);
<> 144:ef7eb2e8f9f7 531
<> 160:d5399cc887bb 532 uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 533 if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
<> 160:d5399cc887bb 534 if (!stop) {
<> 160:d5399cc887bb 535 i2c_stop(obj);
<> 160:d5399cc887bb 536 }
<> 160:d5399cc887bb 537 return I2C_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 538 }
<> 160:d5399cc887bb 539
<> 160:d5399cc887bb 540 return (remaining_time ? 0 : I2C_ERROR_BUS_BUSY);
<> 144:ef7eb2e8f9f7 541 }
<> 160:d5399cc887bb 542
<> 160:d5399cc887bb 543 int result = length;
<> 160:d5399cc887bb 544 do {
<> 160:d5399cc887bb 545 uint8_t byte_write_result = twi_byte_write(twi, (uint8_t)*data++);
<> 160:d5399cc887bb 546 if (byte_write_result != 1) {
<> 160:d5399cc887bb 547 if (byte_write_result == 0) {
<> 160:d5399cc887bb 548 // Check what kind of error has been signaled by TWI.
<> 160:d5399cc887bb 549 uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 550 if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
<> 160:d5399cc887bb 551 result = I2C_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 552 } else {
<> 160:d5399cc887bb 553 // Some other error - return the number of bytes that
<> 160:d5399cc887bb 554 // have been sent successfully.
<> 160:d5399cc887bb 555 result -= length;
<> 160:d5399cc887bb 556 }
<> 160:d5399cc887bb 557 } else {
<> 160:d5399cc887bb 558 result = I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 559 }
<> 160:d5399cc887bb 560 // Force STOP condition.
<> 160:d5399cc887bb 561 stop = 1;
<> 160:d5399cc887bb 562 break;
<> 160:d5399cc887bb 563 }
<> 160:d5399cc887bb 564 --length;
<> 160:d5399cc887bb 565 } while (length > 0);
<> 160:d5399cc887bb 566
<> 160:d5399cc887bb 567 if (stop) {
<> 160:d5399cc887bb 568 (void)i2c_stop(obj);
<> 160:d5399cc887bb 569 }
<> 160:d5399cc887bb 570
<> 160:d5399cc887bb 571 return result;
<> 144:ef7eb2e8f9f7 572 }
<> 144:ef7eb2e8f9f7 573
<> 144:ef7eb2e8f9f7 574 int i2c_byte_read(i2c_t *obj, int last)
<> 144:ef7eb2e8f9f7 575 {
<> 160:d5399cc887bb 576 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 577
<> 160:d5399cc887bb 578 if (last) {
<> 160:d5399cc887bb 579 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_STOP_MASK);
<> 160:d5399cc887bb 580 }
<> 160:d5399cc887bb 581 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 144:ef7eb2e8f9f7 582
<> 160:d5399cc887bb 583 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 584 do {
<> 160:d5399cc887bb 585 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_RXDREADY)) {
<> 160:d5399cc887bb 586 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 587 return nrf_twi_rxd_get(twi);
<> 160:d5399cc887bb 588 }
<> 160:d5399cc887bb 589 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
<> 160:d5399cc887bb 590 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 591 return I2C_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 592 }
<> 160:d5399cc887bb 593 } while (--remaining_time);
<> 160:d5399cc887bb 594
<> 160:d5399cc887bb 595 return I2C_ERROR_BUS_BUSY;
<> 144:ef7eb2e8f9f7 596 }
<> 144:ef7eb2e8f9f7 597
<> 144:ef7eb2e8f9f7 598 int i2c_byte_write(i2c_t *obj, int data)
<> 144:ef7eb2e8f9f7 599 {
<> 160:d5399cc887bb 600 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 601 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 602 if (twi_info->start_twi) {
<> 160:d5399cc887bb 603 twi_info->start_twi = false;
<> 144:ef7eb2e8f9f7 604
<> 160:d5399cc887bb 605 if (data & 1) {
<> 160:d5399cc887bb 606 start_twi_read(twi, data);
<> 160:d5399cc887bb 607 } else {
<> 160:d5399cc887bb 608 start_twi_write(twi, data);
<> 160:d5399cc887bb 609 }
<> 160:d5399cc887bb 610 return 1;
<> 160:d5399cc887bb 611 } else {
<> 160:d5399cc887bb 612 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 613 // 0 - TWI signaled error (NAK is the only possibility here)
<> 160:d5399cc887bb 614 // 1 - ACK received
<> 160:d5399cc887bb 615 // 2 - timeout (clock stretched for too long?)
<> 160:d5399cc887bb 616 return twi_byte_write(twi, (uint8_t)data);
<> 160:d5399cc887bb 617 }
<> 144:ef7eb2e8f9f7 618 }
<> 144:ef7eb2e8f9f7 619
<> 144:ef7eb2e8f9f7 620
<> 144:ef7eb2e8f9f7 621 #if DEVICE_I2C_ASYNCH
<> 144:ef7eb2e8f9f7 622 void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length,
<> 144:ef7eb2e8f9f7 623 void *rx, size_t rx_length, uint32_t address,
<> 144:ef7eb2e8f9f7 624 uint32_t stop, uint32_t handler,
<> 144:ef7eb2e8f9f7 625 uint32_t event, DMAUsage hint)
<> 144:ef7eb2e8f9f7 626 {
<> 144:ef7eb2e8f9f7 627 (void)hint;
<> 144:ef7eb2e8f9f7 628
<> 144:ef7eb2e8f9f7 629 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 630 if (twi_info->active) {
<> 160:d5399cc887bb 631 return;
<> 160:d5399cc887bb 632 }
<> 160:d5399cc887bb 633 twi_info->active = true;
<> 160:d5399cc887bb 634 twi_info->events = 0;
<> 160:d5399cc887bb 635 twi_info->handler = (void (*)(void))handler;
<> 160:d5399cc887bb 636 twi_info->evt_mask = event;
<> 160:d5399cc887bb 637 twi_info->tx_length = tx_length;
<> 160:d5399cc887bb 638 twi_info->tx = tx;
<> 160:d5399cc887bb 639 twi_info->rx_length = rx_length;
<> 160:d5399cc887bb 640 twi_info->rx = rx;
<> 160:d5399cc887bb 641 twi_info->stop = stop;
<> 144:ef7eb2e8f9f7 642
<> 160:d5399cc887bb 643 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 644
<> 160:d5399cc887bb 645 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 646 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 647 nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
<> 160:d5399cc887bb 648 nrf_twi_event_clear(twi, NRF_TWI_EVENT_SUSPENDED);
<> 160:d5399cc887bb 649 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 650 (void)nrf_twi_errorsrc_get_and_clear(twi);
<> 144:ef7eb2e8f9f7 651
<> 160:d5399cc887bb 652 nrf_twi_address_set(twi, twi_address(address));
<> 160:d5399cc887bb 653 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 654 // TX only, or TX + RX (after a repeated start).
<> 160:d5399cc887bb 655 if (tx_length > 0) {
<> 160:d5399cc887bb 656 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
<> 160:d5399cc887bb 657 nrf_twi_txd_set(twi, *(twi_info->tx));
<> 160:d5399cc887bb 658 ++(twi_info->tx);
<> 160:d5399cc887bb 659 // RX only.
<> 160:d5399cc887bb 660 } else if (rx_length > 0) {
<> 160:d5399cc887bb 661 start_asynch_rx(twi_info, twi);
<> 160:d5399cc887bb 662 // Both 'tx_length' and 'rx_length' are 0 - this case may be used
<> 160:d5399cc887bb 663 // to test if the slave is presentand ready for transfer (by just
<> 160:d5399cc887bb 664 // sending the address and checking if it is acknowledged).
<> 160:d5399cc887bb 665 } else {
<> 160:d5399cc887bb 666 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
<> 160:d5399cc887bb 667 if (stop) {
<> 160:d5399cc887bb 668 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 669 } else {
<> 160:d5399cc887bb 670 nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
<> 160:d5399cc887bb 671 nrf_twi_int_enable(twi, NRF_TWI_INT_SUSPENDED_MASK);
<> 160:d5399cc887bb 672 }
<> 160:d5399cc887bb 673 twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
<> 144:ef7eb2e8f9f7 674 }
<> 160:d5399cc887bb 675
<> 160:d5399cc887bb 676 nrf_twi_int_enable(twi, NRF_TWI_INT_TXDSENT_MASK |
<> 160:d5399cc887bb 677 NRF_TWI_INT_RXDREADY_MASK |
<> 160:d5399cc887bb 678 NRF_TWI_INT_STOPPED_MASK |
<> 160:d5399cc887bb 679 NRF_TWI_INT_ERROR_MASK);
<> 144:ef7eb2e8f9f7 680 }
<> 144:ef7eb2e8f9f7 681
<> 144:ef7eb2e8f9f7 682 uint32_t i2c_irq_handler_asynch(i2c_t *obj)
<> 144:ef7eb2e8f9f7 683 {
<> 144:ef7eb2e8f9f7 684 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 685 return (twi_info->events & twi_info->evt_mask);
<> 144:ef7eb2e8f9f7 686 }
<> 144:ef7eb2e8f9f7 687
<> 144:ef7eb2e8f9f7 688 uint8_t i2c_active(i2c_t *obj)
<> 144:ef7eb2e8f9f7 689 {
<> 160:d5399cc887bb 690 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 691 return twi_info->active;
<> 144:ef7eb2e8f9f7 692 }
<> 144:ef7eb2e8f9f7 693
<> 144:ef7eb2e8f9f7 694 void i2c_abort_asynch(i2c_t *obj)
<> 144:ef7eb2e8f9f7 695 {
<> 144:ef7eb2e8f9f7 696 i2c_reset(obj);
<> 144:ef7eb2e8f9f7 697 }
<> 144:ef7eb2e8f9f7 698 #endif // DEVICE_I2C_ASYNCH
<> 144:ef7eb2e8f9f7 699
<> 144:ef7eb2e8f9f7 700 #endif // DEVICE_I2C