Compile C programs on the Router directly

NB! the an ADM5120 processor in the router is much slower than those in modern PCs - even if you run Linux in VMWare !
- but it is fast enough to let you develop small control programs. A compile will take about 20 seconds for a page of code.
If you are not into C coding consider Blassic Basic (menu item 12), no need to compile at all and it can drive all the router ports
- but don't tell your Linux friends ;-)

(with thanks to digital_me and others at http://midge.vlad.org.ua/forum/viewforum.php?f=1)

Objective
Compile small C programs by opening a Telnet session to the router and using gcc
- Thus no need to use a Linux PC.

The C coding development environment or "tool-chain" then resides on the USB memory stick that the router boots from.

Loading the tool-chain
The uClibc project creates a small C compiler that is small enough to reside within an embedded Linux system such as the Sweex/Edimax routers.
A ready made tool chain should be downloaded from http://www.uclibc.org/downloads/root_fs_mipsel.ext2.bz2 using a Linux PC.

Create a temporary folder and place root_fs_mipsel.ext2.bz2 in it

In a terminal window cd to the folder and first unpack the file
bunzip2 root_fs_mipsel.bz2 . . . . . . . . (or use the GUI un-archive program - right click on the file and "extract here")
mkdir root_fs
mount -o loop root_fs_mipsel root_fs

Insert the USB stick into the Linux PC and copy the whole of the new folder root_fs and it's contents to a new folder called root_fs in the root of the USB stick file system.

Enter the router via a Telnet terminal on your PC or Mac

chroot tells the router to move from the normal Kamikaze files and use the file system within root_fs

chroot root_fs

I create a C program in BBedit (a powerful Mac text editor) and save to the router via BBedit's built in FTP program.
I also have a Telnet session running in another window and in there I tell the router to compile the code.

I would save the following to /root_fs/test/hello_uclibc.c

/*
*example hello world program
*/
#include <stdio.h>
int main(int argc, char** argv)
{
/* comment */
printf("Graham says hello world on uClibc\n");
return 0;
}
/*graham_hello.c */

first do

chroot /root_fs

Here is the Telnet session within the uClibc environment -

midge# gcc -o hello_uclibc hello_uclibc.c
midge# ls
hello_uclibc hello_uclibc.c
midge# ./hello_uclibc
Graham says hello world on uClibc
midge#

Hit Control-D to get back to the midge root system.

 

I also compiled a program to drive the i2c bus and read the digital thermometer (see the i2c notes - menu item 6)
For this to run in the uClibc environment you need to copy the contents of the Kamikaze /dev folder into the uClib /dev folder.

However it is simpler to hit Control-D to return to Kamikaze and then just cd /root_fs/test and run the newly compiled program.

The incudes in the i2c code are

#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <linux/i2c.h>

and I was surprised that it ran.

That is all I have tested so far.

I tried to combine the two file structures into a single one by copying all the uClibc files into Kamikaze but it failed to run.
Please email me if you get that to work.