Life is random and unfair!
Fork of mbed-dev by
Embed:
(wiki syntax)
Show/hide line numbers
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 namespace mbed { 00021 00022 I2C *I2C::_owner = NULL; 00023 SingletonPtr<PlatformMutex> I2C::_mutex; 00024 00025 I2C::I2C(PinName sda, PinName scl) : 00026 #if DEVICE_I2C_ASYNCH 00027 _irq(this), _usage(DMA_USAGE_NEVER), 00028 #endif 00029 _i2c(), _hz(100000) { 00030 // No lock needed in the constructor 00031 00032 // The init function also set the frequency to 100000 00033 i2c_init(&_i2c, sda, scl); 00034 00035 // Used to avoid unnecessary frequency updates 00036 _owner = this; 00037 } 00038 00039 void I2C::frequency(int hz) { 00040 lock(); 00041 _hz = hz; 00042 00043 // We want to update the frequency even if we are already the bus owners 00044 i2c_frequency(&_i2c, _hz); 00045 00046 // Updating the frequency of the bus we become the owners of it 00047 _owner = this; 00048 unlock(); 00049 } 00050 00051 void I2C::aquire() { 00052 lock(); 00053 if (_owner != this) { 00054 i2c_frequency(&_i2c, _hz); 00055 _owner = this; 00056 } 00057 unlock(); 00058 } 00059 00060 // write - Master Transmitter Mode 00061 int I2C::write(int address, const char* data, int length, bool repeated) { 00062 lock(); 00063 aquire(); 00064 00065 int stop = (repeated) ? 0 : 1; 00066 int written = i2c_write(&_i2c, address, data, length, stop); 00067 00068 unlock(); 00069 return length != written; 00070 } 00071 00072 int I2C::write(int data) { 00073 lock(); 00074 int ret = i2c_byte_write(&_i2c, data); 00075 unlock(); 00076 return ret; 00077 } 00078 00079 // read - Master Reciever Mode 00080 int I2C::read(int address, char* data, int length, bool repeated) { 00081 lock(); 00082 aquire(); 00083 00084 int stop = (repeated) ? 0 : 1; 00085 int read = i2c_read(&_i2c, address, data, length, stop); 00086 00087 unlock(); 00088 return length != read; 00089 } 00090 00091 int I2C::read(int ack) { 00092 lock(); 00093 int ret; 00094 if (ack) { 00095 ret = i2c_byte_read(&_i2c, 0); 00096 } else { 00097 ret = i2c_byte_read(&_i2c, 1); 00098 } 00099 unlock(); 00100 return ret; 00101 } 00102 00103 void I2C::start(void) { 00104 lock(); 00105 i2c_start(&_i2c); 00106 unlock(); 00107 } 00108 00109 void I2C::stop(void) { 00110 lock(); 00111 i2c_stop(&_i2c); 00112 unlock(); 00113 } 00114 00115 void I2C::lock() { 00116 _mutex->lock(); 00117 } 00118 00119 void I2C::unlock() { 00120 _mutex->unlock(); 00121 } 00122 00123 #if DEVICE_I2C_ASYNCH 00124 00125 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) 00126 { 00127 lock(); 00128 if (i2c_active(&_i2c)) { 00129 unlock(); 00130 return -1; // transaction ongoing 00131 } 00132 aquire(); 00133 00134 _callback = callback; 00135 int stop = (repeated) ? 0 : 1; 00136 _irq.callback(&I2C::irq_handler_asynch); 00137 i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage); 00138 unlock(); 00139 return 0; 00140 } 00141 00142 void I2C::abort_transfer(void) 00143 { 00144 lock(); 00145 i2c_abort_asynch(&_i2c); 00146 unlock(); 00147 } 00148 00149 void I2C::irq_handler_asynch(void) 00150 { 00151 int event = i2c_irq_handler_asynch(&_i2c); 00152 if (_callback && event) { 00153 _callback.call(event); 00154 } 00155 00156 } 00157 00158 00159 #endif 00160 00161 } // namespace mbed 00162 00163 #endif
Generated on Tue Jul 12 2022 19:28:24 by 1.7.2