You are on page 1of 72

EMBEDDED SYSTEM DESIGN

(NORTH MAHARASHTRA UNIVERSITY)

EMBEDDED LABORATORY SOLUTIONS

FROM

OASIS TECHNOLOGIES PRIVATE LIMITED


www.oasistech.co.in 203, Eden Hall Opp. OM Super Market Model Colony Shivajinagar, Pune 411016 Maharashtra, India Phone: +91-20-65236642 / 64000203 Fax: +91-20-25661715 Email: sales@oasistech.co.in support@oasistech.co.in

Learning Manual for Embedded Design Laboratory as per North Maharashtra University Curriculum
Dear Faculty Member, Oasis Technologies is a leading provider of single board computer and embedded systems solutions. We offer a variety of solutions that range from the standard off-the-shelf single board computer products and embedded operating systems to completely custom-designed solutions and intellectual property solutions. We have extensive experience in the area of ARM processor technology. Our product portfolio includes embedded software tools and ARM based reference boards which are highly popular in the area of processor technology academics and in the use of such processor technology amongst the corporate sector. Here we present a complete laboratory manual for 'Embedded System Design' subject, which is newly added in North Maharashtra University syllabus. This manual will certainly be helpful for, not only for faculty members but also for students, to learn and get hold on ARM processor technology. Note that the data provided in meant for internal ciculation only.

For Oasis Technologies Pvt. Ltd.

Elective I

EMBEDDED SYSTEM DESIGN Theory : 4 Hrs/week Marks : 100


Unit I Embedded system Introduction Introduction to Embedded System,History,Design challenges,optimizing design metrics,time to market,applications of embedded systems and recent trends in embedded systems,embedded design concepts and definations,memory management,hardware and software design and testing,communication protocols like SPI,SCI, I2C,CAN etc. Unit II System Architecture Introduction to ARM core architecture,ARM extension family,instruction set,thumb instruction set,Pipeline,memory management,Bus architecture,study of on-chip peripherals like I/O ports,timers,counters,interrupts,on-chip ADC,DAC,RTC modules,WDT,PLL,PWM,USB etc.

Practical:2 Hrs/week Marks:50


T.W.25

Unit III Interfacing and programming Basic embedded C programs for on-chip peripherals studied in system architecture.Need of interfacing,interfacing techniques,interfacing of different displays including Graphic LCD(320 X 240),interfacing of input devices including touch sceen etc,interfacing of output devices like thermal printer etc.,embedded communication using CAN and Ethernet,RF modules,GSM modem for AT command study etc.

Unit IV Real time Operating System Concept Architecture of kernel,task scheduler,ISR,Semaphores,mailbox,message queues, pipes, events,timers, memory management,RTOS services in contrast with traditional OS,Introduction to COSII,Inter-task communication in COSII,memory management in COSII,porting of RTOS. Unit V Embedded Linux Introduction to the Linux kernel,Configuring and booting the kernel,The root file system,Root file directories,/bin,/lib etc.,Linux file systems,Types of file system: Disk,RAM,Flash,Network,Some debug techniques-Syslog and strace,GDB,TCP/IP Networking- Network configuration,Device control from user space- Accessing hardware directly,Multi processing on Linux and Inter Process Communicaton- Linux process model and IPCs, Multithreading using pThreads Threads vs. Processes and pThreads,Linux and Real-Time- Standard kernel problems and patches. 3

Text Books 1) Frank Vahid, Embedded system design , PHI 2) Rajkamal Embedded Systems , TMH 3) David Simon Embedded Systems software primer , Pearson 4) Steve Furber ARM System-on-Chip Architecture , Pearson 5) Jean J. Labrose MicroC/OS-II , Indian Low Price Edition

Reference Books 1) Dr. K.V.K.K. Prasad Embedded /real time systems , Dreamtech 2) Iyer Gupta Embedded real systems Programming ,TMH 3) Steve Heath Embedded System Design , Neuwans

LAB EXERCISE 1) Integrated Development Environment Overview.(Project creation,down load & debug) 2) Study of JTAG Debugger / on-board debugger-emulator. 3) ARM Instructions execution(Barrel Shifter,LDR/STR,SMT,LDM)

List Of Practicals
Group-A 1) 2) 3) 4) Writing basic C-programs for I/O operations C-Program to exlore timers/counter C-Programs for interrupts Program to demonstrate UART operation

Group-B 5) Program to demonstrate I2C Protocol 6) Program to demonstrate CAN Protocol Group -C 7) Program to interface LCD 8) Program to interface Keyboard and display key pressed on LCD 9) Program to interface stepper motor Group D 10) Program to demonstrate RF communication 11) Program to implement AT commands and interface of GSM modem 12) Implementation of USB protocol and transferring data on PC 13) Implementation of algorithm / program for the microcontroller for low power modes Group E 14) Interfacing 4x4 matrix keyboards and 16x2 character LCD display to microcontroller / microprocessor and writting a program using RTOS for displaying a pressed key 15) Writting a scheduler using RTOS for 4 tasks with priority. The tasks may be Keyboard, LCD, LED etc. And porting it on microcontroller / microprocessor Group F 16) Implement a semaphore for any given task switching using RTOS on microcontroller board 17) Create two tasks, which will print some characters on the serial port. Start the scheduler and observe the behavior Group G 18) RTOS based interrupt handling using Embedded Reat Time Linux 19) Program for exploration of (Process Creation, Thread Creation) using Embedded Real Time Linux Group H 20) Program for exploring Message Queues using Embedded Real Time Linux 21) Ethernet based Socket Programming using Embedded Real Time Linux

LAB EXERCISE 1: Objective: To write a Program for I/O operations. Program Description: This program demonstrates blinking of LED using GPIO. Pin Assignment :LED Display Interface Sr.No 1 2 3 4 5 6 7 8 Signal Pin 45 (P0.15) Pin 46 (P0.16) Pin 47 (P0.17) Pin 53 (P0.18) Pin 54 (P0.19) Pin 55 (P0.20) Pin 1 (P0.21) Pin 2 (P0.22) Description L8 L7 L6 L5 L4 L3 L2 L1

Output: LED blinks continuously.

Part List : 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the board with COM port of PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode and observe the output.

*****Code for led blinking in assembly language***** .extern main .global start_main start_main: .TEXT .CODE 32 .EQU IO0SET, 0XE0028004 .EQU IO0DIR, 0XE0028008 .EQU IO0CLR, 0XE002800C .GLOBAL start_main start_main: LED_BLINK: ldr r0,=IO0DIR Ldr r1,=0X007F8000 str r1,[r0] loop: ldr r4, =IO0CLR LDR r3, =0x007F8000 str r3, [r4] BL DELAY ldr ldr str R4,=IO0SET R3,=0x007F8000 R3,[R4]

BL DELAY B loop DELAY: ldr r7,=0x00100000 delay1: sub cmp bne mov .end .end BL main HERE: B HERE .END 7 r7,r7,#0x01 r7,#00 delay1 pc,lr

LAB EXERCISE 2: Objective: To write a Program for Timer(on chip) Program Description: This program demonstrates blinking of LED using GPIO after specified interval. Pin Assignment: LED Display Interface Sr.No Signal 1 Pin 45 (P0.15) 2 3 4 5 6 7 8 Pin 46 (P0.16) Pin 47 (P0.17) Pin 53 (P0.18) Pin 54 (P0.19) Pin 55 (P0.20) Pin 1 (P0.21) Pin 2 (P0.22) Description L8 L7 L6 L5 L4 L3 L2 L1

Output: LED blinks after specific interval. Header Files: LPC21xx.h, board.h Part List : 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode and observe the output.

*****Blinking of LED using GPIO after specified interval***** #include<board.h> #include<LPC21xx.h> int j; void ISRHandler()__attribute__((interrupt("IRQ"))); void ISRHandler(void) { *T0IR=1; int i; q_printf("in isr %d\n",j); j++; *IOSET0=0x00550000; for(i=0;i<0x50000;i++); *IOCLR0=0x007f8000; *VICVectAddr=0 ; } void IRQInit() { *VICVectAddr0=(unsigned int)ISRHandler; *VICVectCntl0=0x20|4; *VICIntEnable=0x010 |*VICIntEnable; } int main(void) { *IODIR0=0x7F8000; IRQInit(); q_printf("main\n "); *T0TCR=1; *T0MCR=0x3; *T0PR=0; *T0MR0=0x1500000; return 0; }

LAB EXERCISE 3: Objective: To write a program for External Interrupts. Program Description: This program demonstrates interrupts using on board EXT SWITCH Pin Assignment : The following table lists details about the 7-Segment Display interface. Description: 7-segment Display Sr.No 1 2 3 4 5 6 7 8 9 10 11 Output: The digit on 7 Segment segment will glow after getting interrupt. Header Files: LPC21xx.h, board.h Part List : 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode and observe the output. 10 Signal Pin 16 (P1.16) Pin 12 (P1.17) Pin 8 (P1.18) Pin 4 (P1.19) Pin 48 (P1.20) Pin 44 (P1.21) Pin 40 (P1.22) Pin 36 (P1.23) Pin 13 (P0.28) Pin 14 (P0.29) GND Description a b c d e f g . NC NC NC

*****External Interrupt based 7-segment display***** #include<board.h> #include<LPC21xx.h> void ISRHandlerEXTINT3()__attribute__((interrupt("IRQ"))); void delay(void) { int j; for (j=0;j<100000;j++); for (j=0;j<100000;j++); for (j=0;j<100000;j++); for (j=0;j<100000;j++); for (j=0;j<100000;j++); } void ISRHandlerEXTINT3(void) { *EXTINT=0x00000008; q_printf("External Interrupt\n"); *IODIR1 = 0XFF0000; *IOCLR1 = 0Xff0000; delay(); *IOSET1 = 0x3f0000; delay(); *IOCLR1 = 0Xff0000; *IOSET1 = 0x060000; delay(); *IOCLR1 = 0Xff0000; *IOSET1 = 0x5b0000; delay(); *IOCLR1 = 0Xff0000; *IOSET1 = 0x4f0000; delay(); *IOCLR1 = 0Xff0000; *IOSET1 = 0x660000; delay(); *IOCLR1 = 0Xff0000; *IOSET1 = 0x6d0000; delay(); *IOCLR1 = 0Xff0000; *IOSET1 = 0x7d0000; delay(); *IOCLR1 = 0Xff0000; *IOSET1 = 0x070000; delay(); *IOCLR1 = 0Xff0000; *IOSET1 = 0x7f0000; delay(); *IOCLR1 = 0Xff0000; *IOSET1 = 0x6f0000; delay(); 11

*IOCLR1 = 0Xff0000; *IOSET1 = 0x770000; delay(); *IOCLR1 = 0xff0000; *IOSET1 = 0x7c0000; delay(); *IOCLR1 = 0xff0000; *IOSET1 = 0x390000; delay(); *IOCLR1 = 0xff0000; *IOSET1 = 0x5e0000; delay(); *IOCLR1 = 0xff0000; *IOSET1 = 0x790000; delay(); *IOCLR1 = 0xff0000; *IOSET1 = 0x710000; delay(); *IOCLR1 = 0xff0000; *IOSET1 = 0x760000; delay(); *IOCLR1 = 0xff0000; *IOSET1 = 0x800000; delay(); *IOSET1 = 0x000000; delay(); q_printf("Press the switch again\n"); *VICVectAddr=0 ; } void IRQInit() { *PINSEL1=0x20000000; *EXTMODE=0x8; *VICVectAddr0=(unsigned int)ISRHandlerEXTINT3; *VICVectCntl0=0x20|0x11; *VICIntEnable=0x20000 |*VICIntEnable; } int main(void) { *IODIR0=0x7F8000; IRQInit(); q_printf("main\n "); return 0; } 12

LAB EXERCISE 4: Objective: To write a Program for UART (on chip) Program Description: This program demonstrates UART based communication. Pin Assignment : Refer the data sheet of LPC21xx of chapter UART. Output: A character pressed on PC keyboard, will be displayed on hyper terminal as per the designed baud rate. Header Files: LPC21xx.h Part List : 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode. 6. Press any key on PC keyboard and observe the output on Hyper Terminal.

13

*****Program for UART based communication***** #include <lpc21xx.h> void txd(); int main(void) { //PINSEL CONFIGURATION *PINSEL0 &=0XFFFFFFF0; *PINSEL0 |=0X05; // BAUD RATE GENERATION *U0LCR=0X80; *U0DLL=0X62; *U0DLM=0X0; //MODE CONFIGURATION *U0LCR=0X03; while(1) { txd(); } return 0; } void txd() { while((*U0LSR & 0x20)!=0x20); *U0THR=*U0RBR; }

14

LAB EXERCISE 5: Objective: To write a Program for I2C. Program Description: This program demonstrates I2C Protocol for TITAN board in Master Transmitter and Receiver mode. Pin Assignment : Refer data sheet for I2C pin details. Output: User can read / write data from / to external EEPROM. Header Files: address_c.h , functions.h Part List : 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode and you can access external EEPROM.

15

*****I2C Test for Master Transmitter and Receiver mode***** #include "address_c.h" #include "functions.h" #define ENABLE 0x040 #define START 0x020 #define STOP 0x10 #define SI 0x08 #define AA 0x04 #define DISABLE 0x040 #define START_CLR 0x020 #define STOP_CLR 0x10 #define SI_CLR 0x08 #define AA_CLR 0x04 int data[200],length=0; int main(void) { q_printf("\nin main function "); i2c_init(); i2c_eeprom_write(0,0,0,16); i2c_eeprom_read(0,0,0,16); asm("loop_forever : b loop_forever"); } /*-------------delay--------------*/ void delay(u32 val) { while(val--) { asm("nop"); } } /*--------- init function---------*/ void i2c_init() { q_printf("\n in init() "); 16

*PINSEL0 = *PINSEL0 & 0XFFFFFF0F; *PINSEL0 = *PINSEL0 | 0X00000050; *I2CONCLR = DISABLE | START_CLR | STOP_CLR | SI_CLR | AA_CLR; *I2SCLH = 150; *I2SCLL = 150; } /*---------read-------------*/ void read_data() { q_printf("\n in read() \n "); int i=0; for(i=length;i>0;i--) { q_printf("%x", data[i]); } } /*----------wait-------------*/ void wait_for_ack(u32 status) { while(1) { if(*I2CONSET & SI) { if(*I2STAT == status) { break; } else { q_printf("\n ERROR STATUS RECEIVED = "); q_printf("%x", *I2STAT); *I2CONSET = STOP; *I2CONCLR = 0xFF; asm("b loop_forever"); } } } }

17

/*----------------- transmit function------------------------*/ void i2c_eeprom_write(u8 dev_addr,u8 page_no,u8 page_offset,u8 no_bytes) { q_printf(" \n in write()"); *I2CONSET = ENABLE; delay(1000); /*---------------------transmit a START and address with write---------------------*/ *I2CONSET = START; wait_for_ack(0x08); q_printf("\n START successful"); *I2DAT = ( (dev_addr <<1)| 0XA0); *I2CONCLR = SI_CLR; *I2CONCLR = START_CLR; wait_for_ack(0x18); q_printf("\n address transmit successful"); /*-----------------------transmit page no-----------------------------*/ q_printf("\n transmitting page address"); *I2DAT = page_no; *I2CONCLR = SI_CLR; wait_for_ack(0x28); *I2DAT = page_offset; *I2CONCLR = SI_CLR; q_printf("\n page address transmitted successfully"); /*-----------------------------transmit data------------------------------*/ q_printf("\n transmitting data"); while(no_bytes > 0) { wait_for_ack(0x28); *I2DAT = 0x5A; no_bytes--; *I2CONCLR = SI_CLR; } q_printf("\n data transmitted successfully");

18

/*-------------transmit a STOP and do acknowledge polling--------------*/ wait_for_ack(0x28); *I2CONCLR = SI_CLR; *I2CONSET = STOP ; while(!(*I2CONSET & SI)) { *I2CONSET = START; delay(100); *I2DAT = 0XA1; while(1) {q_printf(" \n waiting"); if(*I2CONSET & SI) break; } } *I2CONCLR = DISABLE | START_CLR | STOP_CLR | SI_CLR | AA_CLR; } /*------------------------receive function---------------------------*/ void i2c_eeprom_read(u8 dev_addr,u8 page_no,u8 page_offset,u8 no_bytes) { q_printf("\n in read()"); length = no_bytes; *I2CONSET = ENABLE ; delay(1000); /*---------------------transmit a START and address---------------------*/ *I2CONSET = START; wait_for_ack(0x08); q_printf("\n START successful"); *I2CONCLR = START_CLR; *I2DAT = ( (dev_addr <<1)| 0XA0); *I2CONCLR = SI_CLR; wait_for_ack(0x18); q_printf("\n address transmit successful"); /*-----------------------transmit page no-----------------------------*/ q_printf("\n transmitting page address"); *I2DAT = page_no; *I2CONCLR = SI_CLR; wait_for_ack(0x28); *I2DAT = page_offset; *I2CONCLR = SI_CLR; wait_for_ack(0x28); 19

q_printf("\n page address transmitted successfully"); wait_for_ack(0x28); *I2CONCLR = SI_CLR; *I2CONSET = START ; wait_for_ack(0x10); *I2CONCLR = START_CLR; *I2CONSET = AA; *I2DAT = ( (dev_addr <<1)| 0XA1); *I2CONCLR = SI_CLR; wait_for_ack(0x40); *I2CONCLR = SI_CLR; /*--------------------------receive data------------------------------*/ while(no_bytes>0) { wait_for_ack(0x50); data[no_bytes] = *I2DAT; no_bytes--; *I2CONCLR = SI_CLR; } wait_for_ack(0x50); *I2CONCLR = AA_CLR; *I2CONCLR = SI_CLR; wait_for_ack(0x58); /*-------------------transmit a STOP---------------------------------*/ *I2CONSET =STOP ; *I2CONCLR = DISABLE | START_CLR | STOP_CLR | SI_CLR | AA_CLR; read_data(); }

20

LAB EXERCISE 6 : Objective: To write a Program for Controller Area Network. Program Description: This program demonstrates CAN Protocol based communication. Pin Assignment : Refer data sheet for CAN pin details. Output: User can transfer data on CAN bus. Header Files: LPC21xx.h Part List : 1. OASIS ARM7( TITAN) BOARD (2 nos.) 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE for 'Transmitter' & Receiver' . 4. Download the .hex file for 'Transmit Routine' on one board. 5. Download the .hex file for 'Receive Routine' on other board. 6. Put the boards in RUN mode and you can transfer data on CAN. 7. Accept 'PC Keyboard input' using Transmitter board and display the same using Hyper Terminal using Receiver board.

21

*****CAN Transmit Routine***** #include <LPC21xx.h> void Transmit_CAN(unsigned int); void Call_Wait(void); int main(void) { unsigned int i; *PINSEL1 = 0x00054000; *C1MOD = 0x00000001; *C1GSR = 0X00000000; *C1BTR = 0x003E0005; *C1MOD = 0x00000000; Call_Wait(); Transmit_CAN(0x23); Call_Wait(); for(i=0;i<100;i++) { Transmit_CAN(i); } return 0; } void Transmit_CAN(unsigned int i) { if((*C1SR & 0x00000004) == 0x00000004) { *C1TFI1 = 0X00080000; *C1TID1 = 0X00000022; *C1TDA1 = i; *C1TDB1 = i+1; *C1CMR = 0x00000021;

22

q_printf("Transmitting Data"); Call_Wait(); Call_Wait(); } else { q_printf("Previous Transmission Not completed"); Call_Wait(); } Call_Wait(); } void Call_Wait() { unsigned int i; for(i=0;i<1000000;i++); }

23

*****CAN RECEIVE ROUTINE***** #include <LPC21xx.h> void Call_Wait(void); int main(void) { *PINSEL1 = 0x00054000; *C1MOD = 0x00000001; *C1GSR = 0X00000000; *C1BTR = 0x003E0005; *C1MOD = 0x00000000; q_printf("Inside RxRoutine"); Call_Wait(); while(1) { if((*C1RFS == 0x00080000) && (*C1RID == 0x00000022)) { q_printf("Frame Received correctly"); } else { q_printf("RxError"); } q_printf("%x\n",*C1RDA); q_printf("%x\n",*C1RDB); Call_Wait(); *C1CMR = 0x00000004; Call_Wait(); q_printf("%x\n",*C1SR); Call_Wait(); } return 0; } void Call_Wait() { unsigned int i; for(i=0;i<1000000;i++); } 24

LAB EXERCISE 7: Objective: To write a program for interfacing LCD. Program Description: This program displays output on LCD Pin Assignment: LCD display interface Sr. No. 1 2 3 4 5 6 7 8 9 10 11 Signal Pin 16 (P1.16) Pin 12 (P1.17) Pin 8 (P1.18) Pin 4 (P1.19) Pin 48 (P1.20) Pin 44 (P1.21) Pin 40 (P1.22) Pin 36 (P1.23) Pin 13 (P0.28) Pin 14 (P0.29) GND Description Data 0 Data 1 Data 2 Data 3 Data 4 Data 5 Data 6 Data 7 RS EN WR

Output: 'A string' given in code will be displayed on LCD. Header Files: LPC21xx.h Part List : 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode and you can observe the output.

25

*****Interfacing LCD***** #include<lpc21xx.h> void lcddata(int value); void lcdcmd(int value); void delay(int itime); int main() { *PINSEL1=*PINSEL1 & 0xF0FFFFFF; *IODIR1=0x00FF0000; *IODIR0=0x30000000; lcdcmd(0x38); delay(250); lcdcmd(0x0e); delay(250); lcdcmd(0x80); delay(250); lcddata('h'); delay(250); lcddata('e'); delay(250); lcddata('l'); delay(250); lcddata('l'); delay(250); lcddata('o'); delay(250); return 0; } void lcdcmd(int value) { *IOCLR1=0x00FF0000; *IOCLR0=0x10000000; value<<=16; *IOSET1=value; *IOCLR0=0x20000000; delay(250); *IOSET0=0x20000000; delay(250); *IOCLR0=0x20000000; delay(250); }

26

void lcddata(int value) { *IOCLR1=0x00FF0000; *IOSET0=0x10000000; value<<=16; *IOSET1=value; *IOCLR0=0x20000000; delay(250); *IOSET0=0x20000000; delay(250); *IOCLR0=0x20000000; delay(250); } void delay(int itime) { int i; for(i=0;i<itime;i++); }

27

LAB EXERCISE 8: Objective: To write a Program to interface for keypad and LCD. Program Description: This program demonstrates LCD using keypad. Output: Character will be displayed on LCD as per the pressed key on keyboard. Header Files: Board.h Part List : 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode. 6. Press any key on on-board Keypad and you can observe the output on LCD.

28

*****Displaying the key press on LCD***** #include <board.h> int main(void) { char key; q_keyinit(TITAN); q_lcdinit(TITAN); q_printf("%s \r\nkey;","Hello"); q_displaylcd("Hello", 5); while(1) { while((key = q_keyread()) == 0); key = key + 0x30; q_clrscreen(); q_printf("%x \r\n", key); q_displaylcd(&key, 1); } }

29

LAB EXERCISE 9: Objective: To write a program for interface of Stepper Motor. Program Description: This program demonstrates interfacing of Stepper Motor. Pin Assignment : Sr. No. Signal 1 2 3 4 5 6 Pin 37 (P0.11) Pin 38 (P0.12) Pin 39 (P0.13) Pin 41 (P0.14) 5.0V GND

Description Data 0 Data 1 Data 2 Data 3 VCC(5) GND

Output: Motor will rotate as per the defined sequence. Header Files: LPC21xx.h, board.h Part List : 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Connect the stepper Motor as per the Pin diagram given in user manual. 6. Now set the board into RUN mode and observe the output.

30

*****Interfacing a stepper motor***** #include <LPC21xx.h> #include<board.h> int main(void) { unsigned long int i,j; *PINSEL0 = *PINSEL0 &(0XC03FFFFF); *IODIR0 = *IODIR0 | (0X00007800); q_lcdinit(TITAN); while(1) { q_displaylcd("clkwise directxn",16); for(j=0;j<12;j++) { *IOSET0 = 0x00002800; for(i=0;i<25000;i++); *IOCLR0 = 0x00002800; for(i=0;i<25000;i++); *IOSET0 = 0x00003000; for(i=0;i<25000;i++); *IOCLR0 = 0x00003000; for(i=0;i<25000;i++); *IOSET0 = 0x00005000; for(i=0;i<25000;i++); *IOCLR0 = 0x00005000; for(i=0;i<25000;i++); *IOSET0 = 0x00004800; for(i=0;i<25000;i++); *IOCLR0 = 0x00004800; for(i=0;i<25000;i++); } q_clrscreen(); q_displaylcd("anticlk directxn",16); for(j=0;j<12;j++) { *IOSET0 = 0x00002800; for(i=0;i<25000;i++); *IOCLR0 = 0x00002800; for(i=0;i<25000;i++); *IOSET0 = 0x00004800; for(i=0;i<25000;i++); 31

*IOCLR0 = 0x00004800; for(i=0;i<25000;i++); *IOSET0 = 0x00005000; for(i=0;i<25000;i++); *IOCLR0 = 0x00005000; for(i=0;i<25000;i++); *IOSET0 = 0x00003000; for(i=0;i<25000;i++); *IOCLR0 = 0x00003000; for(i=0;i<25000;i++); } q_clrscreen(); } return 0; }

32

LAB EXERCISE 11: Objective: To write a program for implementing AT commands and interface of GSM modem. Program Description: Code for interfacing the GSM module,4x4 keypad(new board) and lcd screen for doing four general mobile operation. 1. 2. 3. 4. Making call Receiving call Sending SMS Receiving SMS and immediately displaying first 16 character of the SMS on LCD without acknowledge.

Header Files: AT91SAM9260.h Part List : 1. OASIS ARM9(Explorer) BOARD 2. Keypad plus LCD peripheral board 3. GSM Modem 4. GSM service SIM card 5. RS232 Serial cable 6. 7 Volt DC Power Supply 7. Triton IDE Procedure: 1. Connect 7V DC Power supply to the OASIS EXPLORER Board. 2. Connect the board with the COM port of the PC using the serial cable. 3. Power ON the board and come to 'Boot Loader prompt'. 4. Download the application binary file. 5. Connect GSM Modem to Explorer Board. 6. Refer manual for GSM Modem and 'Oasis application notes', follow the instructions

33

/************************************************* * Program for interfacing GSM with explorer board, interfacing 4x4 matrix key pad and 16x2 LCD. * For keypad port pin PA4-7 used * For LCD port pin PB0-9 from GPIO connector *************************************************/ #include "AT91SAM9260.h" #define #define #define #define #define #define #define #define linefeed 0x0D send_cmd 0x00 ent_no 0x01 snd_sms 0x02 dial_cmd 0x00 ent_cmd 0x01 no 0x00 yes 0x01

int keyread(void); void lcdcmd(unsigned int); void lcddata(unsigned int); void delay_lcd(void); void waitOnPowerOnForLCD(void); void lcdcmd(unsigned int cmddata); void asciidata(unsigned int outdata); void printData(unsigned int outdata); void lcddata(unsigned int outdata); void delay_lcd(void); void clrscreen(void); void displaylcd(char* , int ); void lcdinit(void); void put_c(unsigned char ); void print_str(char *str, int len); void dela(void); void recv_irq(void); void dela(void); void debounce(void); int keyread (void); void keyinit(void); void led_blink(void); unsigned char recv_ch[100]; unsigned char uart_int_flag; unsigned char sms_flag; unsigned char dial_flag; unsigned char sms_start;

34

int main(void) { unsigned char key; unsigned char prev_key; key=0; prev_key=0; sms_flag = send_cmd; dial_flag = dial_cmd; uart_int_flag=no; *PIOB_ASR = 0xC000; *PIOB_PDR = 0xC000; *DBGU_CR = *DBGU_CR | 0xAC; *DBGU_MR = 0x800; *DBGU_BRGR = 0x28B; //9600 *DBGU_CR = *DBGU_CR | 0x50; keyinit(); lcdinit(); print_str("AT\r",3); dela(); dela(); dela(); print_str("AT+CMGF=1\r",10); dela(); dela(); print_str("AT+CNMI=1,2,0,0,0\r",18); dela(); dela(); displaylcd("Mo On explorer",14); while(1) { while((key = keyread()) == 0) { if(uart_int_flag==yes) break; } if(uart_int_flag==yes) { clrscreen(); recv_irq(); uart_int_flag=no; }

35

if(key != prev_key) { prev_key = key; switch(key) { //case 0x3a: case 'A': sms_flag = send_cmd; clrscreen(); switch(dial_flag) { case dial_cmd: displaylcd("D>",2); put_c('A'); put_c('T'); put_c('D'); dial_flag = ent_cmd; break; case ent_cmd: displaylcd("calling",7); put_c(';'); put_c(13); dial_flag = dial_cmd; break; default: displaylcd("Mo On METIS",11); put_c(27); print_str("AT\r",3); sms_flag = dial_cmd; break; } break; case 'B': sms_flag = send_cmd; dial_flag = dial_cmd; clrscreen(); displaylcd("ATA",3); print_str("ATA\r",4); break; case 'C': sms_flag = send_cmd; dial_flag = dial_cmd; clrscreen(); displaylcd("ATH",3); 36

print_str("ATH\r",4); break; case 'D': dial_flag = dial_cmd; clrscreen(); switch(sms_flag) { case send_cmd: displaylcd("Mo no>",6); put_c('A'); put_c('T'); put_c('+'); put_c('C'); put_c('M'); put_c('G'); put_c('S'); put_c('='); put_c('"'); sms_flag = ent_no; break; case ent_no: displaylcd(">",1); put_c('"'); put_c(13); sms_flag = snd_sms; break; case snd_sms: displaylcd("SMS send ",12); put_c(26); sms_flag = send_cmd; break; default: displaylcd("ERROR=>Esc ",12); put_c(27); sms_flag = send_cmd; break; } break; case 'E': sms_flag = send_cmd; dial_flag = dial_cmd; clrscreen(); displaylcd("Esc",3); put_c(27); break; case 'F': 37

sms_flag = send_cmd; dial_flag = dial_cmd; clrscreen(); displaylcd("Mo On Explorer",14); print_str("AT\r",3); dela(); print_str("AT+CMGF=1\r",10); dela(); print_str("AT+CNMI=1,2,0,0,0\r",18); dela(); break; case 0x31: displaylcd(&key, 1); put_c('1'); prev_key=0; break; case 0x32: displaylcd(&key, 1); put_c('2'); prev_key=0; break; case 0x33: displaylcd(&key, 1); put_c('3'); prev_key=0; break; case 0x34: displaylcd(&key, 1); put_c('4'); prev_key=0; break; case 0x35: displaylcd(&key, 1); put_c('5'); prev_key=0; break; case 0x36: displaylcd(&key, 1); put_c('6'); prev_key=0; break; case 0x37: displaylcd(&key, 1); put_c('7'); prev_key=0; break; case 0x38: displaylcd(&key, 1); put_c('8'); prev_key=0; 38

break; case 0x39: displaylcd(&key, 1); put_c('9'); prev_key=0; break; case '0': key = '0'; displaylcd(&key, 1); put_c('0'); prev_key=0; break; default: displaylcd(&key, 1); print_str("AT\r",3); break; } } } } void put_c(unsigned char val) { *DBGU_THR = val; while(!(*DBGU_SR & 0x02)); } void print_str(char *str, int len) { while(len-- != 0) { put_c(*str); str++; } *DBGU_THR = 0x20; while(!(*DBGU_SR & 0x02)); } void recv_irq() { unsigned char count; switch(recv_ch[0]) { case 'R': 39

if(recv_ch[1] == 'I') { if(recv_ch[2] == 'N') { if(recv_ch[3] == 'G') { clrscreen(); displaylcd("RING", 4); put_c(27); //0x1b-> esc for(count=0;count<2;count++) led_blink(); } } } break; case ':': if(recv_ch[1] == ' ') { if(recv_ch[2] == '"') { clrscreen(); for(count=(sms_start+1) ; count<(sms_start+16);count++) displaylcd(&recv_ch[count],1); sms_start = 0; for(count=0;count<2;count++) led_blink(); print_str("AT+CNMA\r",8); dela(); } } break; default: clrscreen(); break; } }

40

int keyread(void) { unsigned int key_val = 0; unsigned int row = 0,col = 0; //-------------------- Read column-------------------*PIOA_OER = 0x00F0; *PIOA_ODR =0x0F00; *PIOA_SODR = 0x00F0; col = *PIOA_PDSR & 0x0FF0; col = (col >> 4) ; col = col+30; //------------- Read Row ---------------------------*PIOA_OER = 0x0F00; *PIOA_ODR =0x00F0; row = *PIOA_PDSR & 0x0FF0; row = (row >> 4); row = row+30; debounce(); switch(col){ case 0xAD: if(row == 0x2C) key_val = '1'; else if(row == 0x2B) key_val = '2'; else if(row == 0x29) key_val = '3'; else if(row == 0x25) key_val = 'A'; break; case 0x6D: if(row == 0x2C) key_val = '4'; else if(row == 0x2B) key_val = '5'; else if(row == 0x29) key_val = '6'; else if(row == 0x25) key_val = 'B'; break; case 0x4D: if(row == 0x2C) key_val = '7'; else if(row == 0x2B) key_val = '8'; else if(row == 0x29) key_val = '9'; else if(row == 0x25) key_val = 'C'; break; case 0x3D: if(row == 0x2C) key_val = 'D'; else if(row == 0x2B) key_val = '0'; else if(row == 0x29) key_val = 'E'; else if(row == 0x25) key_val = 'F'; break;

41

default:key_val = 0; break; } return key_val; } void debounce(void) { unsigned int j; unsigned char count; unsigned char temp; unsigned char start_flag; start_flag = no; for (j=0;j<355555;j++) { if((*DBGU_SR & 0x01)) { count=0 ; do { if(*DBGU_SR & 0x01) { temp = *DBGU_RHR; if(start_flag == no) { if(temp == 'R' || temp == ':' ) { recv_ch[count++]=temp; uart_int_flag=yes; start_flag = yes; if(recv_ch[0]== ':') { do { if(*DBGU_SR & 0x01) recv_ch[count++]=*DBGU_RHR; }while(recv_ch[count-1]!= '\r'); recv_ch[count-1]= 0; sms_start = count-1; } } else break; } else recv_ch[count++]=*DBGU_RHR; } } 42

while(recv_ch[count-1]!= '\r'); start_flag = no; break; } } } void dela() { unsigned long int i,d; for(i=0;i<10;i++) { for(d=0;d<100;d++); } } void keyinit(void) { *PMC_PCER = *PMC_PCER | (0x1 << 2); *PIOA_PER = 0xFF0; //PA4-11 *PIOA_PUDR = 0xFF0; } void led_blink(void) { *PIOA_OER = 0xFC0; *PIOA_SODR = 0xFC0; dela(); *PIOA_CODR = 0xFC0; dela(); } void waitOnPowerOnForLCD(void) { unsigned int i; for(i=0; i < 65536; i++); for(i=0; i < 65536; i++); } void lcdinit(void) { waitOnPowerOnForLCD(); *PIOB_PER = 0x3FF; *PMC_PCER = 0x00000008; *PIOB_OER = 0x3FF; lcdcmd(0x38); lcdcmd(0x0e); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x80); }

43

void cursratline1clm1(void) { lcdcmd(0x80); } void inccursor(void) { lcdcmd(0x06); } void clrscreen(void) { lcdcmd(0x01); lcdcmd(0x80); } void lcdcmd(unsigned int cmddata) { *PIOB_CODR = *PIOB_CODR | 0x1; *PIOB_CODR = *PIOB_CODR | 0x3FC; cmddata = cmddata << 2; *PIOB_SODR = *PIOB_SODR | cmddata; *PIOB_CODR= *PIOB_CODR | 0x002; delay_lcd(); *PIOB_SODR = *PIOB_SODR | 0x002; delay_lcd(); *PIOB_CODR = *PIOB_CODR | 0x002; } void lcddata(unsigned int outdata) { *PIOB_SODR = *PIOB_SODR | 0x001; outdata = outdata << 2; *PIOB_CODR = *PIOB_CODR | 0x3FC; *PIOB_SODR = outdata; *PIOB_CODR = *PIOB_CODR | 0x002; delay_lcd(); *PIOB_SODR = *PIOB_SODR | 0x0002; delay_lcd(); *PIOB_CODR= *PIOB_CODR | 0x0002; inccursor(); inccursor(); inccursor(); }

44

void displaylcd(char *str, int len) { while(len-- != 0) { lcddata(*str); str++; } } void delay_lcd(void) { int j,i; for (i=0;i<5;i++) for (j=0;j<500;j++); }

45

LAB EXERCISE 12: Objective: To write a program for Implementation of USB Program Description: This program demonstrates Implementation of USB Pin Assignment : Refer the data sheet of LPC21xx of chapter USB. Output: Character will be displayed on hyper terminal as per the designed baud rate. Header Files: string.h, print.h, type.h, usbdebug.h, console.h, usbapi.h, startup.h Part List : 1. OASIS ARM7( TITAN) BOARD with LPC2148 2. RS232 Serial cable 3. USB Cable 4. 9 Volt DC Power Supply 5. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode. 6. Now you can transfer data on USB, accept PC keyboard input and display the same on Hyper Terminal using USB cable.

46

***** Minimal implementation of a USB serial port, using the CDC class. This example application simply echoes everything it receives right back to the host. Windows: Extract the usbser.sys file from .cab file in C:\WINDOWS\Driver Cache\i386 and store it somewhere (C:\temp is a good place) along with the usbser.inf file. Then plug in the LPC214x and direct windows to the usbser driver. Windows then creates an extra COMx port that you can open in a terminal program, like hyper terminal Linux: The device should be recognized automatically by the cdc_acm driver, which creates a /dev/ttyACMx device file that acts just like a regular serial port. ***** #include <string.h> #include <print.h> #include "type.h" #include "usbdebug.h" #include "console.h" #include "usbapi.h" #include "startup.h" //#define BAUD_RATE #define BAUD_RATE #define INT_IN_EP #define BULK_OUT_EP #define BULK_IN_EP 115200 9600 0x81 0x05 0x82 64 (x&0xFF),(x>>8) 0x24 0x25 0x20 0x21 0x22

#define MAX_PACKET_SIZE #define LE_WORD(x) // CDC definitions #define CS_INTERFACE #define CS_ENDPOINT #define #define #define

SET_LINE_CODING GET_LINE_CODING SET_CONTROL_LINE_STATE

// data structure for GET_LINE_CODING / SET_LINE_CODING class requests typedef struct { U32 dwDTERate; U8 bCharFormat; 47

U8 U8 } TLineCoding;

bParityType; bDataBits;

//static TLineCoding LineCoding = {115200, 0, 0, 8}; static TLineCoding LineCoding = {9600, 0, 0, 8}; static U8 abBulkBuf[64]; static const U8 abDescriptors[] = { // device descriptor 0x12, DESC_DEVICE, LE_WORD(0x0200), 0x02, 0x00, 0x00, MAX_PACKET_SIZE0, LE_WORD(0xFFFF), LE_WORD(0x0007), LE_WORD(0x0100), 0x01, 0x02, 0x03, 0x01, // configuration descriptor 0x09, DESC_CONFIGURATION, LE_WORD(67), 0x02, 0x01, 0x00, 0xC0, 0x32, // control class interface 0x09, DESC_INTERFACE, 0x00, 0x00, 0x01, 0x02, 0x02, 0x01, 0x00, // header functional descriptor 0x05, CS_INTERFACE, 48

0x00, LE_WORD(0x0110), // call management functional descriptor 0x05, CS_INTERFACE, 0x01, 0x01, 0x01, // ACM functional descriptor 0x04, CS_INTERFACE, 0x02, 0x02, // union functional descriptor 0x05, CS_INTERFACE, 0x06, 0x00, 0x01, // notification EP 0x07, DESC_ENDPOINT, INT_IN_EP, 0x03, LE_WORD(8), 0x01, // data class interface descriptor 0x09, DESC_INTERFACE, 0x01, 0x00, 0x02, 0x0A, 0x00, 0x00, 0x00, // data EP OUT 0x07, DESC_ENDPOINT, BULK_OUT_EP, 0x02, LE_WORD(MAX_PACKET_SIZE), 0x00, // data EP in 0x07, DESC_ENDPOINT, BULK_IN_EP, 0x02, LE_WORD(MAX_PACKET_SIZE), 0x00, 49

// string descriptors 0x04, DESC_STRING, LE_WORD(0x0409), 0x0E, DESC_STRING, 'L', 0, 'P', 0, 'C', 0, 'U', 0, 'S', 0, 'B', 0, 0x14, DESC_STRING, 'U', 0, 'S', 0, 'B', 0, 'S', 0, 'e', 0, 'r', 0, 'i', 0, 'a', 0, 'l', 0, 0x12, DESC_STRING, 'D', 0, 'E', 0, 'A', 0, 'D', 0, 'C', 0, '0', 0, 'D', 0, 'E', 0, // terminating zero 0 }; /************************************************************************* BulkOut ======= Local function to handle incoming bulk data Currently, this function simply shows the data on the serial console and echoes it back to the host. **************************************************************************/ static void BulkOut(U8 bEP, U8 bEPStatus) { int i, iLen; char c; USBHwEPRead(bEP, abBulkBuf, &iLen); // show on console for (i = 0; i < iLen; i++) { c = abBulkBuf[i]; if ((c == 9) || (c == 10) || (c == 13) || ((c >= 32) && (c <= 126))) { DBG("%c", c); q_printf("%c",c); } else { DBG("."); q_printf("."); } 50

} // echo USBHwEPWrite(BULK_IN_EP, abBulkBuf, iLen); } /************************************************************************* HandleClassRequest ================== Local function to handle the USB-CDC class requests **************************************************************************/ static BOOL HandleClassRequest(TSetupPacket *pSetup, int *piLen, U8 **ppbData) { switch (pSetup->bRequest) { // set line coding case SET_LINE_CODING: DBG("SET_LINE_CODING\n"); memcpy((U8 *)&LineCoding, *ppbData, 7); *piLen = 7; DBG("dwDTERate=%u, bCharFormat=%u, bParityType=%u, bDataBits=%u\n", LineCoding.dwDTERate, LineCoding.bCharFormat, LineCoding.bParityType, LineCoding.bDataBits); break; // get line coding case GET_LINE_CODING: DBG("GET_LINE_CODING\n"); *ppbData = (U8 *)&LineCoding; *piLen = 7; break; // set control line state case SET_CONTROL_LINE_STATE: DBG("SET_CONTROL_LINE_STATE %X\n", pSetup->wValue); break; default: return FALSE; } return TRUE; }

51

/************************************************************************* main ==== **************************************************************************/ #define DBG q_printf int main(void) { //q_printf("Initialising USB stack\n"); #if 1 Initialize(); //q_printf("After init \n"); ConsoleInit(60000000 / (16 * BAUD_RATE)); q_printf("After Console Init"); // initialize stack USBInit(); // register descriptors USBRegisterDescriptors(abDescriptors); // register class request handler USBRegisterRequestHandler(REQTYPE_TYPE_CLASS, HandleClassRequest); // register endpoint handlers USBHwRegisterEPIntHandler(INT_IN_EP, 8, NULL); USBHwRegisterEPIntHandler(BULK_IN_EP, MAX_PACKET_SIZE, NULL); USBHwRegisterEPIntHandler(BULK_OUT_EP, MAX_PACKET_SIZE, BulkOut); DBG("Starting USB communication\n"); // connect to bus USBHwConnect(TRUE); // call USB interrupt handler continuously while (1) { USBHwISR(); } #else q_printf("Hello"); #endif return 0; }

52

LAB EXERCISE 14: Objective: To write a program for interfacing on-board Keypad & LCD, using RTOS . Program Description: This program RTOS based Keypad & LCD interfacing. Output: A key, pressed on Keypad is displayed on LCD. Header Files: board.h, ucos.h, lcd.h, keyboard.h Part List : 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode. 6. Press any key on on-board Keypad and you can observe the output on LCD.

53

*****Code to read key and display it on LCD***** #include<board.h> #include<ucos.h> #include<lcd.h> #include<keyboard.h> void init_timer(); OS_STK TaskStk[2][100]; OS_EVENT *MBox; void t1() { char err,*string; while(1) { OSTimeDly(5); string=OSMboxPend(MBox,0,&err); q_displaylcd(string,1); } } void t2() { char key; while(1) { OSTimeDly(5); while((key=q_keyread())==0); q_clrscreen(); key = key+0x30; OSMboxPost(MBox,&key); } } int main(void) { init_timer(); q_keyinit(TITAN); q_lcdinit(TITAN); q_inccursor(); OSInit(); MBox=OSMboxCreate((void *)0); TaskCreate(t1,0,"High",0); TaskCreate(t2,0,"Low",1); OSStart(); return 0; }

54

LAB EXERCISE 15: Objective: To write a program for RTOS for 3 tasks. Program Description: This program demonstrates RTOS context switching . Output: On-board LEDs will glow in defined pattern. Header Files: LPC21xx.h, board.h ,ucos.h Part List : 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 7. Connect 9 V DC Power supply to the OASIS TITAN Board. 8. Connect the Board with the COM port of the PC using the serial cable. 9. Generate .hex file using Triton IDE. 10. Download the .hex file. 11. Put the board in RUN mode and you can observe the LED output.

55

*****Creating three different tasks and context switching between three tasks***** #include <LPC21xx.H> #include <ucos.h> #include <board.h> #define TASK_STK_SIZE 100 #define NO_TASKS 5 char TaskData[NO_TASKS]; OS_STK TaskStk[NO_TASKS][TASK_STK_SIZE]; OS_STK TaskStartStk[TASK_STK_SIZE]; extern void init_timer (void); void Task3 (void *data) { int n, i; data = data; while(1) { for (n = 0x00200000; n <= 0x00400000; n <<= 1) { /* Blink LED 7, 8 */ *IOSET0 = n; OSTimeDly(10); *IOCLR0 = n; OSTimeDly(10); i++; q_printf("%s", "Task3 "); } } } void Task2 (void *data) { int n, i; data = data; while(1) { for (n = 0x00080000; n <= 0x00100000; n <<= 1) { /* Blink LED 5, 6 */ *IOSET0 = n; OSTimeDly(10); *IOCLR0 = n; 56

OSTimeDly(10); i++; q_printf("LoPrio "); } } } void Task1 (void *data) { int n, i; data = data; while(1) { for (n = 0x00008000; n <= 0x00040000; n <<= 1) { /* Blink LED 1,2, 3, 4 */ *IOSET0 = n; q_printf("Task1 LEDON "); OSTimeDly(10); q_printf("Task1 LEDOFF "); *IOCLR0 = n; OSTimeDly(10); q_printf("%s", "HiPrioTsk "); i++; } } } void startTask(void *data) { init_timer(); TaskCreate(Task1, (void *)&TaskData[0], "HiPrioTsk", 1); TaskCreate(Task2, (void *)&TaskData[1], "LoPrioTsk", 2); TaskCreate(Task3, (void *)&TaskData[2], "Task3", 3); while (1) { OSTimeDly(1000); } }

57

int main (void) { *IODIR0 = 0x007F8000; OSInit(); init_timer(); TaskCreate(startTask, (void *)0, "VHiPrioTsk", 0); OSStart(); return 0; }

58

LAB EXERCISE 16: Objective: To write a Program to implement a semaphore for any given task using RTOS Program Description: This program demonstrates RTOS semaphore . Output: On-board LEDs will glow in defined pattern. Header Files: LPC21xx.h, lcd.h, ucos.h Part List: 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode and you can observe the LED output.

59

***** Describing the semaphore behavior within two different tasks***** #include <LPC21xx.H> #include <ucos.h> #include "lcd.h" #define TASK_STK_SIZE 100 #define NO_TASKS 5 char TaskData[NO_TASKS]; OS_STK TaskStk[NO_TASKS][TASK_STK_SIZE]; OS_STK TaskStartStk[TASK_STK_SIZE]; OS_EVENT *printsem; extern void init_timer (void); void Task3 (void *data) { int n, i; data = data; while(1) { for (n = 0x00200000; n <= 0x00400000; n <<= 1) { /* Blink LED 7, 8 */ *IOSET0 = n; OSTimeDly(20); *IOCLR0 = n; OSTimeDly(20); i++; q_printf("Task3 "); } } } void Task2 (void *data) { int n, i; char err; data = data; while(1) { for (n = 0x00080000; n <= 0x00100000; n <<= 1) { /* Blink LED 5, 6 */ OSSemPend(printsem, 0, &err); q_printf("Task2 "); 60

OSSemPost(printsem); *IOSET0 = n; OSTimeDly(15); *IOCLR0 = n; OSTimeDly(15); i++; } } } void Task1 (void *data) { int n, i; char err; data = data; while(1) { for (n = 0x00008000; n <= 0x00040000; n <<= 1) { /* Blink LED 1,2, 3 , 4*/ OSSemPend(printsem, 0, &err); q_printf("task1 "); OSSemPost(printsem); *IOSET0 = n; OSTimeDly(20); *IOCLR0 = n; OSTimeDly(20); i++; } } } void startTask(void *data) { printsem = OSSemCreate(1); init_timer(); TaskCreate(Task1, (void *)&TaskData[0], "HiPrioTsk", 1); TaskCreate(Task2, (void *)&TaskData[1], "LoPrioTsk", 2); TaskCreate(Task3, (void *)&TaskData[2], "Task3", 3); while (1) { OSTimeDly(1000); } } 61

int main(void) { *IODIR0 = 0x007F8000; OSInit(); TaskCreate(startTask, (void *)0, "VHiPrioTsk", 0); OSStart(); return 0; }

62

LAB EXERCISE 17: Objective: To write a Program to implement a context switching using RTOS Program Description: This program demonstrates context switching . Output: Strings defined in two different tasks are printed on Hyper Terminal. Header Files: ucos.h, Board.h Part List: 1. OASIS ARM7( TITAN) BOARD 2. RS232 Serial cable 3. 9 Volt DC Power Supply 4. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS TITAN Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Generate .hex file using Triton IDE. 4. Download the .hex file. 5. Put the board in RUN mode and you can observe the output on Hyper Terminal.

63

*****Context Switching using RTOS***** #include<ucos.h> #include<board.h> #define TASK_STK_SIZE 100 #define NO_TASKS 3 void init_timer(); OS_STK TaskStk[NO_TASKS][TASK_STK_SIZE]; //OS_STK TaskStartStk[TASK_STK_SIZE]; void Task1() { while(1) { q_printf("high priority task \n"); OSTimeDly(5); } } void Task2() { while(1) { q_printf("low priority task\n"); OSTimeDly(10); } } int main(void) { init_timer(); OSInit(); TaskCreate(Task1,(void*)0,"Hi",1); TaskCreate(Task2,(void*)0,"Low",2); OSStart(); return 0; }

64

LAB EXERCISE 19: Objective: To write a Program to implement a Process creation using Explorer Board Program Description: This program demonstrates Process creation using Linux on Explorer Board. Output: Strings from two different processes are displayed on Hyper Terminal. Header Files: stdlib.h, stdio.h, unistd.h Part List: 1. OASIS ARM9( Explorer) BOARD 2. RS232 Serial cable 3. 7 Volt DC Power Supply 4. Ethernet Cross cable 5. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS Explorer Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Connect the Ethernet cable. 4. Generate .out file using Triton IDE. 5. Download the .out file using TFTP. 6. Observe the output on Hyper Terminal.

65

*****FORK which creates PID for child process and parent process***** #include <stdlib.h> #include <stdio.h> #include <unistd.h> int main () { pid_t pid; pid = fork(); if(pid == 0) { /* Child process: * When fork() returns 0, we are in * the child process. * Here we count up to ten, one each second. */ int j; for(j=0; j < 10; j++) { printf("child: %d\n", j); sleep(1); } _exit(0); /* Note that we do not use exit() */ } else if(pid > 0) { /* Parent process: * Otherwise, we are in the parent process. * Again we count up to ten. */ int i; for(i=0; i < 10; i++) { printf("parent: %d\n", i); sleep(1); } } else { /* Error handling. */ fprintf(stderr, "couldn't fork"); exit(1); } return 0; }

66

LAB EXERCISE 20: Objective: To write a Program to implement a Message Queue using Explorer Board Program Description: This program demonstrates use of Message Queue using Linux on Explorer Board. Output: Output, as defined in the code, is available on Hyper Terminal. Header Files: stdlib.h, stdio.h, string.h, errno.h, unistd.h, sys/types.h, sys/ipc.h, sys/msg.h Part List: 1. OASIS ARM9(Explorer) BOARD 2. RS232 Serial cable 3. 7 Volt DC Power Supply 4. Ethernet Cross cable 5. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS Explorer Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Connect the Ethernet cable. 4. Generate .out file using Triton IDE. 5. Download the .out file using TFTP. 6. Observe the output.

67

*****Receiver program which contains the Message Queue identifier with 'msgget' and then receive messages until "end" is received***** #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> struct my_msg_st { long int my_msg_type; char some_text[BUFSIZ]; }; int main() { int running = 1; int msgid; struct my_msg_st some_data; long int msg_to_receive = 0; msgid = msgget((key_t)1234, 0666 | IPC_CREAT); if (msgid == -1) { fprintf(stderr, "msgget failed with error: %d\n", errno); exit(EXIT_FAILURE); } while(running) { if (msgrcv(msgid, (void *)&some_data, BUFSIZ, msg_to_receive, 0) == -1) { fprintf(stderr, "msgrcv failed with error: %d\n", errno); exit(EXIT_FAILURE); } printf("You wrote: %s", some_data.some_text); if (strncmp(some_data.some_text, "end", 3) == 0) { running = 0; } } if (msgctl(msgid, IPC_RMID, 0) == -1) { fprintf(stderr, "msgctl(IPC_RMID) failed\n"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }

68

*****Sender program which creates message queue with 'msgget' & then adds the messages to the message queue with msgsnd***** #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define MAX_TEXT 512 struct my_msg_st { long int my_msg_type; char some_text[MAX_TEXT]; }; int main() { int running = 1; struct my_msg_st some_data; int msgid; char buffer[BUFSIZ]; msgid = msgget((key_t)1234, 0666 | IPC_CREAT); if (msgid == -1) { fprintf(stderr, "msgget failed with error: %d\n", errno); exit(EXIT_FAILURE); } while(running) { printf("Enter some text: "); fgets(buffer, BUFSIZ, stdin); some_data.my_msg_type = 1; strcpy(some_data.some_text, buffer); if (msgsnd(msgid, (void *)&some_data, MAX_TEXT, 0) == -1) { fprintf(stderr, "msgsnd failed\n"); exit(EXIT_FAILURE); } if (strncmp(buffer, "end", 3) == 0) { running = 0; } } exit(EXIT_SUCCESS); }

69

LAB EXERCISE 21: Objective: To write a Program to implement a Socket using Explorer Board Program Description: This program demonstrates using a Socket using Linux on Explorer Board. Output:

Header Files: sys/types.h, sys/socket.h, sys/un.h, stdio.h, unistd.h Part List: 1. OASIS ARM9(Explorer) BOARD 2. RS232 Serial cable 3. 7 Volt DC Power Supply 4. Ethernet Cross cable 5. Triton IDE Procedure: 1. Connect 9 V DC Power supply to the OASIS Explorer Board. 2. Connect the Board with the COM port of the PC using the serial cable. 3. Connect the Ethernet cable. 4. Generate .out file using Triton IDE. 5. Download the .out file using TFTP. 6. Observe the output.

70

*****Socket Server Program***** #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <stdio.h> #include <unistd.h> #define ADDRESS "server_socket" /* addr to connect */

int main() { int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_un server_address; struct sockaddr_un client_address; //Remove any old sockets and create an unnamed socket for the serve unlink("server_socket"); server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0); //Name the socket server_address.sun_family = AF_UNIX; strcpy(server_address.sun_path, "server_socket"); server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *)&server_address, server_len); //Create a connection queue and wait for clients listen(server_sockfd, 5); while(1) { char ch; printf ("server waiting\n"); //Accept a connection client_len = sizeof(client_address); client_sockfd = accept(server_sockfd,(struct sockaddrr *)&client_address, &client_len); //Read and write to client on client_sockfd read(client_sockfd, &ch, 1); ch++; write(client_sockfd, &ch, 1); close(client_sockfd); } }

71

*****Socket Client Program***** #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <stdio.h> #include <unistd.h> #define ADDRESS "server_socket" /* addr to connect */ int main() { int sockfd; int len; struct sockaddr_un address; int result; char ch ='A'; // Create a socket for the client sockfd = socket (AF_UNIX, SOCK_STREAM, 0); // Name the socket as agreed with the server address.sun_family = AF_UNIX; strcpy(address.sun_path, "server_socket"); len = sizeof(address); //Connect our socket to server's socket result = connect(sockfd, (struct sockaddr *)&address, len); if (result == -1){ perror("oops: client1"); exit(1); } //You can now read and write via sockfd write(sockfd, &ch, 1); read (sockfd, &ch, 1); printf ("char from server = %c\n", ch); close(sockfd); exit(0); }

72