mbed library sources

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2C.cpp Source File

I2C.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 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 "I2C.h"
00017 
00018 #if DEVICE_I2C
00019 
00020 namespace mbed {
00021 
00022 I2C *I2C::_owner = NULL;
00023 
00024 I2C::I2C(PinName sda, PinName scl) :
00025 #if DEVICE_I2C_ASYNCH
00026                                      _irq(this), _usage(DMA_USAGE_NEVER),
00027 #endif
00028                                       _i2c(), _hz(100000) {
00029     // The init function also set the frequency to 100000
00030     i2c_init(&_i2c, sda, scl);
00031 
00032     // Used to avoid unnecessary frequency updates
00033     _owner = this;
00034 }
00035 
00036 void I2C::frequency(int hz) {
00037     _hz = hz;
00038 
00039     // We want to update the frequency even if we are already the bus owners
00040     i2c_frequency(&_i2c, _hz);
00041 
00042     // Updating the frequency of the bus we become the owners of it
00043     _owner = this;
00044 }
00045 
00046 void I2C::aquire() {
00047     if (_owner != this) {
00048         i2c_frequency(&_i2c, _hz);
00049         _owner = this;
00050     }
00051 }
00052 
00053 // write - Master Transmitter Mode
00054 int I2C::write(int address, const char* data, int length, bool repeated) {
00055     aquire();
00056 
00057     int stop = (repeated) ? 0 : 1;
00058     int written = i2c_write(&_i2c, address, data, length, stop);
00059 
00060     return length != written;
00061 }
00062 
00063 int I2C::write(int data) {
00064     return i2c_byte_write(&_i2c, data);
00065 }
00066 
00067 // read - Master Reciever Mode
00068 int I2C::read(int address, char* data, int length, bool repeated) {
00069     aquire();
00070 
00071     int stop = (repeated) ? 0 : 1;
00072     int read = i2c_read(&_i2c, address, data, length, stop);
00073 
00074     return length != read;
00075 }
00076 
00077 int I2C::read(int ack) {
00078     if (ack) {
00079         return i2c_byte_read(&_i2c, 0);
00080     } else {
00081         return i2c_byte_read(&_i2c, 1);
00082     }
00083 }
00084 
00085 void I2C::start(void) {
00086     i2c_start(&_i2c);
00087 }
00088 
00089 void I2C::stop(void) {
00090     i2c_stop(&_i2c);
00091 }
00092 
00093 #if DEVICE_I2C_ASYNCH
00094 
00095 int I2C::transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t & callback, int event, bool repeated)
00096 {
00097     if (i2c_active(&_i2c)) {
00098         return -1; // transaction ongoing
00099     }
00100     aquire();
00101 
00102     _callback = callback;
00103     int stop = (repeated) ? 0 : 1;
00104     _irq.callback(&I2C::irq_handler_asynch);
00105     i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
00106     return 0;
00107 }
00108 
00109 void I2C::abort_transfer(void)
00110 {
00111     i2c_abort_asynch(&_i2c);
00112 }
00113 
00114 void I2C::irq_handler_asynch(void)
00115 {
00116     int event = i2c_irq_handler_asynch(&_i2c);
00117     if (_callback && event) {
00118         _callback.call(event);
00119     }
00120 
00121 }
00122 
00123 
00124 #endif
00125 
00126 } // namespace mbed
00127 
00128 #endif