Simple I2C test program. open/close/read/write supported.

Dependencies:   mbed vt100

Committer:
Rhyme
Date:
Tue Apr 05 02:11:20 2016 +0000
Revision:
1:e105ceaee6ac
Parent:
0:1a2637f8e2dd
Child:
2:218e22a54982
doWrite bug fix;

Who changed what in which revision?

UserRevisionLine numberNew 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 1:e105ceaee6ac 38 if (i2c == 0) {
Rhyme 1:e105ceaee6ac 39 printf("I2C is not opened\n") ;
Rhyme 1:e105ceaee6ac 40 return ;
Rhyme 1:e105ceaee6ac 41 }
Rhyme 0:1a2637f8e2dd 42 scanf("%X %X", &addr, &len) ;
Rhyme 0:1a2637f8e2dd 43 if (len > 0) {
Rhyme 0:1a2637f8e2dd 44 data = new uint8_t[len] ;
Rhyme 0:1a2637f8e2dd 45 }
Rhyme 0:1a2637f8e2dd 46 // i2c->read(addr, data, len) ;
Rhyme 0:1a2637f8e2dd 47 printf("0x%02X : ", (unsigned int)addr) ;
Rhyme 0:1a2637f8e2dd 48 for (int i = 0 ; i < len ; i++ ) {
Rhyme 0:1a2637f8e2dd 49 i2c->read(addr+i, &data[i], 1) ;
Rhyme 0:1a2637f8e2dd 50 printf("%02X ", data[i]) ;
Rhyme 0:1a2637f8e2dd 51 if (((i+1) < len)&&(((i+1)%0x10) == 0)) {
Rhyme 0:1a2637f8e2dd 52 printf("\n") ;
Rhyme 0:1a2637f8e2dd 53 printf("0x%02X : ", (unsigned int)(addr + i + 1)) ;
Rhyme 0:1a2637f8e2dd 54 }
Rhyme 0:1a2637f8e2dd 55 }
Rhyme 0:1a2637f8e2dd 56 printf("\n") ;
Rhyme 0:1a2637f8e2dd 57 }
Rhyme 0:1a2637f8e2dd 58
Rhyme 0:1a2637f8e2dd 59 void doWrite(void)
Rhyme 0:1a2637f8e2dd 60 {
Rhyme 0:1a2637f8e2dd 61 int addr ;
Rhyme 0:1a2637f8e2dd 62 uint8_t len ;
Rhyme 1:e105ceaee6ac 63 uint8_t data ;
Rhyme 1:e105ceaee6ac 64 if (i2c == 0) {
Rhyme 1:e105ceaee6ac 65 printf("I2C is not opened\n") ;
Rhyme 1:e105ceaee6ac 66 return ;
Rhyme 1:e105ceaee6ac 67 }
Rhyme 1:e105ceaee6ac 68 scanf("%X %X", &addr, &data) ;
Rhyme 1:e105ceaee6ac 69 i2c->write(addr, &data, 1) ;
Rhyme 0:1a2637f8e2dd 70 }
Rhyme 0:1a2637f8e2dd 71
Rhyme 0:1a2637f8e2dd 72 void doStatus(void)
Rhyme 0:1a2637f8e2dd 73 {
Rhyme 0:1a2637f8e2dd 74 if (i2c == 0) {
Rhyme 0:1a2637f8e2dd 75 printf("i2c is not opened\n") ;
Rhyme 0:1a2637f8e2dd 76 } else {
Rhyme 1:e105ceaee6ac 77 printf("i2c device at 0x%02X is opened at %d Hz\n",
Rhyme 1:e105ceaee6ac 78 i2c->address(), i2c->frequency()) ;
Rhyme 0:1a2637f8e2dd 79 }
Rhyme 0:1a2637f8e2dd 80 }
Rhyme 0:1a2637f8e2dd 81
Rhyme 0:1a2637f8e2dd 82 void doHelp(void)
Rhyme 0:1a2637f8e2dd 83 {
Rhyme 0:1a2637f8e2dd 84 printf("Simple I2C test program %s for %s\n", __DATE__, BOARD_NAME) ;
Rhyme 0:1a2637f8e2dd 85 printf("\n === usage ===\n") ;
Rhyme 0:1a2637f8e2dd 86 printf("open <7bit addr> : open i2c device at <7bit addr>\n") ;
Rhyme 0:1a2637f8e2dd 87 printf("close : close currently opened i2c\n") ;
Rhyme 0:1a2637f8e2dd 88 printf("read <addr> <len> : read <len> data from <addr>\n") ;
Rhyme 0:1a2637f8e2dd 89 printf("write <addr> <data> : write <data> to register <addr>\n") ;
Rhyme 1:e105ceaee6ac 90 printf("frequency <freq> : change frequency to <freq> Hz\n") ;
Rhyme 0:1a2637f8e2dd 91 printf("status : print current status\n") ;
Rhyme 0:1a2637f8e2dd 92 printf("help : print this help\n") ;
Rhyme 0:1a2637f8e2dd 93 printf("\nPlease set local-echo to see what you are typing.\n") ;
Rhyme 0:1a2637f8e2dd 94 printf("\n") ;
Rhyme 0:1a2637f8e2dd 95 }
Rhyme 0:1a2637f8e2dd 96
Rhyme 1:e105ceaee6ac 97 void doFreq(void)
Rhyme 1:e105ceaee6ac 98 {
Rhyme 1:e105ceaee6ac 99 int freq = 0 ;
Rhyme 1:e105ceaee6ac 100 scanf("%d", &freq) ;
Rhyme 1:e105ceaee6ac 101 if (i2c != 0) {
Rhyme 1:e105ceaee6ac 102 i2c->frequency(freq) ;
Rhyme 1:e105ceaee6ac 103 }
Rhyme 1:e105ceaee6ac 104 }
Rhyme 1:e105ceaee6ac 105
Rhyme 0:1a2637f8e2dd 106 void doCommand(char *str)
Rhyme 0:1a2637f8e2dd 107 {
Rhyme 0:1a2637f8e2dd 108 switch(*str) {
Rhyme 0:1a2637f8e2dd 109 case 'o': case 'O': /* open */
Rhyme 1:e105ceaee6ac 110 doOpen() ; break ;
Rhyme 0:1a2637f8e2dd 111 case 'c': case 'C': /* close */
Rhyme 1:e105ceaee6ac 112 doClose() ; break ;
Rhyme 0:1a2637f8e2dd 113 case 'r': case 'R': /* read */
Rhyme 1:e105ceaee6ac 114 doRead() ; break ;
Rhyme 0:1a2637f8e2dd 115 case 'w': case 'W': /* write */
Rhyme 1:e105ceaee6ac 116 doWrite() ; break ;
Rhyme 0:1a2637f8e2dd 117 case 's': case 'S': /* status */
Rhyme 0:1a2637f8e2dd 118 doStatus() ; break ;
Rhyme 1:e105ceaee6ac 119 case 'f': case 'F': /* Frequency */
Rhyme 1:e105ceaee6ac 120 doFreq() ; break ;
Rhyme 0:1a2637f8e2dd 121 default:
Rhyme 0:1a2637f8e2dd 122 doHelp() ; break ;
Rhyme 0:1a2637f8e2dd 123 }
Rhyme 0:1a2637f8e2dd 124 }
Rhyme 0:1a2637f8e2dd 125
Rhyme 0:1a2637f8e2dd 126 int main() {
Rhyme 0:1a2637f8e2dd 127 char cmd[32] ;
Rhyme 0:1a2637f8e2dd 128 tty = new vt100() ;
Rhyme 0:1a2637f8e2dd 129 tty->cls() ;
Rhyme 0:1a2637f8e2dd 130 doHelp() ;
Rhyme 0:1a2637f8e2dd 131 while(1) {
Rhyme 0:1a2637f8e2dd 132 printf("> ") ;
Rhyme 0:1a2637f8e2dd 133 scanf("%s", cmd) ;
Rhyme 0:1a2637f8e2dd 134 doCommand(cmd) ;
Rhyme 0:1a2637f8e2dd 135 }
Rhyme 0:1a2637f8e2dd 136 }