MSS
/
testI2C
Simple I2C test program. open/close/read/write supported.
main.cpp@0:1a2637f8e2dd, 2016-02-09 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:1a2637f8e2dd | 1 | #include "mbed.h" |
Rhyme | 0:1a2637f8e2dd | 2 | #include <string.h> |
Rhyme | 0:1a2637f8e2dd | 3 | #include <ctype.h> |
Rhyme | 0:1a2637f8e2dd | 4 | #include "vt100.h" |
Rhyme | 0:1a2637f8e2dd | 5 | #include "MSS.h" |
Rhyme | 0:1a2637f8e2dd | 6 | #include "dumb_i2c.h" |
Rhyme | 0:1a2637f8e2dd | 7 | |
Rhyme | 0:1a2637f8e2dd | 8 | DUMB_I2C *i2c = 0 ; |
Rhyme | 0:1a2637f8e2dd | 9 | vt100 *tty = 0 ; |
Rhyme | 0:1a2637f8e2dd | 10 | |
Rhyme | 0:1a2637f8e2dd | 11 | void doClose(void) |
Rhyme | 0:1a2637f8e2dd | 12 | { |
Rhyme | 0:1a2637f8e2dd | 13 | if (i2c != 0) { |
Rhyme | 0:1a2637f8e2dd | 14 | printf("Closing I2C at 0x%02X ... ", i2c->address()) ; |
Rhyme | 0:1a2637f8e2dd | 15 | delete i2c ; |
Rhyme | 0:1a2637f8e2dd | 16 | i2c = 0 ; |
Rhyme | 0:1a2637f8e2dd | 17 | printf("Done\n") ; |
Rhyme | 0:1a2637f8e2dd | 18 | } |
Rhyme | 0:1a2637f8e2dd | 19 | } |
Rhyme | 0:1a2637f8e2dd | 20 | |
Rhyme | 0:1a2637f8e2dd | 21 | void doOpen(void) |
Rhyme | 0:1a2637f8e2dd | 22 | { |
Rhyme | 0:1a2637f8e2dd | 23 | uint8_t address ; |
Rhyme | 0:1a2637f8e2dd | 24 | scanf("%X", &address) ; |
Rhyme | 0:1a2637f8e2dd | 25 | if (i2c != 0) { |
Rhyme | 0:1a2637f8e2dd | 26 | doClose() ; |
Rhyme | 0:1a2637f8e2dd | 27 | } |
Rhyme | 0:1a2637f8e2dd | 28 | printf("Opening I2C at 0x%02X ... ", address) ; |
Rhyme | 0:1a2637f8e2dd | 29 | i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ; |
Rhyme | 0:1a2637f8e2dd | 30 | printf("Done\n") ; |
Rhyme | 0:1a2637f8e2dd | 31 | } |
Rhyme | 0:1a2637f8e2dd | 32 | |
Rhyme | 0:1a2637f8e2dd | 33 | void doRead(void) |
Rhyme | 0:1a2637f8e2dd | 34 | { |
Rhyme | 0:1a2637f8e2dd | 35 | int addr ; |
Rhyme | 0:1a2637f8e2dd | 36 | int len ; |
Rhyme | 0:1a2637f8e2dd | 37 | uint8_t *data ; |
Rhyme | 0:1a2637f8e2dd | 38 | scanf("%X %X", &addr, &len) ; |
Rhyme | 0:1a2637f8e2dd | 39 | if (len > 0) { |
Rhyme | 0:1a2637f8e2dd | 40 | data = new uint8_t[len] ; |
Rhyme | 0:1a2637f8e2dd | 41 | } |
Rhyme | 0:1a2637f8e2dd | 42 | // i2c->read(addr, data, len) ; |
Rhyme | 0:1a2637f8e2dd | 43 | printf("0x%02X : ", (unsigned int)addr) ; |
Rhyme | 0:1a2637f8e2dd | 44 | for (int i = 0 ; i < len ; i++ ) { |
Rhyme | 0:1a2637f8e2dd | 45 | i2c->read(addr+i, &data[i], 1) ; |
Rhyme | 0:1a2637f8e2dd | 46 | printf("%02X ", data[i]) ; |
Rhyme | 0:1a2637f8e2dd | 47 | if (((i+1) < len)&&(((i+1)%0x10) == 0)) { |
Rhyme | 0:1a2637f8e2dd | 48 | printf("\n") ; |
Rhyme | 0:1a2637f8e2dd | 49 | printf("0x%02X : ", (unsigned int)(addr + i + 1)) ; |
Rhyme | 0:1a2637f8e2dd | 50 | } |
Rhyme | 0:1a2637f8e2dd | 51 | } |
Rhyme | 0:1a2637f8e2dd | 52 | printf("\n") ; |
Rhyme | 0:1a2637f8e2dd | 53 | } |
Rhyme | 0:1a2637f8e2dd | 54 | |
Rhyme | 0:1a2637f8e2dd | 55 | void doWrite(void) |
Rhyme | 0:1a2637f8e2dd | 56 | { |
Rhyme | 0:1a2637f8e2dd | 57 | int addr ; |
Rhyme | 0:1a2637f8e2dd | 58 | uint8_t len ; |
Rhyme | 0:1a2637f8e2dd | 59 | uint8_t *data ; |
Rhyme | 0:1a2637f8e2dd | 60 | scanf("%X %X", addr, &data) ; |
Rhyme | 0:1a2637f8e2dd | 61 | i2c->write(addr, data, 1) ; |
Rhyme | 0:1a2637f8e2dd | 62 | } |
Rhyme | 0:1a2637f8e2dd | 63 | |
Rhyme | 0:1a2637f8e2dd | 64 | void doStatus(void) |
Rhyme | 0:1a2637f8e2dd | 65 | { |
Rhyme | 0:1a2637f8e2dd | 66 | if (i2c == 0) { |
Rhyme | 0:1a2637f8e2dd | 67 | printf("i2c is not opened\n") ; |
Rhyme | 0:1a2637f8e2dd | 68 | } else { |
Rhyme | 0:1a2637f8e2dd | 69 | printf("i2c device at 0x%02X is opened\n", i2c->address()) ; |
Rhyme | 0:1a2637f8e2dd | 70 | } |
Rhyme | 0:1a2637f8e2dd | 71 | } |
Rhyme | 0:1a2637f8e2dd | 72 | |
Rhyme | 0:1a2637f8e2dd | 73 | void doHelp(void) |
Rhyme | 0:1a2637f8e2dd | 74 | { |
Rhyme | 0:1a2637f8e2dd | 75 | printf("Simple I2C test program %s for %s\n", __DATE__, BOARD_NAME) ; |
Rhyme | 0:1a2637f8e2dd | 76 | printf("\n === usage ===\n") ; |
Rhyme | 0:1a2637f8e2dd | 77 | printf("open <7bit addr> : open i2c device at <7bit addr>\n") ; |
Rhyme | 0:1a2637f8e2dd | 78 | printf("close : close currently opened i2c\n") ; |
Rhyme | 0:1a2637f8e2dd | 79 | printf("read <addr> <len> : read <len> data from <addr>\n") ; |
Rhyme | 0:1a2637f8e2dd | 80 | printf("write <addr> <data> : write <data> to register <addr>\n") ; |
Rhyme | 0:1a2637f8e2dd | 81 | printf("status : print current status\n") ; |
Rhyme | 0:1a2637f8e2dd | 82 | printf("help : print this help\n") ; |
Rhyme | 0:1a2637f8e2dd | 83 | printf("\nPlease set local-echo to see what you are typing.\n") ; |
Rhyme | 0:1a2637f8e2dd | 84 | printf("\n") ; |
Rhyme | 0:1a2637f8e2dd | 85 | } |
Rhyme | 0:1a2637f8e2dd | 86 | |
Rhyme | 0:1a2637f8e2dd | 87 | void doCommand(char *str) |
Rhyme | 0:1a2637f8e2dd | 88 | { |
Rhyme | 0:1a2637f8e2dd | 89 | switch(*str) { |
Rhyme | 0:1a2637f8e2dd | 90 | case 'o': case 'O': /* open */ |
Rhyme | 0:1a2637f8e2dd | 91 | doOpen() ; break ; |
Rhyme | 0:1a2637f8e2dd | 92 | case 'c': case 'C': /* close */ |
Rhyme | 0:1a2637f8e2dd | 93 | doClose() ; break ; |
Rhyme | 0:1a2637f8e2dd | 94 | case 'r': case 'R': /* read */ |
Rhyme | 0:1a2637f8e2dd | 95 | doRead() ; break ; |
Rhyme | 0:1a2637f8e2dd | 96 | case 'w': case 'W': /* write */ |
Rhyme | 0:1a2637f8e2dd | 97 | doWrite() ; break ; |
Rhyme | 0:1a2637f8e2dd | 98 | case 's': case 'S': /* status */ |
Rhyme | 0:1a2637f8e2dd | 99 | doStatus() ; break ; |
Rhyme | 0:1a2637f8e2dd | 100 | default: |
Rhyme | 0:1a2637f8e2dd | 101 | doHelp() ; break ; |
Rhyme | 0:1a2637f8e2dd | 102 | } |
Rhyme | 0:1a2637f8e2dd | 103 | } |
Rhyme | 0:1a2637f8e2dd | 104 | |
Rhyme | 0:1a2637f8e2dd | 105 | int main() { |
Rhyme | 0:1a2637f8e2dd | 106 | char cmd[32] ; |
Rhyme | 0:1a2637f8e2dd | 107 | tty = new vt100() ; |
Rhyme | 0:1a2637f8e2dd | 108 | tty->cls() ; |
Rhyme | 0:1a2637f8e2dd | 109 | doHelp() ; |
Rhyme | 0:1a2637f8e2dd | 110 | while(1) { |
Rhyme | 0:1a2637f8e2dd | 111 | printf("> ") ; |
Rhyme | 0:1a2637f8e2dd | 112 | scanf("%s", cmd) ; |
Rhyme | 0:1a2637f8e2dd | 113 | doCommand(cmd) ; |
Rhyme | 0:1a2637f8e2dd | 114 | } |
Rhyme | 0:1a2637f8e2dd | 115 | } |