cap touch and DMA weird interaction

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mzero
 
Posts: 19
Joined: Fri Sep 13, 2019 12:44 am

cap touch and DMA weird interaction

Post by mzero »

I'm using a CPE to create a small musical instrument: Capacitive touch on some copper plates will play various sounds out the DAC.

My code for generating the sounds uses double buffering and DMA (via Adafruit_ZeroDMA) to output the samples at 48kHz. The sketch also blinks some of the neopixels and reports some stats over Serial. So far, this all works just fine.

Now, if I enable my code to test for capacitive touches (calling CircuitPlayground.readCap(A1) once every 20ms) -- during the measurement, the DMA to the DAC slows down by little more than ½ during the measurement, but otherwise runs correctly.
SDS00016.png
SDS00016.png (31.66 KiB) Viewed 326 times
The section in this image where the triangle wave is spread out is when a touch measurement is being made. You can see the time from measurement to the next is 20ms (between the cursors) - my touch testing period. During the measurement, it isn't that the DMA is stopped, or paused, but just slowed - as you can see in the zoomed in section on the bottom. The yellow line is the waveform from TC4 out on a pin - it is half the DMA sample rate - and you can see that during the measurement, the timer doesn't alter at all.

Is there a known interaction between the touch controller and DMA? the SAM D21 docs are woefully lacking when it comes to the touch controller. I could find nothing obvious in the code for CPE's touch implementation that seemed to speak of DMA...

Anyone seen anything like this?

User avatar
mzero
 
Posts: 19
Joined: Fri Sep 13, 2019 12:44 am

Re: cap touch and DMA weird interaction

Post by mzero »

Update:

After much probing and test code, I've narrowed this down significantly:
DMA to the DAC is stalled
- for long enough to miss samples even at 48kHz transfer rate
when
- the program does a read of a Read-Synchronized register
- of a peripheral on the same Bridge (C)
and both
- the peripheral is clocked by something other than GCLK_GEN0
- no read request synchronization steps are taken
I verified this without any of the PTC / FreeTouch code by using TC3, which is on the same bridge - setting it to be clocked by same clock PTC would use (the 8MHz clock) - and doing a read of COUNT16.COUNT register (which is read-sync).

A single such read in a program is enough to induce about 185µs of stall in the DMA - which is about 8 samples lost in the DMA transfer. Doing 20 such reads in a row induces 3.7ms of stall, though a few DMA transfers get through in that span.

Since I'm DMA'ing audio, these stalled samples are enough to make things very obviously distorted.

Adafruit_FreeTouch

The FreeTrouch code does what seem to be all the required work for write synchronized registers. However, there are two registers that are read during measurement, one in a spin loop, that (a) induce this problem, and (b) have no read synchronization code. Since the register details for the PTC aren't published, I can only guess that these registers are read synchronized. Alas, in the excellent reverse engineering work that was done for FreeTouch (and LibrePTC) - no Read Request register was unearthed. It's probably there among the unknown registers in the device's address block....

To avoid the slow down, one of two things has to change in the FreeTouch code. Either:
  • set up the PTC to be clocked by GCLK_GEN0 -- but this probably won't work because the little documentation we have on the PTC says "The frequency range of GCLK_PTC is 400kHz to 4MHz"
  • OR, see if I can unearth the location and operation of the read request register for the PTC
More sleuthing to come....

User avatar
mzero
 
Posts: 19
Joined: Fri Sep 13, 2019 12:44 am

Re: cap touch and DMA weird interaction

Post by mzero »

Full analysis and sketch to reproduce the issue posted to this issue: https://github.com/adafruit/Adafruit_Fr ... /issues/17

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”