MSS
/
testI2C
Simple I2C test program. open/close/read/write supported.
Diff: main.cpp
- Revision:
- 0:1a2637f8e2dd
- Child:
- 1:e105ceaee6ac
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Feb 09 00:23:22 2016 +0000 @@ -0,0 +1,115 @@ +#include "mbed.h" +#include <string.h> +#include <ctype.h> +#include "vt100.h" +#include "MSS.h" +#include "dumb_i2c.h" + +DUMB_I2C *i2c = 0 ; +vt100 *tty = 0 ; + +void doClose(void) +{ + if (i2c != 0) { + printf("Closing I2C at 0x%02X ... ", i2c->address()) ; + delete i2c ; + i2c = 0 ; + printf("Done\n") ; + } +} + +void doOpen(void) +{ + uint8_t address ; + scanf("%X", &address) ; + if (i2c != 0) { + doClose() ; + } + printf("Opening I2C at 0x%02X ... ", address) ; + i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ; + printf("Done\n") ; +} + +void doRead(void) +{ + int addr ; + int len ; + uint8_t *data ; + scanf("%X %X", &addr, &len) ; + if (len > 0) { + data = new uint8_t[len] ; + } + // 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)) ; + } + } + printf("\n") ; +} + +void doWrite(void) +{ + int addr ; + uint8_t len ; + uint8_t *data ; + scanf("%X %X", addr, &data) ; + i2c->write(addr, data, 1) ; +} + +void doStatus(void) +{ + if (i2c == 0) { + printf("i2c is not opened\n") ; + } else { + printf("i2c device at 0x%02X is opened\n", i2c->address()) ; + } +} + +void doHelp(void) +{ + printf("Simple I2C test program %s for %s\n", __DATE__, BOARD_NAME) ; + printf("\n === usage ===\n") ; + printf("open <7bit addr> : open i2c device at <7bit addr>\n") ; + printf("close : close currently opened i2c\n") ; + printf("read <addr> <len> : read <len> data from <addr>\n") ; + printf("write <addr> <data> : write <data> to register <addr>\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") ; + printf("\n") ; +} + +void doCommand(char *str) +{ + switch(*str) { + case 'o': case 'O': /* open */ + doOpen() ; break ; + case 'c': case 'C': /* close */ + doClose() ; break ; + case 'r': case 'R': /* read */ + doRead() ; break ; + case 'w': case 'W': /* write */ + doWrite() ; break ; + case 's': case 'S': /* status */ + doStatus() ; break ; + default: + doHelp() ; break ; + } +} + +int main() { + char cmd[32] ; + tty = new vt100() ; + tty->cls() ; + doHelp() ; + while(1) { + printf("> ") ; + scanf("%s", cmd) ; + doCommand(cmd) ; + } +}