This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How do do parallel, concurrent operations?

I am reading data over the duration of 3 seconds on SPI, but while I'm doing that, I need to send that data over RF using microESB library.

I need to make sure the read is not blocking RF send and vice versa. The reading is the most imporant. So I imagine some kind of a big buffer that holds all the data I read, where a task independently reads what's filled in the buffer at it's own pace. As you know transfers can fail, and take substantially longer than a quick SPI read. Right now I read from SPI with 10 ms nrf_delay_ms() in a loop over 3 seconds. I also have the possibility to have the SPI interrupting when it's internal ring buffer is full, doing a large read in short bursts.

Thanks!

Parents
  • As there is only one core, you can not do more than one thing at the same time. However there are other ways to do multiple things in order to have seemingly concurrent threads.

    • You can use interrupt routines (or event handlers) and use different priorities so that a high priority interrupt will be run even tough a low priority interrupt or main context code is being run.
    • You can use flags to signal data between functions running in the interrupt context and main context (or use the scheduler or another synchronization mechanism).
    • For some applications it may be sensible to use a RTOS (e.g. FreeRTOS) which provide you with a simple way to create tasks ("threads") at different priorities and communicate between tasks using message queues etc.

    For the scenario you have described you could do several other things. One possible way of doing this is something like this:

    1. let a SPI event handler write data to a ring buffer.
    2. set a flag indicating that the buffer has been updated
    3. from your main loop you can check that flag, and run another function (call it "thread" if you like) responsible for sending data
    4. from the send-data function, read data from the ring-buffer and write it to the micro_esb library.
  • With a SPI event handler i refer to a event handler that is run when a SPI transfer is completed. E.g. using the SPI master deriver this is (optionally) supplied to the nrf_drv_spi_init() and the handler is called when a transfer initiated with nrf_drv_spi_transfer() is done.

    If you want something truly parallel, the closes you will get is using an RTOS, but it may be overkill in this case.

Reply Children
No Data
Related