sl@0: // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\personality\example\main.cpp sl@0: // Test code for example RTOS personality. sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #ifdef __cplusplus sl@0: extern "C" { sl@0: #endif sl@0: sl@0: #define OC_TASK 0 sl@0: #define L2_TASK 1 sl@0: #define RR_TASK 2 sl@0: #define NONEXISTENT_TASK 3 sl@0: #define TM_TASK 4 sl@0: #define TASK1 6 sl@0: #define TASK2 7 sl@0: #define TASK3 8 sl@0: #define TASK4 9 sl@0: #define L1_TASK 10 sl@0: sl@0: void oo_overall_control(void); sl@0: void l1_task_entry(void); sl@0: void l2_task_entry(void); sl@0: void rr_task_entry(void); sl@0: void tm_task_entry(void); sl@0: void task1_entry(void); sl@0: void task2_entry(void); sl@0: void task3_entry(void); sl@0: void task4_entry(void); sl@0: sl@0: typedef void (*isr_entry)(unsigned); sl@0: sl@0: extern int start_random_isr(isr_entry vector); sl@0: extern void stop_random_isr(void); sl@0: sl@0: const taskinfo task_list[] = sl@0: { sl@0: sl@0: /* entry_pt, priority, stack_size, task_id, auto_start */ sl@0: sl@0: { &oo_overall_control, 120, 1024, OC_TASK, 1 }, sl@0: { &l2_task_entry, 236, 1024, L2_TASK, 0 }, sl@0: { &rr_task_entry, 224, 1024, RR_TASK, 0 }, sl@0: { &tm_task_entry, 240, 1024, TM_TASK, 0 }, sl@0: { &task1_entry, 112, 1024, TASK1, 0 }, sl@0: { &task2_entry, 112, 1024, TASK2, 0 }, sl@0: { &task3_entry, 112, 1024, TASK3, 0 }, sl@0: { &task4_entry, 112, 1024, TASK4, 0 }, sl@0: { &l1_task_entry, 244, 1024, L1_TASK, 0 }, sl@0: /* terminator */ sl@0: { 0, 0, 0, 0, 0 } sl@0: }; sl@0: sl@0: const poolinfo pool_list[] = sl@0: { sl@0: /* block size, block count */ sl@0: { 32, 256 }, sl@0: { 64, 256 }, sl@0: { 128, 128 }, sl@0: { 256, 64 }, sl@0: { 512, 32 }, sl@0: /* terminator */ sl@0: { 0, 0 } sl@0: }; sl@0: sl@0: const int timer_count = 8; sl@0: const int semaphore_count = 2; sl@0: sl@0: #define TM_TIMER 0 sl@0: sl@0: #define TM_INIT_DELAY 1000 sl@0: #define TM_PERIOD 2 sl@0: sl@0: volatile unsigned next_random_id = 0; sl@0: volatile unsigned random_sem_signal_interval = 0; sl@0: volatile unsigned random_sem_signal_count = 0; sl@0: volatile unsigned random_send_interval = 0; sl@0: volatile unsigned random_send_count = 0; sl@0: volatile unsigned tmcount = 0; sl@0: volatile int t1func = 0; sl@0: volatile int t2func = 0; sl@0: volatile int t3func = 0; sl@0: volatile int t4func = 0; sl@0: sl@0: #define TEST_SEM 0 sl@0: #define ISR_SEM 1 sl@0: sl@0: #define MSG_ID_INIT 1 sl@0: #define MSG_ID_RUN 2 sl@0: #define MSG_ID_RUN_P 3 sl@0: #define MSG_ID_RND_ISR 4 sl@0: #define MSG_ID_DONE 5 sl@0: #define MSG_ID_DATA 6 sl@0: #define MSG_ID_FLUSH 7 sl@0: #define MSG_ID_SEM_RPT 8 sl@0: #define MSG_ID_RCV_RPT 9 sl@0: #define MSG_ID_TM_RPT 10 sl@0: sl@0: typedef struct _run_msg sl@0: { sl@0: msghdr header; sl@0: int task_id; sl@0: unsigned tmcount; sl@0: int parameter; sl@0: } run_msg; sl@0: sl@0: typedef struct _random_isr_msg sl@0: { sl@0: msghdr header; sl@0: unsigned random_isr_number; sl@0: unsigned extra; sl@0: } random_isr_msg; sl@0: sl@0: typedef struct _data_msg sl@0: { sl@0: msghdr header; sl@0: int length; sl@0: unsigned char checksum; sl@0: unsigned char data[1]; sl@0: } data_msg; sl@0: sl@0: typedef struct _report_msg sl@0: { sl@0: msghdr header; sl@0: int pad; sl@0: unsigned count; sl@0: unsigned ok_count; sl@0: unsigned bad_count; sl@0: } report_msg; sl@0: sl@0: void busy_wait(unsigned ticks) sl@0: { sl@0: unsigned t0 = tmcount; sl@0: while ((tmcount - t0) < ticks) sl@0: {} sl@0: } sl@0: sl@0: void send_run_signal() sl@0: { sl@0: run_msg* m = (run_msg*)alloc_mem_block(sizeof(run_msg)); sl@0: assert(m); sl@0: m->header.msg_id = MSG_ID_RUN; sl@0: m->task_id = current_task_id(); sl@0: m->tmcount = tmcount; sl@0: int r = send_msg(OC_TASK, &m->header); sl@0: assert(r == OK); sl@0: } sl@0: sl@0: void send_run_signal_p(int parameter) sl@0: { sl@0: run_msg* m = (run_msg*)alloc_mem_block(sizeof(run_msg)); sl@0: assert(m); sl@0: m->header.msg_id = MSG_ID_RUN_P; sl@0: m->task_id = current_task_id(); sl@0: m->tmcount = tmcount; sl@0: m->parameter = parameter; sl@0: int r = send_msg(OC_TASK, &m->header); sl@0: assert(r == OK); sl@0: } sl@0: sl@0: void tsend_run_signal_p(int task_id, int parameter) sl@0: { sl@0: run_msg* m = (run_msg*)alloc_mem_block(sizeof(run_msg)); sl@0: assert(m); sl@0: m->header.msg_id = MSG_ID_RUN_P; sl@0: m->task_id = current_task_id(); sl@0: m->tmcount = tmcount; sl@0: m->parameter = parameter; sl@0: int r = send_msg(task_id, &m->header); sl@0: assert(r == OK); sl@0: } sl@0: sl@0: void check_no_signal() sl@0: { sl@0: msghdr* m = NULL; sl@0: int r = recv_msg(&m, NO_WAIT); sl@0: assert(r == TIMED_OUT); sl@0: } sl@0: sl@0: unsigned check_for_signal(int task_id) sl@0: { sl@0: msghdr* m = NULL; sl@0: int r = recv_msg(&m, NO_WAIT); sl@0: assert(r == OK); sl@0: assert(m->msg_id == MSG_ID_RUN); sl@0: run_msg* rm = (run_msg*)m; sl@0: assert(rm->task_id == task_id); sl@0: unsigned tmc = rm->tmcount; sl@0: free_mem_block(m); sl@0: return tmc; sl@0: } sl@0: sl@0: int check_for_signal_p(int task_id, int task_id2, unsigned* pt) sl@0: { sl@0: msghdr* m = NULL; sl@0: int r = recv_msg(&m, NO_WAIT); sl@0: assert(r == OK); sl@0: assert(m->msg_id == MSG_ID_RUN_P); sl@0: run_msg* rm = (run_msg*)m; sl@0: assert(rm->task_id == task_id); sl@0: assert(m->sending_task_id == task_id2); sl@0: r = rm->parameter; sl@0: if (pt) sl@0: *pt = rm->tmcount; sl@0: free_mem_block(m); sl@0: return r; sl@0: } sl@0: sl@0: int wait_for_signal_p(int task_id, unsigned* pt) sl@0: { sl@0: msghdr* m = NULL; sl@0: int r = recv_msg(&m, WAIT_FOREVER); sl@0: assert(r == OK); sl@0: assert(m->msg_id == MSG_ID_RUN_P); sl@0: run_msg* rm = (run_msg*)m; sl@0: assert(rm->task_id == task_id); sl@0: r = rm->parameter; sl@0: if (pt) sl@0: *pt = rm->tmcount; sl@0: free_mem_block(m); sl@0: return r; sl@0: } sl@0: sl@0: void resume_4(int t1, int t2, int t3, int t4) sl@0: { sl@0: if (t1>=0) sl@0: assert(resume_task(t1)==OK); sl@0: if (t2>=0) sl@0: assert(resume_task(t2)==OK); sl@0: if (t3>=0) sl@0: assert(resume_task(t3)==OK); sl@0: if (t4>=0) sl@0: assert(resume_task(t4)==OK); sl@0: } sl@0: sl@0: void check_signal_4(int t1, int t2, int t3, int t4) sl@0: { sl@0: if (t1>=0) sl@0: check_for_signal(t1); sl@0: else sl@0: check_no_signal(); sl@0: if (t2>=0) sl@0: check_for_signal(t2); sl@0: else sl@0: check_no_signal(); sl@0: if (t3>=0) sl@0: check_for_signal(t3); sl@0: else sl@0: check_no_signal(); sl@0: if (t4>=0) sl@0: check_for_signal(t4); sl@0: else sl@0: check_no_signal(); sl@0: } sl@0: sl@0: void check_for_multiple_signals(int task_id, int count) sl@0: { sl@0: unsigned t = check_for_signal(task_id); sl@0: while (--count) sl@0: { sl@0: unsigned t2 = check_for_signal(task_id); sl@0: assert(t2 - t >= 1); sl@0: t = t2; sl@0: } sl@0: } sl@0: sl@0: int flush_signals(void) sl@0: { sl@0: int c = 0; sl@0: for (;;) sl@0: { sl@0: msghdr* m = NULL; sl@0: int r = recv_msg(&m, NO_WAIT); sl@0: if (r == TIMED_OUT) sl@0: break; sl@0: assert(r == OK); sl@0: assert(m->msg_id == MSG_ID_RUN); sl@0: free_mem_block(m); sl@0: ++c; sl@0: } sl@0: return c; sl@0: } sl@0: sl@0: void test_mem_pool(size_t size, int count, void** chain) sl@0: { sl@0: int i, fill; sl@0: void *b, *bb, *c; sl@0: c = *chain; sl@0: for (i=0; i>5); sl@0: fill += 29; sl@0: fill *= fill; sl@0: fill &= 0xff; sl@0: memset(b, fill, size); sl@0: *(void**)b = c; sl@0: ((int*)b)[1] = (int)size; sl@0: c = b; sl@0: } sl@0: bb = alloc_mem_block(size); sl@0: assert(bb == NULL); sl@0: *chain = c; sl@0: } sl@0: sl@0: void check_blocks(void* chain) sl@0: { sl@0: void* p = chain; sl@0: while (p) sl@0: { sl@0: unsigned char *q, *qq; sl@0: int size, fill, x; sl@0: size = ((int*)p)[1]; sl@0: fill = (size>>5)+29; sl@0: fill = (fill*fill)&0xff; sl@0: q = (unsigned char*)p + sizeof(void*) + sizeof(int); sl@0: qq = (unsigned char*)p + size; sl@0: x = 0; sl@0: while (qblock_size; ++pi) sl@0: { sl@0: nblocks += pi->block_count; sl@0: test_mem_pool(pi->block_size, pi->block_count, &chain); sl@0: } sl@0: check_blocks(chain); sl@0: nfreed = free_blocks(chain); sl@0: assert(nfreed == nblocks); sl@0: chain = NULL; sl@0: for (--pi; pi >= pool_list; --pi) sl@0: test_mem_pool(pi->block_size, pi->block_count, &chain); sl@0: check_blocks(chain); sl@0: nfreed = free_blocks(chain); sl@0: assert(nfreed == nblocks); sl@0: chain = NULL; sl@0: kprintf("Memory Manager Test OK"); sl@0: } sl@0: sl@0: void test_suspend_1(void) sl@0: { sl@0: unsigned t1, t2, t3; sl@0: int r; sl@0: t1 = tmcount; sl@0: delay(5*TM_PERIOD); sl@0: t2 = tmcount; sl@0: assert( ((int)t2)-((int)t1) >= 5 ); sl@0: r = suspend_task(TM_TASK); sl@0: assert(r == OK); sl@0: t1 = tmcount; sl@0: delay(5*TM_PERIOD); sl@0: t2 = tmcount; sl@0: assert(t2==t1); sl@0: r = resume_task(TM_TASK); sl@0: assert(r == OK); sl@0: t3 = tmcount; sl@0: assert( ((int)t3)-((int)t2) >= 5 ); sl@0: sl@0: r = suspend_task(TM_TASK); sl@0: assert(r == OK); sl@0: r = suspend_task(TM_TASK); sl@0: assert(r == OK); sl@0: t1 = tmcount; sl@0: delay(5*TM_PERIOD); sl@0: t2 = tmcount; sl@0: assert(t2==t1); sl@0: r = resume_task(TM_TASK); sl@0: assert(r == OK); sl@0: t3 = tmcount; sl@0: assert(t3==t2); sl@0: r = resume_task(TM_TASK); sl@0: assert(r == OK); sl@0: t3 = tmcount; sl@0: assert( ((int)t3)-((int)t2) >= 5 ); sl@0: sl@0: r = suspend_task(-1); sl@0: assert(r == BAD_TASK_ID); sl@0: r = suspend_task(300); sl@0: assert(r == BAD_TASK_ID); sl@0: r = suspend_task(NONEXISTENT_TASK); sl@0: assert(r == BAD_TASK_ID); sl@0: r = resume_task(-1); sl@0: assert(r == BAD_TASK_ID); sl@0: r = resume_task(300); sl@0: assert(r == BAD_TASK_ID); sl@0: r = resume_task(NONEXISTENT_TASK); sl@0: assert(r == BAD_TASK_ID); sl@0: sl@0: kprintf("test_suspend_1 OK"); sl@0: } sl@0: sl@0: void test_priority_scheduling(void) sl@0: { sl@0: int init_pri = get_task_priority(current_task_id()); sl@0: resume_4(TASK1, TASK2, TASK3, TASK4); sl@0: delay(80*TM_PERIOD); sl@0: check_for_multiple_signals(TASK1, 50); // check no timeslicing sl@0: assert(flush_signals()<=31); sl@0: suspend_task(TASK1); sl@0: delay(80*TM_PERIOD); sl@0: check_for_multiple_signals(TASK2, 50); // check no timeslicing sl@0: assert(flush_signals()<=31); sl@0: suspend_task(TASK2); sl@0: delay(80*TM_PERIOD); sl@0: check_for_multiple_signals(TASK3, 50); // check no timeslicing sl@0: assert(flush_signals()<=31); sl@0: suspend_task(TASK3); sl@0: delay(1); sl@0: check_for_signal(TASK4); sl@0: assert(flush_signals()<=1); sl@0: sl@0: t1func = 1; sl@0: t2func = 1; sl@0: t3func = 1; sl@0: t4func = 1; sl@0: sl@0: resume_4(TASK1, TASK2, TASK3, TASK4); sl@0: delay(10); sl@0: flush_signals(); sl@0: sl@0: resume_4(TASK3, TASK2, TASK4, TASK1); sl@0: delay(10); sl@0: check_signal_4(TASK3, TASK2, TASK4, TASK1); sl@0: check_no_signal(); sl@0: resume_4(TASK1, TASK2, TASK3, TASK4); sl@0: check_no_signal(); // all lower priority so don't run sl@0: set_task_priority(TASK2, 255); // higher than current task so run immediately sl@0: check_for_signal(TASK2); sl@0: set_task_priority(TASK4, 116); sl@0: check_no_signal(); // all lower priority so don't run sl@0: delay(10); sl@0: check_for_signal(TASK4); sl@0: check_for_signal(TASK1); sl@0: check_for_signal(TASK3); sl@0: set_task_priority(TASK1, 116); sl@0: set_task_priority(TASK2, 116); sl@0: set_task_priority(TASK3, 116); sl@0: set_task_priority(TASK4, 116); sl@0: resume_4(TASK1, TASK2, TASK3, TASK4); sl@0: set_task_priority(current_task_id(), 112); // drop current task priority sl@0: assert(get_task_priority(current_task_id())==112); sl@0: check_signal_4(TASK1, TASK2, TASK3, TASK4); sl@0: set_task_priority(current_task_id(), init_pri); sl@0: assert(get_task_priority(current_task_id())==init_pri); sl@0: sl@0: kprintf("test_priority_scheduling OK"); sl@0: } sl@0: sl@0: unsigned sem_test(int task_id) sl@0: { sl@0: int r = semaphore_signal(TEST_SEM); sl@0: assert(r==OK); sl@0: return check_for_signal(task_id); sl@0: } sl@0: sl@0: unsigned sem_test_p(int task_id, int parameter) sl@0: { sl@0: unsigned t; sl@0: int r = semaphore_signal(TEST_SEM); sl@0: assert(r==OK); sl@0: r = check_for_signal_p(task_id, task_id, &t); sl@0: assert(r == parameter); sl@0: return t; sl@0: } sl@0: sl@0: unsigned sem_test_pt(int task_id, int parameter) sl@0: { sl@0: unsigned t; sl@0: int r = semaphore_signal(TEST_SEM); sl@0: assert(r==OK); sl@0: r = check_for_signal_p(task_id, task_id, &t); sl@0: assert(r == parameter); sl@0: return t; sl@0: } sl@0: sl@0: void test_semaphore(void) sl@0: { sl@0: unsigned t1, t2, t3; sl@0: int r; sl@0: int init_pri = get_task_priority(current_task_id()); sl@0: set_task_priority(TASK1, 128); sl@0: set_task_priority(TASK2, 128); sl@0: set_task_priority(TASK3, 128); sl@0: set_task_priority(TASK4, 128); sl@0: t1func = 2; sl@0: t2func = 2; sl@0: t3func = 2; sl@0: t4func = 2; sl@0: resume_4(TASK1, TASK2, TASK3, TASK4); sl@0: delay(10); // let tasks wait on semaphore sl@0: check_no_signal(); sl@0: sem_test(TASK1); // test they are released in same order sl@0: sem_test(TASK2); sl@0: sem_test(TASK3); sl@0: sem_test(TASK4); sl@0: check_no_signal(); sl@0: set_task_priority(TASK3, 132); // test highest priority is released first sl@0: sem_test(TASK3); sl@0: sem_test(TASK3); sl@0: suspend_task(TASK3); // test suspended task doesn't contend for semaphore sl@0: sem_test(TASK1); sl@0: sem_test(TASK2); sl@0: sem_test(TASK4); sl@0: sem_test(TASK1); sl@0: suspend_task(TASK2); sl@0: sem_test(TASK4); sl@0: sem_test(TASK1); sl@0: sem_test(TASK4); sl@0: set_task_priority(TASK2, 136); // change priority while suspended sl@0: sem_test(TASK1); sl@0: sem_test(TASK4); sl@0: sem_test(TASK1); sl@0: resume_task(TASK2); sl@0: sem_test(TASK2); sl@0: sem_test(TASK2); // test new highest priority task acquires semaphore first sl@0: delay(100*TM_PERIOD); sl@0: check_no_signal(); // check waits don't time out sl@0: sl@0: t2func = 3; // switch over to timed waits for task 2 sl@0: t1 = sem_test(TASK2); // get one last message of previous type sl@0: delay(5*TM_PERIOD); sl@0: t2 = sem_test_p(TASK2, OK); // signal after half the timeout and check OK sl@0: delay(11*TM_PERIOD); // wait for > timeout sl@0: r = check_for_signal_p(TASK2, TASK2, &t3); sl@0: assert(r == TIMED_OUT); sl@0: kprintf("t2-t1=%d t3-t2=%d", t2-t1, t3-t2); sl@0: assert(t2-t1 >= 5); sl@0: assert(t3-t2 >= 10); sl@0: sem_test_p(TASK2, OK); sl@0: resume_task(TASK3); sl@0: sl@0: set_task_priority(current_task_id(), 176); // raise current task priority sl@0: semaphore_signal(TEST_SEM); // signal semaphore 4 times - should release all 4 waiting threads sl@0: semaphore_signal(TEST_SEM); sl@0: semaphore_signal(TEST_SEM); sl@0: semaphore_signal(TEST_SEM); sl@0: set_task_priority(current_task_id(), init_pri); // let tasks run sl@0: r = check_for_signal_p(TASK2, TASK2, NULL); sl@0: assert(r == OK); sl@0: check_for_signal(TASK3); sl@0: check_for_signal(TASK4); sl@0: check_for_signal(TASK1); sl@0: set_task_priority(current_task_id(), 176); // raise current task priority sl@0: busy_wait(11); // let semaphore wait time out sl@0: t1func = 4; // switch all threads over sl@0: t2func = 4; // sl@0: t3func = 4; // sl@0: t4func = 4; // sl@0: semaphore_signal(TEST_SEM); // signal semaphore 3 times - should release other 3 waiting threads sl@0: semaphore_signal(TEST_SEM); sl@0: semaphore_signal(TEST_SEM); sl@0: set_task_priority(current_task_id(), init_pri); // let tasks run sl@0: r = check_for_signal_p(TASK2, TASK2, NULL); sl@0: assert(r == TIMED_OUT); sl@0: check_for_signal(TASK3); sl@0: check_for_signal(TASK4); sl@0: check_for_signal(TASK1); sl@0: sl@0: kprintf("test_semaphore OK"); sl@0: } sl@0: sl@0: void test_message_queue(void) sl@0: { sl@0: unsigned t1, t2, t3, t4; sl@0: int tid, p, r; sl@0: int init_pri = get_task_priority(current_task_id()); sl@0: p = 0; sl@0: t1 = 0; sl@0: for (tid = TASK1; tid <= TASK4; ++tid) sl@0: { sl@0: for (p = 1; p; p<<=1) sl@0: { sl@0: tsend_run_signal_p(tid, p); sl@0: r = check_for_signal_p(OC_TASK, tid, NULL); sl@0: assert(r == p); sl@0: } sl@0: } sl@0: check_no_signal(); sl@0: set_task_priority(current_task_id(), 176); // raise current task priority sl@0: set_task_priority(TASK4, 144); // change task priorities while they are waiting sl@0: set_task_priority(TASK3, 140); sl@0: set_task_priority(TASK2, 136); sl@0: set_task_priority(TASK1, 132); sl@0: t1func = 5; // switch task 1 to timed waits sl@0: for (tid = TASK1; tid <= TASK4; ++tid) sl@0: { sl@0: for (p = 0; p<0x40000000; p+=(0x413b9cb+tid)) sl@0: { sl@0: tsend_run_signal_p(tid, p); // let multiple messages accumulate on the queues sl@0: } sl@0: } sl@0: check_no_signal(); sl@0: set_task_priority(current_task_id(), init_pri); // let tasks run sl@0: kprintf("init_pri=%d",init_pri); sl@0: for (tid = TASK4; tid >= TASK1; --tid) sl@0: { sl@0: for (p = 0; p<0x40000000; p+=(0x413b9cb+tid)) sl@0: { sl@0: r = check_for_signal_p(OC_TASK, tid, &t1); sl@0: assert(r == p); sl@0: } sl@0: } sl@0: sl@0: delay(5*TM_PERIOD); sl@0: tsend_run_signal_p(TASK1, p); // send after half timeout sl@0: r = check_for_signal_p(OC_TASK, TASK1, &t2); sl@0: assert(r == p); sl@0: delay(11*TM_PERIOD); // wait for > timeout sl@0: tsend_run_signal_p(TASK1, ~p); // send after timeout sl@0: r = check_for_signal_p(TASK1, TASK1, &t3); sl@0: assert(r == TIMED_OUT); sl@0: kprintf("t2-t1=%d t3-t2=%d", t2-t1, t3-t2); sl@0: assert(t2-t1 >= 5); sl@0: assert(t3-t2 >= 10); sl@0: r = check_for_signal_p(OC_TASK, TASK1, &t4); sl@0: assert(r == ~p); sl@0: assert(t4-t3 <= 1); sl@0: t1func = 6; // switch task 1 to timed semaphore wait sl@0: t2func = 7; // switch task 2 to timed queue wait sl@0: t3func = 8; // sl@0: t4func = 8; // sl@0: for (tid = TASK1; tid <= TASK4; ++tid) sl@0: { sl@0: tsend_run_signal_p(tid, 0); sl@0: r = check_for_signal_p(OC_TASK, tid, NULL); sl@0: assert(r == 0); sl@0: } sl@0: check_no_signal(); sl@0: sl@0: kprintf("test_message_queue OK"); sl@0: } sl@0: sl@0: void random_isr(unsigned n) sl@0: { sl@0: random_isr_msg* m; sl@0: unsigned extra = 1; sl@0: unsigned count = 1; sl@0: int r; sl@0: if (!(n%11)) sl@0: ++count; sl@0: if (!(n%13)) sl@0: ++count; sl@0: while (count--) sl@0: { sl@0: m = (random_isr_msg*)alloc_mem_block(sizeof(random_isr_msg)); sl@0: m->header.msg_id = MSG_ID_RND_ISR; sl@0: m->random_isr_number = n; sl@0: extra *= n; sl@0: m->extra = extra; sl@0: r = send_msg(L1_TASK, &m->header); sl@0: } sl@0: if (random_sem_signal_count && !--random_sem_signal_count) sl@0: { sl@0: random_sem_signal_count = random_sem_signal_interval; sl@0: semaphore_signal(ISR_SEM); sl@0: } sl@0: } sl@0: sl@0: void flush_queue(msghdr** f, msghdr** l, msghdr* tm) sl@0: { sl@0: msghdr* m = *f; sl@0: *f = NULL; sl@0: *l = NULL; sl@0: send_to_epoc(tm); sl@0: while (m) sl@0: { sl@0: msghdr* n = m->next; sl@0: send_to_epoc(m); sl@0: m = n; sl@0: } sl@0: } sl@0: sl@0: void l1_task_entry(void) sl@0: { sl@0: msghdr* first = NULL; sl@0: msghdr* last = NULL; sl@0: unsigned state = 0; sl@0: unsigned extra_count = 0; sl@0: unsigned extra_value = 0; sl@0: assert(current_task_id() == L1_TASK); sl@0: kprintf("L1_TASK running"); sl@0: for (;;) sl@0: { sl@0: msghdr* m = NULL; sl@0: int r = recv_msg(&m, WAIT_FOREVER); sl@0: assert(r == OK); sl@0: switch (m->msg_id) sl@0: { sl@0: case MSG_ID_RND_ISR: sl@0: { sl@0: random_isr_msg* rm = (random_isr_msg*)m; sl@0: assert(m->sending_task_id == TASK_ID_ISR); sl@0: assert(rm->random_isr_number == next_random_id); sl@0: if (state == 0) sl@0: { sl@0: extra_count = 0; sl@0: if (!(next_random_id % 11)) sl@0: ++extra_count; sl@0: if (!(next_random_id % 13)) sl@0: ++extra_count; sl@0: extra_value = next_random_id; sl@0: } sl@0: else if (state > 0) sl@0: { sl@0: extra_value *= next_random_id; sl@0: } sl@0: assert(rm->extra == extra_value); sl@0: if (++state > extra_count) sl@0: state = 0; sl@0: if (state == 0) sl@0: ++next_random_id; sl@0: if (rm->random_isr_number == 0) sl@0: send_msg(OC_TASK, m), m=NULL; sl@0: if (state == 1 && extra_count == 2 && m) sl@0: { sl@0: flush_queue(&first, &last, m); sl@0: m = NULL; sl@0: } sl@0: if (random_send_count && !--random_send_count) sl@0: { sl@0: random_send_count = random_send_interval; sl@0: if (m) sl@0: send_msg(TASK2, m), m=NULL; sl@0: } sl@0: break; sl@0: } sl@0: case MSG_ID_DATA: sl@0: m->next = NULL; sl@0: if (last) sl@0: last->next = m; sl@0: else sl@0: first = m; sl@0: last = m; sl@0: m = NULL; sl@0: break; sl@0: case MSG_ID_FLUSH: sl@0: flush_queue(&first, &last, m); sl@0: m = NULL; sl@0: break; sl@0: default: sl@0: kprintf("L1<-%08x",m->msg_id); sl@0: break; sl@0: } sl@0: if (m) sl@0: free_mem_block(m); sl@0: } sl@0: } sl@0: sl@0: void l2_task_entry(void) sl@0: { sl@0: assert(current_task_id() == L2_TASK); sl@0: kprintf("L2_TASK running"); sl@0: for (;;) sl@0: { sl@0: msghdr* m = NULL; sl@0: int r = recv_msg(&m, WAIT_FOREVER); sl@0: assert(r == OK); sl@0: switch (m->msg_id) sl@0: { sl@0: case MSG_ID_DATA: sl@0: { sl@0: data_msg* dm = (data_msg*)m; sl@0: int i; sl@0: unsigned char cs = 0; sl@0: for (i=0; ilength; ++i) sl@0: cs = (unsigned char)(cs + dm->data[i]); sl@0: dm->checksum = cs; sl@0: send_msg(L1_TASK, m); sl@0: m=NULL; sl@0: break; sl@0: } sl@0: default: sl@0: kprintf("L2<-%08x",m->msg_id); sl@0: break; sl@0: } sl@0: if (m) sl@0: free_mem_block(m); sl@0: } sl@0: } sl@0: sl@0: void rr_task_entry(void) sl@0: { sl@0: assert(current_task_id() == RR_TASK); sl@0: kprintf("RR_TASK running"); sl@0: for (;;) sl@0: { sl@0: msghdr* m = NULL; sl@0: int r = recv_msg(&m, WAIT_FOREVER); sl@0: assert(r == OK); sl@0: switch (m->msg_id) sl@0: { sl@0: case MSG_ID_DATA: sl@0: send_msg(L2_TASK, m); sl@0: m=NULL; sl@0: break; sl@0: default: sl@0: kprintf("RR<-%08x",m->msg_id); sl@0: break; sl@0: } sl@0: if (m) sl@0: free_mem_block(m); sl@0: } sl@0: } sl@0: sl@0: void tm_task_entry(void) sl@0: { sl@0: assert(current_task_id() == TM_TASK); sl@0: kprintf("TM_TASK running"); sl@0: for (;;) sl@0: { sl@0: msghdr* m = NULL; sl@0: int r = recv_msg(&m, WAIT_FOREVER); sl@0: assert(r == OK); sl@0: switch (m->msg_id) sl@0: { sl@0: case MSG_ID_TIMEOUT: sl@0: tmcount = ((timer_msg*)m)->count; sl@0: assert(m->sending_task_id == TASK_ID_ISR); sl@0: if (!(tmcount & 255)) sl@0: { sl@0: report_msg* rpt = (report_msg*)alloc_mem_block(sizeof(report_msg)); sl@0: rpt->header.msg_id = MSG_ID_TM_RPT; sl@0: rpt->count = tmcount; sl@0: rpt->ok_count = 0; sl@0: rpt->bad_count = 0; sl@0: send_to_epoc(&rpt->header); sl@0: } sl@0: break; sl@0: default: sl@0: kprintf("TM<-%08x",m->msg_id); sl@0: break; sl@0: } sl@0: free_mem_block(m); sl@0: } sl@0: } sl@0: sl@0: void generic_task(volatile int* f) sl@0: { sl@0: int r; sl@0: msghdr* m; sl@0: unsigned t1, t2; sl@0: unsigned count = 0; sl@0: unsigned ok_count = 0; sl@0: unsigned bad_count = 0; sl@0: while (*f==0) sl@0: { sl@0: send_run_signal(); sl@0: busy_wait(1); sl@0: } sl@0: while (*f==1) sl@0: { sl@0: send_run_signal(); sl@0: suspend_task(current_task_id()); sl@0: } sl@0: while (*f==2) sl@0: { sl@0: r = semaphore_wait(TEST_SEM, WAIT_FOREVER); sl@0: assert(r == OK); sl@0: send_run_signal(); sl@0: } sl@0: while (*f==3) sl@0: { sl@0: r = semaphore_wait(TEST_SEM, 10*TM_PERIOD); sl@0: assert(r==OK || r==TIMED_OUT); sl@0: send_run_signal_p(r); sl@0: } sl@0: while (*f==4) sl@0: { sl@0: r = recv_msg(&m, WAIT_FOREVER); sl@0: assert(r==OK); sl@0: assert(m->sending_task_id == OC_TASK); sl@0: r = send_msg(OC_TASK, m); sl@0: assert(r == OK); sl@0: } sl@0: while (*f==5) sl@0: { sl@0: r = recv_msg(&m, 10*TM_PERIOD); sl@0: assert(r==OK || r==TIMED_OUT); sl@0: if (r == OK) sl@0: { sl@0: assert(m->sending_task_id == OC_TASK); sl@0: r = send_msg(OC_TASK, m); sl@0: assert(r == OK); sl@0: } sl@0: else sl@0: send_run_signal_p(r); sl@0: } sl@0: while (*f==6) sl@0: { sl@0: t1 = tick_count(); sl@0: r = semaphore_wait(ISR_SEM, 5); sl@0: t2 = tick_count() - t1; sl@0: if (r == TIMED_OUT && t2<5) sl@0: { sl@0: kprintf("SEM timed out too soon: %d", t2); sl@0: ++bad_count; sl@0: } sl@0: if (r == OK) sl@0: ++ok_count; sl@0: ++count; sl@0: if (!(count & 0xff)) sl@0: { sl@0: report_msg* rpt = (report_msg*)alloc_mem_block(sizeof(report_msg)); sl@0: rpt->header.msg_id = MSG_ID_SEM_RPT; sl@0: rpt->count = count; sl@0: rpt->ok_count = ok_count; sl@0: rpt->bad_count = bad_count; sl@0: send_to_epoc(&rpt->header); sl@0: } sl@0: } sl@0: while (*f==7) sl@0: { sl@0: t1 = tick_count(); sl@0: r = recv_msg(&m, 5); sl@0: t2 = tick_count() - t1; sl@0: if (r == TIMED_OUT && t2<5) sl@0: { sl@0: kprintf("RECV timed out too soon: %d", t2); sl@0: ++bad_count; sl@0: } sl@0: if (r==OK) sl@0: ++ok_count, free_mem_block(m); sl@0: ++count; sl@0: if (!(count & 0xff)) sl@0: { sl@0: report_msg* rpt = (report_msg*)alloc_mem_block(sizeof(report_msg)); sl@0: rpt->header.msg_id = MSG_ID_RCV_RPT; sl@0: rpt->count = count; sl@0: rpt->ok_count = ok_count; sl@0: rpt->bad_count = bad_count; sl@0: send_to_epoc(&rpt->header); sl@0: } sl@0: } sl@0: kprintf("Task %d finished", current_task_id()); sl@0: for(;;) sl@0: suspend_task(current_task_id()); sl@0: } sl@0: sl@0: void task1_entry(void) sl@0: { sl@0: assert(current_task_id() == TASK1); sl@0: generic_task(&t1func); sl@0: } sl@0: sl@0: void task2_entry(void) sl@0: { sl@0: assert(current_task_id() == TASK2); sl@0: generic_task(&t2func); sl@0: } sl@0: sl@0: void task3_entry(void) sl@0: { sl@0: assert(current_task_id() == TASK3); sl@0: generic_task(&t3func); sl@0: } sl@0: sl@0: void task4_entry(void) sl@0: { sl@0: assert(current_task_id() == TASK4); sl@0: generic_task(&t4func); sl@0: } sl@0: sl@0: sl@0: sl@0: void oo_overall_control(void) sl@0: { sl@0: int r; sl@0: msghdr* m; sl@0: random_isr_msg* rm; sl@0: unsigned t1, t2, rss_interval; sl@0: kprintf("OC_TASK running"); sl@0: assert(current_task_id() == OC_TASK); sl@0: resume_task(L2_TASK); sl@0: resume_task(RR_TASK); sl@0: resume_task(TM_TASK); sl@0: test_mem_mgr(); sl@0: sl@0: kprintf("Wait for init msg"); sl@0: r = recv_msg(&m, WAIT_FOREVER); sl@0: assert(r == OK); sl@0: assert(m->msg_id == MSG_ID_INIT); sl@0: assert(m->sending_task_id == TASK_ID_UNKNOWN); sl@0: free_mem_block(m); sl@0: kprintf("Received init msg"); sl@0: sl@0: r = start_periodic_timer(TM_TIMER, TM_TASK, TM_INIT_DELAY, TM_PERIOD, NULL); sl@0: assert(r == OK); sl@0: delay(TM_INIT_DELAY-10); sl@0: assert(tmcount == 0); sl@0: delay(10*TM_PERIOD+20); sl@0: assert(tmcount > 0); sl@0: test_suspend_1(); sl@0: test_priority_scheduling(); sl@0: test_semaphore(); sl@0: test_message_queue(); sl@0: sl@0: resume_task(L1_TASK); sl@0: r = start_random_isr(&random_isr); sl@0: if (r != OK) sl@0: goto no_random_isr; sl@0: sl@0: r = recv_msg(&m, WAIT_FOREVER); sl@0: assert(r == OK); sl@0: assert(m->msg_id == MSG_ID_RND_ISR); sl@0: assert(m->sending_task_id == L1_TASK); sl@0: rm = (random_isr_msg*)m; sl@0: assert(rm->random_isr_number == 0); sl@0: free_mem_block(m); sl@0: t1 = next_random_id; sl@0: delay(1024); sl@0: t2 = next_random_id; sl@0: kprintf("%d random ISRs in 1024 ticks", t2-t1); sl@0: rss_interval = (5*(t2-t1)+512)/1024; sl@0: set_task_priority(TASK1, 196); // needs to be higher than DfcThread1 sl@0: set_task_priority(TASK2, 196); sl@0: random_sem_signal_interval = rss_interval; sl@0: random_sem_signal_count = rss_interval; sl@0: random_send_interval = rss_interval; sl@0: random_send_count = rss_interval; sl@0: sl@0: no_random_isr: sl@0: m = (msghdr*)alloc_mem_block(sizeof(msghdr)); sl@0: m->msg_id = MSG_ID_DONE; sl@0: send_to_epoc(m); sl@0: kprintf("All tests completed OK"); sl@0: for (;;) sl@0: { sl@0: int r = recv_msg(&m, WAIT_FOREVER); sl@0: assert(r == OK); sl@0: switch (m->msg_id) sl@0: { sl@0: case MSG_ID_DATA: sl@0: send_msg(RR_TASK, m); sl@0: m=NULL; sl@0: break; sl@0: case MSG_ID_FLUSH: sl@0: send_msg(L1_TASK, m); sl@0: m=NULL; sl@0: break; sl@0: case MSG_ID_DONE: sl@0: stop_random_isr(); sl@0: stop_timer(TM_TIMER); sl@0: suspend_task(L1_TASK); sl@0: suspend_task(L2_TASK); sl@0: suspend_task(RR_TASK); sl@0: suspend_task(TM_TASK); sl@0: suspend_task(TASK1); sl@0: suspend_task(TASK2); sl@0: suspend_task(TASK3); sl@0: suspend_task(TASK4); sl@0: break; sl@0: default: sl@0: kprintf("OC<-%08x",m->msg_id); sl@0: break; sl@0: } sl@0: if (m) sl@0: free_mem_block(m); sl@0: } sl@0: } sl@0: sl@0: #ifdef __cplusplus sl@0: } sl@0: #endif