I am trying to output a signal with dac124s085 from python code on a raspberry pi 3b, running Raspbian Stretch (kernel 4.14.30-v7+ #1102).
I am sure the spi master driver is enabled - dtparam=spi=on isn't commented out in /boot/config.txt
MOSI pin 19 on rpi connected to pin 8 DIN on dac MISO pin 21 on rpi not used by dac SCLK pin 23 on rpi connected to pin 10 SCLK on dac CE0 pin 24 on rpi not used by dac GND pin 25 on rpi connected to pin 6 GND on dac CE1 pin 26 on rpi connected to pin 9 SYNC on dac
pin 7 VREFIN on dac is connected to 5V pin 2 on rpi (decoupled to ground) pin 1 VA on dac is also connected to 5V decoupled to ground pin 2-5 VOUTA-D on dac is connected to two stereojacks via adc on each line
I am using the py-spidev library (https://pypi.org/project/spidev/). Loopback test on spidev0.0 and spidev0.1:
./spidev_test -D /dev/spidev0.0 spi mode: 0 bits per word: 8 max speed: 500000 Hz (500 KHz) FF FF FF FF FF FF 40 00 00 00 00 95 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF DE AD BE EF BA AD F0 0D
The code I am using:
import spidev import math class Dac: def __init__(self, device): self.spi = spidev.SpiDev() self.spi.open(0, device) self.spi.max_speed_hz = 1000000 def write(self, ch, data): wdata =  wdata.append((ch << 6) | ((int(data) & 0xF00) >> 8)) wdata.append(int(data) & 0xFF) self.spi.xfer(wdata) def writeAll(self, ch0, ch1, ch2, ch3): self.write(0, ch0) self.write(1, ch1) self.write(2, ch2) self.write(3, ch3) if __name__ == '__main__': dac = Dac(1) nsamples = 32 a =  * nsamples for n in range(nsamples): a[n] = int(2048 + 512*math.sin(2*math.pi*n/256)) while (True): for sample in a: dac.write(0, sample)
How can I troubleshoot this problem? The script should output a sine wave but does not. It actually works if I grab the cables between the circuit and the rpi. Maybe the conductivity of my body does something with the clock speed or pulls any pin high/low?
Is the script correct? I must confess I do not really understand it.
Logic analyzer is showing this when I run
sudo python3 dac124s085.py on rpi:
Is this right with reference to timing diagram?
Edit: When I try loop.py the dac actually output a constant signal (without me holding the cables or anything)
from adc124s021 import Adc from dac124s085 import Dac adc = Adc(0) dac = Dac(1) while True: ch0, ch1, ch2, ch3 = adc.readAll() print(ch0, ch1, ch2, ch3) dac.writeAll(ch0, ch1, ch2, ch3)
But dac out hooked to a speaker I mostly get a noise sound - except from when I am nearby the circuit with dac, rpi and wires (not touching) I will upload a csv file with the datapoints.
The problem is fixed by adding
self.spi.mode = 3 under
def __init__ in dac124s085.py
I guess I should have a serious read over www.allaboutcircuits.com/technical-articles/spi-serial-peripheral-interface/