mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_NORDIC/TARGET_NRF5/i2c_api.c@150:da61ba4e9755, 2016-11-25 (annotated)
- Committer:
- sivasuren
- Date:
- Fri Nov 25 07:57:40 2016 +0000
- Revision:
- 150:da61ba4e9755
- Parent:
- 149:156823d33999
surendar changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (c) 2013 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" |
<> | 144:ef7eb2e8f9f7 | 46 | #include "nrf_drv_twi.h" |
<> | 144:ef7eb2e8f9f7 | 47 | #include "app_util_platform.h" |
<> | 144:ef7eb2e8f9f7 | 48 | |
<> | 144:ef7eb2e8f9f7 | 49 | #if DEVICE_I2C_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 50 | #define TWI_IDX(obj) ((obj)->i2c.twi_idx) |
<> | 144:ef7eb2e8f9f7 | 51 | #else |
<> | 144:ef7eb2e8f9f7 | 52 | #define TWI_IDX(obj) ((obj)->twi_idx) |
<> | 144:ef7eb2e8f9f7 | 53 | #endif |
<> | 144:ef7eb2e8f9f7 | 54 | #define TWI_INFO(obj) (&m_twi_info[TWI_IDX(obj)]) |
<> | 144:ef7eb2e8f9f7 | 55 | |
<> | 144:ef7eb2e8f9f7 | 56 | typedef struct { |
<> | 144:ef7eb2e8f9f7 | 57 | bool initialized; |
<> | 144:ef7eb2e8f9f7 | 58 | nrf_drv_twi_config_t config; |
<> | 144:ef7eb2e8f9f7 | 59 | volatile bool transfer_finished; |
<> | 144:ef7eb2e8f9f7 | 60 | |
<> | 144:ef7eb2e8f9f7 | 61 | #if DEVICE_I2C_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 62 | volatile uint32_t events; |
<> | 144:ef7eb2e8f9f7 | 63 | void (*handler)(void); |
<> | 144:ef7eb2e8f9f7 | 64 | uint32_t event_mask; |
<> | 144:ef7eb2e8f9f7 | 65 | #endif |
<> | 144:ef7eb2e8f9f7 | 66 | } twi_info_t; |
<> | 144:ef7eb2e8f9f7 | 67 | static twi_info_t m_twi_info[TWI_COUNT]; |
<> | 144:ef7eb2e8f9f7 | 68 | |
<> | 144:ef7eb2e8f9f7 | 69 | static nrf_drv_twi_t const m_twi_instances[TWI_COUNT] = { |
<> | 144:ef7eb2e8f9f7 | 70 | #if TWI0_ENABLED |
<> | 144:ef7eb2e8f9f7 | 71 | NRF_DRV_TWI_INSTANCE(0), |
<> | 144:ef7eb2e8f9f7 | 72 | #endif |
<> | 144:ef7eb2e8f9f7 | 73 | #if TWI1_ENABLED |
<> | 144:ef7eb2e8f9f7 | 74 | NRF_DRV_TWI_INSTANCE(1), |
<> | 144:ef7eb2e8f9f7 | 75 | #endif |
<> | 144:ef7eb2e8f9f7 | 76 | }; |
<> | 144:ef7eb2e8f9f7 | 77 | |
<> | 144:ef7eb2e8f9f7 | 78 | static void twi_event_handler(nrf_drv_twi_evt_t const *event, void *context) |
<> | 144:ef7eb2e8f9f7 | 79 | { |
<> | 144:ef7eb2e8f9f7 | 80 | twi_info_t * twi_info = TWI_INFO((i2c_t *)context); |
<> | 144:ef7eb2e8f9f7 | 81 | twi_info->transfer_finished = true; |
<> | 144:ef7eb2e8f9f7 | 82 | |
<> | 144:ef7eb2e8f9f7 | 83 | #if DEVICE_I2C_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 84 | switch (event->type) { |
<> | 144:ef7eb2e8f9f7 | 85 | case NRF_DRV_TWI_EVT_DONE: |
<> | 144:ef7eb2e8f9f7 | 86 | twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE; |
<> | 144:ef7eb2e8f9f7 | 87 | break; |
<> | 144:ef7eb2e8f9f7 | 88 | |
<> | 144:ef7eb2e8f9f7 | 89 | case NRF_DRV_TWI_EVT_ADDRESS_NACK: |
<> | 144:ef7eb2e8f9f7 | 90 | twi_info->events |= I2C_EVENT_ERROR_NO_SLAVE; |
<> | 144:ef7eb2e8f9f7 | 91 | break; |
<> | 144:ef7eb2e8f9f7 | 92 | |
<> | 144:ef7eb2e8f9f7 | 93 | case NRF_DRV_TWI_EVT_DATA_NACK: |
<> | 144:ef7eb2e8f9f7 | 94 | twi_info->events |= I2C_EVENT_ERROR; |
<> | 144:ef7eb2e8f9f7 | 95 | break; |
<> | 144:ef7eb2e8f9f7 | 96 | } |
<> | 144:ef7eb2e8f9f7 | 97 | |
<> | 144:ef7eb2e8f9f7 | 98 | if (twi_info->handler) { |
<> | 144:ef7eb2e8f9f7 | 99 | twi_info->handler(); |
<> | 144:ef7eb2e8f9f7 | 100 | } |
<> | 144:ef7eb2e8f9f7 | 101 | #endif // DEVICE_I2C_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 102 | } |
<> | 144:ef7eb2e8f9f7 | 103 | |
<> | 144:ef7eb2e8f9f7 | 104 | static uint8_t twi_address(int i2c_address) |
<> | 144:ef7eb2e8f9f7 | 105 | { |
<> | 144:ef7eb2e8f9f7 | 106 | // The TWI driver requires 7-bit slave address (without R/W bit). |
<> | 144:ef7eb2e8f9f7 | 107 | return (i2c_address >> 1); |
<> | 144:ef7eb2e8f9f7 | 108 | } |
<> | 144:ef7eb2e8f9f7 | 109 | |
<> | 144:ef7eb2e8f9f7 | 110 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) |
<> | 144:ef7eb2e8f9f7 | 111 | { |
<> | 144:ef7eb2e8f9f7 | 112 | int i; |
<> | 144:ef7eb2e8f9f7 | 113 | for (i = 0; i < TWI_COUNT; ++i) { |
<> | 144:ef7eb2e8f9f7 | 114 | if (m_twi_info[i].initialized && |
<> | 144:ef7eb2e8f9f7 | 115 | m_twi_info[i].config.sda == (uint32_t)sda && |
<> | 144:ef7eb2e8f9f7 | 116 | m_twi_info[i].config.scl == (uint32_t)scl) { |
<> | 144:ef7eb2e8f9f7 | 117 | TWI_IDX(obj) = i; |
<> | 144:ef7eb2e8f9f7 | 118 | TWI_INFO(obj)->config.frequency = NRF_TWI_FREQ_100K; |
<> | 144:ef7eb2e8f9f7 | 119 | i2c_reset(obj); |
<> | 144:ef7eb2e8f9f7 | 120 | return; |
<> | 144:ef7eb2e8f9f7 | 121 | } |
<> | 144:ef7eb2e8f9f7 | 122 | } |
<> | 144:ef7eb2e8f9f7 | 123 | |
<> | 144:ef7eb2e8f9f7 | 124 | nrf_drv_twi_config_t const config = { |
<> | 144:ef7eb2e8f9f7 | 125 | .scl = scl, |
<> | 144:ef7eb2e8f9f7 | 126 | .sda = sda, |
<> | 144:ef7eb2e8f9f7 | 127 | .frequency = NRF_TWI_FREQ_100K, |
<> | 144:ef7eb2e8f9f7 | 128 | .interrupt_priority = APP_IRQ_PRIORITY_LOW, |
<> | 144:ef7eb2e8f9f7 | 129 | }; |
<> | 144:ef7eb2e8f9f7 | 130 | |
<> | 144:ef7eb2e8f9f7 | 131 | for (i = 0; i < TWI_COUNT; ++i) { |
<> | 144:ef7eb2e8f9f7 | 132 | if (!m_twi_info[i].initialized) { |
<> | 144:ef7eb2e8f9f7 | 133 | nrf_drv_twi_t const *twi = &m_twi_instances[i]; |
<> | 144:ef7eb2e8f9f7 | 134 | ret_code_t ret_code = |
<> | 144:ef7eb2e8f9f7 | 135 | nrf_drv_twi_init(twi, &config, twi_event_handler, obj); |
<> | 144:ef7eb2e8f9f7 | 136 | if (ret_code == NRF_SUCCESS) { |
<> | 144:ef7eb2e8f9f7 | 137 | TWI_IDX(obj) = i; |
<> | 144:ef7eb2e8f9f7 | 138 | TWI_INFO(obj)->initialized = true; |
<> | 144:ef7eb2e8f9f7 | 139 | TWI_INFO(obj)->config = config; |
<> | 144:ef7eb2e8f9f7 | 140 | |
<> | 144:ef7eb2e8f9f7 | 141 | nrf_drv_twi_enable(twi); |
<> | 144:ef7eb2e8f9f7 | 142 | return; |
<> | 144:ef7eb2e8f9f7 | 143 | } |
<> | 144:ef7eb2e8f9f7 | 144 | } |
<> | 144:ef7eb2e8f9f7 | 145 | } |
<> | 144:ef7eb2e8f9f7 | 146 | |
<> | 144:ef7eb2e8f9f7 | 147 | // No available peripheral |
<> | 144:ef7eb2e8f9f7 | 148 | error("No available I2C peripheral\r\n"); |
<> | 144:ef7eb2e8f9f7 | 149 | } |
<> | 144:ef7eb2e8f9f7 | 150 | |
<> | 144:ef7eb2e8f9f7 | 151 | void i2c_reset(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 152 | { |
<> | 144:ef7eb2e8f9f7 | 153 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 154 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 155 | |
<> | 144:ef7eb2e8f9f7 | 156 | nrf_drv_twi_uninit(twi); |
<> | 144:ef7eb2e8f9f7 | 157 | nrf_drv_twi_init(twi, &twi_info->config, twi_event_handler, obj); |
<> | 144:ef7eb2e8f9f7 | 158 | nrf_drv_twi_enable(twi); |
<> | 144:ef7eb2e8f9f7 | 159 | } |
<> | 144:ef7eb2e8f9f7 | 160 | |
<> | 144:ef7eb2e8f9f7 | 161 | int i2c_start(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 162 | { |
<> | 144:ef7eb2e8f9f7 | 163 | (void)obj; |
<> | 144:ef7eb2e8f9f7 | 164 | |
<> | 144:ef7eb2e8f9f7 | 165 | return -1; // Not implemented. |
<> | 144:ef7eb2e8f9f7 | 166 | } |
<> | 144:ef7eb2e8f9f7 | 167 | |
<> | 144:ef7eb2e8f9f7 | 168 | int i2c_stop(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 169 | { |
<> | 144:ef7eb2e8f9f7 | 170 | (void)obj; |
<> | 144:ef7eb2e8f9f7 | 171 | |
<> | 144:ef7eb2e8f9f7 | 172 | return -1; // Not implemented. |
<> | 144:ef7eb2e8f9f7 | 173 | } |
<> | 144:ef7eb2e8f9f7 | 174 | |
<> | 144:ef7eb2e8f9f7 | 175 | void i2c_frequency(i2c_t *obj, int hz) |
<> | 144:ef7eb2e8f9f7 | 176 | { |
<> | 144:ef7eb2e8f9f7 | 177 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 178 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 179 | |
<> | 144:ef7eb2e8f9f7 | 180 | if (hz < 250000) { |
<> | 144:ef7eb2e8f9f7 | 181 | twi_info->config.frequency = NRF_TWI_FREQ_100K; |
<> | 144:ef7eb2e8f9f7 | 182 | } else if (hz < 400000) { |
<> | 144:ef7eb2e8f9f7 | 183 | twi_info->config.frequency = NRF_TWI_FREQ_250K; |
<> | 144:ef7eb2e8f9f7 | 184 | } else { |
<> | 144:ef7eb2e8f9f7 | 185 | twi_info->config.frequency = NRF_TWI_FREQ_400K; |
<> | 144:ef7eb2e8f9f7 | 186 | } |
<> | 144:ef7eb2e8f9f7 | 187 | nrf_twi_frequency_set(twi->reg.p_twi, twi_info->config.frequency); |
<> | 144:ef7eb2e8f9f7 | 188 | } |
<> | 144:ef7eb2e8f9f7 | 189 | |
<> | 144:ef7eb2e8f9f7 | 190 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) |
<> | 144:ef7eb2e8f9f7 | 191 | { |
<> | 144:ef7eb2e8f9f7 | 192 | (void)stop; |
<> | 144:ef7eb2e8f9f7 | 193 | |
<> | 144:ef7eb2e8f9f7 | 194 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 195 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 196 | |
<> | 144:ef7eb2e8f9f7 | 197 | twi_info->transfer_finished = false; |
<> | 144:ef7eb2e8f9f7 | 198 | ret_code_t ret_code = nrf_drv_twi_rx(twi, twi_address(address), |
<> | 144:ef7eb2e8f9f7 | 199 | (uint8_t *)data, length); |
<> | 144:ef7eb2e8f9f7 | 200 | if (ret_code != NRF_SUCCESS) { |
<> | 144:ef7eb2e8f9f7 | 201 | return 0; |
<> | 144:ef7eb2e8f9f7 | 202 | } |
<> | 144:ef7eb2e8f9f7 | 203 | while (!twi_info->transfer_finished) {} |
<> | 144:ef7eb2e8f9f7 | 204 | return nrf_drv_twi_data_count_get(twi); |
<> | 144:ef7eb2e8f9f7 | 205 | } |
<> | 144:ef7eb2e8f9f7 | 206 | |
<> | 144:ef7eb2e8f9f7 | 207 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) |
<> | 144:ef7eb2e8f9f7 | 208 | { |
<> | 144:ef7eb2e8f9f7 | 209 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 210 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 211 | |
<> | 144:ef7eb2e8f9f7 | 212 | twi_info->transfer_finished = false; |
<> | 144:ef7eb2e8f9f7 | 213 | ret_code_t ret_code = nrf_drv_twi_tx(twi, twi_address(address), |
<> | 144:ef7eb2e8f9f7 | 214 | (uint8_t const *)data, length, (stop == 0)); |
<> | 144:ef7eb2e8f9f7 | 215 | if (ret_code != NRF_SUCCESS) { |
<> | 144:ef7eb2e8f9f7 | 216 | return 0; |
<> | 144:ef7eb2e8f9f7 | 217 | } |
<> | 144:ef7eb2e8f9f7 | 218 | while (!twi_info->transfer_finished) {} |
<> | 144:ef7eb2e8f9f7 | 219 | return nrf_drv_twi_data_count_get(twi); |
<> | 144:ef7eb2e8f9f7 | 220 | } |
<> | 144:ef7eb2e8f9f7 | 221 | |
<> | 144:ef7eb2e8f9f7 | 222 | int i2c_byte_read(i2c_t *obj, int last) |
<> | 144:ef7eb2e8f9f7 | 223 | { |
<> | 144:ef7eb2e8f9f7 | 224 | (void)obj; |
<> | 144:ef7eb2e8f9f7 | 225 | (void)last; |
<> | 144:ef7eb2e8f9f7 | 226 | |
<> | 144:ef7eb2e8f9f7 | 227 | return -1; // Not implemented. |
<> | 144:ef7eb2e8f9f7 | 228 | } |
<> | 144:ef7eb2e8f9f7 | 229 | |
<> | 144:ef7eb2e8f9f7 | 230 | int i2c_byte_write(i2c_t *obj, int data) |
<> | 144:ef7eb2e8f9f7 | 231 | { |
<> | 144:ef7eb2e8f9f7 | 232 | (void)obj; |
<> | 144:ef7eb2e8f9f7 | 233 | (void)data; |
<> | 144:ef7eb2e8f9f7 | 234 | |
<> | 144:ef7eb2e8f9f7 | 235 | return -1; // Not implemented. |
<> | 144:ef7eb2e8f9f7 | 236 | } |
<> | 144:ef7eb2e8f9f7 | 237 | |
<> | 144:ef7eb2e8f9f7 | 238 | |
<> | 144:ef7eb2e8f9f7 | 239 | #if DEVICE_I2C_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 240 | |
<> | 144:ef7eb2e8f9f7 | 241 | void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, |
<> | 144:ef7eb2e8f9f7 | 242 | void *rx, size_t rx_length, uint32_t address, |
<> | 144:ef7eb2e8f9f7 | 243 | uint32_t stop, uint32_t handler, |
<> | 144:ef7eb2e8f9f7 | 244 | uint32_t event, DMAUsage hint) |
<> | 144:ef7eb2e8f9f7 | 245 | { |
<> | 144:ef7eb2e8f9f7 | 246 | (void)stop; |
<> | 144:ef7eb2e8f9f7 | 247 | (void)hint; |
<> | 144:ef7eb2e8f9f7 | 248 | |
<> | 144:ef7eb2e8f9f7 | 249 | if (i2c_active(obj)) { |
<> | 144:ef7eb2e8f9f7 | 250 | return; |
<> | 144:ef7eb2e8f9f7 | 251 | } |
<> | 144:ef7eb2e8f9f7 | 252 | if ((tx_length == 0) && (rx_length == 0)) { |
<> | 144:ef7eb2e8f9f7 | 253 | return; |
<> | 144:ef7eb2e8f9f7 | 254 | } |
<> | 144:ef7eb2e8f9f7 | 255 | |
<> | 144:ef7eb2e8f9f7 | 256 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 257 | twi_info->events = 0; |
<> | 144:ef7eb2e8f9f7 | 258 | twi_info->handler = (void (*)(void))handler; |
<> | 144:ef7eb2e8f9f7 | 259 | twi_info->event_mask = event; |
<> | 144:ef7eb2e8f9f7 | 260 | |
<> | 144:ef7eb2e8f9f7 | 261 | uint8_t twi_addr = twi_address(address); |
<> | 144:ef7eb2e8f9f7 | 262 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 263 | |
<> | 144:ef7eb2e8f9f7 | 264 | if ((tx_length > 0) && (rx_length == 0)) { |
<> | 144:ef7eb2e8f9f7 | 265 | nrf_drv_twi_xfer_desc_t const xfer = |
<> | 144:ef7eb2e8f9f7 | 266 | NRF_DRV_TWI_XFER_DESC_TX(twi_addr, (uint8_t *)tx, tx_length); |
<> | 144:ef7eb2e8f9f7 | 267 | nrf_drv_twi_xfer(twi, &xfer, |
<> | 144:ef7eb2e8f9f7 | 268 | stop ? 0 : NRF_DRV_TWI_FLAG_TX_NO_STOP); |
<> | 144:ef7eb2e8f9f7 | 269 | } |
<> | 144:ef7eb2e8f9f7 | 270 | else if ((tx_length == 0) && (rx_length > 0)) { |
<> | 144:ef7eb2e8f9f7 | 271 | nrf_drv_twi_xfer_desc_t const xfer = |
<> | 144:ef7eb2e8f9f7 | 272 | NRF_DRV_TWI_XFER_DESC_RX(twi_addr, rx, rx_length); |
<> | 144:ef7eb2e8f9f7 | 273 | nrf_drv_twi_xfer(twi, &xfer, 0); |
<> | 144:ef7eb2e8f9f7 | 274 | } |
<> | 144:ef7eb2e8f9f7 | 275 | else if ((tx_length > 0) && (rx_length > 0)) { |
<> | 144:ef7eb2e8f9f7 | 276 | nrf_drv_twi_xfer_desc_t const xfer = |
<> | 144:ef7eb2e8f9f7 | 277 | NRF_DRV_TWI_XFER_DESC_TXRX(twi_addr, |
<> | 144:ef7eb2e8f9f7 | 278 | (uint8_t *)tx, tx_length, rx, rx_length); |
<> | 144:ef7eb2e8f9f7 | 279 | nrf_drv_twi_xfer(twi, &xfer, 0); |
<> | 144:ef7eb2e8f9f7 | 280 | } |
<> | 144:ef7eb2e8f9f7 | 281 | } |
<> | 144:ef7eb2e8f9f7 | 282 | |
<> | 144:ef7eb2e8f9f7 | 283 | uint32_t i2c_irq_handler_asynch(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 284 | { |
<> | 144:ef7eb2e8f9f7 | 285 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 286 | return (twi_info->events & twi_info->event_mask); |
<> | 144:ef7eb2e8f9f7 | 287 | } |
<> | 144:ef7eb2e8f9f7 | 288 | |
<> | 144:ef7eb2e8f9f7 | 289 | uint8_t i2c_active(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 290 | { |
<> | 144:ef7eb2e8f9f7 | 291 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 292 | return nrf_drv_twi_is_busy(twi); |
<> | 144:ef7eb2e8f9f7 | 293 | } |
<> | 144:ef7eb2e8f9f7 | 294 | |
<> | 144:ef7eb2e8f9f7 | 295 | void i2c_abort_asynch(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 296 | { |
<> | 144:ef7eb2e8f9f7 | 297 | i2c_reset(obj); |
<> | 144:ef7eb2e8f9f7 | 298 | } |
<> | 144:ef7eb2e8f9f7 | 299 | |
<> | 144:ef7eb2e8f9f7 | 300 | #endif // DEVICE_I2C_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 301 | |
<> | 144:ef7eb2e8f9f7 | 302 | #endif // DEVICE_I2C |