#include #include #include #include #define TERMINATOR_TURTLE 0x01 #define TERMINATOR_RABBIT 0x02 static rtems_id tid_turtle; static rtems_id tid_rabbit; static char shadow_reg_led = 0x00; static char *reg_led = 0x9fff0003; static char *reg_sw = 0x9fff0101; static char turtle_led = 0x01; static char rabbit_led = 0x01; static rtems_id procMutex; static rtems_task turtle(rtems_task_argument ignored) { char sw; struct timespec req, rem; req.tv_sec = 0; req.tv_nsec = (1./4.) * 1000000000L; printf(">>>> start turtle <<<<\n"); for(;;) { if(turtle_led == 0x80) turtle_led = 0x01; else turtle_led <<= 1; rtems_semaphore_obtain(procMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT); shadow_reg_led = turtle_led | rabbit_led; output_byte(reg_led, shadow_reg_led); rtems_semaphore_release(procMutex); sw = input_byte(reg_sw); if(!(sw & TERMINATOR_TURTLE)) break; nanosleep(&req, &rem); } printf(">>>> finish turtle <<<<\n"); rtems_task_delete(RTEMS_SELF); } static rtems_task rabbit(rtems_task_argument ignored) { char sw; struct timespec req, rem; req.tv_sec = 0; req.tv_nsec = (1./16.) * 1000000000L; printf(">>>> start rabbit <<<<\n"); for(;;) { if(rabbit_led == 0x80) rabbit_led = 0x01; else rabbit_led <<= 1; rtems_semaphore_obtain(procMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT); shadow_reg_led = turtle_led | rabbit_led; output_byte(reg_led, shadow_reg_led); rtems_semaphore_release(procMutex); sw = input_byte(reg_sw); if(!(sw & TERMINATOR_RABBIT)) break; nanosleep(&req, &rem); } printf(">>>> finish rabbit <<<<\n"); rtems_task_delete(RTEMS_SELF); } rtems_task tr_init(rtems_task_argument ignored) { printf(">>>> Init for turtle and rabbit <<<<\n"); rtems_semaphore_create(rtems_build_name('M', 'U', 'T', 'X'), 1, RTEMS_FIFO | RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL, 0, &procMutex); rtems_task_create(rtems_build_name('T', 'U', 'R', 'T'), 200, 16*1024, RTEMS_PREEMPT | RTEMS_TIMESLICE | RTEMS_NO_ASR | RTEMS_INTERRUPT_LEVEL(0), RTEMS_FLOATING_POINT | RTEMS_LOCAL, &tid_turtle); rtems_task_start(tid_turtle, turtle, 0); rtems_task_create(rtems_build_name('R', 'A', 'B', 'T'), 200, 16*1024, RTEMS_PREEMPT | RTEMS_TIMESLICE | RTEMS_NO_ASR | RTEMS_INTERRUPT_LEVEL(0), RTEMS_FLOATING_POINT | RTEMS_LOCAL, &tid_rabbit); rtems_task_start(tid_rabbit, rabbit, 0); printf(">>> Finish Init <<<<\n"); }