Linux operating system in EDK

 

Linux operating system kernel compilation:

 

Launch the EDK and open the project from the "Embedded Development Kit" exercise:

 

 

 

The project window will appear:

 

 

 

Now you will configure the EDK to generate the device tree. The device tree holds hardware configuration data needed by Linux kernel (the kernel uses device tree to access devices at proper addresses, interrupts, etc.).

 

Click menu Software and then Software Platform Settings (or icon:  ).

New window will show up:

 

 

In the field OS:  change  standalone  to  device-tree.

 

Then click  OS and Libraries:

 

 

Now, in the field console device write:  RS232_Uart_1

In the field bootargs  change:  console=ttyS0  to  console=ttyUL0

(we are using UartLite in place of standard 16550).

 

 

Then click  OK.

 

Click Software and then Generate Libraries and BSPs  (or the icon:  ).

 

This will generate the file:  xilinx.dts  containing the device tree.

This file will be written to the directory (under the project main directory):

ppc405_0/libsrc/device-tree_v0_00_x

 

Now you will compile the Linux kernel.

 

Open the terminal and (in the home directory) run the script:


linux-kernel-copy

 

This script will copy the Linux kernel distribution directory to your home directory.

 

Then in the terminal window run the command:


source source_me

 

This will set all needed environment variables for PowerPC C compiler.

 

Then enter the Linux Kernel distribution directory:

 

cd linux-2.6-xlnx

 

and run the configuration manager:

 

make menuconfig

 

new window will be displayed:

 


 


Main settings (like processor version) are already properly prepared. You only have to enable GPIO access:

 

To enable GPIO choose option:

 

Device Drivers  --->

 

And press ENTER.

Then choose next option:

 

GPIO Support  --->

 

And press ENTER.

Then check the following position (using SPACE):

 

/sys/class/gpio/... (sysfs interface)

 

The asterisk symbol (*) means the option is checked:

 


 


Then press Exit  several times, when the save confirmation is displayed press  Yes – the application will be closed and current configuration will be written to the .config file.

 

Now you must copy previously generated file with device tree: (it is assumed that edk is your current project directory):

 

cp ~/edk/ppc405_0/libsrc/device-tree_v0_00_x/xilinx.dts  ~/linux-2.6-xlnx/arch/powerpc/boot/dts/virtex405-lab.dts

 

Then copy the file with root filesystem for Linux:

 

cp /opt/buildroot/initrd.patched.ext2.gz ~/linux-2.6-xlnx/arch/powerpc/boot/ramdisk.image.gz

 

Now you can begin the kernel compilation process (your current directory should be: linux-2.6-xlnx): 

 

make -j 2 simpleImage.initrd.virtex405-lab

 

Build process takes about 8 minutes.  Compiled kernel is copied to the following file:

~/linux-2.6-xlnx/arch/powerpc/boot/simpleImage.virtex405-lab.elf

 

Now you will run the Linux on the Development Board.

 

You do not have to start EDK system. You can download the bit file to the FPGA using following commands (it is assumed that edk is your project directory):

 

cd ~/edk

impact -batch etc/download.cmd

rlwrap -c xmd -opt etc/xmd_ppc405_0.opt

 

This procedure will download *.bit file generated previously and then start the XMD debugger in separate window. The connection to the board will be created automatically.

 

Run the  MINICOM  and check if the connection speed is 115200 bps (if not use following commands to change it: CTRL-A Z P I <Enter>.).

 

Now in the XMD window (prompt XMD%) enter following commands:

 

cd ~/linux-2.6-xlnx/arch/powerpc/boot

dow simpleImage.initrd.virtex405-lab.elf

run

 

This procedure is faster than loading the kernel file by software debugger.

 

After the system is loaded log on as user root (without password) at MINICOM console.

 

Mount your home directory from the laboratory PC (enter your login in place of dots):

 

mount -o rsize=1500,wsize=1500 192.168.1.1:/home/...   /mnt

 

You can also mount the application directory (you can use host names or IP numbers, for example: labhost or 192.168.1.1 is the address of the second network card of laboratory PC):

 

mount -o rsize=1500,wsize=1500 labhost:/opt_local   /opt

 

System Linux running on PowerPC (FPGA) has IP network address: 192.168.1.2 and host name: xilinx.

 

rsize and wsize options limit the packet length to the standard Ethernet packet,  (to avoid packet fragmentation and performance penalty) – the MAC controller in the FPGA doesn’t support large frames.

 

 

GPIO support at userspace level (suggested solution):

 

To try the access to the LEDs and switches on the development board change current directory:

 

cd /sys/class/gpio

 

Then display directory contents:

 

ls –al

 

total 0

drwxr-xr-x    5 root     root       0 May  8 23:32 .

drwxr-xr-x   15 root     root       0 May  8 23:32 ..

--w-------    1 root     root    4096 May  9 02:23 export

drwxr-xr-x    2 root     root       0 May  8 23:38 gpiochip243

drwxr-xr-x    2 root     root       0 May  8 23:38 gpiochip248

drwxr-xr-x    2 root     root       0 May  8 23:38 gpiochip252

--w-------    1 root     root    4096 May  9 02:42 unexport

 

gpiochipxxx directories contain information about available GPIO devices.

To be accessible GPIO ports have to be exported to the user first. You can achieve this by writing port id to the export file. Ports are one bit wide and have following ids:

 

Push buttons (5 ports):   243-247

LEDs (4 ports):                  248-251

Switches (4 ports):           252-255

 

# ls

export       gpiochip243  gpiochip248  gpiochip252  unexport

# echo 248 > export

# ls

export       gpio248      gpiochip243  gpiochip248  gpiochip252  unexport

 

New catalog has been created due to export command: gpio248

This catalog contains files for exported GPIO bit control:

 

# cd gpio248

# ls

active_low  direction   subsystem   uevent      value

 

Set the port direction to out:

 

# echo out > direction

 

Now you can write the logic value to the port:

 

# echo 1 > value

# echo 0 > value

 

Or you can switch the port direction to in and read the logic value from the port by reading the value file contents.

 

Now unexport the port:

 

# cd ..

# echo 248 > unexport

# ls

export       gpiochip243  gpiochip248  gpiochip252  unexport

 

 

Below simple script for LED and switch control is presented:

(LEDs blink, state of the switches is printed every 2 seconds):

 

#!/bin/sh

cd /sys/class/gpio

echo "248" > export

echo "249" > export

echo "250" > export

echo "251" > export

echo "252" > export

echo "253" > export

echo "254" > export

echo "255" > export

echo out > gpio248/direction

echo out > gpio249/direction

echo out > gpio250/direction

echo out > gpio251/direction

echo in > gpio252/direction

echo in > gpio253/direction

echo in > gpio254/direction

echo in > gpio255/direction

while true ; do

  sleep 1

  echo 0 > gpio248/value

  echo 1 > gpio249/value

  echo 0 > gpio250/value

  echo 1 > gpio251/value

  sleep 1

  echo 1 > gpio248/value

  echo 0 > gpio249/value

  echo 1 > gpio250/value

  echo 0 > gpio251/value

  echo -n `cat gpio252/value`

  echo -n `cat gpio253/value`

  echo -n `cat gpio254/value`

  echo  `cat gpio255/value`

done

 

When exiting the application outputs should be disabled (direction switched to in) and ports should be unexported:

 

#!/bin/sh

cd /sys/class/gpio

echo in > gpio248/direction

echo in > gpio249/direction

echo in > gpio250/direction

echo in > gpio251/direction

echo "248" > unexport

echo "249" > unexport

echo "250" > unexport

echo "251" > unexport

echo "252" > unexport

echo "253" > unexport

echo "254" > unexport

echo "255" > unexport

 

It is of course possible to access GPIO using other scripting languages (like TCL) or compiler languages (like C, C++, etc.) – you just use the file system interface of the chosen language.

 

 

Task: implementation of mini web server for remote LED control and switch status readout

 

Use simplified httpd server available in the PowerPC Linux system (/usr/sbin/httpd).

Write simple CGI application for GPIO control and integrate it with web server.

CGI can be written in scripting language (shell ash – simplified version of bash), in TCL language (tclsh8.4), in C language (simple hello-world compilation example presented below).

Additional information about CGI can be found below:

·        CGI in TCL:  http://expect.nist.gov/doc/cgi.pdf

·        CGI in ash:  http://isquared.nl/index.html?pagelink=9&desc=bourne/_bash_shell_cgi_scripts

·        CGI in C:  http://www.cs.tut.fi/~jkorpela/forms/cgic.html

 

httpd is a simplified www server:

 

Usage: httpd [-ifv[v]] [-c CONFFILE] [-p [IP:]PORT] [-u USER[:GRP]] [-r REALM] ]

or httpd -d/-e/-m STRING

 

Listen for incoming HTTP requests

 

Options:

        -i              Inetd mode

        -f              Don't daemonize

        -v[v]           Verbose

        -c FILE         Configuration file (default httpd.conf)

        -p [IP:]PORT    Bind to ip:port (default *:80)

        -u USER[:GRP]   Set uid/gid after binding to port

        -r REALM        Authentication Realm for Basic Authentication

        -h HOME         Home directory (default .)

        -m STRING       MD5 crypt STRING

        -e STRING       HTML encode STRING

        -d STRING       URL decode STRING

 

You can run the server this way:

 

httpd -h home_directory_of_www_page

 

CGI scripts should be placed in subdirectory /cgi-bin/

You can also write the configuration file for the server, sample configuration file is presented below:

 

H:/serverroot     # define the server root. It will override -h

A:172.20.         # Allow address from 172.20.0.0/16

A:10.0.0.0/25     # Allow any address from 10.0.0.0-10.0.0.127

A:10.0.0.0/255.255.255.128  # Allow any address that previous set

A:127.0.0.1       # Allow local loopback connections

D:*               # Deny from other IP connections

E404:/path/e404.html # /path/e404.html is the 404 (not found) error page

I:index.html      # Show index.html when a directory is requested

 

P:/url:[http://]hostname[:port]/new/path

                  # When /urlXXXXXX is requested, reverse proxy

                  # it to http://hostname[:port]/new/pathXXXXXX

 

/cgi-bin:foo:bar  # Require user foo, pwd bar on urls starting with /cgi-bin/

/adm:admin:setup  # Require user admin, pwd setup on urls starting with /adm/

/adm:toor:PaSsWd  # or user toor, pwd PaSsWd on urls starting with /adm/

.au:audio/basic   # additional mime type for audio.au files

*.php:/path/php   # run xxx.php through an interpreter

 

A/D may be as a/d or allow/deny - only first char matters.

Deny/Allow IP logic:

 - Default is to allow all (Allow all (A:*) is a no-op).

 - Deny rules take precedence over allow rules.

 - "Deny all" rule (D:*) is applied last.

 

Example:

   1. Allow only specified addresses

     A:172.20          # Allow any address that begins with 172.20.

     A:10.10.          # Allow any address that begins with 10.10.

     A:127.0.0.1       # Allow local loopback connections

     D:*               # Deny from other IP connections

 

   2. Only deny specified addresses

     D:1.2.3.        # deny from 1.2.3.0 - 1.2.3.255

     D:2.3.4.        # deny from 2.3.4.0 - 2.3.4.255

     A:*             # (optional line added for clarity)

 

If a sub directory contains a config file it is parsed and merged with any existing settings as if it was appended to the original configuration.

 

subdir paths are relative to the containing subdir and thus cannot affect the parent rules.

 

Note that since the sub dir is parsed in the forked thread servicing the subdir http request, any merge is discarded when the process exits.

As a result, the subdir settings only have a lifetime of a single request.

 

Custom error pages can contain an absolute path or be relative to 'home_httpd'. Error pages are to be static files (no CGI or script).

Error page can only be defined in the root configuration file and are not taken into account in local (directories) config files.

 

If -c is not set, an attempt will be made to open the default root configuration file.  If -c is set and the file is not found, the server exits with an error.

 

 

Show the running server to the instructor.

 

 

Appendix -

Compilation of C program for PowerPC (for CGI in C):

 

Compilation is performed on the PC, then using the home directory, which can be mounted on PowerPC Linux system, you can easily run the compiled program on the laboratory board.

Run the following command in the terminal window:

 

source source_me

 

Create main.c file.

For example:

 

#include <stdio.h>

main()

{

  printf("Hello World!\n");

}

 

Then compile it using static linkage:

 

ppc_4xx-gcc main.c -o main -static

 

Now you can start compiled program in MINICOM window.