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?

3
  • Could you edit your post and clarify what you want to do? The pseudo code says you read an ADC and discard the data. You then write a constant to a DAC.
    – joan
    Commented Nov 24, 2020 at 10:52
  • The data should go into a file, and the signal for the DAC could be an arbitrary waveform function. I want to be able to output a well defined waveform on the DAC output, while measuring a waveform with the ADC.
    – Martin
    Commented Nov 24, 2020 at 12:44
  • Given that the core O/S is multi-tasking and Python has its own challenges with the GIL ( see realpython.com/python-gil ) is this a valid proposal for a time critical process? Have you seen these older notes raspberrypi.stackexchange.com/questions/61956/… about using one core for you code?
    – user115418
    Commented Nov 24, 2020 at 15:48

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.