Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of I2C_Debug_for_RealTerm by
main.cpp@3:8e2db72d0d7d, 2016-02-29 (annotated)
- Committer:
- Tuxitheone
- Date:
- Mon Feb 29 18:57:56 2016 +0000
- Revision:
- 3:8e2db72d0d7d
- Parent:
- 2:fa8c916b42a9
I2C
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 4180_1 | 0:0c54b73b002c | 1 | #include "mbed.h" |
| Tuxitheone | 3:8e2db72d0d7d | 2 | #include "TextLCD.h" |
| Tuxitheone | 3:8e2db72d0d7d | 3 | |
| 4180_1 | 0:0c54b73b002c | 4 | DigitalOut myled(LED1); |
| 4180_1 | 0:0c54b73b002c | 5 | Serial pc(USBTX, USBRX); |
| 4180_1 | 0:0c54b73b002c | 6 | I2C i2c(p9,p10); |
| Tuxitheone | 3:8e2db72d0d7d | 7 | TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x2); // 4bit bus: rs, e, d4-d7 |
| 4180_1 | 0:0c54b73b002c | 8 | |
| 4180_1 | 0:0c54b73b002c | 9 | int main() { |
| 4180_1 | 1:be1498ba7fb4 | 10 | char last_command=0; |
| 4180_1 | 2:fa8c916b42a9 | 11 | char previous_command=0; |
| 4180_1 | 1:be1498ba7fb4 | 12 | char current_char=0; |
| 4180_1 | 1:be1498ba7fb4 | 13 | char address=0; |
| 4180_1 | 1:be1498ba7fb4 | 14 | char data [255]; |
| 4180_1 | 1:be1498ba7fb4 | 15 | char current_byte=0; |
| 4180_1 | 0:0c54b73b002c | 16 | char cmd[255]; |
| 4180_1 | 0:0c54b73b002c | 17 | int cmd_count=0; |
| 4180_1 | 1:be1498ba7fb4 | 18 | int data_count=0; |
| 4180_1 | 1:be1498ba7fb4 | 19 | int read=0; |
| 4180_1 | 1:be1498ba7fb4 | 20 | int write=0; |
| 4180_1 | 1:be1498ba7fb4 | 21 | int first_data_byte=0; |
| 4180_1 | 0:0c54b73b002c | 22 | int i=0; |
| 4180_1 | 1:be1498ba7fb4 | 23 | |
| 4180_1 | 0:0c54b73b002c | 24 | myled=1; |
| 4180_1 | 0:0c54b73b002c | 25 | i2c.frequency(75000); |
| 4180_1 | 0:0c54b73b002c | 26 | i2c.start(); |
| 4180_1 | 1:be1498ba7fb4 | 27 | i2c.stop(); |
| 4180_1 | 2:fa8c916b42a9 | 28 | // Send a start message to RealTerm |
| Tuxitheone | 3:8e2db72d0d7d | 29 | lcd.printf("\fmbed I2C debug tool ready\n\r"); |
| 4180_1 | 2:fa8c916b42a9 | 30 | // Scan for I2C devices that reply with ack |
| 4180_1 | 2:fa8c916b42a9 | 31 | for (i=0; i<=254; i=i+2) { |
| 4180_1 | 2:fa8c916b42a9 | 32 | if (i2c.read(i, &data[0], 1) ==0) printf("I2C device detected at address=%2.2X\n\r", i); |
| 4180_1 | 2:fa8c916b42a9 | 33 | } |
| 4180_1 | 2:fa8c916b42a9 | 34 | data[0]=0; |
| 4180_1 | 0:0c54b73b002c | 35 | i2c.start(); |
| 4180_1 | 1:be1498ba7fb4 | 36 | i2c.stop(); |
| 4180_1 | 1:be1498ba7fb4 | 37 | // Loop processing command strings from RealTerm |
| 4180_1 | 0:0c54b73b002c | 38 | while (1) { |
| 4180_1 | 1:be1498ba7fb4 | 39 | current_char = pc.getc(); |
| 4180_1 | 1:be1498ba7fb4 | 40 | // Is it a two character ASCII string data byte value |
| 4180_1 | 2:fa8c916b42a9 | 41 | if ((current_char <='F') && (current_char !='?')) { |
| 4180_1 | 1:be1498ba7fb4 | 42 | // convert to a binary byte |
| 4180_1 | 1:be1498ba7fb4 | 43 | if (current_char <='9') current_byte = current_char - '0'; |
| 4180_1 | 1:be1498ba7fb4 | 44 | else current_byte =current_char +10 -'A'; |
| 4180_1 | 1:be1498ba7fb4 | 45 | current_char = pc.getc(); |
| Tuxitheone | 3:8e2db72d0d7d | 46 | if (current_char >'F') lcd.printf("error|\n\r"); |
| 4180_1 | 1:be1498ba7fb4 | 47 | if (current_char <='9') current_byte = (current_byte <<4) | (current_char - '0'); |
| 4180_1 | 1:be1498ba7fb4 | 48 | else current_byte = (current_byte <<4)|(current_char +10 -'A'); |
| 4180_1 | 1:be1498ba7fb4 | 49 | // first byte after S command is the I2C address |
| 4180_1 | 1:be1498ba7fb4 | 50 | if (first_data_byte==1) { |
| 4180_1 | 1:be1498ba7fb4 | 51 | first_data_byte=0; |
| 4180_1 | 1:be1498ba7fb4 | 52 | address=current_byte; |
| Tuxitheone | 3:8e2db72d0d7d | 53 | lcd.printf(" -I2C address=%2.2X ", address); |
| 4180_1 | 1:be1498ba7fb4 | 54 | //Odd I2C address is a read and even is a write |
| 4180_1 | 1:be1498ba7fb4 | 55 | if ((address & 0x01) == 0) write=1; |
| 4180_1 | 1:be1498ba7fb4 | 56 | else read=1; |
| 4180_1 | 1:be1498ba7fb4 | 57 | } else |
| 4180_1 | 1:be1498ba7fb4 | 58 | // Read in cmd bytes for write |
| 4180_1 | 1:be1498ba7fb4 | 59 | if ((last_command == 'S')&&(read==0)) { |
| 4180_1 | 1:be1498ba7fb4 | 60 | cmd[cmd_count]=current_byte; |
| 4180_1 | 1:be1498ba7fb4 | 61 | cmd_count++; |
| 4180_1 | 0:0c54b73b002c | 62 | } |
| 4180_1 | 1:be1498ba7fb4 | 63 | // number of bytes for a read |
| 4180_1 | 1:be1498ba7fb4 | 64 | if ((last_command =='R')||(read==1)) data_count=current_byte; |
| 4180_1 | 1:be1498ba7fb4 | 65 | } else { |
| 4180_1 | 1:be1498ba7fb4 | 66 | // Not a number - it is a command character |
| 4180_1 | 1:be1498ba7fb4 | 67 | last_command = current_char; |
| 4180_1 | 1:be1498ba7fb4 | 68 | switch (last_command) { |
| 4180_1 | 1:be1498ba7fb4 | 69 | // Start |
| 4180_1 | 1:be1498ba7fb4 | 70 | case 'S': |
| 4180_1 | 2:fa8c916b42a9 | 71 | if (previous_command == 'S') i2c.start(); |
| 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) { |
| Tuxitheone | 3:8e2db72d0d7d | 78 | lcd.printf(" write "); |
| Tuxitheone | 3:8e2db72d0d7d | 79 | if (i2c.write(address,cmd,cmd_count)!=0) lcd.printf(" No Ack! "); |
| 4180_1 | 1:be1498ba7fb4 | 80 | for (i=0; i<cmd_count; i++) |
| Tuxitheone | 3:8e2db72d0d7d | 81 | lcd.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) { |
| Tuxitheone | 3:8e2db72d0d7d | 85 | lcd.printf(" read "); |
| Tuxitheone | 3:8e2db72d0d7d | 86 | if (i2c.read(address, data, data_count) != 0) lcd.printf(" No Ack! "); |
| 4180_1 | 1:be1498ba7fb4 | 87 | for (i=0; i<data_count; i++) |
| Tuxitheone | 3:8e2db72d0d7d | 88 | lcd.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(); |
| Tuxitheone | 3:8e2db72d0d7d | 92 | lcd.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 '?': |
| Tuxitheone | 3:8e2db72d0d7d | 105 | lcd.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: |
| Tuxitheone | 3:8e2db72d0d7d | 109 | lcd.printf(" unknown command \n\r"); |
| 4180_1 | 1:be1498ba7fb4 | 110 | break; |
| 4180_1 | 1:be1498ba7fb4 | 111 | } |
| 4180_1 | 2:fa8c916b42a9 | 112 | previous_command = last_command; |
| 4180_1 | 0:0c54b73b002c | 113 | } |
| 4180_1 | 0:0c54b73b002c | 114 | } |
| 4180_1 | 0:0c54b73b002c | 115 | } |
