Task timing in FreeRTOS


I have a question about the time sensitive application. I am using stm32f303 uC. I use two timers for counting pulses that is 3-7 micro seconds wide. One of the timers use the pulses as a clock source and the other timer reads the count value accumulated on the first timer in every 100ms. I have around 100 pulses for every 100ms, but the number of pulses can even go up to 700-800, i.e. new pulse in every 0.12ms.

On the other hand, I would like to use one of the ADCs to measure the peak value of an input pulse . There is a peakholder circuit that captures the peak of the incoming pulse. When there is a pulse, I will start ADC conversion and store the digitized data on the uC to create an histogram. I am guessing the conversion time to be up to 50 micro seconds.

Finally there will be a modbus communication for sending the pulse count in the first task in every 100ms. Also, I would like to send the histogram channel counts via the same modbus channel.

I’ve never used RTOS before. Is it possible to build such system with a microsecond time sensivity in RTOS? Stm32f303 has a 72MHz clock.

Sorry for the long post.
I would be glad for any help-comment! Thank you.

It sounds like all the tight timing should all be happening at the HARDWARE level if you design things properly. FreeRTOS, being designed as a highly portable system, doesn’t have support for that design, but also doesn’t do anything to prevent you from coding that sort of design yourself inside your application.

Hello ,

I have the hardware design that produces the waveforms. You can see the screen shot below.

Pink signal is the actual data itself.
The yellow one is the signal flag. İndicating there is a signal. (Note the voltage scale.)
The green one is the peak holder. That will go to the ADC.

As you can see the signal width is really narrow. Therefore, uC has to poll the input pin with a micro second sensitivity. So, it will not miss the count.

However, as far as I understand RTOS creates tasks and switches between those. Could it poll the pin with 1us sensivity? Along with the other tasks I mentioned above.

You do NOT want the computer trying to poll for that sort of signal. While you could put the signal flag into in interrupt pin and try to do the processing in the ISR, the best option is to use the hardware capability of the processor.

Use the signal flag to directly trigger the ADC converter. Put the signal flag into the count input of a counter-timer of the chip to count them, and then sample/reset that count with another timer generating a time base.

To try to poll at 1 us basically says you need to dedicate the processor to doing that all the time, as you won’t have time to switch to something else in between the sample times.

Moving to the kernel category.

Dear Richard Damon,

Thank you for your response. The sequence you explained was the actual plan for the project. However, on top of that there will be a modbus comm protocol that should transmit the counts as well as the histogram of the peaks generated with the ADC data. Hence, I thought that real time OS could be useful. But from your answer I am assuming that RTOS cannot be faster than the mentioned hardware implementation for uC.

An RTOS lets you rapidly respond to stimuli. If you have hardware to handle it it would be quicker.

You COULD have the signal trigger an interrupt which unblocks a task that does the steps. It might be fast enough to do what you want, but the speed of your processor will be a limiting factor.

Note, your description said “Poll”, and RTOSes don’t like polling, as that is a costly operation. To POLL at a 1 us resolution would require at least 2 context switches per microsecond, which almost surely too fast for your processor. Responding to an interrupt in a couple of microseconds is a different matter.