matthijskooijman / arduino-lmic

:warning: This library is deprecated, see the README for alternatives.
710 stars 652 forks source link

hal_waitUntil() not working ? #12

Open Oliv4945 opened 8 years ago

Oliv4945 commented 8 years ago

Hi,

I am playing with the library, able to transmit (alleluia :-) ) but not to receive. After digging into the library I think that the function hal_waitUntil() is not working due to delayMicroseconds().

It is a bit weird and I really don't understand, unless you changed the timer0 frequency ? But I did not see it in your code...

First, the code from your repository with some prints:

void hal_waitUntil (u4_t time) {
    s4_t delta = delta_time(time);
    // From delayMicroseconds docs: Currently, the largest value that
    // will produce an accurate delay is 16383.
    while (delta > (16000 / US_PER_OSTICK)) {
        delay(16);
        delta -= (16000 / US_PER_OSTICK);
    }
    if (delta > 0) {
LMIC_PRINTF_TO.println(hal_ticks());
        delayMicroseconds(delta * US_PER_OSTICK);
LMIC_PRINTF_TO.println(hal_ticks());
LMIC_PRINTF_TO.print("delta: ");
LMIC_PRINTF_TO.println(delta);
LMIC_PRINTF_TO.print("RX: ");
LMIC_PRINTF_TO.println(time);
    }
}

Outputs: 3256043 3256088 delta: 86 RX : 3256129 -> real wait : 45 ticks, need 41 more

Now if I try to manually add fancy delays

void hal_waitUntil (u4_t time) {
    s4_t delta = delta_time(time);
    // From delayMicroseconds docs: Currently, the largest value that
    // will produce an accurate delay is 16383.
    while (delta > (16000 / US_PER_OSTICK)) {
        delay(16);
        delta -= (16000 / US_PER_OSTICK);
    }
    if (delta > 0) {
LMIC_PRINTF_TO.println(hal_ticks());
        delayMicroseconds(delta * US_PER_OSTICK);
        delayMicroseconds(41*16);
        delayMicroseconds(41*16);
        delayMicroseconds(41*16);
LMIC_PRINTF_TO.println(hal_ticks());
LMIC_PRINTF_TO.print("delta: ");
LMIC_PRINTF_TO.println(delta);
LMIC_PRINTF_TO.print("RX: ");
LMIC_PRINTF_TO.println(time);
    }
}

It outputs 1197911 1197950 delta: 85 RX: 1197996 -> real wait : 39 ticks !!

delayMicroseconds() does not block interrupts since Arduino 018, so the hal_time() should be valid. Does I missed something obvious ?

matthijskooijman commented 8 years ago

Hm, you reasoning looks sound, so something fishy is going on. Did you try printing the result of micros() directly, bypassing hal_ticks()? It would seem the simplest explanation is that hal_ticks() somehow messes up and returns too small times, perhaps I made a mistake in my (admittedly fairly complex) overflow handling code?