Using an enc28j60 on the zero

@kwisatz I will do that for you . It turns out I do have a logic analyzer , so ill put it to hopefully good use.

We just had a new engineer join our team, and this is his first assignment. I’ll let you know if we made more progress on this with GPIO-SPI.

Thanks for your investigation.
We purchased the enc28j60 module and tested it on radxa zero (based on amlogic). The problem you mentioned does exist. At the same time, we also tested on our other boards (based on rockchip) and found that it was feasible. The timing of the cs pin is the same as you described.

@kwisatz @shawaj @iplanux @feng Doing a mass ping here since we have implemented a gpio-spi based overlay and have tested it with real hardware.
You can find the source code for overlay here.
However, when testing against a iperf3 server, we only recorded around 500kpbs speed. Logic analyzer shows inconsistent clock signal, which is an unfortunately limitation with this software SPI imitation.

Edit: A SPI based overlay has been uploaded with combined bandwidth of ~3.5Mbps. @iplanux’s idea of using cs-gpios proves to be a better solution.

Sorry, may I just ask how to compile this overlay? I failed by running just simple dtc:

$ dtc -I dts -O dtb -o meson-g12a-spi-b-enc28j60.dtbo meson-g12a-spi-b-enc28j60.dts
Error: meson-g12a-spi-b-enc28j60.dts:4.1-9 syntax error
FATAL ERROR: Unable to parse input tree

Thank you.

This is currently intended to be compiled along with the kernel, so the kbuild can resolve include directives first. Your error message is on line 4 which is the unsupported #include line for dtc. So for right now you should delete those include lines, and replace the constants (dtc will error out on them) with the value you can find from those included files. Those files are in the Linux kernel.

We will have a out-of-tree build guide later.

Up and working! Thank you very much!

1 Like

@wanthalf perhaps did you forget the -@ parameter to dtc ? Apparently it’s mandatory to compile overlays that lay above another one ; I think it’s related to symbol resolution. I don’t know if it’s involved in includes though.

I have not found any documentation to that option, so I have no idea what it does - but I think (not sure) that I tried it as well, without any effect. But replacing the (three?) constants manually worked well.

I think it would be nice to include both this overlay and the enc28j60.ko kernel module in the pre-built radxa images. It is an easy and cheap way to add a basic wired ethernet interface to the Zero - and probably sufficient for many IoT projects.

I plan to use the third UART_EE_C, so I modified the overlay for SPI-A and it seems to work. I use GPIOH_4 for interrupt – that is number 32 (thanks to @Antonr for the numbers!). I hope it won’t collide with the UART_EE_C just because GPIOH_4 is also assigned as UART_EE_C_RTS…?

UPDATE: Well, it does… :frowning: So, changing to GPIOC_7, number 60.

Just in case someone were interested:

/dts-v1/;
/plugin/;

/ {
	compatible = "radxa,zero", "amlogic,g12a";

	fragment@0 {
		target = <&spicc0>;
		__overlay__ {
			pinctrl-0 = <&spicc0_x_pins>;
			pinctrl-names = "default";
			#address-cells = <1>;
			#size-cells = <0>;
			cs-gpios = <&gpio 75 1>;
			status = "okay";

			ethernet: enc28j60@0 {
				compatible = "microchip,enc28j60";
				reg = <0>;
				spi-max-frequency = <20000000>;

				pinctrl-names = "default";
				pinctrl-0 = <&enc28j60_int_pins>;

				interrupt-parent = <&gpio_intc>;
				interrupts = <60 2>;
			};
		};
	};

	fragment@1 {
		target = <&periphs_pinctrl>;
		__overlay__ {
			enc28j60_int_pins: enc28j60-int-pins@0 {
				mux {
					groups = "GPIOC_7";
					function = "gpio_periphs";
					bias-pull-up;
					drive-strength-microamp = <4000>;
				};
    		};
		};
	};
};

Hi Yuntian,
I have tried to compile my files using dtc
$ dtc -I dts -O dtb -o meson-g12a-spi-b-enc28j60.dtbo meson-g12a-spi-b-enc28j60.dts
But no matter what, it never works and always gives the same error:
Error: meson-g12a-spi-b-enc28j60.dts:xxx syntax error
FATAL ERROR: Unable to parse input tree.
I have already deleted those #includes, but the error came from the next line continue.

Do you have the out-of-tree bulid yet? Or can you please tell me how to compile the .dts file correctly?

Thanks!

Hi @kwisatz @feng,
How do you compile the .dts files into Radxa? I have tried with dtc to compile and still not working.
It always gives same error:
Error: meson-g12a-spi-b-enc28j60.dts: xxx syntax error
FATAL ERROR: Unable to parse input tree.
I have already tried to delete #include, but it changes nothing, the error came immediately from the next line.
I don’t have any clue how to compile it now. Could you please give me some guides?

For @Enoch_Lee, here is my (manually) preprocessed source for meson-g12a-spi-b-enc28j60.dts. In this form it can already be directly compiled.

/dts-v1/;
/plugin/;

/ {
	compatible = "radxa,zero", "amlogic,g12a";

	fragment@0 {
		target = <&spicc1>;
		__overlay__ {
			pinctrl-0 = <&spicc1_pins>;
			pinctrl-names = "default";
			#address-cells = <1>;
			#size-cells = <0>;
			cs-gpios = <&gpio 22 1>;
			status = "okay";

			ethernet: enc28j60@0 {
				compatible = "microchip,enc28j60";
				reg = <0>;
				spi-max-frequency = <20000000>;

				pinctrl-names = "default";
				pinctrl-0 = <&enc28j60_int_pins>;

				interrupt-parent = <&gpio_intc>;
				interrupts = <85 2>;
			};
		};
	};

	fragment@1 {
		target = <&periphs_pinctrl>;
		__overlay__ {
			enc28j60_int_pins: enc28j60-int-pins@0 {
				mux {
					groups = "GPIOX_8";
					function = "gpio_periphs";
					bias-pull-up;
					drive-strength-microamp = <4000>;
				};
    		};
		};
	};
};

The source for meson-g12a-spi-a-enc28j60.dts has been posted above in this post. But I thought the precompiled overlays were now part of the Radxa distribution, aren’t they?

Hi @wanthalf, I have already compiled both .dts Files from you, and copied them into the Overlay Ordner. But I can still not find the Device(ENC28J60) in the lsmod or dmesg and when I connect my enc28j60 to Radxa Zero via SPIA or SPIB pins, I still can’t get any data from the Ethernet. The uEnv.txt data from me is like this:

verbosity=7
console=ttyAML0,115200
overlay_prefix=meson
rootfstype=ext4
fdtfile=amlogic/meson-g12a-radxa-zero.dtb
overlays=meson-g12a-spi-b-enc28j60
param_spidev_spi_bus=1
param_spidev_max_freq=10000000
rootuuid=dc5be088-dcd0-4a74-96e5-7879177587da
initrdsize=0xc16519
kernelversion=5.10.69-999-amlogic
initrdimg=initrd.img-5.10.69-999-amlogic
kernelimg=vmlinuz-5.10.69-999-amlogic

and the Pin assignment is like:

MOSI -> GPIOH4 (SPI_B_MOSI)
MISO -> GPIOH5 (SPI_B_MISO)
SCK -> GPIOH7 (SPI_B_SLK)
CS -> GPIOH6 (SPI_B_SS0)
INT -> GPIOX8 (PWM_C)

I would like to ask:

  • How you connected the PINs to Radxa Zero to get it to work?

  • Did I miss any steps?(Compile -> copy into Overlay -> add new element in uEnv.txt -> Reboot)

  • What did you write in your uEnv.txt?

I hope I can get your reply, it will help me a lot.~( ̄▽ ̄)~
Thank you!!

From the look of it you are using a custom kernel. Please check if you have included enc28j60 driver in your build:

grep -i enc28j60 /boot/config*

Hi @RadxaYuntian ,
Thanks for your reply. The driver from enc28j69 is maybe not in the build, the Terminal shows “CONFIG_ENC28J60 is not set”. I’m using Debian Buster from your official Website. What should I do next?

Are you using custom kernel? You can use make menuconfig and enable CONFIG_ENC28J60 in your custom kernel, and install the updated package.

I am not using a custom kernel. When I give the command make menuconfig, I get:
make:***No rule to make target 'menuconfig'. Stop.
How do I use the custom kernel?
I’m sorry I’m new to Linux, can you please explain it more clearly?

Hi @Enoch_Lee,

How to build a custom kernel is well described on the Radxa wiki: https://wiki.radxa.com/Zero/dev/kernel
If you need help with that, I suggest you open another thread, rather than continue posting on this one which should be limited to the enc28j60 modules specifically.

Can you test this image? This one should contain the overlay and the driver.