Running a Thread at 400Hz for sampling

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Running a Thread at 400Hz for sampling

Post by Tabulous »

Im currently trying to get a thread to loop at 400Hz (need to sample at this frequency).

chconf is currently setup like this.

Code: Select all

/**
 * @brief   System time counter resolution.
 * @note    Allowed values are 16 or 32 bits.
 */
#define CH_CFG_ST_RESOLUTION                32//16

/**
 * @brief   System tick frequency.
 * @details Frequency of the system timer that drives the system ticks. This
 *          setting also defines the system tick time unit.
 */
#define CH_CFG_ST_FREQUENCY                 1000

/**
 * @brief   Time delta constant for the tick-less mode.
 * @note    If this value is zero then the system uses the classic
 *          periodic tick. This value represents the minimum number
 *          of ticks that is safe to specify in a timeout directive.
 *          The value one is not valid, timeouts are rounded up to
 *          this value.
 */
#define CH_CFG_ST_TIMEDELTA                 0//2


so the system tick only has a granularity of 1ms, and thus closest i can get to 400Hz is when using chThdSleepMilliseconds( 2 );

What is teh best way to achieve this ? can one just adjust the CH_CFG_ST_FREQUENCY to say 10000, this will give more granularity, and thus allow to achieve 400Hz ?
User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Has thanked: 83 times
Been thanked: 67 times

Re: Running a Thread at 400Hz for sampling

Post by RoccoMarco »

Hi,
you can use a GPT in order to have a periodic callback without impact on the OS
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Running a Thread at 400Hz for sampling

Post by Giovanni »

Hi,

In tickless mode you can increase the frequency even to 4-16MHz but you have to work with the delta parameter in case of problems (by increasing it).

100khZ should be fine without tweaking.

Anyway, you should consider using HW triggering for ADC using a GPT, this way you don't need software timings at all, measurements would have way less jitter.

Giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: Running a Thread at 400Hz for sampling

Post by Tabulous »

should have been more clear, sampling is for Accelerometer using I2C bus.

Can I2C calls be made in callbacks ? As callback are I-Class ?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Running a Thread at 400Hz for sampling

Post by Giovanni »

Correct, callbacks require I-Class and the I2C driver does not support that. In that case you need a thread.

100kHz should be fine on M3s or M4s, M0s could require an increase in delta.

Giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: Running a Thread at 400Hz for sampling

Post by Tabulous »

Giovanni wrote:Correct, callbacks require I-Class and the I2C driver does not support that. In that case you need a thread.

100kHz should be fine on M3s or M4s, M0s could require an increase in delta.

Giovanni


when you say 100Khz are you talking about the system tick frequency ?

also what would you recommend for the delta ?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Running a Thread at 400Hz for sampling

Post by Giovanni »

Yes, the system tick frequency in tickless mode.

Delta should start at 2 (with assertions enabled), if virtual timer assertions are triggered then is can be progressively increased. This could be required if a lot of virtual timers are used or if callbacks are not short functions, it should not be your case.

Giovanni
Post Reply