Пример #1
0
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");
};
Пример #2
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);
}