mbed library sources. Supersedes mbed-src.

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Tue Mar 14 16:40:56 2017 +0000
Revision:
160:d5399cc887bb
Parent:
150:02e0a0aed4ec
Child:
165:e614a9f1c9e2
This updates the lib to the mbed lib v138

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"
<> 160:d5399cc887bb 48 #include "nrf_drv_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
<> 144:ef7eb2e8f9f7 65 typedef struct {
<> 160:d5399cc887bb 66 bool initialized;
<> 160:d5399cc887bb 67 uint32_t pselsda;
<> 160:d5399cc887bb 68 uint32_t pselscl;
<> 160:d5399cc887bb 69 nrf_twi_frequency_t frequency;
<> 160:d5399cc887bb 70 bool start_twi;
<> 144:ef7eb2e8f9f7 71
<> 160:d5399cc887bb 72 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 73 volatile bool active;
<> 160:d5399cc887bb 74 uint8_t const *tx;
<> 160:d5399cc887bb 75 size_t tx_length;
<> 160:d5399cc887bb 76 uint8_t *rx;
<> 160:d5399cc887bb 77 size_t rx_length;
<> 160:d5399cc887bb 78 bool stop;
<> 160:d5399cc887bb 79
<> 160:d5399cc887bb 80 volatile uint32_t events;
<> 160:d5399cc887bb 81 void (*handler)(void);
<> 160:d5399cc887bb 82 uint32_t evt_mask;
<> 160:d5399cc887bb 83 #endif // DEVICE_I2C_ASYNCH
<> 144:ef7eb2e8f9f7 84 } twi_info_t;
<> 144:ef7eb2e8f9f7 85 static twi_info_t m_twi_info[TWI_COUNT];
<> 144:ef7eb2e8f9f7 86
<> 160:d5399cc887bb 87 static NRF_TWI_Type * const m_twi_instances[TWI_COUNT] = {
<> 144:ef7eb2e8f9f7 88 #if TWI0_ENABLED
<> 160:d5399cc887bb 89 NRF_TWI0,
<> 144:ef7eb2e8f9f7 90 #endif
<> 144:ef7eb2e8f9f7 91 #if TWI1_ENABLED
<> 160:d5399cc887bb 92 NRF_TWI1,
<> 144:ef7eb2e8f9f7 93 #endif
<> 144:ef7eb2e8f9f7 94 };
<> 144:ef7eb2e8f9f7 95
<> 150:02e0a0aed4ec 96 void SPI0_TWI0_IRQHandler(void);
<> 150:02e0a0aed4ec 97 void SPI1_TWI1_IRQHandler(void);
<> 150:02e0a0aed4ec 98
<> 150:02e0a0aed4ec 99 static const peripheral_handler_desc_t twi_handlers[TWI_COUNT] =
<> 150:02e0a0aed4ec 100 {
<> 150:02e0a0aed4ec 101 #if TWI0_ENABLED
<> 150:02e0a0aed4ec 102 {
<> 150:02e0a0aed4ec 103 SPI0_TWI0_IRQn,
<> 150:02e0a0aed4ec 104 (uint32_t) SPI0_TWI0_IRQHandler
<> 150:02e0a0aed4ec 105 },
<> 150:02e0a0aed4ec 106 #endif
<> 150:02e0a0aed4ec 107 #if TWI1_ENABLED
<> 150:02e0a0aed4ec 108 {
<> 150:02e0a0aed4ec 109 SPI1_TWI1_IRQn,
<> 150:02e0a0aed4ec 110 (uint32_t) SPI1_TWI1_IRQHandler
<> 150:02e0a0aed4ec 111 }
<> 160:d5399cc887bb 112 #endif
<> 150:02e0a0aed4ec 113 };
<> 160:d5399cc887bb 114 #ifdef NRF51
<> 160:d5399cc887bb 115 #define TWI_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
<> 160:d5399cc887bb 116 #elif defined(NRF52)
<> 160:d5399cc887bb 117 #define TWI_IRQ_PRIORITY APP_IRQ_PRIORITY_LOWEST
<> 160:d5399cc887bb 118 #endif
<> 160:d5399cc887bb 119
<> 160:d5399cc887bb 120
<> 160:d5399cc887bb 121 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 122 static void start_asynch_rx(twi_info_t *twi_info, NRF_TWI_Type *twi)
<> 160:d5399cc887bb 123 {
<> 160:d5399cc887bb 124 if (twi_info->rx_length == 1 && twi_info->stop) {
<> 160:d5399cc887bb 125 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_STOP_MASK);
<> 160:d5399cc887bb 126 } else {
<> 160:d5399cc887bb 127 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_SUSPEND_MASK);
<> 160:d5399cc887bb 128 }
<> 160:d5399cc887bb 129 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTRX);
<> 160:d5399cc887bb 130 }
<> 160:d5399cc887bb 131
<> 160:d5399cc887bb 132 static void twi_irq_handler(uint8_t instance_idx)
<> 160:d5399cc887bb 133 {
<> 160:d5399cc887bb 134 twi_info_t *twi_info = &m_twi_info[instance_idx];
<> 160:d5399cc887bb 135
<> 160:d5399cc887bb 136 NRF_TWI_Type *twi = m_twi_instances[instance_idx];
<> 160:d5399cc887bb 137 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
<> 160:d5399cc887bb 138 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 139
<> 160:d5399cc887bb 140 // In case of an error, force STOP.
<> 160:d5399cc887bb 141 // The current transfer may be suspended (if it is RX), so it must be
<> 160:d5399cc887bb 142 // resumed before the STOP task is triggered.
<> 160:d5399cc887bb 143 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 144 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 145
<> 160:d5399cc887bb 146 uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 147 twi_info->events |= I2C_EVENT_ERROR;
<> 160:d5399cc887bb 148 if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
<> 160:d5399cc887bb 149 twi_info->events |= I2C_EVENT_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 150 }
<> 160:d5399cc887bb 151 if (errorsrc & NRF_TWI_ERROR_DATA_NACK) {
<> 160:d5399cc887bb 152 twi_info->events |= I2C_EVENT_TRANSFER_EARLY_NACK;
<> 160:d5399cc887bb 153 }
<> 160:d5399cc887bb 154 }
<> 160:d5399cc887bb 155
<> 160:d5399cc887bb 156 bool finished = false;
<> 160:d5399cc887bb 157
<> 160:d5399cc887bb 158 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_TXDSENT)) {
<> 160:d5399cc887bb 159 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 160
<> 160:d5399cc887bb 161 MBED_ASSERT(twi_info->tx_length > 0);
<> 160:d5399cc887bb 162 --(twi_info->tx_length);
<> 160:d5399cc887bb 163 // Send next byte if there is still something to be sent.
<> 160:d5399cc887bb 164 if (twi_info->tx_length > 0) {
<> 160:d5399cc887bb 165 nrf_twi_txd_set(twi, *(twi_info->tx));
<> 160:d5399cc887bb 166 ++(twi_info->tx);
<> 160:d5399cc887bb 167 // It TX is done, start RX if requested.
<> 160:d5399cc887bb 168 } else if (twi_info->rx_length > 0) {
<> 160:d5399cc887bb 169 start_asynch_rx(twi_info, twi);
<> 160:d5399cc887bb 170 // If there is nothing more to do, finalize the transfer.
<> 160:d5399cc887bb 171 } else {
<> 160:d5399cc887bb 172 if (twi_info->stop) {
<> 160:d5399cc887bb 173 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 174 } else {
<> 160:d5399cc887bb 175 nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
<> 160:d5399cc887bb 176 finished = true;
<> 160:d5399cc887bb 177 }
<> 160:d5399cc887bb 178 twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
<> 160:d5399cc887bb 179 }
<> 160:d5399cc887bb 180 }
<> 160:d5399cc887bb 181
<> 160:d5399cc887bb 182 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_RXDREADY)) {
<> 160:d5399cc887bb 183 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 184
<> 160:d5399cc887bb 185 MBED_ASSERT(twi_info->rx_length > 0);
<> 160:d5399cc887bb 186 *(twi_info->rx) = nrf_twi_rxd_get(twi);
<> 160:d5399cc887bb 187 ++(twi_info->rx);
<> 160:d5399cc887bb 188 --(twi_info->rx_length);
<> 160:d5399cc887bb 189
<> 160:d5399cc887bb 190 if (twi_info->rx_length > 0) {
<> 160:d5399cc887bb 191 // If more bytes should be received, resume the transfer
<> 160:d5399cc887bb 192 // (in case the stop condition should be generated after the next
<> 160:d5399cc887bb 193 // byte, change the shortcuts configuration first).
<> 160:d5399cc887bb 194 if (twi_info->rx_length == 1 && twi_info->stop) {
<> 160:d5399cc887bb 195 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_STOP_MASK);
<> 160:d5399cc887bb 196 }
<> 160:d5399cc887bb 197 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 198 } else {
<> 160:d5399cc887bb 199 // If all requested bytes were received, finalize the transfer.
<> 160:d5399cc887bb 200 finished = true;
<> 160:d5399cc887bb 201 twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
<> 160:d5399cc887bb 202 }
<> 160:d5399cc887bb 203 }
<> 160:d5399cc887bb 204
<> 160:d5399cc887bb 205 if (finished ||
<> 160:d5399cc887bb 206 nrf_twi_event_check(twi, NRF_TWI_EVENT_STOPPED) ||
<> 160:d5399cc887bb 207 (nrf_twi_int_enable_check(twi, NRF_TWI_INT_SUSPENDED_MASK) &&
<> 160:d5399cc887bb 208 nrf_twi_event_check(twi, NRF_TWI_EVENT_SUSPENDED))) {
<> 160:d5399cc887bb 209 // There is no need to clear the STOPPED and SUSPENDED events here,
<> 160:d5399cc887bb 210 // they will no longer generate the interrupt - see below.
<> 160:d5399cc887bb 211
<> 160:d5399cc887bb 212 nrf_twi_shorts_set(twi, 0);
<> 160:d5399cc887bb 213 // Disable all interrupt sources.
<> 160:d5399cc887bb 214 nrf_twi_int_disable(twi, UINT32_MAX);
<> 160:d5399cc887bb 215 twi_info->active = false;
<> 160:d5399cc887bb 216
<> 160:d5399cc887bb 217 if (twi_info->handler) {
<> 160:d5399cc887bb 218 twi_info->handler();
<> 160:d5399cc887bb 219 }
<> 160:d5399cc887bb 220 }
<> 160:d5399cc887bb 221 }
<> 160:d5399cc887bb 222
<> 160:d5399cc887bb 223 #if TWI0_ENABLED
<> 160:d5399cc887bb 224 static void irq_handler_twi0(void)
<> 160:d5399cc887bb 225 {
<> 160:d5399cc887bb 226 twi_irq_handler(TWI0_INSTANCE_INDEX);
<> 160:d5399cc887bb 227 }
<> 160:d5399cc887bb 228 #endif
<> 160:d5399cc887bb 229 #if TWI1_ENABLED
<> 160:d5399cc887bb 230 static void irq_handler_twi1(void)
<> 160:d5399cc887bb 231 {
<> 160:d5399cc887bb 232 twi_irq_handler(TWI1_INSTANCE_INDEX);
<> 160:d5399cc887bb 233 }
<> 160:d5399cc887bb 234 #endif
<> 160:d5399cc887bb 235 static nrf_drv_irq_handler_t const m_twi_irq_handlers[TWI_COUNT] =
<> 160:d5399cc887bb 236 {
<> 160:d5399cc887bb 237 #if TWI0_ENABLED
<> 160:d5399cc887bb 238 irq_handler_twi0,
<> 160:d5399cc887bb 239 #endif
<> 160:d5399cc887bb 240 #if TWI1_ENABLED
<> 160:d5399cc887bb 241 irq_handler_twi1,
<> 160:d5399cc887bb 242 #endif
<> 160:d5399cc887bb 243 };
<> 160:d5399cc887bb 244 #endif // DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 245
<> 160:d5399cc887bb 246
<> 160:d5399cc887bb 247 static void configure_twi_pin(uint32_t pin, nrf_gpio_pin_dir_t dir)
<> 160:d5399cc887bb 248 {
<> 160:d5399cc887bb 249 nrf_gpio_cfg(pin,
<> 160:d5399cc887bb 250 dir,
<> 160:d5399cc887bb 251 NRF_GPIO_PIN_INPUT_CONNECT,
<> 160:d5399cc887bb 252 NRF_GPIO_PIN_PULLUP,
<> 160:d5399cc887bb 253 NRF_GPIO_PIN_S0D1,
<> 160:d5399cc887bb 254 NRF_GPIO_PIN_NOSENSE);
<> 160:d5399cc887bb 255 }
<> 160:d5399cc887bb 256
<> 160:d5399cc887bb 257 static void twi_clear_bus(twi_info_t *twi_info)
<> 160:d5399cc887bb 258 {
<> 160:d5399cc887bb 259 // Try to set SDA high, and check if no slave tries to drive it low.
<> 160:d5399cc887bb 260 nrf_gpio_pin_set(twi_info->pselsda);
<> 160:d5399cc887bb 261 configure_twi_pin(twi_info->pselsda, NRF_GPIO_PIN_DIR_OUTPUT);
<> 160:d5399cc887bb 262 // In case SDA is low, make up to 9 cycles on SCL line to help the slave
<> 160:d5399cc887bb 263 // that pulls SDA low release it.
<> 160:d5399cc887bb 264 if (!nrf_gpio_pin_read(twi_info->pselsda)) {
<> 160:d5399cc887bb 265 nrf_gpio_pin_set(twi_info->pselscl);
<> 160:d5399cc887bb 266 configure_twi_pin(twi_info->pselscl, NRF_GPIO_PIN_DIR_OUTPUT);
<> 160:d5399cc887bb 267 nrf_delay_us(4);
<> 160:d5399cc887bb 268
<> 160:d5399cc887bb 269 for (int i = 0; i < 9; i++) {
<> 160:d5399cc887bb 270 if (nrf_gpio_pin_read(twi_info->pselsda)) {
<> 160:d5399cc887bb 271 break;
<> 160:d5399cc887bb 272 }
<> 160:d5399cc887bb 273 nrf_gpio_pin_clear(twi_info->pselscl);
<> 160:d5399cc887bb 274 nrf_delay_us(4);
<> 160:d5399cc887bb 275 nrf_gpio_pin_set(twi_info->pselscl);
<> 160:d5399cc887bb 276 nrf_delay_us(4);
<> 160:d5399cc887bb 277 }
<> 160:d5399cc887bb 278
<> 160:d5399cc887bb 279 // Finally, generate STOP condition to put the bus into initial state.
<> 160:d5399cc887bb 280 nrf_gpio_pin_clear(twi_info->pselsda);
<> 160:d5399cc887bb 281 nrf_delay_us(4);
<> 160:d5399cc887bb 282 nrf_gpio_pin_set(twi_info->pselsda);
<> 160:d5399cc887bb 283 }
<> 160:d5399cc887bb 284 }
<> 150:02e0a0aed4ec 285
<> 144:ef7eb2e8f9f7 286 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
<> 144:ef7eb2e8f9f7 287 {
<> 144:ef7eb2e8f9f7 288 int i;
<> 144:ef7eb2e8f9f7 289 for (i = 0; i < TWI_COUNT; ++i) {
<> 144:ef7eb2e8f9f7 290 if (m_twi_info[i].initialized &&
<> 160:d5399cc887bb 291 m_twi_info[i].pselsda == (uint32_t)sda &&
<> 160:d5399cc887bb 292 m_twi_info[i].pselscl == (uint32_t)scl) {
<> 144:ef7eb2e8f9f7 293 TWI_IDX(obj) = i;
<> 160:d5399cc887bb 294 TWI_INFO(obj)->frequency = NRF_TWI_FREQ_100K;
<> 144:ef7eb2e8f9f7 295 i2c_reset(obj);
<> 144:ef7eb2e8f9f7 296 return;
<> 144:ef7eb2e8f9f7 297 }
<> 144:ef7eb2e8f9f7 298 }
<> 144:ef7eb2e8f9f7 299
<> 144:ef7eb2e8f9f7 300 for (i = 0; i < TWI_COUNT; ++i) {
<> 144:ef7eb2e8f9f7 301 if (!m_twi_info[i].initialized) {
<> 160:d5399cc887bb 302 TWI_IDX(obj) = i;
<> 150:02e0a0aed4ec 303
<> 160:d5399cc887bb 304 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 305 twi_info->initialized = true;
<> 160:d5399cc887bb 306 twi_info->pselsda = (uint32_t)sda;
<> 160:d5399cc887bb 307 twi_info->pselscl = (uint32_t)scl;
<> 160:d5399cc887bb 308 twi_info->frequency = NRF_TWI_FREQ_100K;
<> 160:d5399cc887bb 309 twi_info->start_twi = false;
<> 160:d5399cc887bb 310 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 311 twi_info->active = false;
<> 160:d5399cc887bb 312 #endif
<> 160:d5399cc887bb 313
<> 160:d5399cc887bb 314 twi_clear_bus(twi_info);
<> 150:02e0a0aed4ec 315
<> 160:d5399cc887bb 316 configure_twi_pin(twi_info->pselsda, NRF_GPIO_PIN_DIR_INPUT);
<> 160:d5399cc887bb 317 configure_twi_pin(twi_info->pselscl, NRF_GPIO_PIN_DIR_INPUT);
<> 160:d5399cc887bb 318
<> 160:d5399cc887bb 319 i2c_reset(obj);
<> 144:ef7eb2e8f9f7 320
<> 160:d5399cc887bb 321 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 322 nrf_drv_common_per_res_acquire(m_twi_instances[i],
<> 160:d5399cc887bb 323 m_twi_irq_handlers[i]);
<> 160:d5399cc887bb 324 NVIC_SetVector(twi_handlers[i].IRQn, twi_handlers[i].vector);
<> 160:d5399cc887bb 325 nrf_drv_common_irq_enable(twi_handlers[i].IRQn, TWI_IRQ_PRIORITY);
<> 160:d5399cc887bb 326 #endif
<> 160:d5399cc887bb 327
<> 160:d5399cc887bb 328 return;
<> 144:ef7eb2e8f9f7 329 }
<> 144:ef7eb2e8f9f7 330 }
<> 144:ef7eb2e8f9f7 331
<> 144:ef7eb2e8f9f7 332 error("No available I2C peripheral\r\n");
<> 144:ef7eb2e8f9f7 333 }
<> 144:ef7eb2e8f9f7 334
<> 144:ef7eb2e8f9f7 335 void i2c_reset(i2c_t *obj)
<> 144:ef7eb2e8f9f7 336 {
<> 144:ef7eb2e8f9f7 337 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 338 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 144:ef7eb2e8f9f7 339
<> 160:d5399cc887bb 340 nrf_twi_disable(twi);
<> 160:d5399cc887bb 341 nrf_twi_pins_set(twi, twi_info->pselscl, twi_info->pselsda);
<> 160:d5399cc887bb 342 nrf_twi_frequency_set(twi, twi_info->frequency);
<> 160:d5399cc887bb 343 nrf_twi_enable(twi);
<> 144:ef7eb2e8f9f7 344 }
<> 144:ef7eb2e8f9f7 345
<> 144:ef7eb2e8f9f7 346 int i2c_start(i2c_t *obj)
<> 144:ef7eb2e8f9f7 347 {
<> 160:d5399cc887bb 348 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 349 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 350 if (twi_info->active) {
<> 160:d5399cc887bb 351 return I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 352 }
<> 160:d5399cc887bb 353 #endif
<> 160:d5399cc887bb 354 twi_info->start_twi = true;
<> 144:ef7eb2e8f9f7 355
<> 160:d5399cc887bb 356 return 0;
<> 144:ef7eb2e8f9f7 357 }
<> 144:ef7eb2e8f9f7 358
<> 144:ef7eb2e8f9f7 359 int i2c_stop(i2c_t *obj)
<> 144:ef7eb2e8f9f7 360 {
<> 160:d5399cc887bb 361 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 144:ef7eb2e8f9f7 362
<> 160:d5399cc887bb 363 // The current transfer may be suspended (if it is RX), so it must be
<> 160:d5399cc887bb 364 // resumed before the STOP task is triggered.
<> 160:d5399cc887bb 365 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 366 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 367 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 368 do {
<> 160:d5399cc887bb 369 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_STOPPED)) {
<> 160:d5399cc887bb 370 return 0;
<> 160:d5399cc887bb 371 }
<> 160:d5399cc887bb 372 } while (--remaining_time);
<> 160:d5399cc887bb 373
<> 160:d5399cc887bb 374 return 1;
<> 144:ef7eb2e8f9f7 375 }
<> 144:ef7eb2e8f9f7 376
<> 144:ef7eb2e8f9f7 377 void i2c_frequency(i2c_t *obj, int hz)
<> 144:ef7eb2e8f9f7 378 {
<> 144:ef7eb2e8f9f7 379 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 380 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 144:ef7eb2e8f9f7 381
<> 144:ef7eb2e8f9f7 382 if (hz < 250000) {
<> 160:d5399cc887bb 383 twi_info->frequency = NRF_TWI_FREQ_100K;
<> 144:ef7eb2e8f9f7 384 } else if (hz < 400000) {
<> 160:d5399cc887bb 385 twi_info->frequency = NRF_TWI_FREQ_250K;
<> 144:ef7eb2e8f9f7 386 } else {
<> 160:d5399cc887bb 387 twi_info->frequency = NRF_TWI_FREQ_400K;
<> 144:ef7eb2e8f9f7 388 }
<> 160:d5399cc887bb 389 nrf_twi_frequency_set(twi, twi_info->frequency);
<> 160:d5399cc887bb 390 }
<> 160:d5399cc887bb 391
<> 160:d5399cc887bb 392 static uint8_t twi_address(int i2c_address)
<> 160:d5399cc887bb 393 {
<> 160:d5399cc887bb 394 // The TWI peripheral requires 7-bit slave address (without R/W bit).
<> 160:d5399cc887bb 395 return (i2c_address >> 1);
<> 160:d5399cc887bb 396 }
<> 160:d5399cc887bb 397
<> 160:d5399cc887bb 398 static void start_twi_read(NRF_TWI_Type *twi, int address)
<> 160:d5399cc887bb 399 {
<> 160:d5399cc887bb 400 nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
<> 160:d5399cc887bb 401 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 402 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 403 (void)nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 404
<> 160:d5399cc887bb 405 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_SUSPEND_MASK);
<> 160:d5399cc887bb 406
<> 160:d5399cc887bb 407 nrf_twi_address_set(twi, twi_address(address));
<> 160:d5399cc887bb 408 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 409 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTRX);
<> 144:ef7eb2e8f9f7 410 }
<> 144:ef7eb2e8f9f7 411
<> 144:ef7eb2e8f9f7 412 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
<> 144:ef7eb2e8f9f7 413 {
<> 160:d5399cc887bb 414 // Zero-length RX transfers are not supported. Such transfers cannot
<> 160:d5399cc887bb 415 // be easily achieved with TWI peripheral (some dirty tricks would be
<> 160:d5399cc887bb 416 // required for this), and they are actually useless (TX can be used
<> 160:d5399cc887bb 417 // to check if the address is acknowledged by a slave).
<> 160:d5399cc887bb 418 MBED_ASSERT(length > 0);
<> 144:ef7eb2e8f9f7 419
<> 144:ef7eb2e8f9f7 420 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 421 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 422 if (twi_info->active) {
<> 160:d5399cc887bb 423 return I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 424 }
<> 160:d5399cc887bb 425 #endif
<> 160:d5399cc887bb 426 twi_info->start_twi = false;
<> 160:d5399cc887bb 427
<> 160:d5399cc887bb 428 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 429 start_twi_read(twi, address);
<> 160:d5399cc887bb 430
<> 160:d5399cc887bb 431 int result = length;
<> 160:d5399cc887bb 432 while (length > 0) {
<> 160:d5399cc887bb 433 int byte_read_result = i2c_byte_read(obj, (stop && length == 1));
<> 160:d5399cc887bb 434 if (byte_read_result < 0) {
<> 160:d5399cc887bb 435 // When an error occurs, return the number of bytes that have been
<> 160:d5399cc887bb 436 // received successfully.
<> 160:d5399cc887bb 437 result -= length;
<> 160:d5399cc887bb 438 // Force STOP condition.
<> 160:d5399cc887bb 439 stop = 1;
<> 160:d5399cc887bb 440 break;
<> 160:d5399cc887bb 441 }
<> 160:d5399cc887bb 442 *data++ = (uint8_t)byte_read_result;
<> 160:d5399cc887bb 443 --length;
<> 160:d5399cc887bb 444 }
<> 160:d5399cc887bb 445
<> 160:d5399cc887bb 446 if (stop) {
<> 160:d5399cc887bb 447 (void)i2c_stop(obj);
<> 160:d5399cc887bb 448 }
<> 160:d5399cc887bb 449
<> 160:d5399cc887bb 450 return result;
<> 160:d5399cc887bb 451 }
<> 144:ef7eb2e8f9f7 452
<> 160:d5399cc887bb 453 static uint8_t twi_byte_write(NRF_TWI_Type *twi, uint8_t data)
<> 160:d5399cc887bb 454 {
<> 160:d5399cc887bb 455 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 456 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 457
<> 160:d5399cc887bb 458 nrf_twi_txd_set(twi, data);
<> 160:d5399cc887bb 459 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 460 do {
<> 160:d5399cc887bb 461 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_TXDSENT)) {
<> 160:d5399cc887bb 462 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 463 return 1; // ACK received
<> 160:d5399cc887bb 464 }
<> 160:d5399cc887bb 465 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
<> 160:d5399cc887bb 466 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 467 return 0; // some error occurred
<> 160:d5399cc887bb 468 }
<> 160:d5399cc887bb 469 } while (--remaining_time);
<> 160:d5399cc887bb 470
<> 160:d5399cc887bb 471 return 2; // timeout;
<> 160:d5399cc887bb 472 }
<> 160:d5399cc887bb 473
<> 160:d5399cc887bb 474 static void start_twi_write(NRF_TWI_Type *twi, int address)
<> 160:d5399cc887bb 475 {
<> 160:d5399cc887bb 476 nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
<> 160:d5399cc887bb 477 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 478 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 479 (void)nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 480
<> 160:d5399cc887bb 481 nrf_twi_shorts_set(twi, 0);
<> 160:d5399cc887bb 482
<> 160:d5399cc887bb 483 nrf_twi_address_set(twi, twi_address(address));
<> 160:d5399cc887bb 484 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 485 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
<> 144:ef7eb2e8f9f7 486 }
<> 144:ef7eb2e8f9f7 487
<> 144:ef7eb2e8f9f7 488 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
<> 144:ef7eb2e8f9f7 489 {
<> 144:ef7eb2e8f9f7 490 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 491 #if DEVICE_I2C_ASYNCH
<> 160:d5399cc887bb 492 if (twi_info->active) {
<> 160:d5399cc887bb 493 return I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 494 }
<> 160:d5399cc887bb 495 #endif
<> 160:d5399cc887bb 496 twi_info->start_twi = false;
<> 160:d5399cc887bb 497
<> 160:d5399cc887bb 498 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 499 start_twi_write(twi, address);
<> 160:d5399cc887bb 500
<> 160:d5399cc887bb 501 // Special case - transaction with no data.
<> 160:d5399cc887bb 502 // It can be used to check if a slave acknowledges the address.
<> 160:d5399cc887bb 503 if (length == 0) {
<> 160:d5399cc887bb 504 nrf_twi_event_t event;
<> 160:d5399cc887bb 505 if (stop) {
<> 160:d5399cc887bb 506 event = NRF_TWI_EVENT_STOPPED;
<> 160:d5399cc887bb 507 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 508 } else {
<> 160:d5399cc887bb 509 event = NRF_TWI_EVENT_SUSPENDED;
<> 160:d5399cc887bb 510 nrf_twi_event_clear(twi, event);
<> 160:d5399cc887bb 511 nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
<> 160:d5399cc887bb 512 }
<> 160:d5399cc887bb 513 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 514 do {
<> 160:d5399cc887bb 515 if (nrf_twi_event_check(twi, event)) {
<> 160:d5399cc887bb 516 break;
<> 160:d5399cc887bb 517 }
<> 160:d5399cc887bb 518 } while (--remaining_time);
<> 144:ef7eb2e8f9f7 519
<> 160:d5399cc887bb 520 uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 521 if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
<> 160:d5399cc887bb 522 if (!stop) {
<> 160:d5399cc887bb 523 i2c_stop(obj);
<> 160:d5399cc887bb 524 }
<> 160:d5399cc887bb 525 return I2C_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 526 }
<> 160:d5399cc887bb 527
<> 160:d5399cc887bb 528 return (remaining_time ? 0 : I2C_ERROR_BUS_BUSY);
<> 144:ef7eb2e8f9f7 529 }
<> 160:d5399cc887bb 530
<> 160:d5399cc887bb 531 int result = length;
<> 160:d5399cc887bb 532 do {
<> 160:d5399cc887bb 533 uint8_t byte_write_result = twi_byte_write(twi, (uint8_t)*data++);
<> 160:d5399cc887bb 534 if (byte_write_result != 1) {
<> 160:d5399cc887bb 535 if (byte_write_result == 0) {
<> 160:d5399cc887bb 536 // Check what kind of error has been signaled by TWI.
<> 160:d5399cc887bb 537 uint32_t errorsrc = nrf_twi_errorsrc_get_and_clear(twi);
<> 160:d5399cc887bb 538 if (errorsrc & NRF_TWI_ERROR_ADDRESS_NACK) {
<> 160:d5399cc887bb 539 result = I2C_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 540 } else {
<> 160:d5399cc887bb 541 // Some other error - return the number of bytes that
<> 160:d5399cc887bb 542 // have been sent successfully.
<> 160:d5399cc887bb 543 result -= length;
<> 160:d5399cc887bb 544 }
<> 160:d5399cc887bb 545 } else {
<> 160:d5399cc887bb 546 result = I2C_ERROR_BUS_BUSY;
<> 160:d5399cc887bb 547 }
<> 160:d5399cc887bb 548 // Force STOP condition.
<> 160:d5399cc887bb 549 stop = 1;
<> 160:d5399cc887bb 550 break;
<> 160:d5399cc887bb 551 }
<> 160:d5399cc887bb 552 --length;
<> 160:d5399cc887bb 553 } while (length > 0);
<> 160:d5399cc887bb 554
<> 160:d5399cc887bb 555 if (stop) {
<> 160:d5399cc887bb 556 (void)i2c_stop(obj);
<> 160:d5399cc887bb 557 }
<> 160:d5399cc887bb 558
<> 160:d5399cc887bb 559 return result;
<> 144:ef7eb2e8f9f7 560 }
<> 144:ef7eb2e8f9f7 561
<> 144:ef7eb2e8f9f7 562 int i2c_byte_read(i2c_t *obj, int last)
<> 144:ef7eb2e8f9f7 563 {
<> 160:d5399cc887bb 564 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 565
<> 160:d5399cc887bb 566 if (last) {
<> 160:d5399cc887bb 567 nrf_twi_shorts_set(twi, NRF_TWI_SHORT_BB_STOP_MASK);
<> 160:d5399cc887bb 568 }
<> 160:d5399cc887bb 569 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 144:ef7eb2e8f9f7 570
<> 160:d5399cc887bb 571 uint32_t remaining_time = TIMEOUT_VALUE;
<> 160:d5399cc887bb 572 do {
<> 160:d5399cc887bb 573 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_RXDREADY)) {
<> 160:d5399cc887bb 574 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 575 return nrf_twi_rxd_get(twi);
<> 160:d5399cc887bb 576 }
<> 160:d5399cc887bb 577 if (nrf_twi_event_check(twi, NRF_TWI_EVENT_ERROR)) {
<> 160:d5399cc887bb 578 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 579 return I2C_ERROR_NO_SLAVE;
<> 160:d5399cc887bb 580 }
<> 160:d5399cc887bb 581 } while (--remaining_time);
<> 160:d5399cc887bb 582
<> 160:d5399cc887bb 583 return I2C_ERROR_BUS_BUSY;
<> 144:ef7eb2e8f9f7 584 }
<> 144:ef7eb2e8f9f7 585
<> 144:ef7eb2e8f9f7 586 int i2c_byte_write(i2c_t *obj, int data)
<> 144:ef7eb2e8f9f7 587 {
<> 160:d5399cc887bb 588 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 589 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 590 if (twi_info->start_twi) {
<> 160:d5399cc887bb 591 twi_info->start_twi = false;
<> 144:ef7eb2e8f9f7 592
<> 160:d5399cc887bb 593 if (data & 1) {
<> 160:d5399cc887bb 594 start_twi_read(twi, data);
<> 160:d5399cc887bb 595 } else {
<> 160:d5399cc887bb 596 start_twi_write(twi, data);
<> 160:d5399cc887bb 597 }
<> 160:d5399cc887bb 598 return 1;
<> 160:d5399cc887bb 599 } else {
<> 160:d5399cc887bb 600 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 601 // 0 - TWI signaled error (NAK is the only possibility here)
<> 160:d5399cc887bb 602 // 1 - ACK received
<> 160:d5399cc887bb 603 // 2 - timeout (clock stretched for too long?)
<> 160:d5399cc887bb 604 return twi_byte_write(twi, (uint8_t)data);
<> 160:d5399cc887bb 605 }
<> 144:ef7eb2e8f9f7 606 }
<> 144:ef7eb2e8f9f7 607
<> 144:ef7eb2e8f9f7 608
<> 144:ef7eb2e8f9f7 609 #if DEVICE_I2C_ASYNCH
<> 144:ef7eb2e8f9f7 610 void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length,
<> 144:ef7eb2e8f9f7 611 void *rx, size_t rx_length, uint32_t address,
<> 144:ef7eb2e8f9f7 612 uint32_t stop, uint32_t handler,
<> 144:ef7eb2e8f9f7 613 uint32_t event, DMAUsage hint)
<> 144:ef7eb2e8f9f7 614 {
<> 144:ef7eb2e8f9f7 615 (void)hint;
<> 144:ef7eb2e8f9f7 616
<> 144:ef7eb2e8f9f7 617 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 618 if (twi_info->active) {
<> 160:d5399cc887bb 619 return;
<> 160:d5399cc887bb 620 }
<> 160:d5399cc887bb 621 twi_info->active = true;
<> 160:d5399cc887bb 622 twi_info->events = 0;
<> 160:d5399cc887bb 623 twi_info->handler = (void (*)(void))handler;
<> 160:d5399cc887bb 624 twi_info->evt_mask = event;
<> 160:d5399cc887bb 625 twi_info->tx_length = tx_length;
<> 160:d5399cc887bb 626 twi_info->tx = tx;
<> 160:d5399cc887bb 627 twi_info->rx_length = rx_length;
<> 160:d5399cc887bb 628 twi_info->rx = rx;
<> 160:d5399cc887bb 629 twi_info->stop = stop;
<> 144:ef7eb2e8f9f7 630
<> 160:d5399cc887bb 631 NRF_TWI_Type *twi = m_twi_instances[TWI_IDX(obj)];
<> 160:d5399cc887bb 632
<> 160:d5399cc887bb 633 nrf_twi_event_clear(twi, NRF_TWI_EVENT_TXDSENT);
<> 160:d5399cc887bb 634 nrf_twi_event_clear(twi, NRF_TWI_EVENT_RXDREADY);
<> 160:d5399cc887bb 635 nrf_twi_event_clear(twi, NRF_TWI_EVENT_STOPPED);
<> 160:d5399cc887bb 636 nrf_twi_event_clear(twi, NRF_TWI_EVENT_SUSPENDED);
<> 160:d5399cc887bb 637 nrf_twi_event_clear(twi, NRF_TWI_EVENT_ERROR);
<> 160:d5399cc887bb 638 (void)nrf_twi_errorsrc_get_and_clear(twi);
<> 144:ef7eb2e8f9f7 639
<> 160:d5399cc887bb 640 nrf_twi_address_set(twi, twi_address(address));
<> 160:d5399cc887bb 641 nrf_twi_task_trigger(twi, NRF_TWI_TASK_RESUME);
<> 160:d5399cc887bb 642 // TX only, or TX + RX (after a repeated start).
<> 160:d5399cc887bb 643 if (tx_length > 0) {
<> 160:d5399cc887bb 644 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
<> 160:d5399cc887bb 645 nrf_twi_txd_set(twi, *(twi_info->tx));
<> 160:d5399cc887bb 646 ++(twi_info->tx);
<> 160:d5399cc887bb 647 // RX only.
<> 160:d5399cc887bb 648 } else if (rx_length > 0) {
<> 160:d5399cc887bb 649 start_asynch_rx(twi_info, twi);
<> 160:d5399cc887bb 650 // Both 'tx_length' and 'rx_length' are 0 - this case may be used
<> 160:d5399cc887bb 651 // to test if the slave is presentand ready for transfer (by just
<> 160:d5399cc887bb 652 // sending the address and checking if it is acknowledged).
<> 160:d5399cc887bb 653 } else {
<> 160:d5399cc887bb 654 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STARTTX);
<> 160:d5399cc887bb 655 if (stop) {
<> 160:d5399cc887bb 656 nrf_twi_task_trigger(twi, NRF_TWI_TASK_STOP);
<> 160:d5399cc887bb 657 } else {
<> 160:d5399cc887bb 658 nrf_twi_task_trigger(twi, NRF_TWI_TASK_SUSPEND);
<> 160:d5399cc887bb 659 nrf_twi_int_enable(twi, NRF_TWI_INT_SUSPENDED_MASK);
<> 160:d5399cc887bb 660 }
<> 160:d5399cc887bb 661 twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
<> 144:ef7eb2e8f9f7 662 }
<> 160:d5399cc887bb 663
<> 160:d5399cc887bb 664 nrf_twi_int_enable(twi, NRF_TWI_INT_TXDSENT_MASK |
<> 160:d5399cc887bb 665 NRF_TWI_INT_RXDREADY_MASK |
<> 160:d5399cc887bb 666 NRF_TWI_INT_STOPPED_MASK |
<> 160:d5399cc887bb 667 NRF_TWI_INT_ERROR_MASK);
<> 144:ef7eb2e8f9f7 668 }
<> 144:ef7eb2e8f9f7 669
<> 144:ef7eb2e8f9f7 670 uint32_t i2c_irq_handler_asynch(i2c_t *obj)
<> 144:ef7eb2e8f9f7 671 {
<> 144:ef7eb2e8f9f7 672 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 673 return (twi_info->events & twi_info->evt_mask);
<> 144:ef7eb2e8f9f7 674 }
<> 144:ef7eb2e8f9f7 675
<> 144:ef7eb2e8f9f7 676 uint8_t i2c_active(i2c_t *obj)
<> 144:ef7eb2e8f9f7 677 {
<> 160:d5399cc887bb 678 twi_info_t *twi_info = TWI_INFO(obj);
<> 160:d5399cc887bb 679 return twi_info->active;
<> 144:ef7eb2e8f9f7 680 }
<> 144:ef7eb2e8f9f7 681
<> 144:ef7eb2e8f9f7 682 void i2c_abort_asynch(i2c_t *obj)
<> 144:ef7eb2e8f9f7 683 {
<> 144:ef7eb2e8f9f7 684 i2c_reset(obj);
<> 144:ef7eb2e8f9f7 685 }
<> 144:ef7eb2e8f9f7 686 #endif // DEVICE_I2C_ASYNCH
<> 144:ef7eb2e8f9f7 687
<> 144:ef7eb2e8f9f7 688 #endif // DEVICE_I2C