Page 1 of 1

Any micro for system ticks to milliseconds?

Posted: Fri Aug 01, 2014 8:23 pm
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

Re: Any micro for system ticks to milliseconds?

Posted: Fri Aug 01, 2014 10:34 pm
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

Re: Any micro for system ticks to milliseconds?

Posted: Sat Aug 02, 2014 4:34 am
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.

Re: Any micro for system ticks to milliseconds?

Posted: Sat Aug 02, 2014 7:10 am
by Giovanni
Correct.

Giovanni

Re: Any macro for system ticks to milliseconds?

Posted: Sat Aug 02, 2014 9:33 am
by nikhil
Thanks :)

Re: Any micro for system ticks to milliseconds?

Posted: Wed Aug 20, 2014 8:38 am
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) :)