Picoos-net library has been lacking support for Wifi networks and I have been trying to figure out how to implement it.
I would like to run the network stack on the microcontroller along with my application so I could keep on using APIs I’m familiar with. This rules out modules that have built-in tcp/ip networking. However, writing whole Wifi stack would be a huge task and resulting library might be too big for some microcontrollers I have been using. So this rules out cheap usb-stick style approach.
Broadcom seems to have nice modules also (Wiced) and they seem to provide some kind of SDK, which might make integration of modules easier.
But there is one interesting module made by Hi-Link called HLK-RM04. It’s standard firmware implements a Wifi-rs232 module with AT-command set. But what makes it interesting is the fact that it is supported by OpenWrt firmware also. Flashing the module with OpenWrt allows running any standard Linux stuff on it (well, module has only 16 Mb of memory so small things only).
These modules are also very cheap, one can get them for less than 10€ on ebay or dx.com.
So I started thinking – what if I connect this module with USART or SPI into microcontroller and use that connection to pass raw ethernet frames ? Like I have been using ENC28J60 chip. RM04 module would only take care about Wifi connection setup but rest of network stack would run on microcontroller.
I first implemented a simple linux utility which opens serial port and TAP device and passes packets between TAP and serial port. Ethernet packets needed some extra framing when going over serial line which I solved by using hdlc-like framing used by PPP. Extra framing ensures that packets are not damaged in any way by using CRC checksum. I called this utility “hdlcbridge“.
To get this running on OpenWrt I compiled a minimal distribution using hlk-rm04-defconfig.
Get OpenWRT Barrier Breaker sources on your Linux box ./scripts/feeds update -a< ./scripts/feeds install -a< cp hlk-rm04-defconfig .config make defconfig make
When this ends there is installable (but minimal) OpenWrt version in bin/ramips -directory (detailed build instructions are at OpenWrt web site). After this OpenWrt binary for hdlcbridge can be compiled by using provided openwrt-make.sh script.
Before starting hdlcbridge, OpenWrt wireless network must be configured. I have following configs: /etc/config/network
config interface 'lan' option ifname 'eth0.1 tap0' option force_link '1' option type 'bridge' option proto 'static' option ipaddr 'n.n.n.205' option netmask '255.255.255.0' option gateway 'n.n.n.1' option dns 'n.n.n.10'
config wifi-iface option device radio0 option network lan option mode sta option ssid mynetwork option encryption psk2 option key 'my password' option wds 1
With these configs tap0 is bridged into same network with LAN and Wifi interface (IP address for OpenWrt wouldn’t be strictly necessary but is easier to test and debug when there is network access to OpenWrt module too). Now it is possible to start hdlcbridge:
/root/hdlcbridge --serial /dev/ttyS0 --debug
Hdlcbridge is now runnig and passing packets between ttyS0 and tap0. It also print packet sizes (because –debug was used).
Second step was adding a hdlcbridge driver to picoos-net library. I had an extra Olimex STM32-P152 lying around and as it has an extra serial port available I decided to use it. I wired USART3 RX & TX lines into HLK-RM04’s TX2 & RX2 lines (TX2/RX2 is connected to /dev/ttyS0 in OpenWrt linux). Current picoos-net driver is a over-simplified one, using unoptimized buffering and polling during packet sending as I didn’t want to put too much effort in writing it before I know that this would work.
Indeed it works ! I was able to ping STM32-P152 board from my network. Latency was not optimal, about 20 ms but everything works including TCP connections. I think that latency should actually be better since the USART is running and 115200 baud, there might something left to optimize in hdlcbridge.
It is a little bit odd to use “big” linux as slave for “small” Pico]OS, though.