Closed
Description
Here is the problem - in this program bgn is often larger than end.
void setup() {
Serial.begin(9600);
while(!Serial) {}
Serial.println("bgn, end, bgn - end");
for (uint32_t i = 0; i < 10000; i++) {
uint32_t bgn = micros();
uint32_t end = micros();
if (bgn > end) {
Serial.print(bgn);
Serial. print(',');
Serial.print(end);
Serial.print(',');
Serial.println(bgn - end);
}
}
}
void loop() {
}
Here is the print out for the first few cases with bgn > end.
bgn, end, bgn - end
25992,25000,992
43992,43000,992
65993,65001,992
83994,83002,992
Wonder if the interrupt protection is correct. 1000 is a clue.
unsigned long micros() {
// Convert time to us
NVIC_DisableIRQ(main_timer.get_cfg()->cycle_end_irq);
uint32_t time_us = ((main_timer.get_period_raw() - main_timer.get_counter()) * 1000 / main_timer.get_period_raw()) + (agt_time_ms * 1000);
NVIC_EnableIRQ(main_timer.get_cfg()->cycle_end_irq);
return time_us;
}