Olimex Support Forum

OLinuXino Android / Linux boards and System On Modules => A13 => Topic started by: maggocnx on July 08, 2013, 11:10:44 AM

Title: A13 Kernel High resolution timer
Post by: maggocnx on July 08, 2013, 11:10:44 AM
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 ?
Title: Re: A13 Kernel High resolution timer
Post by: oldpenguin on July 08, 2013, 12:10:57 PM
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
Title: Re: A13 Kernel High resolution timer
Post by: maggocnx on July 08, 2013, 02:46:57 PM
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?
Title: Re: A13 Kernel High resolution timer
Post by: gsbMechatr on July 23, 2015, 05:46:44 PM
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.
Title: Re: A13 Kernel High resolution timer
Post by: maggocnx on July 24, 2015, 09:26:53 AM
open menuconfig

make arch=ARM menuconfig

go down to kernel features

select High Resolution Time support. It's the second item from top
Title: Re: A13 Kernel High resolution timer
Post by: gsbMechatr on July 24, 2015, 12:10:50 PM
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

Title: Re: A13 Kernel High resolution timer
Post by: maggocnx on July 24, 2015, 01:49:11 PM
its not configured at runtime. It has to be compiled into the kernel.