Forked mbed-dev as I use an 20 pins stm32F042 and not the 32 pins version
Fork of mbed-dev by
targets/TARGET_NORDIC/TARGET_NRF5/i2c_api.c@150:02e0a0aed4ec, 2016-11-08 (annotated)
- Committer:
- <>
- Date:
- Tue Nov 08 17:45:16 2016 +0000
- Revision:
- 150:02e0a0aed4ec
- Parent:
- 149:156823d33999
This updates the lib to the mbed lib v129
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 | |
<> | 150:02e0a0aed4ec | 110 | void SPI0_TWI0_IRQHandler(void); |
<> | 150:02e0a0aed4ec | 111 | void SPI1_TWI1_IRQHandler(void); |
<> | 150:02e0a0aed4ec | 112 | |
<> | 150:02e0a0aed4ec | 113 | static const peripheral_handler_desc_t twi_handlers[TWI_COUNT] = |
<> | 150:02e0a0aed4ec | 114 | { |
<> | 150:02e0a0aed4ec | 115 | #if TWI0_ENABLED |
<> | 150:02e0a0aed4ec | 116 | { |
<> | 150:02e0a0aed4ec | 117 | SPI0_TWI0_IRQn, |
<> | 150:02e0a0aed4ec | 118 | (uint32_t) SPI0_TWI0_IRQHandler |
<> | 150:02e0a0aed4ec | 119 | }, |
<> | 150:02e0a0aed4ec | 120 | #endif |
<> | 150:02e0a0aed4ec | 121 | #if TWI1_ENABLED |
<> | 150:02e0a0aed4ec | 122 | { |
<> | 150:02e0a0aed4ec | 123 | SPI1_TWI1_IRQn, |
<> | 150:02e0a0aed4ec | 124 | (uint32_t) SPI1_TWI1_IRQHandler |
<> | 150:02e0a0aed4ec | 125 | } |
<> | 150:02e0a0aed4ec | 126 | #endif |
<> | 150:02e0a0aed4ec | 127 | }; |
<> | 150:02e0a0aed4ec | 128 | |
<> | 144:ef7eb2e8f9f7 | 129 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) |
<> | 144:ef7eb2e8f9f7 | 130 | { |
<> | 144:ef7eb2e8f9f7 | 131 | int i; |
<> | 144:ef7eb2e8f9f7 | 132 | for (i = 0; i < TWI_COUNT; ++i) { |
<> | 144:ef7eb2e8f9f7 | 133 | if (m_twi_info[i].initialized && |
<> | 144:ef7eb2e8f9f7 | 134 | m_twi_info[i].config.sda == (uint32_t)sda && |
<> | 144:ef7eb2e8f9f7 | 135 | m_twi_info[i].config.scl == (uint32_t)scl) { |
<> | 144:ef7eb2e8f9f7 | 136 | TWI_IDX(obj) = i; |
<> | 144:ef7eb2e8f9f7 | 137 | TWI_INFO(obj)->config.frequency = NRF_TWI_FREQ_100K; |
<> | 144:ef7eb2e8f9f7 | 138 | i2c_reset(obj); |
<> | 144:ef7eb2e8f9f7 | 139 | return; |
<> | 144:ef7eb2e8f9f7 | 140 | } |
<> | 144:ef7eb2e8f9f7 | 141 | } |
<> | 144:ef7eb2e8f9f7 | 142 | |
<> | 144:ef7eb2e8f9f7 | 143 | nrf_drv_twi_config_t const config = { |
<> | 144:ef7eb2e8f9f7 | 144 | .scl = scl, |
<> | 144:ef7eb2e8f9f7 | 145 | .sda = sda, |
<> | 150:02e0a0aed4ec | 146 | .frequency = NRF_TWI_FREQ_100K, |
<> | 150:02e0a0aed4ec | 147 | #ifdef NRF51 |
<> | 150:02e0a0aed4ec | 148 | .interrupt_priority = APP_IRQ_PRIORITY_LOW |
<> | 150:02e0a0aed4ec | 149 | #elif defined(NRF52) |
<> | 150:02e0a0aed4ec | 150 | .interrupt_priority = APP_IRQ_PRIORITY_LOWEST |
<> | 150:02e0a0aed4ec | 151 | #endif |
<> | 150:02e0a0aed4ec | 152 | |
<> | 144:ef7eb2e8f9f7 | 153 | }; |
<> | 144:ef7eb2e8f9f7 | 154 | |
<> | 144:ef7eb2e8f9f7 | 155 | for (i = 0; i < TWI_COUNT; ++i) { |
<> | 144:ef7eb2e8f9f7 | 156 | if (!m_twi_info[i].initialized) { |
<> | 150:02e0a0aed4ec | 157 | |
<> | 150:02e0a0aed4ec | 158 | NVIC_SetVector(twi_handlers[i].IRQn, twi_handlers[i].vector); |
<> | 150:02e0a0aed4ec | 159 | |
<> | 144:ef7eb2e8f9f7 | 160 | nrf_drv_twi_t const *twi = &m_twi_instances[i]; |
<> | 144:ef7eb2e8f9f7 | 161 | ret_code_t ret_code = |
<> | 144:ef7eb2e8f9f7 | 162 | nrf_drv_twi_init(twi, &config, twi_event_handler, obj); |
<> | 144:ef7eb2e8f9f7 | 163 | if (ret_code == NRF_SUCCESS) { |
<> | 144:ef7eb2e8f9f7 | 164 | TWI_IDX(obj) = i; |
<> | 144:ef7eb2e8f9f7 | 165 | TWI_INFO(obj)->initialized = true; |
<> | 144:ef7eb2e8f9f7 | 166 | TWI_INFO(obj)->config = config; |
<> | 144:ef7eb2e8f9f7 | 167 | |
<> | 144:ef7eb2e8f9f7 | 168 | nrf_drv_twi_enable(twi); |
<> | 144:ef7eb2e8f9f7 | 169 | return; |
<> | 144:ef7eb2e8f9f7 | 170 | } |
<> | 144:ef7eb2e8f9f7 | 171 | } |
<> | 144:ef7eb2e8f9f7 | 172 | } |
<> | 144:ef7eb2e8f9f7 | 173 | |
<> | 144:ef7eb2e8f9f7 | 174 | // No available peripheral |
<> | 144:ef7eb2e8f9f7 | 175 | error("No available I2C peripheral\r\n"); |
<> | 144:ef7eb2e8f9f7 | 176 | } |
<> | 144:ef7eb2e8f9f7 | 177 | |
<> | 144:ef7eb2e8f9f7 | 178 | void i2c_reset(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 179 | { |
<> | 144:ef7eb2e8f9f7 | 180 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 181 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 182 | |
<> | 144:ef7eb2e8f9f7 | 183 | nrf_drv_twi_uninit(twi); |
<> | 144:ef7eb2e8f9f7 | 184 | nrf_drv_twi_init(twi, &twi_info->config, twi_event_handler, obj); |
<> | 144:ef7eb2e8f9f7 | 185 | nrf_drv_twi_enable(twi); |
<> | 144:ef7eb2e8f9f7 | 186 | } |
<> | 144:ef7eb2e8f9f7 | 187 | |
<> | 144:ef7eb2e8f9f7 | 188 | int i2c_start(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 189 | { |
<> | 144:ef7eb2e8f9f7 | 190 | (void)obj; |
<> | 144:ef7eb2e8f9f7 | 191 | |
<> | 144:ef7eb2e8f9f7 | 192 | return -1; // Not implemented. |
<> | 144:ef7eb2e8f9f7 | 193 | } |
<> | 144:ef7eb2e8f9f7 | 194 | |
<> | 144:ef7eb2e8f9f7 | 195 | int i2c_stop(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 196 | { |
<> | 144:ef7eb2e8f9f7 | 197 | (void)obj; |
<> | 144:ef7eb2e8f9f7 | 198 | |
<> | 144:ef7eb2e8f9f7 | 199 | return -1; // Not implemented. |
<> | 144:ef7eb2e8f9f7 | 200 | } |
<> | 144:ef7eb2e8f9f7 | 201 | |
<> | 144:ef7eb2e8f9f7 | 202 | void i2c_frequency(i2c_t *obj, int hz) |
<> | 144:ef7eb2e8f9f7 | 203 | { |
<> | 144:ef7eb2e8f9f7 | 204 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 205 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 206 | |
<> | 144:ef7eb2e8f9f7 | 207 | if (hz < 250000) { |
<> | 144:ef7eb2e8f9f7 | 208 | twi_info->config.frequency = NRF_TWI_FREQ_100K; |
<> | 144:ef7eb2e8f9f7 | 209 | } else if (hz < 400000) { |
<> | 144:ef7eb2e8f9f7 | 210 | twi_info->config.frequency = NRF_TWI_FREQ_250K; |
<> | 144:ef7eb2e8f9f7 | 211 | } else { |
<> | 144:ef7eb2e8f9f7 | 212 | twi_info->config.frequency = NRF_TWI_FREQ_400K; |
<> | 144:ef7eb2e8f9f7 | 213 | } |
<> | 144:ef7eb2e8f9f7 | 214 | nrf_twi_frequency_set(twi->reg.p_twi, twi_info->config.frequency); |
<> | 144:ef7eb2e8f9f7 | 215 | } |
<> | 144:ef7eb2e8f9f7 | 216 | |
<> | 144:ef7eb2e8f9f7 | 217 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) |
<> | 144:ef7eb2e8f9f7 | 218 | { |
<> | 144:ef7eb2e8f9f7 | 219 | (void)stop; |
<> | 144:ef7eb2e8f9f7 | 220 | |
<> | 144:ef7eb2e8f9f7 | 221 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 222 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 223 | |
<> | 144:ef7eb2e8f9f7 | 224 | twi_info->transfer_finished = false; |
<> | 144:ef7eb2e8f9f7 | 225 | ret_code_t ret_code = nrf_drv_twi_rx(twi, twi_address(address), |
<> | 144:ef7eb2e8f9f7 | 226 | (uint8_t *)data, length); |
<> | 144:ef7eb2e8f9f7 | 227 | if (ret_code != NRF_SUCCESS) { |
<> | 144:ef7eb2e8f9f7 | 228 | return 0; |
<> | 144:ef7eb2e8f9f7 | 229 | } |
<> | 144:ef7eb2e8f9f7 | 230 | while (!twi_info->transfer_finished) {} |
<> | 144:ef7eb2e8f9f7 | 231 | return nrf_drv_twi_data_count_get(twi); |
<> | 144:ef7eb2e8f9f7 | 232 | } |
<> | 144:ef7eb2e8f9f7 | 233 | |
<> | 144:ef7eb2e8f9f7 | 234 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) |
<> | 144:ef7eb2e8f9f7 | 235 | { |
<> | 144:ef7eb2e8f9f7 | 236 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 237 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 238 | |
<> | 144:ef7eb2e8f9f7 | 239 | twi_info->transfer_finished = false; |
<> | 144:ef7eb2e8f9f7 | 240 | ret_code_t ret_code = nrf_drv_twi_tx(twi, twi_address(address), |
<> | 144:ef7eb2e8f9f7 | 241 | (uint8_t const *)data, length, (stop == 0)); |
<> | 144:ef7eb2e8f9f7 | 242 | if (ret_code != NRF_SUCCESS) { |
<> | 144:ef7eb2e8f9f7 | 243 | return 0; |
<> | 144:ef7eb2e8f9f7 | 244 | } |
<> | 144:ef7eb2e8f9f7 | 245 | while (!twi_info->transfer_finished) {} |
<> | 144:ef7eb2e8f9f7 | 246 | return nrf_drv_twi_data_count_get(twi); |
<> | 144:ef7eb2e8f9f7 | 247 | } |
<> | 144:ef7eb2e8f9f7 | 248 | |
<> | 144:ef7eb2e8f9f7 | 249 | int i2c_byte_read(i2c_t *obj, int last) |
<> | 144:ef7eb2e8f9f7 | 250 | { |
<> | 144:ef7eb2e8f9f7 | 251 | (void)obj; |
<> | 144:ef7eb2e8f9f7 | 252 | (void)last; |
<> | 144:ef7eb2e8f9f7 | 253 | |
<> | 144:ef7eb2e8f9f7 | 254 | return -1; // Not implemented. |
<> | 144:ef7eb2e8f9f7 | 255 | } |
<> | 144:ef7eb2e8f9f7 | 256 | |
<> | 144:ef7eb2e8f9f7 | 257 | int i2c_byte_write(i2c_t *obj, int data) |
<> | 144:ef7eb2e8f9f7 | 258 | { |
<> | 144:ef7eb2e8f9f7 | 259 | (void)obj; |
<> | 144:ef7eb2e8f9f7 | 260 | (void)data; |
<> | 144:ef7eb2e8f9f7 | 261 | |
<> | 144:ef7eb2e8f9f7 | 262 | return -1; // Not implemented. |
<> | 144:ef7eb2e8f9f7 | 263 | } |
<> | 144:ef7eb2e8f9f7 | 264 | |
<> | 144:ef7eb2e8f9f7 | 265 | |
<> | 144:ef7eb2e8f9f7 | 266 | #if DEVICE_I2C_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 267 | |
<> | 144:ef7eb2e8f9f7 | 268 | void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, |
<> | 144:ef7eb2e8f9f7 | 269 | void *rx, size_t rx_length, uint32_t address, |
<> | 144:ef7eb2e8f9f7 | 270 | uint32_t stop, uint32_t handler, |
<> | 144:ef7eb2e8f9f7 | 271 | uint32_t event, DMAUsage hint) |
<> | 144:ef7eb2e8f9f7 | 272 | { |
<> | 144:ef7eb2e8f9f7 | 273 | (void)stop; |
<> | 144:ef7eb2e8f9f7 | 274 | (void)hint; |
<> | 144:ef7eb2e8f9f7 | 275 | |
<> | 144:ef7eb2e8f9f7 | 276 | if (i2c_active(obj)) { |
<> | 144:ef7eb2e8f9f7 | 277 | return; |
<> | 144:ef7eb2e8f9f7 | 278 | } |
<> | 144:ef7eb2e8f9f7 | 279 | if ((tx_length == 0) && (rx_length == 0)) { |
<> | 144:ef7eb2e8f9f7 | 280 | return; |
<> | 144:ef7eb2e8f9f7 | 281 | } |
<> | 144:ef7eb2e8f9f7 | 282 | |
<> | 144:ef7eb2e8f9f7 | 283 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 284 | twi_info->events = 0; |
<> | 144:ef7eb2e8f9f7 | 285 | twi_info->handler = (void (*)(void))handler; |
<> | 144:ef7eb2e8f9f7 | 286 | twi_info->event_mask = event; |
<> | 144:ef7eb2e8f9f7 | 287 | |
<> | 144:ef7eb2e8f9f7 | 288 | uint8_t twi_addr = twi_address(address); |
<> | 144:ef7eb2e8f9f7 | 289 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 290 | |
<> | 144:ef7eb2e8f9f7 | 291 | if ((tx_length > 0) && (rx_length == 0)) { |
<> | 144:ef7eb2e8f9f7 | 292 | nrf_drv_twi_xfer_desc_t const xfer = |
<> | 144:ef7eb2e8f9f7 | 293 | NRF_DRV_TWI_XFER_DESC_TX(twi_addr, (uint8_t *)tx, tx_length); |
<> | 144:ef7eb2e8f9f7 | 294 | nrf_drv_twi_xfer(twi, &xfer, |
<> | 144:ef7eb2e8f9f7 | 295 | stop ? 0 : NRF_DRV_TWI_FLAG_TX_NO_STOP); |
<> | 144:ef7eb2e8f9f7 | 296 | } |
<> | 144:ef7eb2e8f9f7 | 297 | else if ((tx_length == 0) && (rx_length > 0)) { |
<> | 144:ef7eb2e8f9f7 | 298 | nrf_drv_twi_xfer_desc_t const xfer = |
<> | 144:ef7eb2e8f9f7 | 299 | NRF_DRV_TWI_XFER_DESC_RX(twi_addr, rx, rx_length); |
<> | 144:ef7eb2e8f9f7 | 300 | nrf_drv_twi_xfer(twi, &xfer, 0); |
<> | 144:ef7eb2e8f9f7 | 301 | } |
<> | 144:ef7eb2e8f9f7 | 302 | else if ((tx_length > 0) && (rx_length > 0)) { |
<> | 144:ef7eb2e8f9f7 | 303 | nrf_drv_twi_xfer_desc_t const xfer = |
<> | 144:ef7eb2e8f9f7 | 304 | NRF_DRV_TWI_XFER_DESC_TXRX(twi_addr, |
<> | 144:ef7eb2e8f9f7 | 305 | (uint8_t *)tx, tx_length, rx, rx_length); |
<> | 144:ef7eb2e8f9f7 | 306 | nrf_drv_twi_xfer(twi, &xfer, 0); |
<> | 144:ef7eb2e8f9f7 | 307 | } |
<> | 144:ef7eb2e8f9f7 | 308 | } |
<> | 144:ef7eb2e8f9f7 | 309 | |
<> | 144:ef7eb2e8f9f7 | 310 | uint32_t i2c_irq_handler_asynch(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 311 | { |
<> | 144:ef7eb2e8f9f7 | 312 | twi_info_t *twi_info = TWI_INFO(obj); |
<> | 144:ef7eb2e8f9f7 | 313 | return (twi_info->events & twi_info->event_mask); |
<> | 144:ef7eb2e8f9f7 | 314 | } |
<> | 144:ef7eb2e8f9f7 | 315 | |
<> | 144:ef7eb2e8f9f7 | 316 | uint8_t i2c_active(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 317 | { |
<> | 144:ef7eb2e8f9f7 | 318 | nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; |
<> | 144:ef7eb2e8f9f7 | 319 | return nrf_drv_twi_is_busy(twi); |
<> | 144:ef7eb2e8f9f7 | 320 | } |
<> | 144:ef7eb2e8f9f7 | 321 | |
<> | 144:ef7eb2e8f9f7 | 322 | void i2c_abort_asynch(i2c_t *obj) |
<> | 144:ef7eb2e8f9f7 | 323 | { |
<> | 144:ef7eb2e8f9f7 | 324 | i2c_reset(obj); |
<> | 144:ef7eb2e8f9f7 | 325 | } |
<> | 144:ef7eb2e8f9f7 | 326 | |
<> | 144:ef7eb2e8f9f7 | 327 | #endif // DEVICE_I2C_ASYNCH |
<> | 144:ef7eb2e8f9f7 | 328 | |
<> | 144:ef7eb2e8f9f7 | 329 | #endif // DEVICE_I2C |