Fastest way to sample data and process it


What is the fastest way to sample data periodically and do the DSP calculations? What I am doing is this, I am obtaining data from an external ADC. This data is used in a control loop. Here are some of my ideas

1) Using GP Timer and callback - every sampling interval, timer callback is executed - read adc in the callback and process data there? The problem here is that, shouldn't I keep callback as short as possible? I also noticed that I am not able to output serial data using UART framework API function that's called inside the callback. It would be nice if I could do this.

2) Using GP Timer, callback, semaphore and thread - every sampling interval, timer callback is executed. Using semaphore, transfer to the thread. I read ADC and do the process in the thread instead of doing it in the callback. The problem is I think using the semaphore and transferring from callback to thread will slower things down. Also, I'm not sure if the time it takes to transfer from callback to thread is a fixed amount of time? 

3)Do away with callback and semaphores - The idea here is just use the thread loop to read adc and process data. But how do I set the timing for this. Say if I want to sample every 100us? 1ms? And how do I ensure that this timing is not affected by other threads that are running?



  • Hi AJ-
    When you say "fastest" do you mean with the least amount of processing time? Or you mean the fastest rate of ADC reads? Or something else? Can you say something more about how much DSP processing is involved?

    I think your option 2 might be best for overall fastest processing, loop time, response, etc. Keeping the callback very quick is important. Using Semaphores and threads takes very little processing time in comparison to even simple DSP calculations...
  • In reply to WarrenM:

    Hi Warren,

    What I mean by fastest is more on processing time - (read ADC, implement the difference equation and then output to the DAC). I did something like this for a digitally controlled magnetic levitation project using the TM4C chip. I wanted to migrate to the S7. I also did the same thing for motor control - (I read voltage proportional to motor speed, implement pid equation then output voltage to a DAC).

    Just wondering, the time it takes to transfer from callback to thread, is that fixed?

    Also, let's say using option 2, I ended to up taking too long in the thread, I assume that the callback executes at higher priority right?

  • In reply to AJ:

    Hi AJ-
    I believe the transfer from callback to thread is fixed. Callbacks are automatically higher priority than a thread, since they are a CPU interrupt.

    My experience has been that the best 'through-put' comes by overlapping operations. For example, process data while the ADC is doing the next conversion. Try not to ever be 'waiting' on something. I believe that should get you the highest thru-put.

    It make take some experimenting to find the optimal balance point- but the ThreadX tools for watching threads and seeing where time is spent will help alot....
  • In reply to WarrenM:

    Thanks Warren. Just to clarify, this can be done using option 2 above with just a single thread?
  • In reply to AJ:

    Hi AJ-
    You can do it that way, but it might not be optimal. I would experiment with multiple threads (maybe one overall control, one for processing, and one each for ADC and DAC.) But then, I like to use threaded architectures ;-)

    Hard to tell without more details on how much processing time you need and what the data input and output rates need to be. If this is a mechanical system or sensor however, and not a higher speed audio or video application, I would expect it to work fine- plenty of time between measurements to do some useful DSP functions...
  • Hi AJ,

    Most of external ADCs have auto conversion command and convert completed interrupt.
    In that case the timer is not needed.
    Assign the converted interrupt as one of free IRQs and trigger communication with DMA or DTC in the IRQ callback function.
    In transfer completed callback you should prepare next communication if it is necessary.