BA / Mbed OS BaBoRo_test2
Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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