Maximising the speed of many consecutive analogWrite commands

So I am trying to use an Arduino Due to mode-lock a laser. Reading in a signal, doing some processing and outputting the result to the laser all work fine, however, there was an issue that the analog output signal (from DAC0) was too noisy. Moreover, analogWrite(PIN, X) would result in slightly different voltages being produced for the same value of X. To counter this, I was hoping to, instead of simply outputting the signal and waiting for the next loop to run and produce another one, write out the voltage to the pin many times in quick succession. This would mean the 'natural wandering' of the output voltage would simply be high frequency noise. A low-pass on the output would then eliminate it and, hopefully, most of the other noise on the signal too.

This is where I hit the problem; consecutive calls of analogWrite(PIN, X) take too long to run (I measured around 3.6 microseconds per call). Using commands from the DACC driver directly, e.g. dacc_write_conversion_data(DACC_INTERFACE, X);, sped this up to about 0.4microseconds per write, though I struggled to find the right form for X.

Finally, I came across this code online for generating a sawtooth profile as fast as possible. Hacking it for my purposes (see attachment the code) I saw another order of magnitude speed improvement so my code was not writing out the same value at 25MHz.

This worked perfectly for a while until, suddenly, the DAC0 pin stopped producing any output at all, whatever code I ran. I presume I fried the pin by writing to it so fast, but I don't understand why it only happend after about a day. So my questions are:

  • Is it possible that I fried the pin writing to it consecutively so fast? If so, is there a limit for the speed it can take?
  • Does anyone know how to modify the below code so that it outputs onto DAC1? I'm really very new to C and Arduino's so I'm struggling to get my head around it.
  • If anyone knows or can think of an equally fast, potentially better way of speeding up the analog output, I would be really keen to hear it!

Any thoughts on the above or general comments would be great to hear.

Thanks.

ForPiotr.ino (8.7 KB)

The DAC0 pin is more sensitive to over-voltage than other pins it seems, people seem to
blow it readily. It doubles up as a hardware wakeup pin so it may talk more directly to the 1.8V core than other pins (a theory only).

I would recommend adding protection on the DAC output pins, perhaps 1k on the pin
to some protection (schottky) diodes.

Certainly with a sensitive 3.3V chip like the Due its best not to take signals any distance
off-board without buffering them.

And yes the DAC is pants on this chip, sparkle codes and weird behaviour, you would
have done better to simply go and get an external DAC like the MCP4922