Tue 27 Aug 2013 06:01:43 AM UTC, original submission:
_delay_ms has the following definition:
void
_delay_ms(double __ms)
{
uint16_t __ticks;
double __tmp ;
#if _HAS_DELAY_CYCLES && defined(__OPTIMIZE_) && \
!defined(_DELAY_BACKWARD_COMPATIBLE_) && \
_STDC_HOSTED_
uint32_t __ticks_dc;
extern void __builtin_avr_delay_cycles(unsigned long);
__tmp = ((F_CPU) / 1e3) * __ms;
#if defined(_DELAY_ROUND_DOWN_)
__ticks_dc = (uint32_t)fabs(__tmp);
#elif defined(_DELAY_ROUND_CLOSEST_)
__ticks_dc = (uint32_t)(fabs(__tmp)+0.5);
#else
//round up by default
__ticks_dc = (uint32_t)(ceil(fabs(__tmp)));
#endif
__builtin_avr_delay_cycles(__ticks_dc);
#else
__tmp = ((F_CPU) / 4e3) * __ms;
if (__tmp < 1.0)
__ticks = 1;
else if (__tmp > 65535)
{
// __ticks = requested delay in 1/10 ms
__ticks = (uint16_t) (__ms * 10.0);
while(__ticks)
{
// wait 1/10 ms
_delay_loop_2(((F_CPU) / 4e3) / 10);
__ticks --;
}
return;
}
else
__ticks = (uint16_t)__tmp;
_delay_loop_2(__ticks);
#endif
}
If it is possible for the first half of the conditional to be true (it was for me) then the definitions of _delay_ms and _delay_us are obviously incorrect. (__ticks is not referenced before the #else clause.) I suggest moving the declaration of __ticks to the #else clause, as follows:
void
_delay_ms(double __ms)
{
double __tmp ;
#if _HAS_DELAY_CYCLES && defined(__OPTIMIZE_) && \
!defined(_DELAY_BACKWARD_COMPATIBLE_) && \
_STDC_HOSTED_
uint32_t __ticks_dc;
extern void __builtin_avr_delay_cycles(unsigned long);
__tmp = ((F_CPU) / 1e3) * __ms;
#if defined(_DELAY_ROUND_DOWN_)
__ticks_dc = (uint32_t)fabs(__tmp);
#elif defined(_DELAY_ROUND_CLOSEST_)
__ticks_dc = (uint32_t)(fabs(__tmp)+0.5);
#else
//round up by default
__ticks_dc = (uint32_t)(ceil(fabs(__tmp)));
#endif
__builtin_avr_delay_cycles(__ticks_dc);
#else
uint16_t __ticks;
__tmp = ((F_CPU) / 4e3) * __ms;
if (__tmp < 1.0)
__ticks = 1;
else if (__tmp > 65535)
{
// __ticks = requested delay in 1/10 ms
__ticks = (uint16_t) (__ms * 10.0);
while(__ticks)
{
// wait 1/10 ms
_delay_loop_2(((F_CPU) / 4e3) / 10);
__ticks --;
}
return;
}
else
__ticks = (uint16_t)__tmp;
_delay_loop_2(__ticks);
#endif
}
|