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