Olimex Support Forum

OLinuXino Android / Linux boards and System On Modules => A64 => Topic started by: lilianEver on April 15, 2021, 12:43:26 PM

Title: MCP2515 on SPI0 UEXT
Post by: lilianEver on April 15, 2021, 12:43:26 PM
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 (https://www.raspberrypi.org/forums/viewtopic.php?t=141052)
Overlay exemple (https://forum.armbian.com/topic/11733-mcp2515-can-bus-user-overlay/)

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.

Title: Re: MCP2515 on SPI0 UEXT
Post by: lilianEver on April 15, 2021, 06:59:36 PM
Update:

I was on ARMBIAN. (official image from armbian focal)
I have seen this topic: Enable SPI0 on UEXT (https://www.olimex.com/forum/index.php?topic=7756.0).

So i have download this image : A64-OLinuXino-focal-minimal-20210318-122357.img (http://images.olimex.com/release/a64/)

- Enable modules on "/etc/modules"

- Donloaded the script for overlay : here (https://github.com/OLIMEX/olinuxino-overlays)

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.
Title: Re: MCP2515 on SPI0 UEXT
Post by: LubOlimex on April 16, 2021, 11:45:04 AM
The unmodified image has the script inside. Download the image, boot and just type:

olinuxino-overlay
Title: Re: MCP2515 on SPI0 UEXT
Post by: lilianEver on April 19, 2021, 04:44:10 PM
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 ?







Title: Re: MCP2515 on SPI0 UEXT
Post by: JohnS on April 19, 2021, 08:49:26 PM
You might try asking on linux-sunxi.

John
Title: Re: MCP2515 on SPI0 UEXT
Post by: lilianEver on April 21, 2021, 06:46:10 PM
Hello, after some search i found more overlays here (https://github.com/armbian/sunxi-DT-overlays).

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.

Title: Re: MCP2515 on SPI0 UEXT
Post by: JohnS on April 21, 2021, 09:00:40 PM
Good find! And excellent :)

John