3.3 Data Transfer from Ping-Pong Buffers to Internal Memory

Data stored in the ping-pong buffers by the ADC can be transferred to the internal Program Flash Memory (PFM) in a two-step process. First, the contents of the ping-pong buffers are transferred to buffer RAM using DMA. Next, the NVM Controller is used to write the buffer RAM contents to a designated page in the PFM. The DMA setup configurations are listed in Table 3-3.
Table 3-3. DMA Setup Configuration (DMA5)
DMA5 Setup
SSASource AddressBeginning of ping buffer
SSZSource Size512 (2 banks for ping and pong buffers)
SMRSource Memory RegionGPR
SMODESource Address ModeSPTR is incremented
DSADestination AddressBeginning of buffer RAM
DSZDestination Size256 (1 bank for buffer RAM)
DMODEDestination Address ModeDPTR is incremented
SIRQENStart Trigger EnableNo (stops when DCNT reloads)
SIRQStart TriggerN/A
SSTPSource Counter Reload StopN/A
DSTPDestination Counter Reload StopN/A
AIRQENAbort Trigger EnableNo (stops when DCNT reloads)
AIRQAbort TriggerN/A
How It Works:
  1. Transfer the contents of the ping buffer to buffer RAM by manually starting the DMA transfer. The DMA stops as soon as the buffer RAM is full (DCNT reloads). Then, proceed to the next step to write to PFM. In the next iteration (when the pong buffer is ready to be transferred), simply start the DMA again manually. SPTR continues into the pong buffer region, whereas DPTR rolls back into buffer RAM again. When the buffer RAM is full and DMA stops again (DCNT reloads), the SPTR rolls back into the ping buffer region(1).
  2. The buffer RAM contents can be written to PFM word-by-word at every sampling timer interrupt so that the CPU stalls are minimized(2, 3). The transfer is performed using the write operation in the NVM Controller in PIC18F57Q43.
  3. Update write address location and number of pages written in the metadata.
Important:
  1. In this application, the ping and the pong buffers are adjacent to each other in data memory. If ping and pong buffers are separated in the data memory, then SSA needs to be reconfigured after every 256 iterations.
  2. To write to PFM, word write is used instead of page write because page write stalls the CPU for typically 10 ms. This seriously affects the data sampling rate of ADC. However, a word write typically takes about 50 µs, which can work with a 10 kHz ADC sampling rate.
  3. PFM word writes stall the CPU for typically 50 μs, which is about 50% time between two ADC samples (100 μs sampling period). Therefore, an interrupt from the sampling timer is used to ensure that PFM writes occur after the ADC has finished sampling and storing data.