How does the system timer works?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

How does the system timer works?

Post by robu »

Hi,

Long times ago I played with ChibiOS/RT and RPI, now I got some time to dig a bit deeper. Before I used Microchip PIC which comes with an IDE, but I don't want to stuck with the PIC so I started with RPI with GCC cross compiler.

I had a look on the RPI port and I found the IRQ handler in the hal_lld.c and I have a few questions regarding the code. In the ChibiOS/RT documentation is written that all the interrupts handler has to be defined in the macro.

Code: Select all

CH_IRQ_HANDLER(IrqHandler)
{
  CH_IRQ_PROLOGUE();

  systimer_serve_interrupt();

#if HAL_USE_SERIAL
  sd_lld_serve_interrupt(&SD1);
#endif

#if HAL_USE_I2C
  i2c_lld_serve_interrupt(&I2C0);
#endif

#if HAL_USE_SPI
  spi_lld_serve_interrupt(&SPI0);
#endif

#if HAL_USE_GPT
  gpt_lld_serve_interrupt();
#endif

  CH_IRQ_EPILOGUE();
}


Is the IrqHandler is defined in the vector.s file?

Regarding the code above, if a hardware interruption has occurred the systimer_serve_interrupt function will be called

void chSysTimerHandlerI (void)
Handles time ticks for round robin preemption and timer increments.


If I enabled only the interruption of the system timer and initialize the timer to have 1ms interruption, which mean that the chSysTimerHandlerI will be called every 1ms. But if I enabled other interruption like SPI, UART etc, than the interruption occurs more often than 1ms which means that the chSysTimerHandlerI will be called more often. My question is how accurate the OS time/virtual timers?

Code: Select all

static void systimer_serve_interrupt( void )
{
  // Update the system time
  chSysLockFromIsr();
  chSysTimerHandlerI();
  chSysUnlockFromIsr();

  // Clear timer interrupt
  ARM_TIMER_CLI = 0;
}


Regards,
Robert
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: How does the system timer works?

Post by Giovanni »

Hi,

Apparently you are right, the function should check the interrupt source before invoking chSysTimerHandlerI().

My question is how accurate the OS time/virtual timers?


Totally unpredictable.

Note that the RPI port is not yet an officially supported one so the code is not in the ChibiOS repository. You should contact the author about this issue, the bug has to be fixed in his repository.

Giovanni
rvanspaa
Posts: 11
Joined: Mon Apr 24, 2017 7:51 am

Re: How does the system timer works?

Post by rvanspaa »

Hi,

This is my first post, and I'm also dabbling with the RPi, and have come across the same problem. I corrected for the time by checking for timer interrupts before calling chVTDoTickI(). Checking at a higher level i.e. before calling chSysTimerHandlerI() has the effect of killing the serial IO for some reason I still haven't found. (I think it may be related to thread swapping.)
Robert, since you were doing all this over 3 years ago, perhaps you could let me know how you progressed?
(Or Steve, if you are watching?)
Post Reply