1

I have an application where I want to write to an external DAC and read from an external ADC using the pi's SPI interface continuously.

The data to the DAC could come from some arbitrary function, and the result from the ADC I want to put into a file for later post-processing.

schematic

simulate this circuit – Schematic created using CircuitLab

I have done this using the spidev module in python, and this works fine. However, the application requires the timing for both transactions to be consistent, i.e. happening exactly every 1 ms, so I can't just do a:

i = 0
while True:
    myADCfile.write(spi.readbytes(16))
    spi.writebytes(arb_function(i))
    time.sleep(0.001)
    i += 1

Because then the timing will depend on the CPU's priorities..

According to the broadcom documentation [1], the pi has a DMA that can control the SPI. However, I can't figure out how to set this up, and I haven't found any examples.
[1] https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf

Can anyone point me to an example (preferably in python), or some resources on this?

Martin
  • 11
  • 3

1 Answers1

1

As long as the data being written to the DAC is not dependent on the data being read from the ADC this may be possible.

Certainly reading the ADC at precisely 1 kHz should be feasible.

I have used DMA to read the MCP3008 (10 bit) and MCP3202 (12 bit) at 25 kHz.

See An experiment in bit-banging SPI.

Outputting to the DAC may be possible depending on whether the data to be output can be prepared some time before transmission. By "some time" I mean of the order of seconds rather than minutes or milliseconds. We would need to understand the data to be sent to comment further.

joan
  • 71,852
  • 5
  • 76
  • 108