我正在使用定时器中断在VxWorks中进行简单测试,并使用fprintf将定时器触发的次数写入文件。但是问题在于计时器不会运行超过一次。
我用定时器间隔参数设置了定时器,如下所示。您认为出了什么问题? 抱歉,这是一个琐碎的问题,但是我对vxWorks和Posix api还是陌生的,从某个时候开始,我无法解决此问题。
#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");
}