Thursday, 9 February 2017

RTOS CONCEPT's ARM LPC2148 EMBEDDED 'C' CODES-I

PROGRAM 1:


PROGRAM TO IMPLEMENT  SIMPLE TASK SWITCHING

#include <rtl.h>
#include <lpc21xx.h>
#include <stdio.h>
void init_serial (void);
OS_TID tsk1,tsk2;
char    cnt1,cnt2,test2;
__task void job1 (void);
__task void job2 (void);
char arr1[20],arr2[20];
unsigned int i = 0;
__task void job1 (void)
 {
  os_tsk_prio_self (2);
  while (1)
   {
 os_tsk_create (job2, 1);
             os_evt_wait_or (0x0001, 0x0000);
             cnt1++;
             sprintf(arr1,"counter1 :%d",cnt1);
          while (arr1[i] != '\0')
          {
                    while (!(U0LSR & 0x20));
                    U0THR = arr1[i];
                   i++;
          }
          i=0;
          while (!(U0LSR & 0x20));
          U0THR = '\r';
          if (cnt1==0x05)
          cnt1=0;
          os_dly_wait(1);                                           
    }
}

__task void job2 (void)
 {
  while (1)
   {
          cnt2++;
sprintf(arr2,"counter2 :%d",cnt2);
while (arr2[i] != '\0')
          {
                    while (!(U0LSR & 0x20));
                    U0THR = arr2[i];
                   i++;
          }
          os_dly_wait(3);
          i=0; 
          while (!(U0LSR & 0x20)); 
          U0THR = '\n';       
          if(cnt2==0x0f)
{
 cnt2=0;
os_evt_set (0x0001, tsk1);        

  }
test2=22;
           os_dly_wait(3);
          test2=0;
          os_dly_wait(3);
  }
}
int main (void)
{
cnt1=0;
cnt2=0;
   init_serial ();
  os_sys_init (job1);
  while (1);
}
void init_serial (void) {
  PINSEL0 = 0X0000005;                 // Enable RxD0 and TxD0             
  U0LCR = 0x83;                         // 8 bits, no Parity, 1 Stop bit    
  U0DLL = 0x61;                           // 9600 Baud Rate @ 15MHz VPB Clock 
  U0LCR = 0x03;                         // DLAB = 0                         
}

PROGRAM 2:

PROGRAM TO IMPLEMENT SIMPLE ROUND ROBIN TASK SWITCHING

#include <rtl.h>
#include <lpc21xx.h>
#include <stdio.h>
void init_serial (void);
int counter1;
int counter2;
char arr1[20],arr2[20];
int i=0;
__task void job1 (void);
__task void job2 (void);
__task void job1 (void) {
  os_tsk_create (job2, 0); 
  while (1) {              
    counter1++; 
          sprintf(arr1,"counter1 :%d",counter1);
          while (arr1[i] != '\0')
          {
                    while (!(U0LSR & 0x20));
                    U0THR = arr1[i];
                   i++;
          }
          i=0;
          while (!(U0LSR & 0x20));
          U0THR = '\n';
                    
  }
}
__task void job2 (void) {
  while (1) {               
    counter2++;             

sprintf(arr2,"counter2 :%d",counter2);
          while (arr2[i] != '\0')
          {
                    while (!(U0LSR & 0x20));
                    U0THR = arr2[i];
                   i++;
          }
          i=0;
          while (!(U0LSR & 0x20));
          U0THR = '\n';
  }
}

void main (void)
 {
init_serial();
  os_sys_init (job1);       
  for (;;);
}

void init_serial (void)
{
  PINSEL0 = 0X0000005;                 // Enable RxD0 and TxD0             
  U0LCR = 0x83;                         // 8 bits, no Parity, 1 Stop bit     
  U0DLL = 0x61;                           // 9600 Baud Rate @ 15MHz VPB Clock 
  U0LCR = 0x03;                         // DLAB = 0                         
}

PROGRAM 3:

PROGRAM TO IMPLEMENT  MAIL_BOX

#include <rtl.h>
#include<stdio.h>
#include<lpc214x.h>
void init_serial(void);
os_mbx_declare (MsgBox, 100);              
U32 mpool[8*sizeof(U32) ];       
unsigned int cnt1,cnt2;
char arr1[20],arr2[20];
char arr3[20]="task2deleted";
int i=0;
__task void task2 (void);
__task void task1 (void)
 {
  U32 *mptr;
  os_tsk_create (task2, 0);
  os_mbx_init (MsgBox, sizeof(MsgBox));
  mptr = _alloc_box (mpool);               
while(1)
 {
           while(!(cnt1==15))
                             {
                             cnt1++;

                             sprintf(arr1,"counter1 :%d",cnt1);
                             while (arr1[i] != '\0')
                             {        os_dly_wait(1);
                                       while (!(U0LSR & 0x20));
                                       U0THR = arr1[i];
                                      i++;
                             }
                             i=0;
                             while (!(U0LSR & 0x20));
                             U0THR = '\n';
                              os_dly_wait(5);
                             }
 if (cnt1==15)
                   {
                   mptr[0] = cnt1;
                   cnt1=0;
                   os_mbx_send (MsgBox, mptr, 0xffff); /* Send the count value to a 'Mailbox' */
                   os_dly_wait(5);
    
                    }
           }
   }

__task void task2 (void)
{
  U32 *rptr  ;
  cnt2=0;
           while(1)
           {
           os_mbx_wait (MsgBox, (void**)&rptr, 0xffff);     
         cnt2 = *rptr;    
          while(!(cnt2==30))
          {       
                             cnt2++;
                   sprintf(arr2,"counter2 :%d",cnt2);
                   os_dly_wait(2);
                             while (arr2[i] != '\0')
                             {
                             while (!(U0LSR & 0x20));
                             U0THR = arr2[i];
                             i++;
                             }
                             i=0;
                             while (!(U0LSR & 0x20));
                             U0THR = '\n';
                             os_dly_wait(5);
                      }
                    cnt2=0;
           }
           }

int main (void)
{
 init_serial();
  _init_box (mpool, sizeof(mpool), sizeof(U32));
  os_sys_init(task1);
}
void init_serial (void)
{
  PINSEL0 = 0X0000005;                 // Enable RxD0 and TxD0             
  U0LCR = 0x83;                               // 8 bits, no Parity, 1 Stop bit    
  U0DLL = 0x61;                              // 9600 Baud Rate @ 15MHz VPB Clock 
  U0LCR = 0x03;                             // DLAB = 0                         
}


No comments:

Post a Comment

we are hear to discuss your queries ,so please feel free to ask any of your queries.

Wikipedia

Search results