How can I generate delay in microsecond scale in MQX? _time_delay() works in ms only. I need it for 1-wire memory protocol.
Typically, default BSP resolution for an MQX tick is 5 ms. So, _time_delay_ticks(1) would put the task back to ready queue 5 milliseconds after the call to it. To get a finer resolution, you can use _time_get_microseconds or _time_get_nanoseconds functions, or even _time_get_hwticks.
If your time delay range is in microseconds, a wait function like the example below works. On TWR-MCF52259 this code gives me about 6 microseconds delay. This kind of delay function should be considered as a minimum delay, due to possible MQX preemptions by higher priority task(s), MQX interrupt(s) and so forth.
#define MIN_DELAY_HWTICKS (10)
/* write logical 1 to the pin - initial state */
lwgpio_set_value(&led1, LWGPIO_VALUE_HIGH); /* set pin to 1 */
/* write logical 0 to the pin - falling edge - delay starts */
lwgpio_set_value(&led1, LWGPIO_VALUE_LOW); /* set pin to 0 */
/* do nothing for minimum 10 hwticks plus some software overhead */
old_hwticks = _time_get_hwticks();
diff_hwticks = 0;
while(MIN_DELAY_HWTICKS > diff_hwticks)
hwticks = _time_get_hwticks();
/* get the time elapsed from old_hwticks.
* we need to consider a possible occurrence of a tick
* between the two _time_get_hwticks() calls.
* if a tick occurs between the two, it is possible that old_hwticks
* is a greater value than the actual hwticks
* (because a tick resets hwticks to 0)
diff_hwticks = hwticks>old_hwticks?(hwticks-old_hwticks):((int_32)_time_get_hw_ticks_per_tick()-old_hwticks+hwticks);
/* write logical 1 to the pin */
Retrieving data ...