Example #1
0
static void task1(void *arg)
{
    POSTASK_t  self;
    POSTIMER_t timer;
    VAR_t      curprio;

    (void) arg;

    /* get handle and priority of current task */
    self    = posTaskGetCurrent();
    curprio = posTaskGetPriority(self);

    /* try to increase current priority */
    posTaskSetPriority(self, curprio + 1);
    curprio = posTaskGetPriority(self);

    /* start the printer task */
    printertask_g = startTask(task_printer, NULL, curprio + 1);

    /* print first message */
    print("HELLO WORLD!\n");

    /* start three tasks that do busy waiting */
    startTask(task_poll, " P1 ", curprio - 1);
    posTaskSleep(HZ/8);
    startTask(task_poll, " P2 ", curprio - 1);
    posTaskSleep(HZ/8);
    startTask(task_poll, " P3 ", curprio - 1);

    /* register software interrupt handler */
    posSoftIntSetHandler(4, softint_handler);

    /* start a task that rises software interrupts */
    startTask(task_softint, (void*) 4, curprio - 1);

    /* allocate a flag object */
    flags_g = posFlagCreate();
    if (flags_g == NULL)
        print("\nCan not allocate a flag object\n");

    /* start flag task */
    startTask(task_flag, NULL, curprio + 2);

    /* allocate a semaphore object */
    timersem_g = posSemaCreate(0);
    if (timersem_g == NULL)
        print("\nCan not allocate a semaphore\n");

    /* start timer task */
    startTask(task_timer, NULL, curprio + 2);

    /* allocate a timer object and set the timer up */
    timer = posTimerCreate();
    if (timer == NULL)
        print("\nCan not allocate a timer\n");
    posTimerSet(timer, timersem_g, 2*HZ, 2*HZ);

    /* Start the timer. The timer triggers every 2 seconds. */
    posTimerStart(timer);

    /* allocate a mutex object for mutex test */
    mutex_g = posMutexCreate();
    if (mutex_g == NULL)
        print("\nCan not allocate a mutex\n");

    /* start three mutex tasks */
    startTask(task_mutex, ":M1 ", curprio+1);
    startTask(task_mutex, ":M2 ", curprio+1);
    startTask(task_mutex, ":M3 ", curprio+1);

    /* allocate semaphore object for semaphore test,
       allow 2 tasks to get the semaphore */
    sema_g = posSemaCreate(2);
    if (sema_g == NULL)
        print("\nCan not allocate a semaphore\n");

    /* start three semaphore tasks */
    posTaskSleep(HZ/6);
    startTask(task_semas, (void*) (int) '1', curprio+2);
    posTaskSleep(HZ/6);
    startTask(task_semas, (void*) (int) '2', curprio+2);
    posTaskSleep(HZ/6);
    startTask(task_semas, (void*) (int) '3', curprio+2);

    /* Our main loop. We will set the flag number 2 every 3 seconds. */
    for (;;)
    {
        /* suspend this task for 3 seconds */
        posTaskSleep(3*HZ);

        /* set flag number 2 */
        posFlagSet(flags_g, 2);
    }
}
Example #2
0
void netMainThread(void* arg)
{
  uint8_t i;
#if !NETSTACK_CONF_WITH_IPV6
  POSTIMER_t arpTimer;
#endif
  POSTIMER_t periodicTimer;
  int sendRequested;
  bool packetSeen;

#if !NETSTACK_CONF_WITH_IPV6
  arpTimer = posTimerCreate();
  P_ASSERT("netMainThread1", arpTimer != NULL);

  posTimerSet(arpTimer, uipGiant, MS(10000), MS(10000));
  posTimerStart(arpTimer);
#endif

  periodicTimer = posTimerCreate();
  P_ASSERT("netMainThread2", periodicTimer != NULL);

  posTimerSet(periodicTimer, uipGiant, MS(500), MS(500));
  posTimerStart(periodicTimer);

  posMutexLock(uipMutex);

  packetSeen = false;

  while(1) {

    posMutexUnlock(uipMutex);

    // Using semaphore here is not fully optimal.
    // As it is a counting one, it can get bumped
    // to larger value than 1 by upper or interrupt 
    // layer. However, not much harm is done,
    // this loop just spins extra times without
    // doing nothing useful.

    // A Pico]OS Flag object would be perfect,
    // but it doesn't work with posTimer* functions.

    if (!packetSeen || pollTicks == INFINITE)
      posSemaWait(uipGiant, pollTicks);

    posMutexLock(uipMutex);

    sendRequested = dataToSend;
    dataToSend = 0;
    packetSeen = false;

    if (sendRequested) {

      for(i = 0; i < UIP_CONNS; i++) {

        uip_len = 0;
        uip_poll_conn(&uip_conns[i]);
        if(uip_len > 0) {

#if NETCFG_UIP_SPLIT == 1
          uip_split_output();
#else
#if NETSTACK_CONF_WITH_IPV6
          tcpip_ipv6_output();
#else
          tcpip_output();
#endif
#endif
        }
      }

#if UIP_UDP
      for(i = 0; i < UIP_UDP_CONNS; i++) {

        uip_len = 0;
        uip_udp_periodic(i);
        if(uip_len > 0) {

#if NETSTACK_CONF_WITH_IPV6
          tcpip_ipv6_output();
#else
          tcpip_output();
#endif
        }
      }
#endif /* UIP_UDP */

    }

    packetSeen = netInterfacePoll();

    if (posTimerFired(periodicTimer)) {

      for(i = 0; i < UIP_CONNS; i++) {

        uip_periodic(i);
        if(uip_len > 0) {

#if NETCFG_UIP_SPLIT == 1
          uip_split_output();
#else
#if NETSTACK_CONF_WITH_IPV6
          tcpip_ipv6_output();
#else
          tcpip_output();
#endif
#endif
        }
      }

#if UIP_UDP
      for(i = 0; i < UIP_UDP_CONNS; i++) {

        uip_udp_periodic(i);
        if(uip_len > 0) {

#if NETSTACK_CONF_WITH_IPV6
          tcpip_ipv6_output();
#else
          tcpip_output();
#endif
        }
      }
#endif /* UIP_UDP */

    }

#if NETSTACK_CONF_WITH_IPV6 == 0
    if (posTimerFired(arpTimer)) {

      uip_arp_timer();
    }
#endif

// Run contiki-style timers.
// Instead of posting events to process like
// contiki does, it just calls common callback function
// to do the work.

    etimer_request_poll();

  }
}