Implemention of Mailbox Function Using LPC2148
Implemention of Mailbox Function Using LPC2148
Implemention of Mailbox Function Using LPC2148
Initialize the header file If OS_CRITICAL_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_EVENT structure created for OSMboxPost() and OSMboxPend() OS_STK is used to create own 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 can be managed by uc/os-II OSMboxCreate() create and initializes mailbox. OSMboxPost() sends message to a task through mailbox and call OSStart()
Task1 Initialize variable err, i, and pointer variable msg Initialize LPC2148BSPInit() 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 out of critical section of code
OSMboxPost() sends message to a task through mailbox and assign it in variable err OSTimeDlyHMSM used to delay the task for user specified time of 100milliseconds
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 msg Assign j=*msg If SW2==0 then decrement j and print the value of j OS_ENTER_CRITICAL() 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 enable processors interrupt and comes out of critical section of code OSMboxPost() sends 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*/
OS_EVENT *comm;
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(sw1 == 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; while(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 = OSMboxPost(comm,(void *)&j); OSTimeDlyHMSM(0,0,0,50); } } void main() { INT8U i=0; LPC2148BSPInit (); IO0DIR = 0x00003000; OSInit(); OSTaskCreate(Task1, NULL, &Task1stk[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; }
OUTPUT
RESULT The mailbox was created in Micro C/OS-II and implemented using LPC2148