Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hal_waitUntil() not working ? #12

Open
Oliv4945 opened this issue Apr 2, 2016 · 1 comment
Open

hal_waitUntil() not working ? #12

Oliv4945 opened this issue Apr 2, 2016 · 1 comment

Comments

@Oliv4945
Copy link
Contributor

Oliv4945 commented Apr 2, 2016

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
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants