mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon May 26 18:15:07 2014 +0100
Revision:
212:34d62c0b2af6
Parent:
13:0645d8841f51
Synchronized with git revision f65b7d907785089e8dd942446f1c974b3a213a39

Full URL: https://github.com/mbedmicro/mbed/commit/f65b7d907785089e8dd942446f1c974b3a213a39/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 13:0645d8841f51 1 /* mbed Microcontroller Library
bogdanm 13:0645d8841f51 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 13:0645d8841f51 3 *
bogdanm 13:0645d8841f51 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 13:0645d8841f51 5 * you may not use this file except in compliance with the License.
bogdanm 13:0645d8841f51 6 * You may obtain a copy of the License at
bogdanm 13:0645d8841f51 7 *
bogdanm 13:0645d8841f51 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 13:0645d8841f51 9 *
bogdanm 13:0645d8841f51 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 13:0645d8841f51 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 13:0645d8841f51 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 13:0645d8841f51 13 * See the License for the specific language governing permissions and
bogdanm 13:0645d8841f51 14 * limitations under the License.
bogdanm 13:0645d8841f51 15 */
bogdanm 13:0645d8841f51 16 #include "I2C.h"
bogdanm 13:0645d8841f51 17
bogdanm 13:0645d8841f51 18 #if DEVICE_I2C
bogdanm 13:0645d8841f51 19
bogdanm 13:0645d8841f51 20 namespace mbed {
bogdanm 13:0645d8841f51 21
bogdanm 13:0645d8841f51 22 I2C *I2C::_owner = NULL;
bogdanm 13:0645d8841f51 23
mbed_official 212:34d62c0b2af6 24 I2C::I2C(PinName sda, PinName scl) : _i2c(), _hz(100000) {
bogdanm 13:0645d8841f51 25 // The init function also set the frequency to 100000
bogdanm 13:0645d8841f51 26 i2c_init(&_i2c, sda, scl);
bogdanm 13:0645d8841f51 27
bogdanm 13:0645d8841f51 28 // Used to avoid unnecessary frequency updates
bogdanm 13:0645d8841f51 29 _owner = this;
bogdanm 13:0645d8841f51 30 }
bogdanm 13:0645d8841f51 31
bogdanm 13:0645d8841f51 32 void I2C::frequency(int hz) {
bogdanm 13:0645d8841f51 33 _hz = hz;
bogdanm 13:0645d8841f51 34
bogdanm 13:0645d8841f51 35 // We want to update the frequency even if we are already the bus owners
bogdanm 13:0645d8841f51 36 i2c_frequency(&_i2c, _hz);
bogdanm 13:0645d8841f51 37
bogdanm 13:0645d8841f51 38 // Updating the frequency of the bus we become the owners of it
bogdanm 13:0645d8841f51 39 _owner = this;
bogdanm 13:0645d8841f51 40 }
bogdanm 13:0645d8841f51 41
bogdanm 13:0645d8841f51 42 void I2C::aquire() {
bogdanm 13:0645d8841f51 43 if (_owner != this) {
bogdanm 13:0645d8841f51 44 i2c_frequency(&_i2c, _hz);
bogdanm 13:0645d8841f51 45 _owner = this;
bogdanm 13:0645d8841f51 46 }
bogdanm 13:0645d8841f51 47 }
bogdanm 13:0645d8841f51 48
bogdanm 13:0645d8841f51 49 // write - Master Transmitter Mode
bogdanm 13:0645d8841f51 50 int I2C::write(int address, const char* data, int length, bool repeated) {
bogdanm 13:0645d8841f51 51 aquire();
bogdanm 13:0645d8841f51 52
bogdanm 13:0645d8841f51 53 int stop = (repeated) ? 0 : 1;
bogdanm 13:0645d8841f51 54 int written = i2c_write(&_i2c, address, data, length, stop);
bogdanm 13:0645d8841f51 55
bogdanm 13:0645d8841f51 56 return length != written;
bogdanm 13:0645d8841f51 57 }
bogdanm 13:0645d8841f51 58
bogdanm 13:0645d8841f51 59 int I2C::write(int data) {
bogdanm 13:0645d8841f51 60 return i2c_byte_write(&_i2c, data);
bogdanm 13:0645d8841f51 61 }
bogdanm 13:0645d8841f51 62
bogdanm 13:0645d8841f51 63 // read - Master Reciever Mode
bogdanm 13:0645d8841f51 64 int I2C::read(int address, char* data, int length, bool repeated) {
bogdanm 13:0645d8841f51 65 aquire();
bogdanm 13:0645d8841f51 66
bogdanm 13:0645d8841f51 67 int stop = (repeated) ? 0 : 1;
bogdanm 13:0645d8841f51 68 int read = i2c_read(&_i2c, address, data, length, stop);
bogdanm 13:0645d8841f51 69
bogdanm 13:0645d8841f51 70 return length != read;
bogdanm 13:0645d8841f51 71 }
bogdanm 13:0645d8841f51 72
bogdanm 13:0645d8841f51 73 int I2C::read(int ack) {
bogdanm 13:0645d8841f51 74 if (ack) {
bogdanm 13:0645d8841f51 75 return i2c_byte_read(&_i2c, 0);
bogdanm 13:0645d8841f51 76 } else {
bogdanm 13:0645d8841f51 77 return i2c_byte_read(&_i2c, 1);
bogdanm 13:0645d8841f51 78 }
bogdanm 13:0645d8841f51 79 }
bogdanm 13:0645d8841f51 80
bogdanm 13:0645d8841f51 81 void I2C::start(void) {
bogdanm 13:0645d8841f51 82 i2c_start(&_i2c);
bogdanm 13:0645d8841f51 83 }
bogdanm 13:0645d8841f51 84
bogdanm 13:0645d8841f51 85 void I2C::stop(void) {
bogdanm 13:0645d8841f51 86 i2c_stop(&_i2c);
bogdanm 13:0645d8841f51 87 }
bogdanm 13:0645d8841f51 88
bogdanm 13:0645d8841f51 89 } // namespace mbed
bogdanm 13:0645d8841f51 90
bogdanm 13:0645d8841f51 91 #endif