Saturday, October 5, 2013

PI 1

                       LAB 1&LAB 2 For Raspberry Pi 

1- Introduction:
       This report is based on the assessment of the lab 1 and lab 2.This report presents the objectives, required materials, tools and the required software. Since different processors or machines needs different assembly code languages, so that this report shows the procedures of how using ARM assembly code to deal with a Raspberry Pi in order to achieve the required goals.
 The objective of lab 1 is to turn on the LED light in the Raspberry Pi board and also keep the light on whenever it is connected to a proper power source. This LED light is labeled as ACT and this ACT label locates near the RCA and USB ports on the Raspberry Pi board.   
The objective of lab 2 is similar in the way of setting up the experiment to the lab 1, but the objective in lab 2 is to turn on and off repeatedly the LED light in the Raspberry Pi until the Raspberry Pi disconnected from a proper power source.
2- Materials and Procedures:
In order to carry this assignment out, there are two types of requirements .The first type is the required equipment and the second one is the software and the operating system.
2.1Equipment:
   1-A Raspberry Pi board (it is a single circuit that considered as a computer which, primary runs on a version of Linux).
   2-A computer uses a Microsoft Windows-based Operating System.
   3- A SD card (it is a memory card).
   4- A power supply.

2.2Software:
1-     Using  the GNU compiler toolchain in order to deal with the architecture
2-     The OS Template File
 (It provides a Makefile script, a Linker script and no assembly code would be provided. Since the compiler needs required instructions in order to create Operating System for the Raspberry Pi, those instructions have been provided by the OS Template file )

2.3 Experimental Procedure
In order to start our experiment, we need to download the following software, then installing them for the Microsoft Windows. 
1-     Downloading the YAGARTO packages
(YAGARTO packages include YAGARTO Tools and YAGARTO GNU ARM toolchain. YAGARTO is a cross compiler that deals with the ARM architecture. It includes the GNU C/C++ toolchain and works with  Eclipse .After the installation of the YAGARTO packages to a path without any space, such as 'C:\YAGARTO\ , we  need to restart the computer  in order to make it work successfully.)

2.4-Downloading the OS Template File
 (It provides a Makefile script, a Linker script and no assembly code would be provided. Since the compiler needs required instructions in order to create Operating System for the Raspberry Pi, those instructions have been provided by the OS Template file )
Now after right tools have been installed and yagarto is on the right path, we need to write the assembly code. But before starting the assembly code, we need to do the following steps for both lab 1 and lab 2:
1-     Creating a file (main.s) in the (Source) directory.
2-     Instructions to the assembler are require handling the communication between assembly code language and the binary machine language.





               
The ARM assembly code for lab 1:

.section .init and .globl _start are directives that instructs the assembler in order to put our assembly code in a section its name is ( .init ).  Because of that, we will be able to control the order of the code execution. Also, the GNU toolchain needs a entry point to work properly, so we need to include symbol _start. Then, we need global, which is required by the linker.
  ldr   r0,=0x20200000

0x20200000 is a number in the form of Hexadecimal digit and the Hexadecimal digit is 4 bits long. Since computer deals with numbers, so the manufacturing designers give this Hexadecimal digit to physical address of the GPIO controller. Now, we have a physical address of the GPIO controller in number, so we need to store this address in the somewhere in the processor. In first assembly commend, the address number 2020000016 will be stored into register r0.
 mov r1,#1
 lsl r1,#18

Most of the instructions in In ARM assembly code consist of three letters such as mov, ldr, lsl and so on.
mov instruction just deals with binary representations ,  and the # is used  in order to indicate  what is followed behind it,  is always  a  number.
The mov instruction stores the 12   into register r1.
In the lsl instruction, it shifts the 12 to the left by 18 positions, so it became 1000000000000000000  instead of 0001 or 12   in the same register r1.
 Why do we need to shift 12 by 18 places to become 10000000000000000002  ?
The answer of this is back to the GPIO controller. The goal is to turn on the LED light that is wired to the 16th GPIO pin, so that we are targeting the 16th GPIO pin.
The GPIO consists of 54 pins and these 54 pins are arranged in 6 sets. Since the 16th GPIO pin locates between the 10 and 19 pins, so we are dealing with a set number 6 which associated with 3 bits for every pin in this set. Then by multiplying 3*6 we get 18 which is the shifting number in lsl command.

 Str r1,[r0,#4]
We store the value of r1 in the r0 after adding 4 to its address.as it is known that r0 is corresponding GPIO controller address and the 4 comes from the GPIO’s 2nd set that associated with 4 bytes.
In this stage we can turn on the LED light, but to do so we need to send that stored value to the GPIO controller.
The Led is designed to be turned on at the low voltage, so we need to write down these commands in order to meet the manufacturing requirements .on other word, we will turn GPIO pin 16 off  by setting the 1 in the 16th pin and o’s in the others pin. Also it is required to translating from binary to hexadecimal notation in ( register r0) at the output to communicate correctly with the GPIO controller.   
 mov r1,#1
 lsl r1,#16
 str r1,[r0,#40]
 In order to end assembly code files correctly, we need to finish with an empty line. Also this will maintain the Raspberry Pi from crashing since the processor would never recognize that we are done.
loop$:
      loop$ is just a label  that  gives a name to the next line.
b loop$
We used the b branch command to execute the next line. As result of that, an infinite loop that makes the processor works until disconnecting from a power supply in the right way.

The ARM assembly code for lab 2:
The ARM assembly code for lab2 that would be able to turn on and off the LED light in the Raspberry Pi repeatedly until the Raspberry Pi disconnected from a proper power source.
The following part is similar to ARM assembly code for lab 1
.section .init
.globl _start
_start:
ldr r0,=0x20200000
mov r1,#1
lsl r1,#18
str r1,[r0,#4]
mov r1,#1
lsl r1,#16

Now, we will start the part is related only to lab 2;
In order to see the flashing as a result of the switching of  the LED between on and off repeatedly  , 
we need to make some delay on time between this switching. Without this delaying technique,our eyes would not be able to recognize this flashing. The reason behind that is the computer has the ability to turning the light from  on to off   many times  per  second .
In lab 1 already we turn on the LED by setting GPIO pin 16 to low voltage.
Now, we will trick the processor by making it busy without doing anything. The processor will stay busy while decrementing the hexadecimal number 0x3F0000 until reaching the 0.

mov r2,#0x3F0000
It is storing the hexadecimal number 0x3F0000 in register r2.

wait1$: is a label for the loop.

sub r2,#1
It subtracting 1 from every time until the value on r2 reaches the 0.
           
cmp r2,#0
It is comparing the value of r2 if it 0 or not ,then storing the comparing result.

bne wait1$


TO execute the branch itself then the next line.
After the waiting was done successfully, we need to turn the LED off, and that could be done by setting GPIO pin 16 to high voltage.
str r1,[r0,#28]
28 is the required offset of the address r0 in order to turn the GPIO pin 16 on which means turning the light off, then storing the value in r1.

Again, we will trick the processor by making it busy without doing anything. The processor will stay busy while decrementing the hexadecimal number 0x3F0000 until reaching the 0.

mov r2,#0x3F0000
It is storing the hexadecimal number 0x3F0000 in register r2.
wait2$: is a label.
sub r2,#1
It subtracting 1 from every time until the value on r2 reaches the 0.

cmp r2,#0
It is comparing the value of r2 if it 0 or not, then storing the comparing result.

bne wait2$
TO execute the branch itself then the next line.
b loop$
We used the b branch command to execute the next line. As result of that, an infinite loop that makes the processor works until disconnecting from a power supply in the right way.

3. Results
For lab 1;
After executing the Arm assembly code for lab 1, we use the generated image with the Raspberry Pi to turn the LED light on.






For lab 2;

After executing the Arm assembly code for lab 2, we use the generated image with the Raspberry Pi to turn the LED light on and off.





4- Conclusion:
In lab 1 and lab 2, we investigated how could  we write an ARM assembly code. Then we used the some tools in order to translating these commends into a machine language.To prove our objectives, we used a raspberry pi to test two cases. The first one was to turn on the LED light " ACT"  and keep it on as long as there is a power source and the second one to turn  the LED on and off repeatedly until the Raspberry Pi would be  disconnected from a proper power source  . 

5- References


No comments:

Post a Comment