Timer with I2C Sampling

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
MobileWill
Posts: 27
Joined: Sat Jan 30, 2016 8:44 am

Timer with I2C Sampling

Post 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.
MobileWill
Posts: 27
Joined: Sat Jan 30, 2016 8:44 am

Re: Timer with I2C Sampling

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Timer with I2C Sampling

Post 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:timing

Giovanni
MobileWill
Posts: 27
Joined: Sat Jan 30, 2016 8:44 am

Re: Timer with I2C Sampling

Post 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) {
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Timer with I2C Sampling

Post 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
Post Reply