Simple I2C test program. open/close/read/write supported.

Dependencies:   mbed vt100

Committer:
Rhyme
Date:
Tue Feb 09 00:23:22 2016 +0000
Revision:
0:1a2637f8e2dd
Child:
1:e105ceaee6ac
First working version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:1a2637f8e2dd 1 #include "mbed.h"
Rhyme 0:1a2637f8e2dd 2 #include "dumb_i2c.h"
Rhyme 0:1a2637f8e2dd 3
Rhyme 0:1a2637f8e2dd 4 DUMB_I2C::DUMB_I2C(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) {
Rhyme 0:1a2637f8e2dd 5 // activate the peripheral
Rhyme 0:1a2637f8e2dd 6 }
Rhyme 0:1a2637f8e2dd 7
Rhyme 0:1a2637f8e2dd 8 DUMB_I2C::~DUMB_I2C() { }
Rhyme 0:1a2637f8e2dd 9
Rhyme 0:1a2637f8e2dd 10 uint8_t DUMB_I2C::address(void)
Rhyme 0:1a2637f8e2dd 11 {
Rhyme 0:1a2637f8e2dd 12 return( m_addr >> 1 ) ;
Rhyme 0:1a2637f8e2dd 13 }
Rhyme 0:1a2637f8e2dd 14
Rhyme 0:1a2637f8e2dd 15 void DUMB_I2C::read(int addr, uint8_t *data, int len)
Rhyme 0:1a2637f8e2dd 16 {
Rhyme 0:1a2637f8e2dd 17 readRegs(addr, data, len) ;
Rhyme 0:1a2637f8e2dd 18 }
Rhyme 0:1a2637f8e2dd 19
Rhyme 0:1a2637f8e2dd 20 void DUMB_I2C::write(int addr, uint8_t *data, int len)
Rhyme 0:1a2637f8e2dd 21 {
Rhyme 0:1a2637f8e2dd 22 uint8_t *buf ;
Rhyme 0:1a2637f8e2dd 23 buf = new uint8_t[len+1] ;
Rhyme 0:1a2637f8e2dd 24 buf[0] = addr ;
Rhyme 0:1a2637f8e2dd 25 for (int i = 0 ; i < len ; i++ ) {
Rhyme 0:1a2637f8e2dd 26 buf[i+1] = data[i] ;
Rhyme 0:1a2637f8e2dd 27 }
Rhyme 0:1a2637f8e2dd 28 writeRegs(buf, len+1) ;
Rhyme 0:1a2637f8e2dd 29 delete buf ;
Rhyme 0:1a2637f8e2dd 30 }
Rhyme 0:1a2637f8e2dd 31
Rhyme 0:1a2637f8e2dd 32 void DUMB_I2C::readRegs(int addr, uint8_t * data, int len) {
Rhyme 0:1a2637f8e2dd 33 char t[1] = {addr};
Rhyme 0:1a2637f8e2dd 34 m_i2c.write(m_addr, t, 1, true);
Rhyme 0:1a2637f8e2dd 35 m_i2c.read(m_addr, (char *)data, len);
Rhyme 0:1a2637f8e2dd 36 }
Rhyme 0:1a2637f8e2dd 37
Rhyme 0:1a2637f8e2dd 38 void DUMB_I2C::writeRegs(uint8_t * data, int len) {
Rhyme 0:1a2637f8e2dd 39 m_i2c.write(m_addr, (char *)data, len);
Rhyme 0:1a2637f8e2dd 40 }