Raspberry Pi 3b+ 带多个 CAN 总线 (MPC2515))

RaspberryPi 3b+ with multiple can buses (MPC2515))

我正在尝试通过 spi0 连接 6 个 mcp2515。我已经调整了一个 SPI 覆盖层来添加必要的芯片 select 行。我的新 SPI 覆盖图如下所示:

{
compatible = "brcm,bcm2835", "brcm,bcm2836", "brcm,bcm2708", "brcm,bcm2709";

fragment@0 {
    target = <&spi0>;
    frag0: __overlay__ {
        #address-cells = <1>;
        #size-cells = <0>;
        pinctrl-0 = <&spi0_pins &spi0_cs_pins>;
        status = "okay";
        cs-gpios = <&gpio 8 1>, <&gpio 7 1>, <&gpio 22 1>, <&gpio 23 1>, <&gpio 24 1>, <&gpio 25 1>;

        spidev@0{
            compatible = "spidev";
            reg = <0>;  /* CE0 */
            #address-cells = <1>;
            #size-cells = <0>;
            spi-max-frequency = <500000>;
        };
        spidev@1{
            compatible = "spidev";
            reg = <1>;  /* CE1 */
            #address-cells = <1>;
            #size-cells = <0>;
            spi-max-frequency = <500000>;
        };
        spidev@2{
            compatible = "spidev";
            reg = <2>;  /* CE2 */
            #address-cells = <1>;
            #size-cells = <0>;
            spi-max-frequency = <500000>;
        };

        spidev@3{
            compatible = "spidev";
            reg = <3>;  /* CE3 */
            #address-cells = <1>;
            #size-cells = <0>;
            spi-max-frequency = <500000>;
        };
        spidev@4{
            compatible = "spidev";
            reg = <4>;  /* CE4 */
            #address-cells = <1>;
            #size-cells = <0>;
            spi-max-frequency = <500000>;
        };
        spidev@5{
            compatible = "spidev";
            reg = <5>;  /* CE5 */
            #address-cells = <1>;
            #size-cells = <0>;
            spi-max-frequency = <500000>;
        };
        
                    
    };
};

fragment@1 {
    target = <&gpio>;
    __overlay__ {
        spi0_cs_pins: spi0_cs_pins {
            brcm,pins = <7 8 22 23 24 25>;
            brcm,function = <1>; /* out */
        };
    };
};

有了这个 SPI 覆盖,我在 /sys/bus/spi/devices/

中有 6 个 spi
spi0.0  spi0.1  spi0.2  spi0.3  spi0.4  spi0.5

我还为 mcp2515(can0 到 can5)制作了新的覆盖层,以便将它们与新芯片 select 行的 spi0 绑定。

我的 /boot/config.txt 看起来像这样:

dtoverlay=spi-gpio-cs-new
dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=5
dtoverlay=mcp2515-can4,oscillator=8000000,interrupt=26
dtoverlay=mcp2515-can5,oscillator=8000000,interrupt=27

dmesg | grep mcp

[    7.870207] mcp251x spi0.5 can0: MCP2515 successfully initialized.
[    7.892886] mcp251x spi0.4 can1: MCP2515 successfully initialized.
[    7.908725] mcp251x spi0.0 can2: MCP2515 successfully initialized.

ifconfig

can0: flags=193<UP,RUNNING,NOARP>  mtu 16
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 10  (UNSPEC)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

can1: flags=193<UP,RUNNING,NOARP>  mtu 16
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 10  (UNSPEC)
        RX packets 36  bytes 180 (180.0 B)
        RX errors 0  dropped 36  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

can2: flags=193<UP,RUNNING,NOARP>  mtu 16
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 10  (UNSPEC)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

目前我只有3块mcp2515板可供使用。为了不损坏 Raspberry Pi GPIO,我修改了它们关于 CAN 收发器(5V)和控制器(3V)的电压供应,这些板已经过单独测试,我能够 send/receive CAN与他们相框。他们像这样连接到树莓派

在这 3 个接口中,只有 can1 (spi0.4) 可以工作!使用 candump 我可以看到网络上的帧。

我的问题是为什么在尝试发送或接收 CAN 消息时 can0 和 can2 被静音(candumpcansend)?

内核中断table

CPU0       CPU1       CPU2       CPU3       
 17:        217          0          0          0  ARMCTRL-level   1 Edge      3f00b880.mailbox
 18:         47          0          0          0  ARMCTRL-level   2 Edge      VCHIQ doorbell
 40:          0          0          0          0  ARMCTRL-level  48 Edge      bcm2708_fb DMA
 42:        352          0          0          0  ARMCTRL-level  50 Edge      DMA IRQ
 44:       3062          0          0          0  ARMCTRL-level  52 Edge      DMA IRQ
 45:          0          0          0          0  ARMCTRL-level  53 Edge      DMA IRQ
 48:          0          0          0          0  ARMCTRL-level  56 Edge      DMA IRQ
 56:      14104          0          0          0  ARMCTRL-level  64 Edge      dwc_otg, dwc_otg_pcd, dwc_otg_hcd:usb1
 78:          0          0          0          0  ARMCTRL-level  86 Edge      3f204000.spi
 80:        158          0          0          0  ARMCTRL-level  88 Edge      mmc0
 81:       7450          0          0          0  ARMCTRL-level  89 Edge      uart-pl011
 86:       4207          0          0          0  ARMCTRL-level  94 Edge      mmc1
161:          0          0          0          0  bcm2836-timer   0 Edge      arch_timer
162:       1813       1900       2264       1528  bcm2836-timer   1 Edge      arch_timer
165:          0          0          0          0  bcm2836-pmu   9 Edge      arm-pmu
166:          0          0          0          0  lan78xx-irqs  17 Edge      usb-001:004:01
167:          0          0          0          0  pinctrl-bcm2835  26 Edge      spi0.5
168:          6          0          0          0  pinctrl-bcm2835  27 Edge      spi0.4
169:          0          0          0          0  pinctrl-bcm2835   5 Level     spi0.0
FIQ:              usb_fiq
IPI0:          0          0          0          0  CPU wakeup interrupts
IPI1:          0          0          0          0  Timer broadcast interrupts
IPI2:       1469       2966       3711       4460  Rescheduling interrupts
IPI3:        203        798        542        445  Function call interrupts
IPI4:          0          0          0          0  CPU stop interrupts
IPI5:         55         85         41         24  IRQ work interrupts
IPI6:          0          0          0          0  completion interrupts
Err:          0 

从这个table我可以看出,SPI 被分配了中断,但实际上只有 spi0.4 被激活。 如何激活spi0.0和spi0.5的另外2个中断?

有效!!! 正如我在第一个 post 中提到的,只有一块板在工作(can1 spi0.4),在我重新检查其他两块不工作的板后,我发现一块硬件损坏导致另一块板无法正常工作。作为最终结论,我的 spi 和 mcp 叠加层功能齐全!

问候 安特玛

 [    7.846788] mcp251x spi0.0 can0: MCP2515 successfully initialized.
 [    7.888039] mcp251x spi0.1 can1: MCP2515 successfully initialized.
 [    7.924747] mcp251x spi0.2 can2: MCP2515 successfully initialized.
 [    7.936608] mcp251x spi0.3 can3: MCP2515 successfully initialized.

can0  241   [5]  67 A4 31 F0 C7
can1  2A0   [2]  02 93
can2  241   [5]  67 A4 31 F0 CB
can3  240   [2]  02 6A