Page 1 of 1

How to work with virtual timers?

Posted: Sat Feb 17, 2018 2:51 pm
by francie84
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?

Re: How to work with virtual timers?

Posted: Sat Feb 17, 2018 3:16 pm
by Giovanni
Hi,

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

Giovanni

Re: How to work with virtual timers?

Posted: Sat Feb 17, 2018 3:44 pm
by francie84
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?

Re: How to work with virtual timers?

Posted: Sat Feb 17, 2018 5:29 pm
by Giovanni
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

Re: How to work with virtual timers?

Posted: Sun Feb 18, 2018 12:54 am
by RoccoMarco
Hi,
I can see a couple of problems problems.
  • halInit is missing
  • there is not the loop inside the main