mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Aug 13 09:00:06 2014 +0100
Revision:
283:bf0f62a62bf4
Parent:
256:76fd9a263045
Child:
285:31249416b6f9
Synchronized with git revision 1409f2a090b8a657fa91fa2e734cbeb643cd56cb

Full URL: https://github.com/mbedmicro/mbed/commit/1409f2a090b8a657fa91fa2e734cbeb643cd56cb/

[HAL] LP4330_M4 i2c/spi update

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 256:76fd9a263045 21 #include "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 256:76fd9a263045 104 // If pins are not dedicated, set SCU functions
mbed_official 256:76fd9a263045 105 if (sda != P_DED) {
mbed_official 256:76fd9a263045 106 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 256:76fd9a263045 107 }
mbed_official 256:76fd9a263045 108 if (scl != P_DED) {
mbed_official 256:76fd9a263045 109 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 256:76fd9a263045 110 }
mbed_official 256:76fd9a263045 111 }
mbed_official 256:76fd9a263045 112
mbed_official 256:76fd9a263045 113 inline int i2c_start(i2c_t *obj) {
mbed_official 256:76fd9a263045 114 int status = 0;
mbed_official 256:76fd9a263045 115 // 8.1 Before master mode can be entered, I2CON must be initialised to:
mbed_official 256:76fd9a263045 116 // - I2EN STA STO SI AA - -
mbed_official 256:76fd9a263045 117 // - 1 0 0 0 x - -
mbed_official 256:76fd9a263045 118 // if AA = 0, it can't enter slave mode
mbed_official 256:76fd9a263045 119 i2c_conclr(obj, 1, 1, 1, 1);
mbed_official 256:76fd9a263045 120
mbed_official 256:76fd9a263045 121 // The master mode may now be entered by setting the STA bit
mbed_official 256:76fd9a263045 122 // this will generate a start condition when the bus becomes free
mbed_official 256:76fd9a263045 123 i2c_conset(obj, 1, 0, 0, 1);
mbed_official 256:76fd9a263045 124
mbed_official 256:76fd9a263045 125 i2c_wait_SI(obj);
mbed_official 256:76fd9a263045 126 status = i2c_status(obj);
mbed_official 256:76fd9a263045 127
mbed_official 256:76fd9a263045 128 // Clear start bit now transmitted, and interrupt bit
mbed_official 256:76fd9a263045 129 i2c_conclr(obj, 1, 0, 0, 0);
mbed_official 256:76fd9a263045 130 return status;
mbed_official 256:76fd9a263045 131 }
mbed_official 256:76fd9a263045 132
mbed_official 256:76fd9a263045 133 inline int i2c_stop(i2c_t *obj) {
mbed_official 256:76fd9a263045 134 int timeout = 0;
mbed_official 256:76fd9a263045 135
mbed_official 256:76fd9a263045 136 // write the stop bit
mbed_official 256:76fd9a263045 137 i2c_conset(obj, 0, 1, 0, 0);
mbed_official 256:76fd9a263045 138 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 139
mbed_official 256:76fd9a263045 140 // wait for STO bit to reset
mbed_official 256:76fd9a263045 141 while(I2C_CONSET(obj) & (1 << 4)) {
mbed_official 256:76fd9a263045 142 timeout ++;
mbed_official 256:76fd9a263045 143 if (timeout > 100000) return 1;
mbed_official 256:76fd9a263045 144 }
mbed_official 256:76fd9a263045 145
mbed_official 256:76fd9a263045 146 return 0;
mbed_official 256:76fd9a263045 147 }
mbed_official 256:76fd9a263045 148
mbed_official 256:76fd9a263045 149 static inline int i2c_do_write(i2c_t *obj, int value, uint8_t addr) {
mbed_official 256:76fd9a263045 150 // write the data
mbed_official 256:76fd9a263045 151 I2C_DAT(obj) = value;
mbed_official 256:76fd9a263045 152
mbed_official 256:76fd9a263045 153 // clear SI to init a send
mbed_official 256:76fd9a263045 154 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 155
mbed_official 256:76fd9a263045 156 // wait and return status
mbed_official 256:76fd9a263045 157 i2c_wait_SI(obj);
mbed_official 256:76fd9a263045 158 return i2c_status(obj);
mbed_official 256:76fd9a263045 159 }
mbed_official 256:76fd9a263045 160
mbed_official 256:76fd9a263045 161 static inline int i2c_do_read(i2c_t *obj, int last) {
mbed_official 256:76fd9a263045 162 // we are in state 0x40 (SLA+R tx'd) or 0x50 (data rx'd and ack)
mbed_official 256:76fd9a263045 163 if(last) {
mbed_official 256:76fd9a263045 164 i2c_conclr(obj, 0, 0, 0, 1); // send a NOT ACK
mbed_official 256:76fd9a263045 165 } else {
mbed_official 256:76fd9a263045 166 i2c_conset(obj, 0, 0, 0, 1); // send a ACK
mbed_official 256:76fd9a263045 167 }
mbed_official 256:76fd9a263045 168
mbed_official 256:76fd9a263045 169 // accept byte
mbed_official 256:76fd9a263045 170 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 171
mbed_official 256:76fd9a263045 172 // wait for it to arrive
mbed_official 256:76fd9a263045 173 i2c_wait_SI(obj);
mbed_official 256:76fd9a263045 174
mbed_official 256:76fd9a263045 175 // return the data
mbed_official 256:76fd9a263045 176 return (I2C_DAT(obj) & 0xFF);
mbed_official 256:76fd9a263045 177 }
mbed_official 256:76fd9a263045 178
mbed_official 256:76fd9a263045 179 void i2c_frequency(i2c_t *obj, int hz) {
mbed_official 256:76fd9a263045 180 // [TODO] set pclk to /4
mbed_official 256:76fd9a263045 181 uint32_t PCLK = SystemCoreClock / 4;
mbed_official 256:76fd9a263045 182
mbed_official 256:76fd9a263045 183 uint32_t pulse = PCLK / (hz * 2);
mbed_official 256:76fd9a263045 184
mbed_official 256:76fd9a263045 185 // I2C Rate
mbed_official 256:76fd9a263045 186 I2C_SCLL(obj, pulse);
mbed_official 256:76fd9a263045 187 I2C_SCLH(obj, pulse);
mbed_official 256:76fd9a263045 188 }
mbed_official 256:76fd9a263045 189
mbed_official 256:76fd9a263045 190 // The I2C does a read or a write as a whole operation
mbed_official 256:76fd9a263045 191 // There are two types of error conditions it can encounter
mbed_official 256:76fd9a263045 192 // 1) it can not obtain the bus
mbed_official 256:76fd9a263045 193 // 2) it gets error responses at part of the transmission
mbed_official 256:76fd9a263045 194 //
mbed_official 256:76fd9a263045 195 // We tackle them as follows:
mbed_official 256:76fd9a263045 196 // 1) we retry until we get the bus. we could have a "timeout" if we can not get it
mbed_official 256:76fd9a263045 197 // which basically turns it in to a 2)
mbed_official 256:76fd9a263045 198 // 2) on error, we use the standard error mechanisms to report/debug
mbed_official 256:76fd9a263045 199 //
mbed_official 256:76fd9a263045 200 // Therefore an I2C transaction should always complete. If it doesn't it is usually
mbed_official 256:76fd9a263045 201 // because something is setup wrong (e.g. wiring), and we don't need to programatically
mbed_official 256:76fd9a263045 202 // check for that
mbed_official 256:76fd9a263045 203
mbed_official 256:76fd9a263045 204 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
mbed_official 256:76fd9a263045 205 int count, status;
mbed_official 256:76fd9a263045 206
mbed_official 256:76fd9a263045 207 status = i2c_start(obj);
mbed_official 256:76fd9a263045 208
mbed_official 256:76fd9a263045 209 if ((status != 0x10) && (status != 0x08)) {
mbed_official 256:76fd9a263045 210 i2c_stop(obj);
mbed_official 256:76fd9a263045 211 return I2C_ERROR_BUS_BUSY;
mbed_official 256:76fd9a263045 212 }
mbed_official 256:76fd9a263045 213
mbed_official 256:76fd9a263045 214 status = i2c_do_write(obj, (address | 0x01), 1);
mbed_official 256:76fd9a263045 215 if (status != 0x40) {
mbed_official 256:76fd9a263045 216 i2c_stop(obj);
mbed_official 256:76fd9a263045 217 return I2C_ERROR_NO_SLAVE;
mbed_official 256:76fd9a263045 218 }
mbed_official 256:76fd9a263045 219
mbed_official 256:76fd9a263045 220 // Read in all except last byte
mbed_official 256:76fd9a263045 221 for (count = 0; count < (length - 1); count++) {
mbed_official 256:76fd9a263045 222 int value = i2c_do_read(obj, 0);
mbed_official 256:76fd9a263045 223 status = i2c_status(obj);
mbed_official 256:76fd9a263045 224 if (status != 0x50) {
mbed_official 256:76fd9a263045 225 i2c_stop(obj);
mbed_official 256:76fd9a263045 226 return count;
mbed_official 256:76fd9a263045 227 }
mbed_official 256:76fd9a263045 228 data[count] = (char) value;
mbed_official 256:76fd9a263045 229 }
mbed_official 256:76fd9a263045 230
mbed_official 256:76fd9a263045 231 // read in last byte
mbed_official 256:76fd9a263045 232 int value = i2c_do_read(obj, 1);
mbed_official 256:76fd9a263045 233 status = i2c_status(obj);
mbed_official 256:76fd9a263045 234 if (status != 0x58) {
mbed_official 256:76fd9a263045 235 i2c_stop(obj);
mbed_official 256:76fd9a263045 236 return length - 1;
mbed_official 256:76fd9a263045 237 }
mbed_official 256:76fd9a263045 238
mbed_official 256:76fd9a263045 239 data[count] = (char) value;
mbed_official 256:76fd9a263045 240
mbed_official 256:76fd9a263045 241 // If not repeated start, send stop.
mbed_official 256:76fd9a263045 242 if (stop) {
mbed_official 256:76fd9a263045 243 i2c_stop(obj);
mbed_official 256:76fd9a263045 244 }
mbed_official 256:76fd9a263045 245
mbed_official 256:76fd9a263045 246 return length;
mbed_official 256:76fd9a263045 247 }
mbed_official 256:76fd9a263045 248
mbed_official 256:76fd9a263045 249 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
mbed_official 256:76fd9a263045 250 int i, status;
mbed_official 256:76fd9a263045 251
mbed_official 256:76fd9a263045 252 status = i2c_start(obj);
mbed_official 256:76fd9a263045 253
mbed_official 256:76fd9a263045 254 if ((status != 0x10) && (status != 0x08)) {
mbed_official 256:76fd9a263045 255 i2c_stop(obj);
mbed_official 256:76fd9a263045 256 return I2C_ERROR_BUS_BUSY;
mbed_official 256:76fd9a263045 257 }
mbed_official 256:76fd9a263045 258
mbed_official 256:76fd9a263045 259 status = i2c_do_write(obj, (address & 0xFE), 1);
mbed_official 256:76fd9a263045 260 if (status != 0x18) {
mbed_official 256:76fd9a263045 261 i2c_stop(obj);
mbed_official 256:76fd9a263045 262 return I2C_ERROR_NO_SLAVE;
mbed_official 256:76fd9a263045 263 }
mbed_official 256:76fd9a263045 264
mbed_official 256:76fd9a263045 265 for (i=0; i<length; i++) {
mbed_official 256:76fd9a263045 266 status = i2c_do_write(obj, data[i], 0);
mbed_official 256:76fd9a263045 267 if(status != 0x28) {
mbed_official 256:76fd9a263045 268 i2c_stop(obj);
mbed_official 256:76fd9a263045 269 return i;
mbed_official 256:76fd9a263045 270 }
mbed_official 256:76fd9a263045 271 }
mbed_official 256:76fd9a263045 272
mbed_official 256:76fd9a263045 273 // clearing the serial interrupt here might cause an unintended rewrite of the last byte
mbed_official 256:76fd9a263045 274 // see also issue report https://mbed.org/users/mbed_official/code/mbed/issues/1
mbed_official 256:76fd9a263045 275 // i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 276
mbed_official 256:76fd9a263045 277 // If not repeated start, send stop.
mbed_official 256:76fd9a263045 278 if (stop) {
mbed_official 256:76fd9a263045 279 i2c_stop(obj);
mbed_official 256:76fd9a263045 280 }
mbed_official 256:76fd9a263045 281
mbed_official 256:76fd9a263045 282 return length;
mbed_official 256:76fd9a263045 283 }
mbed_official 256:76fd9a263045 284
mbed_official 256:76fd9a263045 285 void i2c_reset(i2c_t *obj) {
mbed_official 256:76fd9a263045 286 i2c_stop(obj);
mbed_official 256:76fd9a263045 287 }
mbed_official 256:76fd9a263045 288
mbed_official 256:76fd9a263045 289 int i2c_byte_read(i2c_t *obj, int last) {
mbed_official 256:76fd9a263045 290 return (i2c_do_read(obj, last) & 0xFF);
mbed_official 256:76fd9a263045 291 }
mbed_official 256:76fd9a263045 292
mbed_official 256:76fd9a263045 293 int i2c_byte_write(i2c_t *obj, int data) {
mbed_official 256:76fd9a263045 294 int ack;
mbed_official 256:76fd9a263045 295 int status = i2c_do_write(obj, (data & 0xFF), 0);
mbed_official 256:76fd9a263045 296
mbed_official 256:76fd9a263045 297 switch(status) {
mbed_official 256:76fd9a263045 298 case 0x18: case 0x28: // Master transmit ACKs
mbed_official 256:76fd9a263045 299 ack = 1;
mbed_official 256:76fd9a263045 300 break;
mbed_official 256:76fd9a263045 301 case 0x40: // Master receive address transmitted ACK
mbed_official 256:76fd9a263045 302 ack = 1;
mbed_official 256:76fd9a263045 303 break;
mbed_official 256:76fd9a263045 304 case 0xB8: // Slave transmit ACK
mbed_official 256:76fd9a263045 305 ack = 1;
mbed_official 256:76fd9a263045 306 break;
mbed_official 256:76fd9a263045 307 default:
mbed_official 256:76fd9a263045 308 ack = 0;
mbed_official 256:76fd9a263045 309 break;
mbed_official 256:76fd9a263045 310 }
mbed_official 256:76fd9a263045 311
mbed_official 256:76fd9a263045 312 return ack;
mbed_official 256:76fd9a263045 313 }
mbed_official 256:76fd9a263045 314
mbed_official 256:76fd9a263045 315 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
mbed_official 256:76fd9a263045 316 if (enable_slave != 0) {
mbed_official 256:76fd9a263045 317 i2c_conclr(obj, 1, 1, 1, 0);
mbed_official 256:76fd9a263045 318 i2c_conset(obj, 0, 0, 0, 1);
mbed_official 256:76fd9a263045 319 } else {
mbed_official 256:76fd9a263045 320 i2c_conclr(obj, 1, 1, 1, 1);
mbed_official 256:76fd9a263045 321 }
mbed_official 256:76fd9a263045 322 }
mbed_official 256:76fd9a263045 323
mbed_official 256:76fd9a263045 324 int i2c_slave_receive(i2c_t *obj) {
mbed_official 256:76fd9a263045 325 int status;
mbed_official 256:76fd9a263045 326 int retval;
mbed_official 256:76fd9a263045 327
mbed_official 256:76fd9a263045 328 status = i2c_status(obj);
mbed_official 256:76fd9a263045 329 switch(status) {
mbed_official 256:76fd9a263045 330 case 0x60: retval = 3; break;
mbed_official 256:76fd9a263045 331 case 0x70: retval = 2; break;
mbed_official 256:76fd9a263045 332 case 0xA8: retval = 1; break;
mbed_official 256:76fd9a263045 333 default : retval = 0; break;
mbed_official 256:76fd9a263045 334 }
mbed_official 256:76fd9a263045 335
mbed_official 256:76fd9a263045 336 return(retval);
mbed_official 256:76fd9a263045 337 }
mbed_official 256:76fd9a263045 338
mbed_official 256:76fd9a263045 339 int i2c_slave_read(i2c_t *obj, char *data, int length) {
mbed_official 256:76fd9a263045 340 int count = 0;
mbed_official 256:76fd9a263045 341 int status;
mbed_official 256:76fd9a263045 342
mbed_official 256:76fd9a263045 343 do {
mbed_official 256:76fd9a263045 344 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 345 i2c_wait_SI(obj);
mbed_official 256:76fd9a263045 346 status = i2c_status(obj);
mbed_official 256:76fd9a263045 347 if((status == 0x80) || (status == 0x90)) {
mbed_official 256:76fd9a263045 348 data[count] = I2C_DAT(obj) & 0xFF;
mbed_official 256:76fd9a263045 349 }
mbed_official 256:76fd9a263045 350 count++;
mbed_official 256:76fd9a263045 351 } while (((status == 0x80) || (status == 0x90) ||
mbed_official 256:76fd9a263045 352 (status == 0x060) || (status == 0x70)) && (count < length));
mbed_official 256:76fd9a263045 353
mbed_official 256:76fd9a263045 354 if(status != 0xA0) {
mbed_official 256:76fd9a263045 355 i2c_stop(obj);
mbed_official 256:76fd9a263045 356 }
mbed_official 256:76fd9a263045 357
mbed_official 256:76fd9a263045 358 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 359
mbed_official 256:76fd9a263045 360 return count;
mbed_official 256:76fd9a263045 361 }
mbed_official 256:76fd9a263045 362
mbed_official 256:76fd9a263045 363 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
mbed_official 256:76fd9a263045 364 int count = 0;
mbed_official 256:76fd9a263045 365 int status;
mbed_official 256:76fd9a263045 366
mbed_official 256:76fd9a263045 367 if(length <= 0) {
mbed_official 256:76fd9a263045 368 return(0);
mbed_official 256:76fd9a263045 369 }
mbed_official 256:76fd9a263045 370
mbed_official 256:76fd9a263045 371 do {
mbed_official 256:76fd9a263045 372 status = i2c_do_write(obj, data[count], 0);
mbed_official 256:76fd9a263045 373 count++;
mbed_official 256:76fd9a263045 374 } while ((count < length) && (status == 0xB8));
mbed_official 256:76fd9a263045 375
mbed_official 256:76fd9a263045 376 if ((status != 0xC0) && (status != 0xC8)) {
mbed_official 256:76fd9a263045 377 i2c_stop(obj);
mbed_official 256:76fd9a263045 378 }
mbed_official 256:76fd9a263045 379
mbed_official 256:76fd9a263045 380 i2c_clear_SI(obj);
mbed_official 256:76fd9a263045 381
mbed_official 256:76fd9a263045 382 return(count);
mbed_official 256:76fd9a263045 383 }
mbed_official 256:76fd9a263045 384
mbed_official 256:76fd9a263045 385 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
mbed_official 256:76fd9a263045 386 uint32_t addr;
mbed_official 256:76fd9a263045 387
mbed_official 256:76fd9a263045 388 if ((idx >= 0) && (idx <= 3)) {
mbed_official 256:76fd9a263045 389 addr = ((uint32_t)obj->i2c) + I2C_addr_offset[0][idx];
mbed_official 256:76fd9a263045 390 *((uint32_t *) addr) = address & 0xFF;
mbed_official 256:76fd9a263045 391 addr = ((uint32_t)obj->i2c) + I2C_addr_offset[1][idx];
mbed_official 256:76fd9a263045 392 *((uint32_t *) addr) = mask & 0xFE;
mbed_official 256:76fd9a263045 393 }
mbed_official 256:76fd9a263045 394 }