A13 Kernel High resolution timer

Started by maggocnx, July 08, 2013, 11:10:44 AM

Previous topic - Next topic

maggocnx

Hello,

i am trying to toggle an output with high resolution timers. If i measure the interval with an oscilloscope i don't get an interval smaller then 10 ms. The interval jumps even between 10 and 20ms when i enter a interval smaller then 10ms.
I need an interval around 500us  to drive a thermal printer.

Here is the my code


#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>

unsigned long timer_interval_ns = 1e6 //1 ms;
static struct hrtimer hr_timer;

enum hrtimer_restart timer_callback( struct hrtimer *timer_for_restart )
{
  ktime_t currtime , interval;
  currtime  = ktime_get();
  interval = ktime_set(0,timer_interval_ns);
  hrtimer_forward(timer_for_restart, currtime , interval);
set_pin_value(PIO_G,9,(cnt++ & 1)); //I use oscilloscope to meassure frequency here
return HRTIMER_RESTART;
}

static int __init timer_init(void) {
ktime = ktime_set( 0, timer_interval_ns );
hrtimer_init( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
hr_timer.function = &timer_callback;
hrtimer_start( &hr_timer, ktime, HRTIMER_MODE_REL );
return 0;
}

static void __exit timer_exit(void) {
int ret;
  ret = hrtimer_cancel( &hr_timer );
  if (ret) printk("The timer was still in use...\n");
  printk("HR Timer module uninstalling\n");

}

module_init(timer_init);
module_exit(timer_exit);



Anybody any idea why i dont get my desired interval in nanoseconds ?  Do i need to adjust any hardware timers ?

oldpenguin

Hello maggocnx

I think its more a Linux than problem than a platform problem.
You have to enable HRT (high resolution timers) in the kernel.
If you google around a bit you will find many articles (like this one https://export.writer.zoho.com/public/rreginelli/Chapter-5---High-Resolution-Timers-Final1/fullpage) that explain why you can't achive better than ms resolution in standard configured kernel, and the solution.
Hope this helps

maggocnx

Thanks oldpenguin,

you made my day. The interval is exactly the value in nanoseconds i set, after i enabled HRT in the kernel. Why insn't HRT enabled by default? Is there any disadvantage?

gsbMechatr

Hi Maggocnx,

I know it has been awhile since anyone posted here - I am also looking into toggling an output with a High Resolution timer - is there any chance you can remember how you went about enabling HRT's in the kernal? The link posted in a reply to your initial message does not exist anymore.

Sorry for commenting on an old thread, help would be appreciated.

maggocnx

open menuconfig

make arch=ARM menuconfig

go down to kernel features

select High Resolution Time support. It's the second item from top

gsbMechatr

Thanks for your reply!

Do you perhaps know where the source files are located on the official olimex A13 debian installation?

the dir /usr/src/linux does not exist on my installation. Not sure in which directory I should be to make menuconfig


maggocnx

its not configured at runtime. It has to be compiled into the kernel.