Page 1 of 1
Timer with I2C Sampling
Posted: Sun Feb 07, 2016 6:09 am
by MobileWill
I have been working on a project were I need to sample data from the I2C bus at 10Hz exactly. I had tried with a GPT and Virtual Timer but then quickly learned I can't do I2C reads from there. I am using a thread with a deadline delay but it is a bit jittery. Not much but enough where it will affect my calculations.
What is the best way to get a stable 10Hz sample without waiting for the OS to switch over and be able to do an I2C read? The GPT solution would be great and stable if there is a way to read sensor in it.
Thanks for the help.
Re: Timer with I2C Sampling
Posted: Sun Feb 07, 2016 6:16 am
by MobileWill
I realized I had my chThdSleepUntil(time); before my sample code ran. Moving it to after the sampling seemed to stabilize it. But as my other threads get longer I I may run into this again so I would rather use a GPT. Is there a way to do it?
Re: Timer with I2C Sampling
Posted: Sun Feb 07, 2016 8:09 am
by Giovanni
Hi,
You can't use I2C from ISR context, you need a thread. Try the second solution in this article, it should be low jitter if the thread has higher priority than other threads.
http://www.chibios.org/dokuwiki/doku.ph ... :kb:timingGiovanni
Re: Timer with I2C Sampling
Posted: Sun Feb 07, 2016 8:32 am
by MobileWill
Thanks. I got the 2nd example working. Just had to update chTimeNow for the new function name in 3.x. chVTGetSystemTimeX();
In the example the thread function looks different than I have been using. Is it a different way or older way of creating a thread function?
static THD_FUNCTION(Thread1, arg) vs msg_t my_thread(void *param) {
Re: Timer with I2C Sampling
Posted: Sun Feb 07, 2016 9:56 am
by Giovanni
Creating threads works both ways, the macro could encapsulate compiler-specific optimizations so it is preferred.
I fixed the article too, thanks.
Giovanni