mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed May 07 11:45:52 2014 +0100
Revision:
180:097147d37470
Parent:
178:a873352662d8
Child:
182:242346c42295
Synchronized with git revision 37b8ad8063bc41a763db96f6e13be9a36ec9e4e7

Full URL: https://github.com/mbedmicro/mbed/commit/37b8ad8063bc41a763db96f6e13be9a36ec9e4e7/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 146:f64d43ff0c18 1 /* mbed Microcontroller Library
mbed_official 146:f64d43ff0c18 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 146:f64d43ff0c18 3 *
mbed_official 146:f64d43ff0c18 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 146:f64d43ff0c18 5 * you may not use this file except in compliance with the License.
mbed_official 146:f64d43ff0c18 6 * You may obtain a copy of the License at
mbed_official 146:f64d43ff0c18 7 *
mbed_official 146:f64d43ff0c18 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 146:f64d43ff0c18 9 *
mbed_official 146:f64d43ff0c18 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 146:f64d43ff0c18 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 146:f64d43ff0c18 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 146:f64d43ff0c18 13 * See the License for the specific language governing permissions and
mbed_official 146:f64d43ff0c18 14 * limitations under the License.
mbed_official 146:f64d43ff0c18 15 */
mbed_official 146:f64d43ff0c18 16 #include "i2c_api.h"
mbed_official 146:f64d43ff0c18 17
mbed_official 146:f64d43ff0c18 18 #include "cmsis.h"
mbed_official 146:f64d43ff0c18 19 #include "pinmap.h"
mbed_official 146:f64d43ff0c18 20 #include "error.h"
mbed_official 146:f64d43ff0c18 21 #include "fsl_clock_manager.h"
mbed_official 146:f64d43ff0c18 22 #include "fsl_i2c_hal.h"
mbed_official 180:097147d37470 23 #include "fsl_port_hal.h"
mbed_official 180:097147d37470 24 #include "fsl_sim_hal.h"
mbed_official 146:f64d43ff0c18 25
mbed_official 146:f64d43ff0c18 26 static const PinMap PinMap_I2C_SDA[] = {
mbed_official 146:f64d43ff0c18 27 {PTE25, I2C_0, 5},
mbed_official 146:f64d43ff0c18 28 {PTB1 , I2C_0, 2},
mbed_official 146:f64d43ff0c18 29 {PTB3 , I2C_0, 2},
mbed_official 146:f64d43ff0c18 30 {PTC11, I2C_1, 2},
mbed_official 146:f64d43ff0c18 31 {PTA13, I2C_2, 5},
mbed_official 146:f64d43ff0c18 32 {PTD3 , I2C_0, 7},
mbed_official 146:f64d43ff0c18 33 {PTE0 , I2C_1, 6},
mbed_official 146:f64d43ff0c18 34 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 35 };
mbed_official 146:f64d43ff0c18 36
mbed_official 146:f64d43ff0c18 37 static const PinMap PinMap_I2C_SCL[] = {
mbed_official 146:f64d43ff0c18 38 {PTE24, I2C_0, 5},
mbed_official 146:f64d43ff0c18 39 {PTB0 , I2C_0, 2},
mbed_official 146:f64d43ff0c18 40 {PTB2 , I2C_0, 2},
mbed_official 146:f64d43ff0c18 41 {PTC10, I2C_1, 2},
mbed_official 146:f64d43ff0c18 42 {PTA12, I2C_2, 5},
mbed_official 146:f64d43ff0c18 43 {PTA14, I2C_2, 5},
mbed_official 146:f64d43ff0c18 44 {PTD2 , I2C_0, 7},
mbed_official 146:f64d43ff0c18 45 {PTE1 , I2C_1, 6},
mbed_official 146:f64d43ff0c18 46 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 47 };
mbed_official 146:f64d43ff0c18 48
mbed_official 146:f64d43ff0c18 49 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
mbed_official 146:f64d43ff0c18 50 uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA);
mbed_official 146:f64d43ff0c18 51 uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);
mbed_official 146:f64d43ff0c18 52 obj->instance = pinmap_merge(i2c_sda, i2c_scl);
mbed_official 146:f64d43ff0c18 53 if ((int)obj->instance == NC) {
mbed_official 146:f64d43ff0c18 54 error("I2C pin mapping failed");
mbed_official 146:f64d43ff0c18 55 }
mbed_official 146:f64d43ff0c18 56
mbed_official 146:f64d43ff0c18 57 clock_manager_set_gate(kClockModuleI2C, obj->instance, true);
mbed_official 180:097147d37470 58 clock_manager_set_gate(kClockModulePORT, sda >> GPIO_PORT_SHIFT, true);
mbed_official 180:097147d37470 59 clock_manager_set_gate(kClockModulePORT, scl >> GPIO_PORT_SHIFT, true);
mbed_official 146:f64d43ff0c18 60 i2c_hal_enable(obj->instance);
mbed_official 146:f64d43ff0c18 61 i2c_frequency(obj, 100000);
mbed_official 146:f64d43ff0c18 62
mbed_official 146:f64d43ff0c18 63 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 146:f64d43ff0c18 64 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 180:097147d37470 65 port_hal_configure_open_drain(sda >> GPIO_PORT_SHIFT, sda & 0xFF, true);
mbed_official 180:097147d37470 66 port_hal_configure_open_drain(scl >> GPIO_PORT_SHIFT, scl & 0xFF, true);
mbed_official 146:f64d43ff0c18 67 }
mbed_official 146:f64d43ff0c18 68
mbed_official 146:f64d43ff0c18 69 int i2c_start(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 70 i2c_hal_send_start(obj->instance);
mbed_official 146:f64d43ff0c18 71 return 0;
mbed_official 146:f64d43ff0c18 72 }
mbed_official 146:f64d43ff0c18 73
mbed_official 146:f64d43ff0c18 74 int i2c_stop(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 75 volatile uint32_t n = 0;
mbed_official 146:f64d43ff0c18 76 i2c_hal_send_stop(obj->instance);
mbed_official 146:f64d43ff0c18 77
mbed_official 146:f64d43ff0c18 78 // It seems that there are timing problems
mbed_official 146:f64d43ff0c18 79 // when there is no waiting time after a STOP.
mbed_official 146:f64d43ff0c18 80 // This wait is also included on the samples
mbed_official 146:f64d43ff0c18 81 // code provided with the freedom board
mbed_official 146:f64d43ff0c18 82 for (n = 0; n < 100; n++) __NOP();
mbed_official 146:f64d43ff0c18 83 return 0;
mbed_official 146:f64d43ff0c18 84 }
mbed_official 146:f64d43ff0c18 85
mbed_official 146:f64d43ff0c18 86 static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
mbed_official 146:f64d43ff0c18 87 uint32_t i, timeout = 1000;
mbed_official 146:f64d43ff0c18 88
mbed_official 146:f64d43ff0c18 89 for (i = 0; i < timeout; i++) {
mbed_official 146:f64d43ff0c18 90 if (HW_I2C_S_RD(obj->instance) & mask)
mbed_official 146:f64d43ff0c18 91 return 0;
mbed_official 146:f64d43ff0c18 92 }
mbed_official 146:f64d43ff0c18 93 return 1;
mbed_official 146:f64d43ff0c18 94 }
mbed_official 146:f64d43ff0c18 95
mbed_official 146:f64d43ff0c18 96 // this function waits the end of a tx transfer and return the status of the transaction:
mbed_official 146:f64d43ff0c18 97 // 0: OK ack received
mbed_official 146:f64d43ff0c18 98 // 1: OK ack not received
mbed_official 146:f64d43ff0c18 99 // 2: failure
mbed_official 146:f64d43ff0c18 100 static int i2c_wait_end_tx_transfer(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 101 // wait for the interrupt flag
mbed_official 146:f64d43ff0c18 102 if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) {
mbed_official 146:f64d43ff0c18 103 return 2;
mbed_official 146:f64d43ff0c18 104 }
mbed_official 146:f64d43ff0c18 105
mbed_official 146:f64d43ff0c18 106 i2c_hal_clear_interrupt(obj->instance);
mbed_official 146:f64d43ff0c18 107
mbed_official 146:f64d43ff0c18 108 // wait transfer complete
mbed_official 146:f64d43ff0c18 109 if (timeout_status_poll(obj, I2C_S_TCF_MASK)) {
mbed_official 146:f64d43ff0c18 110 return 2;
mbed_official 146:f64d43ff0c18 111 }
mbed_official 146:f64d43ff0c18 112
mbed_official 146:f64d43ff0c18 113 // check if we received the ACK or not
mbed_official 146:f64d43ff0c18 114 return i2c_hal_get_receive_ack(obj->instance) ? 0 : 1;
mbed_official 146:f64d43ff0c18 115 }
mbed_official 146:f64d43ff0c18 116
mbed_official 146:f64d43ff0c18 117 // this function waits the end of a rx transfer and return the status of the transaction:
mbed_official 146:f64d43ff0c18 118 // 0: OK
mbed_official 146:f64d43ff0c18 119 // 1: failure
mbed_official 146:f64d43ff0c18 120 static int i2c_wait_end_rx_transfer(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 121 // wait for the end of the rx transfer
mbed_official 146:f64d43ff0c18 122 if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) {
mbed_official 146:f64d43ff0c18 123 return 1;
mbed_official 146:f64d43ff0c18 124 }
mbed_official 146:f64d43ff0c18 125
mbed_official 146:f64d43ff0c18 126 i2c_hal_clear_interrupt(obj->instance);
mbed_official 146:f64d43ff0c18 127
mbed_official 146:f64d43ff0c18 128 return 0;
mbed_official 146:f64d43ff0c18 129 }
mbed_official 146:f64d43ff0c18 130
mbed_official 146:f64d43ff0c18 131 static int i2c_do_write(i2c_t *obj, int value) {
mbed_official 146:f64d43ff0c18 132 i2c_hal_write(obj->instance, value);
mbed_official 146:f64d43ff0c18 133
mbed_official 146:f64d43ff0c18 134 // init and wait the end of the transfer
mbed_official 146:f64d43ff0c18 135 return i2c_wait_end_tx_transfer(obj);
mbed_official 146:f64d43ff0c18 136 }
mbed_official 146:f64d43ff0c18 137
mbed_official 146:f64d43ff0c18 138 static int i2c_do_read(i2c_t *obj, char * data, int last) {
mbed_official 146:f64d43ff0c18 139 if (last) {
mbed_official 146:f64d43ff0c18 140 i2c_hal_send_nak(obj->instance);
mbed_official 146:f64d43ff0c18 141 } else {
mbed_official 146:f64d43ff0c18 142 i2c_hal_send_ack(obj->instance);
mbed_official 146:f64d43ff0c18 143 }
mbed_official 146:f64d43ff0c18 144
mbed_official 146:f64d43ff0c18 145 *data = (i2c_hal_read(obj->instance) & 0xFF);
mbed_official 146:f64d43ff0c18 146
mbed_official 146:f64d43ff0c18 147 // start rx transfer and wait the end of the transfer
mbed_official 146:f64d43ff0c18 148 return i2c_wait_end_rx_transfer(obj);
mbed_official 146:f64d43ff0c18 149 }
mbed_official 146:f64d43ff0c18 150
mbed_official 146:f64d43ff0c18 151 void i2c_frequency(i2c_t *obj, int hz) {
mbed_official 146:f64d43ff0c18 152 uint32_t busClock;
mbed_official 146:f64d43ff0c18 153
mbed_official 146:f64d43ff0c18 154 clock_manager_error_code_t error = clock_manager_get_frequency(kBusClock, &busClock);
mbed_official 146:f64d43ff0c18 155 if (error == kClockManagerSuccess) {
mbed_official 146:f64d43ff0c18 156 i2c_hal_set_baud(obj->instance, busClock, hz / 1000, NULL);
mbed_official 146:f64d43ff0c18 157 }
mbed_official 146:f64d43ff0c18 158 }
mbed_official 146:f64d43ff0c18 159
mbed_official 146:f64d43ff0c18 160 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
mbed_official 146:f64d43ff0c18 161 int count;
mbed_official 146:f64d43ff0c18 162 char dummy_read, *ptr;
mbed_official 146:f64d43ff0c18 163
mbed_official 146:f64d43ff0c18 164 if (i2c_start(obj)) {
mbed_official 146:f64d43ff0c18 165 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 166 return I2C_ERROR_BUS_BUSY;
mbed_official 146:f64d43ff0c18 167 }
mbed_official 146:f64d43ff0c18 168
mbed_official 146:f64d43ff0c18 169 if (i2c_do_write(obj, (address | 0x01))) {
mbed_official 146:f64d43ff0c18 170 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 171 return I2C_ERROR_NO_SLAVE;
mbed_official 146:f64d43ff0c18 172 }
mbed_official 146:f64d43ff0c18 173
mbed_official 146:f64d43ff0c18 174 // set rx mode
mbed_official 146:f64d43ff0c18 175 i2c_hal_set_direction(obj->instance, kI2CReceive);
mbed_official 146:f64d43ff0c18 176
mbed_official 146:f64d43ff0c18 177 // Read in bytes
mbed_official 146:f64d43ff0c18 178 for (count = 0; count < (length); count++) {
mbed_official 146:f64d43ff0c18 179 ptr = (count == 0) ? &dummy_read : &data[count - 1];
mbed_official 146:f64d43ff0c18 180 uint8_t stop_ = (count == (length - 1)) ? 1 : 0;
mbed_official 146:f64d43ff0c18 181 if (i2c_do_read(obj, ptr, stop_)) {
mbed_official 146:f64d43ff0c18 182 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 183 return count;
mbed_official 146:f64d43ff0c18 184 }
mbed_official 146:f64d43ff0c18 185 }
mbed_official 146:f64d43ff0c18 186
mbed_official 146:f64d43ff0c18 187 // If not repeated start, send stop.
mbed_official 146:f64d43ff0c18 188 if (stop)
mbed_official 146:f64d43ff0c18 189 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 190
mbed_official 146:f64d43ff0c18 191 // last read
mbed_official 146:f64d43ff0c18 192 data[count-1] = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 193
mbed_official 146:f64d43ff0c18 194 return length;
mbed_official 146:f64d43ff0c18 195 }
mbed_official 146:f64d43ff0c18 196
mbed_official 146:f64d43ff0c18 197 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
mbed_official 146:f64d43ff0c18 198 int i;
mbed_official 146:f64d43ff0c18 199
mbed_official 146:f64d43ff0c18 200 if (i2c_start(obj)) {
mbed_official 146:f64d43ff0c18 201 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 202 return I2C_ERROR_BUS_BUSY;
mbed_official 146:f64d43ff0c18 203 }
mbed_official 146:f64d43ff0c18 204
mbed_official 146:f64d43ff0c18 205 if (i2c_do_write(obj, (address & 0xFE))) {
mbed_official 146:f64d43ff0c18 206 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 207 return I2C_ERROR_NO_SLAVE;
mbed_official 146:f64d43ff0c18 208 }
mbed_official 146:f64d43ff0c18 209
mbed_official 146:f64d43ff0c18 210 for (i = 0; i < length; i++) {
mbed_official 146:f64d43ff0c18 211 if(i2c_do_write(obj, data[i])) {
mbed_official 146:f64d43ff0c18 212 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 213 return i;
mbed_official 146:f64d43ff0c18 214 }
mbed_official 146:f64d43ff0c18 215 }
mbed_official 146:f64d43ff0c18 216
mbed_official 146:f64d43ff0c18 217 if (stop)
mbed_official 146:f64d43ff0c18 218 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 219
mbed_official 146:f64d43ff0c18 220 return length;
mbed_official 146:f64d43ff0c18 221 }
mbed_official 146:f64d43ff0c18 222
mbed_official 146:f64d43ff0c18 223 void i2c_reset(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 224 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 225 }
mbed_official 146:f64d43ff0c18 226
mbed_official 146:f64d43ff0c18 227 int i2c_byte_read(i2c_t *obj, int last) {
mbed_official 146:f64d43ff0c18 228 char data;
mbed_official 146:f64d43ff0c18 229
mbed_official 146:f64d43ff0c18 230 // set rx mode
mbed_official 146:f64d43ff0c18 231 i2c_hal_set_direction(obj->instance, kI2CReceive);
mbed_official 146:f64d43ff0c18 232
mbed_official 178:a873352662d8 233 // Setup read
mbed_official 146:f64d43ff0c18 234 i2c_do_read(obj, &data, last);
mbed_official 146:f64d43ff0c18 235
mbed_official 178:a873352662d8 236 // set tx mode
mbed_official 178:a873352662d8 237 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 178:a873352662d8 238 return i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 239 }
mbed_official 146:f64d43ff0c18 240
mbed_official 146:f64d43ff0c18 241 int i2c_byte_write(i2c_t *obj, int data) {
mbed_official 146:f64d43ff0c18 242 // set tx mode
mbed_official 146:f64d43ff0c18 243 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 146:f64d43ff0c18 244
mbed_official 146:f64d43ff0c18 245 return !i2c_do_write(obj, (data & 0xFF));
mbed_official 146:f64d43ff0c18 246 }
mbed_official 146:f64d43ff0c18 247
mbed_official 146:f64d43ff0c18 248
mbed_official 146:f64d43ff0c18 249 #if DEVICE_I2CSLAVE
mbed_official 146:f64d43ff0c18 250 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
mbed_official 146:f64d43ff0c18 251 if (enable_slave) {
mbed_official 146:f64d43ff0c18 252 // set slave mode
mbed_official 146:f64d43ff0c18 253 BW_I2C_C1_MST(obj->instance, 0);
mbed_official 146:f64d43ff0c18 254 i2c_hal_enable_interrupt(obj->instance);
mbed_official 146:f64d43ff0c18 255 } else {
mbed_official 146:f64d43ff0c18 256 // set master mode
mbed_official 146:f64d43ff0c18 257 BW_I2C_C1_MST(obj->instance, 1);
mbed_official 146:f64d43ff0c18 258 }
mbed_official 146:f64d43ff0c18 259 }
mbed_official 146:f64d43ff0c18 260
mbed_official 146:f64d43ff0c18 261 int i2c_slave_receive(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 262 switch(HW_I2C_S_RD(obj->instance)) {
mbed_official 146:f64d43ff0c18 263 // read addressed
mbed_official 146:f64d43ff0c18 264 case 0xE6:
mbed_official 146:f64d43ff0c18 265 return 1;
mbed_official 146:f64d43ff0c18 266 // write addressed
mbed_official 146:f64d43ff0c18 267 case 0xE2:
mbed_official 146:f64d43ff0c18 268 return 3;
mbed_official 146:f64d43ff0c18 269 default:
mbed_official 146:f64d43ff0c18 270 return 0;
mbed_official 146:f64d43ff0c18 271 }
mbed_official 146:f64d43ff0c18 272 }
mbed_official 146:f64d43ff0c18 273
mbed_official 146:f64d43ff0c18 274 int i2c_slave_read(i2c_t *obj, char *data, int length) {
mbed_official 146:f64d43ff0c18 275 uint8_t dummy_read;
mbed_official 146:f64d43ff0c18 276 uint8_t *ptr;
mbed_official 146:f64d43ff0c18 277 int count;
mbed_official 146:f64d43ff0c18 278
mbed_official 146:f64d43ff0c18 279 // set rx mode
mbed_official 146:f64d43ff0c18 280 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 146:f64d43ff0c18 281
mbed_official 146:f64d43ff0c18 282 // first dummy read
mbed_official 146:f64d43ff0c18 283 dummy_read = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 284 if (i2c_wait_end_rx_transfer(obj))
mbed_official 146:f64d43ff0c18 285 return 0;
mbed_official 146:f64d43ff0c18 286
mbed_official 146:f64d43ff0c18 287 // read address
mbed_official 146:f64d43ff0c18 288 dummy_read = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 289 if (i2c_wait_end_rx_transfer(obj))
mbed_official 146:f64d43ff0c18 290 return 0;
mbed_official 146:f64d43ff0c18 291
mbed_official 146:f64d43ff0c18 292 // read (length - 1) bytes
mbed_official 146:f64d43ff0c18 293 for (count = 0; count < (length - 1); count++) {
mbed_official 146:f64d43ff0c18 294 data[count] = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 295 if (i2c_wait_end_rx_transfer(obj))
mbed_official 146:f64d43ff0c18 296 return count;
mbed_official 146:f64d43ff0c18 297 }
mbed_official 146:f64d43ff0c18 298
mbed_official 146:f64d43ff0c18 299 // read last byte
mbed_official 146:f64d43ff0c18 300 ptr = (length == 0) ? &dummy_read : (uint8_t *)&data[count];
mbed_official 146:f64d43ff0c18 301 *ptr = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 302
mbed_official 146:f64d43ff0c18 303 return (length) ? (count + 1) : 0;
mbed_official 146:f64d43ff0c18 304 }
mbed_official 146:f64d43ff0c18 305
mbed_official 146:f64d43ff0c18 306 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
mbed_official 146:f64d43ff0c18 307 int i, count = 0;
mbed_official 146:f64d43ff0c18 308
mbed_official 146:f64d43ff0c18 309 // set tx mode
mbed_official 146:f64d43ff0c18 310 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 146:f64d43ff0c18 311
mbed_official 146:f64d43ff0c18 312 for (i = 0; i < length; i++) {
mbed_official 146:f64d43ff0c18 313 if (i2c_do_write(obj, data[count++]) == 2)
mbed_official 146:f64d43ff0c18 314 return i;
mbed_official 146:f64d43ff0c18 315 }
mbed_official 146:f64d43ff0c18 316
mbed_official 146:f64d43ff0c18 317 // set rx mode
mbed_official 146:f64d43ff0c18 318 i2c_hal_set_direction(obj->instance, kI2CReceive);
mbed_official 146:f64d43ff0c18 319
mbed_official 146:f64d43ff0c18 320 // dummy rx transfer needed
mbed_official 146:f64d43ff0c18 321 // otherwise the master cannot generate a stop bit
mbed_official 146:f64d43ff0c18 322 i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 323 if (i2c_wait_end_rx_transfer(obj) == 2)
mbed_official 146:f64d43ff0c18 324 return count;
mbed_official 146:f64d43ff0c18 325
mbed_official 146:f64d43ff0c18 326 return count;
mbed_official 146:f64d43ff0c18 327 }
mbed_official 146:f64d43ff0c18 328
mbed_official 146:f64d43ff0c18 329 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
mbed_official 146:f64d43ff0c18 330 i2c_hal_set_upper_slave_address_7bit(obj->instance, address & 0xfe);
mbed_official 146:f64d43ff0c18 331 }
mbed_official 146:f64d43ff0c18 332 #endif
mbed_official 146:f64d43ff0c18 333