(message) interrupts disabled during call to prevent reentrancy (@MUL3232)
I wouldn't want a timer disable, because I need precision.
This is a false concern.
Code is simply protecting itself from an unusual situation that good design can avoid from happening.
When running the timer interrupt service routine (ISR), the re-entrantcy of another timing interrupt would normally only happen in 2 cases:
The timer ISR frequency is too high. This means that the time is takes to handle 1 timer ISR is so short another is occurring before this completes. This is not a good design. To solve, insure the timer frequency is not so high.
Latency. A timer ISR occurred while handling some other ISR that is taking a long time, thus blocking this timer ISR call. By the time this ISR is executed another timer interrupt had occurred. To solve, insure all ISRs together do not take longer than the timer period.
With good design then a 2nd timer interrupt will not occur and temporarily disabling the time to do the multiplication will not block a timer interrupt occurrence.
Or simplify code
aDfilter * 8
needing a @MUL3232
call implies weak optimizations or unnecessary use of signed math. Make code easier for the compiler by using unsigned math.
// some_signed_32_bit_type aDfilter
uint32_t aDfilter;
// and if that is not enough change code
// aDfilter = aDfilter * 8 + aD * 2;
aDfilter = (aDfilter << 3) + aD * 2;
Potential performance improvement. Depending on overall design, the delay_us(40);
should be able to be eliminated. If only 1 channel is used, the following is possible - depending on various requirements.
#int_RTCC
void RTCC_isr(void) {
// delay_us(40); // not needed is timer period >> 40us
unsigned int16 aD = read_adc();
set_adc_channel(0); // select channel now (also at initialization)
aDfilter = aDfilter * 8 + aD * 2;
}
Potential bug. If the read_adc()
is in a mode such that the most significant digit is set (e.g. 10 bit conversion shifted 6 left) and the compiler has a 16-bit int/unsigned, then aD*2
overflows. In that case use:
uint32_t aDfilter;
...
aDfilter = aDfilter*8 + (uint32_t)read_adc()*2;