Any micro for system ticks to milliseconds?

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
nikhil
Posts: 40
Joined: Thu Jan 03, 2013 8:46 pm

Any micro for system ticks to milliseconds?

Post by nikhil »

Is there any way/micro to convert System tick to milliseconds/microseconds?
I found MS2ST() to work other way round but could not find any micro for system ticks to milliseconds/microseconds. I am using chibios 2.6.5


--
nikhil
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Any micro for system ticks to milliseconds?

Post by Giovanni »

Hi,

The macros have been introduced in 3.0:

Code: Select all

/**
 * @name    Time conversion utilities
 * @{
 */
/**
 * @brief   Seconds to system ticks.
 * @details Converts from seconds to system ticks number.
 * @note    The result is rounded upward to the next tick boundary.
 *
 * @param[in] sec       number of seconds
 * @return              The number of ticks.
 *
 * @api
 */
#define S2ST(sec)                                                           \
  ((systime_t)((uint32_t)(sec) * (uint32_t)CH_CFG_ST_FREQUENCY))

/**
 * @brief   Milliseconds to system ticks.
 * @details Converts from milliseconds to system ticks number.
 * @note    The result is rounded upward to the next tick boundary.
 *
 * @param[in] msec      number of milliseconds
 * @return              The number of ticks.
 *
 * @api
 */
#define MS2ST(msec)                                                         \
  ((systime_t)(((((uint32_t)(msec)) *                                       \
                 ((uint32_t)CH_CFG_ST_FREQUENCY) - 1UL) / 1000UL) + 1UL))

/**
 * @brief   Microseconds to system ticks.
 * @details Converts from microseconds to system ticks number.
 * @note    The result is rounded upward to the next tick boundary.
 *
 * @param[in] usec      number of microseconds
 * @return              The number of ticks.
 *
 * @api
 */
#define US2ST(usec)                                                         \
  ((systime_t)(((((uint32_t)(usec)) *                                       \
                 ((uint32_t)CH_CFG_ST_FREQUENCY) - 1UL) / 1000000UL) + 1UL))

/**
 * @brief   System ticks to seconds.
 * @details Converts from system ticks number to seconds.
 * @note    The result is rounded up to the next second boundary.
 *
 * @param[in] n         number of system ticks
 * @return              The number of seconds.
 *
 * @api
 */
#define ST2S(n) ((((n) - 1UL) / CH_CFG_ST_FREQUENCY) + 1UL)

/**
 * @brief   System ticks to milliseconds.
 * @details Converts from system ticks number to milliseconds.
 * @note    The result is rounded up to the next millisecond boundary.
 *
 * @param[in] n         number of system ticks
 * @return              The number of milliseconds.
 *
 * @api
 */
#define ST2MS(n) ((((n) - 1UL) / (CH_CFG_ST_FREQUENCY / 1000UL)) + 1UL)

/**
 * @brief   System ticks to microseconds.
 * @details Converts from system ticks number to microseconds.
 * @note    The result is rounded up to the next microsecond boundary.
 *
 * @param[in] n         number of system ticks
 * @return              The number of microseconds.
 *
 * @api
 */
#define ST2US(n) ((((n) - 1UL) / (CH_CFG_ST_FREQUENCY / 1000000UL)) + 1UL)
/** @} */


Giovanni
nikhil
Posts: 40
Joined: Thu Jan 03, 2013 8:46 pm

Re: Any micro for system ticks to milliseconds?

Post by nikhil »

Great!

I suppose I should be able to use them in 2.6.5 just by replacing CH_CFG_ST_FREQUENCY to CH_FREQUENCY.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Any micro for system ticks to milliseconds?

Post by Giovanni »

Correct.

Giovanni
nikhil
Posts: 40
Joined: Thu Jan 03, 2013 8:46 pm

Re: Any macro for system ticks to milliseconds?

Post by nikhil »

Thanks :)
User avatar
TexZK
Posts: 57
Joined: Sat Sep 22, 2012 7:06 pm

Re: Any micro for system ticks to milliseconds?

Post by TexZK »

@ Giovanni: will you backport them to 2.6.x? I think it's painless, yet useful (I needed such macros in almost all of my projects) :)
Post Reply