Page 1 of 1

Running a Thread at 400Hz for sampling

Posted: Tue Jul 19, 2016 9:07 am
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 ?

Re: Running a Thread at 400Hz for sampling

Posted: Tue Jul 19, 2016 9:11 am
by RoccoMarco
Hi,
you can use a GPT in order to have a periodic callback without impact on the OS

Re: Running a Thread at 400Hz for sampling

Posted: Tue Jul 19, 2016 9:12 am
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

Re: Running a Thread at 400Hz for sampling

Posted: Tue Jul 19, 2016 9:25 am
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 ?

Re: Running a Thread at 400Hz for sampling

Posted: Tue Jul 19, 2016 9:45 am
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

Re: Running a Thread at 400Hz for sampling

Posted: Tue Jul 19, 2016 10:13 am
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 ?

Re: Running a Thread at 400Hz for sampling

Posted: Tue Jul 19, 2016 11:17 am
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