MSS
/
testI2C
Simple I2C test program. open/close/read/write supported.
Revision 2:218e22a54982, committed 2016-04-05
- Comitter:
- Rhyme
- Date:
- Tue Apr 05 06:53:27 2016 +0000
- Parent:
- 1:e105ceaee6ac
- Commit message:
- condition check for read/write and bus scan added;
Changed in this revision
--- a/dumb_i2c.cpp Tue Apr 05 02:11:20 2016 +0000 +++ b/dumb_i2c.cpp Tue Apr 05 06:53:27 2016 +0000 @@ -25,29 +25,37 @@ return(freq) ; } -void DUMB_I2C::read(int addr, uint8_t *data, int len) +int DUMB_I2C::read(int addr, uint8_t *data, int len) { - readRegs(addr, data, len) ; + int result ; + result = readRegs(addr, data, len) ; + return( result ) ; } -void DUMB_I2C::write(int addr, uint8_t *data, int len) +int DUMB_I2C::write(int addr, uint8_t *data, int len) { uint8_t *buf ; + int ack ; buf = new uint8_t[len+1] ; buf[0] = addr ; for (int i = 0 ; i < len ; i++ ) { buf[i+1] = data[i] ; } - writeRegs(buf, len+1) ; + ack = writeRegs(buf, len+1) ; delete buf ; + return( ack ) ; } -void DUMB_I2C::readRegs(int addr, uint8_t * data, int len) { +int DUMB_I2C::readRegs(int addr, uint8_t * data, int len) { + int result ; char t[1] = {addr}; m_i2c.write(m_addr, t, 1, true); - m_i2c.read(m_addr, (char *)data, len); + result = m_i2c.read(m_addr, (char *)data, len); + return( result ) ; } -void DUMB_I2C::writeRegs(uint8_t * data, int len) { - m_i2c.write(m_addr, (char *)data, len); +int DUMB_I2C::writeRegs(uint8_t * data, int len) { + int ack ; + ack = m_i2c.write(m_addr, (char *)data, len); + return( ack ) ; } \ No newline at end of file
--- a/dumb_i2c.h Tue Apr 05 02:11:20 2016 +0000 +++ b/dumb_i2c.h Tue Apr 05 06:53:27 2016 +0000 @@ -21,14 +21,14 @@ void frequency(uint32_t f) ; uint32_t frequency(void) ; uint8_t address(void) ; - void read(int addr, uint8_t *data, int len) ; - void write(int addr, uint8_t *data, int len) ; + int read(int addr, uint8_t *data, int len) ; + int write(int addr, uint8_t *data, int len) ; private: I2C m_i2c; int m_addr; uint32_t freq ; - void readRegs(int addr, uint8_t * data, int len); - void writeRegs(uint8_t * data, int len); + int readRegs(int addr, uint8_t * data, int len); + int writeRegs(uint8_t * data, int len); }; #endif /* _DUMB_I2C_H_ */ \ No newline at end of file
--- a/main.cpp Tue Apr 05 02:11:20 2016 +0000 +++ b/main.cpp Tue Apr 05 06:53:27 2016 +0000 @@ -35,6 +35,7 @@ int addr ; int len ; uint8_t *data ; + int result ; if (i2c == 0) { printf("I2C is not opened\n") ; return ; @@ -46,12 +47,16 @@ // i2c->read(addr, data, len) ; printf("0x%02X : ", (unsigned int)addr) ; for (int i = 0 ; i < len ; i++ ) { - i2c->read(addr+i, &data[i], 1) ; - printf("%02X ", data[i]) ; - if (((i+1) < len)&&(((i+1)%0x10) == 0)) { - printf("\n") ; - printf("0x%02X : ", (unsigned int)(addr + i + 1)) ; - } + result = i2c->read(addr+i, &data[i], 1) ; + if (result == 0) { + printf("%02X ", data[i]) ; + if (((i+1) < len)&&(((i+1)%0x10) == 0)) { + printf("\n") ; + printf("0x%02X : ", (unsigned int)(addr + i + 1)) ; + } + } else { + printf("failed to read 0x%02X\n", addr+i) ; + } } printf("\n") ; } @@ -61,12 +66,17 @@ int addr ; uint8_t len ; uint8_t data ; - if (i2c == 0) { + int ack ; + if (i2c == 0) { printf("I2C is not opened\n") ; return ; } scanf("%X %X", &addr, &data) ; - i2c->write(addr, &data, 1) ; + ack = i2c->write(addr, &data, 1) ; + if (ack != 0) { + printf("NAK at writing data[0x%02X] to address[0x%02X]\n", + data, addr ) ; + } } void doStatus(void) @@ -88,6 +98,7 @@ printf("read <addr> <len> : read <len> data from <addr>\n") ; printf("write <addr> <data> : write <data> to register <addr>\n") ; printf("frequency <freq> : change frequency to <freq> Hz\n") ; + printf("bus : bus scan for existing I2C addresses\n") ; printf("status : print current status\n") ; printf("help : print this help\n") ; printf("\nPlease set local-echo to see what you are typing.\n") ; @@ -103,21 +114,47 @@ } } +void doBusScan(void) +{ + int address ; + uint8_t data ; + int result ; + if (i2c != 0) { + printf("Closing I2C at 0x%02X ... ", i2c->address()) ; + delete i2c ; + i2c = 0 ; + printf("Done\n") ; + } + + for (address = 1; address < 127 ; address++) { + i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ; + result = i2c->read(address, &data, 1) ; + if (result == 0) { + printf("%02X ", address) ; + } + delete i2c ; + i2c = 0 ; + } + printf("\n") ; +} + void doCommand(char *str) { switch(*str) { case 'o': case 'O': /* open */ - doOpen() ; break ; + doOpen() ; break ; case 'c': case 'C': /* close */ - doClose() ; break ; + doClose() ; break ; case 'r': case 'R': /* read */ - doRead() ; break ; + doRead() ; break ; case 'w': case 'W': /* write */ - doWrite() ; break ; + doWrite() ; break ; case 's': case 'S': /* status */ - doStatus() ; break ; + doStatus() ; break ; case 'f': case 'F': /* Frequency */ - doFreq() ; break ; + doFreq() ; break ; + case 'b': case 'B': /* Bus Scan */ + doBusScan() ; break ; default: doHelp() ; break ; }