C vxWorks定时器:不能多次调用处理程序
我在VxWorks中使用定时器中断进行简单测试,并使用fprintf将定时器触发的次数写入文件。但问题是计时器不会运行多次 我用定时器间隔参数设置定时器,如下所示。你认为问题出在哪里? 很抱歉,如果这是一个微不足道的问题,但我对vxWorks和Posix API还不熟悉,从某个时候起我就无法解决这个问题C vxWorks定时器:不能多次调用处理程序,c,timer,posix,vxworks,rtos,C,Timer,Posix,Vxworks,Rtos,我在VxWorks中使用定时器中断进行简单测试,并使用fprintf将定时器触发的次数写入文件。但问题是计时器不会运行多次 我用定时器间隔参数设置定时器,如下所示。你认为问题出在哪里? 很抱歉,如果这是一个微不足道的问题,但我对vxWorks和Posix API还不熟悉,从某个时候起我就无法解决这个问题 #include "schdlr.h" #define MILLION 1000000 /* one million = 10^6*/ #define s
#include "schdlr.h"
#define MILLION 1000000 /* one million = 10^6*/
#define sec_to_nsec 1000000000 /* ns to s conversion = 10^9 */
#define MAX_ISR_READ 1000000 /* Numbers of sample to save -- 20170123
prova con 1000000 = ~50 minuti*/
#define timer_sec 0.00332 /* e.g. 0.04 seconds = 40 ms //// 0.00332
seconds = 3.332 ms*/
#define FILENAME "SchedLog.txt"
#define FLUSH_TIME 10.0
#define SIG_LLP_TIMER SIGRTMIN
int isr_idx; /* counter of ISR occurred -- starts from 0 and
increments at each interrupt*/
volatile float clk_k, /* MY_CLOCK() value for the current sample*/
clk_k_1; /* MY_CLOCK() value for the previous sample*/
/*clock and timer values*/
struct itimerspec custom_itimerspec;
timer_t timer_id;
clockid_t USED_CLK;
struct timespec tv;
float a_n;
typedef struct TimeProbe
{
float max_value;
float min_value;
float curr_value;
float curr_mean;
float curr_std;
float prev_mean;
float prev_std;
unsigned int cnt;
} TimeProbe_t;
TimeProbe_t * ptp;
sem_t *sem;
const char* semName = "SCHEDULER_SEMAPHORE";
void init_timeProbe(TimeProbe_t* ptp)
{
ptp->cnt = 0;
ptp->curr_value = 0.0;
ptp->curr_mean = ptp->curr_std = ptp->max_value = ptp->prev_mean =
ptp->prev_std = 0.0;
ptp->min_value = 100000.0;
}
void init_data(TimeProbe_t* ptp)
{
isr_idx = 0;
USED_CLK = CLOCK_REALTIME;
clk_k = 1000.f;
clk_k_1 = 0.0;
/*TimeProbe_t struct init*/
init_timeProbe(ptp);
}
void fprintf_timeProbe(FILE* pfile, float clk, TimeProbe_t* ptp, int
flushRequestSent)
{
fprintf(pfile,"hi");
}
void timer_ISR( int sig, siginfo_t *si, void *uc)
{
isr_idx++;
}
void create_a_timer()
{
int status;
struct sigevent custom_sigevent;
struct sigaction act;
sigset_t mask;
long long nanosecs = (long long)(timer_sec *sec_to_nsec);
printf("Creating timer");
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = timer_ISR;
sigemptyset(&act.sa_mask);
if (sigaction(SIG_LLP_TIMER, &act, (struct sigaction *) NULL) == -1)
{
printf("sigaction fail\n");
}
custom_sigevent.sigev_notify = SIGEV_SIGNAL;
custom_sigevent.sigev_value.sival_ptr = &timer_id;
custom_sigevent.sigev_signo = SIG_LLP_TIMER;
status = timer_create(USED_CLK, &custom_sigevent, &timer_id);
if (status == -1)
printf("ERROR Create timer");
custom_itimerspec.it_value.tv_sec = 1;
custom_itimerspec.it_value.tv_nsec = 0;
custom_itimerspec.it_interval.tv_sec = 0;
custom_itimerspec.it_interval.tv_nsec = 500*1000*1000;
printf("settime starts\n");
status = timer_settime(timer_id, 0, &custom_itimerspec, NULL);
if (status == -1)
printf("ERROR Set timer");
}
void* thread_schdl(void)
{
FILE* pfile;
int toBeFlushed = 0;
float current_flush_time, last_flush_time;
timer_t timerid;
/*file stuff fopen*/
if((pfile = fopen(FILENAME,"w+"))==NULL)
{
printf("ERROR opening file. QUIT. \n", FILENAME);
}
else
printf("OK opening file\n");
fprintf(pfile,"Starting log of scheduling test on VxWorks OS\n");
create_a_timer();
sleep(10);
timer_cancel(timer_id);
fprintf(pfile,"Timer times %d \n",isr_idx);
/*file stuff fclose at the end - without this close it doesnot get created!*/
fclose(pfile);
exit(OK);
}
int main(void)
{
struct sched_param myParam;
int toBeFlushed = 0;
float current_flush_time, last_flush_time;
printf("Scheduler Test Starts on VxWorks OS for interval of 3.32ms \n");
ptp = (TimeProbe_t*)malloc(sizeof(TimeProbe_t));
sem = (sem_t*)malloc(sizeof(sem_t));
init_data(ptp); /*init data*/
Task_Schdlr = taskSpawn ("Task_Schdlr", 187, VX_FP_TASK, STACK_SIZE_T,
(FUNCPTR) thread_schdl,0,0,0,0,0,0,0,0,0,0);
if((Task_Schdlr == ERROR))
{
printf("taskSpawn failed\n");
}
else
{
myParam.sched_priority = 187;
/*sched_setscheduler(Task_Schdlr, SCHED_FIFO, &myParam);*/
}
if ((SEM_schdlr = sem_open("SEM_schdlr", O_CREAT, 0, 0)) == (sem_t *) -1)
{
printf("taskSpawn sem_open\n");
}
system("clear");
printf("ciao \n");
}
您的
sleep()
调用是否被第一个计时器信号中断,导致进程提前结束?另一种可能是调用sleep()
会干扰计时器。你能尝试其他阻止的方法吗,比如等待控制台的输入吗?谢谢Andrew的建议,我尝试了while(1),但遇到了同样的问题。请发布一个不处理信号量或调度等的方法,这样它只会显示你问到的问题。发布的代码包括一些自制的头文件schdlr.h
但未能发布该头文件的内容发布的代码很长且杂乱无章。因此,我会问:“硬件计时器是否设置为自动重复?”和“在中断处理程序处理过程中,代码是否重置“状态”标志,以便计时器可以再次“触发”?sleep()
调用被第一个计时器信号中断,导致进程提前结束?另一种可能是对sleep()
的调用干扰了计时器。您可以尝试其他阻止方法,例如等待控制台的输入吗?感谢Andrew的建议,我已经尝试了while(1),但也有同样的问题。请发布一个不处理信号量或调度等的文件,这样它只会显示您所问的问题。发布的代码包括一些自制的头文件;schdlr.h
,但无法发布头文件的内容发布的代码很长而且杂乱无章。因此我会问:硬件计时器是否设置为自动重复?“和”在中断处理程序处理期间,代码是否重置“状态”标志,以便计时器可以再次“触发”