Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
21 years ago

Nios II Timer Problems

I am porting a design from Nios 1 to Nios II and the timer doens't seem to be working correctly. I don't want to use the HAL device drivers (System Clock Driver or Timestamp Driver) becuase they don't fit my needs. Anyway, here is a snippet of the relevent timer code:

#define TIMER0_PERIOD (ALT_CPU_FREQ / 8000)    // set to <divisor> Hz.
void timer_init( int period )
/* where period is an integer with range 0..65535, in multiples of system clk.
   This sets up timer0 which is used for the servo loop.  The ISR is elsewhere.
*/
{
  // stop timer
  IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE, ALTERA_AVALON_TIMER_CONTROL_STOP_MSK);
  IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE, 0);    // clears TO flag
  // note: for simplicity only set the periodl register.  This precludes 
  // any periods longer than (system clock)/65535.
  IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_0_BASE, period);
  IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_0_BASE, 0);
  // set continuous mode and turn on the interrupt, it won&#39;t do anything till timer started
  IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE, (ALTERA_AVALON_TIMER_CONTROL_CONT_MSK |
          ALTERA_AVALON_TIMER_CONTROL_ITO_MSK));
}
void timer_start()
{
  IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE, (IORD_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE) | ALTERA_AVALON_TIMER_CONTROL_START_MSK));
}
void timer_clear_interrupt( void )
{
  IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE, 0);
}
int timer_snapshot( void )
{
  IOWR_ALTERA_AVALON_TIMER_SNAPL(TIMER_0_BASE, 0);    // capture snapshot
  return ((IORD_ALTERA_AVALON_TIMER_SNAPH(TIMER_0_BASE) << 16) + IORD_ALTERA_AVALON_TIMER_SNAPL(TIMER_0_BASE));
}
int main(void)
{
  timer_init( TIMER0_PERIOD );
  alt_irq_register(TIMER_0_IRQ, (void *)0, timer_ISR);
  timer_start();
  more_init_stuff_here();
  for(;;)
  {
    // main loop
  }
}
void timer_ISR( void *context, uint32 id )
{
  static unsigned int isr_count = 0;
  int fader;
  timer_clear_interrupt();
  snap0 = timer_snapshot();    // for execution profiling
  ++isr_count;
  // toggle LED
}

timer_ISR only gets called once (the LED only turns on and off once and then remains off). I&#39;ve tried everything. Is there something I&#39;m missing? Something simple maybe? The CONT bit means the timer is continuous, right? I&#39;m really stuck and would appreciate any enlightenment. Thanks ahead of time.

dave
No RepliesBe the first to reply