Page 1 of 1
translate Delay() to chibios 3.0
Posted: Sat Nov 14, 2015 9:19 pm
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();
}
Re: translate Delay() to chibios 3.0
Posted: Sat Nov 14, 2015 10:28 pm
by Giovanni
Hi,
It is chSysIsCounterWithinX(), there is also chSysPolledDelayX() that replaces all that code.
Giovanni
Re: translate Delay() to chibios 3.0
Posted: Sun Nov 15, 2015 9:46 am
by fastlink30
Thank you Giovanni, i want use chSysPolledDelayX, but how to convert uS to cycles? US2RTC is correct?
Re: translate Delay() to chibios 3.0
Posted: Sun Nov 15, 2015 10:00 am
by Giovanni
Yes, it is the same mechanism moved into the kernel.
Giovanni
Re: translate Delay() to chibios 3.0
Posted: Thu Feb 11, 2016 4:45 pm
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
Re: translate Delay() to chibios 3.0
Posted: Thu Feb 11, 2016 5:07 pm
by Giovanni
Yes, use US2RT().
Giovanni
Re: translate Delay() to chibios 3.0
Posted: Thu Feb 11, 2016 6:44 pm
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..
Re: translate Delay() to chibios 3.0
Posted: Thu Feb 11, 2016 7:07 pm
by Giovanni
It is US2RT and you must specify the right clock frequency.
Giovanni
Re: translate Delay() to chibios 3.0
Posted: Thu Feb 11, 2016 7:11 pm
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
Re: translate Delay() to chibios 3.0
Posted: Thu Feb 11, 2016 8:21 pm
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