Bare Metal programming A13

Revision as of 06:53, 18 May 2013 by Scott (talk | contribs) (Building quickly and adding new files)

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 u-boot-sunxi/

With the following command you can start the uboot build:

# make a13_olinuxino CROSS_COMPILE=arm-linux-gnueabi-

If errors come up that suggest its unable to find arm-4.6-linux-gnueabi-gcc its likely the compilation is expecting the files in a different place or to be named differently to how they were installed.
To resolve this you can use symbolic links example

ln -s /usr/bin/arm-linux-gnueabi-gcc-4.6 /usr/bin/arm-4.6-linux-gnueabi-gcc

Once you have compiled you can see A13-OLinuXino already has 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 in directory the git repository was downloaded to and in the following path

u-boot-sunxi/examples/standalone
The file is named: 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
u-boot-sunxi/include/configs/sunxi-common.h

Change the line 129 from

  1. define CONFIG-STANDALONE_LOAD_ADDR 0x50000000

to

  1. define CONFIG-STANDALONE_LOAD_ADDR 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.

Setup Eclipse tool chain

Eclipse is an IDE (Integrated Development Environment). Its a program that allows for one click building as well as making it easier to navigate code across files.

To install Eclipse follow the instructions here

Install eclipse

Eclipse is a GUI which enables easy compilation and debugging when the appropriate tools have been installed.

This may be able to be installed using the package manager on your linux system.

Manual eclipes install
To manually install the most up to date eclipse perform the following

Go to the eclipse download section

Eclipse download Download to your linux machine the suitable eclipse version.

Decompress the downloaded file into any directory you like by moving the downloaded file to your desired directory. Open a terminal, go to the directory and decompress with the command

tar -xzvf FILENAME.

This will create a directory in this location called eclipse Within this directory is the executable eclipse.exe

It is recommended to create a launcher on your desktop which points to this executable file to make it easier to start.

Manual Java runtime install
As eclipse is written in java the jave runtime is required as well.

Download the java runtime.

Java download page Download the java RPM file that your eclipse version requires.

Follow the instructions on the java download page beside the download to install

Your eclipse installation should now start up.

Setup Eclipse

To setup eclipse to create the hello world program complete the following steps

- Start eclipse

- Create a new project by clicking File -> New -> Makefile project with existing code
- Enter a project name
- locate the folder with the source code previously downloaded - the folder should be named olinuxino/u-boot-sunxi
- Select Cross gcc
- Press OK, it may take some time to index

Eclipse Build Setup

To setup the build command
- wait for the project to stop indexing
- when ready press project then Properties this will bring up a window
- Within the properties window click on C/C++ Build
- Beside build command enter in the box
make a13_olinuxino CROSS_COMPILE=arm-linux-gnueabi-

Once you click Ok the system will be ready to build when build is pressed.

Licensing

Use of any of the existing code within u boot is ok however this code is licensed under GPL and therefore if any of it is included in code created, the code created must also be licensed under GPL.

Creating a stand alone program that uses u boot is ok and doesn't require licensing under GPL however including any source files within like GPIO will mean the code created must be licensed under GPL.

There is plenty of info on GPL online for further info.

Do something useful, Modify the hello world to set GPIO

All devices within the A13 processor are memory mapped therefore changing a memory location with data will take affect on the associated hardware.

The hello world program can be modified as follows to turn on the LED on the olinuxino A13 board.

Add in the following definitions under the includes
Note: all of the information in these defines are contained within the A10 detailed manual

#define GPIOBase 0x01C20800
#define GPIOIncrement 0x24

#define ALLWINNER_GPIO_G    GPIOBase + (6*GPIOIncrement)

#define Port_CFG1 0x04 //pin direction and function 8 to 15
#define Port_DAT  0x10

Add in the following code at the top of the main file

uint32_t  	*portGConfig;
uint32_t  	*portGData;

//setup pointers for GPIO registers
portGConfig = (uint32_t *)(ALLWINNER_GPIO_G + Port_CFG1);
portGData = (uint32_t *)(ALLWINNER_GPIO_G + Port_DAT);

//configure port G pin 9 to output
*portGConfig |= 0x10;
//set output to on
*portGData |= 0x0200;

Compile the new program, run it and the GPIO should turn on.

Building quickly and adding new files

Building the stand alone program at the moment requires all of uboot to be compiled, this can take several minutes depending on the machine used.

It is also difficult to add extra files which makes it hard for more complex projects.

To speed this up and make it easier to add in new files for compilation. The text in the following wiki page can be used to create a make file

Stand_Alone_MakeFile

Create a file in the standalone directory called "myMakefile", add in the text in the above wiki page.

In a terminal go to the standalone directory and type make -f myMakefile

This should compile the hello_world program. It will also generate some assembly files which can be useful to use when optimizing your code.


Adding new header files can be done without any change as long as they are referenced in the hello_world file.

Adding new c files is difficult with the original make files. With the new make file just add the new file name in the "SOURCES" line after hello_world.c
For example if you add in a gpio.c file the first line should become
SOURCES=hello_world.c gpio.c

you can now quickly compile and add in new files