atmel-samd: Fix PulseIn duration math to handle case when current us time is after the last one but more than a ms has passed.

This commit is contained in:
Scott Shawcroft 2017-04-11 16:55:42 -07:00
parent f28f8ba568
commit 1e8fc15a41

View File

@ -87,13 +87,15 @@ static void pulsein_callback(void) {
} else {
uint32_t ms_diff = current_ms - last_ms[self->channel];
uint16_t us_diff = current_us - last_us[self->channel];
if (last_us[self->channel] > current_us) {
us_diff = 1000 + current_us - last_us[self->channel];
}
uint32_t total_diff = us_diff;
if (last_us[self->channel] > current_us) {
total_diff = 1000 + current_us - last_us[self->channel];
if (ms_diff > 1) {
total_diff += (ms_diff - 1) * 1000;
}
} else {
total_diff += ms_diff * 1000;
}
uint16_t duration = 0xffff;
if (total_diff < duration) {
duration = total_diff;