takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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 
00017 #include "drivers/I2C.h"
00018 #include "drivers/DigitalInOut.h"
00019 #include "platform/mbed_wait_api.h"
00020 
00021 #if DEVICE_I2C
00022 
00023 #if DEVICE_I2C_ASYNCH
00024 #include "platform/mbed_power_mgmt.h"
00025 #endif
00026 
00027 namespace mbed {
00028 
00029 I2C *I2C::_owner = NULL;
00030 SingletonPtr<PlatformMutex>  I2C::_mutex;
00031 
00032 I2C::I2C(PinName sda, PinName scl) :
00033 #if DEVICE_I2C_ASYNCH
00034     _irq(this), _usage(DMA_USAGE_NEVER), _deep_sleep_locked(false),
00035 #endif
00036     _i2c(), _hz(100000)
00037 {
00038     lock();
00039     // The init function also set the frequency to 100000
00040     _sda = sda;
00041     _scl = scl;
00042     recover(sda, scl);
00043     i2c_init(&_i2c, _sda, _scl);
00044     // Used to avoid unnecessary frequency updates
00045     _owner = this;
00046     unlock();
00047 }
00048 
00049 void I2C::frequency(int hz)
00050 {
00051     lock();
00052     _hz = hz;
00053 
00054     // We want to update the frequency even if we are already the bus owners
00055     i2c_frequency(&_i2c, _hz);
00056 
00057     // Updating the frequency of the bus we become the owners of it
00058     _owner = this;
00059     unlock();
00060 }
00061 
00062 void I2C::aquire()
00063 {
00064     lock();
00065     if (_owner != this) {
00066         i2c_frequency(&_i2c, _hz);
00067         _owner = this;
00068     }
00069     unlock();
00070 }
00071 
00072 // write - Master Transmitter Mode
00073 int I2C::write(int address, const char *data, int length, bool repeated)
00074 {
00075     lock();
00076     aquire();
00077 
00078     int stop = (repeated) ? 0 : 1;
00079     int written = i2c_write(&_i2c, address, data, length, stop);
00080 
00081     unlock();
00082     return length != written;
00083 }
00084 
00085 int I2C::write(int data)
00086 {
00087     lock();
00088     int ret = i2c_byte_write(&_i2c, data);
00089     unlock();
00090     return ret;
00091 }
00092 
00093 // read - Master Receiver Mode
00094 int I2C::read(int address, char *data, int length, bool repeated)
00095 {
00096     lock();
00097     aquire();
00098 
00099     int stop = (repeated) ? 0 : 1;
00100     int read = i2c_read(&_i2c, address, data, length, stop);
00101 
00102     unlock();
00103     return length != read;
00104 }
00105 
00106 int I2C::read(int ack)
00107 {
00108     lock();
00109     int ret;
00110     if (ack) {
00111         ret = i2c_byte_read(&_i2c, 0);
00112     } else {
00113         ret = i2c_byte_read(&_i2c, 1);
00114     }
00115     unlock();
00116     return ret;
00117 }
00118 
00119 void I2C::start(void)
00120 {
00121     lock();
00122     i2c_start(&_i2c);
00123     unlock();
00124 }
00125 
00126 void I2C::stop(void)
00127 {
00128     lock();
00129     i2c_stop(&_i2c);
00130     unlock();
00131 }
00132 
00133 void I2C::lock()
00134 {
00135     _mutex->lock();
00136 }
00137 
00138 void I2C::unlock()
00139 {
00140     _mutex->unlock();
00141 }
00142 
00143 int I2C::recover(PinName sda, PinName scl)
00144 {
00145     DigitalInOut pin_sda(sda, PIN_INPUT, PullNone, 1);
00146     DigitalInOut pin_scl(scl, PIN_INPUT, PullNone, 1);
00147 
00148     // Return as SCL is low and no access to become master.
00149     if (pin_scl == 0) {
00150         return I2C_ERROR_BUS_BUSY;
00151     }
00152 
00153     // Return successfully as SDA and SCL is high
00154     if (pin_sda == 1) {
00155         return 0;
00156     }
00157 
00158     // Send clock pulses, for device to recover 9
00159     pin_scl.mode(PullNone);
00160     pin_scl.output();
00161     for (int count = 0; count < 10; count++) {
00162         pin_scl.mode(PullNone);
00163         pin_scl = 0;
00164         wait_us(5);
00165         pin_scl.mode(PullUp);
00166         pin_scl = 1;
00167         wait_us(5);
00168     }
00169 
00170     // Send Stop
00171     pin_sda.output();
00172     pin_sda = 0;
00173     wait_us(5);
00174     pin_scl = 1;
00175     wait_us(5);
00176     pin_sda = 1;
00177     wait_us(5);
00178 
00179     pin_sda.input();
00180     pin_scl.input();
00181     if ((pin_scl == 0) || (pin_sda == 0)) {
00182         // Return as SCL is low and no access to become master.
00183         return I2C_ERROR_BUS_BUSY;
00184     }
00185 
00186     return 0;
00187 }
00188 
00189 #if DEVICE_I2C_ASYNCH
00190 
00191 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)
00192 {
00193     lock();
00194     if (i2c_active(&_i2c)) {
00195         unlock();
00196         return -1; // transaction ongoing
00197     }
00198     lock_deep_sleep();
00199     aquire();
00200 
00201     _callback = callback;
00202     int stop = (repeated) ? 0 : 1;
00203     _irq.callback(&I2C::irq_handler_asynch);
00204     i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
00205     unlock();
00206     return 0;
00207 }
00208 
00209 void I2C::abort_transfer(void)
00210 {
00211     lock();
00212     i2c_abort_asynch(&_i2c);
00213     unlock_deep_sleep();
00214     unlock();
00215 }
00216 
00217 void I2C::irq_handler_asynch(void)
00218 {
00219     int event = i2c_irq_handler_asynch(&_i2c);
00220     if (_callback && event) {
00221         _callback.call(event);
00222     }
00223 
00224     if (event) {
00225         unlock_deep_sleep();
00226     }
00227 }
00228 
00229 void I2C::lock_deep_sleep()
00230 {
00231     if (_deep_sleep_locked == false) {
00232         sleep_manager_lock_deep_sleep();
00233         _deep_sleep_locked = true;
00234     }
00235 }
00236 
00237 void I2C::unlock_deep_sleep()
00238 {
00239     if (_deep_sleep_locked == true) {
00240         sleep_manager_unlock_deep_sleep();
00241         _deep_sleep_locked = false;
00242     }
00243 }
00244 
00245 #endif
00246 
00247 } // namespace mbed
00248 
00249 #endif