MSS
/
testI2C
Simple I2C test program. open/close/read/write supported.
main.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 <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 | 2:218e22a54982 | 38 | int result ; |
Rhyme | 1:e105ceaee6ac | 39 | if (i2c == 0) { |
Rhyme | 1:e105ceaee6ac | 40 | printf("I2C is not opened\n") ; |
Rhyme | 1:e105ceaee6ac | 41 | return ; |
Rhyme | 1:e105ceaee6ac | 42 | } |
Rhyme | 0:1a2637f8e2dd | 43 | scanf("%X %X", &addr, &len) ; |
Rhyme | 0:1a2637f8e2dd | 44 | if (len > 0) { |
Rhyme | 0:1a2637f8e2dd | 45 | data = new uint8_t[len] ; |
Rhyme | 0:1a2637f8e2dd | 46 | } |
Rhyme | 0:1a2637f8e2dd | 47 | // i2c->read(addr, data, len) ; |
Rhyme | 0:1a2637f8e2dd | 48 | printf("0x%02X : ", (unsigned int)addr) ; |
Rhyme | 0:1a2637f8e2dd | 49 | for (int i = 0 ; i < len ; i++ ) { |
Rhyme | 2:218e22a54982 | 50 | result = i2c->read(addr+i, &data[i], 1) ; |
Rhyme | 2:218e22a54982 | 51 | if (result == 0) { |
Rhyme | 2:218e22a54982 | 52 | printf("%02X ", data[i]) ; |
Rhyme | 2:218e22a54982 | 53 | if (((i+1) < len)&&(((i+1)%0x10) == 0)) { |
Rhyme | 2:218e22a54982 | 54 | printf("\n") ; |
Rhyme | 2:218e22a54982 | 55 | printf("0x%02X : ", (unsigned int)(addr + i + 1)) ; |
Rhyme | 2:218e22a54982 | 56 | } |
Rhyme | 2:218e22a54982 | 57 | } else { |
Rhyme | 2:218e22a54982 | 58 | printf("failed to read 0x%02X\n", addr+i) ; |
Rhyme | 2:218e22a54982 | 59 | } |
Rhyme | 0:1a2637f8e2dd | 60 | } |
Rhyme | 0:1a2637f8e2dd | 61 | printf("\n") ; |
Rhyme | 0:1a2637f8e2dd | 62 | } |
Rhyme | 0:1a2637f8e2dd | 63 | |
Rhyme | 0:1a2637f8e2dd | 64 | void doWrite(void) |
Rhyme | 0:1a2637f8e2dd | 65 | { |
Rhyme | 0:1a2637f8e2dd | 66 | int addr ; |
Rhyme | 0:1a2637f8e2dd | 67 | uint8_t len ; |
Rhyme | 1:e105ceaee6ac | 68 | uint8_t data ; |
Rhyme | 2:218e22a54982 | 69 | int ack ; |
Rhyme | 2:218e22a54982 | 70 | if (i2c == 0) { |
Rhyme | 1:e105ceaee6ac | 71 | printf("I2C is not opened\n") ; |
Rhyme | 1:e105ceaee6ac | 72 | return ; |
Rhyme | 1:e105ceaee6ac | 73 | } |
Rhyme | 1:e105ceaee6ac | 74 | scanf("%X %X", &addr, &data) ; |
Rhyme | 2:218e22a54982 | 75 | ack = i2c->write(addr, &data, 1) ; |
Rhyme | 2:218e22a54982 | 76 | if (ack != 0) { |
Rhyme | 2:218e22a54982 | 77 | printf("NAK at writing data[0x%02X] to address[0x%02X]\n", |
Rhyme | 2:218e22a54982 | 78 | data, addr ) ; |
Rhyme | 2:218e22a54982 | 79 | } |
Rhyme | 0:1a2637f8e2dd | 80 | } |
Rhyme | 0:1a2637f8e2dd | 81 | |
Rhyme | 0:1a2637f8e2dd | 82 | void doStatus(void) |
Rhyme | 0:1a2637f8e2dd | 83 | { |
Rhyme | 0:1a2637f8e2dd | 84 | if (i2c == 0) { |
Rhyme | 0:1a2637f8e2dd | 85 | printf("i2c is not opened\n") ; |
Rhyme | 0:1a2637f8e2dd | 86 | } else { |
Rhyme | 1:e105ceaee6ac | 87 | printf("i2c device at 0x%02X is opened at %d Hz\n", |
Rhyme | 1:e105ceaee6ac | 88 | i2c->address(), i2c->frequency()) ; |
Rhyme | 0:1a2637f8e2dd | 89 | } |
Rhyme | 0:1a2637f8e2dd | 90 | } |
Rhyme | 0:1a2637f8e2dd | 91 | |
Rhyme | 0:1a2637f8e2dd | 92 | void doHelp(void) |
Rhyme | 0:1a2637f8e2dd | 93 | { |
Rhyme | 0:1a2637f8e2dd | 94 | printf("Simple I2C test program %s for %s\n", __DATE__, BOARD_NAME) ; |
Rhyme | 0:1a2637f8e2dd | 95 | printf("\n === usage ===\n") ; |
Rhyme | 0:1a2637f8e2dd | 96 | printf("open <7bit addr> : open i2c device at <7bit addr>\n") ; |
Rhyme | 0:1a2637f8e2dd | 97 | printf("close : close currently opened i2c\n") ; |
Rhyme | 0:1a2637f8e2dd | 98 | printf("read <addr> <len> : read <len> data from <addr>\n") ; |
Rhyme | 0:1a2637f8e2dd | 99 | printf("write <addr> <data> : write <data> to register <addr>\n") ; |
Rhyme | 1:e105ceaee6ac | 100 | printf("frequency <freq> : change frequency to <freq> Hz\n") ; |
Rhyme | 2:218e22a54982 | 101 | printf("bus : bus scan for existing I2C addresses\n") ; |
Rhyme | 0:1a2637f8e2dd | 102 | printf("status : print current status\n") ; |
Rhyme | 0:1a2637f8e2dd | 103 | printf("help : print this help\n") ; |
Rhyme | 0:1a2637f8e2dd | 104 | printf("\nPlease set local-echo to see what you are typing.\n") ; |
Rhyme | 0:1a2637f8e2dd | 105 | printf("\n") ; |
Rhyme | 0:1a2637f8e2dd | 106 | } |
Rhyme | 0:1a2637f8e2dd | 107 | |
Rhyme | 1:e105ceaee6ac | 108 | void doFreq(void) |
Rhyme | 1:e105ceaee6ac | 109 | { |
Rhyme | 1:e105ceaee6ac | 110 | int freq = 0 ; |
Rhyme | 1:e105ceaee6ac | 111 | scanf("%d", &freq) ; |
Rhyme | 1:e105ceaee6ac | 112 | if (i2c != 0) { |
Rhyme | 1:e105ceaee6ac | 113 | i2c->frequency(freq) ; |
Rhyme | 1:e105ceaee6ac | 114 | } |
Rhyme | 1:e105ceaee6ac | 115 | } |
Rhyme | 1:e105ceaee6ac | 116 | |
Rhyme | 2:218e22a54982 | 117 | void doBusScan(void) |
Rhyme | 2:218e22a54982 | 118 | { |
Rhyme | 2:218e22a54982 | 119 | int address ; |
Rhyme | 2:218e22a54982 | 120 | uint8_t data ; |
Rhyme | 2:218e22a54982 | 121 | int result ; |
Rhyme | 2:218e22a54982 | 122 | if (i2c != 0) { |
Rhyme | 2:218e22a54982 | 123 | printf("Closing I2C at 0x%02X ... ", i2c->address()) ; |
Rhyme | 2:218e22a54982 | 124 | delete i2c ; |
Rhyme | 2:218e22a54982 | 125 | i2c = 0 ; |
Rhyme | 2:218e22a54982 | 126 | printf("Done\n") ; |
Rhyme | 2:218e22a54982 | 127 | } |
Rhyme | 2:218e22a54982 | 128 | |
Rhyme | 2:218e22a54982 | 129 | for (address = 1; address < 127 ; address++) { |
Rhyme | 2:218e22a54982 | 130 | i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ; |
Rhyme | 2:218e22a54982 | 131 | result = i2c->read(address, &data, 1) ; |
Rhyme | 2:218e22a54982 | 132 | if (result == 0) { |
Rhyme | 2:218e22a54982 | 133 | printf("%02X ", address) ; |
Rhyme | 2:218e22a54982 | 134 | } |
Rhyme | 2:218e22a54982 | 135 | delete i2c ; |
Rhyme | 2:218e22a54982 | 136 | i2c = 0 ; |
Rhyme | 2:218e22a54982 | 137 | } |
Rhyme | 2:218e22a54982 | 138 | printf("\n") ; |
Rhyme | 2:218e22a54982 | 139 | } |
Rhyme | 2:218e22a54982 | 140 | |
Rhyme | 0:1a2637f8e2dd | 141 | void doCommand(char *str) |
Rhyme | 0:1a2637f8e2dd | 142 | { |
Rhyme | 0:1a2637f8e2dd | 143 | switch(*str) { |
Rhyme | 0:1a2637f8e2dd | 144 | case 'o': case 'O': /* open */ |
Rhyme | 2:218e22a54982 | 145 | doOpen() ; break ; |
Rhyme | 0:1a2637f8e2dd | 146 | case 'c': case 'C': /* close */ |
Rhyme | 2:218e22a54982 | 147 | doClose() ; break ; |
Rhyme | 0:1a2637f8e2dd | 148 | case 'r': case 'R': /* read */ |
Rhyme | 2:218e22a54982 | 149 | doRead() ; break ; |
Rhyme | 0:1a2637f8e2dd | 150 | case 'w': case 'W': /* write */ |
Rhyme | 2:218e22a54982 | 151 | doWrite() ; break ; |
Rhyme | 0:1a2637f8e2dd | 152 | case 's': case 'S': /* status */ |
Rhyme | 2:218e22a54982 | 153 | doStatus() ; break ; |
Rhyme | 1:e105ceaee6ac | 154 | case 'f': case 'F': /* Frequency */ |
Rhyme | 2:218e22a54982 | 155 | doFreq() ; break ; |
Rhyme | 2:218e22a54982 | 156 | case 'b': case 'B': /* Bus Scan */ |
Rhyme | 2:218e22a54982 | 157 | doBusScan() ; break ; |
Rhyme | 0:1a2637f8e2dd | 158 | default: |
Rhyme | 0:1a2637f8e2dd | 159 | doHelp() ; break ; |
Rhyme | 0:1a2637f8e2dd | 160 | } |
Rhyme | 0:1a2637f8e2dd | 161 | } |
Rhyme | 0:1a2637f8e2dd | 162 | |
Rhyme | 0:1a2637f8e2dd | 163 | int main() { |
Rhyme | 0:1a2637f8e2dd | 164 | char cmd[32] ; |
Rhyme | 0:1a2637f8e2dd | 165 | tty = new vt100() ; |
Rhyme | 0:1a2637f8e2dd | 166 | tty->cls() ; |
Rhyme | 0:1a2637f8e2dd | 167 | doHelp() ; |
Rhyme | 0:1a2637f8e2dd | 168 | while(1) { |
Rhyme | 0:1a2637f8e2dd | 169 | printf("> ") ; |
Rhyme | 0:1a2637f8e2dd | 170 | scanf("%s", cmd) ; |
Rhyme | 0:1a2637f8e2dd | 171 | doCommand(cmd) ; |
Rhyme | 0:1a2637f8e2dd | 172 | } |
Rhyme | 0:1a2637f8e2dd | 173 | } |