mbed library sources

Fork of mbed-src by mbed official

Committer:
lzbpli
Date:
Thu Jul 07 06:48:59 2016 +0000
Revision:
636:b0d178e9fa10
Parent:
318:bad45627264e
l053

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 256:76fd9a263045 1 /* mbed Microcontroller Library
mbed_official 256:76fd9a263045 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 256:76fd9a263045 3 *
mbed_official 256:76fd9a263045 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 256:76fd9a263045 5 * you may not use this file except in compliance with the License.
mbed_official 256:76fd9a263045 6 * You may obtain a copy of the License at
mbed_official 256:76fd9a263045 7 *
mbed_official 256:76fd9a263045 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 256:76fd9a263045 9 *
mbed_official 256:76fd9a263045 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 256:76fd9a263045 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 256:76fd9a263045 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 256:76fd9a263045 13 * See the License for the specific language governing permissions and
mbed_official 256:76fd9a263045 14 * limitations under the License.
mbed_official 256:76fd9a263045 15 *
mbed_official 256:76fd9a263045 16 * Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
mbed_official 256:76fd9a263045 17 */
mbed_official 256:76fd9a263045 18 #include "i2c_api.h"
mbed_official 256:76fd9a263045 19 #include "cmsis.h"
mbed_official 256:76fd9a263045 20 #include "pinmap.h"
mbed_official 285:31249416b6f9 21 #include "mbed_error.h"
mbed_official 256:76fd9a263045 22
mbed_official 283:bf0f62a62bf4 23 // SCU mode for I2C SCL/SDA pins
mbed_official 283:bf0f62a62bf4 24 #define SCU_PINIO_I2C SCU_PINIO_PULLNONE
mbed_official 283:bf0f62a62bf4 25
mbed_official 256:76fd9a263045 26 static const PinMap PinMap_I2C_SDA[] = {
mbed_official 256:76fd9a263045 27 {P_DED, I2C_0, 0},
mbed_official 283:bf0f62a62bf4 28 {P2_3, I2C_1, (SCU_PINIO_I2C | 1)},
mbed_official 283:bf0f62a62bf4 29 {PE_13, I2C_1, (SCU_PINIO_I2C | 2)},
mbed_official 256:76fd9a263045 30 {NC, NC, 0}
mbed_official 256:76fd9a263045 31 };
mbed_official 256:76fd9a263045 32
mbed_official 256:76fd9a263045 33 static const PinMap PinMap_I2C_SCL[] = {
mbed_official 256:76fd9a263045 34 {P_DED, I2C_0, 0},
mbed_official 283:bf0f62a62bf4 35 {P2_4, I2C_1, (SCU_PINIO_I2C | 1)},
mbed_official 283:bf0f62a62bf4 36 {PE_14, I2C_1, (SCU_PINIO_I2C | 2)},
mbed_official 256:76fd9a263045 37 {NC, NC, 0}
mbed_official 256:76fd9a263045 38 };
mbed_official 256:76fd9a263045 39
mbed_official 256:76fd9a263045 40 #define I2C_CONSET(x) (x->i2c->CONSET)
mbed_official 256:76fd9a263045 41 #define I2C_CONCLR(x) (x->i2c->CONCLR)
mbed_official 256:76fd9a263045 42 #define I2C_STAT(x) (x->i2c->STAT)
mbed_official 256:76fd9a263045 43 #define I2C_DAT(x) (x->i2c->DAT)
mbed_official 256:76fd9a263045 44 #define I2C_SCLL(x, val) (x->i2c->SCLL = val)
mbed_official 256:76fd9a263045 45 #define I2C_SCLH(x, val) (x->i2c->SCLH = val)
mbed_official 256:76fd9a263045 46
mbed_official 256:76fd9a263045 47 static const uint32_t I2C_addr_offset[2][4] = {
mbed_official 256:76fd9a263045 48 {0x0C, 0x20, 0x24, 0x28},
mbed_official 256:76fd9a263045 49 {0x30, 0x34, 0x38, 0x3C}
mbed_official 256:76fd9a263045 50 };
mbed_official 256:76fd9a263045 51
mbed_official 256:76fd9a263045 52 static inline void i2c_conclr(i2c_t *obj, int start, int stop, int interrupt, int acknowledge) {
mbed_official 256:76fd9a263045 53 I2C_CONCLR(obj) = (start << 5)
mbed_official 256:76fd9a263045 54 | (stop << 4)
mbed_official 256:76fd9a263045 55 | (interrupt << 3)
mbed_official 256:76fd9a263045 56 | (acknowledge << 2);
mbed_official 256:76fd9a263045 57 }
mbed_official 256:76fd9a263045 58
mbed_official 256:76fd9a263045 59 static inline void i2c_conset(i2c_t *obj, int start, int stop, int interrupt, int acknowledge) {
mbed_official 256:76fd9a263045 60 I2C_CONSET(obj) = (start << 5)
mbed_official 256:76fd9a263045 61 | (stop << 4)
mbed_official 256:76fd9a263045 62 | (interrupt << 3)
mbed_official 256:76fd9a263045 63 | (acknowledge << 2);
mbed_official 256:76fd9a263045 64 }
mbed_official 256:76fd9a263045 65
mbed_official 256:76fd9a263045 66 // Clear the Serial Interrupt (SI)
mbed_official 256:76fd9a263045 67 static inline void i2c_clear_SI(i2c_t *obj) {
mbed_official 256:76fd9a263045 68 i2c_conclr(obj, 0, 0, 1, 0);
mbed_official 256:76fd9a263045 69 }
mbed_official 256:76fd9a263045 70
mbed_official 256:76fd9a263045 71 static inline int i2c_status(i2c_t *obj) {
mbed_official 256:76fd9a263045 72 return I2C_STAT(obj);
mbed_official 256:76fd9a263045 73 }
mbed_official 256:76fd9a263045 74
mbed_official 256:76fd9a263045 75 // Wait until the Serial Interrupt (SI) is set
mbed_official 256:76fd9a263045 76 static int i2c_wait_SI(i2c_t *obj) {
mbed_official 256:76fd9a263045 77 int timeout = 0;
mbed_official 256:76fd9a263045 78 while (!(I2C_CONSET(obj) & (1 << 3))) {
mbed_official 256:76fd9a263045 79 timeout++;
mbed_official 256:76fd9a263045 80 if (timeout > 100000) return -1;
mbed_official 256:76fd9a263045 81 }
mbed_official 256:76fd9a263045 82 return 0;
mbed_official 256:76fd9a263045 83 }
mbed_official 256:76fd9a263045 84
mbed_official 256:76fd9a263045 85 static inline void i2c_interface_enable(i2c_t *obj) {
mbed_official 256:76fd9a263045 86 I2C_CONSET(obj) = 0x40;
mbed_official 256:76fd9a263045 87 }
mbed_official 256:76fd9a263045 88
mbed_official 256:76fd9a263045 89 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
mbed_official 256:76fd9a263045 90 // determine the SPI to use
mbed_official 256:76fd9a263045 91 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
mbed_official 256:76fd9a263045 92 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
mbed_official 256:76fd9a263045 93 obj->i2c = (LPC_I2C_T *)pinmap_merge(i2c_sda, i2c_scl);
mbed_official 256:76fd9a263045 94
mbed_official 256:76fd9a263045 95 if ((int)obj->i2c == NC) {
mbed_official 256:76fd9a263045 96 error("I2C pin mapping failed");
mbed_official 256:76fd9a263045 97 }
mbed_official 256:76fd9a263045 98
mbed_official 256:76fd9a263045 99 // set default frequency at 100k
mbed_official 256:76fd9a263045 100 i2c_frequency(obj, 100000);
mbed_official 256:76fd9a263045 101 i2c_conclr(obj, 1, 1, 1, 1);
mbed_official 256:76fd9a263045 102 i2c_interface_enable(obj);
mbed_official 256:76fd9a263045 103
mbed_official 318:bad45627264e 104 // Set SCU functions
mbed_official 318:bad45627264e 105 if (scl == P_DED) {
mbed_official 318:bad45627264e 106 // Enable dedicated I2C0 SDA and SCL pins (open drain)
mbed_official 318:bad45627264e 107 LPC_SCU->SFSI2C0 = (1 << 11) | (1 << 3);
mbed_official 318:bad45627264e 108 } else {
mbed_official 256:76fd9a263045 109 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 256:76fd9a263045 110 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 256:76fd9a263045 111 }
mbed_official 256:76fd9a263045 112 }
mbed_official 256:76fd9a263045 113
mbed_official 256:76fd9a263045 114 inline int i2c_start(i2c_t *obj) {
mbed_official 256:76fd9a263045 115 int status = 0;
mbed_official 256:76fd9a263045 116 // 8.1 Before master mode can be entered, I2CON must be initialised to:
mbed_official 256:76fd9a263045 117 // - I2EN STA STO SI AA - -
mbed_official 256:76fd9a263045 118 // - 1 0 0 0 x - -
mbed_official 256:76fd9a263045 119 // if AA = 0, it can't enter slave mode
mbed_official 256:76fd9a263045 120 i2c_conclr(obj, 1, 1, 1, 1);
mbed_official 256:76fd9a263045 121
mbed_official 256:76fd9a263045 122 // The master mode may now be entered by setting the STA bit
mbed_official 256:76fd9a263045 123 // this will generate a start condition when the bus becomes free
mbed_official 256:76fd9a263045 124 i2c_conset(obj, 1, 0, 0, 1);
mbed_official 256:76fd9a263045 125
mbed_official 256:76fd9a263045 126 i2c_wait_SI(obj);
mbed_official 256:76fd9a263045 127 status = i2c_status(obj);
mbed_official 256:76fd9a263045 128
mbed_official 256:76fd9a263045 129 // Clear start bit now transmitted, and interrupt bit
mbed_official 256:76fd9a263045 130 i2c_conclr(obj, 1, 0, 0, 0);
mbed_official 256:76fd9a263045 131 return status;
mbed_official 256:76fd9a263045 132 }
mbed_official 256:76fd9a263045 133
mbed_official 256:76fd9a263045 134 inline int i2c_stop(i2c_t *obj) {
mbed_official 256:76fd9a263045 135 int timeout = 0;
mbed_official 256:76fd9a263045 136
mbed_official 256:76fd9a263045 137 // write the stop bit
mbed_official 256:76fd9a263045 138 i2c_conset(obj, 0, 1, 0, 0);
mbed_official 256:76fd9a263045 139 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 140
mbed_official 256:76fd9a263045 141 // wait for STO bit to reset
mbed_official 256:76fd9a263045 142 while(I2C_CONSET(obj) & (1 << 4)) {
mbed_official 256:76fd9a263045 143 timeout ++;
mbed_official 256:76fd9a263045 144 if (timeout > 100000) return 1;
mbed_official 256:76fd9a263045 145 }
mbed_official 256:76fd9a263045 146
mbed_official 256:76fd9a263045 147 return 0;
mbed_official 256:76fd9a263045 148 }
mbed_official 256:76fd9a263045 149
mbed_official 256:76fd9a263045 150 static inline int i2c_do_write(i2c_t *obj, int value, uint8_t addr) {
mbed_official 256:76fd9a263045 151 // write the data
mbed_official 256:76fd9a263045 152 I2C_DAT(obj) = value;
mbed_official 256:76fd9a263045 153
mbed_official 256:76fd9a263045 154 // clear SI to init a send
mbed_official 256:76fd9a263045 155 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 156
mbed_official 256:76fd9a263045 157 // wait and return status
mbed_official 256:76fd9a263045 158 i2c_wait_SI(obj);
mbed_official 256:76fd9a263045 159 return i2c_status(obj);
mbed_official 256:76fd9a263045 160 }
mbed_official 256:76fd9a263045 161
mbed_official 256:76fd9a263045 162 static inline int i2c_do_read(i2c_t *obj, int last) {
mbed_official 256:76fd9a263045 163 // we are in state 0x40 (SLA+R tx'd) or 0x50 (data rx'd and ack)
mbed_official 256:76fd9a263045 164 if(last) {
mbed_official 256:76fd9a263045 165 i2c_conclr(obj, 0, 0, 0, 1); // send a NOT ACK
mbed_official 256:76fd9a263045 166 } else {
mbed_official 256:76fd9a263045 167 i2c_conset(obj, 0, 0, 0, 1); // send a ACK
mbed_official 256:76fd9a263045 168 }
mbed_official 256:76fd9a263045 169
mbed_official 256:76fd9a263045 170 // accept byte
mbed_official 256:76fd9a263045 171 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 172
mbed_official 256:76fd9a263045 173 // wait for it to arrive
mbed_official 256:76fd9a263045 174 i2c_wait_SI(obj);
mbed_official 256:76fd9a263045 175
mbed_official 256:76fd9a263045 176 // return the data
mbed_official 256:76fd9a263045 177 return (I2C_DAT(obj) & 0xFF);
mbed_official 256:76fd9a263045 178 }
mbed_official 256:76fd9a263045 179
mbed_official 256:76fd9a263045 180 void i2c_frequency(i2c_t *obj, int hz) {
mbed_official 256:76fd9a263045 181 // [TODO] set pclk to /4
mbed_official 256:76fd9a263045 182 uint32_t PCLK = SystemCoreClock / 4;
mbed_official 256:76fd9a263045 183
mbed_official 256:76fd9a263045 184 uint32_t pulse = PCLK / (hz * 2);
mbed_official 256:76fd9a263045 185
mbed_official 256:76fd9a263045 186 // I2C Rate
mbed_official 256:76fd9a263045 187 I2C_SCLL(obj, pulse);
mbed_official 256:76fd9a263045 188 I2C_SCLH(obj, pulse);
mbed_official 256:76fd9a263045 189 }
mbed_official 256:76fd9a263045 190
mbed_official 256:76fd9a263045 191 // The I2C does a read or a write as a whole operation
mbed_official 256:76fd9a263045 192 // There are two types of error conditions it can encounter
mbed_official 256:76fd9a263045 193 // 1) it can not obtain the bus
mbed_official 256:76fd9a263045 194 // 2) it gets error responses at part of the transmission
mbed_official 256:76fd9a263045 195 //
mbed_official 256:76fd9a263045 196 // We tackle them as follows:
mbed_official 256:76fd9a263045 197 // 1) we retry until we get the bus. we could have a "timeout" if we can not get it
mbed_official 256:76fd9a263045 198 // which basically turns it in to a 2)
mbed_official 256:76fd9a263045 199 // 2) on error, we use the standard error mechanisms to report/debug
mbed_official 256:76fd9a263045 200 //
mbed_official 256:76fd9a263045 201 // Therefore an I2C transaction should always complete. If it doesn't it is usually
mbed_official 256:76fd9a263045 202 // because something is setup wrong (e.g. wiring), and we don't need to programatically
mbed_official 256:76fd9a263045 203 // check for that
mbed_official 256:76fd9a263045 204
mbed_official 256:76fd9a263045 205 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
mbed_official 256:76fd9a263045 206 int count, status;
mbed_official 256:76fd9a263045 207
mbed_official 256:76fd9a263045 208 status = i2c_start(obj);
mbed_official 256:76fd9a263045 209
mbed_official 256:76fd9a263045 210 if ((status != 0x10) && (status != 0x08)) {
mbed_official 256:76fd9a263045 211 i2c_stop(obj);
mbed_official 256:76fd9a263045 212 return I2C_ERROR_BUS_BUSY;
mbed_official 256:76fd9a263045 213 }
mbed_official 256:76fd9a263045 214
mbed_official 256:76fd9a263045 215 status = i2c_do_write(obj, (address | 0x01), 1);
mbed_official 256:76fd9a263045 216 if (status != 0x40) {
mbed_official 256:76fd9a263045 217 i2c_stop(obj);
mbed_official 256:76fd9a263045 218 return I2C_ERROR_NO_SLAVE;
mbed_official 256:76fd9a263045 219 }
mbed_official 256:76fd9a263045 220
mbed_official 256:76fd9a263045 221 // Read in all except last byte
mbed_official 256:76fd9a263045 222 for (count = 0; count < (length - 1); count++) {
mbed_official 256:76fd9a263045 223 int value = i2c_do_read(obj, 0);
mbed_official 256:76fd9a263045 224 status = i2c_status(obj);
mbed_official 256:76fd9a263045 225 if (status != 0x50) {
mbed_official 256:76fd9a263045 226 i2c_stop(obj);
mbed_official 256:76fd9a263045 227 return count;
mbed_official 256:76fd9a263045 228 }
mbed_official 256:76fd9a263045 229 data[count] = (char) value;
mbed_official 256:76fd9a263045 230 }
mbed_official 256:76fd9a263045 231
mbed_official 256:76fd9a263045 232 // read in last byte
mbed_official 256:76fd9a263045 233 int value = i2c_do_read(obj, 1);
mbed_official 256:76fd9a263045 234 status = i2c_status(obj);
mbed_official 256:76fd9a263045 235 if (status != 0x58) {
mbed_official 256:76fd9a263045 236 i2c_stop(obj);
mbed_official 256:76fd9a263045 237 return length - 1;
mbed_official 256:76fd9a263045 238 }
mbed_official 256:76fd9a263045 239
mbed_official 256:76fd9a263045 240 data[count] = (char) value;
mbed_official 256:76fd9a263045 241
mbed_official 256:76fd9a263045 242 // If not repeated start, send stop.
mbed_official 256:76fd9a263045 243 if (stop) {
mbed_official 256:76fd9a263045 244 i2c_stop(obj);
mbed_official 256:76fd9a263045 245 }
mbed_official 256:76fd9a263045 246
mbed_official 256:76fd9a263045 247 return length;
mbed_official 256:76fd9a263045 248 }
mbed_official 256:76fd9a263045 249
mbed_official 256:76fd9a263045 250 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
mbed_official 256:76fd9a263045 251 int i, status;
mbed_official 256:76fd9a263045 252
mbed_official 256:76fd9a263045 253 status = i2c_start(obj);
mbed_official 256:76fd9a263045 254
mbed_official 256:76fd9a263045 255 if ((status != 0x10) && (status != 0x08)) {
mbed_official 256:76fd9a263045 256 i2c_stop(obj);
mbed_official 256:76fd9a263045 257 return I2C_ERROR_BUS_BUSY;
mbed_official 256:76fd9a263045 258 }
mbed_official 256:76fd9a263045 259
mbed_official 256:76fd9a263045 260 status = i2c_do_write(obj, (address & 0xFE), 1);
mbed_official 256:76fd9a263045 261 if (status != 0x18) {
mbed_official 256:76fd9a263045 262 i2c_stop(obj);
mbed_official 256:76fd9a263045 263 return I2C_ERROR_NO_SLAVE;
mbed_official 256:76fd9a263045 264 }
mbed_official 256:76fd9a263045 265
mbed_official 256:76fd9a263045 266 for (i=0; i<length; i++) {
mbed_official 256:76fd9a263045 267 status = i2c_do_write(obj, data[i], 0);
mbed_official 256:76fd9a263045 268 if(status != 0x28) {
mbed_official 256:76fd9a263045 269 i2c_stop(obj);
mbed_official 256:76fd9a263045 270 return i;
mbed_official 256:76fd9a263045 271 }
mbed_official 256:76fd9a263045 272 }
mbed_official 256:76fd9a263045 273
mbed_official 256:76fd9a263045 274 // clearing the serial interrupt here might cause an unintended rewrite of the last byte
mbed_official 256:76fd9a263045 275 // see also issue report https://mbed.org/users/mbed_official/code/mbed/issues/1
mbed_official 256:76fd9a263045 276 // i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 277
mbed_official 256:76fd9a263045 278 // If not repeated start, send stop.
mbed_official 256:76fd9a263045 279 if (stop) {
mbed_official 256:76fd9a263045 280 i2c_stop(obj);
mbed_official 256:76fd9a263045 281 }
mbed_official 256:76fd9a263045 282
mbed_official 256:76fd9a263045 283 return length;
mbed_official 256:76fd9a263045 284 }
mbed_official 256:76fd9a263045 285
mbed_official 256:76fd9a263045 286 void i2c_reset(i2c_t *obj) {
mbed_official 256:76fd9a263045 287 i2c_stop(obj);
mbed_official 256:76fd9a263045 288 }
mbed_official 256:76fd9a263045 289
mbed_official 256:76fd9a263045 290 int i2c_byte_read(i2c_t *obj, int last) {
mbed_official 256:76fd9a263045 291 return (i2c_do_read(obj, last) & 0xFF);
mbed_official 256:76fd9a263045 292 }
mbed_official 256:76fd9a263045 293
mbed_official 256:76fd9a263045 294 int i2c_byte_write(i2c_t *obj, int data) {
mbed_official 256:76fd9a263045 295 int ack;
mbed_official 256:76fd9a263045 296 int status = i2c_do_write(obj, (data & 0xFF), 0);
mbed_official 256:76fd9a263045 297
mbed_official 256:76fd9a263045 298 switch(status) {
mbed_official 256:76fd9a263045 299 case 0x18: case 0x28: // Master transmit ACKs
mbed_official 256:76fd9a263045 300 ack = 1;
mbed_official 256:76fd9a263045 301 break;
mbed_official 256:76fd9a263045 302 case 0x40: // Master receive address transmitted ACK
mbed_official 256:76fd9a263045 303 ack = 1;
mbed_official 256:76fd9a263045 304 break;
mbed_official 256:76fd9a263045 305 case 0xB8: // Slave transmit ACK
mbed_official 256:76fd9a263045 306 ack = 1;
mbed_official 256:76fd9a263045 307 break;
mbed_official 256:76fd9a263045 308 default:
mbed_official 256:76fd9a263045 309 ack = 0;
mbed_official 256:76fd9a263045 310 break;
mbed_official 256:76fd9a263045 311 }
mbed_official 256:76fd9a263045 312
mbed_official 256:76fd9a263045 313 return ack;
mbed_official 256:76fd9a263045 314 }
mbed_official 256:76fd9a263045 315
mbed_official 256:76fd9a263045 316 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
mbed_official 256:76fd9a263045 317 if (enable_slave != 0) {
mbed_official 256:76fd9a263045 318 i2c_conclr(obj, 1, 1, 1, 0);
mbed_official 256:76fd9a263045 319 i2c_conset(obj, 0, 0, 0, 1);
mbed_official 256:76fd9a263045 320 } else {
mbed_official 256:76fd9a263045 321 i2c_conclr(obj, 1, 1, 1, 1);
mbed_official 256:76fd9a263045 322 }
mbed_official 256:76fd9a263045 323 }
mbed_official 256:76fd9a263045 324
mbed_official 256:76fd9a263045 325 int i2c_slave_receive(i2c_t *obj) {
mbed_official 256:76fd9a263045 326 int status;
mbed_official 256:76fd9a263045 327 int retval;
mbed_official 256:76fd9a263045 328
mbed_official 256:76fd9a263045 329 status = i2c_status(obj);
mbed_official 256:76fd9a263045 330 switch(status) {
mbed_official 256:76fd9a263045 331 case 0x60: retval = 3; break;
mbed_official 256:76fd9a263045 332 case 0x70: retval = 2; break;
mbed_official 256:76fd9a263045 333 case 0xA8: retval = 1; break;
mbed_official 256:76fd9a263045 334 default : retval = 0; break;
mbed_official 256:76fd9a263045 335 }
mbed_official 256:76fd9a263045 336
mbed_official 256:76fd9a263045 337 return(retval);
mbed_official 256:76fd9a263045 338 }
mbed_official 256:76fd9a263045 339
mbed_official 256:76fd9a263045 340 int i2c_slave_read(i2c_t *obj, char *data, int length) {
mbed_official 256:76fd9a263045 341 int count = 0;
mbed_official 256:76fd9a263045 342 int status;
mbed_official 256:76fd9a263045 343
mbed_official 256:76fd9a263045 344 do {
mbed_official 256:76fd9a263045 345 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 346 i2c_wait_SI(obj);
mbed_official 256:76fd9a263045 347 status = i2c_status(obj);
mbed_official 256:76fd9a263045 348 if((status == 0x80) || (status == 0x90)) {
mbed_official 256:76fd9a263045 349 data[count] = I2C_DAT(obj) & 0xFF;
mbed_official 256:76fd9a263045 350 }
mbed_official 256:76fd9a263045 351 count++;
mbed_official 256:76fd9a263045 352 } while (((status == 0x80) || (status == 0x90) ||
mbed_official 256:76fd9a263045 353 (status == 0x060) || (status == 0x70)) && (count < length));
mbed_official 256:76fd9a263045 354
mbed_official 256:76fd9a263045 355 if(status != 0xA0) {
mbed_official 256:76fd9a263045 356 i2c_stop(obj);
mbed_official 256:76fd9a263045 357 }
mbed_official 256:76fd9a263045 358
mbed_official 256:76fd9a263045 359 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 360
mbed_official 256:76fd9a263045 361 return count;
mbed_official 256:76fd9a263045 362 }
mbed_official 256:76fd9a263045 363
mbed_official 256:76fd9a263045 364 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
mbed_official 256:76fd9a263045 365 int count = 0;
mbed_official 256:76fd9a263045 366 int status;
mbed_official 256:76fd9a263045 367
mbed_official 256:76fd9a263045 368 if(length <= 0) {
mbed_official 256:76fd9a263045 369 return(0);
mbed_official 256:76fd9a263045 370 }
mbed_official 256:76fd9a263045 371
mbed_official 256:76fd9a263045 372 do {
mbed_official 256:76fd9a263045 373 status = i2c_do_write(obj, data[count], 0);
mbed_official 256:76fd9a263045 374 count++;
mbed_official 256:76fd9a263045 375 } while ((count < length) && (status == 0xB8));
mbed_official 256:76fd9a263045 376
mbed_official 256:76fd9a263045 377 if ((status != 0xC0) && (status != 0xC8)) {
mbed_official 256:76fd9a263045 378 i2c_stop(obj);
mbed_official 256:76fd9a263045 379 }
mbed_official 256:76fd9a263045 380
mbed_official 256:76fd9a263045 381 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 382
mbed_official 256:76fd9a263045 383 return(count);
mbed_official 256:76fd9a263045 384 }
mbed_official 256:76fd9a263045 385
mbed_official 256:76fd9a263045 386 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
mbed_official 256:76fd9a263045 387 uint32_t addr;
mbed_official 256:76fd9a263045 388
mbed_official 256:76fd9a263045 389 if ((idx >= 0) && (idx <= 3)) {
mbed_official 256:76fd9a263045 390 addr = ((uint32_t)obj->i2c) + I2C_addr_offset[0][idx];
mbed_official 256:76fd9a263045 391 *((uint32_t *) addr) = address & 0xFF;
mbed_official 256:76fd9a263045 392 addr = ((uint32_t)obj->i2c) + I2C_addr_offset[1][idx];
mbed_official 256:76fd9a263045 393 *((uint32_t *) addr) = mask & 0xFE;
mbed_official 256:76fd9a263045 394 }
mbed_official 256:76fd9a263045 395 }