mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*
elessair 0:f269e3021894 2 * Copyright (c) 2013 Nordic Semiconductor ASA
elessair 0:f269e3021894 3 * All rights reserved.
elessair 0:f269e3021894 4 *
elessair 0:f269e3021894 5 * Redistribution and use in source and binary forms, with or without modification,
elessair 0:f269e3021894 6 * are permitted provided that the following conditions are met:
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * 1. Redistributions of source code must retain the above copyright notice, this list
elessair 0:f269e3021894 9 * of conditions and the following disclaimer.
elessair 0:f269e3021894 10 *
elessair 0:f269e3021894 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
elessair 0:f269e3021894 12 * integrated circuit in a product or a software update for such product, must reproduce
elessair 0:f269e3021894 13 * the above copyright notice, this list of conditions and the following disclaimer in
elessair 0:f269e3021894 14 * the documentation and/or other materials provided with the distribution.
elessair 0:f269e3021894 15 *
elessair 0:f269e3021894 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
elessair 0:f269e3021894 17 * used to endorse or promote products derived from this software without specific prior
elessair 0:f269e3021894 18 * written permission.
elessair 0:f269e3021894 19 *
elessair 0:f269e3021894 20 * 4. This software, with or without modification, must only be used with a
elessair 0:f269e3021894 21 * Nordic Semiconductor ASA integrated circuit.
elessair 0:f269e3021894 22 *
elessair 0:f269e3021894 23 * 5. Any software provided in binary or object form under this license must not be reverse
elessair 0:f269e3021894 24 * engineered, decompiled, modified and/or disassembled.
elessair 0:f269e3021894 25 *
elessair 0:f269e3021894 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
elessair 0:f269e3021894 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
elessair 0:f269e3021894 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
elessair 0:f269e3021894 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
elessair 0:f269e3021894 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
elessair 0:f269e3021894 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
elessair 0:f269e3021894 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
elessair 0:f269e3021894 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
elessair 0:f269e3021894 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
elessair 0:f269e3021894 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
elessair 0:f269e3021894 36 *
elessair 0:f269e3021894 37 */
elessair 0:f269e3021894 38
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 #include "i2c_api.h"
elessair 0:f269e3021894 41
elessair 0:f269e3021894 42 #if DEVICE_I2C
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 #include "mbed_assert.h"
elessair 0:f269e3021894 45 #include "mbed_error.h"
elessair 0:f269e3021894 46 #include "nrf_drv_twi.h"
elessair 0:f269e3021894 47 #include "app_util_platform.h"
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 #if DEVICE_I2C_ASYNCH
elessair 0:f269e3021894 50 #define TWI_IDX(obj) ((obj)->i2c.twi_idx)
elessair 0:f269e3021894 51 #else
elessair 0:f269e3021894 52 #define TWI_IDX(obj) ((obj)->twi_idx)
elessair 0:f269e3021894 53 #endif
elessair 0:f269e3021894 54 #define TWI_INFO(obj) (&m_twi_info[TWI_IDX(obj)])
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 typedef struct {
elessair 0:f269e3021894 57 bool initialized;
elessair 0:f269e3021894 58 nrf_drv_twi_config_t config;
elessair 0:f269e3021894 59 volatile bool transfer_finished;
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 #if DEVICE_I2C_ASYNCH
elessair 0:f269e3021894 62 volatile uint32_t events;
elessair 0:f269e3021894 63 void (*handler)(void);
elessair 0:f269e3021894 64 uint32_t event_mask;
elessair 0:f269e3021894 65 #endif
elessair 0:f269e3021894 66 } twi_info_t;
elessair 0:f269e3021894 67 static twi_info_t m_twi_info[TWI_COUNT];
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 static nrf_drv_twi_t const m_twi_instances[TWI_COUNT] = {
elessair 0:f269e3021894 70 #if TWI0_ENABLED
elessair 0:f269e3021894 71 NRF_DRV_TWI_INSTANCE(0),
elessair 0:f269e3021894 72 #endif
elessair 0:f269e3021894 73 #if TWI1_ENABLED
elessair 0:f269e3021894 74 NRF_DRV_TWI_INSTANCE(1),
elessair 0:f269e3021894 75 #endif
elessair 0:f269e3021894 76 };
elessair 0:f269e3021894 77
elessair 0:f269e3021894 78 static void twi_event_handler(nrf_drv_twi_evt_t const *event, void *context)
elessair 0:f269e3021894 79 {
elessair 0:f269e3021894 80 twi_info_t * twi_info = TWI_INFO((i2c_t *)context);
elessair 0:f269e3021894 81 twi_info->transfer_finished = true;
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83 #if DEVICE_I2C_ASYNCH
elessair 0:f269e3021894 84 switch (event->type) {
elessair 0:f269e3021894 85 case NRF_DRV_TWI_EVT_DONE:
elessair 0:f269e3021894 86 twi_info->events |= I2C_EVENT_TRANSFER_COMPLETE;
elessair 0:f269e3021894 87 break;
elessair 0:f269e3021894 88
elessair 0:f269e3021894 89 case NRF_DRV_TWI_EVT_ADDRESS_NACK:
elessair 0:f269e3021894 90 twi_info->events |= I2C_EVENT_ERROR_NO_SLAVE;
elessair 0:f269e3021894 91 break;
elessair 0:f269e3021894 92
elessair 0:f269e3021894 93 case NRF_DRV_TWI_EVT_DATA_NACK:
elessair 0:f269e3021894 94 twi_info->events |= I2C_EVENT_ERROR;
elessair 0:f269e3021894 95 break;
elessair 0:f269e3021894 96 }
elessair 0:f269e3021894 97
elessair 0:f269e3021894 98 if (twi_info->handler) {
elessair 0:f269e3021894 99 twi_info->handler();
elessair 0:f269e3021894 100 }
elessair 0:f269e3021894 101 #endif // DEVICE_I2C_ASYNCH
elessair 0:f269e3021894 102 }
elessair 0:f269e3021894 103
elessair 0:f269e3021894 104 static uint8_t twi_address(int i2c_address)
elessair 0:f269e3021894 105 {
elessair 0:f269e3021894 106 // The TWI driver requires 7-bit slave address (without R/W bit).
elessair 0:f269e3021894 107 return (i2c_address >> 1);
elessair 0:f269e3021894 108 }
elessair 0:f269e3021894 109
elessair 0:f269e3021894 110 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
elessair 0:f269e3021894 111 {
elessair 0:f269e3021894 112 int i;
elessair 0:f269e3021894 113 for (i = 0; i < TWI_COUNT; ++i) {
elessair 0:f269e3021894 114 if (m_twi_info[i].initialized &&
elessair 0:f269e3021894 115 m_twi_info[i].config.sda == (uint32_t)sda &&
elessair 0:f269e3021894 116 m_twi_info[i].config.scl == (uint32_t)scl) {
elessair 0:f269e3021894 117 TWI_IDX(obj) = i;
elessair 0:f269e3021894 118 TWI_INFO(obj)->config.frequency = NRF_TWI_FREQ_100K;
elessair 0:f269e3021894 119 i2c_reset(obj);
elessair 0:f269e3021894 120 return;
elessair 0:f269e3021894 121 }
elessair 0:f269e3021894 122 }
elessair 0:f269e3021894 123
elessair 0:f269e3021894 124 nrf_drv_twi_config_t const config = {
elessair 0:f269e3021894 125 .scl = scl,
elessair 0:f269e3021894 126 .sda = sda,
elessair 0:f269e3021894 127 .frequency = NRF_TWI_FREQ_100K,
elessair 0:f269e3021894 128 .interrupt_priority = APP_IRQ_PRIORITY_LOW,
elessair 0:f269e3021894 129 };
elessair 0:f269e3021894 130
elessair 0:f269e3021894 131 for (i = 0; i < TWI_COUNT; ++i) {
elessair 0:f269e3021894 132 if (!m_twi_info[i].initialized) {
elessair 0:f269e3021894 133 nrf_drv_twi_t const *twi = &m_twi_instances[i];
elessair 0:f269e3021894 134 ret_code_t ret_code =
elessair 0:f269e3021894 135 nrf_drv_twi_init(twi, &config, twi_event_handler, obj);
elessair 0:f269e3021894 136 if (ret_code == NRF_SUCCESS) {
elessair 0:f269e3021894 137 TWI_IDX(obj) = i;
elessair 0:f269e3021894 138 TWI_INFO(obj)->initialized = true;
elessair 0:f269e3021894 139 TWI_INFO(obj)->config = config;
elessair 0:f269e3021894 140
elessair 0:f269e3021894 141 nrf_drv_twi_enable(twi);
elessair 0:f269e3021894 142 return;
elessair 0:f269e3021894 143 }
elessair 0:f269e3021894 144 }
elessair 0:f269e3021894 145 }
elessair 0:f269e3021894 146
elessair 0:f269e3021894 147 // No available peripheral
elessair 0:f269e3021894 148 error("No available I2C peripheral\r\n");
elessair 0:f269e3021894 149 }
elessair 0:f269e3021894 150
elessair 0:f269e3021894 151 void i2c_reset(i2c_t *obj)
elessair 0:f269e3021894 152 {
elessair 0:f269e3021894 153 twi_info_t *twi_info = TWI_INFO(obj);
elessair 0:f269e3021894 154 nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
elessair 0:f269e3021894 155
elessair 0:f269e3021894 156 nrf_drv_twi_uninit(twi);
elessair 0:f269e3021894 157 nrf_drv_twi_init(twi, &twi_info->config, twi_event_handler, obj);
elessair 0:f269e3021894 158 nrf_drv_twi_enable(twi);
elessair 0:f269e3021894 159 }
elessair 0:f269e3021894 160
elessair 0:f269e3021894 161 int i2c_start(i2c_t *obj)
elessair 0:f269e3021894 162 {
elessair 0:f269e3021894 163 (void)obj;
elessair 0:f269e3021894 164
elessair 0:f269e3021894 165 return -1; // Not implemented.
elessair 0:f269e3021894 166 }
elessair 0:f269e3021894 167
elessair 0:f269e3021894 168 int i2c_stop(i2c_t *obj)
elessair 0:f269e3021894 169 {
elessair 0:f269e3021894 170 (void)obj;
elessair 0:f269e3021894 171
elessair 0:f269e3021894 172 return -1; // Not implemented.
elessair 0:f269e3021894 173 }
elessair 0:f269e3021894 174
elessair 0:f269e3021894 175 void i2c_frequency(i2c_t *obj, int hz)
elessair 0:f269e3021894 176 {
elessair 0:f269e3021894 177 twi_info_t *twi_info = TWI_INFO(obj);
elessair 0:f269e3021894 178 nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
elessair 0:f269e3021894 179
elessair 0:f269e3021894 180 if (hz < 250000) {
elessair 0:f269e3021894 181 twi_info->config.frequency = NRF_TWI_FREQ_100K;
elessair 0:f269e3021894 182 } else if (hz < 400000) {
elessair 0:f269e3021894 183 twi_info->config.frequency = NRF_TWI_FREQ_250K;
elessair 0:f269e3021894 184 } else {
elessair 0:f269e3021894 185 twi_info->config.frequency = NRF_TWI_FREQ_400K;
elessair 0:f269e3021894 186 }
elessair 0:f269e3021894 187 nrf_twi_frequency_set(twi->reg.p_twi, twi_info->config.frequency);
elessair 0:f269e3021894 188 }
elessair 0:f269e3021894 189
elessair 0:f269e3021894 190 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
elessair 0:f269e3021894 191 {
elessair 0:f269e3021894 192 (void)stop;
elessair 0:f269e3021894 193
elessair 0:f269e3021894 194 twi_info_t *twi_info = TWI_INFO(obj);
elessair 0:f269e3021894 195 nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
elessair 0:f269e3021894 196
elessair 0:f269e3021894 197 twi_info->transfer_finished = false;
elessair 0:f269e3021894 198 ret_code_t ret_code = nrf_drv_twi_rx(twi, twi_address(address),
elessair 0:f269e3021894 199 (uint8_t *)data, length);
elessair 0:f269e3021894 200 if (ret_code != NRF_SUCCESS) {
elessair 0:f269e3021894 201 return 0;
elessair 0:f269e3021894 202 }
elessair 0:f269e3021894 203 while (!twi_info->transfer_finished) {}
elessair 0:f269e3021894 204 return nrf_drv_twi_data_count_get(twi);
elessair 0:f269e3021894 205 }
elessair 0:f269e3021894 206
elessair 0:f269e3021894 207 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
elessair 0:f269e3021894 208 {
elessair 0:f269e3021894 209 twi_info_t *twi_info = TWI_INFO(obj);
elessair 0:f269e3021894 210 nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
elessair 0:f269e3021894 211
elessair 0:f269e3021894 212 twi_info->transfer_finished = false;
elessair 0:f269e3021894 213 ret_code_t ret_code = nrf_drv_twi_tx(twi, twi_address(address),
elessair 0:f269e3021894 214 (uint8_t const *)data, length, (stop == 0));
elessair 0:f269e3021894 215 if (ret_code != NRF_SUCCESS) {
elessair 0:f269e3021894 216 return 0;
elessair 0:f269e3021894 217 }
elessair 0:f269e3021894 218 while (!twi_info->transfer_finished) {}
elessair 0:f269e3021894 219 return nrf_drv_twi_data_count_get(twi);
elessair 0:f269e3021894 220 }
elessair 0:f269e3021894 221
elessair 0:f269e3021894 222 int i2c_byte_read(i2c_t *obj, int last)
elessair 0:f269e3021894 223 {
elessair 0:f269e3021894 224 (void)obj;
elessair 0:f269e3021894 225 (void)last;
elessair 0:f269e3021894 226
elessair 0:f269e3021894 227 return -1; // Not implemented.
elessair 0:f269e3021894 228 }
elessair 0:f269e3021894 229
elessair 0:f269e3021894 230 int i2c_byte_write(i2c_t *obj, int data)
elessair 0:f269e3021894 231 {
elessair 0:f269e3021894 232 (void)obj;
elessair 0:f269e3021894 233 (void)data;
elessair 0:f269e3021894 234
elessair 0:f269e3021894 235 return -1; // Not implemented.
elessair 0:f269e3021894 236 }
elessair 0:f269e3021894 237
elessair 0:f269e3021894 238
elessair 0:f269e3021894 239 #if DEVICE_I2C_ASYNCH
elessair 0:f269e3021894 240
elessair 0:f269e3021894 241 void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length,
elessair 0:f269e3021894 242 void *rx, size_t rx_length, uint32_t address,
elessair 0:f269e3021894 243 uint32_t stop, uint32_t handler,
elessair 0:f269e3021894 244 uint32_t event, DMAUsage hint)
elessair 0:f269e3021894 245 {
elessair 0:f269e3021894 246 (void)stop;
elessair 0:f269e3021894 247 (void)hint;
elessair 0:f269e3021894 248
elessair 0:f269e3021894 249 if (i2c_active(obj)) {
elessair 0:f269e3021894 250 return;
elessair 0:f269e3021894 251 }
elessair 0:f269e3021894 252 if ((tx_length == 0) && (rx_length == 0)) {
elessair 0:f269e3021894 253 return;
elessair 0:f269e3021894 254 }
elessair 0:f269e3021894 255
elessair 0:f269e3021894 256 twi_info_t *twi_info = TWI_INFO(obj);
elessair 0:f269e3021894 257 twi_info->events = 0;
elessair 0:f269e3021894 258 twi_info->handler = (void (*)(void))handler;
elessair 0:f269e3021894 259 twi_info->event_mask = event;
elessair 0:f269e3021894 260
elessair 0:f269e3021894 261 uint8_t twi_addr = twi_address(address);
elessair 0:f269e3021894 262 nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
elessair 0:f269e3021894 263
elessair 0:f269e3021894 264 if ((tx_length > 0) && (rx_length == 0)) {
elessair 0:f269e3021894 265 nrf_drv_twi_xfer_desc_t const xfer =
elessair 0:f269e3021894 266 NRF_DRV_TWI_XFER_DESC_TX(twi_addr, (uint8_t *)tx, tx_length);
elessair 0:f269e3021894 267 nrf_drv_twi_xfer(twi, &xfer,
elessair 0:f269e3021894 268 stop ? 0 : NRF_DRV_TWI_FLAG_TX_NO_STOP);
elessair 0:f269e3021894 269 }
elessair 0:f269e3021894 270 else if ((tx_length == 0) && (rx_length > 0)) {
elessair 0:f269e3021894 271 nrf_drv_twi_xfer_desc_t const xfer =
elessair 0:f269e3021894 272 NRF_DRV_TWI_XFER_DESC_RX(twi_addr, rx, rx_length);
elessair 0:f269e3021894 273 nrf_drv_twi_xfer(twi, &xfer, 0);
elessair 0:f269e3021894 274 }
elessair 0:f269e3021894 275 else if ((tx_length > 0) && (rx_length > 0)) {
elessair 0:f269e3021894 276 nrf_drv_twi_xfer_desc_t const xfer =
elessair 0:f269e3021894 277 NRF_DRV_TWI_XFER_DESC_TXRX(twi_addr,
elessair 0:f269e3021894 278 (uint8_t *)tx, tx_length, rx, rx_length);
elessair 0:f269e3021894 279 nrf_drv_twi_xfer(twi, &xfer, 0);
elessair 0:f269e3021894 280 }
elessair 0:f269e3021894 281 }
elessair 0:f269e3021894 282
elessair 0:f269e3021894 283 uint32_t i2c_irq_handler_asynch(i2c_t *obj)
elessair 0:f269e3021894 284 {
elessair 0:f269e3021894 285 twi_info_t *twi_info = TWI_INFO(obj);
elessair 0:f269e3021894 286 return (twi_info->events & twi_info->event_mask);
elessair 0:f269e3021894 287 }
elessair 0:f269e3021894 288
elessair 0:f269e3021894 289 uint8_t i2c_active(i2c_t *obj)
elessair 0:f269e3021894 290 {
elessair 0:f269e3021894 291 nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)];
elessair 0:f269e3021894 292 return nrf_drv_twi_is_busy(twi);
elessair 0:f269e3021894 293 }
elessair 0:f269e3021894 294
elessair 0:f269e3021894 295 void i2c_abort_asynch(i2c_t *obj)
elessair 0:f269e3021894 296 {
elessair 0:f269e3021894 297 i2c_reset(obj);
elessair 0:f269e3021894 298 }
elessair 0:f269e3021894 299
elessair 0:f269e3021894 300 #endif // DEVICE_I2C_ASYNCH
elessair 0:f269e3021894 301
elessair 0:f269e3021894 302 #endif // DEVICE_I2C