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++

alt_alarm_start

Altera_Forum
Honored Contributor II
2,629 Views

I am trying to use alt_alarm_start() as a general purpose timer. My callback is not getting called at all. Status from my call is good. Could someone look at the following and give me some hints? Thanx 

 

# include <stdlib.h># include "stdio.h"# include "sys/alt_alarm.h"# include "alt_types.h"# include "system.h" 

 

alt_alarm debug_timerTEST ; 

 

alt_u32 DebugTESTISR(int context) 

printf("running...\n"); 

 

 

int main() 

int status=0; 

alt_u32 clocks_per_uS = (DEBUG_TIMER_FREQ / 1000); // 1K uS per msec. 

alt_u32 debugclockspercall = clocks_per_uS * 200;//0;//00; 

 

//status = alt_sysclk_init(DEBUG_TIMER_FREQ); 

 

 

if (alt_alarm_start(&debug_timerTEST, debugclockspercall, DebugTESTISR, NULL) != 0) 

fprintf(stdout, "Error: failed to start DEBUG timer\n"); 

 

while(1) 

printf("."); 

usleep(1000); 

 

}
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
919 Views

example program: 

 

// ALARMS 

 

static alt_alarm alarm_100ms; 

alt_u32 alarm_100ms_callback (void* context); 

static volatile unsigned char alarmflags=0;# define ALARM_100MS 0x01 

 

# define ALARMTICKS(x) ((alt_ticks_per_second()*(x))/1000) 

 

 

int main() { 

 

if (alt_alarm_start (&alarm_100ms,ALARMTICKS(100),alarm_100ms_callback,NULL) < 0) { 

printf("No system clock available\r\n"); 

 

 

while(1) { 

 

if(alarmflags&ALARM_100MS) { 

alarmflags&=~ALARM_100MS; 

printf("Alarm arrived\r\n"); 

 

 

 

//################################################################## 

// ALRM CALLBACK FUNCTIONS 

//################################################################## 

 

alt_u32 alarm_100ms_callback (void* context) { 

alarmflags|=ALARM_100MS; 

return ALARMTICKS(100); 

 

 

 

there must be a return value in the alarm callback function to specify 

the next arrival of the alarm. 

if it is 0, the callback is called only once. 

 

also pay attention that the alarm callback is called in interrupt context,  

so to use printf in the alarmcallback is not a good idea. 

this is also why alarmflags is volatile
0 Kudos
Altera_Forum
Honored Contributor II
919 Views

Thanks, that got me working...  

 

What is the resolution of that alarm (how accurate)?  

 

Is it more accurate for me to define a timer in SOPC Builder and use that? 

 

thanx
0 Kudos
Altera_Forum
Honored Contributor II
919 Views

I think, 

the timer used for the alarms is the System clock timer, which is set in the 

System Library properties to a real timer in SOPC Builder.  

(normally sys_clk_timer with resolution 1ms). 

the resolution of the timer can be read by software with alt_ticks_per_second().
0 Kudos
Altera_Forum
Honored Contributor II
919 Views

Hi all, I have copied 100% of the example and tried it. But I did not get any alarm activity. Can you point out the problem? 

# include <stdlib.h># include "stdio.h"# include "alt_types.h"# include "altera_avalon_pio_regs.h"# include "altera_avalon_timer.h"# include "altera_avalon_timer_regs.h"# include "system.h"# include "sys/alt_alarm.h" 

 

 

static alt_alarm alarm_100ms; 

alt_u32 alarm_100ms_callback (void* context); 

static volatile unsigned char alarmflags=0;# define ALARM_100MS 0x01 

 

# define ALARMTICKS(x) ((alt_ticks_per_second()*(x))/1000) 

 

 

int main() { 

 

if (alt_alarm_start (&alarm_100ms,ALARMTICKS(100),alarm_100ms_callback ,NULL) < 0) { 

printf("No system clock available\r\n"); 

 

 

while(1) { 

 

if(alarmflags&ALARM_100MS) { 

alarmflags&=~ALARM_100MS; 

printf("Alarm arrived\r\n"); 

 

 

 

//################################################### ############### 

// ALRM CALLBACK FUNCTIONS 

//################################################### ############### 

 

alt_u32 alarm_100ms_callback (void* context) { 

alarmflags|=ALARM_100MS; 

printf("In alarm\r\n"); 

return alt_ticks_per_second (); 

}
0 Kudos
Reply