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