void semaphore_test(void) { int producer_th; int consumer_th; int producer_ret; int consumer_ret; /* allocate the empty signal semaphores */ /* initialize the empty as 1 so we can insert an item immediately. */ sem_empty = thinkos_sem_alloc(1); /* allocate the full signal semaphores */ /* initialize the full as 0 as we don't have produced anything yet. */ sem_full = thinkos_sem_alloc(0); /* create the producer thread */ producer_th = thinkos_thread_create(producer_task, NULL, producer_stack, sizeof(producer_stack)); /* create the consuer thread */ consumer_th = thinkos_thread_create(consumer_task, NULL, consumer_stack, sizeof(consumer_stack)); printf(" * Empty semaphore: %d\n", sem_empty); printf(" * Full semaphore: %d\n", sem_full); printf(" * Producer thread: %d\n", producer_th); printf(" * Consumer thread: %d\n", consumer_th); printf("\n"); thinkos_sleep(100); /* number of items to be produced */ prod_count = 100; /* wait for the production thread to finish */ producer_ret = thinkos_join(producer_th); /* wait for the consumer thread to finish */ consumer_ret = thinkos_join(consumer_th); printf(" * Production return = %d\n", producer_ret); printf(" * Consumer return = %d\n", consumer_ret); /* release the semaphores */ thinkos_sem_free(sem_empty); thinkos_sem_free(sem_full); printf("\n"); };
int main(int argc, char ** argv) { int i; DCC_LOG_INIT(); DCC_LOG_CONNECT(); DCC_LOG(LOG_TRACE, "1. cm3_udelay_calibrate()"); /* calibrate usecond delay loop */ cm3_udelay_calibrate(); DCC_LOG(LOG_TRACE, "2. io_init()"); io_init(); DCC_LOG(LOG_TRACE, "3. thinkos_init()"); thinkos_init(THINKOS_OPT_PRIORITY(0) | THINKOS_OPT_ID(32)); tmr_sem = thinkos_sem_alloc(0); DCC_LOG1(LOG_TRACE, "tmr_sem=%d", tmr_sem); oneshot_timer_init(168000000 / 2); periodic_timer_init(2); for (i = 0; ; ++i) { if ((i & 0x7) == 0) DCC_LOG3(LOG_TRACE, "i=%d IRQ=%d TMR=%d", i, irq_count, tmr_count); thinkos_sem_wait(tmr_sem); thinkos_sem_wait(tmr_sem); // if (irq_count != tmr_count) // DCC_LOG2(LOG_TRACE, "IRQ=%d TMR=%d", irq_count, tmr_count); // thinkos_sleep((1 + (i % 9)) * 100); } return 0; }
int loopif_startup(struct ifnet * __if) { struct loopif_drv * drv = (struct loopif_drv *)__if->if_drv; __if->if_mtu = LOOPIF_BUF_SIZE; __if->if_flags |= IFF_LOOPBACK; /* alloc a semaphore to control packet transmission */ drv->tx_sem = thinkos_sem_alloc(1); drv->pkt.len = 0; DCC_LOG1(LOG_INFO, "mtu=%d", __if->if_mtu); return 0; }
usb_cdc_class_t * usb_cdc_init(const usb_dev_t * usb, const uint8_t * const str[], unsigned int strcnt) { struct usb_cdc_acm_dev * dev = &usb_cdc_rt; usb_class_t * cl = (usb_class_t *)dev; /* initialize USB device */ dev->usb = (usb_dev_t *)usb; #ifndef CDC_RX_SEM_NO dev->rx_sem = thinkos_sem_alloc(0); #endif #ifndef CDC_TX_DONE_NO dev->tx_done = thinkos_flag_alloc(); #endif #ifndef CDC_TX_LOCK_NO dev->tx_lock = thinkos_flag_alloc(); #endif #ifndef CDC_CTL_FLAG_NO dev->ctl_flag = thinkos_flag_alloc(); #endif dev->rx_cnt = 0; dev->rx_pos = 0; dev->str = str; dev->strcnt = strcnt; DCC_LOG4(LOG_INFO, "tx_done=%d tx_lock=%d rx_sem=%d ctl_flag=%d", TX_DONE, TX_LOCK, RX_SEM, CTL_FLAG); thinkos_flag_clr(TX_DONE); thinkos_flag_clr(TX_LOCK); thinkos_flag_clr(CTL_FLAG); usb_dev_init(dev->usb, cl, &usb_cdc_ev); return (usb_cdc_class_t *)dev; }
int main(int argc, char ** argv) { struct acc_info acc; uint32_t cnt = 0; int dt; int x; int y; int i; /* calibrate usecond delay loop */ cm3_udelay_calibrate(); __leds_io_init(); __leds_all_on(); udelay(100000); __leds_all_off(); udelay(100000); /* Initialize the stdin, stdout and stderr */ stdio_init(); __leds_all_on(); udelay(100000); /* Print a useful information message */ printf("\n"); __leds_all_off(); printf("---------------------------------------------------------\n"); printf(" STM32F4 Discovery example\n"); printf("---------------------------------------------------------\n"); printf("\n"); // while (1) { // __leds_all_on(); // udelay(100000); // __leds_all_off(); // udelay(100000); // } thinkos_init(THINKOS_OPT_PRIORITY(4) | THINKOS_OPT_ID(4)); leds_init(); btn_init(); #if 0 for (i = 0; ; ++i) { led_off((i - 2) & 0x7); led_on(i & 0x7); dt = i & 255; if (dt > 127) dt = 255 - dt; thinkos_sleep(dt); } #endif acc.sem = thinkos_sem_alloc(0); printf("%s(): acc.sem=%d.\n", __func__, acc.sem); thinkos_thread_create((void *)accelerometer_task, (void *)&acc, accelerometer_stack, sizeof(accelerometer_stack) | THINKOS_OPT_PRIORITY(1)); for (i = 0; ; ++i) { thinkos_sem_wait(acc.sem); if (btn_event_get() == BTN_PRESSED) { /* request calibration */ acc.cal_req = true; } /* Scale */ x = acc.x * 64 / 512; y = acc.y * 64 / 512; if ((++cnt & 0x03) == 0) { printf("%5d,%5d\r", x, y); } if (x == 0) { led_on(1); led_on(3); } else if (x < 0) { led_blink(3, -x); led_off(1); } else { led_blink(1, x); led_off(3); } if (y == 0) { led_on(0); led_on(2); } else if (y < 0) { led_off(0); led_blink(2, -y); } else { led_off(2); led_blink(0, y); } } return 0; }
void irq_test(void) { int timer_th; int event_th; struct set max; struct set avg; struct set ticks; struct set ticks0; struct set dt; int i; int ms; /* make sure IRQs are disabled */ cm3_irq_disable(STM32F_IRQ_TIM6); cm3_irq_disable(STM32F_IRQ_TIM7); cm3_irq_disable(STM32F_IRQ_TIM9); /* allocate semaphore */ printf("1.\n"); sem_timer = thinkos_sem_alloc(0); /* allocate event */ printf("2.\n"); ev_timer = thinkos_ev_alloc(); /* initialize timer 6 */ timer_init(STM32F_TIM6); /* initialize timer 7 */ timer_init(STM32F_TIM7); /* set timer 7 to very high priority */ cm3_irq_pri_set(STM32F_IRQ_TIM7, 0x20); cm3_irq_enable(STM32F_IRQ_TIM7); /* initialize timer 9 */ timer_init(STM32F_TIM9); /* set timer 9 to very low priority */ cm3_irq_pri_set(STM32F_IRQ_TIM9, 0xff); cm3_irq_enable(STM32F_IRQ_TIM9); printf("4.\n"); event_th = thinkos_thread_create(event_wait_task, NULL, stack[1], STACK_SIZE, THINKOS_OPT_PRIORITY(0) | THINKOS_OPT_ID(0)); printf("5.\n"); timer_th = thinkos_thread_create(timer_isr_task, NULL, stack[2], STACK_SIZE, THINKOS_OPT_PRIORITY(0) | THINKOS_OPT_ID(0)); thinkos_sleep(100); // printf("- All times in microseconds\n"); printf("| TIM6 IRQ Wait | TIM7 High Pri " "| TIM9 Low Pri | TIM7 > Ev Wait |\n"); printf("| dt avg max | dt avg max " "| dt avg max | dt avg max |\n"); memset(&meter, 0, sizeof(meter)); timer_start(STM32F_TIM6); timer_start(STM32F_TIM7); timer_start(STM32F_TIM9); ticks0.tim6 = 0; ticks0.tim7 = 0; ticks0.tim9 = 0; ticks0.event = 0; // for (i = 0; i < 10; i++) { for (i = 0; i < 5; i++) { for (ms = 0; ms < 1000; ms++) thinkos_sem_wait(sem_timer); /* get data */ max = meter.max; avg = meter.avg; ticks = meter.ticks; avg.tim6 = (avg.tim6 * 33) / 64; max.tim6 *= 33; avg.tim7 = (avg.tim7 * 33) / 64; max.tim7 *= 33; avg.tim9 = (avg.tim9 * 33) / 64; max.tim9 *= 33; avg.event = (avg.event * 33) / 64; max.event *= 33; dt.tim6 = ticks.tim6 - ticks0.tim6; ticks0.tim6 = ticks.tim6; dt.tim7 = ticks.tim7 - ticks0.tim7; ticks0.tim7 = ticks.tim7; dt.tim9 = ticks.tim9 - ticks0.tim9; ticks0.tim9 = ticks.tim9; dt.event = ticks.event - ticks0.event; ticks0.event = ticks.event; printf("| %4d %4d %4d | %4d %4d %4d | %4d %4d %4d | %4d %4d %4d |\n", dt.tim6, avg.tim6, max.tim6, dt.tim7, avg.tim7, max.tim7, dt.tim9, avg.tim9, max.tim9, dt.event, avg.event, max.event); } printf("\n"); cm3_irq_disable(STM32F_IRQ_TIM7); cm3_irq_disable(STM32F_IRQ_TIM9); thinkos_cancel(event_th, 0); thinkos_cancel(timer_th, 0); thinkos_ev_free(ev_timer); thinkos_sem_free(sem_timer); }