mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
I2C.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2015 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #include "drivers/I2C.h" 00019 #include "drivers/DigitalInOut.h" 00020 #include "platform/mbed_wait_api.h" 00021 00022 #if DEVICE_I2C 00023 00024 #if DEVICE_I2C_ASYNCH 00025 #include "platform/mbed_power_mgmt.h" 00026 #endif 00027 00028 namespace mbed { 00029 00030 I2C *I2C::_owner = NULL; 00031 SingletonPtr<PlatformMutex> I2C::_mutex; 00032 00033 I2C::I2C(PinName sda, PinName scl) : 00034 #if DEVICE_I2C_ASYNCH 00035 _irq(this), _usage(DMA_USAGE_NEVER), _deep_sleep_locked(false), 00036 #endif 00037 _i2c(), _hz(100000) 00038 { 00039 lock(); 00040 // The init function also set the frequency to 100000 00041 _sda = sda; 00042 _scl = scl; 00043 recover(sda, scl); 00044 i2c_init(&_i2c, _sda, _scl); 00045 // Used to avoid unnecessary frequency updates 00046 _owner = this; 00047 unlock(); 00048 } 00049 00050 void I2C::frequency(int hz) 00051 { 00052 lock(); 00053 _hz = hz; 00054 00055 // We want to update the frequency even if we are already the bus owners 00056 i2c_frequency(&_i2c, _hz); 00057 00058 // Updating the frequency of the bus we become the owners of it 00059 _owner = this; 00060 unlock(); 00061 } 00062 00063 void I2C::aquire() 00064 { 00065 lock(); 00066 if (_owner != this) { 00067 i2c_frequency(&_i2c, _hz); 00068 _owner = this; 00069 } 00070 unlock(); 00071 } 00072 00073 // write - Master Transmitter Mode 00074 int I2C::write(int address, const char *data, int length, bool repeated) 00075 { 00076 lock(); 00077 aquire(); 00078 00079 int stop = (repeated) ? 0 : 1; 00080 int written = i2c_write(&_i2c, address, data, length, stop); 00081 00082 unlock(); 00083 return length != written; 00084 } 00085 00086 int I2C::write(int data) 00087 { 00088 lock(); 00089 int ret = i2c_byte_write(&_i2c, data); 00090 unlock(); 00091 return ret; 00092 } 00093 00094 // read - Master Receiver Mode 00095 int I2C::read(int address, char *data, int length, bool repeated) 00096 { 00097 lock(); 00098 aquire(); 00099 00100 int stop = (repeated) ? 0 : 1; 00101 int read = i2c_read(&_i2c, address, data, length, stop); 00102 00103 unlock(); 00104 return length != read; 00105 } 00106 00107 int I2C::read(int ack) 00108 { 00109 lock(); 00110 int ret; 00111 if (ack) { 00112 ret = i2c_byte_read(&_i2c, 0); 00113 } else { 00114 ret = i2c_byte_read(&_i2c, 1); 00115 } 00116 unlock(); 00117 return ret; 00118 } 00119 00120 void I2C::start(void) 00121 { 00122 lock(); 00123 i2c_start(&_i2c); 00124 unlock(); 00125 } 00126 00127 void I2C::stop(void) 00128 { 00129 lock(); 00130 i2c_stop(&_i2c); 00131 unlock(); 00132 } 00133 00134 void I2C::lock() 00135 { 00136 _mutex->lock(); 00137 } 00138 00139 void I2C::unlock() 00140 { 00141 _mutex->unlock(); 00142 } 00143 00144 int I2C::recover(PinName sda, PinName scl) 00145 { 00146 DigitalInOut pin_sda(sda, PIN_INPUT, PullNone, 1); 00147 DigitalInOut pin_scl(scl, PIN_INPUT, PullNone, 1); 00148 00149 // Return as SCL is low and no access to become master. 00150 if (pin_scl == 0) { 00151 return I2C_ERROR_BUS_BUSY; 00152 } 00153 00154 // Return successfully as SDA and SCL is high 00155 if (pin_sda == 1) { 00156 return 0; 00157 } 00158 00159 // Send clock pulses, for device to recover 9 00160 pin_scl.mode(PullNone); 00161 pin_scl.output(); 00162 for (int count = 0; count < 10; count++) { 00163 pin_scl.mode(PullNone); 00164 pin_scl = 0; 00165 wait_us(5); 00166 pin_scl.mode(PullUp); 00167 pin_scl = 1; 00168 wait_us(5); 00169 } 00170 00171 // Send Stop 00172 pin_sda.output(); 00173 pin_sda = 0; 00174 wait_us(5); 00175 pin_scl = 1; 00176 wait_us(5); 00177 pin_sda = 1; 00178 wait_us(5); 00179 00180 pin_sda.input(); 00181 pin_scl.input(); 00182 if ((pin_scl == 0) || (pin_sda == 0)) { 00183 // Return as SCL is low and no access to become master. 00184 return I2C_ERROR_BUS_BUSY; 00185 } 00186 00187 return 0; 00188 } 00189 00190 #if DEVICE_I2C_ASYNCH 00191 00192 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) 00193 { 00194 lock(); 00195 if (i2c_active(&_i2c)) { 00196 unlock(); 00197 return -1; // transaction ongoing 00198 } 00199 lock_deep_sleep(); 00200 aquire(); 00201 00202 _callback = callback; 00203 int stop = (repeated) ? 0 : 1; 00204 _irq.callback(&I2C::irq_handler_asynch); 00205 i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage); 00206 unlock(); 00207 return 0; 00208 } 00209 00210 void I2C::abort_transfer(void) 00211 { 00212 lock(); 00213 i2c_abort_asynch(&_i2c); 00214 unlock_deep_sleep(); 00215 unlock(); 00216 } 00217 00218 void I2C::irq_handler_asynch(void) 00219 { 00220 int event = i2c_irq_handler_asynch(&_i2c); 00221 if (_callback && event) { 00222 _callback.call(event); 00223 } 00224 00225 if (event) { 00226 unlock_deep_sleep(); 00227 } 00228 } 00229 00230 void I2C::lock_deep_sleep() 00231 { 00232 if (_deep_sleep_locked == false) { 00233 sleep_manager_lock_deep_sleep(); 00234 _deep_sleep_locked = true; 00235 } 00236 } 00237 00238 void I2C::unlock_deep_sleep() 00239 { 00240 if (_deep_sleep_locked == true) { 00241 sleep_manager_unlock_deep_sleep(); 00242 _deep_sleep_locked = false; 00243 } 00244 } 00245 00246 #endif 00247 00248 } // namespace mbed 00249 00250 #endif
Generated on Tue Jul 12 2022 20:41:15 by 1.7.2