It is a fork I2C debug program for use with RealTerm. Sends I2C commands to D14,D15. Run program on mbed. Start RealTerm, select the mbed virtual com port under the port tab, then open RealTerm's I2C command tab. Reset mbed. See http://mbed.org/users/4180_1/notebook/i2c-debug-for-realterm/ for instructions

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 // The program sends and receives I2C commands using RealTerm's I2C tab feature
00003 // Run Realterm on PC and this program on mbed
00004 // In RealTerm connect to the mbed's USB virtual COM port
00005 // Switch to I2C tab
00006 // Type in I2C address & data to read and write and see the response
00007 // It is handy to debug complex I2C hardware setups
00008 // Prints "No Ack!" when no I2C device responds to the address
00009 //
00010 // See Instructions at http://mbed.org/users/4180_1/notebook/i2c-debug-for-realterm/
00011 //
00012 DigitalOut myled(LED1);
00013 Serial pc(USBTX, USBRX);
00014 I2C i2c(D14,D15);
00015 
00016 int main() {
00017     pc.baud(9600);
00018     char last_command=0;
00019     char previous_command=0;
00020     char current_char=0;
00021     char address=0;
00022     char data [255];
00023     char current_byte=0;
00024     char cmd[255];
00025     int cmd_count=0;
00026     int data_count=0;
00027     int read=0;
00028     int write=0;
00029     int first_data_byte=0;
00030     int i=0;
00031 
00032     myled=1;
00033     i2c.frequency(100000);
00034     i2c.start();
00035     i2c.stop();
00036     // Send a start message to RealTerm
00037     pc.printf("\fmbed I2C debug tool ready\n\r");
00038     // Scan for I2C devices that reply with ack
00039     for (i=0; i<=254; i=i+2) {
00040         if (i2c.read(i, &data[0], 1) ==0) printf("I2C device detected at address=%2.2X\n\r", i);
00041     }
00042     data[0]=0;
00043     i2c.start();
00044     i2c.stop();
00045 // Loop processing command strings from RealTerm
00046     while (1) {
00047         current_char = pc.getc();
00048         // Is it a two character ASCII string data byte value
00049         if ((current_char <='F') && (current_char !='?')) {
00050             // convert to a binary byte
00051             if (current_char <='9') current_byte = current_char - '0';
00052             else current_byte =current_char +10 -'A';
00053             current_char = pc.getc();
00054             if (current_char >'F') pc.printf("error|\n\r");
00055             if (current_char <='9') current_byte = (current_byte <<4) | (current_char - '0');
00056             else current_byte = (current_byte <<4)|(current_char +10 -'A');
00057             // first byte after S command is the I2C address
00058             if (first_data_byte==1) {
00059                 first_data_byte=0;
00060                 address=current_byte;
00061                 pc.printf(" -I2C address=%2.2X ", address);
00062                 //Odd I2C address is a read and even is a write
00063                 if ((address & 0x01) == 0) write=1;
00064                 else read=1;
00065             } else
00066                 // Read in cmd bytes for write
00067                 if ((last_command == 'S')&&(read==0)) {
00068                     cmd[cmd_count]=current_byte;
00069                     cmd_count++;
00070                 }
00071             // number of bytes for a read
00072             if ((last_command =='R')||(read==1)) data_count=current_byte;
00073         } else {
00074             // Not a number - it is a command character
00075             last_command = current_char;
00076             switch (last_command) {
00077                     // Start
00078                 case 'S':
00079                     if (previous_command == 'S') i2c.start();
00080                     first_data_byte=1;
00081                     break;
00082                     // Stop
00083                 case 'P':
00084                     // Do the I2C write
00085                     if (write==1) {
00086                         pc.printf(" write ");
00087                         if (i2c.write(address,cmd,cmd_count)!=0) pc.printf(" No Ack! ");
00088                         for (i=0; i<cmd_count; i++)
00089                             pc.printf(" cmd=%2.2X ", cmd[i]);
00090                     }
00091                     // Do the I2C read
00092                     if (read==1) {
00093                         pc.printf(" read ");
00094                         if (i2c.read(address, data, data_count) != 0) pc.printf(" No Ack! ");
00095                         for (i=0; i<data_count; i++)
00096                             pc.printf(" data=%2.2X ",data[i]);
00097                     }
00098                     // reset values for next I2C operation
00099                     i2c.stop();
00100                     pc.printf("\n\r");
00101                     read=0;
00102                     data_count=0;
00103                     cmd_count=0;
00104                     write=0;
00105                     first_data_byte=0;
00106                     break;
00107                     // Read after write
00108                 case 'R':
00109                     read=1;
00110                     break;
00111                     // Status request
00112                 case '?':
00113                     pc.printf(" mbed ready \n\r");
00114                     break;
00115                     // Unknown or unimplemented command
00116                 default:
00117                     pc.printf(" unknown command \n\r");
00118                     break;
00119             }
00120             previous_command = last_command;
00121         }
00122     }
00123 }