Documentation:NGW/CygwinHelloWorld

From AVRFreaks Wiki

Jump to: navigation, search

Contents

[edit] Programming: C++ Hello World in Cygwin

This is a step by step of how to load and compile the hello world! code with tool chain under Cygwin on a windows PC.

Note that I can not spell and I do miss words when I type, so if you see an spelling problem or a missed word can you fix it. Thanks.

[edit] Set-up

1. First you need to download avr32-gnu-toolchain-1.3.2-0.exe from AVR32 Tools and software

2. Time to install tool chain. It will install Cygwin if you do not have it on your system.

3. Now you should have Cygwin Bash shell under Start->All Programs->Cygwin->Bash shell

4. Time to run it. You should see a DOS window with you user name, an "@" and the Computer Name. i.e. User1@HomePC ~

[edit] Playing with Cygwin

OK you are now running Cygwin which lets you do a lot of Linux commands, like pwd, ls and gcc.

If you type pwd you should see the following. note that this directory maps out to C:\Cygwin\home\User1 on you PC.

User1@HomePC ~
$ pwd
/home/User1


You can cd / and you will find the following. The avr32 files are in /usr/local/avr32 and /usr/local/acr32-linux

User1@HomePC ~
$ cd /

User1@HomePC /
$ ls
Cygwin.bat  Cygwin.ico  bin  cygdrive  etc  home  lib  proc  tmp  usr  var

[edit] NOTE: That the NGW use the avr32-linux lib and that the avr32/io.h file is NOT there!

I lost a day trying to get a AP7 Application to compile. I kept getting "Can not find avr32/io.h file". (This was under AVR32 Studio. I.e. trying to tell the AVR32 Studio where the file was). That is when I download tool chain and started using make file. And found that the avr32-linux does NOT have avr32/io.h and therefore the code will NOT compile.

This is NOT a problem! The IO is done with drivers and not HW code.

Atmel has two compilers avr32-gcc and avr32-linux-gcc the Linux IO does not work the same as what is show in the AP7 Applications. It would be nice if Atmel put some AP7 Applications with the linux IO drivers.

The avr32-gcc lets you build from the ground up. Where you use avr32-linux-gcc you get a Kernel, busybox and lets you use Linux code in you project. i.e. a lot of free ware.


[edit] Software Time

OK, if you have not compile on a linux box it may look hard, but it not. Most stuff on a Linux box is done with text files. And compile is the same. You need a code file i.e. hello.c and a Make file i.e. Makefile tell the gcc what to do.

File: hello.c

#include <stdio.h>
int main(int argc, char** argv)
{	
       printf("Hello World!\n");
	return 0;
}


File: Makefile

CROSS_COMPILE		:= avr32-linux-
CC			:= $(CROSS_COMPILE)gcc

CPPFLAGS		:= -D_GNU_SOURCE
CFLAGS			:= -pipe -O2 -g -Wall

hello: hello.o
	$(CC) $(CFLAGS) -o $@ $^

.PHONY: clean
clean:
	$(RM) *.o *~ hello

Note that the -static can be added to the CFLAGS, if need. But most of the std lib are in the Kernel and therefore do not need to be added into you code. This make the code a lot smaller. The hello code is only 5k with out -static and 15k with -static . I do not know why the AVR32 studio output is 214k for the same code and -static?


The Makefile above is basic, but works well. To compile the code just type make in lower case.


Running the code:

$ make
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

The output file will be hello there is also a hello.o file which is the output file from the compile and is use by the linker. Note the hello.o will NOT run it need to be linked.

$ ls -l
total 14
-rwxr-xr-x 1 User1 None  214 Jan 30 12:18 Makefile
-rwxr-xr-x 1 User1 None 4903 Jan 30 12:18 hello
-rwxr-xr-x 1 User1 None  108 Jan 30 12:16 hello.c
-rw-r--r-- 1 User1 None 2980 Jan 30 12:18 hello.o

[edit] Time To FTP

You now need to upload the code. I like FTP as I can do this from Cygwin window. The commands are in bold

Note that I added a user to the NGW100 call user1 with the adduser command before FTP. This way you do not end up putting file into the root directory it goes into /home/user1 directory. No need to do a cd, and if the program or you mess up and does a rm * only the files in the user1 directory will be removed, NOT the root img.


$ ftp 192.168.0.208
Connected to 192.168.0.208.
220-Setting memory limit to 1024+1024kbytes
220-Local time is now 18:38 and the load is 0.00.
220 You will be disconnected after 1800 seconds of inactivity.
User (192.168.0.208:(none)): user1
331 User user1 OK.  Password required.
Password:
230 OK.  Current directory is /home/user1
ftp> bin
200 TYPE is now 8-bit binary
ftp> put hello
200 PORT command successful
150 Connecting to 192.168.0.101:5003
226-File written successfully
226 1.2 Mbytes free disk space
ftp: 4903 bytes sent in 0.00Seconds 4903000.00Kbytes/sec.
ftp> ls
200 PORT command successful
150 Connecting to 192.168.0.101:5004
total 1
-rwxrwxrwx   1 501      501       213541 Jan 23 15:25 Foo2.elf
-rwxr-xr-x   1 501      501        39769 Jan 29 20:03 gpio-test
-rw-r--r--   1 501      501         4903 Jan 30 18:39 hello
226-Options: -l
226 3 matches total
ftp: 199 bytes received in 0.01Seconds 19.90Kbytes/sec.
ftp> bye
221-Goodbye.  You uploaded 5 and downloaded 0 kbytes.
221 CPU time spent on you: 0.132 seconds.


The following script can be used to FTP the file up

Note you need to install perl and Net::FTP for this to work. (I think the Net::FTP is now in the base perl install, as it need for ppm). You can download perl for a PC from www.activestate.com. You only need Standard Distribution (i.e. the free one). For more perl help goto www.perl.com.

#!/usr/bin/perl
# This file ftp a file to the AVR32 unit. It is simple, but it works well
#
# To run
#
# perl ftp.pl
#
# or
#
# perl ftp.pl File_Name
#
# or
#
# perl ftp.pl uimange.bin /home/user1 

$Site       = "192.168.0.120";
$UserName   = "user1";
$Password   = "passowrd";
$cd         = "/home/user1"; # or /
$UpLoadFile = "reader";

# checking for filename on command like
if ($ARGV[0]) {
	$UpLoadFile = $ARGV[0];
}

# checking for location
if ($ARGV[1]) {
	$cd = $ARGV[1];
}

use  Net::FTP;

print "\nFTP File: $UpLoadFile   To: ${Site}${cd} \n";
$ftp = Net::FTP->new($Site, Debug => 0)
       or die "Cannot connect to $Site: $@";

$ftp->login($UserName, $Password)
      or die "Cannot login ", $ftp->message;

$ftp->cwd($cd)
        or die "Cannot change working directory ", $ftp->message;

$ftp->binary;

$ftp->put($UpLoadFile)
	 or die "put failed ", $ftp->message;

print "Done\n";


Or you could download ncftpput that can be found in the ncftp client suite.

[edit] Run Time

You need to change the file to make it an executable and then run it. Note that the file should be green, if your telnet program support color displays.

Note that telnet does not ask for a user name it just put you in as root.


telnet 192.168.0.208 


BusyBox v1.4.2 (2007-04-17 15:34:55 CEST) Built-in shell (ash)
Enter 'help' for a list of built-in commands.

~ # cd /home/user1
/home/user1 # ls -l
-rwxrwxrwx    1 user1 user1   213541 Jan 23 15:25 Foo2.elf
-rwxr-xr-x    1 user1 user1    39769 Jan 29 20:03 gpio-test
-rw-r--r--    1 user1 user1     4903 Jan 30 18:39 hello
/home/user1 # chmod +x hello
/home/user1 # ls -l
-rwxrwxrwx    1 user1 user1   213541 Jan 23 15:25 Foo2.elf
-rwxr-xr-x    1 user1 user1    39769 Jan 29 20:03 gpio-test
-rwxr-xr-x    1 user1 user1     4903 Jan 30 18:39 hello
/home/user1 # ./hello
Hello World!
Personal tools