translate Delay() to chibios 3.0

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
fastlink30
Posts: 42
Joined: Tue Aug 12, 2014 1:25 pm

translate Delay() to chibios 3.0

Post by fastlink30 »

how to translate this code for chibios 3.0?

Code: Select all

static void Delay_us(uint32_t us)
{
    chSysLock();

    uint32_t start = halGetCounterValue();
    uint32_t timeout  = start + US2RTT(us);
    while (halIsCounterWithin(start, timeout))
        asm("nop");

    chSysUnlock();
}
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: translate Delay() to chibios 3.0

Post by Giovanni »

Hi,

It is chSysIsCounterWithinX(), there is also chSysPolledDelayX() that replaces all that code.

Giovanni
fastlink30
Posts: 42
Joined: Tue Aug 12, 2014 1:25 pm

Re: translate Delay() to chibios 3.0

Post by fastlink30 »

Thank you Giovanni, i want use chSysPolledDelayX, but how to convert uS to cycles? US2RTC is correct?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: translate Delay() to chibios 3.0

Post by Giovanni »

Yes, it is the same mechanism moved into the kernel.

Giovanni
kennethlimcp
Posts: 6
Joined: Thu Feb 11, 2016 4:33 pm

Re: translate Delay() to chibios 3.0

Post by kennethlimcp »

Hey Giovanni,

Do i use US2RT() for chSysPolledDelayX()? If not, what should be the correct way to use chSysPolledDelayX() to create microsecond delays?

Thanks!
ken
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: translate Delay() to chibios 3.0

Post by Giovanni »

Yes, use US2RT().

Giovanni
kennethlimcp
Posts: 6
Joined: Thu Feb 11, 2016 4:33 pm

Re: translate Delay() to chibios 3.0

Post by kennethlimcp »

Code: Select all

while(TRUE){
   palWritePad(GPIOC, GPIOC_EINK_CKV, TRUE);
   chSysPolledDelayX(US2ST(5));
   palWritePad(GPIOC, GPIOC_EINK_CKV, FALSE);
   chSysPolledDelayX(US2ST(5));
   }


Opps my typo. Should be `US2ST()`?

I did this but the logic analyzer shows a waveform of 0.25us delay..
Last edited by kennethlimcp on Thu Feb 11, 2016 7:12 pm, edited 1 time in total.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: translate Delay() to chibios 3.0

Post by Giovanni »

It is US2RT and you must specify the right clock frequency.

Giovanni
kennethlimcp
Posts: 6
Joined: Thu Feb 11, 2016 4:33 pm

Re: translate Delay() to chibios 3.0

Post by kennethlimcp »

I cannot find `US2RT()` are you able to point me to the correct file? The compiler throws an error as well when i use it.

Would appreciate if you can point out any incorrect settings for the clock frequency - https://github.com/kennethlimcp/ChibiOS ... /mcuconf.h
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: translate Delay() to chibios 3.0

Post by Giovanni »

Sorry, there is a missing C:

Code: Select all


/**
 * @brief   Microseconds to realtime counter.
 * @details Converts from microseconds to realtime counter cycles.
 * @note    The result is rounded upward to the next microsecond boundary.
 * @note    The macro assumes that @p freq >= @p 1000000.
 *
 * @param[in] freq      clock frequency, in Hz, of the realtime counter
 * @param[in] usec      number of microseconds
 * @return              The number of cycles.
 *
 * @api
 */
#define US2RTC(freq, usec) (rtcnt_t)((((freq) + 999999UL) / 1000000UL) * (usec))


It is in chsys.h.

Giovanni
Post Reply