I2C

Dependencies:   TextLCD mbed

Fork of I2C_Debug_for_RealTerm by jim hamblen

Committer:
4180_1
Date:
Tue Mar 08 03:16:34 2011 +0000
Revision:
0:0c54b73b002c
Child:
1:be1498ba7fb4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:0c54b73b002c 1 #include "mbed.h"
4180_1 0:0c54b73b002c 2
4180_1 0:0c54b73b002c 3 // The program sends and receives I2C commands using RealTerm's I2C tab feature
4180_1 0:0c54b73b002c 4 // Run Realterm on PC and this program on mbed
4180_1 0:0c54b73b002c 5 // In RealTerm connect to the mbed's USB virtual COM port
4180_1 0:0c54b73b002c 6 // Switch to I2C tab
4180_1 0:0c54b73b002c 7 // Type in I2C address & data to read and write and see the response
4180_1 0:0c54b73b002c 8 // It is handy to debug complex I2C hardware setups
4180_1 0:0c54b73b002c 9 // Prints "noack!" when no I2C device responds
4180_1 0:0c54b73b002c 10 DigitalOut myled(LED1);
4180_1 0:0c54b73b002c 11 Serial pc(USBTX, USBRX);
4180_1 0:0c54b73b002c 12 I2C i2c(p9,p10);
4180_1 0:0c54b73b002c 13
4180_1 0:0c54b73b002c 14 char data_length=0;
4180_1 0:0c54b73b002c 15 int read=0;
4180_1 0:0c54b73b002c 16 int write=0;
4180_1 0:0c54b73b002c 17
4180_1 0:0c54b73b002c 18 //ugly code structure - should start over and fix this later
4180_1 0:0c54b73b002c 19 //now that I understand syntax for command strings a bit more - but it works
4180_1 0:0c54b73b002c 20 // Read in two ASCII characters and convert to a byte
4180_1 0:0c54b73b002c 21 // ...unless a command character is found
4180_1 0:0c54b73b002c 22 int getbyte(char *data) {
4180_1 0:0c54b73b002c 23 char charin=0;
4180_1 0:0c54b73b002c 24 charin = pc.getc();
4180_1 0:0c54b73b002c 25 if (charin == 'R') {
4180_1 0:0c54b73b002c 26 read=1;
4180_1 0:0c54b73b002c 27 if (getbyte(&data_length)==0) pc.printf("error");
4180_1 0:0c54b73b002c 28 return 2;
4180_1 0:0c54b73b002c 29 }
4180_1 0:0c54b73b002c 30 if (charin == 'P') return 3;
4180_1 0:0c54b73b002c 31 if (charin > 'F') return 0;
4180_1 0:0c54b73b002c 32 if (charin <='9') charin = charin - '0';
4180_1 0:0c54b73b002c 33 else charin = charin +10 - 'A';
4180_1 0:0c54b73b002c 34 *data = charin << 4;
4180_1 0:0c54b73b002c 35 charin = pc.getc();
4180_1 0:0c54b73b002c 36 if (charin > 'F') return 0;
4180_1 0:0c54b73b002c 37 if (charin <='9') charin = charin - '0';
4180_1 0:0c54b73b002c 38 else charin = charin +10 - 'A';
4180_1 0:0c54b73b002c 39 *data = *data | charin;
4180_1 0:0c54b73b002c 40 return 1;
4180_1 0:0c54b73b002c 41 }
4180_1 0:0c54b73b002c 42 int main() {
4180_1 0:0c54b73b002c 43
4180_1 0:0c54b73b002c 44 char command=0;
4180_1 0:0c54b73b002c 45 char address=0x40;
4180_1 0:0c54b73b002c 46 char data[255];
4180_1 0:0c54b73b002c 47 char cmd[255];
4180_1 0:0c54b73b002c 48 int cmd_count=0;
4180_1 0:0c54b73b002c 49 int more_cmd = 0;
4180_1 0:0c54b73b002c 50 int i=0;
4180_1 0:0c54b73b002c 51 myled=1;
4180_1 0:0c54b73b002c 52 data[0]=0;
4180_1 0:0c54b73b002c 53 data[1]=0;
4180_1 0:0c54b73b002c 54 i2c.frequency(75000);
4180_1 0:0c54b73b002c 55 i2c.start();
4180_1 0:0c54b73b002c 56 i2c.start();
4180_1 0:0c54b73b002c 57 pc.printf("\fmbed I2C ready\n\r");
4180_1 0:0c54b73b002c 58 // loop forever waiting for PC serial commands
4180_1 0:0c54b73b002c 59 while (1) {
4180_1 0:0c54b73b002c 60 command = pc.getc();
4180_1 0:0c54b73b002c 61 switch (command) {
4180_1 0:0c54b73b002c 62 case 'S':
4180_1 0:0c54b73b002c 63 //Start - automatic start sent on read or write
4180_1 0:0c54b73b002c 64 data_length=0;
4180_1 0:0c54b73b002c 65 cmd_count=0;
4180_1 0:0c54b73b002c 66 read=0;
4180_1 0:0c54b73b002c 67 write=0;
4180_1 0:0c54b73b002c 68 // in case of multiple starts
4180_1 0:0c54b73b002c 69 while (getbyte(&address)==0) {i2c.start();};
4180_1 0:0c54b73b002c 70 pc.printf(" - I2C address=%2.2X ", address);
4180_1 0:0c54b73b002c 71 //odd address means a read, otherwise a write
4180_1 0:0c54b73b002c 72 if ((address & 0x01) == 1) read=1;
4180_1 0:0c54b73b002c 73 else write=1;
4180_1 0:0c54b73b002c 74 if (read ==1) {
4180_1 0:0c54b73b002c 75 //read number of bytes to read
4180_1 0:0c54b73b002c 76 if (getbyte(&data_length)==0) break;
4180_1 0:0c54b73b002c 77 }
4180_1 0:0c54b73b002c 78 if (write==1) {
4180_1 0:0c54b73b002c 79 more_cmd =1;
4180_1 0:0c54b73b002c 80 while (more_cmd==1) {
4180_1 0:0c54b73b002c 81 if (getbyte(&cmd[cmd_count])>=2) {
4180_1 0:0c54b73b002c 82 pc.printf(" write ");
4180_1 0:0c54b73b002c 83 // write I2C command(s)
4180_1 0:0c54b73b002c 84 if (i2c.write(address, cmd, cmd_count)!=0) pc.printf(" noack! ");
4180_1 0:0c54b73b002c 85 for (i=0; i<cmd_count; i++)
4180_1 0:0c54b73b002c 86 pc.printf(" command=%2.2X ", cmd[i]);
4180_1 0:0c54b73b002c 87 cmd_count=0;
4180_1 0:0c54b73b002c 88 more_cmd=0;
4180_1 0:0c54b73b002c 89 write = 0;
4180_1 0:0c54b73b002c 90 } else cmd_count++;
4180_1 0:0c54b73b002c 91 }
4180_1 0:0c54b73b002c 92 if (read==0) pc.printf("\n\r");;
4180_1 0:0c54b73b002c 93 }
4180_1 0:0c54b73b002c 94 break;
4180_1 0:0c54b73b002c 95 case 'P':
4180_1 0:0c54b73b002c 96 //Stop
4180_1 0:0c54b73b002c 97 if (read == 1) {
4180_1 0:0c54b73b002c 98 pc.printf(" read ");
4180_1 0:0c54b73b002c 99 // read I2C data
4180_1 0:0c54b73b002c 100 if (i2c.read(address, data, data_length)!=0) pc.printf(" noack! ");
4180_1 0:0c54b73b002c 101 read = 0;
4180_1 0:0c54b73b002c 102 write = 0;
4180_1 0:0c54b73b002c 103 // send I2C data back to PC serial port
4180_1 0:0c54b73b002c 104 for (i=0; i<data_length; i++)
4180_1 0:0c54b73b002c 105 pc.printf(" data=%2.2X ",data[i]);
4180_1 0:0c54b73b002c 106 }
4180_1 0:0c54b73b002c 107 pc.printf("\n\r");
4180_1 0:0c54b73b002c 108 data_length=0;
4180_1 0:0c54b73b002c 109 cmd_count=0;
4180_1 0:0c54b73b002c 110 i2c.stop();
4180_1 0:0c54b73b002c 111 break;
4180_1 0:0c54b73b002c 112 case 'R':
4180_1 0:0c54b73b002c 113 //automatic read after write - read number of bytes to read
4180_1 0:0c54b73b002c 114 if (getbyte(&data_length)==0) break;
4180_1 0:0c54b73b002c 115 break;
4180_1 0:0c54b73b002c 116 case '?':
4180_1 0:0c54b73b002c 117 //Status
4180_1 0:0c54b73b002c 118 pc.printf(" mbed ready \n\r");
4180_1 0:0c54b73b002c 119 break;
4180_1 0:0c54b73b002c 120 default:
4180_1 0:0c54b73b002c 121
4180_1 0:0c54b73b002c 122 myled = 0;
4180_1 0:0c54b73b002c 123 break;
4180_1 0:0c54b73b002c 124 }
4180_1 0:0c54b73b002c 125
4180_1 0:0c54b73b002c 126
4180_1 0:0c54b73b002c 127 }
4180_1 0:0c54b73b002c 128 }