mbed library sources modified for open wear

Dependents:   openwear-lifelogger-example

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jul 29 18:45:06 2014 +0100
Revision:
267:8673334f2cbe
Parent:
265:9632ea190e16
Synchronized with git revision 1d586e1f8df5e4ff9eb4b8420095fd3f74426163

Full URL: https://github.com/mbedmicro/mbed/commit/1d586e1f8df5e4ff9eb4b8420095fd3f74426163/

Had duplicate set of api drivers in the directory - deleted

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 146:f64d43ff0c18 1 /* mbed Microcontroller Library
mbed_official 146:f64d43ff0c18 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 146:f64d43ff0c18 3 *
mbed_official 146:f64d43ff0c18 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 146:f64d43ff0c18 5 * you may not use this file except in compliance with the License.
mbed_official 146:f64d43ff0c18 6 * You may obtain a copy of the License at
mbed_official 146:f64d43ff0c18 7 *
mbed_official 146:f64d43ff0c18 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 146:f64d43ff0c18 9 *
mbed_official 146:f64d43ff0c18 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 146:f64d43ff0c18 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 146:f64d43ff0c18 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 146:f64d43ff0c18 13 * See the License for the specific language governing permissions and
mbed_official 146:f64d43ff0c18 14 * limitations under the License.
mbed_official 146:f64d43ff0c18 15 */
mbed_official 227:7bd0639b8911 16 #include "mbed_assert.h"
mbed_official 146:f64d43ff0c18 17 #include "i2c_api.h"
mbed_official 146:f64d43ff0c18 18
mbed_official 267:8673334f2cbe 19 #if DEVICE_I2C
mbed_official 267:8673334f2cbe 20
mbed_official 146:f64d43ff0c18 21 #include "cmsis.h"
mbed_official 146:f64d43ff0c18 22 #include "pinmap.h"
mbed_official 146:f64d43ff0c18 23 #include "fsl_clock_manager.h"
mbed_official 146:f64d43ff0c18 24 #include "fsl_i2c_hal.h"
mbed_official 180:097147d37470 25 #include "fsl_port_hal.h"
mbed_official 180:097147d37470 26 #include "fsl_sim_hal.h"
mbed_official 265:9632ea190e16 27 #include "PeripheralPins.h"
mbed_official 146:f64d43ff0c18 28
mbed_official 146:f64d43ff0c18 29 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
mbed_official 146:f64d43ff0c18 30 uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA);
mbed_official 146:f64d43ff0c18 31 uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);
mbed_official 146:f64d43ff0c18 32 obj->instance = pinmap_merge(i2c_sda, i2c_scl);
mbed_official 227:7bd0639b8911 33 MBED_ASSERT((int)obj->instance != NC);
mbed_official 146:f64d43ff0c18 34
mbed_official 146:f64d43ff0c18 35 clock_manager_set_gate(kClockModuleI2C, obj->instance, true);
mbed_official 180:097147d37470 36 clock_manager_set_gate(kClockModulePORT, sda >> GPIO_PORT_SHIFT, true);
mbed_official 180:097147d37470 37 clock_manager_set_gate(kClockModulePORT, scl >> GPIO_PORT_SHIFT, true);
mbed_official 146:f64d43ff0c18 38 i2c_hal_enable(obj->instance);
mbed_official 146:f64d43ff0c18 39 i2c_frequency(obj, 100000);
mbed_official 146:f64d43ff0c18 40
mbed_official 146:f64d43ff0c18 41 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 146:f64d43ff0c18 42 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 180:097147d37470 43 port_hal_configure_open_drain(sda >> GPIO_PORT_SHIFT, sda & 0xFF, true);
mbed_official 180:097147d37470 44 port_hal_configure_open_drain(scl >> GPIO_PORT_SHIFT, scl & 0xFF, true);
mbed_official 146:f64d43ff0c18 45 }
mbed_official 146:f64d43ff0c18 46
mbed_official 146:f64d43ff0c18 47 int i2c_start(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 48 i2c_hal_send_start(obj->instance);
mbed_official 146:f64d43ff0c18 49 return 0;
mbed_official 146:f64d43ff0c18 50 }
mbed_official 146:f64d43ff0c18 51
mbed_official 146:f64d43ff0c18 52 int i2c_stop(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 53 volatile uint32_t n = 0;
mbed_official 146:f64d43ff0c18 54 i2c_hal_send_stop(obj->instance);
mbed_official 146:f64d43ff0c18 55
mbed_official 146:f64d43ff0c18 56 // It seems that there are timing problems
mbed_official 146:f64d43ff0c18 57 // when there is no waiting time after a STOP.
mbed_official 146:f64d43ff0c18 58 // This wait is also included on the samples
mbed_official 146:f64d43ff0c18 59 // code provided with the freedom board
mbed_official 210:45934c3d812a 60 for (n = 0; n < 200; n++) __NOP();
mbed_official 146:f64d43ff0c18 61 return 0;
mbed_official 146:f64d43ff0c18 62 }
mbed_official 146:f64d43ff0c18 63
mbed_official 146:f64d43ff0c18 64 static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
mbed_official 182:242346c42295 65 uint32_t i, timeout = 100000;
mbed_official 146:f64d43ff0c18 66
mbed_official 146:f64d43ff0c18 67 for (i = 0; i < timeout; i++) {
mbed_official 146:f64d43ff0c18 68 if (HW_I2C_S_RD(obj->instance) & mask)
mbed_official 146:f64d43ff0c18 69 return 0;
mbed_official 146:f64d43ff0c18 70 }
mbed_official 146:f64d43ff0c18 71 return 1;
mbed_official 146:f64d43ff0c18 72 }
mbed_official 146:f64d43ff0c18 73
mbed_official 146:f64d43ff0c18 74 // this function waits the end of a tx transfer and return the status of the transaction:
mbed_official 146:f64d43ff0c18 75 // 0: OK ack received
mbed_official 146:f64d43ff0c18 76 // 1: OK ack not received
mbed_official 146:f64d43ff0c18 77 // 2: failure
mbed_official 146:f64d43ff0c18 78 static int i2c_wait_end_tx_transfer(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 79 // wait for the interrupt flag
mbed_official 146:f64d43ff0c18 80 if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) {
mbed_official 146:f64d43ff0c18 81 return 2;
mbed_official 146:f64d43ff0c18 82 }
mbed_official 146:f64d43ff0c18 83
mbed_official 146:f64d43ff0c18 84 i2c_hal_clear_interrupt(obj->instance);
mbed_official 146:f64d43ff0c18 85
mbed_official 146:f64d43ff0c18 86 // wait transfer complete
mbed_official 146:f64d43ff0c18 87 if (timeout_status_poll(obj, I2C_S_TCF_MASK)) {
mbed_official 146:f64d43ff0c18 88 return 2;
mbed_official 146:f64d43ff0c18 89 }
mbed_official 146:f64d43ff0c18 90
mbed_official 146:f64d43ff0c18 91 // check if we received the ACK or not
mbed_official 146:f64d43ff0c18 92 return i2c_hal_get_receive_ack(obj->instance) ? 0 : 1;
mbed_official 146:f64d43ff0c18 93 }
mbed_official 146:f64d43ff0c18 94
mbed_official 146:f64d43ff0c18 95 // this function waits the end of a rx transfer and return the status of the transaction:
mbed_official 146:f64d43ff0c18 96 // 0: OK
mbed_official 146:f64d43ff0c18 97 // 1: failure
mbed_official 146:f64d43ff0c18 98 static int i2c_wait_end_rx_transfer(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 99 // wait for the end of the rx transfer
mbed_official 146:f64d43ff0c18 100 if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) {
mbed_official 146:f64d43ff0c18 101 return 1;
mbed_official 146:f64d43ff0c18 102 }
mbed_official 146:f64d43ff0c18 103
mbed_official 146:f64d43ff0c18 104 i2c_hal_clear_interrupt(obj->instance);
mbed_official 146:f64d43ff0c18 105
mbed_official 146:f64d43ff0c18 106 return 0;
mbed_official 146:f64d43ff0c18 107 }
mbed_official 146:f64d43ff0c18 108
mbed_official 146:f64d43ff0c18 109 static int i2c_do_write(i2c_t *obj, int value) {
mbed_official 146:f64d43ff0c18 110 i2c_hal_write(obj->instance, value);
mbed_official 146:f64d43ff0c18 111
mbed_official 146:f64d43ff0c18 112 // init and wait the end of the transfer
mbed_official 146:f64d43ff0c18 113 return i2c_wait_end_tx_transfer(obj);
mbed_official 146:f64d43ff0c18 114 }
mbed_official 146:f64d43ff0c18 115
mbed_official 146:f64d43ff0c18 116 static int i2c_do_read(i2c_t *obj, char * data, int last) {
mbed_official 146:f64d43ff0c18 117 if (last) {
mbed_official 146:f64d43ff0c18 118 i2c_hal_send_nak(obj->instance);
mbed_official 146:f64d43ff0c18 119 } else {
mbed_official 146:f64d43ff0c18 120 i2c_hal_send_ack(obj->instance);
mbed_official 146:f64d43ff0c18 121 }
mbed_official 146:f64d43ff0c18 122
mbed_official 146:f64d43ff0c18 123 *data = (i2c_hal_read(obj->instance) & 0xFF);
mbed_official 146:f64d43ff0c18 124
mbed_official 146:f64d43ff0c18 125 // start rx transfer and wait the end of the transfer
mbed_official 146:f64d43ff0c18 126 return i2c_wait_end_rx_transfer(obj);
mbed_official 146:f64d43ff0c18 127 }
mbed_official 146:f64d43ff0c18 128
mbed_official 146:f64d43ff0c18 129 void i2c_frequency(i2c_t *obj, int hz) {
mbed_official 146:f64d43ff0c18 130 uint32_t busClock;
mbed_official 146:f64d43ff0c18 131
mbed_official 146:f64d43ff0c18 132 clock_manager_error_code_t error = clock_manager_get_frequency(kBusClock, &busClock);
mbed_official 146:f64d43ff0c18 133 if (error == kClockManagerSuccess) {
mbed_official 146:f64d43ff0c18 134 i2c_hal_set_baud(obj->instance, busClock, hz / 1000, NULL);
mbed_official 146:f64d43ff0c18 135 }
mbed_official 146:f64d43ff0c18 136 }
mbed_official 146:f64d43ff0c18 137
mbed_official 146:f64d43ff0c18 138 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
mbed_official 146:f64d43ff0c18 139 int count;
mbed_official 146:f64d43ff0c18 140 char dummy_read, *ptr;
mbed_official 146:f64d43ff0c18 141
mbed_official 146:f64d43ff0c18 142 if (i2c_start(obj)) {
mbed_official 146:f64d43ff0c18 143 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 144 return I2C_ERROR_BUS_BUSY;
mbed_official 146:f64d43ff0c18 145 }
mbed_official 146:f64d43ff0c18 146
mbed_official 146:f64d43ff0c18 147 if (i2c_do_write(obj, (address | 0x01))) {
mbed_official 146:f64d43ff0c18 148 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 149 return I2C_ERROR_NO_SLAVE;
mbed_official 146:f64d43ff0c18 150 }
mbed_official 146:f64d43ff0c18 151
mbed_official 146:f64d43ff0c18 152 // set rx mode
mbed_official 146:f64d43ff0c18 153 i2c_hal_set_direction(obj->instance, kI2CReceive);
mbed_official 146:f64d43ff0c18 154
mbed_official 146:f64d43ff0c18 155 // Read in bytes
mbed_official 146:f64d43ff0c18 156 for (count = 0; count < (length); count++) {
mbed_official 146:f64d43ff0c18 157 ptr = (count == 0) ? &dummy_read : &data[count - 1];
mbed_official 146:f64d43ff0c18 158 uint8_t stop_ = (count == (length - 1)) ? 1 : 0;
mbed_official 146:f64d43ff0c18 159 if (i2c_do_read(obj, ptr, stop_)) {
mbed_official 146:f64d43ff0c18 160 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 161 return count;
mbed_official 146:f64d43ff0c18 162 }
mbed_official 146:f64d43ff0c18 163 }
mbed_official 146:f64d43ff0c18 164
mbed_official 146:f64d43ff0c18 165 // If not repeated start, send stop.
mbed_official 146:f64d43ff0c18 166 if (stop)
mbed_official 146:f64d43ff0c18 167 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 168
mbed_official 146:f64d43ff0c18 169 // last read
mbed_official 146:f64d43ff0c18 170 data[count-1] = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 171
mbed_official 146:f64d43ff0c18 172 return length;
mbed_official 146:f64d43ff0c18 173 }
mbed_official 146:f64d43ff0c18 174
mbed_official 146:f64d43ff0c18 175 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
mbed_official 146:f64d43ff0c18 176 int i;
mbed_official 146:f64d43ff0c18 177
mbed_official 146:f64d43ff0c18 178 if (i2c_start(obj)) {
mbed_official 146:f64d43ff0c18 179 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 180 return I2C_ERROR_BUS_BUSY;
mbed_official 146:f64d43ff0c18 181 }
mbed_official 146:f64d43ff0c18 182
mbed_official 146:f64d43ff0c18 183 if (i2c_do_write(obj, (address & 0xFE))) {
mbed_official 146:f64d43ff0c18 184 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 185 return I2C_ERROR_NO_SLAVE;
mbed_official 146:f64d43ff0c18 186 }
mbed_official 146:f64d43ff0c18 187
mbed_official 146:f64d43ff0c18 188 for (i = 0; i < length; i++) {
mbed_official 146:f64d43ff0c18 189 if(i2c_do_write(obj, data[i])) {
mbed_official 146:f64d43ff0c18 190 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 191 return i;
mbed_official 146:f64d43ff0c18 192 }
mbed_official 146:f64d43ff0c18 193 }
mbed_official 146:f64d43ff0c18 194
mbed_official 146:f64d43ff0c18 195 if (stop)
mbed_official 146:f64d43ff0c18 196 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 197
mbed_official 146:f64d43ff0c18 198 return length;
mbed_official 146:f64d43ff0c18 199 }
mbed_official 146:f64d43ff0c18 200
mbed_official 146:f64d43ff0c18 201 void i2c_reset(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 202 i2c_stop(obj);
mbed_official 146:f64d43ff0c18 203 }
mbed_official 146:f64d43ff0c18 204
mbed_official 146:f64d43ff0c18 205 int i2c_byte_read(i2c_t *obj, int last) {
mbed_official 146:f64d43ff0c18 206 char data;
mbed_official 146:f64d43ff0c18 207
mbed_official 146:f64d43ff0c18 208 // set rx mode
mbed_official 146:f64d43ff0c18 209 i2c_hal_set_direction(obj->instance, kI2CReceive);
mbed_official 146:f64d43ff0c18 210
mbed_official 178:a873352662d8 211 // Setup read
mbed_official 146:f64d43ff0c18 212 i2c_do_read(obj, &data, last);
mbed_official 146:f64d43ff0c18 213
mbed_official 178:a873352662d8 214 // set tx mode
mbed_official 178:a873352662d8 215 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 178:a873352662d8 216 return i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 217 }
mbed_official 146:f64d43ff0c18 218
mbed_official 146:f64d43ff0c18 219 int i2c_byte_write(i2c_t *obj, int data) {
mbed_official 146:f64d43ff0c18 220 // set tx mode
mbed_official 146:f64d43ff0c18 221 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 146:f64d43ff0c18 222
mbed_official 146:f64d43ff0c18 223 return !i2c_do_write(obj, (data & 0xFF));
mbed_official 146:f64d43ff0c18 224 }
mbed_official 146:f64d43ff0c18 225
mbed_official 146:f64d43ff0c18 226
mbed_official 146:f64d43ff0c18 227 #if DEVICE_I2CSLAVE
mbed_official 146:f64d43ff0c18 228 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
mbed_official 146:f64d43ff0c18 229 if (enable_slave) {
mbed_official 146:f64d43ff0c18 230 // set slave mode
mbed_official 146:f64d43ff0c18 231 BW_I2C_C1_MST(obj->instance, 0);
mbed_official 146:f64d43ff0c18 232 i2c_hal_enable_interrupt(obj->instance);
mbed_official 146:f64d43ff0c18 233 } else {
mbed_official 146:f64d43ff0c18 234 // set master mode
mbed_official 146:f64d43ff0c18 235 BW_I2C_C1_MST(obj->instance, 1);
mbed_official 146:f64d43ff0c18 236 }
mbed_official 146:f64d43ff0c18 237 }
mbed_official 146:f64d43ff0c18 238
mbed_official 146:f64d43ff0c18 239 int i2c_slave_receive(i2c_t *obj) {
mbed_official 146:f64d43ff0c18 240 switch(HW_I2C_S_RD(obj->instance)) {
mbed_official 146:f64d43ff0c18 241 // read addressed
mbed_official 146:f64d43ff0c18 242 case 0xE6:
mbed_official 146:f64d43ff0c18 243 return 1;
mbed_official 146:f64d43ff0c18 244 // write addressed
mbed_official 146:f64d43ff0c18 245 case 0xE2:
mbed_official 146:f64d43ff0c18 246 return 3;
mbed_official 146:f64d43ff0c18 247 default:
mbed_official 146:f64d43ff0c18 248 return 0;
mbed_official 146:f64d43ff0c18 249 }
mbed_official 146:f64d43ff0c18 250 }
mbed_official 146:f64d43ff0c18 251
mbed_official 146:f64d43ff0c18 252 int i2c_slave_read(i2c_t *obj, char *data, int length) {
mbed_official 146:f64d43ff0c18 253 uint8_t dummy_read;
mbed_official 146:f64d43ff0c18 254 uint8_t *ptr;
mbed_official 146:f64d43ff0c18 255 int count;
mbed_official 146:f64d43ff0c18 256
mbed_official 146:f64d43ff0c18 257 // set rx mode
mbed_official 146:f64d43ff0c18 258 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 146:f64d43ff0c18 259
mbed_official 146:f64d43ff0c18 260 // first dummy read
mbed_official 146:f64d43ff0c18 261 dummy_read = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 262 if (i2c_wait_end_rx_transfer(obj))
mbed_official 146:f64d43ff0c18 263 return 0;
mbed_official 146:f64d43ff0c18 264
mbed_official 146:f64d43ff0c18 265 // read address
mbed_official 146:f64d43ff0c18 266 dummy_read = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 267 if (i2c_wait_end_rx_transfer(obj))
mbed_official 146:f64d43ff0c18 268 return 0;
mbed_official 146:f64d43ff0c18 269
mbed_official 146:f64d43ff0c18 270 // read (length - 1) bytes
mbed_official 146:f64d43ff0c18 271 for (count = 0; count < (length - 1); count++) {
mbed_official 146:f64d43ff0c18 272 data[count] = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 273 if (i2c_wait_end_rx_transfer(obj))
mbed_official 146:f64d43ff0c18 274 return count;
mbed_official 146:f64d43ff0c18 275 }
mbed_official 146:f64d43ff0c18 276
mbed_official 146:f64d43ff0c18 277 // read last byte
mbed_official 146:f64d43ff0c18 278 ptr = (length == 0) ? &dummy_read : (uint8_t *)&data[count];
mbed_official 146:f64d43ff0c18 279 *ptr = i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 280
mbed_official 146:f64d43ff0c18 281 return (length) ? (count + 1) : 0;
mbed_official 146:f64d43ff0c18 282 }
mbed_official 146:f64d43ff0c18 283
mbed_official 146:f64d43ff0c18 284 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
mbed_official 146:f64d43ff0c18 285 int i, count = 0;
mbed_official 146:f64d43ff0c18 286
mbed_official 146:f64d43ff0c18 287 // set tx mode
mbed_official 146:f64d43ff0c18 288 i2c_hal_set_direction(obj->instance, kI2CTransmit);
mbed_official 146:f64d43ff0c18 289
mbed_official 146:f64d43ff0c18 290 for (i = 0; i < length; i++) {
mbed_official 146:f64d43ff0c18 291 if (i2c_do_write(obj, data[count++]) == 2)
mbed_official 146:f64d43ff0c18 292 return i;
mbed_official 146:f64d43ff0c18 293 }
mbed_official 146:f64d43ff0c18 294
mbed_official 146:f64d43ff0c18 295 // set rx mode
mbed_official 146:f64d43ff0c18 296 i2c_hal_set_direction(obj->instance, kI2CReceive);
mbed_official 146:f64d43ff0c18 297
mbed_official 146:f64d43ff0c18 298 // dummy rx transfer needed
mbed_official 146:f64d43ff0c18 299 // otherwise the master cannot generate a stop bit
mbed_official 146:f64d43ff0c18 300 i2c_hal_read(obj->instance);
mbed_official 146:f64d43ff0c18 301 if (i2c_wait_end_rx_transfer(obj) == 2)
mbed_official 146:f64d43ff0c18 302 return count;
mbed_official 146:f64d43ff0c18 303
mbed_official 146:f64d43ff0c18 304 return count;
mbed_official 146:f64d43ff0c18 305 }
mbed_official 146:f64d43ff0c18 306
mbed_official 146:f64d43ff0c18 307 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
mbed_official 146:f64d43ff0c18 308 i2c_hal_set_upper_slave_address_7bit(obj->instance, address & 0xfe);
mbed_official 146:f64d43ff0c18 309 }
mbed_official 146:f64d43ff0c18 310 #endif
mbed_official 146:f64d43ff0c18 311
mbed_official 267:8673334f2cbe 312 #endif