1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 I2C *I2C::_owner = NULL;
24 I2C::I2C(PinName sda, PinName scl) : _i2c(), _hz(100000) {
25 // The init function also set the frequency to 100000
26 i2c_init(&_i2c, sda, scl);
28 // Used to avoid unnecessary frequency updates
32 void I2C::frequency(int hz) {
35 // We want to update the frequency even if we are already the bus owners
36 i2c_frequency(&_i2c, _hz);
38 // Updating the frequency of the bus we become the owners of it
44 i2c_frequency(&_i2c, _hz);
49 // write - Master Transmitter Mode
50 int I2C::write(int address, const char* data, int length, bool repeated) {
53 int stop = (repeated) ? 0 : 1;
54 int written = i2c_write(&_i2c, address, data, length, stop);
56 return length != written;
59 int I2C::write(int data) {
60 return i2c_byte_write(&_i2c, data);
63 // read - Master Reciever Mode
64 int I2C::read(int address, char* data, int length, bool repeated) {
67 int stop = (repeated) ? 0 : 1;
68 int read = i2c_read(&_i2c, address, data, length, stop);
70 return length != read;
73 int I2C::read(int ack) {
75 return i2c_byte_read(&_i2c, 0);
77 return i2c_byte_read(&_i2c, 1);
81 void I2C::start(void) {
85 void I2C::stop(void) {