0

现在我正在寻找使用 Cadence SPI 驱动程序在 Linux 上执行一些基本的读写操作。我刚刚使用了 I2C 驱动程序,但对于所有这些驱动程序如何组合在一起以及是否存在它们都符合的通用接口,我仍然有些困惑。

这是我为使用 I2C 驱动程序而编写的代码

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>

#include <errno.h>
#include <string.h>

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>

#define I2C_ADAPTER                 "/dev/i2c-0"
#define I2C_SWITHC_MUX_ADDRESS      0x74
#define DEVICE_ADDRESS              0x54


int main (int argc, char *argv[])
{
    int file;

    uint8_t reg, value;

    char *end;

    /* Take a value to write */

    printf("The device address on the bus: %d", DEVICE_ADDRESS);

    if(argc == 2) {
        value = strtol(argv[1], &end, 16);
        printf("value to write is: %d\n", value);
    }
    else {
        printf("arg failed\n\n.");
    }


    if((file = open(I2C_ADAPTER, O_RDWR)) < 0) {
        printf("Failed to open the bus\n");
        return -1;
    }

    if(ioctl(file, I2C_SLAVE_FORCE, I2C_SWITHC_MUX_ADDRESS) < 0) {
        printf("Unable to open device as slave \n%s\n", strerror(errno));
        return -1;
    }

    char buf[10];

    reg = DEVICE_ADDRESS;

    buf[0] = reg;
    buf[1] = value;

    if(write(file, buf, 2) != 2) {
        printf("Failed to write to bus %s.\n\n", strerror(errno));
    }
    else {
        printf("Successful write\n");
        printf(buf);
        printf("\n\n");
    }

    if(read(file, buf, 1) != 1) {
        printf("Failed to read from the i2c bus.\n %s\n\n", strerror(errno));
    }
    else {
        printf("Successful read\n");
        printf("Buf = [%02u]\n", buf[0]);
        printf("\n\n");
    }

    return 0;
}

我对实际调用驱动程序的位置感到困惑。现在我在这里阅读 SPI 驱动程序文档:

https://www.kernel.org/doc/html/v4.11/driver-api/spi.html

I2C 驱动程序的相应文档在这里:

https://www.kernel.org/doc/html/v4.11/driver-api/i2c.html

I2C 文档中定义的第一个结构是 i2c_driver。我不认为我在编写 I2C 程序时使用了 I2C 驱动程序文档中定义的任何结构。我真的使用过 I2C Cadence 驱动程序吗?如果没有,如何重写我的程序以使用 I2C Cadence 驱动程序,这样我就可以了解如何在用户空间中使用驱动程序,从而可以使用 SPI 驱动程序。

4

2 回答 2

0

把我的两分钱放进去,你也可以使用 i2c-tools 实用程序。这使得使用 linux 处理 i2c 设备变得非常容易。参考如何使用它。https://elinux.org/Interfacing_with_I2C_Devices

于 2017-11-08T07:41:59.763 回答
0

这是调用堆栈从用户空间通过内核 I2C 框架到 Cadence I2C 驱动程序的样子(类似于read()/write()):

  1. 用户空间驱动程序ioctl()调用compat_i2cdev_ioctl()i2c-dev.c
  2. i2cdev_ioctl_rdwr()呼入i2c_transfer()_i2c-core-case.c
  3. __i2c_transfer()来电adap->algo->master_xfer()

并且这个函数指针在注册 I2C 驱动程序时.master_xfer使用定义的回调进行初始化,在函数中:i2c-cadence.cprobe()

id->adap.algo = &cdns_i2c_algo;

struct i2c_algorithm cdns_i2c_algo = {
.master_xfer    = cdns_i2c_master_xfer,

希望能帮助到你。

于 2018-12-10T15:53:21.607 回答