首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >存取设备的寄存器i2c

存取设备的寄存器i2c
EN

Stack Overflow用户
提问于 2016-09-09 10:12:08
回答 2查看 11K关注 0票数 5

我最近买了一个gy-521板,我正试图通过以下连接将它与Raspberry Pi 3一起使用

代码语言:javascript
复制
RPi3     |     GY-521
---------------------
3.3V <-------> Vcc
GND  <-------> GND
SCL  <-------> SCL
SDA  <-------> SDA

使用i2cdetect -y 1,我获得以下信息

代码语言:javascript
复制
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

所以这个设备的地址是0x68。通过阅读数据表,我发现,例如,X轴上的加速度存储在寄存器3B (较高位)和3C (较低位)中。我的问题是如何访问这些寄存器?

我的想法是,如果我将/dev/i2c-1作为文件描述符打开,我可以使用普通的readwrite函数。然后,我可以使用poll来处理新的可用数据,而不是一直获取数据。

我试着按照read中的建议使用文档函数,但这不起作用(我只得到零),当我使用poll时,似乎没有人在另一边,超时(100 as )过期了。我想我应该对芯片说“给我3B寄存器的价值”,但我不知道该如何做。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <poll.h>
#include <errno.h>

const char *filename = "/dev/i2c-1";
int DEBUG = 0;
int ADDRESS = 0x68;
struct pollfd pfd;

void debug(const char* format, ...)
{
    if (DEBUG)
    {
        va_list argptr;
        va_start(argptr, format);
        fprintf(stdout, "### ");
        vfprintf(stdout, format, argptr);
        va_end(argptr);
    }
}

void error_handler(const char *msg)
{
    perror(msg);
    exit(EXIT_FAILURE);
}

void set_debug(const char *deb_lev)
{
    unsigned long num;
    char *p;
    errno = 0;

    num = strtoul(deb_lev, &p, 10);

    if (errno != 0 || *p != '\0')
        error_handler("set_debug | strtoul");

    DEBUG = (num > 0);
}

int open_file(const char *filename)
{
    int fd;

    if ((fd = open(filename, O_RDWR)) == -1)
        error_handler("open_file | open");

    debug("\"%s\" opened at %d\n", filename, fd);
    return fd;
}

int8_t read_value(int fd)
{
    debug("Reading from %d\n", fd);

    int8_t num;
    char *p = (char *)&num;
    ssize_t size = sizeof(int8_t);
    ssize_t r = 0;

    while (size > 0)
    {
        if ((r = read(fd, p, size)) == -1)
            error_handler("read_value | read");

        size -= r;
        p += r;
    }

    return num;
}

void command(uint16_t reg, int fd)
{
    debug("Writing to %d\n", fd);

    unsigned char reg_buf[2];
    ssize_t w = 0;
    ssize_t size = sizeof(unsigned char)*2;

    reg_buf[0] = (reg >> 0) & 0xFF;
    reg_buf[1] = (reg >> 8) & 0xFF;

    if ((w = write(fd, reg_buf, size)) == -1)
        error_handler("command | write");
}

void read_data_from_imu(struct pollfd *pfd)
{
    int8_t val;
    int p;

    for (;;)
    {
        command(0x3b, pfd->fd);

        switch (p = poll(pfd, 1, 100))
        {
            case -1:
                error_handler("read_data_from_imu | poll");
            case 0:
                fprintf(stderr, "Timeout expired\n");
                break;
            default:
                val = read_value(pfd->fd);
                printf("Read: %u\n", val);
                break;
        }
    }
}

int main(int argc, const char **argv)
{
    if (argc < 2)
    {
        fprintf(stderr, "Usage: %s debug_flag\n", argv[0]);
        return EXIT_FAILURE;
    }

    set_debug(argv[1]);

    pfd.fd = open_file(filename);

    debug("Setting slave address\n");
    if (ioctl(pfd.fd, I2C_SLAVE, ADDRESS) == -1)
        error_handler("main | ioctl");

    read_data_from_imu(&pfd);

    return EXIT_SUCCESS;
}

编辑

多亏了肯尼贤,添加了一个write解决了这个问题。

因此,如果您想要在x轴上读取加速度计的测量值,您必须这样做

代码语言:javascript
复制
...
#define ACCEL_XOUT_H 0x3b
#define ACCEL_XOUT_L 0x3c
...
write_register(ACCEL_XOUT_H, pfd->fd);
x_data = read_value(pfd->fd);
write_register(ACCEL_XOUT_L, pfd->fd);
x_data = (x_data << 8) + read_value(pfd->fd);
...

编辑2

此外,您还可以简化写入后直接读取2字节的代码。您将得到类似的内容(错误处理省略)

代码语言:javascript
复制
int8_t buff[2];
write_register(ACCEL_XOUT_H, pfd->fd);
read(pfd->fd, buff, 2); //in this way you'll read both the ACCEL_XOUT_H register and the ACCEL_XOUT_L (the next one).
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-09 12:45:24

您可能需要了解i2c的基本知识。

http://www.ti.com/lit/an/slva704/slva704.pdf

3.2 I2C总线上的奴隶读物

要读取I2C寄存器,您需要编写从地址、注册地址和从地址,然后从总线读取数据。但这是司机干的。从地址由ioctl在fd中设置。但是你仍然需要写注册地址。

从你的链接..。

代码语言:javascript
复制
/* Using SMBus commands */
  res = i2c_smbus_read_word_data(file, reg);
  if (res < 0) {
    /* ERROR HANDLING: i2c transaction failed */
  } else {
    /* res contains the read word */
  }

i2c_smbus_read_word_data有包含寄存器地址的reg。但是read()不是。您需要write(reg),然后才能read()

你只需要读取一个字节,除非使用突发模式或什么的。它读取1字节,因为size是1,但却没有意义,while和p++。

command(0x3b, pfd->fd);阅读之前,你正在写3b。但这就像写作一样

代码语言:javascript
复制
68 > 3B , 68 > 00 , 68 <

试着阅读。(其中>对于读位,0,< for 1)可能只需要write(pfd->fd, 0x3b, 1)而不是command

票数 5
EN

Stack Overflow用户

发布于 2016-09-09 13:07:31

i2cdetect来自i2c-tools项目。您可能已经有了i2cget,如果您只需要一个命令行程序来读取寄存器,它就可以工作了。您还可以根据需要进行修改,并从源代码:链接到Github重新编译。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39409124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档