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:
Thu Apr 03 11:45:06 2014 +0100
Revision:
149:1fb5f62b92bd
Parent:
targets/hal/TARGET_Freescale/TARGET_KSDK_MCUS/TARGET_K64F/i2c_api.c@146:f64d43ff0c18
Child:
178:a873352662d8
Synchronized with git revision 220c0bb39ceee40016e1e86350c058963d01ed42

Full URL: https://github.com/mbedmicro/mbed/commit/220c0bb39ceee40016e1e86350c058963d01ed42/

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 146:f64d43ff0c18 23
mbed_official 146:f64d43ff0c18 24 static const PinMap PinMap_I2C_SDA[] = {
mbed_official 146:f64d43ff0c18 25 {PTE25, I2C_0, 5},
mbed_official 146:f64d43ff0c18 26 {PTB1 , I2C_0, 2},
mbed_official 146:f64d43ff0c18 27 {PTB3 , I2C_0, 2},
mbed_official 146:f64d43ff0c18 28 {PTC11, I2C_1, 2},
mbed_official 146:f64d43ff0c18 29 {PTA13, I2C_2, 5},
mbed_official 146:f64d43ff0c18 30 {PTD3 , I2C_0, 7},
mbed_official 146:f64d43ff0c18 31 {PTE0 , I2C_1, 6},
mbed_official 146:f64d43ff0c18 32 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 33 };
mbed_official 146:f64d43ff0c18 34
mbed_official 146:f64d43ff0c18 35 static const PinMap PinMap_I2C_SCL[] = {
mbed_official 146:f64d43ff0c18 36 {PTE24, I2C_0, 5},
mbed_official 146:f64d43ff0c18 37 {PTB0 , I2C_0, 2},
mbed_official 146:f64d43ff0c18 38 {PTB2 , I2C_0, 2},
mbed_official 146:f64d43ff0c18 39 {PTC10, I2C_1, 2},
mbed_official 146:f64d43ff0c18 40 {PTA12, I2C_2, 5},
mbed_official 146:f64d43ff0c18 41 {PTA14, I2C_2, 5},
mbed_official 146:f64d43ff0c18 42 {PTD2 , I2C_0, 7},
mbed_official 146:f64d43ff0c18 43 {PTE1 , I2C_1, 6},
mbed_official 146:f64d43ff0c18 44 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 45 };
mbed_official 146:f64d43ff0c18 46
mbed_official 146:f64d43ff0c18 47 static uint8_t first_read;
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 146:f64d43ff0c18 58 i2c_hal_enable(obj->instance);
mbed_official 146:f64d43ff0c18 59 i2c_frequency(obj, 100000);
mbed_official 146:f64d43ff0c18 60
mbed_official 146:f64d43ff0c18 61 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 146:f64d43ff0c18 62 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 146:f64d43ff0c18 63 first_read = 1;
mbed_official 146:f64d43ff0c18 64 }
mbed_official 146:f64d43ff0c18 65
mbed_official 146:f64d43ff0c18 66 int i2c_start(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 67 i2c_hal_send_start(obj->instance);
mbed_official 146:f64d43ff0c18 68 first_read = 1;
mbed_official 146:f64d43ff0c18 69 return 0;
mbed_official 146:f64d43ff0c18 70 }
mbed_official 146:f64d43ff0c18 71
mbed_official 146:f64d43ff0c18 72 int i2c_stop(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 73 volatile uint32_t n = 0;
mbed_official 146:f64d43ff0c18 74 i2c_hal_send_stop(obj->instance);
mbed_official 146:f64d43ff0c18 75
mbed_official 146:f64d43ff0c18 76 // It seems that there are timing problems
mbed_official 146:f64d43ff0c18 77 // when there is no waiting time after a STOP.
mbed_official 146:f64d43ff0c18 78 // This wait is also included on the samples
mbed_official 146:f64d43ff0c18 79 // code provided with the freedom board
mbed_official 146:f64d43ff0c18 80 for (n = 0; n < 100; n++) __NOP();
mbed_official 146:f64d43ff0c18 81 first_read = 1;
mbed_official 146:f64d43ff0c18 82 return 0;
mbed_official 146:f64d43ff0c18 83 }
mbed_official 146:f64d43ff0c18 84
mbed_official 146:f64d43ff0c18 85 static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
mbed_official 146:f64d43ff0c18 86 uint32_t i, timeout = 1000;
mbed_official 146:f64d43ff0c18 87
mbed_official 146:f64d43ff0c18 88 for (i = 0; i < timeout; i++) {
mbed_official 146:f64d43ff0c18 89 if (HW_I2C_S_RD(obj->instance) & mask)
mbed_official 146:f64d43ff0c18 90 return 0;
mbed_official 146:f64d43ff0c18 91 }
mbed_official 146:f64d43ff0c18 92 return 1;
mbed_official 146:f64d43ff0c18 93 }
mbed_official 146:f64d43ff0c18 94
mbed_official 146:f64d43ff0c18 95 // this function waits the end of a tx transfer and return the status of the transaction:
mbed_official 146:f64d43ff0c18 96 // 0: OK ack received
mbed_official 146:f64d43ff0c18 97 // 1: OK ack not received
mbed_official 146:f64d43ff0c18 98 // 2: failure
mbed_official 146:f64d43ff0c18 99 static int i2c_wait_end_tx_transfer(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 100 // wait for the interrupt flag
mbed_official 146:f64d43ff0c18 101 if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) {
mbed_official 146:f64d43ff0c18 102 return 2;
mbed_official 146:f64d43ff0c18 103 }
mbed_official 146:f64d43ff0c18 104
mbed_official 146:f64d43ff0c18 105 i2c_hal_clear_interrupt(obj->instance);
mbed_official 146:f64d43ff0c18 106
mbed_official 146:f64d43ff0c18 107 // wait transfer complete
mbed_official 146:f64d43ff0c18 108 if (timeout_status_poll(obj, I2C_S_TCF_MASK)) {
mbed_official 146:f64d43ff0c18 109 return 2;
mbed_official 146:f64d43ff0c18 110 }
mbed_official 146:f64d43ff0c18 111
mbed_official 146:f64d43ff0c18 112 // check if we received the ACK or not
mbed_official 146:f64d43ff0c18 113 return i2c_hal_get_receive_ack(obj->instance) ? 0 : 1;
mbed_official 146:f64d43ff0c18 114 }
mbed_official 146:f64d43ff0c18 115
mbed_official 146:f64d43ff0c18 116 // this function waits the end of a rx transfer and return the status of the transaction:
mbed_official 146:f64d43ff0c18 117 // 0: OK
mbed_official 146:f64d43ff0c18 118 // 1: failure
mbed_official 146:f64d43ff0c18 119 static int i2c_wait_end_rx_transfer(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 120 // wait for the end of the rx transfer
mbed_official 146:f64d43ff0c18 121 if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) {
mbed_official 146:f64d43ff0c18 122 return 1;
mbed_official 146:f64d43ff0c18 123 }
mbed_official 146:f64d43ff0c18 124
mbed_official 146:f64d43ff0c18 125 i2c_hal_clear_interrupt(obj->instance);
mbed_official 146:f64d43ff0c18 126
mbed_official 146:f64d43ff0c18 127 return 0;
mbed_official 146:f64d43ff0c18 128 }
mbed_official 146:f64d43ff0c18 129
mbed_official 146:f64d43ff0c18 130 static int i2c_do_write(i2c_t *obj, int value) {
mbed_official 146:f64d43ff0c18 131 i2c_hal_write(obj->instance, value);
mbed_official 146:f64d43ff0c18 132
mbed_official 146:f64d43ff0c18 133 // init and wait the end of the transfer
mbed_official 146:f64d43ff0c18 134 return i2c_wait_end_tx_transfer(obj);
mbed_official 146:f64d43ff0c18 135 }
mbed_official 146:f64d43ff0c18 136
mbed_official 146:f64d43ff0c18 137 static int i2c_do_read(i2c_t *obj, char * data, int last) {
mbed_official 146:f64d43ff0c18 138 if (last) {
mbed_official 146:f64d43ff0c18 139 i2c_hal_send_nak(obj->instance);
mbed_official 146:f64d43ff0c18 140 } else {
mbed_official 146:f64d43ff0c18 141 i2c_hal_send_ack(obj->instance);
mbed_official 146:f64d43ff0c18 142 }
mbed_official 146:f64d43ff0c18 143
mbed_official 146:f64d43ff0c18 144 *data = (i2c_hal_read(obj->instance) & 0xFF);
mbed_official 146:f64d43ff0c18 145
mbed_official 146:f64d43ff0c18 146 // start rx transfer and wait the end of the transfer
mbed_official 146:f64d43ff0c18 147 return i2c_wait_end_rx_transfer(obj);
mbed_official 146:f64d43ff0c18 148 }
mbed_official 146:f64d43ff0c18 149
mbed_official 146:f64d43ff0c18 150 void i2c_frequency(i2c_t *obj, int hz) {
mbed_official 146:f64d43ff0c18 151 uint32_t busClock;
mbed_official 146:f64d43ff0c18 152
mbed_official 146:f64d43ff0c18 153 clock_manager_error_code_t error = clock_manager_get_frequency(kBusClock, &busClock);
mbed_official 146:f64d43ff0c18 154 if (error == kClockManagerSuccess) {
mbed_official 146:f64d43ff0c18 155 i2c_hal_set_baud(obj->instance, busClock, hz / 1000, NULL);
mbed_official 146:f64d43ff0c18 156 }
mbed_official 146:f64d43ff0c18 157 }
mbed_official 146:f64d43ff0c18 158
mbed_official 146:f64d43ff0c18 159 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
mbed_official 146:f64d43ff0c18 160 int count;
mbed_official 146:f64d43ff0c18 161 char dummy_read, *ptr;
mbed_official 146:f64d43ff0c18 162
mbed_official 146:f64d43ff0c18 163 if (i2c_start(obj)) {
mbed_official 146:f64d43ff0c18 164 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 165 return I2C_ERROR_BUS_BUSY;
mbed_official 146:f64d43ff0c18 166 }
mbed_official 146:f64d43ff0c18 167
mbed_official 146:f64d43ff0c18 168 if (i2c_do_write(obj, (address | 0x01))) {
mbed_official 146:f64d43ff0c18 169 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 170 return I2C_ERROR_NO_SLAVE;
mbed_official 146:f64d43ff0c18 171 }
mbed_official 146:f64d43ff0c18 172
mbed_official 146:f64d43ff0c18 173 // set rx mode
mbed_official 146:f64d43ff0c18 174 i2c_hal_set_direction(obj->instance, kI2CReceive);
mbed_official 146:f64d43ff0c18 175
mbed_official 146:f64d43ff0c18 176 // Read in bytes
mbed_official 146:f64d43ff0c18 177 for (count = 0; count < (length); count++) {
mbed_official 146:f64d43ff0c18 178 ptr = (count == 0) ? &dummy_read : &data[count - 1];
mbed_official 146:f64d43ff0c18 179 uint8_t stop_ = (count == (length - 1)) ? 1 : 0;
mbed_official 146:f64d43ff0c18 180 if (i2c_do_read(obj, ptr, stop_)) {
mbed_official 146:f64d43ff0c18 181 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 182 return count;
mbed_official 146:f64d43ff0c18 183 }
mbed_official 146:f64d43ff0c18 184 }
mbed_official 146:f64d43ff0c18 185
mbed_official 146:f64d43ff0c18 186 // If not repeated start, send stop.
mbed_official 146:f64d43ff0c18 187 if (stop)
mbed_official 146:f64d43ff0c18 188 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 189
mbed_official 146:f64d43ff0c18 190 // last read
mbed_official 146:f64d43ff0c18 191 data[count-1] = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 192
mbed_official 146:f64d43ff0c18 193 return length;
mbed_official 146:f64d43ff0c18 194 }
mbed_official 146:f64d43ff0c18 195
mbed_official 146:f64d43ff0c18 196 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
mbed_official 146:f64d43ff0c18 197 int i;
mbed_official 146:f64d43ff0c18 198
mbed_official 146:f64d43ff0c18 199 if (i2c_start(obj)) {
mbed_official 146:f64d43ff0c18 200 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 201 return I2C_ERROR_BUS_BUSY;
mbed_official 146:f64d43ff0c18 202 }
mbed_official 146:f64d43ff0c18 203
mbed_official 146:f64d43ff0c18 204 if (i2c_do_write(obj, (address & 0xFE))) {
mbed_official 146:f64d43ff0c18 205 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 206 return I2C_ERROR_NO_SLAVE;
mbed_official 146:f64d43ff0c18 207 }
mbed_official 146:f64d43ff0c18 208
mbed_official 146:f64d43ff0c18 209 for (i = 0; i < length; i++) {
mbed_official 146:f64d43ff0c18 210 if(i2c_do_write(obj, data[i])) {
mbed_official 146:f64d43ff0c18 211 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 212 return i;
mbed_official 146:f64d43ff0c18 213 }
mbed_official 146:f64d43ff0c18 214 }
mbed_official 146:f64d43ff0c18 215
mbed_official 146:f64d43ff0c18 216 if (stop)
mbed_official 146:f64d43ff0c18 217 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 218
mbed_official 146:f64d43ff0c18 219 return length;
mbed_official 146:f64d43ff0c18 220 }
mbed_official 146:f64d43ff0c18 221
mbed_official 146:f64d43ff0c18 222 void i2c_reset(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 223 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 224 }
mbed_official 146:f64d43ff0c18 225
mbed_official 146:f64d43ff0c18 226 int i2c_byte_read(i2c_t *obj, int last) {
mbed_official 146:f64d43ff0c18 227 char data;
mbed_official 146:f64d43ff0c18 228
mbed_official 146:f64d43ff0c18 229 // set rx mode
mbed_official 146:f64d43ff0c18 230 i2c_hal_set_direction(obj->instance, kI2CReceive);
mbed_official 146:f64d43ff0c18 231
mbed_official 146:f64d43ff0c18 232 if(first_read) {
mbed_official 146:f64d43ff0c18 233 // first dummy read
mbed_official 146:f64d43ff0c18 234 i2c_do_read(obj, &data, 0);
mbed_official 146:f64d43ff0c18 235 first_read = 0;
mbed_official 146:f64d43ff0c18 236 }
mbed_official 146:f64d43ff0c18 237
mbed_official 146:f64d43ff0c18 238 if (last) {
mbed_official 146:f64d43ff0c18 239 // set tx mode
mbed_official 146:f64d43ff0c18 240 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 146:f64d43ff0c18 241 return i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 242 }
mbed_official 146:f64d43ff0c18 243
mbed_official 146:f64d43ff0c18 244 i2c_do_read(obj, &data, last);
mbed_official 146:f64d43ff0c18 245
mbed_official 146:f64d43ff0c18 246 return data;
mbed_official 146:f64d43ff0c18 247 }
mbed_official 146:f64d43ff0c18 248
mbed_official 146:f64d43ff0c18 249 int i2c_byte_write(i2c_t *obj, int data) {
mbed_official 146:f64d43ff0c18 250 first_read = 1;
mbed_official 146:f64d43ff0c18 251
mbed_official 146:f64d43ff0c18 252 // set tx mode
mbed_official 146:f64d43ff0c18 253 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 146:f64d43ff0c18 254
mbed_official 146:f64d43ff0c18 255 return !i2c_do_write(obj, (data & 0xFF));
mbed_official 146:f64d43ff0c18 256 }
mbed_official 146:f64d43ff0c18 257
mbed_official 146:f64d43ff0c18 258
mbed_official 146:f64d43ff0c18 259 #if DEVICE_I2CSLAVE
mbed_official 146:f64d43ff0c18 260 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
mbed_official 146:f64d43ff0c18 261 if (enable_slave) {
mbed_official 146:f64d43ff0c18 262 // set slave mode
mbed_official 146:f64d43ff0c18 263 BW_I2C_C1_MST(obj->instance, 0);
mbed_official 146:f64d43ff0c18 264 i2c_hal_enable_interrupt(obj->instance);
mbed_official 146:f64d43ff0c18 265 } else {
mbed_official 146:f64d43ff0c18 266 // set master mode
mbed_official 146:f64d43ff0c18 267 BW_I2C_C1_MST(obj->instance, 1);
mbed_official 146:f64d43ff0c18 268 }
mbed_official 146:f64d43ff0c18 269 }
mbed_official 146:f64d43ff0c18 270
mbed_official 146:f64d43ff0c18 271 int i2c_slave_receive(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 272 switch(HW_I2C_S_RD(obj->instance)) {
mbed_official 146:f64d43ff0c18 273 // read addressed
mbed_official 146:f64d43ff0c18 274 case 0xE6:
mbed_official 146:f64d43ff0c18 275 return 1;
mbed_official 146:f64d43ff0c18 276 // write addressed
mbed_official 146:f64d43ff0c18 277 case 0xE2:
mbed_official 146:f64d43ff0c18 278 return 3;
mbed_official 146:f64d43ff0c18 279 default:
mbed_official 146:f64d43ff0c18 280 return 0;
mbed_official 146:f64d43ff0c18 281 }
mbed_official 146:f64d43ff0c18 282 }
mbed_official 146:f64d43ff0c18 283
mbed_official 146:f64d43ff0c18 284 int i2c_slave_read(i2c_t *obj, char *data, int length) {
mbed_official 146:f64d43ff0c18 285 uint8_t dummy_read;
mbed_official 146:f64d43ff0c18 286 uint8_t *ptr;
mbed_official 146:f64d43ff0c18 287 int count;
mbed_official 146:f64d43ff0c18 288
mbed_official 146:f64d43ff0c18 289 // set rx mode
mbed_official 146:f64d43ff0c18 290 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 146:f64d43ff0c18 291
mbed_official 146:f64d43ff0c18 292 // first dummy read
mbed_official 146:f64d43ff0c18 293 dummy_read = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 294 if (i2c_wait_end_rx_transfer(obj))
mbed_official 146:f64d43ff0c18 295 return 0;
mbed_official 146:f64d43ff0c18 296
mbed_official 146:f64d43ff0c18 297 // read address
mbed_official 146:f64d43ff0c18 298 dummy_read = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 299 if (i2c_wait_end_rx_transfer(obj))
mbed_official 146:f64d43ff0c18 300 return 0;
mbed_official 146:f64d43ff0c18 301
mbed_official 146:f64d43ff0c18 302 // read (length - 1) bytes
mbed_official 146:f64d43ff0c18 303 for (count = 0; count < (length - 1); count++) {
mbed_official 146:f64d43ff0c18 304 data[count] = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 305 if (i2c_wait_end_rx_transfer(obj))
mbed_official 146:f64d43ff0c18 306 return count;
mbed_official 146:f64d43ff0c18 307 }
mbed_official 146:f64d43ff0c18 308
mbed_official 146:f64d43ff0c18 309 // read last byte
mbed_official 146:f64d43ff0c18 310 ptr = (length == 0) ? &dummy_read : (uint8_t *)&data[count];
mbed_official 146:f64d43ff0c18 311 *ptr = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 312
mbed_official 146:f64d43ff0c18 313 return (length) ? (count + 1) : 0;
mbed_official 146:f64d43ff0c18 314 }
mbed_official 146:f64d43ff0c18 315
mbed_official 146:f64d43ff0c18 316 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
mbed_official 146:f64d43ff0c18 317 int i, count = 0;
mbed_official 146:f64d43ff0c18 318
mbed_official 146:f64d43ff0c18 319 // set tx mode
mbed_official 146:f64d43ff0c18 320 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 146:f64d43ff0c18 321
mbed_official 146:f64d43ff0c18 322 for (i = 0; i < length; i++) {
mbed_official 146:f64d43ff0c18 323 if (i2c_do_write(obj, data[count++]) == 2)
mbed_official 146:f64d43ff0c18 324 return i;
mbed_official 146:f64d43ff0c18 325 }
mbed_official 146:f64d43ff0c18 326
mbed_official 146:f64d43ff0c18 327 // set rx mode
mbed_official 146:f64d43ff0c18 328 i2c_hal_set_direction(obj->instance, kI2CReceive);
mbed_official 146:f64d43ff0c18 329
mbed_official 146:f64d43ff0c18 330 // dummy rx transfer needed
mbed_official 146:f64d43ff0c18 331 // otherwise the master cannot generate a stop bit
mbed_official 146:f64d43ff0c18 332 i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 333 if (i2c_wait_end_rx_transfer(obj) == 2)
mbed_official 146:f64d43ff0c18 334 return count;
mbed_official 146:f64d43ff0c18 335
mbed_official 146:f64d43ff0c18 336 return count;
mbed_official 146:f64d43ff0c18 337 }
mbed_official 146:f64d43ff0c18 338
mbed_official 146:f64d43ff0c18 339 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
mbed_official 146:f64d43ff0c18 340 i2c_hal_set_upper_slave_address_7bit(obj->instance, address & 0xfe);
mbed_official 146:f64d43ff0c18 341 }
mbed_official 146:f64d43ff0c18 342 #endif
mbed_official 146:f64d43ff0c18 343