Step 5 Build_Openwrt

The ability to compile a completely new custom Linux kernel makes it easy to create devices based on the HLK-RM04 module. There are many software packages that are available as part of OpenWRT. Once you can compile your own kernel you have the option to add your own custom code to the build. Building a kernel is straight forward but for the first time user the configuration options are overwhelming. First we need to configure the kernel so that it recognizes the amount of SDRAM memory we have installed on your module. Next we configure the Linux image so that it builds code for the RT5350 system on a chip.

There are two memory limitations that you should be aware of when building a kernel. First the compressed Linux image needs to fit onto the SPI FLASH which can be either 4M or 8M depending on which module you purchased. Note that 192K is reserved for the Uboot loader while two 64K blocks are reserved for parameters like the MAC address, so the image needs to fit in 4M-320K or 8M-320K depending on the module. This limitation is most important for the 4M configuration because it limits how many applications you can install at boot time. I have actually cut out a few less useful options to give me more space. The second memory limitation is the size of the SDRAM. With only 16M you are quite limited in what you can do so I would recommend spending a couple of dollars more to get the 32M module.

Once we finish configuring the packages to be built we still have a few configuration options to deal with. The default state of the Linux OS running on the module needs to be defined. There are configuration files that show up in the Linux build but their default values are not very useful. To solve this I add a directory named “files” to the openWRT root directory. The content of the “files” directory is copied to the kernel image so it shows up on our freshly built module. This feature allows me to define the wired and wireless network configuration as well as setup a web interface. You can always manually configure a module using the VI editor but over time having this as part of the build will be easier. I usually manually create a working configuration and then I copy the config files back to the “files” FILES.ZIP directory so they get re-built any time I re-compile the kernel.

Installing Openwrt source code

To get started we need to install the openWRT source files on our Ubuntu host computer. The source code directory is created using the following commands.
cd ~/Desktop
git clone git://git.openwrt.org/openwrt.git
cd ~/Desktop/openwrt
./scripts/feeds update -a
./scripts/feeds install ser2net
./scripts/feeds install avrdude
make menuconfig
make -j7

When you run make menuconfig and make you will create a cross compiler and default linux image. The first build is very slow since the entire compiler tool chain must be built. This can take several hours so be patient. Subsequent builds are faster as only new packages need to be compiled. Next we will tweak the source code to customize the openwrt build for our own needs. You can start by copying the “files” directory to the openwrt directory (~/Desktop/openwrt).

You will need to edit the wireless and network config files to configure the modules IP address for your home network. I described this process in “Step 2 Install OpenWRT” blog entry.

After compiling the default image for the first time, I alter a few configurations to produce a useful base image. I need to add these changes after the first build so that the options show up in menuconfig. Copy the files from DNCLABS.ZIP to the directories as shown. This will customize your openWRT source tree for the HLKRM04. DNCLABS.ZIP

These changes configure the hardware specifically for this project. I have also tweaked the device tree structure( DTS file) to match the physical hardware of the HLK-RM04 module. This initial setup has only created the source files we need to build our image. From here we need to select the kernel options and linux options that we want to build. In the next sections we will configure the build.

Kernel Memory Configuration

Based on the HLK-RM04 module you purchased the kernel needs to be configured differently. The HLK module is equipped with either 16 or 32M of SDRAM and uses hardware jumpers to detect the memory size. The Hi-Link module jumpers are always set to read 32M even when only 16M is installed so we need to tell the kernel the memory size. This can be done using the kernel configuration console.make kernel_menuconfig

For a 16M SDRAM, 4M of SPI flash modulerootfstype=squashfs,jffs2 mem=16M

For a 32M SDRAM, 8M of SPI flash modulerootfstype=squashfs,jffs2 mem=32M

Openwrt configuration

To configure openWRT features you can use a graphical tool called “menuconfig” that allows you to include or exclude features from your target image. Open a terminal window in the openWRT install directory . The features I have included and excluded are as follows.

Save and exit the menuconfig utility. Then build your image with the following command.make -j7

The -j7 needs to match the number of processors you have on your Ubuntu machine. The number is set to the number of CPUs +1 (in my case a 6 core machine plus one).

Once the openWRT image has been built you can go back to my blog entry step 2 and install your own freshly built image on the module.

OpenWRT Overview

Initially I found it difficult to find files and directories that I needed to change while configuring my Linux builds. To help others I have provided a brief description of the file hierarchy. With so many directories and files it was like looking for a needle in a hay stack to find where changes needed to be made. In the end it turns out I only really needed to worry about three directories and a few files.

Image generation

The “bin/ramips” directory contains the linux image after the make command completes. The DNCLABS-sysupgrade.bin file is the new image that we will TFTP transfer over to the module. Notice that the linux image is in the file named openwrt-ramips-rt305x-dnclabs-squashfs-sysupgrade.bin which I truncate to dnclabs-sysupgrade.bin before downloading it to the module.
openwrt-ramips-rt305x-dnclabs-squashfs-sysupgrade.bin

bin – where the final Linux image resides after a successful build.ramips – is the RALINK MIPS architecture that the HLK RM04 uses

Files

The files directory is added to the openwrt source tree to provide feature customizations that are built into the image file. The “files” directory contains a set of files that will get copied to the final compressed Linux image. I have setup /etc/config/network and /etc/config/wireless files to pre-configure the module so that it connects to my network. I also added a web interface and WS2812B driver and code here.

/files/etc – contains the default configuration for your applications as well as providing a splash banner file when you boot. The avrdude.conf file is an abbreviated config file that allows you to load code into an Arduino UNO over the uartf serial port./files/etc/config – each of the network peripherals needs to have its interface defined. These files allow you to setup your IP address, SSID and the type of access (client or host)./files/usr – this directory contains any code you want to install on the module. This is typically you own code written in ‘C’./files/www – The web server looks in the www directory to find its HTML source code so this is an ideal place to create a custom web interface.

The “temp.txt” file is generated the first time you run “make” If you want to change the option presented in “menuconfig” this file can be removed so that this file is re-built with any changes you have made. The command line instruction is:rm -rf temp.txt

The “build_dir” is the heart of the compilation. The files in this directory form the final Linux image when compiled.

Tools

OpenWRT builds a cross compiler for the target architecture you specified when you ran the menuconfig command. The following folders are associated with the cross compiler. You shouldn’t need to edit or modify these files.

tools -contains all the build instructions for building the cross compiler.toolchain -contains the kernel headers, the C library, the bin-utilsinclude – header filesscripts – perl scripts that manage the openWRT packagesstaging_dir – where the cross-compilation tools are installed

Feeds

OpenWRT builds itself by downloading its source code from a code repository maintained by the openWRT community. The feeds.conf.default file in the root directory defines the location of these web repositories.

feeds – internet files that can be downloaded to add features to Openwrt.dl – user packages are downloaded herepackage – the openWRT makefiles and patches for all of the main packages. OpenWRT uses a custom “make” syntax. The makefile defines meta information for each packages ie)where to download the package, how to compile, where to installed the compiled binaries.

Device Tree Structure

The device tree structure defines how the physical hardware will be allocated. The HLK RM04 module has a limited number of I/O pins that are brought out to connector pins. The device tree structure allows us to define how these pins are to be accessed. The location of the DTS file is buried deep in the target directory as shown.