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.
Dependencies: FXOS8700CQ HIH6130 IS31SE5000 MAG3110 MAX44000 MAX44005 MAX44008 MMA8451Q MMA8452Q MPL3115A2 VEML6040 VEML6075 mbed vt100 LM75B FXAS21002 MAX30101 VCNL4020 VCNL4100
dumb_i2c.cpp
00001 #include "mbed.h" 00002 #include "dumb_i2c.h" 00003 00004 DUMB_I2C::DUMB_I2C(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) { 00005 // activate the peripheral 00006 freq = 100000 ; /* 100KHz */ 00007 m_i2c.frequency(freq) ; 00008 } 00009 00010 DUMB_I2C::~DUMB_I2C() { } 00011 00012 uint8_t DUMB_I2C::address(void) 00013 { 00014 return( m_addr >> 1 ) ; 00015 } 00016 00017 void DUMB_I2C::frequency(uint32_t f) 00018 { 00019 freq = f ; 00020 m_i2c.frequency(freq) ; 00021 } 00022 00023 uint32_t DUMB_I2C::frequency(void) 00024 { 00025 return(freq) ; 00026 } 00027 00028 void DUMB_I2C::start(void) 00029 { 00030 m_i2c.start() ; 00031 } 00032 00033 void DUMB_I2C::stop(void) 00034 { 00035 m_i2c.stop() ; 00036 } 00037 00038 int DUMB_I2C::read(int addr, uint8_t *data, int len) 00039 { 00040 int result ; 00041 result = readRegs(addr, data, len) ; 00042 return( result ) ; 00043 } 00044 00045 int DUMB_I2C::write(int addr, uint8_t *data, int len) 00046 { 00047 uint8_t *buf ; 00048 int ack ; 00049 buf = new uint8_t[len+1] ; 00050 buf[0] = addr ; 00051 for (int i = 0 ; i < len ; i++ ) { 00052 buf[i+1] = data[i] ; 00053 } 00054 ack = writeRegs(buf, len+1) ; 00055 delete buf ; 00056 return( ack ) ; 00057 } 00058 00059 int DUMB_I2C::readRegs(int addr, uint8_t * data, int len) { 00060 int result ; 00061 char t[1] = {addr}; 00062 m_i2c.write(m_addr, t, 1, true); 00063 result = m_i2c.read(m_addr, (char *)data, len); 00064 return( result ) ; 00065 } 00066 00067 int DUMB_I2C::writeRegs(uint8_t * data, int len) { 00068 int ack ; 00069 ack = m_i2c.write(m_addr, (char *)data, len); 00070 return( ack ) ; 00071 }
Generated on Fri Jul 15 2022 06:46:16 by
1.7.2