Pages: 1 [2]   Go Down
Author Topic: Céu: a high-level programming language that runs on Arduino  (Read 9432 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Quote
That said, I also cannot agree that blinking leds should run with the highest priority in an application.

They shouldn't but it makes sure that the measurement is not affected by possible external source of errors.

I performed a test myself, admittedly not on an Arduino but another board with another CPU. In order to make it more "interesting" I ran 4 threads with timings in the order of milliseconds instead of seconds, there is a fifth thread doing a loop too.

This is the code:

Code:
#include "ch.h"
#include "hal.h"

static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {

  (void)arg;
  chRegSetThreadName("blinker1");
  while (TRUE) {
    palSetPad(GPIOC, 10);
    chThdSleepMilliseconds(100);
    palClearPad(GPIOC, 10);
    chThdSleepMilliseconds(100);
  }
}

static WORKING_AREA(waThread2, 128);
static msg_t Thread2(void *arg) {

  (void)arg;
  chRegSetThreadName("blinker2");
  while (TRUE) {
    palSetPad(GPIOC, 11);
    chThdSleepMilliseconds(50);
    palClearPad(GPIOC, 11);
    chThdSleepMilliseconds(50);
  }
}

static WORKING_AREA(waThread3, 128);
static msg_t Thread3(void *arg) {

  (void)arg;
  chRegSetThreadName("blinker3");
  while (TRUE) {
    palSetPad(GPIOC, 12);
    chThdSleepMilliseconds(25);
    palClearPad(GPIOC, 12);
    chThdSleepMilliseconds(25);
  }
}

static WORKING_AREA(waThread4, 128);
static msg_t Thread4(void *arg) {

  (void)arg;
  chRegSetThreadName("blinker4");
  while (TRUE) {
    palSetPad(GPIOC, 13);
    chThdSleepMilliseconds(5);
    palClearPad(GPIOC, 13);
    chThdSleepMilliseconds(5);
  }
}

/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  palSetPadMode(GPIOC, 10, PAL_MODE_OUTPUT_PUSHPULL);
  palSetPadMode(GPIOC, 11, PAL_MODE_OUTPUT_PUSHPULL);
  palSetPadMode(GPIOC, 12, PAL_MODE_OUTPUT_PUSHPULL);
  palSetPadMode(GPIOC, 13, PAL_MODE_OUTPUT_PUSHPULL);

  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
  chThdCreateStatic(waThread3, sizeof(waThread3), NORMALPRIO, Thread3, NULL);
  chThdCreateStatic(waThread4, sizeof(waThread4), NORMALPRIO, Thread4, NULL);

  while (TRUE) {
    chThdSleepMilliseconds(10);
  }
}

I didn't have a metronome at hand so I had to use a logic analyzer smiley this is the measurement after 30 minutes of runtime.

Time scale 10mS, no phase shift among threads:



Time scale 10uS, here it is visible the context switch latency that manifests itself as a 10uS jitter:



This without using any particular trick, the five threads all run at same priority.

Giovanni

« Last Edit: March 26, 2012, 02:21:51 pm by gdisirio » Logged


Pages: 1 [2]   Go Up
Jump to: