// test TRT timers with four led blinking tasks #include #include "trtSettings.h" #include "trtkernel644.c" #include "trtTimers.c" #include #include #include // serial communication library // Don't mess with the semaphores #define SEM_RX_ISR_SIGNAL 1 #define SEM_STRING_DONE 2 // user hit #include "trtUart.h" #include "trtUart.c" // UART file descriptor // putchar and getchar are in uart.c FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); // Timer controls #define SEM_TIMER_1 3 #define SEM_TIMER_2 4 #define SEM_TIMER_3 5 #define TIMER_1 1 #define TIMER_2 2 #define TIMER_3 3 // the usual #define begin { #define end } // --- Blink LEDs and run uart --------------------------------- // input arguments to each thread // not actually used in this example int args[4] ; // --- define task 1 ---------------------------------------- void led1(void* args) begin uint32_t rel, dead ; // set up a timer to control task2 trtSetTimer(TIMER_1, SECONDS2TIMERTICK(2.0), PERIODIC, SEM_TIMER_1); trtStartTimer(TIMER_1) ; while(1) begin // blink the led PORTC = PORTC ^ 0x02 ; // Sleep rel = trtCurrentTime() + SECONDS2TICKS(1); dead = trtCurrentTime() + SECONDS2TICKS(1); trtSleepUntil(rel, dead); end end // --- define task 2 ---------------------------------------- void led2(void* args) begin //set up a timer to control task3 trtSetTimer(TIMER_2, SECONDS2TIMERTICK(0.50), PERIODIC, SEM_TIMER_2); while(1) begin // when timer1 times out, toggle run/stop on timer2 trtWait(SEM_TIMER_1); if (trtStatusTimer(TIMER_2) & (1< // Timer synch // each of these semaphores will be signaled by a timer trtCreateSemaphore(SEM_TIMER_1, 0) ; // signal to task2 to execute trtCreateSemaphore(SEM_TIMER_2, 0) ; // signal to task3 to execute trtCreateSemaphore(SEM_TIMER_3, 0) ; // signal to task4 to execute // --- creat tasks ---------------- trtCreateTask(led1, 100, SECONDS2TICKS(1), SECONDS2TICKS(1), &(args[0])); trtCreateTask(led2, 100, SECONDS2TICKS(0.1), SECONDS2TICKS(0.1), &(args[1])); trtCreateTask(led3, 100, SECONDS2TICKS(0.1), SECONDS2TICKS(0.1), &(args[2])); trtCreateTask(led4, 100, SECONDS2TICKS(0.1), SECONDS2TICKS(0.1), &(args[2])); // --- Idle task -------------------------------------- // just sleeps the cpu to save power // every time it executes set_sleep_mode(SLEEP_MODE_IDLE); sleep_enable(); while (1) begin sleep_cpu(); end } // main