Some notes on experience of OrangePiZero.

Downloaded the ‘legacy 3.4’ Armbian image from here, pushed it onto a 32Gbyte uSD card using Roadkill’s DiskImage_1_6_WinAll.exe and LockDismount.exe.

The card booted on the Orangepizero with no issue and expanded it’s partition to fill the card.

I then used Peter Scargill’s excellent script to install a bunch of useful stuff.  The used space on the SD card ended up around 2Gbytes….

I’d bought 4 OrangePiZero boards with boxes and extra USB boards, and the first thing I wanted to have was a pattern card to copy to populate each.  32Gbytes is a lot of data to move from card to card, so I reduced the partition size to 4GBytes using a linux VM, resize2fs, and careful use of fdisk.

The next thing was to enable the OrangePiZero to talk to my OWL energy monitor.  This is a USB connected device which runs at 250kbits; not a linux supported rate.  I had modified the CP120x driver in a 4.8 Debian kernel for this before (using a x86 VM and cross compilation), but wanted to see if I could build a single driver within the OrangePiZero.

Kernel hacking is something I’ve done sporadically over the years, but never became an expert in.  I knew I needed the kernel headers, configuration, and the correct kernel sources.  The kernel headers appear to be in the Armbian image already.  The sources (for the legacy kernel version) appear to be here.

So, in the OrangePiZero command prompt, I created a new partition with the (newly empty) SD card space, mounted it to ~/linuxmnt, and in that folder:

wget https://github.com/igorpecovnik/linux/archive/sun8i.zip
mkdir sunx8i
unzip sun8i.zip -d sun8i

now in theory, I have the kernel sources…  then following this…

cd sun8i/linux-sun8i
make oldconfig

This gave some questions, which I just hit ‘return’ to… then…

make prepare

which ends with an error:

include/linux/compiler-gcc.h:100:30: fatal error: linux/compiler-gcc5.h: No such file or directory

a bit of a show stopper; googling indicates that this means that kernels in the 3.4 range were not designed to be built with gcc 5+; which is what is installed on the OrangePiZero :(.

What to do?  more googling says you can havd gcc5 and 4 installed, and use ‘update-alternatives’ to switch between them, so of we go:

sudo apt-get install gcc-4.9

But I should have read better; seems g++-4.9 does not exist, so

sudo apt-get install gcc-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50 --slave /usr/bin/g++ g++ /usr/bin/g++-5
sudo update-alternatives --config gcc

and select gcc-4.8, then again:

make prepare

This time it completes, so on to:

make scripts

this also completed.  so now to build a single module.  First renaming the original:

sudo mv -v /lib/modules/$(uname -r)/kernel/drivers/usb/serial/cp210x.ko /lib/modules/$(uname -r)/kernel/drivers/usb/serial/cp210x.ko.backup

Then to attempt a first rebuild of the module: First I tried (from the linux sources root)

make drivers/usb/serial/cp210x.ko

but when insmodded, got ‘invalid module format’.  So I tried:

make modules SUBDIRS=drivers/usb/serial

this makes ALL the usb serial modules (It did not take long), but same deal;

insmod: ERROR: could not insert module drivers/usb/serial/cp210x.ko: Invalid module format

so then I tried:

cd ~/linuxmnt/sun8i/linux-sun8i/drivers/usb/serial
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules

which rebuilt all the modules again…

but from the same folder

sudo insmod cp210x.ko

gave a different error:

insmod: ERROR: could not insert module cp210x.ko: Unknown symbol in module

but if I

sudo insmod usbserial.ko
sudo insmod /lib/modules/3.4.113-sun8i/kernel/drivers/usb/serial/cp210x.ko

then all is ok…  so now the module built correctly.  and after

sudo cp ~/linuxmnt/sun8i/linux-sun8i/drivers/usb/serial/cp210x.ko /lib/modules/$(uname -r)/kernel/drivers/usb/serial/

then from a new login, modprobe cp210x correctly installed the driver.

So now I’m ready to make the couple of simple modifications to the driver to enable 250kbaud as highlighted here.

The kernel I was updating (3.4.113) did not know about the USB params for the OWL device, so I had to add (using nano as the editor):

 { USB_DEVICE(0x0FDE, 0xCA05) }, /* OWL Wireless Electricity Monitor CM-160 */

then for the baud rate, we just say that if we’re asked for 0 baud, set it to 250kbaud (*** see below – baud == 0 did not work ***):

 if( baud == 0) {
 baud = 250000; /* KLUDGE for Owl Energy Monitor, Use baud rate $
 }
 else if (baud <= 300) baud = 300;

then repeating the make (still within the usb/serial folder):

make -C /lib/modules/$(uname -r)/build M=$(pwd) modules

only makes the cp210x.ko module…  copy it to the modules folder:

sudo cp ~/linuxmnt/sun8i/linux-sun8i/drivers/usb/serial/cp210x.ko /lib/modules/$(uname -r)/kernel/drivers/usb/serial/

and test again with modprobe from a separate command prompt:

pi@orangepizero2:~$ sudo modprobe cp210x
pi@orangepizero2:~$ lsmod
Module Size Used by
cp210x 11647 0
usbserial 29089 1 cp210x
pcf8591 3363 0
bmp085 3487 0
xradio_wlan 210530 0
g_serial 27617 2
mac80211 358445 1 xradio_wlan
pi@orangepizero2:~$

hopefully done!..  All that remains is to test with the actual USB device, and check that the TTY it creates is not hooked by a terminal.

So..  hooked up the USB and checked in dmesg; it is found and became TTYUSB0.

cu -l /dev/ttyUSB0 -s 0

shows connected, and receives some garbage. (note: ~.<return> will disconnect).  promising, but can I make it work from node-red….

Node-red does not come installed (from Peter’s script) with node-red-node-serialport.  Installing from the node-red interface failed for me, but then ‘npm i node-red-node-serialport’ also failed.  Until I re-selected gcc-5 with

sudo update-alternatives --config gcc

After that

npm i node-red-node-serialport

worked.  But node-red must be restarted.  running

ps -aux | grep node

will reveal the node-red pid, which can then be killed (it will auto-restart; only did not in my case.  Did a ‘sudo shutdown -r now’ to see if it starts on boot…).  A restart did restart node-red; but the usb device did not auto-load CP210x.ko; after a ‘modprobe cp210x’, then ttyUSB0 appeared, and node-red can contact the port.

Seems a ‘sudo depmod -a’ may solve this.

Turns out that using baud == 0 did not work; but re-doing the above with baud == 300 and selecting 300 in node-red then worked correctly.

 

 

 

 

 

 

Â