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 ltc2991_test by
LT_I2C.cpp
00001 00002 /*! 00003 LT_I2C: Routines to communicate with ATmega328P's hardware I2C port. 00004 00005 @verbatim 00006 00007 LT_I2C contains the low level routines to communicate with devices using the 00008 ATMega328's onboard hardware I2C port. Each routine checks the Two Wire Status 00009 Register (TWSR) at the end of the transaction and returns 0 if successful and 1 00010 if not successful. 00011 00012 I2C Frequency = (CPU Clock frequency)/(16+2(TWBR)*Prescaler) 00013 00014 TWBR-Two Wire Bit Rate Register 00015 TWCR=Two Wire Control Register (TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIE) 00016 TWSR=Two Wire Status Register 00017 00018 Prescaler Values: 00019 TWSR1 TWSR0 Prescaler 00020 0 0 1 00021 0 1 4 00022 1 0 16 00023 1 1 64 00024 00025 Examples: 00026 CPU Frequency = 16Mhz on Arduino Uno 00027 I2C Frequency Prescaler TWSR1 TWSR0 TWBR 00028 1khz 64 1 1 125 00029 10khz 64 1 1 12 00030 50khz 16 1 0 10 00031 100khz 4 0 1 18 00032 400khz 1 0 0 12 00033 00034 @endverbatim 00035 00036 REVISION HISTORY 00037 $Revision: 5469 $ 00038 $Date: 2016-07-22 17:01:32 -0700 (Fri, 22 Jul 2016) $ 00039 00040 Copyright (c) 2013, Linear Technology Corp.(LTC) 00041 All rights reserved. 00042 00043 Redistribution and use in source and binary forms, with or without 00044 modification, are permitted provided that the following conditions are met: 00045 00046 1. Redistributions of source code must retain the above copyright notice, this 00047 list of conditions and the following disclaimer. 00048 2. Redistributions in binary form must reproduce the above copyright notice, 00049 this list of conditions and the following disclaimer in the documentation 00050 and/or other materials provided with the distribution. 00051 00052 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00053 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00054 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00055 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 00056 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00057 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00058 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00059 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00060 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00061 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00062 00063 The views and conclusions contained in the software and documentation are those 00064 of the authors and should not be interpreted as representing official policies, 00065 either expressed or implied, of Linear Technology Corp. 00066 00067 The Linear Technology Linduino is not affiliated with the official Arduino team. 00068 However, the Linduino is only possible because of the Arduino team's commitment 00069 to the open-source community. Please, visit http://www.arduino.cc and 00070 http://store.arduino.cc , and consider a purchase that will help fund their 00071 ongoing work. 00072 */ 00073 00074 //! @defgroup LT_I2C LT_I2C: Routines to Communicate With ATmega328P's hardware I2C port. 00075 00076 /*! @file 00077 @ingroup LT_I2C 00078 Library for LT_I2C: Routines to Communicate With ATmega328P's hardware I2C port. 00079 */ 00080 00081 //#include <Arduino.h> 00082 #include <stdint.h> 00083 //#include <util/delay.h> 00084 #include "Linduino.h " 00085 #include "LT_I2C.h " 00086 #include "mbed.h" 00087 00088 //! CPU master clock frequency 00089 #ifndef F_CPU 00090 #define F_CPU 16000000UL 00091 #endif 00092 00093 00094 LT_I2C::LT_I2C() { 00095 i2c = new I2C(I2C_SDA, I2C_SCL); 00096 i2c->frequency(400000); //maximum from datasheet for ltc2991 00097 } 00098 00099 LT_I2C::LT_I2C(PinName sda_, PinName scl_) { 00100 sda = sda_; 00101 scl = scl_; 00102 i2c = new I2C(sda_ , scl_); 00103 i2c->frequency(400000); //maximum from datasheet for ltc2991 00104 } 00105 00106 // Read a byte of data at register specified by "command", store in "value" 00107 int8_t LT_I2C::i2c_read_byte_data(uint8_t address, uint8_t command, uint8_t *value) 00108 { 00109 int8_t ret = 0; 00110 00111 char cmd1[1]; 00112 cmd1[0] = command; 00113 char v[1]; 00114 v[0] = 0; 00115 00116 ret |= i2c->write((address<<1) | I2C_WRITE_BIT, cmd1, 1, true); 00117 ret |= i2c->read((address<<1) | I2C_READ_BIT, v, 1, false); 00118 00119 *value = v[0]; 00120 00121 if (ret == 0) { 00122 return 0; //success 00123 } 00124 return(1); 00125 } 00126 00127 // Write a byte of data to register specified by "command" 00128 int8_t LT_I2C::i2c_write_byte_data(uint8_t address, uint8_t command, uint8_t value) 00129 { 00130 int8_t ret = 0; 00131 char cmd[2]; 00132 cmd[0] = command; 00133 cmd[1] = value; 00134 ret |= i2c->write((address<<1) | I2C_WRITE_BIT, cmd, 2, false); 00135 00136 if (ret == 0) { 00137 return 0; //success 00138 } 00139 return(1); 00140 } 00141 00142 int8_t LT_I2C::i2c_read_word_data(uint8_t address, uint8_t command, uint16_t *value) 00143 { 00144 int8_t ret = 0; 00145 00146 00147 union { 00148 char b[2]; 00149 uint16_t w; 00150 } data; 00151 00152 char writedata[2]; 00153 writedata[0] = command; //msb 00154 writedata[1] = command+1; //lsb 00155 00156 ret |= i2c->write((address << 1) | I2C_WRITE_BIT, &writedata[0], 1, true); 00157 ret |= i2c->read((address << 1) | I2C_READ_BIT, &data.b[1], 1, false); 00158 ret |= i2c->write((address << 1) | I2C_WRITE_BIT, &writedata[1], 1, true); 00159 ret |= i2c->read((address << 1) | I2C_READ_BIT, &data.b[0], 1, false); 00160 00161 00162 *value = data.w; 00163 00164 if (ret == 0) { 00165 return 0; 00166 } 00167 return(1); 00168 } 00169 00170 // Write a 16-bit word of data to register specified by "command" 00171 int8_t LT_I2C::i2c_write_word_data(uint8_t address, uint8_t command, uint16_t value) 00172 { 00173 int8_t ret = 0; 00174 00175 union 00176 { 00177 uint8_t b[2]; 00178 uint16_t w; 00179 } data; 00180 data.w = value; 00181 00182 char cmd[3]; 00183 cmd[0] = command; 00184 cmd[1] = data.b[1]; 00185 cmd[2] = data.b[0]; 00186 00187 ret |= i2c->write((address<<1) | I2C_WRITE_BIT, cmd, 3, false); 00188 00189 if (ret == 0) { 00190 return 0; 00191 } 00192 return(1); 00193 }
Generated on Wed Jul 13 2022 18:29:42 by
1.7.2
