MSS
/
testI2C
Simple I2C test program. open/close/read/write supported.
dumb_i2c.cpp@2:218e22a54982, 2016-04-05 (annotated)
- Committer:
- Rhyme
- Date:
- Tue Apr 05 06:53:27 2016 +0000
- Revision:
- 2:218e22a54982
- Parent:
- 1:e105ceaee6ac
condition check for read/write and bus scan added;
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:218e22a54982 | 28 | int DUMB_I2C::read(int addr, uint8_t *data, int len) |
Rhyme | 0:1a2637f8e2dd | 29 | { |
Rhyme | 2:218e22a54982 | 30 | int result ; |
Rhyme | 2:218e22a54982 | 31 | result = readRegs(addr, data, len) ; |
Rhyme | 2:218e22a54982 | 32 | return( result ) ; |
Rhyme | 0:1a2637f8e2dd | 33 | } |
Rhyme | 0:1a2637f8e2dd | 34 | |
Rhyme | 2:218e22a54982 | 35 | int DUMB_I2C::write(int addr, uint8_t *data, int len) |
Rhyme | 0:1a2637f8e2dd | 36 | { |
Rhyme | 0:1a2637f8e2dd | 37 | uint8_t *buf ; |
Rhyme | 2:218e22a54982 | 38 | int ack ; |
Rhyme | 0:1a2637f8e2dd | 39 | buf = new uint8_t[len+1] ; |
Rhyme | 0:1a2637f8e2dd | 40 | buf[0] = addr ; |
Rhyme | 0:1a2637f8e2dd | 41 | for (int i = 0 ; i < len ; i++ ) { |
Rhyme | 0:1a2637f8e2dd | 42 | buf[i+1] = data[i] ; |
Rhyme | 0:1a2637f8e2dd | 43 | } |
Rhyme | 2:218e22a54982 | 44 | ack = writeRegs(buf, len+1) ; |
Rhyme | 0:1a2637f8e2dd | 45 | delete buf ; |
Rhyme | 2:218e22a54982 | 46 | return( ack ) ; |
Rhyme | 0:1a2637f8e2dd | 47 | } |
Rhyme | 0:1a2637f8e2dd | 48 | |
Rhyme | 2:218e22a54982 | 49 | int DUMB_I2C::readRegs(int addr, uint8_t * data, int len) { |
Rhyme | 2:218e22a54982 | 50 | int result ; |
Rhyme | 0:1a2637f8e2dd | 51 | char t[1] = {addr}; |
Rhyme | 0:1a2637f8e2dd | 52 | m_i2c.write(m_addr, t, 1, true); |
Rhyme | 2:218e22a54982 | 53 | result = m_i2c.read(m_addr, (char *)data, len); |
Rhyme | 2:218e22a54982 | 54 | return( result ) ; |
Rhyme | 0:1a2637f8e2dd | 55 | } |
Rhyme | 0:1a2637f8e2dd | 56 | |
Rhyme | 2:218e22a54982 | 57 | int DUMB_I2C::writeRegs(uint8_t * data, int len) { |
Rhyme | 2:218e22a54982 | 58 | int ack ; |
Rhyme | 2:218e22a54982 | 59 | ack = m_i2c.write(m_addr, (char *)data, len); |
Rhyme | 2:218e22a54982 | 60 | return( ack ) ; |
Rhyme | 0:1a2637f8e2dd | 61 | } |