Modified I2C api to work with NRF's dynamic pin asignment.

Dependents:   Seed_Barometer_Sensor_custom_I2C_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CX.cpp Source File

I2CX.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "I2CX.h"
00017 
00018 #if DEVICE_I2C
00019 
00020 namespace mbed {
00021 
00022 I2CX *I2CX::_owner = NULL;
00023 
00024 I2CX::I2CX(I2CName peripheral, PinName sda, PinName scl) : _i2c(), _hz(100000) {
00025     
00026     // Hacky way to get around pin mapping
00027     if(peripheral == I2C_0) {
00028         i2c_init(&_i2c, p22, p20);
00029     }
00030     else if(peripheral == I2C_1) {
00031         i2c_init(&_i2c, p13, p15);
00032     }
00033     else {
00034         i2c_init(&_i2c, NC, NC);
00035     }
00036     
00037     _i2c.sda = sda;
00038     _i2c.scl = scl;
00039     
00040     i2c_reset(&_i2c);
00041     
00042     // Used to avoid unnecessary frequency updates
00043     _owner = this;
00044 }
00045 
00046 void I2CX::frequency(int hz) {
00047     _hz = hz;
00048 
00049     // We want to update the frequency even if we are already the bus owners
00050     i2c_frequency(&_i2c, _hz);
00051 
00052     // Updating the frequency of the bus we become the owners of it
00053     _owner = this;
00054 }
00055 
00056 void I2CX::aquire() {
00057     if (_owner != this) {
00058         i2c_frequency(&_i2c, _hz);
00059         _owner = this;
00060     }
00061 }
00062 
00063 // write - Master Transmitter Mode
00064 int I2CX::write(int address, const char* data, int length, bool repeated) {
00065     aquire();
00066 
00067     int stop = (repeated) ? 0 : 1;
00068     int written = i2c_write(&_i2c, address, data, length, stop);
00069 
00070     return length != written;
00071 }
00072 
00073 int I2CX::write(int data) {
00074     return i2c_byte_write(&_i2c, data);
00075 }
00076 
00077 // read - Master Reciever Mode
00078 int I2CX::read(int address, char* data, int length, bool repeated) {
00079     aquire();
00080 
00081     int stop = (repeated) ? 0 : 1;
00082     int read = i2c_read(&_i2c, address, data, length, stop);
00083 
00084     return length != read;
00085 }
00086 
00087 int I2CX::read(int ack) {
00088     if (ack) {
00089         return i2c_byte_read(&_i2c, 0);
00090     } else {
00091         return i2c_byte_read(&_i2c, 1);
00092     }
00093 }
00094 
00095 void I2CX::start(void) {
00096     i2c_start(&_i2c);
00097 }
00098 
00099 void I2CX::stop(void) {
00100     i2c_stop(&_i2c);
00101 }
00102 
00103 } // namespace mbed
00104 
00105 #endif