BA / Mbed OS BaBoRo1
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 "drivers/I2C.h"
00017 
00018 #if DEVICE_I2C
00019 
00020 #if DEVICE_I2C_ASYNCH
00021 #include "platform/mbed_power_mgmt.h"
00022 #endif
00023 
00024 namespace mbed {
00025 
00026 I2C *I2C::_owner = NULL;
00027 SingletonPtr<PlatformMutex>  I2C::_mutex;
00028 
00029 I2C::I2C(PinName sda, PinName scl) :
00030 #if DEVICE_I2C_ASYNCH
00031     _irq(this), _usage(DMA_USAGE_NEVER), _deep_sleep_locked(false),
00032 #endif
00033     _i2c(), _hz(100000)
00034 {
00035     // No lock needed in the constructor
00036 
00037     // The init function also set the frequency to 100000
00038     i2c_init(&_i2c, sda, scl);
00039 
00040     // Used to avoid unnecessary frequency updates
00041     _owner = this;
00042 }
00043 
00044 void I2C::frequency(int hz) {
00045     lock();
00046     _hz = hz;
00047 
00048     // We want to update the frequency even if we are already the bus owners
00049     i2c_frequency(&_i2c, _hz);
00050 
00051     // Updating the frequency of the bus we become the owners of it
00052     _owner = this;
00053     unlock();
00054 }
00055 
00056 void I2C::aquire() {
00057     lock();
00058     if (_owner != this) {
00059         i2c_frequency(&_i2c, _hz);
00060         _owner = this;
00061     }
00062     unlock();
00063 }
00064 
00065 // write - Master Transmitter Mode
00066 int I2C::write(int address, const char* data, int length, bool repeated) {
00067     lock();
00068     aquire();
00069 
00070     int stop = (repeated) ? 0 : 1;
00071     int written = i2c_write(&_i2c, address, data, length, stop);
00072 
00073     unlock();
00074     return length != written;
00075 }
00076 
00077 int I2C::write(int data) {
00078     lock();
00079     int ret = i2c_byte_write(&_i2c, data);
00080     unlock();
00081     return ret;
00082 }
00083 
00084 // read - Master Receiver Mode
00085 int I2C::read(int address, char* data, int length, bool repeated) {
00086     lock();
00087     aquire();
00088 
00089     int stop = (repeated) ? 0 : 1;
00090     int read = i2c_read(&_i2c, address, data, length, stop);
00091 
00092     unlock();
00093     return length != read;
00094 }
00095 
00096 int I2C::read(int ack) {
00097     lock();
00098     int ret;
00099     if (ack) {
00100         ret = i2c_byte_read(&_i2c, 0);
00101     } else {
00102         ret = i2c_byte_read(&_i2c, 1);
00103     }
00104     unlock();
00105     return ret;
00106 }
00107 
00108 void I2C::start(void) {
00109     lock();
00110     i2c_start(&_i2c);
00111     unlock();
00112 }
00113 
00114 void I2C::stop(void) {
00115     lock();
00116     i2c_stop(&_i2c);
00117     unlock();
00118 }
00119 
00120 void I2C::lock() {
00121     _mutex->lock();
00122 }
00123 
00124 void I2C::unlock() {
00125     _mutex->unlock();
00126 }
00127 
00128 #if DEVICE_I2C_ASYNCH
00129 
00130 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)
00131 {
00132     lock();
00133     if (i2c_active(&_i2c)) {
00134         unlock();
00135         return -1; // transaction ongoing
00136     }
00137     lock_deep_sleep();
00138     aquire();
00139 
00140     _callback = callback;
00141     int stop = (repeated) ? 0 : 1;
00142     _irq.callback(&I2C::irq_handler_asynch);
00143     i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
00144     unlock();
00145     return 0;
00146 }
00147 
00148 void I2C::abort_transfer(void)
00149 {
00150     lock();
00151     i2c_abort_asynch(&_i2c);
00152     unlock_deep_sleep();
00153     unlock();
00154 }
00155 
00156 void I2C::irq_handler_asynch(void)
00157 {
00158     int event = i2c_irq_handler_asynch(&_i2c);
00159     if (_callback && event) {
00160         _callback.call(event);
00161     }
00162     if (event) {
00163         unlock_deep_sleep();
00164     }
00165 
00166 }
00167 
00168 void I2C::lock_deep_sleep()
00169 {
00170     if (_deep_sleep_locked == false) {
00171         sleep_manager_lock_deep_sleep();
00172         _deep_sleep_locked = true;
00173     }
00174 }
00175 
00176 void I2C::unlock_deep_sleep()
00177 {
00178     if (_deep_sleep_locked == true) {
00179         sleep_manager_unlock_deep_sleep();
00180         _deep_sleep_locked = false;
00181     }
00182 }
00183 
00184 #endif
00185 
00186 } // namespace mbed
00187 
00188 #endif