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

Dependencies:   mbed vt100

Committer:
Rhyme
Date:
Tue Apr 05 02:11:20 2016 +0000
Revision:
1:e105ceaee6ac
Parent:
0:1a2637f8e2dd
Child:
2:218e22a54982
doWrite bug fix;

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 1:e105ceaee6ac 6 freq = 100000 ; /* 100KHz */
Rhyme 1:e105ceaee6ac 7 m_i2c.frequency(freq) ;
Rhyme 0:1a2637f8e2dd 8 }
Rhyme 0:1a2637f8e2dd 9
Rhyme 0:1a2637f8e2dd 10 DUMB_I2C::~DUMB_I2C() { }
Rhyme 0:1a2637f8e2dd 11
Rhyme 0:1a2637f8e2dd 12 uint8_t DUMB_I2C::address(void)
Rhyme 0:1a2637f8e2dd 13 {
Rhyme 0:1a2637f8e2dd 14 return( m_addr >> 1 ) ;
Rhyme 0:1a2637f8e2dd 15 }
Rhyme 0:1a2637f8e2dd 16
Rhyme 1:e105ceaee6ac 17 void DUMB_I2C::frequency(uint32_t f)
Rhyme 1:e105ceaee6ac 18 {
Rhyme 1:e105ceaee6ac 19 freq = f ;
Rhyme 1:e105ceaee6ac 20 m_i2c.frequency(freq) ;
Rhyme 1:e105ceaee6ac 21 }
Rhyme 1:e105ceaee6ac 22
Rhyme 1:e105ceaee6ac 23 uint32_t DUMB_I2C::frequency(void)
Rhyme 1:e105ceaee6ac 24 {
Rhyme 1:e105ceaee6ac 25 return(freq) ;
Rhyme 1:e105ceaee6ac 26 }
Rhyme 1:e105ceaee6ac 27
Rhyme 0:1a2637f8e2dd 28 void DUMB_I2C::read(int addr, uint8_t *data, int len)
Rhyme 0:1a2637f8e2dd 29 {
Rhyme 0:1a2637f8e2dd 30 readRegs(addr, data, len) ;
Rhyme 0:1a2637f8e2dd 31 }
Rhyme 0:1a2637f8e2dd 32
Rhyme 0:1a2637f8e2dd 33 void DUMB_I2C::write(int addr, uint8_t *data, int len)
Rhyme 0:1a2637f8e2dd 34 {
Rhyme 0:1a2637f8e2dd 35 uint8_t *buf ;
Rhyme 0:1a2637f8e2dd 36 buf = new uint8_t[len+1] ;
Rhyme 0:1a2637f8e2dd 37 buf[0] = addr ;
Rhyme 0:1a2637f8e2dd 38 for (int i = 0 ; i < len ; i++ ) {
Rhyme 0:1a2637f8e2dd 39 buf[i+1] = data[i] ;
Rhyme 0:1a2637f8e2dd 40 }
Rhyme 0:1a2637f8e2dd 41 writeRegs(buf, len+1) ;
Rhyme 0:1a2637f8e2dd 42 delete buf ;
Rhyme 0:1a2637f8e2dd 43 }
Rhyme 0:1a2637f8e2dd 44
Rhyme 0:1a2637f8e2dd 45 void DUMB_I2C::readRegs(int addr, uint8_t * data, int len) {
Rhyme 0:1a2637f8e2dd 46 char t[1] = {addr};
Rhyme 0:1a2637f8e2dd 47 m_i2c.write(m_addr, t, 1, true);
Rhyme 0:1a2637f8e2dd 48 m_i2c.read(m_addr, (char *)data, len);
Rhyme 0:1a2637f8e2dd 49 }
Rhyme 0:1a2637f8e2dd 50
Rhyme 0:1a2637f8e2dd 51 void DUMB_I2C::writeRegs(uint8_t * data, int len) {
Rhyme 0:1a2637f8e2dd 52 m_i2c.write(m_addr, (char *)data, len);
Rhyme 0:1a2637f8e2dd 53 }