/** * Initialize the UART and create the necessary sync. **/ void uart_init(void) { /* Initialize the UART Registers */ UBRRL = UBRRL_VALUE; UBRRH = UBRRH_VALUE; /* Enable the UARTs now */ UCSRB |= _BV(RXCIE) | _BV(RXEN) | _BV(TXEN); UCSRA |= (USE_2X << U2X); tx->lock = posSemaCreate(UART_BUF_SIZE); rx->lock = posSemaCreate(0); }
NOSSEMA_t POSCALL nosSemaCreate(INT_t initcount, UVAR_t options, const char *name) { POSSEMA_t sem; REGELEM_t re; re = nos_regNewSysKey(REGTYPE_SEMAPHORE, name == NULL ? (const char*)"s*" : name); if (re == NULL) return NULL; (void) options; sem = posSemaCreate(initcount); if (sem == NULL) { nos_regDelSysKey(REGTYPE_SEMAPHORE, NULL, re); } else { nos_regEnableSysKey(re, sem); POS_SETEVENTNAME(sem, re->name); } return (NOSSEMA_t) sem; }
void POSCALL nos_initRegistry(void) { NOSREGTYPE_t rt; reglist_free_g = NULL; reglist_sema_g = posSemaCreate(1); while (reglist_sema_g == NULL); POS_SETEVENTNAME(reglist_sema_g, "registry sync"); for (rt = MIN_REGTYPE; rt <= MAX_REGTYPE; ++rt) reglist_syselem_g[rt] = NULL; }
/* This is the first function that is called in the multitasking context. * (See file ex_init4.c for how to setup pico]OS). */ void firsttask(void *arg) { POSTASK_t t; VAR_t status; (void) arg; /* create a semaphore, initialize to 0 */ event = posSemaCreate(0); if (event == NULL) { nosPrint("Failed to create a semaphore!\n"); return; } /* start a second task */ t = nosTaskCreate(task2, /* pointer to new task-function */ event, /* optional argument for the task-function */ 2, /* priority level of the new task */ 0, /* stack size (0 = default size) */ "task2"); /* optional name of the second task */ if (t == NULL) { nosPrint("Failed to start second task!\n"); return; } /* install software interrupt handler (no. 4) */ status = posSoftIntSetHandler(4, softisr); if (status != E_OK) { nosPrint("Failed to install software interrupt handler!\n"); return; } /* Signal the interrupt every second. * Usually, the software interrupt would be raised * asynchronously through eg. a hardware isr handler. */ for(;;) { /* raise the software interrupt no. 4 */ posSoftInt(4, 0); /* wait 1 second */ posTaskSleep(MS(1000)); } }
void firsttask(void *arg) { POSTASK_t t; UVAR_t i; posAtomicSet(&counter, 0); /* creating the flag */ flagset = posFlagCreate(); if (flagset == NULL) { nosPrint("Failed to create a set of flags!\n"); return; } /* creating semaphore object with set one default value */ semaphore = posSemaCreate(1); if (semaphore == NULL){ nosPrint("Failed to create a semaphore!\n"); return; } t = nosTaskCreate(task2, /* ptr to function: task2 that is executed */ NULL, /* optional argument, not used here */ 1, /* priority of the first task */ 0, /* stack size for the first task, 0 = default */ "task2"); /* task name */ if (t == NULL) { nosPrint("Failed to start second task!\n"); } t = nosTaskCreate(task3, /* ptr to function: task3 that is executed */ NULL, /* optional argument, not used here */ 1, /* priority of the first task */ 0, /* stack size for the first task, 0 = default */ "task3"); /* task name */ if (t == NULL) { nosPrint("Failed to start third task!\n"); } /* first flag status change */ posFlagSet(flagset, 1); /* task1 handled */ task1(arg); }
/* This is the first function that is called in the multitasking context. * (See file ex_init4.c for how to setup pico]OS). */ void firsttask(void *arg) { POSTASK_t t; posAtomicSet(&counter, 0); /* Create a semaphore, initialize to 2. * You may vary the initialization count between * 1 and 3 and observe the output of this program. * * The initialization semaphore count limits the number * of tasks that are allowed to access a shared resource * at the same time. */ semaphore = posSemaCreate(2); if (semaphore == NULL) { nosPrint("Failed to create a semaphore!\n"); return; } /* start a second task */ t = nosTaskCreate(task2, /* pointer to new task-function */ NULL, /* optional argument for the task-function */ 2, /* priority level of the new task */ 0, /* stack size (0 = default size) */ "task2"); /* optional name of the second task */ if (t == NULL) { nosPrint("Failed to start second task!\n"); } /* start a second task */ t = nosTaskCreate(task3, /* pointer to new task-function */ NULL, /* optional argument for the task-function */ 3, /* priority level of the new task */ 0, /* stack size (0 = default size) */ "task3"); /* optional name of the third task */ if (t == NULL) { nosPrint("Failed to start third task!\n"); } /* continue execution in function task1 */ task1(arg); }
void netInit() { POSTASK_t t; int i; uipGiant = posSemaCreate(0); uipMutex = posMutexCreate(); pollTicks = INFINITE; P_ASSERT("netInit", uipGiant != NULL && uipMutex != NULL); POS_SETEVENTNAME(uipGiant, "uip:giant"); POS_SETEVENTNAME(uipMutex, "uip:mutex"); // uosFS setup netFS.base.mountPoint = "/socket"; netFS.base.cf = &netFSConf; uosMount(&netFS.base); // Initialize contiki-style timers (used by uip code) etimer_init(); dataToSend = 0; for(i = 0; i < UIP_CONNS; i++) uip_conns[i].appstate.file = NULL; #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i++) uip_udp_conns[i].appstate.file = NULL; #endif /* UIP_UDP */ netInterfaceInit(); uip_init(); #if NETSTACK_CONF_WITH_IPV6 == 0 uip_arp_init(); #endif t = posTaskCreate(netMainThread, NULL, NETCFG_TASK_PRIORITY, NETCFG_STACK_SIZE); P_ASSERT("netInit2", t != NULL); POS_SETTASKNAME(t, "uip:main"); }
/* This is the first function that is called in the multitasking context. * (See file ex_init4.c for how to setup pico]OS). */ void firsttask(void *arg) { POSTASK_t t; /* Note: You may uncomment the source below by setting the #if to * zero and then observe the output of this program. * This is a nice example of how synchronization works. */ #if 1 /* create a semaphore, initialize to 1 */ semaphore = posSemaCreate(1); if (semaphore == NULL) { nosPrint("Failed to create a semaphore!\n"); return; } #endif /* start a second task */ t = nosTaskCreate(task2, /* pointer to new task-function */ NULL, /* optional argument for the task-function */ 2, /* priority level of the new task */ 0, /* stack size (0 = default size) */ "task2"); /* optional name of the second task */ if (t == NULL) { nosPrint("Failed to start second task!\n"); } /* continue execution in function task1 */ task1(arg); }
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); } }