How to work with virtual timers?

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

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

francie84
Posts: 3
Joined: Wed Feb 07, 2018 4:12 pm

How to work with virtual timers?

Postby francie84 » Sat Feb 17, 2018 2:51 pm

Hi,
i'm using the last available ChibiStudio release (With ChibiOS eclipse tools 2.5.0) and a STM32F401RE Nucleo board.
I'm working on the following code:

Code: Select all

#include "ch.h"
#include "hal.h"
#include "chprintf.h"
BaseSequentialStream* chp = (BaseSequentialStream*) &SD2;
static virtual_timer_t led_vt;
/*
*  timer callback.
*/
static void cb(void *arg) {
chprintf(chp,"Hi!\r\n");
chSysLockFromISR();
chVTSetI(&led_vt, MS2ST(500), cb, NULL);
chSysUnlockFromISR();
}
/*
* Application main function.
*/
int main(void) {
/*
* System initializations.
* - Kernel initialization, the main() function becomes a thread * RTOS is active. Interrupts are enabled on chSysInit() exit.
*/
chSysInit();
/*
* Activates the serial driver 2 using the driver default configuration.
*/
sdStart(&SD2, NULL);
/* timer initialization.*/
chVTObjectInit(&led_vt);
/* Starting .*/
chVTSet(&led_vt, MS2ST(500), cb, NULL);
}


That's supposed to display " hi " every 500ms, but it's not working, i can't see anything on my terminal.
Do i have to enable any particular driver or something for Virtual Timers?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: How to work with virtual timers?

Postby Giovanni » Sat Feb 17, 2018 3:16 pm

Hi,

Enable state checker and assertions, those would reveal that you cannot call blocking functions from ISR context (writing on a stream).

Giovanni

francie84
Posts: 3
Joined: Wed Feb 07, 2018 4:12 pm

Re: How to work with virtual timers?

Postby francie84 » Sat Feb 17, 2018 3:44 pm

Giovanni wrote:Hi,

Enable state checker and assertions, those would reveal that you cannot call blocking functions from ISR context (writing on a stream).

Giovanni


Thank you Giovanni, can you tell me a bit more?
I need that timer to set a float variabile when the callback occours, and i used that chprintf to prove the timer was working fine.. i also read that way is the good one to re-set the timer (i mean using chSysLock and chSysUnlock in the function).. where am i wrong?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: How to work with virtual timers?

Postby Giovanni » Sat Feb 17, 2018 5:29 pm

Hi,

Writing is a blocking operation, you cannot block in an ISR, there is not much else to say.

What you can do is to wakeup a thread that then does the writing.

Giovanni

User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Location: Munich (Germany)
Has thanked: 83 times
Been thanked: 67 times
Contact:

Re: How to work with virtual timers?

Postby RoccoMarco » Sun Feb 18, 2018 12:54 am

Hi,
I can see a couple of problems problems.
  • halInit is missing
  • there is not the loop inside the main
Ciao,
RM


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 12 guests