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