njh/contiki — comment on commit f6da78d3238a082ab91f2e52caf0e2279cb555a7
File:  line 0
By dak664:

clock.c for each cpu does clock_wait(ticks) and clock_delay(undefined time).
clock_wait_us and clock_wait_msec could logically go there but could not involve defines because the only clock.h is in /core/sys
which is not appropriate for architectural defines.

rtimer-arch.h is defined for each cpu and the core rtimer.h includes that.
So one way would be core/rtimer.h and .c
#define delay_usec rtimer_arch_delay_usec
void delay_msec(uint16_t howlong) {while (howlong--) rtimer_arch_delay_usec(1000);}

and /cpu/rtimer-arch-h
#include
#define rtimer_arch_delay_usec( us ) _delay_loop_2(1+((unsigned long long)us*F_CPU)/4000000UL) )

gcc gives a warning if the expanded arg is > 16 bits:
191 delay_usec(42);
192 delay_usec(32768);
193 delay_msec(65535);
194 delay_msec(65536);
../../platform/avr-raven/./contiki-raven-main.c:192: warning: large integer implicitly truncated to unsigned type
../../platform/avr-raven/./contiki-raven-main.c:194: warning: large integer implicitly truncated to unsigned type

Some googling found a way to give a compile time error, but it's hardly worth the trouble:
#define CASSERT(predicate, file) _impl_CASSERT_LINE(predicate,__LINE__,file)
#define _impl_PASTE(a,b) a##b
#define _impl_CASSERT_LINE(predicate, line, file) typedef char _impl_PASTE(assertion_failed_##file##_,line)[2*!!(predicate)-1];
#include
#define rtimer_delay_usec( us ) {CASSERT(((unsigned long long)us*F_CPU)/4000000UL<65535,usec_delay_too_long);{
_delay_loop_2(1+((unsigned long long)us*F_CPU)/4000000UL);}}
191 delay_usec(42);
192 delay_usec(32768);
193 delay_msec(65535);
194 delay_msec(65536);

../../platform/avr-raven/./contiki-raven-main.c:192: error: size of array 'assertion_failed_usec_delay_too_long_192' is negative
../../platform/avr-raven/./contiki-raven-main.c:192 warning: large integer implicitly truncated to unsigned type
../../platform/avr-raven/./contiki-raven-main.c:194: warning: large integer implicitly truncated to unsigned type

-----Original Message-----
From: Nicholas Humfrey
Sent: Saturday, February 25, 2012 8:01 AM
To: David Kopf
Subject: Re: [contiki] Removed #define F_CPU from cpu/avr/dev/delay.h - it should be defined by the platform. (f6da78d)

No, I don't have a rt18019. I was just looking for generic delay routines. delay.c probably shouldn't be in the 'dev' directory.

I agree, it would be definitely be preferable to have define delay_us() and delay_ms() instead. It would be good to have an
assertion that the delay period isn't too long for the CPU clock frequency.

---
Reply to this email directly or view it on GitHub:
https://github.com/njh/contiki/commit/f6da78d3238a082ab91f2e52caf0e2279cb555a7#commitcomment-1010756