/************************************************************************* This file is part of aOS. aOS is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. aOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with aOS; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Copyright 2001, 2002 Anssi Ylätalo ================================== March 2005 Modified by Bruce Land to include: All functions in aOS + my extensions **************************************************************************/ #include #include #include "aos.h" #include "user_tasks.h" #include "aos_core.c" #include "aos_task.c" #include "aos_semaphore.c" //omit if no semaphores or mail used #include "aos_mbox.c" //omit if no mailboxes #include "aos_uart.c" //omit if you don't need UART //global variables for all tasks //force the data to be in RAM using pragma #pragma regalloc- aos_mailbox *task23_pipe ; /*string from task2 to task3*/ aos_mailbox *task11_pipe ; /*string from task1 to itself*/ /*shared memory task 1, 3, 4*/ UWORD sleep_time ; //memory lock semaphore aos_semaphore *sem_memory; //task control block pointer aos_tcb *t1, *t2, *t3, *t4 ; //allow register vars again #pragma regalloc+ //******************************************************** void task1( void ) { char t1str[32]; char t1astr[20]; char*msg ; //get the current task control block t1 = aos_ctbl.tcb_current; //start 3 tasks and retrun control block pointers t2=aos_task_create( task2, hw_stack2, data_stack2, 100 ); t3=aos_task_create( task3, hw_stack3, data_stack3, 150 ); t4=aos_task_create( task4, hw_stack4, data_stack4, 200 ); //allocate UART structures, turn on UART //parameter is baud rate given as an integer //This routine uses the clock value set in the //Project...Config dialog!! aos_init_uart(9600); //This is a test to make sure the system does not fail //and reboot itself //printf("Starting..."); // while (PINC.0==1){}; //init the sleep time -- modifed by tasks 3 and 4 sleep_time = 100; //init portB to output DDRB = 0xff ; PORTB = 0xff; //init portC to input DDRC = 0x00; //make a commuinctions channel from task 1 to itself task11_pipe = aos_mbox_create(); //make a commuinctions channel from task 2 to task 3 task23_pipe = aos_mbox_create(); //create memory lock sem sem_memory = (aos_semaphore *)aos_sem_create( 1 ); while(1) { //toggle the led PORTB ^= 0x01; //sleep_time is set by task 3 or 4 aos_sleep( sleep_time ); //format aOS uptime and send it if button is pushed if (PINC.2==0){ sprintf(t1astr,"Uptime: %ld\n\r",aos_uptime()) ; aos_mbox_send(task11_pipe,t1astr); } //The accept is nonblocking msg = aos_mbox_accept(task11_pipe); if (msg!=NULL) aos_puts(msg); //get task4 stack sizes sprintf(t1str,"Hstk Dstk task1: %ld %ld\n\r\n\r", aos_task_hstk_chck(t1),aos_task_dstk_chck(t1)) ; aos_puts(t1str); //suspends itself!! if(PINC.4==0) aos_suspend_task(t1); // lock memory and modify it if a button is pushed //aos_wait(sem_memory); if(PINC.1==0) sleep_time = 10; //aos_signal(sem_memory); } } //******************************************************** void task2( void ) { char t2str[32]; while(1) { //toggle the led PORTB ^= 0x02; //read user input aos_gets(t2str); //and send it aos_mbox_send(task23_pipe,t2str); } } //******************************************************** void task3( void ) { char *msg; char crlf[4]; while(1) { //toggle the led PORTB ^= 0x04; //wait for a message msg = aos_mbox_recv(task23_pipe); //and print it aos_puts(msg); sprintf(crlf,"\n\r"); aos_puts(crlf); } } //******************************************************** void task4( void ) { char str4[40]; long tt1, tt2, tt3, tt4, tnull; while(1) { tt1=aos_task_gettime(t1); tt2=aos_task_gettime(t2); tt3=aos_task_gettime(t3); tt4=aos_task_gettime(t4); //get nulltask/sec tnull=aos_task_gettime(NULL_TASK); aos_task_settime(NULL_TASK,0); sprintf(str4,"Task times:\n\r%ld %ld %ld %ld %ld \n\r", tt1,tt2,tt3,tt4,tnull); // t1->ttime,t2->ttime, // t3->ttime,t4->ttime,(NULL_TASK->ttime)) ; aos_puts(str4); //Print the dispatches/sec sprintf(str4,"%ld Dispatch/sec\n\r", aos_getdispatch()) ; aos_setdispatch(0); //reset for next second aos_puts(str4); //Print status of task 2 sprintf(str4,"%d task2 status\n\r", aos_task_getstatus(t2)) ; aos_puts(str4); aos_sleep(100); //toggle the led PORTB ^= 0x08; //check and set a shared variable which affects task 1 //using aos_sem_accept means that the process does //not block if the semaphore is in use //if(aos_sem_accept(sem_memory)) { if(PINC.0==0) sleep_time = 100; //aos_signal(sem_memory); } //put task 1 to sleep longer //for one sleep timeout if(PINC.7==0) aos_sleep_task(t1,1000); //resume task 1 from suspension if(PINC.6==0) aos_resume_task(t1); //unsleep task 1 if(PINC.5==0) aos_unsleep_task(t1); //toggle scheduler if(PINC.3==0) aos_sched_lock() ; if(PINC.2==0) aos_sched_unlock() ; } } //********************************************************