Search code examples
craspberry-pi3sensorsi2c

How to configure Hall effect sensor (si7210) using i2c program with raspberry pi (C language)


I would like to configure a Hall effect sensor with I2C but I don't really see how to manage it. I don't really know which value that I have to enter in each registers, but this not the biggest issue. Indeed i would like to read the registers in order to modify them to have an idea of what i could change.

To do so i wrote 2 programms, respectively to write/read into registers. But i'm far from being an expert and i would't like some recommandations and advice on how i could change my code in order to make it work properly.

includes for both files

#include <unistd.h>     
#include <fcntl.h>      
#include <sys/ioctl.h>      
#include <linux/i2c-dev.h>  
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>

Write :

int file_i2c;

uint8_t buffer[2] = { 0 };

int addr = 0x31;

char *end;
char buf[12];

int main(void)
{

    //----- OPEN THE I2C BUS -----
    char *filename = (char *)"/dev/i2c-1";
    if ((file_i2c = open(filename, O_RDWR)) < 0)
    {
        return EXIT_FAILURE;
    }

    if (ioctl(file_i2c, I2C_SLAVE, addr) < 0)
    {
        return EXIT_FAILURE;
    }

    int length = 0;
    for( int i = 0xC0 ; i<0xE5 ; i++)
    {
        if( i < 0xD1 || i > 0xE0)
        {
            buffer[0] = i;
            printf("Rgister value for 0x%02X : ",buffer[0]);
            do {
                    if (!fgets(buf, sizeof buf, stdin))
                        break;

                    // remove \n
                    buf[strlen(buf) - 1] = 0;
                    buffer[1] = strtol(buf, &end, 2);
            } while (end != buf + strlen(buf));
            printf("\n");
            length = 2; 
            if (write(file_i2c, buffer, length) != length)      
            {
                printf("Failed to write to the i2c bus.\n");
            }
        }
    }

    close(file_i2c);
    return EXIT_SUCCESS;
}

Read :

int file_i2c;

int addr = 0x31;

int main(void)
{

    //----- OPEN THE I2C BUS -----
    char *filename = (char *)"/dev/i2c-1";
    if ((file_i2c = open(filename, O_RDWR)) < 0)
    {
        return EXIT_FAILURE;
    }

    if (ioctl(file_i2c, I2C_SLAVE, addr) < 0)
    {
        return EXIT_FAILURE;
    }

    uint8_t register_addr;
    uint8_t str;
    for( int i = 0xC0 ; i<0xE5 ; i++)
    {
        if( i < 0xD1 || i > 0xE0)
        {
            register_addr = i >> 8;
            if(write(file_i2c, &register_addr, 1) == 1)
            {
                if (read(file_i2c, &str, 1) != 1)   
                {
                    fprintf(stderr,"Failed to read\n");
                }
                else
                {
                    printf("data on 0x%02X : 0x%02X\n",i,str);
                }
            }
        }
    }
    close(file_i2c);
    return EXIT_SUCCESS;
}

we cannot know if the writing program works because the reading program returns

data on 0xC0 : 0x00
data on 0xC1 : 0x00
data on 0xC2 : 0x00
data on 0xC3 : 0x00
data on 0xC4 : 0x00
data on 0xC5 : 0x00
data on 0xC6 : 0x00
data on 0xC7 : 0x00
data on 0xC8 : 0x00
data on 0xC9 : 0x00
data on 0xCA : 0x00
data on 0xCB : 0x00
data on 0xCC : 0x00
data on 0xCD : 0x00
data on 0xCE : 0x00
data on 0xCF : 0x00
data on 0xD0 : 0x00
data on 0xE1 : 0x00
data on 0xE2 : 0x00
data on 0xE3 : 0x00
data on 0xE4 : 0x00

I hope you'll be able to help, thanks a lot, yocvito


Solution

  • Finally, the reading program works perfectly.

    there was just a problem on that line.

    register_addr = i >> 8;
    

    I shifted right by 8 bits without really knowing why

    so I replaced this line with

    register_addr = i;
    

    And so I have the expected return (the values ​​correspond to those expected)

    data on 0xC0 : 0x14
    data on 0xC1 : 0x00
    data on 0xC2 : 0x00
    data on 0xC3 : 0x00
    data on 0xC4 : 0x02
    data on 0xC5 : 0x00
    data on 0xC6 : 0xB0
    data on 0xC7 : 0x12
    data on 0xC8 : 0xE6
    data on 0xC9 : 0xBD
    data on 0xCA : 0xD1
    data on 0xCB : 0x58
    data on 0xCC : 0xB9
    data on 0xCD : 0x00
    data on 0xCE : 0x05
    data on 0xCF : 0x33
    data on 0xD0 : 0x00
    data on 0xE1 : 0x00
    data on 0xE2 : 0x00
    data on 0xE3 : 0x00
    data on 0xE4 : 0x00
    

    As for the writing program, it was already working (he wrote well in the registers indicated). But, the written values ​​are wrong (except for the first register).

    Write :

    Valeur de registre pour 0xC0 : 00010100
    
    Valeur de registre pour 0xC1 : 11111111
    
    Valeur de registre pour 0xC2 : 11111111
    
    

    Read :

    data on 0xC0 : 0x14
    data on 0xC1 : 0x7F
    data on 0xC2 : 0x1F
    
    

    Do you have any idea where this might come from?