如何加载/创建一个控制移位寄存器的spi内核模块?

时间:2016-10-10 14:32:31

标签: linux linux-kernel linux-device-driver kernel-module spi

HeJ小鼠,

我想在我的定制板上访问一个通过SPI连接的移位寄存器。加载我的内核模块时,我收到了spi_busnum_to_master失败的错误。

我的内核模块是一个字符设备,它写入移位寄存器+切换一个引脚。用户可以写入和读取此设备。模块转换输入并通过spi发送一些字节。

检查后发现可以创建一个SPI Slave设备。我的代码:

int helper_init_spi(void) {
    int ret;
    struct spi_master *master;

    struct spi_board_info spi_device_info = {
        .modalias = "mySPIslave",
        .max_speed_hz = 1000000, //speed your device (slave) can handle
        .bus_num = 0,
        .chip_select = 0,
        .mode = 3, // 3 wire
    };

    // get the spi master
    master = spi_busnum_to_master(spi_device_info.bus_num);
    if( !master ){
        printk(KERN_ALERT "%s: spi bus master (spi%i) not found\n", "myModule", spi_device_info.bus_num);
        return -ENODEV;
    }

    // create a new slave device, given the master and device info
    gMyDev = spi_new_device( master, &spi_device_info );
    if(!gMyDev) {
        printk(KERN_ALERT "%s: failed to create the %s spi slave\n", "myModule", spi_device_info.modalias);
        return -ENODEV;
    }

    // set 8Bit mode
    gpESAledMatrix->bits_per_word = 8;

    // setup
    ret = spi_setup(gMyDev);
    if( ret ){
        printk(KERN_ALERT "%s: failed to setup spi slave\n", "myModule");
        spi_unregister_device(gpESAledMatrix);
        gpESAledMatrix = NULL;
        return -ENODEV;
    }
    return 0;
}

at" spi_busnum_to_master"我的代码失败了。我正在检查内核中的spi总线(通过dmesg):

[    2.870000] atmel_spi f0004000.spi: version: 0x213
[    2.870000] atmel_spi f0004000.spi: Using dma0chan3 (tx) and dma0chan4 (rx) for DMA transfers
[    2.880000] atmel_spi f0004000.spi: Atmel SPI Controller at 0xf0004000 (irq 25)
[    3.090000] atmel_spi f8008000.spi: version: 0x213
[    3.100000] atmel_spi f8008000.spi: Using dma1chan0 (tx) and dma1chan1 (rx) for DMA transfers
[    3.100000] atmel_spi f8008000.spi: Atmel SPI Controller at 0xf8008000 (irq 30)
[    3.110000] m25p80 spi32765.0: s25fl164k (8192 Kbytes)

对我来说,看起来这两个spi-bus已正确初始化。但是当检查" / dev /"没有spi-masters。

root@sama5d3xek:/# find . -name *spi*
./sys/bus/spi
./sys/bus/spi/devices/spi32765.0
./sys/bus/spi/drivers/m25p80/spi32765.0
./sys/bus/spi/drivers/spidev
./sys/bus/platform/devices/f0004000.spi
./sys/bus/platform/devices/f8008000.spi
./sys/bus/platform/drivers/atmel_spi
./sys/bus/platform/drivers/atmel_spi/f0004000.spi
./sys/bus/platform/drivers/atmel_spi/f8008000.spi
./sys/bus/platform/drivers/atmel_qspi
./sys/bus/platform/drivers/spi_gpio
./sys/devices/soc0/ahb/ahb:apb/f0004000.spi
./sys/devices/soc0/ahb/ahb:apb/f0004000.spi/spi_master
./sys/devices/soc0/ahb/ahb:apb/f0004000.spi/spi_master/spi32766
./sys/devices/soc0/ahb/ahb:apb/f0004000.spi/spi_master/spi32766/statistics/spi_sync_immediate
./sys/devices/soc0/ahb/ahb:apb/f0004000.spi/spi_master/spi32766/statistics/spi_sync
./sys/devices/soc0/ahb/ahb:apb/f0004000.spi/spi_master/spi32766/statistics/spi_async
./sys/devices/soc0/ahb/ahb:apb/f8008000.spi
./sys/devices/soc0/ahb/ahb:apb/f8008000.spi/spi_master
./sys/devices/soc0/ahb/ahb:apb/f8008000.spi/spi_master/spi32765
./sys/devices/soc0/ahb/ahb:apb/f8008000.spi/spi_master/spi32765/statistics/spi_sync_immediate
./sys/devices/soc0/ahb/ahb:apb/f8008000.spi/spi_master/spi32765/statistics/spi_sync
./sys/devices/soc0/ahb/ahb:apb/f8008000.spi/spi_master/spi32765/statistics/spi_async
./sys/devices/soc0/ahb/ahb:apb/f8008000.spi/spi_master/spi32765/spi32765.0
./sys/devices/soc0/ahb/ahb:apb/f8008000.spi/spi_master/spi32765/spi32765.0/statistics/spi_sync_immediate
./sys/devices/soc0/ahb/ahb:apb/f8008000.spi/spi_master/spi32765/spi32765.0/statistics/spi_sync
./sys/devices/soc0/ahb/ahb:apb/f8008000.spi/spi_master/spi32765/spi32765.0/statistics/spi_async
./sys/class/spi_master
./sys/class/spi_master/spi32765
./sys/class/spi_master/spi32766
./sys/class/spidev
./sys/firmware/devicetree/base/ahb/apb/pmc@fffffc00/periphck/spi0_clk
./sys/firmware/devicetree/base/ahb/apb/pmc@fffffc00/periphck/spi1_clk
./sys/firmware/devicetree/base/ahb/apb/pinctrl@fffff200/spi0
./sys/firmware/devicetree/base/ahb/apb/pinctrl@fffff200/spi0/spi0-0
./sys/firmware/devicetree/base/ahb/apb/pinctrl@fffff200/spi1
./sys/firmware/devicetree/base/ahb/apb/pinctrl@fffff200/spi1/spi1-0
./sys/firmware/devicetree/base/ahb/apb/spi@f0004000
./sys/firmware/devicetree/base/ahb/apb/spi@f8008000
./sys/firmware/devicetree/base/ahb/apb/spi@f8008000/m25p80@0/spi-max-frequency
./sys/kernel/debug/clk/spi0_clk
./sys/kernel/debug/clk/spi1_clk
./sys/module/spidev
./usr/share/vim/vim74/ftplugin/slpspi.vim
./usr/share/vim/vim74/syntax/slpspi.vim
./usr/share/vim/vim74/syntax/spice.vim
./proc/irq/25/f0004000.spi
./proc/irq/30/f8008000.spi

任何想法如何解决?

0 个答案:

没有答案