You are on page 1of 35

EMBEDDED SYSTEM DESIGN

LAB MANUAL 2011-12

STAFF: SUSHMA RAWAL Asst. Professor Dept. of Telecommunication Engg

LIST OF EXPERIMENTS

CYCLE I 1. Write a program to demonstrate I/O operation of ARM kit using GPIO LED port 2. Write a program to interface Seven segment display to ARM kit. 3. Write a program to interface LCD to ARM kit. 4. Write an ALP to find the GCD (Greatest Common Divisor), with and without conditional execution of ARM instructions. CYCLE II 5. Write a program to multiply two matrices with and without MLA instruction. 6. Write a program to scan the keypad, assign own values to the keys and display the key pressed. 7. Write a program for convolution of two sequences with and without MLA instruction. 8. Write a program to open a file and using fork system call create a child process. Let both the parent and child process write to the same file. Check the output of the file. CYCLE III 9. Write a program to communicate between two processes using (a) PIPE (b) FIFO. 10.Write a program to synchronize shared memory usage using Semaphore. 11.(a) Write a simple program to create three threads. (b) Perform 3x3 matrices addition using threads. 12.Open ended programs

SUSHMA RAWAL

ESD LAB MANUAL

1: INPUT-OUTPUT OPERATION OF ARM KIT USING GPIO LED PORT

Block Diagram

Algorithm:
1. Enable the LEDs from PIOA_OER 2. Output the values onto PIOA_SODR 3. Call delay routine 4. Clear LEDs by writing values on PIOA_CODR 5. Call delay function. Go back to step 2.

SUSHMA RAWAL

ESD LAB MANUAL

Program:
#include AT91SAM9260.h void delay(void); void main() { *PIOA_OER=0xFC0; While(1) { *PIOA_SODR=0xFC0; delay(); *PIOA_CODR=0xFC0; delay(); } } void delay(void) { int i; for(i=0;i<10000;i++); for(i=0;i<10000;i++); } /* clear PA6-PA11 to stop LED*/ /* Set PA6-PA11 to make LEDs glow*/ /* enable PA6-PA11 */ /* include header files*/

SUSHMA RAWAL

ESD LAB MANUAL

2: WRITE A PROGRAM TO INTERFACE SEVEN SEGMENT DISPLAY TO ARM KIT

Algorithm
1. Create an array of the data to be displayed. 2. Enable 0,1,2 and 24 pins of portA and enable output direction on them. 3. Enable the 6 data lines on portB and make them output lines. 4. Send 1s on 1,2 and 24 lines of portA and 0 on pin0 of portA(active low) to enable the seven segment displays. 5. Send data in the array to portB pins, left shifted. 6. Call delay. And pass the next data. Go to step 5.

Program
#includeAT91SAM9260.h void delay(void) { int i; for(i=0;i<100000;i++); for(i=0;i<100000;i++); } int main(void) { unsigned int; unsigned int display[]={0x86,0xDB,0xF1,0x80}; *PIOA_PER=0x01000007; /* enable PIOA pins*/ /* include header files*/

SUSHMA RAWAL

ESD LAB MANUAL

*PIOA_OER=0x01000007; *PIOB_PER=0x3FC; *PIOB_OER=0x3FC; *PIOA_SODR=0x01000006; *PIOA_CODR=0x00000001; while(1) { for(i=0;i<4;i++) { *PIOB_SODR=display[i]<<2; delay(); *PIOB_CODR=display[i]<<2; delay(); } } } /* enable the four displays*/ /* enable PIOB pins*/

SUSHMA RAWAL

ESD LAB MANUAL

3A: WRITE AN ALP TO FIND GCD OF TWO NUMBERS USING CONDITIONAL INSTRUCTIONS Algorithm:
1. Store the two values in R0 and R1 2. Compare R0 and R1 3. If R0 is greater, subtract R0 from R1 and store in R0. Else subtract R1 from R0 and store in R1 4. Continue this till contents of R0 and R1 is equal 5. End program

AREA GCD,CODE,READONLY MOV R0, #35 MOV R1, #15 X CMP R0,R1 SUBHI R0,R1,R0 SUBLT R1,R1,R0 BNE X END

SUSHMA RAWAL

ESD LAB MANUAL

3B:WRITE AN ALP TO FIND GCD WITHOUT USING CONDITIONAL INSTRUCTIONS


AREA GCD, CODE, READONLY MOV R0, #25 MOV R1, #15 GCD CMP R0, R1 BEQ X BLT LESS SUB R0, R0, R1 LESS SUB R1, R1, R0 B GCD X BX

SUSHMA RAWAL

ESD LAB MANUAL

4: WRITE AN ALP TO INTERFACE LCD DISPLAY TO AT91SAM9260 Algorithm:


1. Create an array of the commands to be executed and an array of the data to be displayed. 2. Send ones on PA1 and PA2 peripherals and enable output direction on them. 3. Enable the 8 data lines on portB and make them output lines. 4. Call the lcdcmd function and pass the commands in the command array tolcdcmd, sequentially, using a loop. 5. Call the lcddata function and pass the data array to it sequentially, using a loop. 6. In the function lcdcmd(): i. Select command register by passing a 0 to PA1 ii. Clear data bus iii. Left shift data by 2 and send it to PIOB_SODR iv. Set enable to 1, call delay(). Set enable to 0 and call delay(). 7. In the function lcddata() i. Select data register by passing a 1 to PA!. ii. Clear data bus. iii. Left shift data by 2 and send it to PIOB_SODR. iv. Set the enable and call delay().

SUSHMA RAWAL

ESD LAB MANUAL

PROGRAM:
#include AT91SAM9260.h void lcdcmd (unsigned int); void lcddata (unsigned int); void delay (void); int main(void) { unsigned int I; char datary[]={Embedded}; /*define data array*/ /*define command /* include header files*/ /* define function prototypes*/

unsigned int x[5]={0x38, 0x0E, 0x01, 0x06, 0x80}; register array*/ *PIOA_PER=0xo6; *PIOA_OER=0x06; *PIOB_PER=0x3FC; *PIOB_OER=0x3FC; for(i=0;i<5;i++) { lcdcmd(x[i]); } for(i=0;i<8;i++) { lcddata(dataary[i]); } } void lcdcmd(unsigned int cmddata)

/*enable PA1 and PA2 */ /* Rs=1, E=1 */ /*enable PB2-PB9*/

SUSHMA RAWAL

ESD LAB MANUAL

10

{ *PIOA_CODR=0x02; *PIOB_CODR=0x3FC; cmddata=cmddata<<2; *PIOB_SODR=cmddata; *PIOA_SODR=0x04; delay(); *PIOA_CODR=0x04; delay(); } void lcddata(unsigned int outdata) { *PIOA_CODR=0x02; *PIOB_CODR=0x3FC; outdata=outdata<<2; *PIOB_SODR=outdata; *PIOA_SODR=0x04; delay(); } void delay(void) { int I; for(i=0;i<15000;i++); } /*E=1*/ /*select command register*/ /*clear data bus*/ /*E=0*/ /*E=1*/ /*select command register*/ /*clear data bus*/

SUSHMA RAWAL

ESD LAB MANUAL

11

5: WRITE AN ALP TO INTERFACE KEYBOARD TO AT91SAM9260


Algorithm: 1. Drive a one on Row0, read all the columns. 2. If a key has been pressed, the corresponding column will be 1 else 0. 3. Keep repeating inner loop for each successive row.

PROGRAM

#include AT91SAM9260.h #include print.h #define row 0x1000 #define rowen 0xF000 #define col 0x10000 #define colen 0x38 #define col3 0x80000 Void delay(void) { Unsigned int i; For(i=0;i<100000;i++); } Void keydetect(unsigned int cldect, unsigned int i) { Int key[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; Unsigned int j, col1; Col1=col;
SUSHMA RAWAL ESD LAB MANUAL 12

For(j=0;j<4;j++) { If(col1==coldect) { q_printf( Key pressed is %d \n, key[(i*4)+j]); break; } else Col1=coll<<1; } } int main (void) { unsigned int row1,colvar,colvar1,i; *PIOA_PER=rowen|colen|col3; *PIOA_OER=rowen|colen|col3; While(1) { row1=row; for(i-0;i<4;i++) { *PIOA_CODR=rowen|colen|col3; *PIOA_SODR=row1; colvar=(*PIOA_PDSR)&colen;

SUSHMA RAWAL

ESD LAB MANUAL

13

colvar=colvar<<13; colvar1=(*PIOA_PDSRcol3; colvar=colvar|colvar1; delay(): keydect(colvar,i); row=row1<<I; } } return 0; }

SUSHMA RAWAL

ESD LAB MANUAL

14

6a:WRITE AN ALP FOR MATRIX MULTIPLICATION USING MLA INSTRUCTION Algorithm:


1. Store the addresses of the two matrices and resultant matrix in register. 2. Load each element onto the register and increment and multiply each element of first row matrix1 with first column of matrix2. This gives us the first element of the resultant matrix. 3. Second element of result matrix is the sum of product of the 1st row elements of matrix 1 and 2nd column of 2nd matrix. Hence, increment the corresponding count of each register. 4. Obtain the resultant matrix.

AREA, MATMUL, CODE, READWRITE ADR R1, ARRAY1 ADR R3, RESULT MATRIX MOV R0, #3 COUNTER BACK: ADR R2, ARRAY2 MOV R4, #3 COUNTER NEXT: MOV R6, #0 MOV R5, #3 MULTIPLICATION X: LDR R7, [R1], #4 LDR R8, [R2], #12 MLA R6, R7, R8, R6 ; DOT ; 1ST MATRIX ; RESULTANT ; ROW ; 2ND MATRIX ; COLUMN

SUSHMA RAWAL

ESD LAB MANUAL

15

SUBS R5, R5, #1 BNE X STR R6, [R3], #4 SUBS R4, R4, #1 SUB R2, R2, #0X20 SUBNE R1, R1, #12 BNE NEXT HERE: B HERE ARRAY1: DCD 1, 2, 3 DCD 1, 2, 3 DCD 1, 2, 3 ARRAY2: DCD1, 1, 1 DCD1, 1, 1 DCD 1, 1, 1

RESULT: SPACE 50 END

SUSHMA RAWAL

ESD LAB MANUAL

16

6b: MATRIX MULTIPLICATION WITHOUT USING MLA


AREA, MATMUL, CODE, READWRITE ADR R1, ARRAY1 ADR R3, RESULT MATRIX MOV R0, #3 COUNTER BACK: ADR R2, ARRAY2 MOV R4, #3 COUNTER NEXT: MOV R6, #0 MOV R5, #3 MULTIPLICATION X: LDR R7, [R1], #4 LDR R8, [R2], #12 MUL R9, R7, R8 ADD R6, R6, R9 SUBS R5, R5, #1 BNE X STR R6, [R3], #4 SUBS R4, R4, #1 SUB R2, R2, #0X20 SUBNE R1, R1, #12 BNE NEXT HERE: B HERE ARRAY1: DCD 1, 2, 3
SUSHMA RAWAL ESD LAB MANUAL 17

; 1ST MATRIX ; RESULTANT ; ROW ; 2ND MATRIX ; COLUMN

; DOT

DCD 1, 2, 3 DCD 1, 2, 3 ARRAY2: DCD1, 1, 1 DCD 1, 1, 1 DCD1, 1, 1 RESULT: SPACE 50 END

SUSHMA RAWAL

ESD LAB MANUAL

18

7: WRITE AN ALP FOR CONVOLUTION OF TWO SEQUENCES USING MLA INSTRUCTION


Seq1= 1,2,3,4 Seq2= 5,6,7,8 PROGRAM AREA CONVOLUTION,CODE,READWRITE ADR R0, SEQ1 ADR R5, RES MOV R10, #4 MOV R9, #1 BACK: MOV R1, #0 ADR R1, SEQ2 MOV R11,R9 BL PROD STR R2, [R6], #4 ADD R9, R9, #1 MOV R12, R9, LSL #2 ADD R0, R0, R12 SUB R10, R10, #1 BNE BACK MOV R10, #3;
ELEMENTS

; COUNTER FOR LAST 3

MOV R12,#4 BACK1: ADR R1, SEQ2 ADR R0, SEQ1 + 12


SUSHMA RAWAL ESD LAB MANUAL 19

MOV R2, #0 ADD R1, R1, R12 MOV R11, R10 BL PROD STR R2, [R5], #4 ADD R12, R12, #$ SUBS R10, R10, #1 BNE BACK1 LOOP: B LOOP PROD: LDR R3, [R0], #-4 LDR R4, [R1], #4 MLA R2,R3, R4, R2 SUBS R11,R11, #1 BNE PROD MOV PC, LR SEQ1: DCDE 1, 2, 3, 4 SEQ2: DCD 5, 6, 7, 8 RES: SPACE 100 END

; ACCUMULATOR

; SUBROUTINE

SUSHMA RAWAL

ESD LAB MANUAL

20

8A: WAP TO CREATE A PROCESS USING FORK() SYSTEM CALL


ALGORITHM: 1. Create a child process using fork() system call, while storing the process id in pid 2. If pid is negative, cannot create child process. Go to step 5. 3. If pid is zero then the current process running is the child process. 4. If the pid is any other value, then the current process is running under the parent process. 5. Exit

#include<stdio.h> #include<unistd.h> main() { int pid; pid=fork(); If(pid<0) { printf(\ncant fork); } else if (pid==0) { printf(\n child process); printf(\nchild pid=%d, parent pid=%d, getpid(),getppid()); }
SUSHMA RAWAL ESD LAB MANUAL 21

else { printf(\nparent process); printf(\n child pid=%d,parent pid=%d\n,pid,getpid()); } }

SUSHMA RAWAL

ESD LAB MANUAL

22

8B: WAP TO CREATE CHILD PROCESS AND LET BOTH PROCESSES WRITE INTO SAME FILE
ALGORITHM: 1. Create a child process using fork system call 2. Using fd open, create a write only file with appropriate permissions. 3. If in child process, enter the data and let it be on the buffer. 4. While in parent process, read the data from the buffer and write onto the file.

#Include< stdio.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h>

main() { int fd; char buf[5]; printf(\n fork program); fd=open(file,O_WRONLY|O_CREAT,0744); printf(\nfd=%d,fd); if(fork()==0) { printf(\nchild: enter data:); scanf(%s,buf); write(fd,buf,sizeof(buf));
SUSHMA RAWAL ESD LAB MANUAL 23

} else { printf(\n parent: eter data:); scanf(%s,buf); write(fd,buf,sizeof(buf)); } }

SUSHMA RAWAL

ESD LAB MANUAL

24

9A: WAP TO DEMONSTRATE INTER PROCESS COMMUNICATION USING PIPE


ALGORITHM: 1. Use pipe system call to create a pipe for ipc. 2. Create a child process using fork() call. 3. If child cant be created go to step 6. 4. In child process(=0), close the read end of the file and write onto the buffer. 5. While in parent process close the write end of the pipe and read from the buffer. 6. Exit.

#Include<unistd.h> #include<stdio.h> main() { int fd[2],childpid; char buf[80]; if(pipe(fd)<0) { printf(\ncant create pipe\n); } printf(\nfd[0]=%d,fd[1]=%d\n,fd[0],fd[1]); if((childpid=fork())<0) { printf(cant fork); }
SUSHMA RAWAL ESD LAB MANUAL 25

else if(child==0) { close(fd[0]); write(fd[1],hello world,12); } else { wait(); close(fd[0],buf,12); read(fd[0],buf,12); printf(\nbuf=%s\n,buf); } }

SUSHMA RAWAL

ESD LAB MANUAL

26

9B: WAP TO DEMONSTRATE INTERPROCESS COMMUNICATION USING FIFO


ALGORITHM: 1. First create a fifo using the command mknod myfifo m 0744 p Or mkfifo m 0744 myfifo At the command line. 2. Create 2 files, first file fiforead.c for reading from fifo. Second file fifowrite.c for writing into fifo. 3. Compile the two files and redirect them to another file. cc fiforead.c o read cc fifowrite.c o write 4. Now the compile files available by the name read and write. 5. First execute the read file using the command in one terminal, then execute the write file using command ./write in another terminal.

FIFOREAD.C #include<stdio.h> #include<unistd.h> #include<fcntl.h> main() { int fd; char buf[80];

SUSHMA RAWAL

ESD LAB MANUAL

27

fd=open(myfifo,O_RDONLY); read(fd, buf, sizeof(buf)); printf(%s\n,buf); }

FIFOWRITE.C #include<stdio.h> #include<unistd.h> #include<fcntl.h> main() { int fd; char buf[80]; fd=open(myfifo,O_WRONLY); printf(\nwrite: ); scanf(%[^\n],buf); write(fd,buf,sizeof(buf)); }

SUSHMA RAWAL

ESD LAB MANUAL

28

10. WRITE A PROGRAM TO SYNCHRONIZE SHARED MEMORY USAGE USING SEMAPHORE. ALGORITHM: 1. Create file. 2. Compile and execute file semaphore.c 3. Open 2 terminals. Run the file semind.c in first terminal. Two statements will be displayed. (before entering into critical section, inside the critical section) 4. Run the same file in another terminal. Here one statement will be displayed ( before entering into critical section) Critical section is blocked. When you press and enter in the first terminal then the statement inside the critical section is displayed on 2nd terminal(process 2 is unblocked and allowed to enter critical section)

10A. SEMAPHORE.C

#include<stdio.h> #include<sys/ipc.h> #include<sys/sem.h> #include<sys/types.h> union semun { int val; struct semid_ds *buf; unsigned short *array; };
SUSHMA RAWAL ESD LAB MANUAL 29

main() { int key,id; union semun arg; key=ftok(semaphore.c,d); printf(\n key=%d,key); id=semget(key,1,IPC_CREAT|0644); printf(\nsemid=%d\n,id); arg.val=1; semctl(id,0,setval,arg); }

10b. semimp.c

#include<stdio.h> #include<sys/ipc.h> #include<sys/sem.h> main() { int key,semid; key=ftok(semaphore.c,d); printf(key=%d,key); struct sembuf buf={0,-1,0}; semid=semget(key,1,0);

SUSHMA RAWAL

ESD LAB MANUAL

30

printf(\n before entering into critical section\n); semop(semid,&buf,1); printf(\ninside the critical section\n); getchar(); buf.sem_op=1; semop(semid,&buf,1); }

SUSHMA RAWAL

ESD LAB MANUAL

31

11A. WAP TO CREATE AND EXECUTE 3 THREADS


ALGORITHM: 1. Include header files. 2. Define the thread function(fun) 3. Create three threads, using for loop. In each thread, call the thread function(fun); #include<stdio.h> #include<pthread.h> int i=1; void *fun() { printf(thread %d is executing\n,i++); } main() { int i; pthread_t tid; for(i=0;i<3;i++) { pthread_create(&tid,null,fun,null); pthread_join(tid,null); } }

SUSHMA RAWAL

ESD LAB MANUAL

32

11B. WAP TO ADD TWO MATRICES USING THREADS


ALGORITHM:

1. Include header files 2. Define thread_function. Define the addition of corresponding elements of the two matrices in the function. 3. Input the two matrices. 4. Using for loops, create threads for each matrix element addition and in each thread, call thread_function. 5. Display the resultant matrix using for loops.

#include<stdio.h> #include<pthread.h> #include<unistd.h> int a[3][3],b[3][3],c[3][3]; int i,j,x=1; void *thread_function() { printf(\nthread %d created \n,x++) c[i][j]=a[i][j]+b[i][j]; } main() { pthread_t mythread[3][3]; printf(enter the first matrix element);
SUSHMA RAWAL ESD LAB MANUAL 33

for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(enter the [%d][%d] element:,i,j); scanf(%d,&a[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { pthread_create(&mythread[i][j],null,thread_function,null); pthread_join(mythread[i][j],null); } } printf(\n the addition of two matrices using threads:\n); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(\t%3d,c[i][j]); printf(\n\n); }

SUSHMA RAWAL

ESD LAB MANUAL

34

printf(\n\n\n); }

*******************

SUSHMA RAWAL

ESD LAB MANUAL

35

You might also like