0% found this document useful (0 votes)
92 views2 pages

Osed

This document describes using semaphores to control access to a shared serial interface between two tasks in an RTOS. Task 1 and Task 2 each wait for a semaphore to gain access to the serial interface, print a message, and then release the semaphore. The semaphore ensures only one task can access the serial interface at a time to avoid a conflict.

Uploaded by

Shrinidhi K T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views2 pages

Osed

This document describes using semaphores to control access to a shared serial interface between two tasks in an RTOS. Task 1 and Task 2 each wait for a semaphore to gain access to the serial interface, print a message, and then release the semaphore. The semaphore ensures only one task can access the serial interface at a time to avoid a conflict.

Uploaded by

Shrinidhi K T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

5) Semaphore: __task void task2 (void) {

#include <RTL.h>

#include <LPC21xx.h> while (1)

#include <stdio.h> {

__task void task2(void); /* Wait indefinetly for a free


semaphore */
void init_serial (void);
os_sem_wait (semaphore1, 0x0e);

/* OK, the serial interface is free


unsigned char msg1[]="task1\r\n", now, use it. */
msg2[]="task2\n";
//printf ("Task 2 \n");
unsigned int j,i=0;
while (msg2[i] != '\0')
OS_TID tsk1, tsk2;
{
OS_SEM semaphore1;
while (!(U0LSR &
__task void task1 (void) { 0x20));
OS_RESULT ret; U0THR = msg2[i];

i++;
os_tsk_prio_self(2); }
tsk2 = os_tsk_create (task2, i=0;
1);
os_sem_send (semaphore1);
while (1) {
os_tsk_prio_self(4);
// os_dly_wait(3);
}
/* Wait 1 ticks for the free
semaphore */ }

ret = os_sem_wait (semaphore1, void init_serial (void) {


0x0e); PINSEL0 = 0X0000005;
if (ret == OS_R_SEM) U0LCR = 0x83;
{ U0DLL = 0x61;
while (msg1[i] != '\0') U0LCR = 0x03;
{ }
while (!(U0LSR & int main (void)
0x20));
{
U0THR = msg1[i];
init_serial();
i++;
os_sem_init (semaphore1, 1);
}

i=0;
os_sys_init (task1);
os_sem_send (semaphore1);
}
}

}
1) Event_basics: 2) Prio_basics: 4) Round Robin with event:
#include <rtl.h> #include <rtl.h> #include <rtl.h>

#include <lpc21xx.h> #include <lpc21xx.h> #include <lpc21xx.h>

#include <stdio.h> #include <stdio.h> #include <stdio.h>

OS_TID tsk1,tsk2; //declare task OS_TID tsk1,tsk2; OS_TID tsk1,tsk2,tsk3;


identification number variables
int cnt1,cnt2, i; OS_RESULT result1,result2,result3;
OS_RESULT RE1,RE2;
__task void job1 (void); int cnt1,cnt2,cnt3,i;
int cnt1,cnt2,i,cnt3;
__task void job2 (void); __task void job1 (void);
__task void job1 (void); //declare
__task void job1 (void) { __task void job2 (void);
function for job1
os_tsk_prio_self (2); __task void job3 (void);
__task void job2 (void); //declare
tsk2=os_tsk_create (job2, 0);
function for job2 __task void job1 (void)
while (1) { cnt1++;cnt1++;} }
__task void job1 (void) { {
__task void job2 (void)
os_tsk_prio_self (2); tsk1=os_tsk_self();
{ while (1) {cnt2++;} }
tsk1=os_tsk_self(); os_tsk_prio_self (1);
int main (void) {cnt1=0; cnt2=0;
os_tsk_create (job2, 1); while os_sys_init (job1); while (1);} tsk2= os_tsk_create (job2, 0);
(1){
3) Round_Robin: tsk3=os_tsk_create (job3, 0); while (1) {
RE1=os_evt_wait_and (0x0001, 0x0001);
#include <rtl.h> int counter1; os_evt_wait_or(0x0001,0x0010);

if (RE1== OS_R_EVT) int counter2; for (i=0; i<15; i++) { cnt1++; }


os_dly_wait(10);
{for(i=0;i<15;i++) __task void job1 (void);
os_evt_set (0x0002, tsk2);}}
{cnt1++; __task void job2 (void);
__task void job2 (void){while (1){
for(i=0;i<65000;i++);(write 4 times)} __task void job1 (void) {
os_evt_wait_or (0x0002, 0x0020);
os_dly_wait(1); } os_tsk_create (job2, 0);
for (i=0; i<15; i++) { cnt2++;}
else if (RE1== OS_R_TMO) while (1) { counter1++; }}
os_dly_wait(10);
{for(i=0;i<15;i++) __task void job2 (void) {
os_evt_set (0x0003, tsk3);}}
{ cnt3++;for(i=0;i<65000;i++);(write 4 while (1) { counter2++; }}
times) __task void job3 (void){while (1)
void main (void) {
//os_dly_wait(1); }}} {os_evt_wait_or (0x0003, 0x0030);
os_sys_init (job1);
__task void job2 (void) { for (i=0;i<15;i++){ cnt3++;
for (;;); }os_dly_wait(10);
while (1) { for(i=0;i<15;i++) } os_evt_set (0x0001, tsk1);}}
{cnt2++; for(i=0;i<65000;i++);(4 times)}
int main (void) {cnt1=0;cnt2=0;cnt3=0;
//os_dly_wait(1);
os_sys_init (job1);
os_evt_set (0x0001, tsk1); }}int main
(void) while (1);

{cnt1=0; cnt2=0; }

os_sys_init (job1); while (1);}

You might also like