Linux 使用 SPIDEV 访问 SPI 设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10697689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:25:38  来源:igfitidea点击:

SPI device access using SPIDEV

linuxlinux-device-driverembedded-linuxspi

提问by stef

I want to access an SPI device (an optical mouse device from Avago Tech) on an embedded Linux system using the SPIDEV driver. The device is connected to SPI0.

我想使用 SPIDEV 驱动程序访问嵌入式 Linux 系统上的 SPI 设备(Avago Tech 的光学鼠标设备)。该设备连接到 SPI0。

I enabled SPI and "User mode SPI device driver support" in menuconfig > "Device Drivers" > "SPI".

我在 menuconfig >“设备驱动程序”>“SPI”中启用了 SPI 和“用户模式 ​​SPI 设备驱动程序支持”。

I added the code to the board.c file

我将代码添加到 board.c 文件中

static struct spi_board_info spidev_board_info[] {
    {
        .modalias = "spidev",
        .max_speed_hz = 1000000,
        .bus_num = 1,
        .chips_select = 0,
        .mode = SPI_MODE_3,
    },
    {
        .modalias = "spidev",
        .max_speed_hz = 1000000,
        .bus_num = 1,
        .chips_select = 1,
        .mode = SPI_MODE_3,
    },
};
spi_register_board_info(spidev_board_info, ARRAY_SIZE(spidev_board_info));

I tried both 500000 and 1000000 as max_speed_hz (1Mhz being the highest allowed by the sensor). SPI_MODE_3 is correct, checked on the datasheet. bus_num = 1 should correct as it refers to SPI0 (I also tried = 0 out of curiosity).

我尝试了 500000 和 1000000 作为 max_speed_hz(1Mhz 是传感器允许的最高值)。SPI_MODE_3 是正确的,在数据​​表上检查过。bus_num = 1 应该是正确的,因为它指的是 SPI0(出于好奇,我也试过 = 0)。

I checked the electrical connections and are all working.

我检查了电气连接,一切正常。

The kernel compiles and the image starts correctly, but I cannot find any device in /sys/class/spidev/ (neither in /sys/bus/spi/...). No reference to SPI appears during system boot either.

内核编译并且映像正确启动,但我在 /sys/class/spidev/ 中找不到任何设备(在 /sys/bus/spi/... 中都没有)。在系统启动期间也不会出现对 SPI 的引用。

Any idea on where the problem can be?

知道问题出在哪里吗?

采纳答案by Longfield

I think that with the Atmel boards, you are supposed to register the devices with the at91_add_device_spi function. It would then be:

我认为对于 Atmel 板,您应该使用 at91_add_device_spi 函数注册设备。那么它将是:

at91_add_device_spi(spidev_board_info, ARRAY_SIZE(spidev_board_info));

At least you have to make sure that this function gets called by your board support file board-sam9x5ek.cso that the SPI master controller gets initialized. If this is done, your above proposed initialization is correct.

至少您必须确保您的电路板支持文件调用此函数,board-sam9x5ek.c以便初始化 SPI 主控制器。如果这样做,你上面建议的初始化是正确的。