From 875c8f368683e77371f75c0b9f1aa18237f118f2 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Wed, 15 Oct 2008 18:26:16 +0000 Subject: [PATCH] git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@468 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/MSP430-MSP430x1611-GCC/main.c | 6 +++--- src/include/sleep.h | 17 +++++++++++++++++ test/test.c | 10 ++++++---- test/test.h | 6 +++--- test/testmtx.c | 14 +++++++------- test/testsem.c | 6 +++--- 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/demos/MSP430-MSP430x1611-GCC/main.c b/demos/MSP430-MSP430x1611-GCC/main.c index 9904dfdb0..72025d6a4 100644 --- a/demos/MSP430-MSP430x1611-GCC/main.c +++ b/demos/MSP430-MSP430x1611-GCC/main.c @@ -31,9 +31,9 @@ static msg_t Thread1(void *arg) { while (TRUE) { P6OUT |= P6_O_LED; - chThdSleep(50); + chThdSleep(MS2ST(500)); P6OUT &= ~P6_O_LED; - chThdSleep(50); + chThdSleep(MS2ST(500)); } return 0; } @@ -66,7 +66,7 @@ int main(int argc, char **argv) { while (TRUE) { if (!(P6IN & P6_I_BUTTON)) TestThread(&COM1); - chThdSleep(50); + chThdSleep(MS2ST(500)); } return 0; } diff --git a/src/include/sleep.h b/src/include/sleep.h index 36ba9470a..ac76b3fe8 100644 --- a/src/include/sleep.h +++ b/src/include/sleep.h @@ -25,6 +25,23 @@ #ifndef _SLEEP_H_ #define _SLEEP_H_ +/** + * Time conversion utility. Converts from seconds to system ticks number. + */ +#define S2ST(sec) ((sec) * CH_FREQUENCY) + +/** + * Time conversion utility. Converts from milliseconds to system ticks number. + * @note The result is rounded upward to the next tick boundary. + */ +#define MS2ST(msec) (((((msec) - 1L) * CH_FREQUENCY) / 1000) + 1) + +/** + * Time conversion utility. Converts from microseconds to system ticks number. + * @note The result is rounded upward to the next tick boundary. + */ +#define US2ST(usec) (((((usec) - 1L) * CH_FREQUENCY) / 1000000) + 1) + #ifdef __cplusplus extern "C" { #endif diff --git a/test/test.c b/test/test.c index fa2625215..bd39b1371 100644 --- a/test/test.c +++ b/test/test.c @@ -182,10 +182,11 @@ void test_wait_threads(void) { chThdWait(threads[i]); } -void test_cpu_pulse(systime_t ms) { +void test_cpu_pulse(unsigned ms) { + systime_t duration = MS2ST(ms); systime_t start = chSysGetTime(); - while (chSysInTimeWindow(start, start + ms)) { + while (chSysInTimeWindow(start, start + duration)) { #if defined(WIN32) ChkIntSources(); #endif @@ -209,11 +210,12 @@ static void tmr(void *p) { test_timer_done = TRUE; } -void test_start_timer(systime_t time) { +void test_start_timer(unsigned ms) { + systime_t duration = MS2ST(ms); test_timer_done = FALSE; chSysLock(); - chVTSetI(&vt, time, tmr, NULL); + chVTSetI(&vt, duration, tmr, NULL); chSysUnlock(); } diff --git a/test/test.h b/test/test.h index bb59c196e..524907b76 100644 --- a/test/test.h +++ b/test/test.h @@ -22,7 +22,7 @@ #define MAX_THREADS 5 #define MAX_TOKENS 16 -#define DELAY_BETWEEN_TESTS 200 +#define DELAY_BETWEEN_TESTS MS2ST(200) #if defined(CH_ARCHITECTURE_AVR) || defined(CH_ARCHITECTURE_MSP430) #define THREADS_STACK_SIZE 64 @@ -53,8 +53,8 @@ extern "C" { void test_terminate_threads(void); void test_wait_threads(void); systime_t test_wait_tick(void); - void test_cpu_pulse(systime_t ms); - void test_start_timer(systime_t time); + void test_cpu_pulse(unsigned ms); + void test_start_timer(unsigned ms); #if defined(WIN32) void ChkIntSources(void); #endif diff --git a/test/testmtx.c b/test/testmtx.c index 3954e9338..f389ebe6f 100644 --- a/test/testmtx.c +++ b/test/testmtx.c @@ -85,7 +85,7 @@ static void mtx2_teardown(void) { static msg_t thread2(void *p) { - chThdSleep(5); + chThdSleep(MS2ST(10)); chMtxLock(&m1); chMtxUnlock(); test_emit_token(*(char *)p); @@ -95,7 +95,7 @@ static msg_t thread2(void *p) { static msg_t thread3(void *p) { chMtxLock(&m1); - chThdSleep(20); + chThdSleep(MS2ST(40)); chMtxUnlock(); test_emit_token(*(char *)p); return 0; @@ -103,7 +103,7 @@ static msg_t thread3(void *p) { static msg_t thread4(void *p) { - chThdSleep(10); + chThdSleep(MS2ST(20)); test_cpu_pulse(50); test_emit_token(*(char *)p); return 0; @@ -156,7 +156,7 @@ static msg_t thread5(void *p) { static msg_t thread6(void *p) { - chThdSleep(10); + chThdSleep(MS2ST(10)); chMtxLock(&m2); test_cpu_pulse(20); chMtxLock(&m1); @@ -170,7 +170,7 @@ static msg_t thread6(void *p) { static msg_t thread7(void *p) { - chThdSleep(20); + chThdSleep(MS2ST(20)); chMtxLock(&m2); test_cpu_pulse(50); chMtxUnlock(); @@ -180,7 +180,7 @@ static msg_t thread7(void *p) { static msg_t thread8(void *p) { - chThdSleep(40); + chThdSleep(MS2ST(40)); test_cpu_pulse(200); test_emit_token(*(char *)p); return 0; @@ -188,7 +188,7 @@ static msg_t thread8(void *p) { static msg_t thread9(void *p) { - chThdSleep(50); + chThdSleep(MS2ST(50)); chMtxLock(&m2); test_cpu_pulse(50); chMtxUnlock(); diff --git a/test/testsem.c b/test/testsem.c index 6afa715cb..f2105a7dd 100644 --- a/test/testsem.c +++ b/test/testsem.c @@ -21,7 +21,7 @@ #include "test.h" -#define ALLOWED_DELAY 5 +#define ALLOWED_DELAY MS2ST(5) static Semaphore sem1; @@ -85,10 +85,10 @@ static void sem2_execute(void) { int i; systime_t target_time; - target_time = chSysGetTime() + 5 * 500; + target_time = chSysGetTime() + MS2ST(5 * 500); for (i = 0; i < 5; i++) { test_emit_token('A' + i); - chSemWaitTimeout(&sem1, 500); + chSemWaitTimeout(&sem1, MS2ST(500)); } test_assert_sequence("ABCDE"); test_assert_time_window(target_time, target_time + ALLOWED_DELAY);