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