I2C debug program for use with RealTerm. Sends I2C commands to P9 and P10. 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
main.cpp@1:be1498ba7fb4, 2011-03-11 (annotated)
- Committer:
- 4180_1
- Date:
- Fri Mar 11 03:25:42 2011 +0000
- Revision:
- 1:be1498ba7fb4
- Parent:
- 0:0c54b73b002c
- Child:
- 2:fa8c916b42a9
ver 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:0c54b73b002c | 1 | #include "mbed.h" |
4180_1 | 0:0c54b73b002c | 2 | // The program sends and receives I2C commands using RealTerm's I2C tab feature |
4180_1 | 0:0c54b73b002c | 3 | // Run Realterm on PC and this program on mbed |
4180_1 | 0:0c54b73b002c | 4 | // In RealTerm connect to the mbed's USB virtual COM port |
4180_1 | 0:0c54b73b002c | 5 | // Switch to I2C tab |
4180_1 | 0:0c54b73b002c | 6 | // Type in I2C address & data to read and write and see the response |
4180_1 | 0:0c54b73b002c | 7 | // It is handy to debug complex I2C hardware setups |
4180_1 | 1:be1498ba7fb4 | 8 | // Prints "No Ack!" when no I2C device responds to the address |
4180_1 | 1:be1498ba7fb4 | 9 | // |
4180_1 | 1:be1498ba7fb4 | 10 | // See Instructions at http://mbed.org/users/4180_1/notebook/i2c-debug-for-realterm/ |
4180_1 | 1:be1498ba7fb4 | 11 | // |
4180_1 | 0:0c54b73b002c | 12 | DigitalOut myled(LED1); |
4180_1 | 0:0c54b73b002c | 13 | Serial pc(USBTX, USBRX); |
4180_1 | 0:0c54b73b002c | 14 | I2C i2c(p9,p10); |
4180_1 | 0:0c54b73b002c | 15 | |
4180_1 | 0:0c54b73b002c | 16 | int main() { |
4180_1 | 1:be1498ba7fb4 | 17 | char last_command=0; |
4180_1 | 1:be1498ba7fb4 | 18 | char current_char=0; |
4180_1 | 1:be1498ba7fb4 | 19 | char address=0; |
4180_1 | 1:be1498ba7fb4 | 20 | char data [255]; |
4180_1 | 1:be1498ba7fb4 | 21 | char current_byte=0; |
4180_1 | 0:0c54b73b002c | 22 | char cmd[255]; |
4180_1 | 0:0c54b73b002c | 23 | int cmd_count=0; |
4180_1 | 1:be1498ba7fb4 | 24 | int data_count=0; |
4180_1 | 1:be1498ba7fb4 | 25 | int read=0; |
4180_1 | 1:be1498ba7fb4 | 26 | int write=0; |
4180_1 | 1:be1498ba7fb4 | 27 | int first_data_byte=0; |
4180_1 | 0:0c54b73b002c | 28 | int i=0; |
4180_1 | 1:be1498ba7fb4 | 29 | |
4180_1 | 0:0c54b73b002c | 30 | myled=1; |
4180_1 | 0:0c54b73b002c | 31 | i2c.frequency(75000); |
4180_1 | 0:0c54b73b002c | 32 | i2c.start(); |
4180_1 | 1:be1498ba7fb4 | 33 | i2c.stop(); |
4180_1 | 0:0c54b73b002c | 34 | i2c.start(); |
4180_1 | 1:be1498ba7fb4 | 35 | i2c.stop(); |
4180_1 | 1:be1498ba7fb4 | 36 | // Send a start message to RealTerm |
4180_1 | 1:be1498ba7fb4 | 37 | pc.printf("\fmbed I2C debug tool ready\n\r"); |
4180_1 | 1:be1498ba7fb4 | 38 | // Loop processing command strings from RealTerm |
4180_1 | 0:0c54b73b002c | 39 | while (1) { |
4180_1 | 1:be1498ba7fb4 | 40 | current_char = pc.getc(); |
4180_1 | 1:be1498ba7fb4 | 41 | // Is it a two character ASCII string data byte value |
4180_1 | 1:be1498ba7fb4 | 42 | if (current_char <='F') { |
4180_1 | 1:be1498ba7fb4 | 43 | // convert to a binary byte |
4180_1 | 1:be1498ba7fb4 | 44 | if (current_char <='9') current_byte = current_char - '0'; |
4180_1 | 1:be1498ba7fb4 | 45 | else current_byte =current_char +10 -'A'; |
4180_1 | 1:be1498ba7fb4 | 46 | current_char = pc.getc(); |
4180_1 | 1:be1498ba7fb4 | 47 | if (current_char >'F') pc.printf("error|\n\r"); |
4180_1 | 1:be1498ba7fb4 | 48 | if (current_char <='9') current_byte = (current_byte <<4) | (current_char - '0'); |
4180_1 | 1:be1498ba7fb4 | 49 | else current_byte = (current_byte <<4)|(current_char +10 -'A'); |
4180_1 | 1:be1498ba7fb4 | 50 | // first byte after S command is the I2C address |
4180_1 | 1:be1498ba7fb4 | 51 | if (first_data_byte==1) { |
4180_1 | 1:be1498ba7fb4 | 52 | first_data_byte=0; |
4180_1 | 1:be1498ba7fb4 | 53 | address=current_byte; |
4180_1 | 1:be1498ba7fb4 | 54 | pc.printf(" -I2C address=%2.2X ", address); |
4180_1 | 1:be1498ba7fb4 | 55 | //Odd I2C address is a read and even is a write |
4180_1 | 1:be1498ba7fb4 | 56 | if ((address & 0x01) == 0) write=1; |
4180_1 | 1:be1498ba7fb4 | 57 | else read=1; |
4180_1 | 1:be1498ba7fb4 | 58 | } else |
4180_1 | 1:be1498ba7fb4 | 59 | // Read in cmd bytes for write |
4180_1 | 1:be1498ba7fb4 | 60 | if ((last_command == 'S')&&(read==0)) { |
4180_1 | 1:be1498ba7fb4 | 61 | cmd[cmd_count]=current_byte; |
4180_1 | 1:be1498ba7fb4 | 62 | cmd_count++; |
4180_1 | 0:0c54b73b002c | 63 | } |
4180_1 | 1:be1498ba7fb4 | 64 | // number of bytes for a read |
4180_1 | 1:be1498ba7fb4 | 65 | if ((last_command =='R')||(read==1)) data_count=current_byte; |
4180_1 | 1:be1498ba7fb4 | 66 | } else { |
4180_1 | 1:be1498ba7fb4 | 67 | // Not a number - it is a command character |
4180_1 | 1:be1498ba7fb4 | 68 | last_command = current_char; |
4180_1 | 1:be1498ba7fb4 | 69 | switch (last_command) { |
4180_1 | 1:be1498ba7fb4 | 70 | // Start |
4180_1 | 1:be1498ba7fb4 | 71 | case 'S': |
4180_1 | 1:be1498ba7fb4 | 72 | first_data_byte=1; |
4180_1 | 1:be1498ba7fb4 | 73 | break; |
4180_1 | 1:be1498ba7fb4 | 74 | // Stop |
4180_1 | 1:be1498ba7fb4 | 75 | case 'P': |
4180_1 | 1:be1498ba7fb4 | 76 | // Do the I2C write |
4180_1 | 1:be1498ba7fb4 | 77 | if (write==1) { |
4180_1 | 1:be1498ba7fb4 | 78 | pc.printf(" write "); |
4180_1 | 1:be1498ba7fb4 | 79 | if (i2c.write(address,cmd,cmd_count)!=0) pc.printf(" No Ack! "); |
4180_1 | 1:be1498ba7fb4 | 80 | for (i=0; i<cmd_count; i++) |
4180_1 | 1:be1498ba7fb4 | 81 | pc.printf(" cmd=%2.2X ", cmd[i]); |
4180_1 | 0:0c54b73b002c | 82 | } |
4180_1 | 1:be1498ba7fb4 | 83 | // Do the I2C read |
4180_1 | 1:be1498ba7fb4 | 84 | if (read==1) { |
4180_1 | 1:be1498ba7fb4 | 85 | pc.printf(" read "); |
4180_1 | 1:be1498ba7fb4 | 86 | if (i2c.read(address, data, data_count) != 0) pc.printf(" No Ack! "); |
4180_1 | 1:be1498ba7fb4 | 87 | for (i=0; i<data_count; i++) |
4180_1 | 1:be1498ba7fb4 | 88 | pc.printf(" data=%2.2X ",data[i]); |
4180_1 | 1:be1498ba7fb4 | 89 | } |
4180_1 | 1:be1498ba7fb4 | 90 | // reset values for next I2C operation |
4180_1 | 1:be1498ba7fb4 | 91 | i2c.stop(); |
4180_1 | 1:be1498ba7fb4 | 92 | pc.printf("\n\r"); |
4180_1 | 1:be1498ba7fb4 | 93 | read=0; |
4180_1 | 1:be1498ba7fb4 | 94 | data_count=0; |
4180_1 | 1:be1498ba7fb4 | 95 | cmd_count=0; |
4180_1 | 1:be1498ba7fb4 | 96 | write=0; |
4180_1 | 1:be1498ba7fb4 | 97 | first_data_byte=0; |
4180_1 | 1:be1498ba7fb4 | 98 | break; |
4180_1 | 1:be1498ba7fb4 | 99 | // Read after write |
4180_1 | 1:be1498ba7fb4 | 100 | case 'R': |
4180_1 | 1:be1498ba7fb4 | 101 | read=1; |
4180_1 | 1:be1498ba7fb4 | 102 | break; |
4180_1 | 1:be1498ba7fb4 | 103 | // Status request |
4180_1 | 1:be1498ba7fb4 | 104 | case '?': |
4180_1 | 1:be1498ba7fb4 | 105 | pc.printf(" mbed ready \n\r"); |
4180_1 | 1:be1498ba7fb4 | 106 | break; |
4180_1 | 1:be1498ba7fb4 | 107 | // Unknown or unimplemented command |
4180_1 | 1:be1498ba7fb4 | 108 | default: |
4180_1 | 1:be1498ba7fb4 | 109 | pc.printf("unknown command"); |
4180_1 | 1:be1498ba7fb4 | 110 | break; |
4180_1 | 1:be1498ba7fb4 | 111 | } |
4180_1 | 0:0c54b73b002c | 112 | } |
4180_1 | 0:0c54b73b002c | 113 | } |
4180_1 | 0:0c54b73b002c | 114 | } |