MCP2515 on SPI0 UEXT

Started by lilianEver, April 15, 2021, 12:43:26 PM

Previous topic - Next topic

lilianEver

Hello,

I want to add CAN functionality on my Olimex card and i am a little stuck.

Olimex: A64-OLinuXino-2Ge8G-IND
Réference:
Can module
Overlay exemple

I want to use the module on the SPI0 on UEXT connector:

| Olimex A64 | Nb Conn |   Function   | Output | Module  |
| ---------- | ------- | ------------ | ------ | ------- |
| +3.3       | 1       |              | VCC    | 7       |
| GND        | 2       |              | GND    | 6       |
| PH4        | 3       | UART3TX      |        |         |
| PH5        | 4       | UART3RX      |        |         |
| PH2        | 5       | TWI1-SCK     | INT    | 1       |
| PH3        | 6       | TWI1-SDA     |        |         |
| PC1        | 7       | SPI_MISO     | SO     | 4       |
| PC0        | 8       | SPI_MOSI     | SI     | 3       |
| PC2        | 9       | SPI_CLK      | SCK    | 2       |
| PC3        | 10      | SPI_CS       | CS     | 5       |

I have added some module to load for the kernel in "/etc/modules"
can-dev
mcp251x

Here the overlay i have tryed to add on the system:

/dts-v1/;
/plugin/;

/ {
        model = "Olimex A64-olinuxino-2Ge8G";
        compatible = "olimex,a64-olinuxino-2Ge8G\0allwinner,sun50i-a64";

        fragment@0 {
                target-path = "/clocks";
                __overlay__ {
                        can0_osc_fixed: can0_osc_fixed {
                                compatible = "fixed-clock";
                                #clock-cells = <0>;
                                clock-frequency  = <8000000>;
                        };
                };
        };

        fragment@1 {
                target = <&r_pio>;
                __overlay__ {
                        can0_pin_irq: can0_pin_irq {
                                pins = "PH2";
                                function = "irq";
                                bias-pull-up;
                        };
                };
        };

        fragment@2 {
                target = <&spi0>;
                __overlay__ {
                        status = "okay";
                        #address-cells = <1>;
                        #size-cells = <0>;
                        can0:mcp2515@0 {
                                reg = <0>;
                                compatible = "microchip,mcp2515";
                                pinctrl-names = "default";
                                pinctrl-0 = <&can0_pin_irq>;
                                spi-max-frequency = <10000000>;
                                interrupt-parent = <&r_pio>;
                                interrupts = <0 8 2>; /* FIXME PH2 IRQ_TYPE_EDGE_FALLING */
                                clocks = <&can0_osc_fixed>;
                                status = "okay";
                        };
                };
        };
};

Then i do:

armbian-add-overlay spi-a64-mcp2515.dts
But after a reboot, i see nothing  on dmesg

dmesg | grep mcp
I forget something to enable the spi or the can bus ?

Thank for your help.


lilianEver

Update:

I was on ARMBIAN. (official image from armbian focal)
I have seen this topic: Enable SPI0 on UEXT.

So i have download this image : A64-OLinuXino-focal-minimal-20210318-122357.img

- Enable modules on "/etc/modules"

- Donloaded the script for overlay : here

then:
dtc -O dtb -o sun50i-a64-mcp251x.dtbo -b 0 -@ sun50i-a64-mcp251x.dts
cp ../sun50i-a64/sun50i-a64-mcp251x.dtbo /usr/lib/olinuxino-overlays/sun50i-a64/
./olinuxino-overlays/scripts/olinuxino-overlay

- Enable spi0 and my custom mcp251x. I don't see any changes.

LubOlimex

The unmodified image has the script inside. Download the image, boot and just type:

olinuxino-overlay
Technical support and documentation manager at Olimex

lilianEver

Hello,

The overlay was bugged, i changed few things and the can0 appear (with both image Olinuxino and Armbian)

i have change "/clocks" to "/" and change "r_pio" by "pio".

The complete changes:
/dts-v1/;
/plugin/;

/ {
        compatible = "allwinner,sun50i-a64",
                "olimex,a64-olinuxino";
        description = "Enable mcp2515 on SPI0";

        fragment@0 {
                target-path = "/";
                __overlay__ {
                        can0_osc_fixed: can0_osc_fixed {
                                compatible = "fixed-clock";
                                #clock-cells = <0>;
                                clock-frequency  = <8000000>;
                        };
                };
        };

        fragment@1 {
                target = <&pio>;
                __overlay__ {
                        can0_pin_irq: can0_pin_irq {
                                pins = "PH2";
                                function = "irq";
                                bias-pull-up;
                        };
                };
        };

        fragment@2 {
                target = <&spi0>;
                __overlay__ {
                        status = "okay";
                        #address-cells = <1>;
                        #size-cells = <0>;
                        can0:mcp2515@0 {
                                reg = <0>;
                                compatible = "microchip,mcp2515";
                                pinctrl-names = "default";
                                pinctrl-0 = <&can0_pin_irq>;
                                spi-max-frequency = <10000000>;
                                interrupt-parent = <&pio>;
                                interrupts = <0 194 2>; /* PH2 IRQ_TYPE_EDGE_FALLING */
                                clocks = <&can0_osc_fixed>;
                                status = "okay";
                        };
                };
        };
};

when i use the command "dmesg", i got:

mcp251x spi0.0 can0: MCP2515 successfully initialized.
In my first attempt, it was impossible to up the can0 interface with the command :ip link set can0 up type can bitrate 500000
After some changes on this line:
  interrupts = <0 194 2>; /* PH2 IRQ_TYPE_EDGE_FALLING */ It is was possible to up the interface without problem.
But i think my settings are wrongs. I don't see interrupt when i do cat /proc/interrupts
Someone know how to configure this line ?








JohnS

You might try asking on linux-sunxi.

John

lilianEver

Hello, after some search i found more overlays here.

i have deduce how set interrupts:
- Interrupts = <7 9 2>; /* PH9 IRQ_TYPE_EDGE_FALLING */
- Interrupts = <0 10 2>; /* PA10 IRQ_TYPE_EDGE_FALLING */

I use PB0 pin so i set this one:

interrupts = <1 0 2>; /* PB0 */

I tested it on Armbian (kernel 5.10). The new problem was the impossibility to up the CAN bus. IT was freezing when you do "ip link set can0 up"
The last problem was interrupt type:

1 = low-to-high edge triggered.
2 = high-to-low edge triggered.
4 = active high level-sensitive.
8 = active low level-sensitive.

We need set to "8" the interrupt type:

interrupts = <1 0 8>; /* PB0 */

That it. the can0 bus seem to work now.


JohnS

Good find! And excellent :)

John