Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
I2C.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 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) : _i2c(), _hz(100000) { 00025 // The init function also set the frequency to 100000 00026 i2c_init(&_i2c, sda, scl); 00027 00028 // Used to avoid unnecessary frequency updates 00029 _owner = this; 00030 } 00031 00032 void I2C::frequency(int hz) { 00033 _hz = hz; 00034 00035 // We want to update the frequency even if we are already the bus owners 00036 i2c_frequency(&_i2c, _hz); 00037 00038 // Updating the frequency of the bus we become the owners of it 00039 _owner = this; 00040 } 00041 00042 void I2C::aquire() { 00043 if (_owner != this) { 00044 i2c_frequency(&_i2c, _hz); 00045 _owner = this; 00046 } 00047 } 00048 00049 // write - Master Transmitter Mode 00050 int I2C::write(int address, const char* data, int length, bool repeated) { 00051 aquire(); 00052 00053 int stop = (repeated) ? 0 : 1; 00054 int written = i2c_write(&_i2c, address, data, length, stop); 00055 00056 return length != written; 00057 } 00058 00059 int I2C::write(int data) { 00060 return i2c_byte_write(&_i2c, data); 00061 } 00062 00063 // read - Master Reciever Mode 00064 int I2C::read(int address, char* data, int length, bool repeated) { 00065 aquire(); 00066 00067 int stop = (repeated) ? 0 : 1; 00068 int read = i2c_read(&_i2c, address, data, length, stop); 00069 00070 return length != read; 00071 } 00072 00073 int I2C::read(int ack) { 00074 if (ack) { 00075 return i2c_byte_read(&_i2c, 0); 00076 } else { 00077 return i2c_byte_read(&_i2c, 1); 00078 } 00079 } 00080 00081 void I2C::start(void) { 00082 i2c_start(&_i2c); 00083 } 00084 00085 void I2C::stop(void) { 00086 i2c_stop(&_i2c); 00087 } 00088 00089 } // namespace mbed 00090 00091 #endif
Generated on Thu Jul 14 2022 13:45:19 by
