I have Xenomai installed in an ARM PC (Xenomai 2.5.6 - Linux Kernel 2.6.35.9) and I need to read a 10 kHz clock signal. The signal is electrically connected to one of my GPIO's, which is mapped to a system file. If I create a task in user-space and open-read-close the file while measuring the time I get an average delay of 650 µs (i.e. this is the time that takes a full open-read-close cycle). This yields to a sampling rate of ~1.5 kHz.
while(1) // Task's infinite loop
{
t1 = rt_timer_read();
if((fd = open("test_file",O_RDONLY)) > 0)
{
read(fd,&buff,1);
close(fd);
}else{
errors++;
}
t2 = rt_timer_read();
t += t2-t1;
rt_task_wait_period(NULL);
}
Output:
[RT:] Start reading files: 05:19:05.804.754
[RT:] End reading files: 05:19:13.338.078
[RT:] Average time (10000 open-read-close cycles): 00000671.901 (microseconds)
[RT:] Errors found: 0
[RT:] (sig_handler) Signal received! (signo = 2)
I've read somewhere in the Internet that forcing my task to run in Kernel-space rather than user-space would enable it for a faster execution, but I'm not sure if this would be enough, nor if what I'm trying to do here is completely right.
I come from an electronics engineering background where I have always been told not to treat synchronous signals (like clock signals) asynchronously, and reading a file (i.e. my sync. signal) using a periodic task that may introduce jitter depending on the CPU/system load, doesn't look like a good approach. Is there a better way to do so?
Thanks!
Here are my thoughts on a possible solution. You are right you shouldn't handle synchronous signals asynchronously. Therefore, you should avoid opening a fd in userspace to read the signal level from the GPIO.
Instead you should handle this in the kernel space where it's more appropriate. Once in the kernel space you can setup the GPIO as an irq to trigger on rising/falling edge and handle it in real time through Xenomai. The latency won't disappear but it should be OK for 10 KHz clock speed.
Unfortunately, you didn't specify what you are doing with the clock signal. I'm guessing you are trying to act as a slave drive to some device?
Any way I hope I've been helpful.