That's weird, I would expect any modern compiler to turn that division into a multiplication. I suspect it's caused by usage of -Ospace
instead of -Otime
. You may want to tune this option per file.
BTW, here's the list of other places referring to __aeabi_uidivmod
or __aeabi_idivmod
:
i2c_frequency()
, expression "PCLK / (i*ICR[j]);
". There might be some clever optimization here, but it's not terribly important since it's unlikely to be called often. You could calculate the table on the first call but that would increase the RAM consumption.
spi_frequency()
: similar to above.
serial_baud()
: reverse constant division ("PCLK / (16 * baudrate);
") so not easily optimized. Since it's a setup function like i2c_frequency()
it's not important.
clock()
: division by 10000 that should be replaced by multiplication (maybe try #pragma Otime
for this specific function). This one can be called often so it's worth fixing IMO.
Timer::read_ms()
: division by 1000. Can and should be fixed.
Related:
Timer::read()
: you could eliminate a call to __aeabi_fdiv
by doing division first (which should be optimized to multiplication) and conversion to float
second.
There are many FP calculations in pwmout_api.c
and I think some can be optimized. E.g.:
float pwmout_read(pwmout_t* obj)
{
float v = (float)(*obj->CnV) / (float)(*obj->MOD);
return (v > 1.0) ? (1.0) : (v);
}
I would replace by:
float pwmout_read(pwmout_t* obj) {
uint32_t CnV = *obj->CnV;
uint32_t MOD = *obj->MOD;
if ( CnV >= MOD )
return 1.0f;
else
return (float)(CnV) / (float)(MOD);
}
The title should refer to the wait_us function, NOT the wait_ms function :(
The us_ticker_read function for the Freescale board has an issue with
code from us_ticker.c
Seems that the compiler is killing us with the divide by 24 code such that a wait_us(1) is actually closer to 30+us and it doesn't get close to the requested delay until you are over the 100us mark.
I've implemented a version of wait_us which is more accurate for smaller delays (does have greater errors than the mbed library over 100us). Or simply remove the divide by 24 altogether and treat it as a wait_tick function..
NOTE: You do have to call the regular us_ticker_read() function first to ensure the ticker is initialized...
Regards Andy