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