Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

timer problem

Altera_Forum
Honored Contributor II
1,114 Views

I'm new to altera and c-programming. Just can't make the internal timer work. 

 

int main(void) 

 

 

volatile int * red_leds =(int*) 0x10000000; 

volatile int * timer =(int*) 0x10002000; 

 

int data, exit_data, delay, run; 

 

//turning timer on 

int counter = 0xee6b280; // 1/(50 mhz) x (0xee6b280) = 5 sec 

*(timer + 0x8) = (counter & 0xffff); 

*(timer + 0xc) = (counter >> 16) & 0xffff; 

*(timer + 0x4) = 0x4; 

run=*(timer); 

*(red_leds)=0; 

///////////////////////////////////////////// 

 

while (run==0x00000001) // 

*(red_leds)=0x000002aa; 

 

while(1) 

*(red_leds)=0; 

0 Kudos
5 Replies
Altera_Forum
Honored Contributor II
390 Views

You defined timer as a pointer to int (=32bit) but then you used it with byte address increments, so you are not writing the correct registers. 

 

Anyway this is a deprecated way of programming.  

A few hints: 

- use the symbolic defines generated by sopc builder (in system.h file) instead of the numeric addresses 0x10000000, 0x10002000 which would possibly change if you regenerate the system 

- don't use pointers but rather the IOWR/IORD macros which automatically match the addressed memory data size 

- in this case it would be even better exploiting the timer control macros (IOWR_ALTERA_AVALON_TIMER_CONTROL, IOWR_ALTERA_AVALON_TIMER_PERIODL, ...) available in the HAL driver.
0 Kudos
Altera_Forum
Honored Contributor II
390 Views

thanks for answer, i've started to use macroses, but timer still isn't working. 

 

 

# include "system.h" 

# include "altera_avalon_timer_regs.h" 

# include "altera_avalon_pio_regs.h" 

 

 

int main(void) 

 

 

 

 

 

//turning timer on  

// 1/(50 MHz) x (0x1DCD6500) = 10 sec 

IOWR_ALTERA_AVALON_TIMER_PERIODL (INTERVAL_TIMER_BASE + 0x8, 0x6500); 

IOWR_ALTERA_AVALON_TIMER_PERIODH (INTERVAL_TIMER_BASE + 0xC,0x1dcd);  

 

IOWR_ALTERA_AVALON_TIMER_CONTROL (INTERVAL_TIMER_BASE + 0x4, 0x4); 

run=IORD_ALTERA_AVALON_TIMER_STATUS(INTERVAL_TIMER_BASE); 

run=(run & 0x00000002); 

IOWR_ALTERA_AVALON_PIO_DATA(RED_LEDS_BASE, 0); 

///////////////////////////////////////////// 

 

while (run) // 

IOWR_ALTERA_AVALON_PIO_DATA(RED_LEDS_BASE, 0x2AA);  

 

while(1) 

IOWR_ALTERA_AVALON_PIO_DATA(RED_LEDS_BASE, 0); 

}
0 Kudos
Altera_Forum
Honored Contributor II
390 Views

/* 

* Interval_timer configuration 

*/ 

 

# define ALT_MODULE_CLASS_Interval_timer altera_avalon_timer 

# define INTERVAL_TIMER_ALWAYS_RUN 0 

# define INTERVAL_TIMER_BASE 0x10002000 

# define INTERVAL_TIMER_COUNTER_SIZE 32 

# define INTERVAL_TIMER_FIXED_PERIOD 0 

# define INTERVAL_TIMER_FREQ 50000000 

# define INTERVAL_TIMER_IRQ 1 

# define INTERVAL_TIMER_IRQ_INTERRUPT_CONTROLLER_ID 0 

# define INTERVAL_TIMER_LOAD_VALUE 6249999 

# define INTERVAL_TIMER_MULT 0.0010 

# define INTERVAL_TIMER_NAME "/dev/Interval_timer" 

# define INTERVAL_TIMER_PERIOD 125.0 

# define INTERVAL_TIMER_PERIOD_UNITS "ms" 

# define INTERVAL_TIMER_RESET_OUTPUT 0 

# define INTERVAL_TIMER_SNAPSHOT 1 

# define INTERVAL_TIMER_SPAN 32 

# define INTERVAL_TIMER_TICKS_PER_SEC 8.0 

# define INTERVAL_TIMER_TIMEOUT_PULSE_OUTPUT 0 

# define INTERVAL_TIMER_TYPE "altera_avalon_timer"
0 Kudos
Altera_Forum
Honored Contributor II
390 Views

 

--- Quote Start ---  

 

IOWR_ALTERA_AVALON_TIMER_PERIODL (INTERVAL_TIMER_BASE + 0x8, 0x6500); 

IOWR_ALTERA_AVALON_TIMER_PERIODH (INTERVAL_TIMER_BASE + 0xC,0x1dcd);  

 

IOWR_ALTERA_AVALON_TIMER_CONTROL (INTERVAL_TIMER_BASE + 0x4, 0x4); 

run=IORD_ALTERA_AVALON_TIMER_STATUS(INTERVAL_TIMER_BASE); 

 

--- Quote End ---  

 

 

Timer macros already include the offset. 

The correct usage is: 

IOWR_ALTERA_AVALON_TIMER_PERIODL (INTERVAL_TIMER_BASE, 0x6500); 

IOWR_ALTERA_AVALON_TIMER_PERIODH (INTERVAL_TIMER_BASE, 0x1dcd);  

IOWR_ALTERA_AVALON_TIMER_CONTROL (INTERVAL_TIMER_BASE , 0x4);
0 Kudos
Altera_Forum
Honored Contributor II
390 Views

thanks for help

0 Kudos
Reply