mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_Freescale/TARGET_K20D5M/i2c_api.c@178:a873352662d8, 2014-05-07 (annotated)
- Committer:
- mbed_official
- Date:
- Wed May 07 09:00:07 2014 +0100
- Revision:
- 178:a873352662d8
- Parent:
- 73:299c67215126
- Child:
- 180:097147d37470
Synchronized with git revision 42a3edb815167b2aed138226781ff2ebcac662e4
Full URL: https://github.com/mbedmicro/mbed/commit/42a3edb815167b2aed138226781ff2ebcac662e4/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 68:41613245dfd7 | 1 | /* mbed Microcontroller Library |
mbed_official | 68:41613245dfd7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 68:41613245dfd7 | 3 | * |
mbed_official | 68:41613245dfd7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 68:41613245dfd7 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 68:41613245dfd7 | 6 | * You may obtain a copy of the License at |
mbed_official | 68:41613245dfd7 | 7 | * |
mbed_official | 68:41613245dfd7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 68:41613245dfd7 | 9 | * |
mbed_official | 68:41613245dfd7 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 68:41613245dfd7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 68:41613245dfd7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 68:41613245dfd7 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 68:41613245dfd7 | 14 | * limitations under the License. |
mbed_official | 68:41613245dfd7 | 15 | */ |
mbed_official | 68:41613245dfd7 | 16 | #include "i2c_api.h" |
mbed_official | 68:41613245dfd7 | 17 | |
mbed_official | 68:41613245dfd7 | 18 | #include "cmsis.h" |
mbed_official | 68:41613245dfd7 | 19 | #include "pinmap.h" |
mbed_official | 68:41613245dfd7 | 20 | #include "error.h" |
mbed_official | 73:299c67215126 | 21 | #include "clk_freqs.h" |
mbed_official | 68:41613245dfd7 | 22 | |
mbed_official | 68:41613245dfd7 | 23 | static const PinMap PinMap_I2C_SDA[] = { |
mbed_official | 68:41613245dfd7 | 24 | {PTB1, I2C_0, 2}, |
mbed_official | 68:41613245dfd7 | 25 | {PTB3, I2C_0, 2}, |
mbed_official | 68:41613245dfd7 | 26 | {NC , NC , 0} |
mbed_official | 68:41613245dfd7 | 27 | }; |
mbed_official | 68:41613245dfd7 | 28 | |
mbed_official | 68:41613245dfd7 | 29 | static const PinMap PinMap_I2C_SCL[] = { |
mbed_official | 68:41613245dfd7 | 30 | {PTB0, I2C_0, 2}, |
mbed_official | 68:41613245dfd7 | 31 | {PTB2, I2C_0, 2}, |
mbed_official | 68:41613245dfd7 | 32 | {NC , NC, 0} |
mbed_official | 68:41613245dfd7 | 33 | }; |
mbed_official | 68:41613245dfd7 | 34 | |
mbed_official | 68:41613245dfd7 | 35 | static const uint16_t ICR[0x40] = { |
mbed_official | 68:41613245dfd7 | 36 | 20, 22, 24, 26, 28, |
mbed_official | 68:41613245dfd7 | 37 | 30, 34, 40, 28, 32, |
mbed_official | 68:41613245dfd7 | 38 | 36, 40, 44, 48, 56, |
mbed_official | 68:41613245dfd7 | 39 | 68, 48, 56, 64, 72, |
mbed_official | 68:41613245dfd7 | 40 | 80, 88, 104, 128, 80, |
mbed_official | 68:41613245dfd7 | 41 | 96, 112, 128, 144, 160, |
mbed_official | 68:41613245dfd7 | 42 | 192, 240, 160, 192, 224, |
mbed_official | 68:41613245dfd7 | 43 | 256, 288, 320, 384, 480, |
mbed_official | 68:41613245dfd7 | 44 | 320, 384, 448, 512, 576, |
mbed_official | 68:41613245dfd7 | 45 | 640, 768, 960, 640, 768, |
mbed_official | 68:41613245dfd7 | 46 | 896, 1024, 1152, 1280, 1536, |
mbed_official | 68:41613245dfd7 | 47 | 1920, 1280, 1536, 1792, 2048, |
mbed_official | 68:41613245dfd7 | 48 | 2304, 2560, 3072, 3840 |
mbed_official | 68:41613245dfd7 | 49 | }; |
mbed_official | 68:41613245dfd7 | 50 | |
mbed_official | 68:41613245dfd7 | 51 | |
mbed_official | 68:41613245dfd7 | 52 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) { |
mbed_official | 68:41613245dfd7 | 53 | // determine the I2C to use |
mbed_official | 68:41613245dfd7 | 54 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
mbed_official | 68:41613245dfd7 | 55 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
mbed_official | 68:41613245dfd7 | 56 | obj->i2c = (I2C_Type*)pinmap_merge(i2c_sda, i2c_scl); |
mbed_official | 68:41613245dfd7 | 57 | if ((int)obj->i2c == NC) |
mbed_official | 68:41613245dfd7 | 58 | error("I2C pin mapping failed"); |
mbed_official | 68:41613245dfd7 | 59 | |
mbed_official | 68:41613245dfd7 | 60 | SIM->SCGC4 |= SIM_SCGC4_I2C0_MASK; |
mbed_official | 68:41613245dfd7 | 61 | SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK; |
mbed_official | 68:41613245dfd7 | 62 | |
mbed_official | 68:41613245dfd7 | 63 | // set default frequency at 100k |
mbed_official | 68:41613245dfd7 | 64 | i2c_frequency(obj, 100000); |
mbed_official | 68:41613245dfd7 | 65 | |
mbed_official | 68:41613245dfd7 | 66 | // enable I2C interface |
mbed_official | 68:41613245dfd7 | 67 | obj->i2c->C1 |= 0x80; |
mbed_official | 68:41613245dfd7 | 68 | |
mbed_official | 68:41613245dfd7 | 69 | pinmap_pinout(sda, PinMap_I2C_SDA); |
mbed_official | 68:41613245dfd7 | 70 | pinmap_pinout(scl, PinMap_I2C_SCL); |
mbed_official | 68:41613245dfd7 | 71 | } |
mbed_official | 68:41613245dfd7 | 72 | |
mbed_official | 68:41613245dfd7 | 73 | int i2c_start(i2c_t *obj) { |
mbed_official | 68:41613245dfd7 | 74 | // if we are in the middle of a transaction |
mbed_official | 68:41613245dfd7 | 75 | // activate the repeat_start flag |
mbed_official | 68:41613245dfd7 | 76 | if (obj->i2c->S & I2C_S_BUSY_MASK) { |
mbed_official | 68:41613245dfd7 | 77 | obj->i2c->C1 |= 0x04; |
mbed_official | 68:41613245dfd7 | 78 | } else { |
mbed_official | 68:41613245dfd7 | 79 | obj->i2c->C1 |= I2C_C1_MST_MASK; |
mbed_official | 68:41613245dfd7 | 80 | obj->i2c->C1 |= I2C_C1_TX_MASK; |
mbed_official | 68:41613245dfd7 | 81 | } |
mbed_official | 68:41613245dfd7 | 82 | return 0; |
mbed_official | 68:41613245dfd7 | 83 | } |
mbed_official | 68:41613245dfd7 | 84 | |
mbed_official | 68:41613245dfd7 | 85 | int i2c_stop(i2c_t *obj) { |
mbed_official | 68:41613245dfd7 | 86 | volatile uint32_t n = 0; |
mbed_official | 68:41613245dfd7 | 87 | obj->i2c->C1 &= ~I2C_C1_MST_MASK; |
mbed_official | 68:41613245dfd7 | 88 | obj->i2c->C1 &= ~I2C_C1_TX_MASK; |
mbed_official | 68:41613245dfd7 | 89 | |
mbed_official | 68:41613245dfd7 | 90 | // It seems that there are timing problems |
mbed_official | 68:41613245dfd7 | 91 | // when there is no waiting time after a STOP. |
mbed_official | 68:41613245dfd7 | 92 | // This wait is also included on the samples |
mbed_official | 68:41613245dfd7 | 93 | // code provided with the freedom board |
mbed_official | 68:41613245dfd7 | 94 | for (n = 0; n < 100; n++) |
mbed_official | 68:41613245dfd7 | 95 | __NOP(); |
mbed_official | 68:41613245dfd7 | 96 | return 0; |
mbed_official | 68:41613245dfd7 | 97 | } |
mbed_official | 68:41613245dfd7 | 98 | |
mbed_official | 68:41613245dfd7 | 99 | static int timeout_status_poll(i2c_t *obj, uint32_t mask) { |
mbed_official | 68:41613245dfd7 | 100 | uint32_t i, timeout = 1000; |
mbed_official | 68:41613245dfd7 | 101 | |
mbed_official | 68:41613245dfd7 | 102 | for (i = 0; i < timeout; i++) { |
mbed_official | 68:41613245dfd7 | 103 | if (obj->i2c->S & mask) |
mbed_official | 68:41613245dfd7 | 104 | return 0; |
mbed_official | 68:41613245dfd7 | 105 | } |
mbed_official | 68:41613245dfd7 | 106 | |
mbed_official | 68:41613245dfd7 | 107 | return 1; |
mbed_official | 68:41613245dfd7 | 108 | } |
mbed_official | 68:41613245dfd7 | 109 | |
mbed_official | 68:41613245dfd7 | 110 | // this function waits the end of a tx transfer and return the status of the transaction: |
mbed_official | 68:41613245dfd7 | 111 | // 0: OK ack received |
mbed_official | 68:41613245dfd7 | 112 | // 1: OK ack not received |
mbed_official | 68:41613245dfd7 | 113 | // 2: failure |
mbed_official | 68:41613245dfd7 | 114 | static int i2c_wait_end_tx_transfer(i2c_t *obj) { |
mbed_official | 68:41613245dfd7 | 115 | |
mbed_official | 68:41613245dfd7 | 116 | // wait for the interrupt flag |
mbed_official | 68:41613245dfd7 | 117 | if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) { |
mbed_official | 68:41613245dfd7 | 118 | return 2; |
mbed_official | 68:41613245dfd7 | 119 | } |
mbed_official | 68:41613245dfd7 | 120 | |
mbed_official | 68:41613245dfd7 | 121 | obj->i2c->S |= I2C_S_IICIF_MASK; |
mbed_official | 68:41613245dfd7 | 122 | |
mbed_official | 68:41613245dfd7 | 123 | // wait transfer complete |
mbed_official | 68:41613245dfd7 | 124 | if (timeout_status_poll(obj, I2C_S_TCF_MASK)) { |
mbed_official | 68:41613245dfd7 | 125 | return 2; |
mbed_official | 68:41613245dfd7 | 126 | } |
mbed_official | 68:41613245dfd7 | 127 | |
mbed_official | 68:41613245dfd7 | 128 | // check if we received the ACK or not |
mbed_official | 68:41613245dfd7 | 129 | return obj->i2c->S & I2C_S_RXAK_MASK ? 1 : 0; |
mbed_official | 68:41613245dfd7 | 130 | } |
mbed_official | 68:41613245dfd7 | 131 | |
mbed_official | 68:41613245dfd7 | 132 | // this function waits the end of a rx transfer and return the status of the transaction: |
mbed_official | 68:41613245dfd7 | 133 | // 0: OK |
mbed_official | 68:41613245dfd7 | 134 | // 1: failure |
mbed_official | 68:41613245dfd7 | 135 | static int i2c_wait_end_rx_transfer(i2c_t *obj) { |
mbed_official | 68:41613245dfd7 | 136 | // wait for the end of the rx transfer |
mbed_official | 68:41613245dfd7 | 137 | if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) { |
mbed_official | 68:41613245dfd7 | 138 | return 1; |
mbed_official | 68:41613245dfd7 | 139 | } |
mbed_official | 68:41613245dfd7 | 140 | |
mbed_official | 68:41613245dfd7 | 141 | obj->i2c->S |= I2C_S_IICIF_MASK; |
mbed_official | 68:41613245dfd7 | 142 | |
mbed_official | 68:41613245dfd7 | 143 | return 0; |
mbed_official | 68:41613245dfd7 | 144 | } |
mbed_official | 68:41613245dfd7 | 145 | |
mbed_official | 68:41613245dfd7 | 146 | static void i2c_send_nack(i2c_t *obj) { |
mbed_official | 68:41613245dfd7 | 147 | obj->i2c->C1 |= I2C_C1_TXAK_MASK; // NACK |
mbed_official | 68:41613245dfd7 | 148 | } |
mbed_official | 68:41613245dfd7 | 149 | |
mbed_official | 68:41613245dfd7 | 150 | static void i2c_send_ack(i2c_t *obj) { |
mbed_official | 68:41613245dfd7 | 151 | obj->i2c->C1 &= ~I2C_C1_TXAK_MASK; // ACK |
mbed_official | 68:41613245dfd7 | 152 | } |
mbed_official | 68:41613245dfd7 | 153 | |
mbed_official | 68:41613245dfd7 | 154 | static int i2c_do_write(i2c_t *obj, int value) { |
mbed_official | 68:41613245dfd7 | 155 | // write the data |
mbed_official | 68:41613245dfd7 | 156 | obj->i2c->D = value; |
mbed_official | 68:41613245dfd7 | 157 | |
mbed_official | 68:41613245dfd7 | 158 | // init and wait the end of the transfer |
mbed_official | 68:41613245dfd7 | 159 | return i2c_wait_end_tx_transfer(obj); |
mbed_official | 68:41613245dfd7 | 160 | } |
mbed_official | 68:41613245dfd7 | 161 | |
mbed_official | 68:41613245dfd7 | 162 | static int i2c_do_read(i2c_t *obj, char * data, int last) { |
mbed_official | 73:299c67215126 | 163 | if (last) { |
mbed_official | 68:41613245dfd7 | 164 | i2c_send_nack(obj); |
mbed_official | 73:299c67215126 | 165 | } else { |
mbed_official | 68:41613245dfd7 | 166 | i2c_send_ack(obj); |
mbed_official | 73:299c67215126 | 167 | } |
mbed_official | 68:41613245dfd7 | 168 | |
mbed_official | 68:41613245dfd7 | 169 | *data = (obj->i2c->D & 0xFF); |
mbed_official | 68:41613245dfd7 | 170 | |
mbed_official | 68:41613245dfd7 | 171 | // start rx transfer and wait the end of the transfer |
mbed_official | 68:41613245dfd7 | 172 | return i2c_wait_end_rx_transfer(obj); |
mbed_official | 68:41613245dfd7 | 173 | } |
mbed_official | 68:41613245dfd7 | 174 | |
mbed_official | 68:41613245dfd7 | 175 | void i2c_frequency(i2c_t *obj, int hz) { |
mbed_official | 68:41613245dfd7 | 176 | uint8_t icr = 0; |
mbed_official | 68:41613245dfd7 | 177 | uint8_t mult = 0; |
mbed_official | 68:41613245dfd7 | 178 | uint32_t error = 0; |
mbed_official | 68:41613245dfd7 | 179 | uint32_t p_error = 0xffffffff; |
mbed_official | 68:41613245dfd7 | 180 | uint32_t ref = 0; |
mbed_official | 68:41613245dfd7 | 181 | uint8_t i, j; |
mbed_official | 68:41613245dfd7 | 182 | // bus clk |
mbed_official | 73:299c67215126 | 183 | uint32_t PCLK = bus_frequency(); |
mbed_official | 68:41613245dfd7 | 184 | uint32_t pulse = PCLK / (hz * 2); |
mbed_official | 68:41613245dfd7 | 185 | |
mbed_official | 68:41613245dfd7 | 186 | // we look for the values that minimize the error |
mbed_official | 68:41613245dfd7 | 187 | |
mbed_official | 68:41613245dfd7 | 188 | // test all the MULT values |
mbed_official | 68:41613245dfd7 | 189 | for (i = 1; i < 5; i*=2) { |
mbed_official | 68:41613245dfd7 | 190 | for (j = 0; j < 0x40; j++) { |
mbed_official | 68:41613245dfd7 | 191 | ref = PCLK / (i*ICR[j]); |
mbed_official | 68:41613245dfd7 | 192 | if (ref > (uint32_t)hz) |
mbed_official | 68:41613245dfd7 | 193 | continue; |
mbed_official | 68:41613245dfd7 | 194 | error = hz - ref; |
mbed_official | 68:41613245dfd7 | 195 | if (error < p_error) { |
mbed_official | 68:41613245dfd7 | 196 | icr = j; |
mbed_official | 68:41613245dfd7 | 197 | mult = i/2; |
mbed_official | 68:41613245dfd7 | 198 | p_error = error; |
mbed_official | 68:41613245dfd7 | 199 | } |
mbed_official | 68:41613245dfd7 | 200 | } |
mbed_official | 68:41613245dfd7 | 201 | } |
mbed_official | 68:41613245dfd7 | 202 | pulse = icr | (mult << 6); |
mbed_official | 68:41613245dfd7 | 203 | |
mbed_official | 68:41613245dfd7 | 204 | // I2C Rate |
mbed_official | 68:41613245dfd7 | 205 | obj->i2c->F = pulse; |
mbed_official | 68:41613245dfd7 | 206 | } |
mbed_official | 68:41613245dfd7 | 207 | |
mbed_official | 68:41613245dfd7 | 208 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { |
mbed_official | 68:41613245dfd7 | 209 | int count; |
mbed_official | 68:41613245dfd7 | 210 | char dummy_read, *ptr; |
mbed_official | 68:41613245dfd7 | 211 | |
mbed_official | 68:41613245dfd7 | 212 | if (i2c_start(obj)) { |
mbed_official | 68:41613245dfd7 | 213 | i2c_stop(obj); |
mbed_official | 68:41613245dfd7 | 214 | return I2C_ERROR_BUS_BUSY; |
mbed_official | 68:41613245dfd7 | 215 | } |
mbed_official | 68:41613245dfd7 | 216 | |
mbed_official | 68:41613245dfd7 | 217 | if (i2c_do_write(obj, (address | 0x01))) { |
mbed_official | 68:41613245dfd7 | 218 | i2c_stop(obj); |
mbed_official | 68:41613245dfd7 | 219 | return I2C_ERROR_NO_SLAVE; |
mbed_official | 68:41613245dfd7 | 220 | } |
mbed_official | 68:41613245dfd7 | 221 | |
mbed_official | 68:41613245dfd7 | 222 | // set rx mode |
mbed_official | 68:41613245dfd7 | 223 | obj->i2c->C1 &= ~I2C_C1_TX_MASK; |
mbed_official | 68:41613245dfd7 | 224 | |
mbed_official | 68:41613245dfd7 | 225 | // Read in bytes |
mbed_official | 68:41613245dfd7 | 226 | for (count = 0; count < (length); count++) { |
mbed_official | 68:41613245dfd7 | 227 | ptr = (count == 0) ? &dummy_read : &data[count - 1]; |
mbed_official | 68:41613245dfd7 | 228 | uint8_t stop_ = (count == (length - 1)) ? 1 : 0; |
mbed_official | 68:41613245dfd7 | 229 | if (i2c_do_read(obj, ptr, stop_)) { |
mbed_official | 68:41613245dfd7 | 230 | i2c_stop(obj); |
mbed_official | 68:41613245dfd7 | 231 | return count; |
mbed_official | 68:41613245dfd7 | 232 | } |
mbed_official | 68:41613245dfd7 | 233 | } |
mbed_official | 68:41613245dfd7 | 234 | |
mbed_official | 68:41613245dfd7 | 235 | // If not repeated start, send stop. |
mbed_official | 73:299c67215126 | 236 | if (stop) |
mbed_official | 68:41613245dfd7 | 237 | i2c_stop(obj); |
mbed_official | 68:41613245dfd7 | 238 | |
mbed_official | 68:41613245dfd7 | 239 | // last read |
mbed_official | 68:41613245dfd7 | 240 | data[count-1] = obj->i2c->D; |
mbed_official | 68:41613245dfd7 | 241 | |
mbed_official | 68:41613245dfd7 | 242 | return length; |
mbed_official | 68:41613245dfd7 | 243 | } |
mbed_official | 68:41613245dfd7 | 244 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { |
mbed_official | 68:41613245dfd7 | 245 | int i; |
mbed_official | 68:41613245dfd7 | 246 | |
mbed_official | 68:41613245dfd7 | 247 | if (i2c_start(obj)) { |
mbed_official | 68:41613245dfd7 | 248 | i2c_stop(obj); |
mbed_official | 68:41613245dfd7 | 249 | return I2C_ERROR_BUS_BUSY; |
mbed_official | 68:41613245dfd7 | 250 | } |
mbed_official | 68:41613245dfd7 | 251 | |
mbed_official | 68:41613245dfd7 | 252 | if (i2c_do_write(obj, (address & 0xFE))) { |
mbed_official | 68:41613245dfd7 | 253 | i2c_stop(obj); |
mbed_official | 68:41613245dfd7 | 254 | return I2C_ERROR_NO_SLAVE; |
mbed_official | 68:41613245dfd7 | 255 | } |
mbed_official | 68:41613245dfd7 | 256 | |
mbed_official | 68:41613245dfd7 | 257 | for (i = 0; i < length; i++) { |
mbed_official | 68:41613245dfd7 | 258 | if(i2c_do_write(obj, data[i])) { |
mbed_official | 68:41613245dfd7 | 259 | i2c_stop(obj); |
mbed_official | 68:41613245dfd7 | 260 | return i; |
mbed_official | 68:41613245dfd7 | 261 | } |
mbed_official | 68:41613245dfd7 | 262 | } |
mbed_official | 68:41613245dfd7 | 263 | |
mbed_official | 68:41613245dfd7 | 264 | if (stop) |
mbed_official | 68:41613245dfd7 | 265 | i2c_stop(obj); |
mbed_official | 68:41613245dfd7 | 266 | |
mbed_official | 68:41613245dfd7 | 267 | return length; |
mbed_official | 68:41613245dfd7 | 268 | } |
mbed_official | 68:41613245dfd7 | 269 | |
mbed_official | 68:41613245dfd7 | 270 | void i2c_reset(i2c_t *obj) { |
mbed_official | 68:41613245dfd7 | 271 | i2c_stop(obj); |
mbed_official | 68:41613245dfd7 | 272 | } |
mbed_official | 68:41613245dfd7 | 273 | |
mbed_official | 68:41613245dfd7 | 274 | int i2c_byte_read(i2c_t *obj, int last) { |
mbed_official | 68:41613245dfd7 | 275 | char data; |
mbed_official | 178:a873352662d8 | 276 | |
mbed_official | 68:41613245dfd7 | 277 | // set rx mode |
mbed_official | 68:41613245dfd7 | 278 | obj->i2c->C1 &= ~I2C_C1_TX_MASK; |
mbed_official | 178:a873352662d8 | 279 | |
mbed_official | 178:a873352662d8 | 280 | // Setup read |
mbed_official | 68:41613245dfd7 | 281 | i2c_do_read(obj, &data, last); |
mbed_official | 68:41613245dfd7 | 282 | |
mbed_official | 178:a873352662d8 | 283 | // set tx mode |
mbed_official | 178:a873352662d8 | 284 | obj->i2c->C1 |= I2C_C1_TX_MASK; |
mbed_official | 178:a873352662d8 | 285 | return obj->i2c->D; |
mbed_official | 68:41613245dfd7 | 286 | } |
mbed_official | 68:41613245dfd7 | 287 | |
mbed_official | 68:41613245dfd7 | 288 | int i2c_byte_write(i2c_t *obj, int data) { |
mbed_official | 68:41613245dfd7 | 289 | // set tx mode |
mbed_official | 68:41613245dfd7 | 290 | obj->i2c->C1 |= I2C_C1_TX_MASK; |
mbed_official | 68:41613245dfd7 | 291 | |
mbed_official | 68:41613245dfd7 | 292 | return !i2c_do_write(obj, (data & 0xFF)); |
mbed_official | 68:41613245dfd7 | 293 | } |
mbed_official | 68:41613245dfd7 | 294 | |
mbed_official | 68:41613245dfd7 | 295 | |
mbed_official | 68:41613245dfd7 | 296 | #if DEVICE_I2CSLAVE |
mbed_official | 68:41613245dfd7 | 297 | void i2c_slave_mode(i2c_t *obj, int enable_slave) { |
mbed_official | 68:41613245dfd7 | 298 | if (enable_slave) { |
mbed_official | 68:41613245dfd7 | 299 | // set slave mode |
mbed_official | 68:41613245dfd7 | 300 | obj->i2c->C1 &= ~I2C_C1_MST_MASK; |
mbed_official | 68:41613245dfd7 | 301 | obj->i2c->C1 |= I2C_C1_IICIE_MASK; |
mbed_official | 68:41613245dfd7 | 302 | } else { |
mbed_official | 68:41613245dfd7 | 303 | // set master mode |
mbed_official | 68:41613245dfd7 | 304 | obj->i2c->C1 |= I2C_C1_MST_MASK; |
mbed_official | 68:41613245dfd7 | 305 | } |
mbed_official | 68:41613245dfd7 | 306 | } |
mbed_official | 68:41613245dfd7 | 307 | |
mbed_official | 68:41613245dfd7 | 308 | int i2c_slave_receive(i2c_t *obj) { |
mbed_official | 68:41613245dfd7 | 309 | switch(obj->i2c->S) { |
mbed_official | 68:41613245dfd7 | 310 | // read addressed |
mbed_official | 68:41613245dfd7 | 311 | case 0xE6: |
mbed_official | 68:41613245dfd7 | 312 | return 1; |
mbed_official | 68:41613245dfd7 | 313 | // write addressed |
mbed_official | 68:41613245dfd7 | 314 | case 0xE2: |
mbed_official | 68:41613245dfd7 | 315 | return 3; |
mbed_official | 68:41613245dfd7 | 316 | default: |
mbed_official | 68:41613245dfd7 | 317 | return 0; |
mbed_official | 68:41613245dfd7 | 318 | } |
mbed_official | 68:41613245dfd7 | 319 | } |
mbed_official | 68:41613245dfd7 | 320 | |
mbed_official | 68:41613245dfd7 | 321 | int i2c_slave_read(i2c_t *obj, char *data, int length) { |
mbed_official | 68:41613245dfd7 | 322 | uint8_t dummy_read; |
mbed_official | 68:41613245dfd7 | 323 | uint8_t * ptr; |
mbed_official | 68:41613245dfd7 | 324 | int count; |
mbed_official | 68:41613245dfd7 | 325 | |
mbed_official | 68:41613245dfd7 | 326 | // set rx mode |
mbed_official | 68:41613245dfd7 | 327 | obj->i2c->C1 &= ~I2C_C1_TX_MASK; |
mbed_official | 68:41613245dfd7 | 328 | |
mbed_official | 68:41613245dfd7 | 329 | // first dummy read |
mbed_official | 68:41613245dfd7 | 330 | dummy_read = obj->i2c->D; |
mbed_official | 68:41613245dfd7 | 331 | if (i2c_wait_end_rx_transfer(obj)) |
mbed_official | 68:41613245dfd7 | 332 | return 0; |
mbed_official | 68:41613245dfd7 | 333 | |
mbed_official | 68:41613245dfd7 | 334 | // read address |
mbed_official | 68:41613245dfd7 | 335 | dummy_read = obj->i2c->D; |
mbed_official | 68:41613245dfd7 | 336 | if (i2c_wait_end_rx_transfer(obj)) |
mbed_official | 68:41613245dfd7 | 337 | return 0; |
mbed_official | 68:41613245dfd7 | 338 | |
mbed_official | 68:41613245dfd7 | 339 | // read (length - 1) bytes |
mbed_official | 68:41613245dfd7 | 340 | for (count = 0; count < (length - 1); count++) { |
mbed_official | 68:41613245dfd7 | 341 | data[count] = obj->i2c->D; |
mbed_official | 68:41613245dfd7 | 342 | if (i2c_wait_end_rx_transfer(obj)) |
mbed_official | 68:41613245dfd7 | 343 | return count; |
mbed_official | 68:41613245dfd7 | 344 | } |
mbed_official | 68:41613245dfd7 | 345 | |
mbed_official | 68:41613245dfd7 | 346 | // read last byte |
mbed_official | 68:41613245dfd7 | 347 | ptr = (length == 0) ? &dummy_read : (uint8_t *)&data[count]; |
mbed_official | 68:41613245dfd7 | 348 | *ptr = obj->i2c->D; |
mbed_official | 68:41613245dfd7 | 349 | |
mbed_official | 68:41613245dfd7 | 350 | return (length) ? (count + 1) : 0; |
mbed_official | 68:41613245dfd7 | 351 | } |
mbed_official | 68:41613245dfd7 | 352 | |
mbed_official | 68:41613245dfd7 | 353 | int i2c_slave_write(i2c_t *obj, const char *data, int length) { |
mbed_official | 68:41613245dfd7 | 354 | int i, count = 0; |
mbed_official | 68:41613245dfd7 | 355 | |
mbed_official | 68:41613245dfd7 | 356 | // set tx mode |
mbed_official | 68:41613245dfd7 | 357 | obj->i2c->C1 |= I2C_C1_TX_MASK; |
mbed_official | 68:41613245dfd7 | 358 | |
mbed_official | 68:41613245dfd7 | 359 | for (i = 0; i < length; i++) { |
mbed_official | 68:41613245dfd7 | 360 | if (i2c_do_write(obj, data[count++]) == 2) |
mbed_official | 68:41613245dfd7 | 361 | return i; |
mbed_official | 68:41613245dfd7 | 362 | } |
mbed_official | 68:41613245dfd7 | 363 | |
mbed_official | 68:41613245dfd7 | 364 | // set rx mode |
mbed_official | 68:41613245dfd7 | 365 | obj->i2c->C1 &= ~I2C_C1_TX_MASK; |
mbed_official | 68:41613245dfd7 | 366 | |
mbed_official | 68:41613245dfd7 | 367 | // dummy rx transfer needed |
mbed_official | 68:41613245dfd7 | 368 | // otherwise the master cannot generate a stop bit |
mbed_official | 68:41613245dfd7 | 369 | obj->i2c->D; |
mbed_official | 68:41613245dfd7 | 370 | if (i2c_wait_end_rx_transfer(obj) == 2) |
mbed_official | 68:41613245dfd7 | 371 | return count; |
mbed_official | 68:41613245dfd7 | 372 | |
mbed_official | 68:41613245dfd7 | 373 | return count; |
mbed_official | 68:41613245dfd7 | 374 | } |
mbed_official | 68:41613245dfd7 | 375 | |
mbed_official | 68:41613245dfd7 | 376 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { |
mbed_official | 68:41613245dfd7 | 377 | obj->i2c->A1 = address & 0xfe; |
mbed_official | 68:41613245dfd7 | 378 | } |
mbed_official | 68:41613245dfd7 | 379 | #endif |
mbed_official | 68:41613245dfd7 | 380 |