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

How to use DMA in NRF52 while writing on SD card, while BLE is also operating?

Hi,

In my application of interfacing SD card with nrf52, I am using SAADC to sample the input signal at 1000 samples per seconds. These samples I am combining in 84 packets each consisting 12 samples and 20 bytes. In one second I am sending 84 packets over BLE to remote tablet device.

So, I want to write these packets in SD card. Writing one packet at a time is not a good practice. So I want to write 40 packets at a time in SD card. For this I need DMA. As I am never used DMA before, I want to understand what is the correct steps to use DMA for my application.

Does any one has any idea about this?

SDC+BLE.c

  • I'll start from #3: 3) SD card library uses SPI in DMA mode if SPIn_USE_EASY_DMA is enabled in sdk_config.h (where n is the SPI instance number used by app_sdcard, defined by APP_SDCARD_SPI_INSTANCE; by default (in fatfs example): instance 0, dma enabled).

    1. Call FATFS API functions from the main context (not an interrupt handler) and BLE functions from interrupts (which will preempt the fatfs call). BLE communication is time sensitive, so it's a good idea to keep the SPI priority lower than the priority from which wireless transmission is triggered (eg. SAADC, timer). By default SPI priority is set to lowest (SPI_DEFAULT_CONFIG_IRQ_PRIORITY (sdk_config.h) equal to APP_IRQ_PRIORITY_LOWEST (app_util_platform.h) = 7 on nRF52/Cortex M4).

    I'd also recomment using app_scheduler. If you will be able to implement ADC sampling / BLE transmission in interrupt driven way then all you need to do is schedule the SD write in one of the interrupts (then it will be executed from main cotext) and make sure that there is enough time to write the data on SD (in the worst case scenario) between filling the buffers.

    1. Take a look at nrf_drv_saadc_buffer_convert(). It takes a pointer to the buffer and buffer size as an argument. My concept is something like this: uint16_t buf[2][12N]; call _convert() with: &buf[0][0], size 12; then [0][12], then [0][24], [0][36], and so on until it's filled. When it's full (last conversion to buf[0][] finished) call _convert with( &buf[1][0]. trigger SD write of buffer 0, then convert to [1][12]. [1][24]... nrf_drv_saadc_buffer_convert() will result in interrupt and calling the handler, so you can trigger the next sampling / wireless data transmission there.
  • Hi Pawel,

    I have implemented the SAADC DMA buffer using the function nrf_drv_saadc_buffer_convert(). I am attaching my complete code with this thread, at top. I have kept the SAADC buffer size as 24 and SAADC is running in scan mode. So every 12ms Event done interrupt will occur. Now I am performing SD write operation after 120 ms. But Still I am starting streaming data at 1000 sps entire code stops.

    I am calling the SD write operation in main function by checking the SD write flag which is activated after 120ms .

    Does App scheduler will really solve this issue as I have not worked on scheduler till now?

  • Scheduler would be the recommended way of doing this if you want to extend the application in future. In this simple case, a single flag is good enough. (btw. it should be defined as volatile bool, not bool as it's being modiffed from different context). The general idea is OK - SD card operations from main context only. You might also want to increase the priority of SAADC interrupt in from _LOWEST (default) to APP_IRQ_PRIORITY_LOW (to be above SD/SPI).

    Do you have an environment set up for debugging to see where it stops? To find the caouse, try to run the blocks separately - sampling/sending, then sampling/writing or periodic writing (or sending) some random data.

    In the attached code, adc is initialized (and started) at the beginning, before the initialization of BLE / SD card - that could also be a source of problems.

  • Hi Pawel,

    Thanks for your suggestion in mentioned attached code above.I will change it volatile bool . Actually I am initializing SAADC before SD card and BLE but not starting it to sample the input signal.After every thing has been initialized properly, then using Nordic UART service I am sending some values(1 or 2) to start sampling the SAADC. (This happens after BLE starts advertising ). So AS sson as received any value from master control panel ADC samples the signal and after 12 samples it generates EVENT DONE event in Callback.

    I am using IAR for developing my code and using MCP to get the values over APP. AS soon as I sent any value 1 or 2 , entire code gets stopped and over MCP I received GATT error :133(0x85). In IAR it is not showing the cause of sopping the code.

    In SAADC initialization I have set priority as APP_IRQ_PRIORITY_LOW from default value of lowest.

Related