Book Image

Banana Pro Blueprints

By : Tony Zhang
Book Image

Banana Pro Blueprints

By: Tony Zhang

Overview of this book

This book follows a tactical plan that will guide you through the implementation of Banana Pro and its configurations. You will then learn the various programming languages used with Banana Pi with the help of examples. In no time at all, you’ll be working on a wireless project that implements AirPlay servers, hotspots, and so on. Following this, you’ll develop a retro-style arcade kiosk game. Then we’ll move on to explore the multimedia features of Banana Pro by designing and building an enclosure for it. After this, you’ll learn to build a remote-controlled smart car and we’ll examine how to control a robotic arm. The book will conclude with the creation of a home sensor system that has the ability to expand or shrink to suit any home.
Table of Contents (15 chapters)
Banana Pro Blueprints
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Available operating systems for Banana Pro


This section presents some of the available operating systems for Banana Pro. Here, Android has briefly described some of the most important Linux distributions:

Figure 12: A screenshot of operating systems available on the LeMaker web page

Tip

LeMaker offers image versions for both Banana Pi and Banana Pro. However, be sure to download the correct image.

Figure 13

Android

Although Android is based on Linux, both architectures are quite different. This can be seen even in the example of low-level C language routines, which Google exchanged for Android. In addition to this, Google prohibits all code that's licensed under GPL (GNU Public License). For this reason, Android only implements a rudimentary shell featuring only a few shell commands. Due to the C-library incompatibilities, normal Linux code, even if it's especially compiled for the ARM architecture, will not run under Android. Nevertheless, Android is a very popular operating system (OS) that's used by many phones and tablets. It fully supports graphic hardware in terms of accelerated video and 3D graphics. For this reason, the first Banana Pro version of Kodi (formerly known as XBMC and Xbox Media Centre) made use of an accelerated Android graphics library (refer to Chapter 4, An Arcade Cabinet).

Applications for Android can be developed using a special software development kit (SDK) and Java itself as most Android applications are based in Java. Although this book concentrates on Linux projects rather than Android projects, we will take a short look at Android on Banana Pro.

LeMaker provides three different Android images: one for a 5 inch TFT, the second for a 7 inch TFT, and the third using the HDMI cable as a video output. To install an Android OS, simply download the required version. You most probably want to start with the HDMI enabled version. Use PhoenixCard (as seen in the previous section) in order to write the image to an SD card. Insert this SD card and boot Android. After a while, the start screen of Android will be visible.

Figure 14: This is Android 4.4 running on Banana Pi/Pro (http://www.bananapi-kaufen.de/wp-content/uploads/2014/07/800px-Android_v2.jpg)

Android can be controlled by using a keyboard and mouse. Bluetooth and WLAN will work out of the box and some applications are already preinstalled.

Figure 15: Android 4.4 running on Banana Pro (source: bananapi.com)

Linux

There are a variety of Linux distributions available on the LeMaker download site (Figure 13). The most famous operating systems are Raspbian, Bananian, Lubuntu, and Gentoo. LeMedia, a special Linux distribution that provides the Xbox media center, Kodi (formerly known as XBMC), is not available on the official LeMaker download website. We will later on compile and install XBMC for other operating systems such as a Debian distribution. No matter which Linux OS you decide on incorporating, the procedure is always the same: Copy the image to an SD card and boot the card within Banana Pro.

After copying the image, the SD card will contain a directory called /boot. This directory contains the Linux kernel (called uImage; refer to Chapter 2, Programming Languages, for this), a file called uEnv.txt, and a binary file (usually script.bin or bananapro.bin). The uEnv.txt file contains kernel parameters, such as screen resolutions, and the binary file contains the complete settings for Banana Pro with respect to all connectors.

My uEnv.txt file looks like this:

console=tty1
root=/dev/mmcblk0p1 rootwait
extraargs=rootfstype=ext4 sunxi_ve_mem_reserve=190 sunxi_g2d_mem_reserve=16
sunxi_fb_mem_reserve=32 hdmi.audio=EDID:1 disp.screen0_output_mode=1920x1080p50 panic=10 consoleblank=0
script=bananapro.bin
kernel=uImage

It defines /dev/tty1 as a console terminal, uses the first partition of an SD card as a root device (/dev/mmcblk0p1), and provides some additional kernel parameters. The video output mode is completely HD progressive (1920x1080p50). The binary file is called bananapro.bin in my case, and the kernel is uImage within the /boot directory. The function of the binary file is described in the next section (the FEX file).

For my experiments, I've used a Banana Pi Linux image of Igor Pečovnik (http://www.igorpecovnik.com/2014/09/07/banana-pi-debian-sd-image/), which I adapted to Banana Pro. In the meantime, the Banana Pi image is compatible with Banana Pro.

I recommend that you download the Debian Wheezy version that comes with kernel 3.4. Although this kernel is not up to date, it enables all the hardware features of Banana Pro as opposed to kernel 4.2, which, for example, neither has an audio driver nor an accelerated video output.

The image itself is based on Debian Wheezy and comes with kernel 3.4. Similar to Bananian, this distribution does not come with any X11 server or display manager. Therefore, I've installed the MATE Windows manager and the slim login manager. Similar to the X11 framebuffer driver, I've used sources from https://github.com/ssvb/xf86-video-fbturbo. This driver requires a number of packages to be installed first as follows:

sudo apt-get install git build-essential xorg-dev xutils-dev x11proto-dri2-dev
sudo apt-get install libltdl-dev libtool automake libdrm-dev autoconf

Now, acquire the sources of xf86-video-fbturbo, compile them, and then install them as follows:

sudo su
cd /usr/local/src
git clone https://github.com/ssvb/xf86-video-fbturbo.git
cd xf86-video-fbturbo
autoreconf -vi
./configure --prefix=/usr
make
make install

Afterwards, copy the default xorg.conf file to the location of your xorg.conf like this:

sudo cp xorg.conf /etc/X11/xorg.conf

Do not forget to back up any existing xorg.conf files first. In Chapter 4, An Arcade Cabinet, we will install accelerated Mali drivers for a desktop in addition to a framebuffer turbo driver.

Figure 16: The MATE desktop environment (source: mate-desktop.org)

MATE needs to be installed from the Debian Wheezy backports. Add deb http://http.debian.net/debian wheezy-backports main to the /etc/apt/sources.list file. This can be done using this command:

sudo nano /etc/apt/sources.list

Afterwards, run the following shell commands:

sudo apt-get update
sudo apt-get upgrade
apt-get install mate-desktop-environment mate-desktop-environment-extras
sudo apt-get install slim

In order to install the MATE desktop and slim, a configuration can be done in the /etc/slim.conf file. I've added/changed the following lines:

sessionstart_cmd xhost + && xset s noblank && xset s off && xset –dpms
default_user	pi
auto_login yes

The first line disables access control to the X-windows server. This is needed later on in Chapter 4, An Arcade Cabinet. In addition to this, screensavers and power management for the X-server are switched off. The second line sets the default user as the pi user. The last line allows the pi user to automatically log in without providing a password.

This user pi can be added to the system using this shell command:

sudo adduser

You need to answer all the questions prompted by the system and use the pi as a username.. Most Linux distributions provide an already secure shell access (ssh). If not, simply install the openssh server by typing the following command that will enable a remote login with ssh on port 22:

sudo apt-get install openssh-server

The FEX file

Banana Pro reads interface settings from a binary file called script.bin or bananapro.bin that is located in the /boot directory. This file defines video output, GPIO selection, and much more. Each value consists of a key = value pair combination under a [sectionheader]. All three entries are case-sensitive. A complete selection of all possible entries can be found at http://linux-sunxi.org/Fex_Guide.

The FEX files can be compiled into a binary file (and recompiled into an ASCII FEX file) using the fex2bin or bin2fex commands. These tools are available on GitHub (sunxi-tools is available at https://github.com/linux-sunxi/sunxi-tools). The sunxi-tools require libusb as a prerequisite.

Additionally, git will be installed. Git can be used in order to clone a directory that's available on the Internet on a local SD card or hard disk using this command:

sudo apt-get install libusb-1.0-0-dev git

Afterwards, clone the sunxi-tools repository and then build and install the tools:

git clone https://github.com/linux-sunxi/sunxi-tools
cd sunxi-toolsmake
sudo make install

The syntax of fex2bin and bin2fex is easy. Simply provide two filenames (the original and the converted one, respectively). Take a look at the following example:

sudo fex2bin /boot/script.fex /boot/script.bin

This will generate a new script.bin binary file that's based on the script.fex ASCII file in the /boot directory.

Tip

If uEnv.txt redefines, for example, display settings as compared to script.bin, this file will be preferred over script.bin.

A typical display section in the FEX file looks like this:

[disp_init]
disp_init_enable = 1
disp_mode = 0
screen0_output_type = 3
screen0_output_mode = 4
screen1_output_type = 2
screen1_output_mode = 11
fb0_framebuffer_num = 2
fb0_format = 10
fb0_pixel_sequence = 0
fb0_scaler_mode_enable = 0
fb1_framebuffer_num = 2
fb1_format = 10
fb1_pixel_sequence = 0
fb1_scaler_mode_enable = 0
lcd0_backlight = 197
lcd1_backlight = 197
lcd0_bright = 50
lcd0_contrast = 50
lcd0_saturation = 57
lcd0_hue = 50
lcd1_bright = 50
lcd1_contrast = 50
lcd1_saturation = 57
lcd1_hue = 50

The section header of the FEX file is [disp_init].disp_init_enable, the value of which can be either 1 (enabled display output) or 0 (disabled display output). The display mode is a value between 0 and 4 according to the following table:

Mode

Display mode

0

screen0 (screen0, fb0)

1

screen1 (screen1, fb0)

2

dualhead (screen0, screen1, fb0, fb1) (two screens and two framebuffers)

3

xinerama (screen0, screen1, fb0) (two screens and one big framebuffer)

4

clone (screen0, screen1, fb0) (two screens and one standard framebuffer)

Table 2: The display modes for Banana Pro

The screen0_out_color_range mode defines the output color for HDMI. It applies to both screen0 and screen1. Therefore, there is no screen1_out_color_range. The output color range can be set according to the following table:

Type

Output color range

0

16-255 is the default limited range

1

0-255 is the full range (PC level)

2

16-235 is the limited range (video level)

Table 3: The output color range for Banana Pro

The screen0_output_type mode sets the output type for screen0 according to the following table. Therefore, use screen0_output_type accordingly:

Type

Output type

0

None

1

LCD

2

TV

3

HDMI

4

Analog video

Table 4: The output types for Banana Pro

The following screen output modes (screen0_output_mode and screen1_output_mode) can be defined for screen0 and screen1:

Mode

Used for the TV/HDMI output

Used for analog video output

0

480i

1680 x 1050

1

576i

1440 x 900

2

480p

1360 x 768

3

576p

1280 x 1024

4

720p50

1024 x 768

5

720p60

800 x 600

6

1080i50

640 x 480

7

1080i60

 

8

1080p24

 

9

1080p50

 

10

1080p60

1920 x 1080

11

PAL

1280 x 720

14

NTSC

 

Table 5: The screen output modes for Banana Pro

The fb0_framebuffer_num value is the number of framebuffers to be used for the 0 framebuffer. It can be set to 2 for double buffering. Use fb1_framebuffer_num accordingly. The pixel format for both framebuffers can be defined as fb0_format or fb1_format according to this table:

Format

fb0_format

4

RGB655

5

RGB565

6

RGB556

7

ARGB1555

8

RGBA5551

9

RGB888

10

ARGB8888

12

ARGB4444

Table 6: The framebuffer format for Banana Pro

The framebuffer pixel sequences (fb0_pixel_sequency and fb1_pixel_sequence) must be set to 0 for Linux and 2 for Android operating systems (refer to this table):

Sequence

fb0_pixel_sequence or fb1_pixel_sequence

0

ARGB

2

BGRA

Table 7: The framebuffer pixel sequence for Banana Pro

The Framebufferscaler mode can be enabled or disabled for both framebuffers (fb0_scaler_mode_enable and fb1_scaler_mode_enable). Use a value of 0 to disable the scaler mode and a value of 1 to enable the scaler mode. The Scaler mode may improve the picture quality of some settings. The lcd0_backlight is a value between 0 and 240 and sets the PWM rate on a selected PWM GPIO. The same is true for lcd1_backlight. All the remaining settings are self-explanatory.

LeMaker provides special configuration files that can be used with LCD screens (refer to The LCD module section).

Transferring an OS to a hard disk

Banana Pro cannot boot directly from a hard disk. This section will show you how the Linux operating system can be copied from a microSD card to a hard disk and how a system can boot into the OS of the hard disk.

First, boot your Banana Pro with a connected hard disk (SATA and DC). The kernel messages will show the SATA device (usually /dev/sda). In order to get a filesystem overview, the lsblk command can be used. This command will list the complete filesystem including the attached hard disk.

Verify the contents of the hard disk using the following command:

fdisk –l /dev/sda

Principally, the available hard disk space must be equal to or larger than the size of the SD card in order to host the OS. The fdisk command can be used to partition the disk in terms of erasing or adding hard disk partitions. Use this program with care as it can delete everything that exists on your hard disk. The following steps assume an empty hard disk or a hard disk whose content is not needed any more.

The hard disk can be partitioned using the fdisk command:

sudo fdisk /dev/sda

The p (print) command will print partition table:

Command (m for help): p

Existing partitions can be removed using the d (delete) command:

Command (m for help): d
Partition number (1-6): 1

New partitions can be added using the n (new) command:

Command (m for help): n   
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1

The w (write) command writes all changes to the disk:

Command (m for help): w
The partition table has been altered!

After this, the fdisk write command will automatically quit. If no other partitions (for example, swap) are required, you can press RETURN when prompted for the first and last sectors. In its simplest sense, a swap partition serves as an overflow to Banana Pro's memory (RAM). It might be useful when compiling huge software packages such as Kodi (formerly known as XBMC). As an alternative, a file on a hard disk or SD card can be used for swapping. A swap partition itself has the 82 partition ID, which can be changed using the t (toggle) command followed by the ID. Swap partitions can be formatted using the following command:

mkswap /dev/sdaX,

X represents the swap partition (for example, 6). Swap partitions will be activated as follows:

swapon /dev/sdaX

New (empty) partitions must be formatted before usage. The following command formats the first partition (sda1) with the .ext4 filesystem and labels it as rootfs. In doing so, all existing data on this partition will be erased. Therefore, use this command with caution and only format empty partitions or partitions whose content is not needed any more:

sudo mke2fs -t ext4 -L rootfs /dev/sda1

In this step, the complete data from the microSD card will be copied to the hard disk. In our example, the first partition of the hard disk will be mounted into the /mnt directory; rsync will be used to transfer all data to the directory:

sudo mount /dev/sda1 /mnt
sudo rsync -ax / /mnt

The complete copy process will take a while. In the last step, the boot parameters of the uEnv.txt file will be adjusted to use the hard drive. Change root=/dev/mmcblk0p1 in the /boot/uEnv.txt file to this:

root=/dev/sda1

This can be done using the nano editor by typing this:

sudo nano /boot/uEnv.txt

Finally, restart Banana Pro with the following command:

sudo reboot

The system should be redirected from the microSD card to the hard disk right now. A df -h command on my system delivers the following (320 GB of free hard disk space):

Filesystem      Size  Used Avail Use% Mounted on
rootfs          289G  157G  118G  57% /
/dev/root       289G  157G  118G  57% /
devtmpfs        380M     0  380M   0% /dev
tmpfs           128M  280K  128M   1% /run
tmpfs           5,0M     0  5,0M   0% /run/lock
tmpfs           128M  2,3M  126M   2% /run/shm
tmpfs           1,0G  4,0K  1,0G   1% /tmp
/dev/mmcblk0p1   14G   11G  2,4G  83% /media/lime-next 

Tip

If you want to make any changes to uEnv.txt after booting to the hard disk, you will need to mount the SD card first, change into the SD card boot directory, and edit uEnv.txt accordingly:

sudo mount /dev/mmcblk0p1 /mnt
cd /mnt/boot
sudo nano uEnv.txt

The same applies to the installation of a new kernel (refer to Chapter 2, Programming Languages).

The gparted software is a graphical frontend for fdisk and other tools. It is very useful to add, delete, or even resize partitions. It may later be required for backups, where it can be used to shrink a partition, for example, on an SD card in order to use windisk32imager to copy the contents of the SD card to the backup medium.

Figure 17