ChibiOS public support forum for all topics not covered by a specific support forum.
Moderators: RoccoMarco , lbednarz , tfAteba
fastlink30
Posts: 42 Joined: Tue Aug 12, 2014 1:25 pm
Post
by fastlink30 » Sat Nov 14, 2015 9:19 pm
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(); }
Giovanni
Site Admin
Posts: 14893 Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times
Post
by Giovanni » Sat Nov 14, 2015 10:28 pm
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
Post
by fastlink30 » Sun Nov 15, 2015 9:46 am
Thank you Giovanni, i want use chSysPolledDelayX, but how to convert uS to cycles? US2RTC is correct?
kennethlimcp
Posts: 6 Joined: Thu Feb 11, 2016 4:33 pm
Post
by kennethlimcp » Thu Feb 11, 2016 4:45 pm
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
kennethlimcp
Posts: 6 Joined: Thu Feb 11, 2016 4:33 pm
Post
by kennethlimcp » Thu Feb 11, 2016 6:44 pm
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.
Giovanni
Site Admin
Posts: 14893 Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times
Post
by Giovanni » Thu Feb 11, 2016 7:07 pm
It is US2RT and you must specify the right clock frequency. Giovanni
Giovanni
Site Admin
Posts: 14893 Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times
Post
by Giovanni » Thu Feb 11, 2016 8:21 pm
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