[En-Nut-Discussion] Timing issues on Ethernut 1.3 (AVR)

Harald Kipp harald.kipp at egnite.de
Thu Nov 12 10:10:44 CET 2009


Hi Daniel,

Daniel wrote:

> Issue 1, NutGetMillis()
> =======================

I tried this  one:

#include <dev/board.h>
#include <sys/thread.h>
#include <sys/timer.h>

#include <stdio.h>
#include <io.h>

THREAD(Thread, arg)
{
    for (;;) {
        uint32_t start = NutGetMillis();
        uint32_t end = start + 500;
        uint16_t i = 0;

        printf("Start Millis %p: %lu ms\n", arg, NutGetMillis());
        do {
            NutThreadYield();
            i++;
            if (i > 500) {
                printf("Current Millis %p: %lu ms\n", arg, NutGetMillis());
                i = 0;
            }
        } while(NutGetMillis() < end);
        printf("  End Millis %p: %lu ms\n", arg, NutGetMillis());
    }
}

int main(void)
{
    uint32_t baud = 115200;
    int i;

    NutRegisterDevice(&DEV_DEBUG, 0, 0);
    freopen(DEV_DEBUG_NAME, "w", stdout);
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);

    puts("\nTest Millis");

    for (i = 1; i <= 10; i++) {
        NutThreadCreate("th", Thread, (void *)i, 512);
    }

    for (;;) {
        NutSleep(1000);
    }
    return 0;
}

It works as expected:

Test Millis
Start Millis 0x1: 5 ms
Start Millis 0x2: 7 ms
Start Millis 0x3: 9 ms
Start Millis 0x4: 11 ms
Start Millis 0x5: 13 ms
Start Millis 0x6: 15 ms
Start Millis 0x7: 17 ms
Start Millis 0x8: 20 ms
Start Millis 0x9: 22 ms
Start Millis 0xa: 24 ms
Current Millis 0x1: 109 ms
Current Millis 0x2: 111 ms
Current Millis 0x3: 114 ms
Current Millis 0x4: 116 ms
Current Millis 0x5: 119 ms
Current Millis 0x6: 121 ms
Current Millis 0x7: 124 ms
Current Millis 0x8: 126 ms
Current Millis 0x9: 129 ms
Current Millis 0xa: 131 ms
Current Millis 0x1: 218 ms
Current Millis 0x2: 220 ms
...
  End Millis 0x1: 505 ms
Start Millis 0x1: 507 ms
  End Millis 0x2: 509 ms
Start Millis 0x2: 511 ms
  End Millis 0x3: 514 ms
Start Millis 0x3: 516 ms
  End Millis 0x4: 518 ms
Start Millis 0x4: 520 ms
  End Millis 0x5: 523 ms
Start Millis 0x5: 525 ms
  End Millis 0x6: 527 ms
Start Millis 0x6: 529 ms
  End Millis 0x7: 532 ms
Start Millis 0x7: 534 ms
  End Millis 0x8: 536 ms
Start Millis 0x8: 539 ms
  End Millis 0x9: 541 ms
Start Millis 0x9: 543 ms
  End Millis 0xa: 545 ms
Start Millis 0xa: 548 ms
...

May be in your application you should check entry and return time of
NutThreadYield(). Possibly another thread occupies the CPU for too long
time.

Harald

PS: I tested on Ethernut 3 (ARM), but AVR should work similar.



More information about the En-Nut-Discussion mailing list