test

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Fri May 26 12:39:01 2017 +0100
Revision:
166:e614a9f1c9e2
Parent:
160:d5399cc887bb
Child:
170:e3b6fe271b81
This updates the lib to the mbed lib v 143

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 166: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 166:e614a9f1c9e2 65 #ifdef TARGET_SDK13
AnnaBridge 166:e614a9f1c9e2 66 #define TWI0_INSTANCE_INDEX 0
AnnaBridge 166:e614a9f1c9e2 67 #define TWI1_INSTANCE_INDEX TWI0_INSTANCE_INDEX+TWI0_ENABLED
AnnaBridge 166:e614a9f1c9e2 68 #endif
AnnaBridge 166: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 166: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 166:e614a9f1c9e2 111 #endif
AnnaBridge 166: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 166:e614a9f1c9e2 117 #endif
<> 150:02e0a0aed4ec 118 };
<> 160:d5399cc887bb 119 #ifdef NRF51
<> 160:d5399cc887bb 120 #define TWI_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
AnnaBridge 166: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 {
<> 144:ef7eb2e8f9f7 293 int i;
<> 144:ef7eb2e8f9f7 294 for (i = 0; i < TWI_COUNT; ++i) {
<> 144:ef7eb2e8f9f7 295 if (m_twi_info[i].initialized &&
<> 160:d5399cc887bb 296 m_twi_info[i].pselsda == (uint32_t)sda &&
<> 160:d5399cc887bb 297 m_twi_info[i].pselscl == (uint32_t)scl) {
<> 144:ef7eb2e8f9f7 298 TWI_IDX(obj) = i;
<> 160:d5399cc887bb 299 TWI_INFO(obj)->frequency = NRF_TWI_FREQ_100K;
<> 144:ef7eb2e8f9f7 300 i2c_reset(obj);
<> 144:ef7eb2e8f9f7 301 return;
<> 144:ef7eb2e8f9f7 302 }
<> 144:ef7eb2e8f9f7 303 }
<> 144:ef7eb2e8f9f7 304
<> 144:ef7eb2e8f9f7 305 for (i = 0; i < TWI_COUNT; ++i) {
<> 144:ef7eb2e8f9f7 306 if (!m_twi_info[i].initialized) {
<> 160:d5399cc887bb 307 TWI_IDX(obj) = i;
<> 150:02e0a0aed4ec 308
<> 160:d5399cc887bb 309 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 310 twi_info->initialized = true;
<> 160:d5399cc887bb 311 twi_info->pselsda = (uint32_t)sda;
<> 160:d5399cc887bb 312 twi_info->pselscl = (uint32_t)scl;
<> 160:d5399cc887bb 313 twi_info->frequency = NRF_TWI_FREQ_100K;
<> 160:d5399cc887bb 314 twi_info->start_twi = false;
<> 160:d5399cc887bb 315 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 316 twi_info->active = false;
<> 160:d5399cc887bb 317 #endif
<> 160:d5399cc887bb 318
<> 160:d5399cc887bb 319 twi_clear_bus(twi_info);
<> 150:02e0a0aed4ec 320
<> 160:d5399cc887bb 321 configure_twi_pin(twi_info->pselsda, NRF_GPIO_PIN_DIR_INPUT);
<> 160:d5399cc887bb 322 configure_twi_pin(twi_info->pselscl, NRF_GPIO_PIN_DIR_INPUT);
<> 160:d5399cc887bb 323
<> 160:d5399cc887bb 324 i2c_reset(obj);
<> 144:ef7eb2e8f9f7 325
<> 160:d5399cc887bb 326 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 327 nrf_drv_common_per_res_acquire(m_twi_instances[i],
<> 160:d5399cc887bb 328 m_twi_irq_handlers[i]);
<> 160:d5399cc887bb 329 NVIC_SetVector(twi_handlers[i].IRQn, twi_handlers[i].vector);
<> 160:d5399cc887bb 330 nrf_drv_common_irq_enable(twi_handlers[i].IRQn, TWI_IRQ_PRIORITY);
<> 160:d5399cc887bb 331 #endif
<> 160:d5399cc887bb 332
<> 160:d5399cc887bb 333 return;
<> 144:ef7eb2e8f9f7 334 }
<> 144:ef7eb2e8f9f7 335 }
<> 144:ef7eb2e8f9f7 336
<> 144:ef7eb2e8f9f7 337 error("No available I2C peripheral\r\n");
<> 144:ef7eb2e8f9f7 338 }
<> 144:ef7eb2e8f9f7 339
<> 144:ef7eb2e8f9f7 340 void i2c_reset(i2c_t *obj)
<> 144:ef7eb2e8f9f7 341 {
<> 144:ef7eb2e8f9f7 342 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 343 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 144:ef7eb2e8f9f7 344
<> 160:d5399cc887bb 345 nrf_twi_disable(twi);
<> 160:d5399cc887bb 346 nrf_twi_pins_set(twi, twi_info->pselscl, twi_info->pselsda);
<> 160:d5399cc887bb 347 nrf_twi_frequency_set(twi, twi_info->frequency);
<> 160:d5399cc887bb 348 nrf_twi_enable(twi);
<> 144:ef7eb2e8f9f7 349 }
<> 144:ef7eb2e8f9f7 350
<> 144:ef7eb2e8f9f7 351 int i2c_start(i2c_t *obj)
<> 144:ef7eb2e8f9f7 352 {
<> 160:d5399cc887bb 353 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 354 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 355 if (twi_info->active) {
<> 160:d5399cc887bb 356 return I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 357 }
<> 160:d5399cc887bb 358 #endif
<> 160:d5399cc887bb 359 twi_info->start_twi = true;
<> 144:ef7eb2e8f9f7 360
<> 160:d5399cc887bb 361 return 0;
<> 144:ef7eb2e8f9f7 362 }
<> 144:ef7eb2e8f9f7 363
<> 144:ef7eb2e8f9f7 364 int i2c_stop(i2c_t *obj)
<> 144:ef7eb2e8f9f7 365 {
<> 160:d5399cc887bb 366 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 144:ef7eb2e8f9f7 367
<> 160:d5399cc887bb 368 // The current transfer may be suspended (if it is RX), so it must be
<> 160:d5399cc887bb 369 // resumed before the STOP task is triggered.
<> 160:d5399cc887bb 370 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 371 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 372 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 373 do {
<> 160:d5399cc887bb 374 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_STOPPED)) {
<> 160:d5399cc887bb 375 return 0;
<> 160:d5399cc887bb 376 }
<> 160:d5399cc887bb 377 } while (--remaining_time);
<> 160:d5399cc887bb 378
<> 160:d5399cc887bb 379 return 1;
<> 144:ef7eb2e8f9f7 380 }
<> 144:ef7eb2e8f9f7 381
<> 144:ef7eb2e8f9f7 382 void i2c_frequency(i2c_t *obj, int hz)
<> 144:ef7eb2e8f9f7 383 {
<> 144:ef7eb2e8f9f7 384 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 385 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 144:ef7eb2e8f9f7 386
<> 144:ef7eb2e8f9f7 387 if (hz < 250000) {
<> 160:d5399cc887bb 388 twi_info->frequency = NRF_TWI_FREQ_100K;
<> 144:ef7eb2e8f9f7 389 } else if (hz < 400000) {
<> 160:d5399cc887bb 390 twi_info->frequency = NRF_TWI_FREQ_250K;
<> 144:ef7eb2e8f9f7 391 } else {
<> 160:d5399cc887bb 392 twi_info->frequency = NRF_TWI_FREQ_400K;
<> 144:ef7eb2e8f9f7 393 }
<> 160:d5399cc887bb 394 nrf_twi_frequency_set(twi, twi_info->frequency);
<> 160:d5399cc887bb 395 }
<> 160:d5399cc887bb 396
<> 160:d5399cc887bb 397 static uint8_t twi_address(int i2c_address)
<> 160:d5399cc887bb 398 {
<> 160:d5399cc887bb 399 // The TWI peripheral requires 7-bit slave address (without R/W bit).
<> 160:d5399cc887bb 400 return (i2c_address >> 1);
<> 160:d5399cc887bb 401 }
<> 160:d5399cc887bb 402
<> 160:d5399cc887bb 403 static void start_twi_read(NRF_TWI_Type *twi, int address)
<> 160:d5399cc887bb 404 {
<> 160:d5399cc887bb 405 nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
<> 160:d5399cc887bb 406 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 407 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 408 (void)nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 409
<> 160:d5399cc887bb 410 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_SUSPEND_MASK);
<> 160:d5399cc887bb 411
<> 160:d5399cc887bb 412 nrf_twi_address_set(twi, twi_address(address));
<> 160:d5399cc887bb 413 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 414 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTRX);
<> 144:ef7eb2e8f9f7 415 }
<> 144:ef7eb2e8f9f7 416
<> 144:ef7eb2e8f9f7 417 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
<> 144:ef7eb2e8f9f7 418 {
<> 160:d5399cc887bb 419 // Zero-length RX transfers are not supported. Such transfers cannot
<> 160:d5399cc887bb 420 // be easily achieved with TWI peripheral (some dirty tricks would be
<> 160:d5399cc887bb 421 // required for this), and they are actually useless (TX can be used
<> 160:d5399cc887bb 422 // to check if the address is acknowledged by a slave).
<> 160:d5399cc887bb 423 MBED_ASSERT(length > 0);
<> 144:ef7eb2e8f9f7 424
<> 144:ef7eb2e8f9f7 425 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 426 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 427 if (twi_info->active) {
<> 160:d5399cc887bb 428 return I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 429 }
<> 160:d5399cc887bb 430 #endif
<> 160:d5399cc887bb 431 twi_info->start_twi = false;
<> 160:d5399cc887bb 432
<> 160:d5399cc887bb 433 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 434 start_twi_read(twi, address);
<> 160:d5399cc887bb 435
<> 160:d5399cc887bb 436 int result = length;
<> 160:d5399cc887bb 437 while (length > 0) {
<> 160:d5399cc887bb 438 int byte_read_result = i2c_byte_read(obj, (stop && length == 1));
<> 160:d5399cc887bb 439 if (byte_read_result < 0) {
<> 160:d5399cc887bb 440 // When an error occurs, return the number of bytes that have been
<> 160:d5399cc887bb 441 // received successfully.
<> 160:d5399cc887bb 442 result -= length;
<> 160:d5399cc887bb 443 // Force STOP condition.
<> 160:d5399cc887bb 444 stop = 1;
<> 160:d5399cc887bb 445 break;
<> 160:d5399cc887bb 446 }
<> 160:d5399cc887bb 447 *data++ = (uint8_t)byte_read_result;
<> 160:d5399cc887bb 448 --length;
<> 160:d5399cc887bb 449 }
<> 160:d5399cc887bb 450
<> 160:d5399cc887bb 451 if (stop) {
<> 160:d5399cc887bb 452 (void)i2c_stop(obj);
<> 160:d5399cc887bb 453 }
<> 160:d5399cc887bb 454
<> 160:d5399cc887bb 455 return result;
<> 160:d5399cc887bb 456 }
<> 144:ef7eb2e8f9f7 457
<> 160:d5399cc887bb 458 static uint8_t twi_byte_write(NRF_TWI_Type *twi, uint8_t data)
<> 160:d5399cc887bb 459 {
<> 160:d5399cc887bb 460 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 461 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 462
<> 160:d5399cc887bb 463 nrf_twi_txd_set(twi, data);
<> 160:d5399cc887bb 464 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 465 do {
<> 160:d5399cc887bb 466 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_TXDSENT)) {
<> 160:d5399cc887bb 467 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 468 return 1; // ACK received
<> 160:d5399cc887bb 469 }
<> 160:d5399cc887bb 470 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
<> 160:d5399cc887bb 471 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 472 return 0; // some error occurred
<> 160:d5399cc887bb 473 }
<> 160:d5399cc887bb 474 } while (--remaining_time);
<> 160:d5399cc887bb 475
<> 160:d5399cc887bb 476 return 2; // timeout;
<> 160:d5399cc887bb 477 }
<> 160:d5399cc887bb 478
<> 160:d5399cc887bb 479 static void start_twi_write(NRF_TWI_Type *twi, int address)
<> 160:d5399cc887bb 480 {
<> 160:d5399cc887bb 481 nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
<> 160:d5399cc887bb 482 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 483 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 484 (void)nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 485
<> 160:d5399cc887bb 486 nrf_twi_shorts_set(twi, 0);
<> 160:d5399cc887bb 487
<> 160:d5399cc887bb 488 nrf_twi_address_set(twi, twi_address(address));
<> 160:d5399cc887bb 489 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 490 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
<> 144:ef7eb2e8f9f7 491 }
<> 144:ef7eb2e8f9f7 492
<> 144:ef7eb2e8f9f7 493 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
<> 144:ef7eb2e8f9f7 494 {
<> 144:ef7eb2e8f9f7 495 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 496 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 497 if (twi_info->active) {
<> 160:d5399cc887bb 498 return I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 499 }
<> 160:d5399cc887bb 500 #endif
<> 160:d5399cc887bb 501 twi_info->start_twi = false;
<> 160:d5399cc887bb 502
<> 160:d5399cc887bb 503 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 504 start_twi_write(twi, address);
<> 160:d5399cc887bb 505
<> 160:d5399cc887bb 506 // Special case - transaction with no data.
<> 160:d5399cc887bb 507 // It can be used to check if a slave acknowledges the address.
<> 160:d5399cc887bb 508 if (length == 0) {
<> 160:d5399cc887bb 509 nrf_twi_event_t event;
<> 160:d5399cc887bb 510 if (stop) {
<> 160:d5399cc887bb 511 event = NRF_TWI_EVENT_STOPPED;
<> 160:d5399cc887bb 512 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 513 } else {
<> 160:d5399cc887bb 514 event = NRF_TWI_EVENT_SUSPENDED;
<> 160:d5399cc887bb 515 nrf_twi_event_clear(twi, event);
<> 160:d5399cc887bb 516 nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
<> 160:d5399cc887bb 517 }
<> 160:d5399cc887bb 518 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 519 do {
<> 160:d5399cc887bb 520 if (nrf_twi_event_check(twi, event)) {
<> 160:d5399cc887bb 521 break;
<> 160:d5399cc887bb 522 }
<> 160:d5399cc887bb 523 } while (--remaining_time);
<> 144:ef7eb2e8f9f7 524
<> 160:d5399cc887bb 525 uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 526 if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
<> 160:d5399cc887bb 527 if (!stop) {
<> 160:d5399cc887bb 528 i2c_stop(obj);
<> 160:d5399cc887bb 529 }
<> 160:d5399cc887bb 530 return I2C_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 531 }
<> 160:d5399cc887bb 532
<> 160:d5399cc887bb 533 return (remaining_time ? 0 : I2C_ERROR_BUS_BUSY);
<> 144:ef7eb2e8f9f7 534 }
<> 160:d5399cc887bb 535
<> 160:d5399cc887bb 536 int result = length;
<> 160:d5399cc887bb 537 do {
<> 160:d5399cc887bb 538 uint8_t byte_write_result = twi_byte_write(twi, (uint8_t)*data++);
<> 160:d5399cc887bb 539 if (byte_write_result != 1) {
<> 160:d5399cc887bb 540 if (byte_write_result == 0) {
<> 160:d5399cc887bb 541 // Check what kind of error has been signaled by TWI.
<> 160:d5399cc887bb 542 uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 543 if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
<> 160:d5399cc887bb 544 result = I2C_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 545 } else {
<> 160:d5399cc887bb 546 // Some other error - return the number of bytes that
<> 160:d5399cc887bb 547 // have been sent successfully.
<> 160:d5399cc887bb 548 result -= length;
<> 160:d5399cc887bb 549 }
<> 160:d5399cc887bb 550 } else {
<> 160:d5399cc887bb 551 result = I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 552 }
<> 160:d5399cc887bb 553 // Force STOP condition.
<> 160:d5399cc887bb 554 stop = 1;
<> 160:d5399cc887bb 555 break;
<> 160:d5399cc887bb 556 }
<> 160:d5399cc887bb 557 --length;
<> 160:d5399cc887bb 558 } while (length > 0);
<> 160:d5399cc887bb 559
<> 160:d5399cc887bb 560 if (stop) {
<> 160:d5399cc887bb 561 (void)i2c_stop(obj);
<> 160:d5399cc887bb 562 }
<> 160:d5399cc887bb 563
<> 160:d5399cc887bb 564 return result;
<> 144:ef7eb2e8f9f7 565 }
<> 144:ef7eb2e8f9f7 566
<> 144:ef7eb2e8f9f7 567 int i2c_byte_read(i2c_t *obj, int last)
<> 144:ef7eb2e8f9f7 568 {
<> 160:d5399cc887bb 569 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 570
<> 160:d5399cc887bb 571 if (last) {
<> 160:d5399cc887bb 572 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_STOP_MASK);
<> 160:d5399cc887bb 573 }
<> 160:d5399cc887bb 574 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 144:ef7eb2e8f9f7 575
<> 160:d5399cc887bb 576 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 577 do {
<> 160:d5399cc887bb 578 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_RXDREADY)) {
<> 160:d5399cc887bb 579 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 580 return nrf_twi_rxd_get(twi);
<> 160:d5399cc887bb 581 }
<> 160:d5399cc887bb 582 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
<> 160:d5399cc887bb 583 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 584 return I2C_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 585 }
<> 160:d5399cc887bb 586 } while (--remaining_time);
<> 160:d5399cc887bb 587
<> 160:d5399cc887bb 588 return I2C_ERROR_BUS_BUSY;
<> 144:ef7eb2e8f9f7 589 }
<> 144:ef7eb2e8f9f7 590
<> 144:ef7eb2e8f9f7 591 int i2c_byte_write(i2c_t *obj, int data)
<> 144:ef7eb2e8f9f7 592 {
<> 160:d5399cc887bb 593 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 594 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 595 if (twi_info->start_twi) {
<> 160:d5399cc887bb 596 twi_info->start_twi = false;
<> 144:ef7eb2e8f9f7 597
<> 160:d5399cc887bb 598 if (data & 1) {
<> 160:d5399cc887bb 599 start_twi_read(twi, data);
<> 160:d5399cc887bb 600 } else {
<> 160:d5399cc887bb 601 start_twi_write(twi, data);
<> 160:d5399cc887bb 602 }
<> 160:d5399cc887bb 603 return 1;
<> 160:d5399cc887bb 604 } else {
<> 160:d5399cc887bb 605 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 606 // 0 - TWI signaled error (NAK is the only possibility here)
<> 160:d5399cc887bb 607 // 1 - ACK received
<> 160:d5399cc887bb 608 // 2 - timeout (clock stretched for too long?)
<> 160:d5399cc887bb 609 return twi_byte_write(twi, (uint8_t)data);
<> 160:d5399cc887bb 610 }
<> 144:ef7eb2e8f9f7 611 }
<> 144:ef7eb2e8f9f7 612
<> 144:ef7eb2e8f9f7 613
<> 144:ef7eb2e8f9f7 614 #if DEVICE_I2C_ASYNCH
<> 144:ef7eb2e8f9f7 615 void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length,
<> 144:ef7eb2e8f9f7 616 void *rx, size_t rx_length, uint32_t address,
<> 144:ef7eb2e8f9f7 617 uint32_t stop, uint32_t handler,
<> 144:ef7eb2e8f9f7 618 uint32_t event, DMAUsage hint)
<> 144:ef7eb2e8f9f7 619 {
<> 144:ef7eb2e8f9f7 620 (void)hint;
<> 144:ef7eb2e8f9f7 621
<> 144:ef7eb2e8f9f7 622 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 623 if (twi_info->active) {
<> 160:d5399cc887bb 624 return;
<> 160:d5399cc887bb 625 }
<> 160:d5399cc887bb 626 twi_info->active = true;
<> 160:d5399cc887bb 627 twi_info->events = 0;
<> 160:d5399cc887bb 628 twi_info->handler = (void (*)(void))handler;
<> 160:d5399cc887bb 629 twi_info->evt_mask = event;
<> 160:d5399cc887bb 630 twi_info->tx_length = tx_length;
<> 160:d5399cc887bb 631 twi_info->tx = tx;
<> 160:d5399cc887bb 632 twi_info->rx_length = rx_length;
<> 160:d5399cc887bb 633 twi_info->rx = rx;
<> 160:d5399cc887bb 634 twi_info->stop = stop;
<> 144:ef7eb2e8f9f7 635
<> 160:d5399cc887bb 636 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 637
<> 160:d5399cc887bb 638 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 639 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 640 nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
<> 160:d5399cc887bb 641 nrf_twi_event_clear(twi, NRF_TWI_EVENT_SUSPENDED);
<> 160:d5399cc887bb 642 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 643 (void)nrf_twi_errorsrc_get_and_clear(twi);
<> 144:ef7eb2e8f9f7 644
<> 160:d5399cc887bb 645 nrf_twi_address_set(twi, twi_address(address));
<> 160:d5399cc887bb 646 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 647 // TX only, or TX + RX (after a repeated start).
<> 160:d5399cc887bb 648 if (tx_length > 0) {
<> 160:d5399cc887bb 649 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
<> 160:d5399cc887bb 650 nrf_twi_txd_set(twi, *(twi_info->tx));
<> 160:d5399cc887bb 651 ++(twi_info->tx);
<> 160:d5399cc887bb 652 // RX only.
<> 160:d5399cc887bb 653 } else if (rx_length > 0) {
<> 160:d5399cc887bb 654 start_asynch_rx(twi_info, twi);
<> 160:d5399cc887bb 655 // Both 'tx_length' and 'rx_length' are 0 - this case may be used
<> 160:d5399cc887bb 656 // to test if the slave is presentand ready for transfer (by just
<> 160:d5399cc887bb 657 // sending the address and checking if it is acknowledged).
<> 160:d5399cc887bb 658 } else {
<> 160:d5399cc887bb 659 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
<> 160:d5399cc887bb 660 if (stop) {
<> 160:d5399cc887bb 661 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 662 } else {
<> 160:d5399cc887bb 663 nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
<> 160:d5399cc887bb 664 nrf_twi_int_enable(twi, NRF_TWI_INT_SUSPENDED_MASK);
<> 160:d5399cc887bb 665 }
<> 160:d5399cc887bb 666 twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
<> 144:ef7eb2e8f9f7 667 }
<> 160:d5399cc887bb 668
<> 160:d5399cc887bb 669 nrf_twi_int_enable(twi, NRF_TWI_INT_TXDSENT_MASK |
<> 160:d5399cc887bb 670 NRF_TWI_INT_RXDREADY_MASK |
<> 160:d5399cc887bb 671 NRF_TWI_INT_STOPPED_MASK |
<> 160:d5399cc887bb 672 NRF_TWI_INT_ERROR_MASK);
<> 144:ef7eb2e8f9f7 673 }
<> 144:ef7eb2e8f9f7 674
<> 144:ef7eb2e8f9f7 675 uint32_t i2c_irq_handler_asynch(i2c_t *obj)
<> 144:ef7eb2e8f9f7 676 {
<> 144:ef7eb2e8f9f7 677 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 678 return (twi_info->events & twi_info->evt_mask);
<> 144:ef7eb2e8f9f7 679 }
<> 144:ef7eb2e8f9f7 680
<> 144:ef7eb2e8f9f7 681 uint8_t i2c_active(i2c_t *obj)
<> 144:ef7eb2e8f9f7 682 {
<> 160:d5399cc887bb 683 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 684 return twi_info->active;
<> 144:ef7eb2e8f9f7 685 }
<> 144:ef7eb2e8f9f7 686
<> 144:ef7eb2e8f9f7 687 void i2c_abort_asynch(i2c_t *obj)
<> 144:ef7eb2e8f9f7 688 {
<> 144:ef7eb2e8f9f7 689 i2c_reset(obj);
<> 144:ef7eb2e8f9f7 690 }
<> 144:ef7eb2e8f9f7 691 #endif // DEVICE_I2C_ASYNCH
<> 144:ef7eb2e8f9f7 692
<> 144:ef7eb2e8f9f7 693 #endif // DEVICE_I2C