Exemplo n.º 1
0
int main(void) {
	
	led2_init(); // Setup GPIO for led2 (on GPIO0.22)

	// Turn on power to TIMER2 and TIMER3
	// (TIMER0 and TIMER1 on by default)
	LPC_SC->PCONP |=  PCTIM2_POWERON | PCTIM3_POWERON;

	// Initialise all timers
	init_timer( 0, TIMER0_INTERVAL );
	init_timer( 1, TIMER1_INTERVAL );
	init_timer( 2, TIMER2_INTERVAL );
	init_timer( 3, TIMER3_INTERVAL );

	// Enable all timers
	enable_timer( 0 );
	enable_timer( 1 );
	enable_timer( 2 );
	enable_timer( 3 );

	// Enter an infinite loop, just incrementing a counter
	volatile static int i = 0 ;
	while(1) {
		i++ ;
	}
	return 0 ;
}
Exemplo n.º 2
0
static int wake_timer_suspend(struct device *dev)
{
	struct wake_timer *tm = dev_get_drvdata(dev);
#ifdef USE_32K_CLK
	int val;
#endif
	if(!(tm->can_suspend && tm->suspend_ms))
		return -EBUSY;
#ifdef USE_32K_CLK
	val = readl(__io_address(PRCM_TIMER_WKUP_CLK));
	val |= 0x07;//timer_wkup_clken
	val |= (0x3<<TIMER_WKUP_CLK_SEL_BIT);
	writel(val, __io_address(PRCM_TIMER_WKUP_CLK));

	val = readl(__io_address(PRCM_WKUP_IRQ_CTRL));
	//val = 0x3ff10300;
	val |= (0x1<<TIMER_WKUP_INTER_EN_BIT);
	writel(val, __io_address(PRCM_WKUP_IRQ_CTRL));
#endif

	enable_irq_wake(tm->irq[0]);
	/*enable_irq_wake(tm->irq[1]);*/
	enable_timer(tm, tm->suspend_ms*10, 0);
#ifdef MANU_UNLOCK
	disable_timer(tm, 1);
#endif
	return 0;
}
Exemplo n.º 3
0
static void __init clocksource_init()
{
#if 1
  disable_timer(1);

  putreg32(0, STLR_TIMER_GPTMCFG(1));
  // Setup periodic timer with incrementing counter
  putreg32(TIMER_GPTMTAMR_TAMR_PERIODIC | TIMER_GPTMTAMR_TACDIR_UP, STLR_TIMER_GPTMTAMR(1));
  putreg32(0xFFFFFFFF, STLR_TIMER_GPTMTAILR(1));
  // Enable timer
  enable_timer(1);

	clocksource_calc_mult_shift(&sysclk_clocksource, CLOCK_TICK_RATE, 20);
	sysclk_clocksource.mask = CLOCKSOURCE_MASK(32);

  //sysclk_clocksource.mult =
  //  clocksource_khz2mult(CLOCK_TICK_RATE / 1000, sysclk_clocksource.shift);
#else
	clocksource_calc_mult_shift(&sysclk_clocksource, 4000000, 20);
	sysclk_clocksource.mask = CLOCKSOURCE_MASK(24);
	putreg32(0xFFFFFF, STLR_SYSTICK_RELOAD);
	putreg32(STLR_SYSTICK_CTRL_ENABLE | STLR_SYSTICK_CTRL_CLK_SRC_PIOSC_DIV_4, STLR_SYSTICK_CTRL);
#endif

  clocksource_register(&sysclk_clocksource);
}
Exemplo n.º 4
0
void timer_start(int timer_index) {
  os_printf("Timer driver loaded\n");
  //set_prescale(timer_index,2);
  enable_timer(timer_index);
  os_printf("control address:%x\n", &(timer_pointers[timer_index]->control));
  os_printf("control value:%x\n", timer_pointers[timer_index]->control);
}
Exemplo n.º 5
0
/*****************************************************************************
**   Main Function  main()
*****************************************************************************/
int main (void)
{	    		
  DWORD counter = 0;

  /************ The main Function is an endless loop ************/
  /* The timer routine is tested on the Keil MCB214x board */
  FIO2DIR = 0x000000FF;		/* P1.16..23 defined as Outputs */
  FIO2CLR = 0x000000FF;		/* turn off all the LEDs */
    
  init_timer( 0, TIME_INTERVAL );
  enable_timer( 0 );

  while (1) 
  {					/* Loop forever */
	if ( timer0_counter >= (0x20 * counter) )
	{
	  FIO2SET = 1 << counter;
	  counter++;
	  if ( counter > 8 )
	  {
		counter = 0;	
		timer0_counter = 0;
		FIO2CLR = 0x000000FF;
	  }
	}
  }
  return 0;
}
Exemplo n.º 6
0
// This is executed as part of the current running thread
// Activities to be done :
// 1. System time updating.
// 2. Timer object value decrementing and setting the flag
//    for time out event processing.
// 3. Set if necessary priority recomputation flag.
// 4. Update the time quantum left and cpu usage for the current thread.
//    If necessary call the scheduler function after enablig the timer.
// flags : runrun, kprunrun, priocompute, timeoutflag
void timer_handler (int irq)
{
    unsigned long oflags;

    /* Update system time value. HZ = 50 */
    millesecs += 20;
    if (millesecs >= 1000)
    {
        secs++;
        millesecs -= 1000;
        priocompute = 1;// Once in every one second recompute
        //priorities by applying decay factor.
    }

    // Without locking only the first timer object is observed.
    if (timers != NULL)
    {
        timers->timer_count -=  20;
        if (timers->timer_count <= 0) timeoutflag = 1;
    }

    // Update current thread cpu usage statistics
    rr_cl_tick((struct kthread *)current_thread);
    if (current_thread->kt_schedinf.sc_tqleft <= 0 || runrun == 1 || kprunrun == 1)
    {
        // Start scheduler function
        CLI;
        enable_timer();
        scheduler();
        STI;
    }
    return;
}
Exemplo n.º 7
0
/*****************************************************************************
**   Main Function  main()
*****************************************************************************/
int main (void)
{	    		
  uint32_t counter = 0;
   
  SystemInit();
  
  LPC_GPIO2->FIODIR = 0x000000FF;		/* P2.0...P2.7 defined as Outputs */
  LPC_GPIO2->FIOCLR = 0x000000FF;		/* turn off all the LEDs */
    
  init_timer( 0, TIME_INTERVAL ); // 10ms	
  enable_timer( 0 );

  while (1) 
  {					/* Loop forever */
	if ( timer0_counter >= (50 * counter) )	   // 0.5s
	{
	  LPC_GPIO2->FIOSET = 1 << counter;
	  counter++;
	  if ( counter > 8 )
	  {
		counter = 0;	
		timer0_counter = 0;
		LPC_GPIO2->FIOCLR = 0x000000FF;
	  }
	}
  }
}
Exemplo n.º 8
0
Arquivo: kb.c Projeto: raywill/maray
static void kb_irq(void)
{
	char buf[2]={0};
	char numbuf[6];/*five digit will be enough for a int*/
	int i;
	
	
	byte scancode=inportb(0x60);
	//really ineffective
	for(i=0;i<sizeof(Scan_Tab);i++)
	{
		if(scancode==Scan_Tab[i]) break;
	}
	
	if(i<sizeof(Scan_Tab) )
	{
		kprintf("%c", Disp_Tab[i]);
		switch (Disp_Tab[i]) {
			case 'X': enable_timer();
				  break;
			case 'R': disable_timer();
				  break;
			case 'S': reset_sys();
		}
	}
	outportb(0x20,0x20);
}
Exemplo n.º 9
0
void init_status_led( void )
{
  IO1DIR |= LEDE; //set LEDE to be an output.
  IO1DIR |= DEBUGPIN;
  init_timer( TIMER0, statusSignal[globalStatus].us_period);
  reset_timer(0);
  enable_timer(0);
}
Exemplo n.º 10
0
static int userspace_tramp(void *arg)
{
	init_new_thread_signals(0);
	enable_timer();
	ptrace(PTRACE_TRACEME, 0, 0, 0);
	os_stop_process(os_getpid());
	return(0);
}
Exemplo n.º 11
0
static int timer_set_next_event(unsigned long evt,
        struct clock_event_device *clk)
{
  if( clk != &sysclk_clockevent )
  {
    printk(KERN_ERR "%s: unknown clock device %s\n", __func__, clk->name);
    return -1;
  }

  disable_timer(0);
  putreg32(evt, STLR_TIMER_GPTMTAILR(0));
  enable_timer(0);

  return 0;
}
Exemplo n.º 12
0
static int wake_timer_resume(struct device *dev)
{
	struct wake_timer *tm = dev_get_drvdata(dev);
#ifdef USE_32_CLK
	int val;
	val = readl(__io_address(PRCM_TIMER_WKUP_CLK));
	val &= ~(3<<TIMER_WKUP_CLK_SEL_BIT);
	writel(val, __io_address(PRCM_TIMER_WKUP_CLK));
#endif
	disable_irq_wake(tm->irq[0]);
	/*disable_irq_wake(tm->irq[1]);*/
	disable_timer(tm, 0);
#ifdef MANU_UNLOCK
	enable_timer(tm, tm->wake_ms, 1);
#endif
	return 0;
}
Exemplo n.º 13
0
/*****************************************************************************
**   Main Function  main()
*****************************************************************************/
int main (void)
{	    		
  uint32_t i;

  /* SystemClockUpdate() updates the SystemFrequency variable */
  SystemClockUpdate();
   
  LPC_GPIO2->FIODIR = 0x000000FF;		/* P2.0..7 defined as Outputs */
  LPC_GPIO2->FIOCLR = 0x000000FF;		/* turn off all the LEDs */
    
  for ( i = 0; i < 2; i++ )
  {  
	init_timer( i , TIME_INTERVAL );	
	enable_timer( i );
  }

  /* Loop forever */
  while (1) 
  {					/* Loop forever */
	if ( (timer0_m0_counter > 0) && (timer0_m0_counter <= BLINK_INTERVAL) )
	{
	  LPC_GPIO2->FIOSET = 1 << 2;
	}
	if ( (timer0_m0_counter > BLINK_INTERVAL) && (timer0_m0_counter <= (BLINK_INTERVAL * 2)) )
	{
	  LPC_GPIO2->FIOCLR = 1 << 2;
	}
	else if ( timer0_m0_counter > (BLINK_INTERVAL * 2) )
	{
	  timer0_m0_counter = 0;
	}
	/* Timer 1 blinky LED 1 */
	if ( (timer1_m0_counter > 0) && (timer1_m0_counter <= BLINK_INTERVAL) )
	{
	  LPC_GPIO2->FIOSET = 1 << 3;
	}
	if ( (timer1_m0_counter > BLINK_INTERVAL) && (timer1_m0_counter <= (BLINK_INTERVAL * 2)) )
	{
	  LPC_GPIO2->FIOCLR = 1 << 3;
	}
	else if ( timer1_m0_counter > (BLINK_INTERVAL * 2) )
	{
	  timer1_m0_counter = 0;
	}
  }
}
Exemplo n.º 14
0
/**
 * @brief 
 *      This function sleeps for given number of seconds
 * @param secs
 *      Number of secs to sleep
 */
void sw_sleep(u32 secs)
{
    timeval_t time;
    time.tval.nsec = 0;
    time.tval.sec = secs;

# ifndef CONFIG_EMULATE_FIQ
    int current_context;
    struct timer_event *tevent;

    current_context = get_current_task_id();

#ifdef TIMER_DBG
    sw_printf("SW: sleep sec 0x%08x nsec 0x%08x \n",time.tval.sec,time.tval.nsec);
#endif

    tevent = timer_event_create(&wake_up_from_sleep, &current_context);
    if(!tevent){
        sw_printf("SW: Out of memory : Cannot Perform Sleep\n");
        return;
    }

    timer_event_start(tevent, &time);

    suspend_task(current_context, TASK_STATE_WAIT);
    schedule();
# else

    u32 clockcycles = timeval_to_clockcycles(&time);
#ifdef TIMER_DBG
    sw_printf("SW: clockcycles 0x%08x \n",clockcycles);
#endif

    enable_timer();
    while(1){
        u32 curr_val = read_sleep_timer();
        if(curr_val > clockcycles){
            break;
        }
    }
    disable_timer();
# endif
    return;
}
Exemplo n.º 15
0
static void timer_set_mode(enum clock_event_mode mode,
         struct clock_event_device *clk)
{
  if( clk != &sysclk_clockevent )
  {
    printk(KERN_ERR "%s: unknown clock device %s\n", __func__, clk->name);
    return;
  }

  disable_timer(0);

  switch(mode) {
  case CLOCK_EVT_MODE_PERIODIC:
    printk(KERN_DEBUG "%s\n", "\tCLOCK_EVT_MODE_PERIODIC");
    // Clear configuration register
    putreg32(0, STLR_TIMER_GPTMCFG(0));
    // Setup periodic timer with decrimenting counter
    putreg32(TIMER_GPTMTAMR_TAMR_PERIODIC, STLR_TIMER_GPTMTAMR(0));
    // Set CONFIG_HZ interval
    putreg32(CLOCK_TICK_RATE / CONFIG_HZ, STLR_TIMER_GPTMTAILR(0));
    // Enable timer interrupt
    putreg32(TIMER_GPTMIMR_TATOIM_MASK, STLR_TIMER_GPTMIMR(0));
    // Enable timer
    enable_timer(0);
    break;
  case CLOCK_EVT_MODE_ONESHOT:
    printk(KERN_DEBUG "%s\n", "\tCLOCK_EVT_MODE_ONESHOT");
    putreg32(0, STLR_TIMER_GPTMCFG(0));
    // Setup one shot timer with decrimenting counter
    putreg32(TIMER_GPTMTAMR_TAMR_ONESHOT, STLR_TIMER_GPTMTAMR(0));

    break;
	case CLOCK_EVT_MODE_RESUME:
		printk(KERN_DEBUG "%s\n", "\tCLOCK_EVT_MODE_RESUME");
		break;
  case CLOCK_EVT_MODE_UNUSED:
    printk(KERN_DEBUG "%s\n", "\tCLOCK_EVT_MODE_UNUSED");
    break;
  case CLOCK_EVT_MODE_SHUTDOWN:
    printk(KERN_DEBUG "%s\n", "\tCLOCK_EVT_MODE_SHUTDOWN");
    break;
  }
}
Exemplo n.º 16
0
void main ( void )
{
	
	init_timer0();
	enable_timer(0);
	Delay(1000);
	LCD_Init();
	LCD_Clear();
	LCD_DisplayString(1,1,"Welcome To Arya Omnitalk");
	
	
	SET_PORT1_PIN_DIRN_OUT(1<<24);
	LCD_BKLIT_OFF;
	while(1)
	{		
		LCD_BKLIT_OFF;
		Delay(10);
		LCD_BKLIT_ON;
		Delay(10);
	}
}
Exemplo n.º 17
0
/*
*how do we init timer:
*		1.init the timer frequency
*		2.set the timer irq routine.
*		3.enable timer
*/
void init_timer()
{
	int max;
	vector_t v;

	timefly = 0;
	
	/*00,use IRQ timer,00:read 2 bytes,low &high;
		  011,mode 3(square wave);0,binary mode*/
	max=SHED_RREQ;				/*set to 10ms */
	outportb(0x43,0x36);	/*0000110110b=0x36*/
	outportb(0x40, max & 0xff);//LSB
	outportb( 0x40,max >> 8 );//MSB

	enable_timer(); /* timer enable here, no longer use keyboard */
	
	//install the timer handler
   	//v.eip = (unsigned)timer_irq;
	//v.access_byte = 0x8E; /* present, ring 0, '386 interrupt gate D=32bit gate*/
	//setvect(&v, 0x20);
}
Exemplo n.º 18
0
static void new_thread_handler(int sig)
{
	unsigned long disable;
	int (*fn)(void *);
	void *arg;

	fn = current->thread.request.u.thread.proc;
	arg = current->thread.request.u.thread.arg;

	UPT_SC(&current->thread.regs.regs) = (void *) (&sig + 1);
	disable = (1 << (SIGVTALRM - 1)) | (1 << (SIGALRM - 1)) |
		(1 << (SIGIO - 1)) | (1 << (SIGPROF - 1));
	SC_SIGMASK(UPT_SC(&current->thread.regs.regs)) &= ~disable;

	suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);

	force_flush_all();
	if(current->thread.prev_sched != NULL)
		schedule_tail(current->thread.prev_sched);
	current->thread.prev_sched = NULL;

	init_new_thread_signals(1);
	enable_timer();
	free_page(current->thread.temp_stack);
	set_cmdline("(kernel thread)");

	change_sig(SIGUSR1, 1);
	change_sig(SIGVTALRM, 1);
	change_sig(SIGPROF, 1);
	local_irq_enable();
	if(!run_kernel_thread(fn, arg, &current->thread.exec_buf))
		do_exit(0);

	/* XXX No set_user_mode here because a newly execed process will
	 * immediately segfault on its non-existent IP, coming straight back
	 * to the signal handler, which will call set_user_mode on its way
	 * out.  This should probably change since it's confusing.
	 */
}
Exemplo n.º 19
0
void bootstrap (struct td *tds, void (*f)(), int *stacks) {
  bwprintf (COM2, "This is a taskbar."
      "");
  bwprintf(COM2, "[?25l");
  bwputstr(COM2, "Welcome to\r\n"
  "+--------------------------------------------------+\r\n"
  "|    ______                 __        ______  ____ |\r\n"
  "|   / __/ /________  __ _  / /  ___  / / __ \\/ __/ |\r\n"
  "|  _\\ \\/ __/ __/ _ \\/  ' \\/ _ \\/ _ \\/ / /_/ /\\ \\   |\r\n"
  "| /___/\\__/_/  \\___/_/_/_/_.__/\\___/_/\\____/___/   |\r\n"
  "|                          v0.0.3 (Techno Fitness) |\r\n"
  "+--------------------------------------------------+\r\n\r\n");

  DPRINTFUNC ("bootstrap");
  install_handler ();
  DPRINTOK ("Interrupt handler installed.\n");
  enable_timer ();
  enable_interrupts ();
  _kCreate(tds, IDLE, f, 0, 0, stacks);
  DPRINTOK ("First user task created.\n");
  DPRINTOK ("Booting complete.\n");
}
Exemplo n.º 20
0
void finish_fork_handler(int sig)
{
 	UPT_SC(&current->thread.regs.regs) = (void *) (&sig + 1);
	suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);

	force_flush_all();
	if(current->thread.prev_sched != NULL)
		schedule_tail(current->thread.prev_sched);
	current->thread.prev_sched = NULL;

	enable_timer();
	change_sig(SIGVTALRM, 1);
	local_irq_enable();
	if(current->mm != current->parent->mm)
		protect_memory(uml_reserved, high_physmem - uml_reserved, 1, 
			       1, 0, 1);
	task_protections((unsigned long) current_thread);

	free_page(current->thread.temp_stack);
	local_irq_disable();
	change_sig(SIGUSR1, 0);
	set_user_mode(current);
}
Exemplo n.º 21
0
void timer_thread(void)
{
	char fast_flag=0;
	char slow_flag=0;
	//char dns_flag=0;
	
	disable_timer();//变量重入	
	fast_flag=time_flag;
	time_flag=0;
	slow_flag=slow_timer;
	if(dns_time>4){
		dns_time=0;
	}
	enable_timer();
	if(fast_flag){//lwip不可重入解决方法
		time_flag=0;
		tcp_fasttmr();
		if(slow_flag)
			tcp_slowtmr();
//		if(dns_flag)
//	 	 	dns_tmr();//dns updata
	}
}
Exemplo n.º 22
0
void flush_thread_tt(void)
{
	unsigned long stack;
	int new_pid;

	stack = alloc_stack(0, 0);
	if(stack == 0){
		printk(KERN_ERR 
		       "flush_thread : failed to allocate temporary stack\n");
		do_exit(SIGKILL);
	}
		
	new_pid = start_fork_tramp(task_stack_page(current), stack, 0, exec_tramp);
	if(new_pid < 0){
		printk(KERN_ERR 
		       "flush_thread : new thread failed, errno = %d\n",
		       -new_pid);
		do_exit(SIGKILL);
	}

	if(current_thread->cpu == 0)
		forward_interrupts(new_pid);
	current->thread.request.op = OP_EXEC;
	current->thread.request.u.exec.pid = new_pid;
	unprotect_stack((unsigned long) current_thread);
	os_usr1_process(os_getpid());
	change_sig(SIGUSR1, 1);

	change_sig(SIGUSR1, 0);
	enable_timer();
	free_page(stack);
	protect_memory(uml_reserved, high_physmem - uml_reserved, 1, 1, 0, 1);
	stack_protections((unsigned long) current_thread);
	force_flush_all();
	unblock_signals();
}
Exemplo n.º 23
0
void mcount( unsigned long frompc, unsigned long selfpc )
{
	register struct gmonparam *p = NULL;
	register long toindex, fromindex;
	int j;

	disable_timer();

	//print("CG: "); putnum(frompc); print("->"); putnum(selfpc); print("\r\n");
	// check that frompcindex is a reasonable pc value.
	// for example:	signal catchers get called from the stack,
	//		not from text space.  too bad.
	//
	for(j = 0; j < n_gmon_sections; j++ ){
		if((frompc >= _gmonparam[j].lowpc) && (frompc < _gmonparam[j].highpc)) {
			p = &_gmonparam[j];
			break;
		}
	}
	if( j == n_gmon_sections )
		goto done;

#ifdef PROFILE_NO_FUNCPTR
	fromindex = searchpc( p->cgtable, p->cgtable_size, frompc ) ;
	if( fromindex == -1 ) {
		fromindex = p->cgtable_size ;
		p->cgtable_size++ ;
		p->cgtable[fromindex].frompc = frompc ;
		p->cgtable[fromindex].selfpc = selfpc ;
		p->cgtable[fromindex].count = 1 ;
		goto done ;
	}
	p->cgtable[fromindex].count++ ;
#else
	fromindex = searchpc( p->froms, p->fromssize, frompc ) ;
	if( fromindex == -1 ) {
		fromindex = p->fromssize ;
		p->fromssize++ ;
		//if( fromindex >= N_FROMS ) {
		//print("Error : From PC table overflow\r\n") ;
		//goto overflow ;
		//}
		p->froms[fromindex].frompc = frompc ;
		p->froms[fromindex].link = -1 ;
	}else {
		toindex = p->froms[fromindex].link ;
		while(toindex != -1) {
			toindex = (p->tossize - toindex)-1 ;
			if( p->tos[toindex].selfpc == selfpc ) {
				p->tos[toindex].count++ ;
				goto done ;
			}
			toindex = p->tos[toindex].link ;
		}
	}

	//if( toindex == -1 ) {
	p->tos-- ;
	p->tossize++ ;
	//if( toindex >= N_TOS ) {
	//print("Error : To PC table overflow\r\n") ;
	//goto overflow ;
	//}
	p->tos[0].selfpc = selfpc ;
	p->tos[0].count = 1 ;
	p->tos[0].link = p->froms[fromindex].link ;
	p->froms[fromindex].link = p->tossize-1 ;
#endif

 done:
	p->state = GMON_PROF_ON;
	goto enable_timer ;
 //overflow:
	p->state = GMON_PROF_ERROR;
 enable_timer:
	enable_timer();
	return ;
}
Exemplo n.º 24
0
/*****************************************************************************
**   Main Function  main()
*****************************************************************************/
int main (void)
{
  /* SystemClockUpdate() updates the SystemFrequency variable */
  SystemClockUpdate();

  LPC_GPIO2->FIODIR = 0x0000000F;		/* P2.0..3 defined as Outputs */
  LPC_GPIO2->FIOCLR = 0x0000000F;		/* turn off all the LEDs */

  /* Do a P2M DMA transfer using timer match out as a trigger */  
  DMA_Init(P2M);
#if 0
  LPC_GPDMACH0->CConfig |= 0x0C001|(TMR0_M0_REQ<<1)|(0x00<<6)|(0x02<<11);
  LPC_SC->DMAREQSEL = (0x1<<0);
  init_timer( 0, TIME_INTERVAL );	
  enable_timer( 0 );
#endif
#if 0
  LPC_GPDMACH0->CConfig |= 0x0C001|(TMR0_M1_REQ<<1)|(0x00<<6)|(0x02<<11);
  LPC_SC->DMAREQSEL = (0x1<<1);
  init_timer( 0, TIME_INTERVAL );	
  enable_timer( 0 );
#endif
#if 1
  LPC_GPDMACH0->CConfig |= 0x0C001|(TMR1_M0_REQ<<1)|(0x00<<6)|(0x02<<11);
  LPC_SC->DMAREQSEL = (0x1<<2);
  init_timer( 1, TIME_INTERVAL );	
  enable_timer( 1 );
#endif
#if 0
  LPC_GPDMACH0->CConfig |= 0x0C001|(TMR1_M1_REQ<<1)|(0x00<<6)|(0x02<<11);
  LPC_SC->DMAREQSEL = (0x1<<3);
  init_timer( 1, TIME_INTERVAL );	
  enable_timer( 1 );
#endif

  /* When DMADone is set, it indicates that DMA has been triggered 
  by MAT_OUT(0.0). */
  while ( !CH0DMADone );
  CH0DMADone = 0;

  /* Loop forever */
  while (1) 
  {		
	/* Timer 0 blinky LED 0 */
	if ( (timer0_m0_counter > 0) && (timer0_m0_counter <= BLINK_INTERVAL) )
	{
	  LPC_GPIO2->FIOSET = 1 << 2;
	}
	if ( (timer0_m0_counter > BLINK_INTERVAL) && (timer0_m0_counter <= (BLINK_INTERVAL * 2)) )
	{
	  LPC_GPIO2->FIOCLR = 1 << 2;
	}
	else if ( timer0_m0_counter > (BLINK_INTERVAL * 2) )
	{
	  timer0_m0_counter = 0;
	}
	/* Timer 1 blinky LED 1 */
	if ( (timer1_m0_counter > 0) && (timer1_m0_counter <= BLINK_INTERVAL) )
	{
	  LPC_GPIO2->FIOSET = 1 << 3;
	}
	if ( (timer1_m0_counter > BLINK_INTERVAL) && (timer1_m0_counter <= (BLINK_INTERVAL * 2)) )
	{
	  LPC_GPIO2->FIOCLR = 1 << 3;
	}
	else if ( timer1_m0_counter > (BLINK_INTERVAL * 2) )
	{
	  timer1_m0_counter = 0;
	}
  }
}
int
main (int argc, char **argv, char **envp)
{
    global_prog_name = argv[0];
    if (argc == 1) {
        usage(global_prog_name);
        exit(1);
    }

    // Determine which version of OS X we are running on.
    int major_version_num;
    int minor_version_num;
    int subminor_version_num;
    get_macosx_version(&major_version_num, &minor_version_num,
                       &subminor_version_num);
    struct poll_state ps;
    ps.use_polling = 0;
    if (major_version_num >= 10) {
        // Mac OS X 10.5.* and earlier return a number that appears
        // correct from the ru_maxrss field of getrusage(), also
        // filled in by wait4().

        // Mac OS X 10.6.* always returns 0 for ru_maxrss, so we must
        // use a different method to measure it.
        ps.use_polling = 1;
    }

    char **child_argv = (char **) malloc((unsigned) argc * sizeof(char *));
    int i;
    for (i = 1; i < argc; i++) {
        child_argv[i-1] = argv[i];
    }
    child_argv[argc-1] = NULL;
    // char **p;
    // for (p = child_argv; *p != NULL; p++) {
    //     fprintf(stderr, " p[%d]='%s'", p-child_argv, *p);
    // }
    // fprintf(stderr, "\n");

    struct timeval start_time;
    struct timeval end_time;
    unsigned int num_cpus = get_num_cpus();
    cpu_usage *total_cpu_stats_start = malloc_cpu_stats(1);
    cpu_usage *per_cpu_stats_start = malloc_cpu_stats(num_cpus);
    cpu_usage *total_cpu_stats_end = malloc_cpu_stats(1);
    cpu_usage *per_cpu_stats_end = malloc_cpu_stats(num_cpus);

    get_cpu_usage(num_cpus, per_cpu_stats_start, total_cpu_stats_start);
    int ret = gettimeofday(&start_time, NULL);
    // tbd: check ret

    pid_t pid = fork();
    if (pid == -1) {
        fprintf(stderr, "Error return status -1 while attempting"
                " to call fork().  errno=%d\n", errno);
        perror(global_prog_name);
        exit(3);
    } else if (pid == 0) {

        // We are the child process

        // Set the uid to the original uid of the process that invoked
        // the timemem-darwin process, so that the command being
        // measured is run with that user's priviliges, not with root
        // privileges.
        int original_uid = getuid();
        int ret = setuid(original_uid);
        if (ret != 0) {
            fprintf(stderr, "Error return status %d while attempting"
                    " to set uid to %d.  errno=%d\n", ret, original_uid,
                    errno);
            perror(global_prog_name);
            exit(4);
        }

        ret = execvp(child_argv[0], child_argv);
        // Normally the call above will not return.
        fprintf(stderr, "Error return status %d while attempting"
                " to call execvp().  errno=%d\n", ret, errno);
        perror(global_prog_name);
        exit(2);

    }

    // We are the parent process.

    // We want to wait until the child process finishes, but we also
    // want to periodically poll the child process's resident set size
    // (memory usage).  On OS X 10.5.8 and earlier, simply using
    // wait4() for the child to finish would fill in the rusage struct
    // with the maximum resident set size, but this value is always
    // filled in with 0 in OS X 10.6, hence the use of polling.

    // We implement the polling by calling setitimer() so that we are
    // sent a SIGALRM signal every 100 msec.  This should cause
    // wait4() to return early.  We handle the signal, and then call
    // wait4() again.

    // Read the current maximum resident set size once before starting
    // the timer, because the most likely reason for it to fail is
    // that we are not running with root privileges.
    if (ps.use_polling) {
        if (init_polling_process_rss(pid, &ps) != 0) {
            run_as_superuser_msg(global_prog_name);
            exit(1);
        }
        poll_process_rss(&ps);
        
        // Set up the SIGALRM signal handler.
        global_sigalrm_handled = 0;
        enable_handling_sigalrm();
        
        // Set timer to send us a SIGALRM signal every 100 msec.
        int timer_period_msec = 100;
        enable_timer(timer_period_msec);
    }

    //int wait_opts = WNOHANG;
    int wait_opts = 0;
    int wait_status;
    int wait4_ret;
    struct rusage r;
    while (1) {
        wait4_ret = wait4(pid, &wait_status, wait_opts, &r);
        if (wait4_ret != -1) {
            break;
        }
        if ((errno == EINTR) && ps.use_polling) {
            // Most likely the SIGALRM timer signal was handled.  If
            // so, poll the child process's memory use once.  The
            // timer should automatically signal again periodically
            // without having to reset it.
            if (global_sigalrm_handled) {
                poll_process_rss(&ps);
                global_sigalrm_handled = 0;
                if (ps.task_info_errored) {
                    disable_timer();
                    ignore_sigalrm();
                }
            }
            // Go around and call wait4() again.
        } else {
            fprintf(stderr, "wait4() returned %d.  errno=%d\n",
                    wait4_ret, errno);
            perror(global_prog_name);
            exit(5);
        }
    }

    // We may not use end_time if there are errors we haven't checked
    // for yet from wait4(), but it is more accurate to call this as
    // soon after wait4() returns as we can.  It is out of the loop
    // above to avoid the overhead of calling it on every poll time.
    if (debug) {
        fprintf(stderr, "About to call gettimeofday()\n");
    }
    ret = gettimeofday(&end_time, NULL);
    // tbd: check ret
    get_cpu_usage(num_cpus, per_cpu_stats_end, total_cpu_stats_end);
    
    if (wait4_ret != pid) {
        fprintf(stderr, "wait4() returned pid=%d.  Expected pid"
                " %d of child process.  Try again.\n", wait4_ret, pid);
        fprintf(stderr, "wait4 r.ru_maxrss=%ld\n", r.ru_maxrss);
        exit(7);
    }
    ps.wait4_returned_normally = 1;
    if (debug) {
        fprintf(stderr, "wait4() returned pid=%d of child process."
                "  Done!\n", pid);
    }
    
    if (ps.use_polling) {
        // Disable the timer.  Ignore SIGALRM, too, just in case one
        // more happens.
        if (debug) {
            fprintf(stderr, "About to disable the timer\n");
        }
        disable_timer();
        if (debug) {
            fprintf(stderr, "About to ignore SIGALRM\n");
        }
        ignore_sigalrm();
    }

    // Elapsed time
    int elapsed_msec = timeval_diff_msec(&start_time, &end_time);
    fprintf(stderr, "real %9d.%03d\n", (elapsed_msec / 1000),
            (elapsed_msec % 1000));

    // User, sys times
    fprintf(stderr, "user %9ld.%03d\n", r.ru_utime.tv_sec,
            r.ru_utime.tv_usec / 1000);
    fprintf(stderr, "sys  %9ld.%03d\n", r.ru_stime.tv_sec,
            r.ru_stime.tv_usec / 1000);
    
    // Maximum resident set size
    
    if (! ps.use_polling) {
        // At least on the Intel Core 2 Duo Mac OS X 10.5.8 machine on
        // which I first tested this code, it seemed to give a value
        // of up to 2^31-4096 bytes correctly, but if it went a little
        // bit over that, the fprintf statement showed it as 0, not
        // 2^31 bytes.  For now, I'll do a special check for 0 and
        // print out what I believe to be the correct value.
        
        // One way to test this on that machine is as follows.  The
        // first command below prints 2^31-4096 bytes as the maximum
        // resident set size.  Without the "if" condition below, the
        // second command below prints 0 as the maximum resident set
        // size.
        
        // ./timemem-darwin ../../memuse/test-memuse 2096863
        // ./timemem-darwin ../../memuse/test-memuse 2096864
        
        // Reference:
        // http://lists.apple.com/archives/darwin-kernel/2009/Mar/msg00005.html

        if (r.ru_maxrss == 0L) {
            // Print 2^31 bytes exactly
            fprintf(stderr, "2147483648  maximum resident set size from getrusage\n");
        } else {
            fprintf(stderr, "%10lu  maximum resident set size from getrusage\n",
                    (unsigned long) r.ru_maxrss);
        }
    }
    if (ps.use_polling) {
        long delta = (long) ps.max_rss_bytes - (long) r.ru_maxrss;
        fprintf(stderr, "%10lu  maximum resident set size from polling (%.1f MB, delta %ld bytes = %.1f MB)\n",
                (unsigned long) ps.max_rss_bytes,
                (double) ps.max_rss_bytes / (1024.0 * 1024.0),
                delta,
                (double) delta / (1024.0 * 1024.0));
        double elapsed_time_sec = (double) elapsed_msec / 1000.0;
        fprintf(stderr,
                "number of times rss polled=%ld, avg of %.1f times per second\n", 
                ps.num_polls,
                (double) ps.num_polls / elapsed_time_sec);
        fprintf(stderr,
                "time between consecutive polls (msec): min=%.1f max=%.1f\n",
                (double) ps.consecutive_poll_separation_min_msec,
                (double) ps.consecutive_poll_separation_max_msec);
        int64 max_rss_first_seen_msec =
            timeval_diff_msec(&start_time,
                              &(ps.poll_time_when_maxrss_first_seen));
        fprintf(stderr, "Max RSS observed %.1f sec after start time\n",
                (double) max_rss_first_seen_msec / 1000.0);
        if (ps.task_info_errored) {
            int64 diff_msec = timeval_diff_msec(&end_time,
                                                &(ps.task_info_error_time));
            if (diff_msec <= 0 && diff_msec >= -100) {
                // Then the error most likely occurred because the
                // child process had already exited.  Ignore it.
            } else {
                fprintf(stderr, "A call to task_info() returned an error.  error_time - end_time = %.3f sec.  This may mean the maximum resident set size measurement above is too low.\n",
                        (double) diff_msec / 1000.0);
            }
        }
    }

    // Show separate busy percentage for each CPU core
    fprintf(stderr, "Per core CPU utilization (%d cores):", num_cpus);
    for (i = 0; i < num_cpus; i++) {
        uint64 total = (per_cpu_stats_end[i].total -
                        per_cpu_stats_start[i].total);
        int cpu_busy_percent = 0;
        if (total != 0) {
            uint64 idle = (per_cpu_stats_end[i].idle -
                           per_cpu_stats_start[i].idle);
            cpu_busy_percent =
                (int) round(100.0 * (1.0 - ((float) idle)/total));
        }
        fprintf(stderr, " %d%%", cpu_busy_percent);
    }
    fprintf(stderr, "\n");
    
    if (WIFEXITED(wait_status)) {
        // Exit with the same status that the child process did.
        exit(WEXITSTATUS(wait_status));
    } else if (WIFSIGNALED(wait_status)) {
        fprintf(stderr,
                "Command stopped due to signal %d without calling exit().\n",
                WTERMSIG(wait_status));
        exit(1);
    } else {
        fprintf(stderr,
                "Command is stopped due to signal %d, and can be restarted.\n",
                WSTOPSIG(wait_status));
        exit(2);
    }

    return 0;
}
Exemplo n.º 26
0
void* scheduler(void* p)
{
//這裡還要加上signal mask防止interrupt過來
	th_queue_t* queueSche;
	th_queue_t* nextQueueTh;
//	queueSche = th_queue_head(ready_queueHead);	
//	srand(time(NULL));		//seed rand()
	//disable_timer();
	pthread_mutex_lock(&output_lock);
	pthread_mutex_lock(&output_lock);
	printf("I`m in %d, %d\n", getpid(), sched_queueHead==NULL);fflush(stdout);
	pthread_mutex_unlock(&output_lock);
	for(;;)	//infinite loop
	{
		if((queueSche = find_thread_by_tid(sched_queueHead, getpid())) == NULL)
			abort();
		pthread_mutex_lock(&kill_lock);
		if(wait_for_kill>0)
		{
			if(getpid() != main_kernel_id)
			{
				th_queue_delete(sched_queueHead, queueSche->thread);
				wait_for_kill--;
				
				pthread_mutex_unlock(&kill_lock);
				pthread_mutex_lock(&output_lock);
				printf("kill %d, wait = %d\n", getpid(),wait_for_kill );fflush(stdout);
				pthread_mutex_unlock(&output_lock);
				exit(0);
				abort();	
			}
		}
		pthread_mutex_unlock(&kill_lock);
		
		pthread_mutex_lock(&findthread_lock);
		for(;;)
		{
			if((nextQueueTh = find_next_thread(ready_queueHead, queueSche->thread->current_tid)) == NULL)
				abort();
			queueSche->thread->current_tid = nextQueueTh->thread->tid;

			if(nextQueueTh->thread->mctx.status == TH_WAITING)
			{
				break;
			}
		}
		pthread_mutex_lock(&output_lock);
		printf("I`m %d find out %d, %dn", getpid(), nextQueueTh->thread->tid, nextQueueTh->thread->mctx.status);fflush(stdout);
		pthread_mutex_unlock(&output_lock);
	
		nextQueueTh->thread->mctx.status = TH_RUNNING;
		queueSche->thread->current_tid = nextQueueTh->thread->tid;
		pthread_mutex_unlock(&findthread_lock);
		
		enable_timer();
		mctx_switch(&(queueSche->thread->mctx), &(nextQueueTh->thread->mctx));
		
		if(nextQueueTh->thread->mctx.status !=TH_EXITED && \
		   nextQueueTh->thread->mctx.status !=TH_KILLED)
			nextQueueTh->thread->mctx.status = TH_WAITING;
		
		pthread_mutex_lock(&output_lock);
		//printf("I`m %d comeback from %d\n", getpid(), nextQueueTh->thread->tid);fflush(stdout);
		
		pthread_mutex_unlock(&output_lock);
		
		/*
		mctx_list[currentTid].status=TH_RUNNING;
		mctx_switch(&mctx_list[0],&mctx_list[currentTid]);
		*/
	}
	return NULL;
}
Exemplo n.º 27
0
static int __devinit wake_timer_probe(struct platform_device *pdev)
{
	struct resource *res = NULL;
	struct wake_timer * tm = NULL;
	int retval = 0;
	struct wake_timer_data * data = pdev->dev.platform_data;
	
	/*
	 * Dynamically allocate info and par
	 */
	tm = kmalloc(sizeof(struct wake_timer), GFP_KERNEL);
	
	if (!tm) {
		/* goto error path */
		retval = -ENOMEM;
		goto out;
	}

	tm->pdev = pdev;
	tm->stat = STAT_ON;
	tm->count = 0;
	tm->can_suspend = (load_n3s3_fw(pdev) == 0? 1:0);
	if(data) {
		tm->suspend_ms = data->suspend_ms;
		tm->wake_ms = data->wake_ms;
	}
	
#ifdef CONFIG_PM_AUTO_TEST_SUSPEND
	/*
	 *init clk	
	*/
#if 0
	tm->clk = clk_get(&pdev->dev, WAKEUP_TIMER_NAME);
	if(IS_ERR(tm->clk)) {
		dev_err(&pdev->dev,"get wake_timer clock failed");
		return PTR_ERR(tm->clk);
	}
#endif
#ifdef CONFIG_PM_WAKEUP_DEVICE_AUTO_TEST_SUSPEND
	retval = register_test_input_dev(tm);
	if(retval){
		dev_err(&pdev->dev,"register test input dev failed");
		goto free_wake_timer;
	}
#endif

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if(!res) {
		dev_err(&pdev->dev,"get_resource failed");
		retval = -ENXIO;
		goto free_wake_timer;
	}
	
	tm->mmio = ioremap_nocache(res->start,resource_size(res));
	if(!tm->mmio) {
		dev_err(&pdev->dev,"ioremap failed");
		retval = -ENOMEM;
		goto free_wake_timer;
	}

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if(!res) {
		dev_err(&pdev->dev,"get_resource failed");
		retval = -ENXIO;
		goto unmap_mmio;
	}

	tm->irq[0] = res->start;
	tm->irq[1] = res->start+1;

	retval = request_irq(tm->irq[0], wake_timer_irq, 0,\
				 WAKEUP_TIMER_NAME, tm);
	if(retval < 0) {
		dev_err(&pdev->dev,"request_irq %d failed",tm->irq[0]);
		retval = -ENXIO;
		goto unmap_mmio;
	}

	retval = request_irq(tm->irq[1], wake_timer_irq, 0, \
				WAKEUP_TIMER_NAME, tm);
	if(retval < 0) {
		dev_err(&pdev->dev,"request_irq %d failed", tm->irq[1]);
		retval = -ENXIO;
		goto free_irq_0;
	}


	wake_lock_init(&(tm->wake_lock), WAKE_LOCK_SUSPEND,
			WAKEUP_TIMER_WAKELOCK_NAME);

	device_init_wakeup(&pdev->dev, false);

	retval = pm_runtime_set_active(&pdev->dev); 
	if(retval) {
		dev_err(&pdev->dev, "pm runtime set active failed %d", retval);
	}
	pm_runtime_enable(&pdev->dev);
	platform_set_drvdata(pdev, tm);

	enable_timer(tm, RT_MS, 1);
	return retval;

free_irq_0:
	free_irq(tm->irq[0], tm);
unmap_mmio:
	iounmap(tm->mmio);
free_wake_timer:
	kfree(tm);
#endif /*CONFIG_PM_AUTO_TEST_SUSPEND*/	
out:
	return retval;
}