Documentation:Tutorials Hello world on NGW100 built on Ubuntu
From AVRFreaks Wiki
This is a short tutorial about how to get a Hello world into the NGW100 that is built on a Ubuntu 8.04.
Contents |
[edit] Hello world!
If you have missed out on what Hello world is you can check it out on wikipedia, and we are going to use the C version off Hello world.
The first step is to put this into a file called hello.c (vim is a good editor to use).
#include <stdio.h>
int main(int argc, char** argv)
{
printf("Hello World!\n");
return 0;
}
And then do a normal compile, so you don't have any problems with your code at this point.
gcc hello.c -o hello_native
And run it.
./hello_native
And you would get this
Hello World!
If this don't work please make sure that you have build-essential installed, if not then install them with.
sudo apt-get install build-essential
That was the local hello world, nothing more to that...
[edit] Hello world on NGW100
To do this you need to have the avr32-toolchain installed, just follow the instructions on this page.
And that page basically tells us to add the atmel repository and install avr32-gnu-toolchain, and I must say that I really love the fact that they put the effort into creating a repository since that makes life so much easier.
Then we nearly follow the instructions on this page
And the short version off that page is
avr32-linux-gcc -pipe -O2 -g -Wall -D_GNU_SOURCE -c -o hello.o hello.c avr32-linux-gcc -pipe -O2 -g -Wall -o hello hello.o
Please note that the above is nearly the same as avr32-linux-gcc -o hello hello.c, but with some small extra things like -Wall that you always should use since it warns you about simple mistakes.
And the executable from this just called hello will not run on your local computer, feel free to try thou. Ubuntu will just tell you something like this.
bash: ./hello: cannot execute binary file
And that is basicly becurse you just created a AVR32 compatible executable (and not a x86 compatible.)
Now it is time to get that executable to the board.
[edit] Get the board up and running
This part is more or less inspired from this page
So now the fun begins and the easiest way that I have found to talk to this card is over a old school rs232 cable, I bet you can find a old modem-cable deep down in your closet. And as a terminal program you can use GtkTerm, you can get that with apt-get as usual. Don't forget to set the speed to 115200 bps.
Now this is basically the next steps
- Connect the rs232 cable and start GtkTerm
- Connect a normal Ethernet cable from your local network to the WAN-port.
- Insert the "power cord"
If everything is ok you will get a lot off prints in your GtkTerm, and it will look something like this to start with
U-Boot 1.1.4-at0 (Jan 3 2007 - 10:30:09) U-Boot code: 00000000 -> 000144f7 data: 24000000 -> 24002d80 SDRAM: 32 MB at address 0x10000000 Testing SDRAM...OK malloc: Using memory from 0x11fc0000 to 0x12000000 Flash: 8 MB at address 0x00000000 DRAM Configuration: Bank #0: 10000000 32 MB In: serial Out: serial Err: serial Net: macb0, macb1 Press SPACE to abort autoboot in 1 seconds ### JFFS2 loading 'uImage' to 0x10200000 Scanning JFFS2 FS: ....... done.
And this will continue for a little while and eventually you will get a prompt
* get board type for GPIO ... 'NGW' * setup GPIO boot LED ... [ OK ] * setup GPIO LED A ... [ OK ] * setup GPIO LED B ... [ OK ] Network Gateway ready BusyBox v1.4.2 (2007-04-17 15:34:55 CEST) Built-in shell (ash) Enter 'help' for a list of built-in commands. ~ #
Now doesn't that make you happy? The card is up and running :-)
[edit] Get the binary into the NGW100
Now this is the tricky part, and there is a lot off different ways to do this. But I kind off like to use the Ethernet to do this since that is fast and flexible and kind off easy, and since I'm on a protected LAN I can ignore a lot off security things.
First out is to find out if your NGW100 is connected to your LAN and has a correct IP-number, so I use the ifconfig and look at the eth0 part (that is the WAN-port).
~ # ifconfig
eth0 Link encap:Ethernet HWaddr 00:04:25:1C:64:58
inet addr:192.168.0.101 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:56 errors:0 dropped:0 overruns:0 frame:0
TX packets:96 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6075 (5.9 KiB) TX bytes:11814 (11.5 KiB)
Interrupt:25 Base address:0x1800
And it seems like I have 192.168.0.101, so that looks good.
Now you can ftp the hello executable to the NGW100 with something like ncftp, but my personal choice would be to use ncftpput because that could easily be but into the Makefile later so that every build ends up with a deploy off the executable into the NGW100.
ncftpput -u root -p roota 192.168.0.101 . hello
[edit] And run it
Then you go back to the GtkTerm and just run the executable (if needed make it runnable first with chmod)
~ # ls bin etc lib mnt sys usr config hello linuxrc proc tmp var dev home media sbin uImage www ~ # chmod 777 hello ~ # ./hello Hello World! ~ #
Now wasn't that nice, we have a Hello world in the NGW100.
[edit] A Makefile to speed things up
But there is a way that could make this little process go a little bit faster, and that is the usage off make. This is actually quite important since you really want to keep the development cycle as fast as possible.
Think off it as the time from you want to compile until you get a "test result" back and you know if your code was good or bad...
Anyhow this is a embryo off a makefile, later you must enhance it to fit your real needs in the same pace as your program grows.
Save this in a file called Makefile
TARGET := avr32 #TARGET := x86 ifeq ($(TARGET), avr32) CROSS_COMPILE := avr32-linux- else CROSS_COMPILE := endif CC := $(CROSS_COMPILE)gcc CPPFLAGS := -D_GNU_SOURCE CFLAGS := -pipe -O2 -g -Wall IP := 192.168.0.101 USER := root PASS := roota hello: hello.o $(CC) $(CFLAGS) -lm -o $@ $^ .PHONY: clean clean: $(RM) *.o *~ hello deploy: hello ncftpput -u $(USER) -p $(PASS) $(IP) . hello
then you could type
make deploy
then your code builds and moves out into your NGW100.
Note: sometimes you want to run your code on your local computer, then change the TARGET line from avr32 to x86 and you will have a normal compile.
[edit] Perl telnet
And then... can't we speed things up a little more?
After some time you get kind off tired off typing those "chmod 777 hello" and "./hello" in that gtkterm window, and ask your self why.
Well it is hard to link in gtkterm into your Makefile, but can't we do something fun with perl and telnet? And since I ask the question off curse there is, how about to use Net::Telnet from CPAN.
First off install this package
apt-get install libnet-telnet-perl
Then create the script called perl_telnet.pl
#!/usr/bin/perl
use Net::Telnet;
$numArgs = $#ARGV + 1;
if($numArgs != 3){
die( "Usage ./perl_telnet.pl [ip] [user] [password]\n");
}
$ip = $ARGV[0];
$user = $ARGV[1];
$pass = $ARGV[2];
$telnet = new Net::Telnet (
Timeout=>10,
Errmode=>'die',
Prompt =>'/#\>/');
$telnet->open($ip);
#$telnet->login($user, $pass);
print $telnet->cmd('PS1=#\>');
print $telnet->cmd('chmod 777 hello');
print $telnet->cmd('./hello');
print "\n";
Then add a run section in the Makefile that depends on deploy and runs the perl script.
run: deploy
./perl_telnet.pl $(IP) $(USER) $(PASS)
Now type
make run
And look, first we compile and link then we deploy and finaly we run the program ...
All this from 1 command line, how about that :-)
