Page 1 of 1

High precision timings

Posted: Sat Mar 29, 2014 7:04 pm
by kab
hiya!

I would like to add DHT22 sensor to my project (based on custom stm32f103ret6 mcu).
DHT22 use custom 1-wire communication, to start you have to send 500 us low and 40 us high impulse on pin.
I've read this: http://www.chibios.org/dokuwiki/doku.ph ... :kb:timing

And write following code in thread:

Code: Select all

     palClearPad(IOPORT, IOPIN);
     time = chTimeNow() + US2ST(500);
     chThdSleepUntil(time);

     palSetPad(IOPORT, IOPIN);
     time = chTimeNow() + US2ST(40);
     chThdSleepUntil(time);


But this not works, communication did not start. I take logic analizer and see, that width of impulse is much more high, than expected.

I understand, that my code did not works, because other thread are running (i've use USB-CDC example as template, because i need USB CDC too) and current thread did not got executed at proper time, when timeout is over.

I've found 2 workaround, both of them have some disadvantages:
1. set this thread prio to high (thread start communication, run ICU driver and sleep for a while). this is works, but sometimes USB communication became unstable.
2. change CH_FREQUENCY to 1000000 in chconf.h. This works ok, but as i understand, too fast task switching is not ok.

I cannot use VT, because after timeout i need to start ICU and i cannot start it from interrupt handler routine.

Is there any other way to get this code works and keep USB stable and task switching regular?

Re: High precision timings

Posted: Sun Mar 30, 2014 8:12 am
by Giovanni
Hi,

You have three ways:
1) GPT driver.
2) The function halPolledDelay() (not present on all platforms).

Note that interrupt servicing will add a *lot* of Jitter to those timings.

3) I would consider a state machine driven by a "fast" interrupt generated by an hardware timer.

Giovanni

Re: High precision timings

Posted: Sun Mar 30, 2014 11:46 am
by kab
Thanks for reply.

But i miss what difference between VT, GPT and fast hardware driving state machine for this purpose.
Anyway i got callback in right time, but inside ISR and i cannot start ICU inside it.
After second dfelay (40 us) i have to start ICU iimmediately, otherwise i lost data.
Since this is 1-wire, can i did not stop ICU, but only change pin type (from input to output), make proper delays, configure pin to input back and hope that ICU will still work?

Re: High precision timings

Posted: Sun Mar 30, 2014 11:53 am
by Giovanni
Inside callbacks you can only call i-class functions, so you cannot start ICU there, you have to start the driver at application level.

GPT is a driver around an HW timer (high resolution).
VT are software timer (low resolution).

Giovanni

Re: High precision timings

Posted: Sun Mar 30, 2014 6:08 pm
by kab
So, main problem is return to exact task (get task with ICU executed) right after callback with timer.
How to do that correctly? Set thread prio to high and wait for binary semaphore (and release semaphore from ISR)?

Re: High precision timings

Posted: Sun Mar 30, 2014 7:17 pm
by Giovanni
ICU does not belong to a specific task. You may also not need a task at all if you implement the protocol using a state machine driven by the ICU/GPT callbacks.

Are you familiar with such an approach?

Giovanni

Re: High precision timings

Posted: Mon Mar 31, 2014 9:34 am
by kab
ICU in my project handled in specific task (where i start and stop it via icuStart and icuStop). So, in my case i need to return to this task to call icuStart after second timeout in 40 us.

Readed http://www.chibios.org/dokuwiki/doku.ph ... tos:wakeup and do as described via chSchGoSleepS/chSchReadyI.
Seems to be working (for timer i use GPT driver).

Yes, i know what is state machine and how to use it, but i still need place to safe call to icuStart. Seems that task is good place for that.