Prints all availabe devices on the i2c bus

Dependencies:   mbed

Committer:
aAXEe
Date:
Wed Nov 05 09:28:02 2014 +0000
Revision:
0:2ccb0f491eab
add names for devices

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aAXEe 0:2ccb0f491eab 1 #include "mbed.h"
aAXEe 0:2ccb0f491eab 2
aAXEe 0:2ccb0f491eab 3
aAXEe 0:2ccb0f491eab 4 I2C i2c(I2C_SDA, I2C_SCL);
aAXEe 0:2ccb0f491eab 5
aAXEe 0:2ccb0f491eab 6
aAXEe 0:2ccb0f491eab 7 Serial pc(SERIAL_TX, SERIAL_RX);
aAXEe 0:2ccb0f491eab 8
aAXEe 0:2ccb0f491eab 9 #define CHAR_BIT 8
aAXEe 0:2ccb0f491eab 10 #define FMT_BUF_SIZE (CHAR_BIT*sizeof(uintmax_t)+1)
aAXEe 0:2ccb0f491eab 11 char *binary_fmt(uintmax_t x, char* buf)
aAXEe 0:2ccb0f491eab 12 {
aAXEe 0:2ccb0f491eab 13 char *s = buf + FMT_BUF_SIZE;
aAXEe 0:2ccb0f491eab 14 *--s = 0;
aAXEe 0:2ccb0f491eab 15 if (!x) *--s = '0';
aAXEe 0:2ccb0f491eab 16 for(; x; x/=2) *--s = '0' + x%2;
aAXEe 0:2ccb0f491eab 17 return s;
aAXEe 0:2ccb0f491eab 18 }
aAXEe 0:2ccb0f491eab 19 char binary_buffer[FMT_BUF_SIZE];
aAXEe 0:2ccb0f491eab 20
aAXEe 0:2ccb0f491eab 21
aAXEe 0:2ccb0f491eab 22 char text_buffer[100];
aAXEe 0:2ccb0f491eab 23 char* getDeviceName(int addr){
aAXEe 0:2ccb0f491eab 24 int devType = (addr & 0xf0) >> 4;
aAXEe 0:2ccb0f491eab 25
aAXEe 0:2ccb0f491eab 26 int count = 0;
aAXEe 0:2ccb0f491eab 27
aAXEe 0:2ccb0f491eab 28 switch(devType){
aAXEe 0:2ccb0f491eab 29 case 0:
aAXEe 0:2ccb0f491eab 30 count += sprintf(text_buffer + count, "General call");
aAXEe 0:2ccb0f491eab 31 break;
aAXEe 0:2ccb0f491eab 32 case 0x9:
aAXEe 0:2ccb0f491eab 33 count += sprintf(text_buffer + count, "TMP10X");
aAXEe 0:2ccb0f491eab 34 break;
aAXEe 0:2ccb0f491eab 35 case 0xD:
aAXEe 0:2ccb0f491eab 36 count += sprintf(text_buffer + count, "ADC MCP3428");
aAXEe 0:2ccb0f491eab 37 break;
aAXEe 0:2ccb0f491eab 38 case 0xC:
aAXEe 0:2ccb0f491eab 39 count += sprintf(text_buffer + count, "DAC MCP4728");
aAXEe 0:2ccb0f491eab 40 break;
aAXEe 0:2ccb0f491eab 41 case 0x4:
aAXEe 0:2ccb0f491eab 42 count += sprintf(text_buffer + count, "IO Expander PCF8574");
aAXEe 0:2ccb0f491eab 43 break;
aAXEe 0:2ccb0f491eab 44 case 0xA:
aAXEe 0:2ccb0f491eab 45 count += sprintf(text_buffer + count, "FRAM FM24CL16B");
aAXEe 0:2ccb0f491eab 46 break;
aAXEe 0:2ccb0f491eab 47 default:
aAXEe 0:2ccb0f491eab 48 count += sprintf(text_buffer + count, "unknown");
aAXEe 0:2ccb0f491eab 49 }
aAXEe 0:2ccb0f491eab 50
aAXEe 0:2ccb0f491eab 51 int addrPins = (addr & 0xf) >> 1;
aAXEe 0:2ccb0f491eab 52 int rw = (addr & 1);
aAXEe 0:2ccb0f491eab 53
aAXEe 0:2ccb0f491eab 54 count += sprintf(text_buffer + count, " addr: %s %s", binary_fmt(addrPins, binary_buffer), rw ? "read" : "write");
aAXEe 0:2ccb0f491eab 55
aAXEe 0:2ccb0f491eab 56 return text_buffer;
aAXEe 0:2ccb0f491eab 57 }
aAXEe 0:2ccb0f491eab 58
aAXEe 0:2ccb0f491eab 59
aAXEe 0:2ccb0f491eab 60 void findDevices(void)
aAXEe 0:2ccb0f491eab 61 {
aAXEe 0:2ccb0f491eab 62 pc.printf("Scanning for devices ..\n");
aAXEe 0:2ccb0f491eab 63 for(int i=0; i<256; i++) {
aAXEe 0:2ccb0f491eab 64 if(i2c.write(i, NULL, 0, 0) == 0){
aAXEe 0:2ccb0f491eab 65 printf("Device %s is present: %s\n", binary_fmt(i, binary_buffer), getDeviceName(i));
aAXEe 0:2ccb0f491eab 66 //printf("Device %i present\n", i);
aAXEe 0:2ccb0f491eab 67 }
aAXEe 0:2ccb0f491eab 68 // else
aAXEe 0:2ccb0f491eab 69 // printf("Device %i not present\n", i);
aAXEe 0:2ccb0f491eab 70 }
aAXEe 0:2ccb0f491eab 71
aAXEe 0:2ccb0f491eab 72 }
aAXEe 0:2ccb0f491eab 73
aAXEe 0:2ccb0f491eab 74 int main()
aAXEe 0:2ccb0f491eab 75 {
aAXEe 0:2ccb0f491eab 76
aAXEe 0:2ccb0f491eab 77
aAXEe 0:2ccb0f491eab 78 while (1) {
aAXEe 0:2ccb0f491eab 79 findDevices();
aAXEe 0:2ccb0f491eab 80 wait(1.0);
aAXEe 0:2ccb0f491eab 81 }
aAXEe 0:2ccb0f491eab 82
aAXEe 0:2ccb0f491eab 83 }
aAXEe 0:2ccb0f491eab 84