UClinux/Hacking an uClinux app

From Hamsterworks Wiki!

(Redirected from UClinux/Hacking a app)
Jump to: navigation, search

Here's an example of how to hack uClinux to include your own program in the build environment.


You should be adding a kernel driver, and creating a character mode device file that will allow coordinated access between many user programs. The kernel driver should also pick up the base address of the PIO component from the hardware definition file.

But with that said, here is a quick and dirty way to access the port, which can be achieved in an hour. The 'poke' program is also a good general testing tool while building your own designs.

Have a brief read of Chapter 10 of http://www.altera.com/literature/ug/ug_embedded_ip.pdf to get familiar with how the PIO module works. You will see that the output port is at BASE+0. We need to write to this port to turn a LED on.


"poke" - a tiny program to write a byte into memory

As my build of uClinux lacks a MMU there is no memory protection. Any application can write to any memory address - just like back in the MS-DOS days! This makes it quite easy to test the PIO port we just added.

Here is a small C program to write any address in memory:

 * poke.c - write a byte value into a memory address
 * A little test program for uClinux
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
  unsigned char *addr;
  unsigned char val;
  if(argc != 3)
    fprintf(stderr,"Usage: %s <addr> <val>\n",argv[0]);
  addr = (unsigned char *)atol(argv[1]);
  val = atoi(argv[2]);
  printf("Poking %p with 0x%08x\n",addr,val);
  *addr = val;
  return 0;

And we need a 'Makefile' too - I copied and amended one from another program:

[hamster@uclinux poke]$ cat Makefile 

EXEC = poke
OBJS = poke.o 


all: $(EXEC)

#uses non LGPL librarys
$(EXEC): $(OBJS)
	$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

	$(ROMFSINST) /bin/$(EXEC)

	-rm -f $(EXEC) *.elf *.gdb *.o


I've put them in ~/uClinux/distro/nios2-linux/uClinux-dist/user/poke

Adding it to the default build

In /home/hamster/uClinux/distro/nios2-linux/uClinux-dist/user/Makefile


dir_y += games

-include Makefile.local


dir_y += games
dir_y += poke

-include Makefile.local

Building the new zImage file

In In /home/hamster/uClinux/distro/nios2-linux/uClinux-dist type:


This should build a new 'zImage' file in the 'images' directory, and in the 'poke' directory the new binaries and a '.o' file should have been made.


Following the steps at the end of UClinux/Building for the Terasic DE0-nano on Fedora 14 to complete the following:

Once booted, look in /bin. You should have a 'poke' executable.

Next you need to convert the base address 0x04002000 into decimal - my trusty calculator gives 67117056

Now you can test it by running:

poke 67117056 255

All the LEDs should light.

Personal tools