[ USB types ]

[ Arduino slave receiver ] [ Wire.h ]

[ i2c-tools documentation ]

We decided to use an arduino uno as our slave to see if our i2c-tools were working right. First, we tried configuring i2c-2 with an echo command, like we had done on the angstrom distribution. Whatever, i2cdetect worked just fine:

Screen Shot 2015-03-04 at 6.26.33 PM

Next, we checked if we could read our Arduino’s address. The Wire library example was assigning an address of 0x04 to the uno, and we were quite happy to see it come up! We did double-check this by changing the arduino’s address to 0x05 – and it worked!

Screen Shot 2015-03-04 at 5.37.48 PM

We just followed along with the i2c-tools documentation and tried the dump next. Lots of XXs.

Screen Shot 2015-03-04 at 5.46.33 PM

What we really wanted to do was send some characters over the Arduino’s serial. So, just to figure out what the Wire library does, we came across the suggestion to write our own I2C communication program (just because the library tool doesn’t let the arduino MCU do anything else!) This sent us back to our cryptocape maker’s page, and we grabbed his code, added a main() function, and this is what we had:

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
    int file;
    char *filename = "/dev/i2c-1";

    
    if ((file = open(filename, O_RDWR)) < 0) {
        /* ERROR HANDLING: you can check errno to see what went wrong */
        perror("Failed to open the i2c bus");
        exit(1);
    }

    int addr = 0x05;     // The I2C address of the device
    
    if (ioctl(file, I2C_SLAVE, addr) < 0) {
        printf("Failed to acquire bus access and/or talk to slave.\n");
        /* ERROR HANDLING; you can check errno to see what went wrong */
        exit(1);
    }

    unsigned char buf[10] = {0};
 
    for (int i = 0; i<4; i++) {
        // Using I2C Read
        if (read(file,buf,2) != 2) {
            /* ERROR HANDLING: i2c transaction failed */
            printf("Failed to read from the i2c bus: %s.\n", strerror(errno));
            printf("\n\n");
        } else {
        /* Device specific stuff here */
            for(int i = 0; i < 10; i++) {
                printf("%c ", buf[i]);
                }
            printf("\n");
        }
    }
return 0;
}

On compiling with -out, we ended with our binary file just named ut…we did read something from the bus, but we have no idea (yet) about what it means. Just to make sure that we were reading from the bus itself, we changed the address, ran the code, and got errors alright! The following image was printf’ing %x (hex), which is why these don’t look like chars.

Screen Shot 2015-03-04 at 6.14.00 PM

Anyway, we did print chars too…

Screen Shot 2015-03-04 at 6.17.27 PM

Here we can see some empty / space characters and a classic question mark.

Screen Shot 2015-03-04 at 6.15.58 PM

I am guessing that this question mark represents ASCII 255…

Screen Shot 2015-03-04 at 6.16.57 PM

And our little master with its slave. This terminology is bad, really bad. They could have been persuasive friends!20150304-182014.jpg

Advertisements

i2c-1 works on debian! Not sure what we are reading though…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s