Difference between revisions of "Bare Metal programming A13"

Line 114: Line 114:
 
<nowiki># mkfs.vfat /dev/sdX1</nowiki>
 
<nowiki># mkfs.vfat /dev/sdX1</nowiki>
  
===Running hello world===
+
===Copying u boot to SD card===
The compilation performed in the previous step compiles uboot and it compiles a few stand alone programs as uboot calls them. One stand alone program is the hello world program
+
Now the SD card is formatted and uboot has been compiled we can copy the files to the SD card.
  
The hello world program code will be located in the directory previously created and in the following sub directories /examples/standalone/hello_world.c
+
write Uboot
 +
 
 +
<nowiki>
 +
# cd uboot-allwinner/
 +
</nowiki>
 +
 
 +
<nowiki>
 +
# dd if=spl/sunxi-spl.bin of=/dev/sdX bs=1024 seek=8
 +
</nowiki>
 +
 
 +
<nowiki>
 +
# dd if=u-boot.bin of=/dev/sdX bs=1024 seek=32
 +
</nowiki>
 +
 
 +
<nowiki>
 +
# sync
 +
</nowiki>
 +
 
 +
<nowiki>
 +
# umount /mnt
 +
</nowiki>
 +
 
 +
===The hello world program compilation===
 +
The compilation performed in a previous step compiled uboot but it also compiled a few stand alone programs which is the name u boot uses for programs that can be executed by uboot. One stand alone program is the hello world program
 +
 
 +
The hello world program code will be located in the directory previously created and in the following sub directories olinuxino/examples/standalone/hello_world.c
 +
 
 +
The compiled hello world program will be located at
 +
???????? hello_world.bin
 +
 
 +
copy this to a computer with a serial connection to the A13 olinuxino
 +
 
 +
===Using uboot on the A13olinuxino===
 +
Take the created SD card and with the power disconnected from the olinuxino board plug the SD card into the board.
 +
 
 +
Connect the computer to the A13 olinuxino serial port with an appropriate level translator (the olinuxino serial levels are 0-3v3 they must be increased to RS232 levels)
 +
 
 +
Setup your computer to look at the serial port. If a windows XP computer is used do this by starting hyperterminal.<br>
 +
The serial settings are 115200 baud, 8 bits, 1 stop
 +
 
 +
Turn the power on.
 +
 
 +
Immediately data will be sent out the serial port, This is uboot starting up, after a few seconds data will stop sending and u boot is ready to use.
 +
 
 +
typing in the terminal "help" will list the available commands
 +
 
 +
The use of U boot is explained in [http://www.denx.de/wiki/DULG/Manual U boot manual] look for the section "u-boot command line interface"
 +
 
 +
type "loadb" <br>
 +
at this point uboot is waiting for the computer to send the binary file to it, it will not respond to commands until the binary is sent.<br>
 +
To do this using hyperterminal click on tools -> send file -> locate the hello_world.bin and click send. After some time this will indicate its sent
 +
 
 +
If no errors came up the terminal will report the file was loaded to 0x50000000 which is a location in RAM so now the file is in the ram of the A13olinuxino ready to be executed.
 +
 
 +
To execute type "go 0x50000000"
 +
 
 +
If the terminal writes a few lines and says "Hit any key to exit ... ". You have executed your first program without linux on the A13olinuxino.
 +
 
 +
===Run the hello world program from the SD card===
 +
Nobody wants to run their code with a serial console forever here is how to load it from the SD card.
 +
 
 +
At the moment a bug exists in the uboot code for the sunxi branch, this bug happens to overwrite sections of memory at 0x50000000 when the SD card is accessed. This wouldn't normally be a problem however the code is compiled to only operate from 0x50000000 the exact same location in RAM. <br>
 +
This issue was reported to Henrik Nordström one of the developers for the sunxi branch of uboot, He quickly responded noting it was a bug and promptly fixed it in the development branch.<br>
 +
A work around until this change is committed to the main branch is to change the make file setting of where the program is compiled to execute from.
 +
In the file ????
 +
Change the line ??? to 0x48000000
 +
 
 +
0x48000000 is the area that was going to hold the kernel so we know its free.
 +
 
 +
To recompile go to the directory
 +
 
 +
<nowiki># cd uboot-allwinner/</nowiki>
 +
 
 +
With the following command you can start the uboot build:
 +
 
 +
<nowiki># make a13_olinuxino CROSS_COMPILE=arm-linux-gnueabi-</nowiki>
 +
 
 +
Now the hello_world.bin can be copied to the SD card
 +
 
 +
Move the SD card to the olinuxino board, power it on with the serial connection connected.
 +
 
 +
If you type "help" you will notice the command fatls
 +
 
 +
We are using a fat file system and fat ls will list the files available. To do this type the following in the terminal
 +
 
 +
fatls mmc 0 0x48000000 hello_world.bin
 +
 
 +
it will say something like "read 566 bytes" number of bytes may be different. If it says this the load was successful and the binary file is in ram at 0x48000000 as specified
 +
 
 +
To execute again type
 +
 
 +
go 0x48000000
 +
 
 +
You may be thinking im still using the computer to execute this so what is the advantage. All of the commands sent can be scripted so that the stand alone program will load from the SD card and execute once the power is turned on.
 +
 
 +
===Do something useful, Modify the hello world to set GPIO===
 +
COMING SOON

Revision as of 06:38, 30 April 2013

Programming with no OS, development tools and poor documentation isn't easy therefore this page is currently a work in progress. It will be updated as the author works out more about the process

Stand alone program running with uboot

The linux operating systems shown in the wiki runs on an SD card however it is booted using u-boot. U-boot sets up the CPU, memory and a few other things then starts Linux executing.
The following method outlines how to create a program that replaces linux but still relies on u-boot for setup.

Tools

- Computer for cross compiling the program and uboot, such as a linux computer
- SD card and SD card reader / writer
- A13 olinuxino board
- Serial adapter for the A13 olinuxino board (Only way to work with uboot as no screen is configured in uboot)

Setup computer for cross compiling and compiling uboot

You should make sure you have the tools for building the Linux Kernel and install them if you don’t have them. To install new software you should be with super user rights so do this type in a terminal.

$ sudo su

you will be asked for your password and then your prompt will change to # which means you are now the super user, all future commands should be run in this mode

First update apt-get links by typing

#apt-get update

Install the toolchain by typing the following.

# apt-get install gcc-4.6-arm-linux-gnueabi ncurses-dev uboot-mkimage build-essential git

with this line you make sure you have all tools necessary for the A13 kernel and uboot build:

  • GCC compiler used to compile the kernal
  • The kernel config menu
  • uboot make image which is required to allow the SD card to book into the linux image
  • Git which allows you to download from the github which holds source code for some of the system
  • Some other tools for building the kernel


after the installation you now have all tools to make your very own A13 kernel image

Building Uboot

The Allwinner community uboot is maintained by Henrik Nordström aka hno on #freenode irc channel.

First let’s make the directory where we will build the A13-OLinuXino Linux:

# mkdir olinuxino # cd olinuxino

Then let’s download the uboot sources from GitHub repository, note there are lot of branches but you have to use sunxi branch, the files are about 70 MB

# git clone -b sunxi https://github.com/linux-sunxi/u-boot-sunxi.git

After the download you should have a new directory

# cd uboot-allwinner/

With the following command you can start the uboot build:

# make a13_olinuxino CROSS_COMPILE=arm-linux-gnueabi-

As you can see A13-OLinuXino already have support configuration in Allwinner community uboot

At the end of the process you can check if everything is OK by

# ls u-boot.bin spl/sunxi-spl.bin

If you got these two files everything is complete, well done so far

Format and setup the SD-card

First we have to make the correct card partitions, this is done with fdisk.

Plug SD card into your SD card reader and enter in the terminal

# ls /dev/sd

Then press two times <TAB> you will see a list of your sd devices like sda sdb sdc note that some of these devices may be your hard disk so make sure you know which one is your sd card before you proceed as you can damage your HDD if you choose the wrong sd-device. You can do this by unplugging your sd card reader and identify which "sd" devices remove from the list.

Once you know which device is your sdcard like sda use this text instead of the sdX name in the references below:

# fdisk -u=sectors /dev/sdX

then do these steps:

1. p

will list your partitions

if there are already partitions on your card do:

2. d 1

to delete them all

3. n p 1

create the first partition, starting from 2048

4. beginning 2048

then list the created partitions:

6. p

then write the partitions to the card

7. w

now we have to format the file system on the card:

the first partition should be vfat as this is FS which U-boot understands

# mkfs.vfat /dev/sdX1

Copying u boot to SD card

Now the SD card is formatted and uboot has been compiled we can copy the files to the SD card.

write Uboot

# cd uboot-allwinner/

# dd if=spl/sunxi-spl.bin of=/dev/sdX bs=1024 seek=8

# dd if=u-boot.bin of=/dev/sdX bs=1024 seek=32

# sync

# umount /mnt

The hello world program compilation

The compilation performed in a previous step compiled uboot but it also compiled a few stand alone programs which is the name u boot uses for programs that can be executed by uboot. One stand alone program is the hello world program

The hello world program code will be located in the directory previously created and in the following sub directories olinuxino/examples/standalone/hello_world.c

The compiled hello world program will be located at ???????? hello_world.bin

copy this to a computer with a serial connection to the A13 olinuxino

Using uboot on the A13olinuxino

Take the created SD card and with the power disconnected from the olinuxino board plug the SD card into the board.

Connect the computer to the A13 olinuxino serial port with an appropriate level translator (the olinuxino serial levels are 0-3v3 they must be increased to RS232 levels)

Setup your computer to look at the serial port. If a windows XP computer is used do this by starting hyperterminal.
The serial settings are 115200 baud, 8 bits, 1 stop

Turn the power on.

Immediately data will be sent out the serial port, This is uboot starting up, after a few seconds data will stop sending and u boot is ready to use.

typing in the terminal "help" will list the available commands

The use of U boot is explained in U boot manual look for the section "u-boot command line interface"

type "loadb"
at this point uboot is waiting for the computer to send the binary file to it, it will not respond to commands until the binary is sent.
To do this using hyperterminal click on tools -> send file -> locate the hello_world.bin and click send. After some time this will indicate its sent

If no errors came up the terminal will report the file was loaded to 0x50000000 which is a location in RAM so now the file is in the ram of the A13olinuxino ready to be executed.

To execute type "go 0x50000000"

If the terminal writes a few lines and says "Hit any key to exit ... ". You have executed your first program without linux on the A13olinuxino.

Run the hello world program from the SD card

Nobody wants to run their code with a serial console forever here is how to load it from the SD card.

At the moment a bug exists in the uboot code for the sunxi branch, this bug happens to overwrite sections of memory at 0x50000000 when the SD card is accessed. This wouldn't normally be a problem however the code is compiled to only operate from 0x50000000 the exact same location in RAM.
This issue was reported to Henrik Nordström one of the developers for the sunxi branch of uboot, He quickly responded noting it was a bug and promptly fixed it in the development branch.
A work around until this change is committed to the main branch is to change the make file setting of where the program is compiled to execute from. In the file ???? Change the line ??? to 0x48000000

0x48000000 is the area that was going to hold the kernel so we know its free.

To recompile go to the directory

# cd uboot-allwinner/

With the following command you can start the uboot build:

# make a13_olinuxino CROSS_COMPILE=arm-linux-gnueabi-

Now the hello_world.bin can be copied to the SD card

Move the SD card to the olinuxino board, power it on with the serial connection connected.

If you type "help" you will notice the command fatls

We are using a fat file system and fat ls will list the files available. To do this type the following in the terminal

fatls mmc 0 0x48000000 hello_world.bin

it will say something like "read 566 bytes" number of bytes may be different. If it says this the load was successful and the binary file is in ram at 0x48000000 as specified

To execute again type

go 0x48000000

You may be thinking im still using the computer to execute this so what is the advantage. All of the commands sent can be scripted so that the stand alone program will load from the SD card and execute once the power is turned on.

Do something useful, Modify the hello world to set GPIO

COMING SOON