/************************************************************************* 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 ================================== Dec 2004 Modified by Bruce Land to include: Class demo code **************************************************************************/ #include #include #include "aos.h" #include "user_tasks.h" #include "aos_core.c" #include "aos_task.c" #include "semaphore.c" //omit if no semaphores or mail used #include "mbox.c" //omit if no mailboxes #include "aos_uart.c" //omit if you don't need UART //global variables for all tasks aos_mailbox *task23_pipe ; /*string from task2 to task3*/ /*shared memory task 1, 3, 4*/ //force the data to be in RAM using pragma AOS_GLOBAL_VAR UWORD sleep_time ; AOS_GLOBAL_END //memory lock semaphore aos_semaphore *sem_memory; //task control block pointer aos_tcb *t1, *t2, *t3, *t4 ; //******************************************************** void task1( void ) { char t1str[32]; //get the current task control block t1 = aos_ctbl.tcb_current; //start 3 tasks aos_task_create( task2, hw_stack2, data_stack2, 100 ); aos_task_create( task3, hw_stack3, data_stack3, 150 ); aos_task_create( task4, hw_stack4, data_stack4, 200 ); //allocate UART structures, turn on UART aos_init_uart(); //init the sleep time -- modifed by task 4 sleep_time = 100; //init portB to output DDRB = 0xff ; //init portC to input DDRC = 0x00; while(1) { //toggle the led PORTB ^= 0x01; //sleep_time is set by task 4 aos_sleep( sleep_time ); //format aOS uptime and print it sprintf(t1str,"Uptime: %ld\n\r",aos_uptime()) ; aos_puts(t1str); //aos_sleep(1); //get task1 stack use and print it 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); } } //******************************************************** void task2( void ) { char t2str[32]; t2 = aos_ctbl.tcb_current; //make a commuinctions channel from task 2 to task 3 task23_pipe = aos_mbox_create(); 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]; t3 = aos_ctbl.tcb_current; //create memory lock sem sem_memory = (aos_semaphore *)aos_sem_create( 1 ); while(1) { //toggle the led PORTB ^= 0x04; //wait for a message msg = aos_mbox_recv(task23_pipe); //and print it aos_puts(msg); //crlf sprintf(crlf,"\n\r"); aos_puts(crlf); // lock memory and modify it if a button is pushed aos_wait(sem_memory); if(PINC.2==0) sleep_time = 100; if(PINC.3==0) sleep_time = 10; aos_signal(sem_memory); } } //******************************************************** void task4( void ) { char str4[32]; t4 = aos_ctbl.tcb_current; while(1) { sprintf(str4,"Task times:\n\r%ld %ld %ld %ld %ld \n\r", t1->ttime,t2->ttime, t3->ttime,t4->ttime,(NULL_TASK->ttime)) ; aos_puts(str4); //Print the dispatches/sec //aos_sleep(1); sprintf(str4,"%ld Dispatch/sec\n\r", aos_ctbl.aos_dispcntr) ; aos_ctbl.aos_dispcntr = 0; //reset for next second aos_puts(str4); aos_sleep(100); //lock and set a shared variable which affects task 1 aos_wait(sem_memory); if(PINC.0==0) sleep_time = 100; if(PINC.1==0) sleep_time = 10; aos_signal(sem_memory); //put task 1 to sleep longer //for one sleep timeout if(PINC.7==0) aos_sleep_task(t1,100); //suspend task 1 if(PINC.5==0) aos_suspend_task(t1); //resume task 1 if(PINC.6==0) aos_resume_task(t1); } } //********************************************************