You are on page 1of 6

EX NO: DATE: IMPLEMENTION OF MAILBOX FUNCTION USING LPC2148 AIM To create a mail

box in Micro C/OS-II using LPC2148 ALGORITHM Initialize the header file If OS_CR
ITICAL_METHOD==3 then it will allocate local storage for variable type OS_CPU_SR
cpu_sr=0 and end if statement Define SW1 and SW2 for Pin 0.10 and 0.11 OS_EVEN
structure created for OSMboxPost() and OSMboxPend() OS_STK is used to create ow
n stack space for task1,and task2 Declare the task functions Initialize variable
i and call LPC2148BSPInit() Set pins 0.12 and 0.13 of port0 as output and call
OSInit() OSTaskCreate() is used to create task1,and task2 with priority so it ca
n be managed by uc/os-II OSMboxCreate() create and initializes mailbox. OSMboxPo
st() sends message to a task through mailbox and call OSStart()
Task1 Initialize variable err, i, and pointer variable msg Initialize LPC2148BSP
Init() Within infinite loop, OSMboxPend() used by the task to receive a message
and assign it in variable msg Assign i=*msg If SW1==0 then increment i and print
the value of i


OS_ENTER_CRITICAL() used to disable processors interrupt and enters into critical
section of code where it waits until SW1==0

If SW1!=0 then it use OS_EXIT_CRITICAL to enable processors interrupt and comes o


ut of critical section of code

OSMboxPost() sends message to a task through mailbox and assign it in variable e
rr OSTimeDlyHMSM used to delay the task for user specified time of 100millisecon
ds
Task2 Initialize variable err, j, and pointer variable msg Within infinite loop,
OSMboxPend() used by the task to receive a message and assign it in variable ms
g Assign j=*msg If SW2==0 then decrement j and print the value of j OS_ENTER_CRI
TICAL() used to disable processors interrupt and enters into critical section of
code where it waits until SW2==0 If SW2!=0 then it use OS_EXIT_CRITICAL to enabl
e processors interrupt and comes out of critical section of code OSMboxPost() sen
ds message to a task through mailbox and assign it in variable err OSTimeDlyHMSM
used to delay the task for user specified time of 50milliseconds
CODING #include "includes.h" #if OS_CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr = 0; #
endif /* Allocate storage for CPU status register*/
#define sw1 IO0PIN_bit.P0_10 #define sw2 IO0PIN_bit.P0_11
OS_EVENT *comm;
OS_STK Task1stk[1000]; OS_STK Task2stk[1000];

void Task1(void *); void Task2(void *);


void Task1(void *pdata) { INT8U err; INT32S i=0; INT32U *msg; LPC2148BSPInit ();
IO0DIR = 0x00000000; while(1) { //printf("AA"); msg = OSMboxPend(comm,0,&err);
i = *msg; if(sw1 == 0) { i++; printf(" i = %d ",i); OS_ENTER_CRITICAL(); while(s
w1 == 0); OS_EXIT_CRITICAL(); } err = OSMboxPost(comm,(void *)&i); OSTimeDlyHMSM
(0,0,0,100); } } void Task2(void *pdata) { INT8U err; INT32U *msg; INT32S j=0; w
hile(1)

{ msg = OSMboxPend(comm,0,&err); j = *msg; if(sw2 == 0) { j = j-1; printf(" j =


%d ",j); OS_ENTER_CRITICAL(); while(sw2 == 0); OS_EXIT_CRITICAL(); } err = OSMbo
xPost(comm,(void *)&j); OSTimeDlyHMSM(0,0,0,50); } } void main() { INT8U i=0; LP
C2148BSPInit (); IO0DIR = 0x00003000; OSInit(); OSTaskCreate(Task1, NULL, &Task1
stk[999], 1); OSTaskCreate(Task2, NULL, &Task2stk[998], 2); comm = OSMboxCreate(
(void *)0); OSMboxPost(comm,(void *)&i); OSStart(); } int putchar(int ch) { if (
ch == \n ) { while (!(U0LSR & 0x20)); U0THR = ch; }

while (!(U0LSR & 0x20)); return (U0THR = ch); }


OUTPUT
RESULT The mailbox was created in Micro C/OS-II and implemented using LPC2148

You might also like