I2C

Dependencies:   TextLCD mbed

Fork of I2C_Debug_for_RealTerm by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 
00004 DigitalOut myled(LED1);
00005 Serial pc(USBTX, USBRX);
00006 I2C i2c(p9,p10);
00007 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x2);        // 4bit bus: rs, e, d4-d7
00008 
00009 int main() {
00010     char last_command=0;
00011     char previous_command=0;
00012     char current_char=0;
00013     char address=0;
00014     char data [255];
00015     char current_byte=0;
00016     char cmd[255];
00017     int cmd_count=0;
00018     int data_count=0;
00019     int read=0;
00020     int write=0;
00021     int first_data_byte=0;
00022     int i=0;
00023 
00024     myled=1;
00025     i2c.frequency(75000);
00026     i2c.start();
00027     i2c.stop();
00028     // Send a start message to RealTerm
00029     lcd.printf("\fmbed I2C debug tool ready\n\r");
00030     // Scan for I2C devices that reply with ack
00031     for (i=0; i<=254; i=i+2) {
00032         if (i2c.read(i, &data[0], 1) ==0) printf("I2C device detected at address=%2.2X\n\r", i);
00033     }
00034     data[0]=0;
00035     i2c.start();
00036     i2c.stop();
00037 // Loop processing command strings from RealTerm
00038     while (1) {
00039         current_char = pc.getc();
00040         // Is it a two character ASCII string data byte value
00041         if ((current_char <='F') && (current_char !='?')) {
00042             // convert to a binary byte
00043             if (current_char <='9') current_byte = current_char - '0';
00044             else current_byte =current_char +10 -'A';
00045             current_char = pc.getc();
00046             if (current_char >'F') lcd.printf("error|\n\r");
00047             if (current_char <='9') current_byte = (current_byte <<4) | (current_char - '0');
00048             else current_byte = (current_byte <<4)|(current_char +10 -'A');
00049             // first byte after S command is the I2C address
00050             if (first_data_byte==1) {
00051                 first_data_byte=0;
00052                 address=current_byte;
00053                 lcd.printf(" -I2C address=%2.2X ", address);
00054                 //Odd I2C address is a read and even is a write
00055                 if ((address & 0x01) == 0) write=1;
00056                 else read=1;
00057             } else
00058                 // Read in cmd bytes for write
00059                 if ((last_command == 'S')&&(read==0)) {
00060                     cmd[cmd_count]=current_byte;
00061                     cmd_count++;
00062                 }
00063             // number of bytes for a read
00064             if ((last_command =='R')||(read==1)) data_count=current_byte;
00065         } else {
00066             // Not a number - it is a command character
00067             last_command = current_char;
00068             switch (last_command) {
00069                     // Start
00070                 case 'S':
00071                     if (previous_command == 'S') i2c.start();
00072                     first_data_byte=1;
00073                     break;
00074                     // Stop
00075                 case 'P':
00076                     // Do the I2C write
00077                     if (write==1) {
00078                         lcd.printf(" write ");
00079                         if (i2c.write(address,cmd,cmd_count)!=0) lcd.printf(" No Ack! ");
00080                         for (i=0; i<cmd_count; i++)
00081                             lcd.printf(" cmd=%2.2X ", cmd[i]);
00082                     }
00083                     // Do the I2C read
00084                     if (read==1) {
00085                         lcd.printf(" read ");
00086                         if (i2c.read(address, data, data_count) != 0) lcd.printf(" No Ack! ");
00087                         for (i=0; i<data_count; i++)
00088                             lcd.printf(" data=%2.2X ",data[i]);
00089                     }
00090                     // reset values for next I2C operation
00091                     i2c.stop();
00092                     lcd.printf("\n\r");
00093                     read=0;
00094                     data_count=0;
00095                     cmd_count=0;
00096                     write=0;
00097                     first_data_byte=0;
00098                     break;
00099                     // Read after write
00100                 case 'R':
00101                     read=1;
00102                     break;
00103                     // Status request
00104                 case '?':
00105                     lcd.printf(" mbed ready \n\r");
00106                     break;
00107                     // Unknown or unimplemented command
00108                 default:
00109                     lcd.printf(" unknown command \n\r");
00110                     break;
00111             }
00112             previous_command = last_command;
00113         }
00114     }
00115 }