How to make bootable SD card with Debian Linux for A20-OLinuXino-MICRO based on Kernel 3.4

debian_logo

Linux-Sunxi is changing the 3.4 Kernel with amazing speed, about 20-30 patches apply every day, few days ago we made our Debian image3 based on 3.4.53 and now few days later the kernel is 3.4.61+ so we blog based on this snapshot but if you try few days later things may change again. From the patches I see the development is focused on adding CedarX (VPU acceleration), Mali400 GPU and NAND support, and cleaning old code.

1. Setup of the toolchain

We use Linux debian 3.2.0-4-686-pae #1 SMP Debian 3.2.41-2+deb7u2 i686 GNU/Linux on our server to generate the images, it may or it may not work on other distributions if you follow the instructions below.

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 on your Linux machine, 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.7-arm-linux-gnueabihf ncurses-dev uboot-mkimage build-essential git

This will install: 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

Note that if you use debian may be you will need to add:

deb http://www.emdebian.org/debian squeeze main

in the file below:

/etc/apt/sources.list

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

2. Building Uboot

The Allwinner Linux-Sunxi community uboot is maintained by Henrik Nordstrom aka hno on Freenode irc. You can find him in #linux-sunxi or #olimex channels, if something with uboot is broken he is your man 🙂

First let’s make the directory where we will build the A20-OLinuXino-Micro linux:

# mkdir a20_kernel_3.4/
# cd a20_kernel_3.4/

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

Note that the OLinuXino-A20-Micro board u-boot is tested with the next branch:

# git rev-parse --verify HEAD
3d2fc4e8ff764209a8249c3b52dc937f3a106a7f

Download u-boot sourses:

# 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 A20-OLinuXino_MICRO CROSS_COMPILE=arm-linux-gnueabihf-

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

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

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

# cd ..

You should be in the following directory:

/home/user/a20_kernel_3.4/#

3. Building kernel sources for Olinuxino-A20-Micro

The Allwinner Linux-Sunxi community Kernel is maintained by Alejandro Mery aka mnemoc on Freenode irc. You can find him in #linux-sunxi or #olimex channel, if something is broken with the Linux Kernel you can contact him or use http://linux-sunxi.org/Mailing_list

Kernel sources for A20 are available on GitHub. Note that the following building is made with the revision below:

# git rev-parse --verify HEAD
8388cf06936aaf05342fb912ed6fc1a3b04e5afb

You can download the kernel sources using the following command:

# git clone https://github.com/linux-sunxi/linux-sunxi -b stage/sunxi-3.4

After the download go to the kernel directory

# cd linux-sunxi/

Here you need from a20 configuration file – olinuxinoa20_defconfig. The file contains all kernel module settings.

Download olinuxinoa20_defconfig https://docs.google.com/file/d/0B-bAEPML8fwldEVvU1hCZ29DODQ/edit?usp=sharing

then copy olinuxinoa20_defconfig file to configs directory:

# cp olinuxinoa20_defconfig linux-sunxi/arch/arm/configs/.

and make:

# make ARCH=arm olinuxinoa20_defconfig

The result should be:

configuration written to .config

If you wish to make your changes in the kernel configuration do:

# make ARCH=arm menuconfig

The menuconfig changes a .config text file, which you can view/edit even with a text editor like vi,nano.

With this command you can add or remove different modules for the different peripherials in the kernel. Be careful when use this as this may cause the kernel to not work properly.

Note that before compiling kernel you have to do some patches which are still not merged in 3.4:

the first patch is related with PWM:

download the patch sunxi-pwm.patch from https://docs.google.com/file/d/0B-bAEPML8fwlLVRucUJwamFQWHc/edit?usp=sharing

and apply the patch:

# patch -p0 < sunxi-pwm.patch

The second patch is realted with I2C speed and it is not mandatory. The patch changes i2c speed from 200kHz to 100kHz.

If you want to use some of olimex’s i2c modules on UEXT then you need from this patch.

download the patch sunxi-i2c.patch from https://docs.google.com/file/d/0B-bAEPML8fwlbnFyVnZHczJjVDQ/edit?usp=sharing

and apply the patch:

# patch -p0 < sunxi-i2c.patch

Now you can continue with kernel image compiling

# make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j4 uImage

when this finish’s you will have uImage ready and the result should be:

Image Name: Linux-3.4.61+
Created: Mon Sep 16 13:39:04 2013
Image Type: ARM Linux Kernel Image (uncompressed)
Data Size: 5450904 Bytes = 5323.15 kB = 5.20 MB
Load Address: 40008000
Entry Point: 40008000
Image arch/arm/boot/uImage is ready

Now you can build the kernel modules:

# make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j4 INSTALL_MOD_PATH=out modules

# make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j4 INSTALL_MOD_PATH=out modules_install

DONE! At this point you have uboot and kernel modules.

The uImage file is located in linux-sunxi/arch/arm/boot/

The kernel modules are located in linux-sunxi/out/lib/modules/3.x.xx

where 3.x.xx is kernel version

in our case the directory with modules is:

linux-sunxi/out/lib/modules/3.4.61+

4. Format and setup the SD-card

We suggest 4GB class 10 micro sd-card but you can use any card between 2GB and 16GB.

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 /dev/sdX

then do these steps:

  1. p
    will list your partitions

    if there are already partitions on your card do:

  2. d enter 1
    if you have more than one partitition press d while delete them all
  3. create the first partition, starting from 2048
    n enter p enter 1 enter enter +16M
  4. create second partition
    n enter p enter 2 enter enter enter

    then list the created partitions:
    p enter
    if you did everything correctly on 4GB card you should see something like:

    Disk /dev/sdg: 3980 MB, 3980394496 bytes
    123 heads, 62 sectors/track, 1019 cylinders, total 7774208 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x00000000

    Device Boot Start End Blocks Id System
    /dev/sdg1 2048 34815 16384 83 Linux
    /dev/sdg2 34816 7774207 3869696 83 Linux

  5. w

    write changes to sd card

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

the first partition should be vfat as this is FS which the Allwinner bootloader understands

# mkfs.vfat /dev/sdX1

the second should be normal Linux EXT3 FS

# mkfs.ext3 /dev/sdX2

5. Write the Uboot and sunxi-spl.bin

You should be in /home/user/a20_kernel_3.4/# directory
Note that you have to write u-boot-sunxi-with-spl.bin in /dev/sdX (not sdX1 or sdX2)

# dd if=u-boot-sunxi/u-boot-sunxi-with-spl.bin of=/dev/sdg bs=1024 seek=8

6. Write kernel uImage you build to the SD-card

You should be in the directory below

/home/user/a20_kernel_3.4/# directory 
# mount /dev/sdX1 /mnt/sd

copy the Kernel uImage to root directory in partition 1

# cp linux-sunxi/arch/arm/boot/uImage /mnt/sd

7. Write script.bin file

script.bin is a file with very important configuration parameters like port GPIO assignments, DDR memory parameters, Video resolution etc,
download the script.bin file from https://docs.google.com/file/d/0B-bAEPML8fwlbENYRHVJVXhrUmM/edit?usp=sharing

# cp script.bin /mnt/sd
# sync
# umount /dev/sdX1

8. Debian rootfs

The Linux Kernel and Uboot are ready, now we have need from Linux distribution rootfs.

Basically the only difference between the different Linux distributions is the rootfs, so if you put Debian rootfs you will have Debian, if you put Ubuntu rootfs it will be Ubuntu etc.

How to build one is a long topic, the good thing is that there are many already pre-built so we can just download one and use.

exit the kernel directory

# cd ..

You should be in the directory below

# /home/user/a20_kernel_3.4/

download debian rootfs – debian_34_fs.tgz from https://docs.google.com/file/d/0B-bAEPML8fwlZ3E1SnVNMDk4d2s/edit?usp=sharing

mount your sd card EXT3 FS partition:

# mkdir /mnt/sd
# mount /dev/sdX2 /mnt/sd

and unarchive the rootfs

# tar xzvf debian_34_fs.tgz -C /mnt/sd
# ls /mnt/sd

the right result should be:

bin dev home lost+found mnt proc run selinux sys usr
boot etc lib media opt root sbin srv tmp var

Now you have to replace the new generated kernel modules from

/home/user/a20_kernel_3.4/linux-sunxi/out/lib/modules/ to the new debian file system
# rm -rf /mnt/sd/lib/modules/3.4.43+/
# cp -rfv linux-sunxi/out/lib/modules/3.x.xx+/ /mnt/sd/lib/modules/

where x.xx is the kernel version
in our case:
# cp -rfv linux-sunxi/out/lib/modules/3.4.61+/ /mnt/sd/lib/modules/

replace /lib/firmware folder with the generated /linux-sunxi/out/firmware

#rm -rf /mnt/sd/lib/firmware/
#cp -rfv linux-sunxi/out/lib/firmware/ /mnt/sd/lib/
# sync
# umount /mnt/sdX2

at this point you have Debian on your SD card second partition and
you have an SD card ready to boot debian on A20-OLinuXino-Micro

Note that if you want to use the Olimex’s USB-WIFI module(https://www.olimex.com/Products/Modules/Ethernet/MOD-WIFI-RTL8188)
you need to install realtek firmware

for example:

connect LAN cable to the board

# dhclient eth0
# apt-get install firmware-realtek

Connect USB-SERIAL-CABLE-F to UEXT Tx.Rx and GND, or connect a HDMI screen. Put the SD-card in A20-OLinuXino-Micro and apply 6-16V power, you should see Uboot and then Kernel messages on the console

default username/password is : root / olimex

The ready made image-4 is here:

https://docs.google.com/file/d/0B-bAEPML8fwleDJMaG5reWZZeDQ/edit?usp=sharing

Aside

A13-OLinuXino playing with GPIOs

Image

Today I will show you how to access A13-OLinuXino GPIOs.

First make sure you have installed the GPIO drivers by

# lsmod

you will see the list of the installed kernel modules and if “sun4i_gpio” is there everything is OK

If you got A13-OLinuXino-SD with the pre-installed Debian everything is already setup for you, if not you can follow the instructions of this post and make one SD card yourself: https://olimex.wordpress.com/2012/10/12/building-bootable-sd-card-with-debian-linux-image-for-a13-olinuxino/ do not forget when compile the kernel to enable SUN4I_GPIO_UGLY = y inside .config , why UGLY? you will understand soon 😉

Allwinner do not care too much for the GPIOs as they primary support Android and their major market is the tablet manufacturers. This SUN4I-GPIO is made by Tom Cubie who work at Allwinner and made available to community, this driver though is not so flexible as the sysfs and you can’t switch between input and output easily.

With this driver you permanently assign one port to be input or output in the script.bin and when this gpio kernel module is loading it reads from script.bin the assignments and make them permanently, so if you want to change some GPIO to input or output you have to unload the driver make change in the script.bin and load it again and this is ugly 🙂

Anyway with the card we ship we made these gpios defined:

pe4, pe5, pe6,  pe7: inputs

pe8,  pe9,  pg10,  pg11,  pg9, pb10,  pb3,  pb4,  pe10,  pe11:  outputs

pg9 is GPIO output with the green status LED connected to it

to switch LED on you can do:

# echo 1 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9

and to switch LED off you can do:

# echo 0 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9

Now let’s see how fast we can toggle LED from shell script:

# vi gpio

and enter this code:

while [ true]

do

echo 1 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9

echo 0 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9

done

# chmod +x gpio

#./gpio

with the oscilloscope is seen square wave with 220 uS period i.e. 45 454 Hz! this is quite more fast than the 300Hz we achieved on iMX233 GPIOs from shell https://olimex.wordpress.com/2012/09/07/imx233-olinuxino-working-with-gpios/

now let’s try same in C:

# vi gpioc.c

#include <stdio.h>
#include <fcntl.h>
#include <string.h>

int main() {

char s_0[] = “0”;
char s_1[] = “1”;
int fd;

//toggle pg9 LED gpio

if((fd=open(“/sys/devices/virtual/misc/sun4i-gpio/pin/pg9”,O_RDWR))<0) return(1);
else {

while(1) {

if(write(fd,s_1,strlen(s_1))<0) return(1);

if(write(fd,s_0,strlen(s_0))<0) return(1);
};

if(close(fd)<0) return(1);

}
}

# cc gpioc.c -o gpioc

# chmod +x gpioc

# ./gpioc

the oscilloscope show 4.2 uS period square wave i.e. about 238 095Hz not so fast than iMX233 140kHz. But obviously if we chase speed should not use file system.

Now next step is to make access via /dev/mem as mmgpio with iMX233 https://olimex.wordpress.com/2012/09/11/imx233-olinuxino-gpios-faster-and-faster/

Using directly /dev/mem iMX233 achieved up to 5.7 Mhz GPIO toggle, I do wonder how fast we can go with A13, but this will be done in my next blog about it.

Aside

Open Fest and electronics Hackathon

Image

Open Fest is yearly event for open source and open technologies.

This year Open Fest celebrate 10 years and it will be on 3rd and 4th of November as usual in Sofia Interpred centre.

The foreign lecturers this year are: Brian Doll и Hengeveld Nick from GitHub and Harald Welte from OpenMoco.

Olimex will present where we are with the open source Hardware project OLinuXino and what is the further roadmap of devices which will be covered.

This year there will be an practical Hackathon which will run during the Open Fest. Everybody with idea to implement something with Arduino, Pinguino, Maple or OLinuXino have chance to complete the project during the event and to get as reward free board.

Please register your interest to attend the Hackathon to Marian Marinov (mm at openfest org) or to Olimex (info at olimex).

Aside