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