Simple C code to dump I2C device memory

I have been using I2C devices for a while and most of the times when I put my hands on a new device, I need to dump the contents of its registers on my screen, for debug purposes. For this task, I wrote a while ago a little script. Today, I feel I can share this code to whom it may be useful. Use it at your own risk.

This code was written in C language, on a Raspberry Pi and requires the WiringPi library to be installed.

Once you saved this code into a file (e.g., main.c), compile it with the following command in a shell:

$ gcc -o regdump main.c -lm -lwiringPi -Wall

If all went well and you have some I2C device plugged into your Raspberry Pi, your are good to go as long as you know the device’s bus address (try the command i2cdetect -y 1, if your are not sure of its address or if your device is connected to the bus).

So, once you are good to go, just type the following command, specifying the device’s I2C address and the first and last registers addresses to be read (all in Hexadecimal format):

$ ./regdump <device I2C address> <start address> <stop address>

For example, typing the command $ ./regdump 0x77 0x75 0xd3 returns the following result, from the reading of a BMP085 pressure sensor existing in my I2C bus.

regdump_screenshot

The code is available for download, here!

Feel free to change it to your needs. And if suits your needs, I will be glad to receive some feedback, opinions or critics about it; either via a comment below or via e-mail to webmaster @rrob@ airlomba dot net.

Have fun!