opposite polarities on the 2 DACs

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
andreo
Posts: 13
Joined: Fri Mar 02, 2018 6:46 pm
Location: Belgium

opposite polarities on the 2 DACs

Post by andreo » Fri Mar 02, 2018 7:05 pm

On the pyboard the 2 DACs give opposite polarities when used together.
(MicroPython v1.9.3 on 2017-11-01; PYBv1.1 with STM32F405RG)
i.e: issuing

dac1.write_timed(buf1, timer6, mode=DAC.CIRCULAR)
dac2.write_timed(buf2, timer7, mode=DAC.CIRCULAR)

with the same data in both buffers, the 2 outputs have opposite polarities.
Is there a way to choose polarity ?

(this could itch audiophiles as it changes push/pull on start of transients)

User avatar
pythoncoder
Posts: 4664
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: opposite polarities on the 2 DACs

Post by pythoncoder » Sat Mar 03, 2018 6:57 am

I think you're experiencing a phase difference here. The problem is that there is currently no way to start the two timed writes simultaneously. Each line of code takes some time to execute so there is always a phase difference between the two DAC outputs (which will appear as an inversion with sufficiently fast clocks).

The problem is even more acute with the ADC's as timed reads are blocking. This makes the task of measuring phase differences quite challenging.
Peter Hinch

andreo
Posts: 13
Joined: Fri Mar 02, 2018 6:46 pm
Location: Belgium

Re: opposite polarities on the 2 DACs

Post by andreo » Sat Mar 03, 2018 8:49 am

Thanks a lot for your answer.

With a rather slow signal, I thought to control the phase. Please find here the details:

The buffer is a 40 bytes bytearray
with a timer Timer(6, freq=2400, prescaler=0, period=34999, mode=UP, div=1) sf=84000000
it gives a pretty slow 60 Hz signal.
I can even introduce a phase difference with a 2 ms delay between the 'write_timed'

dac1.write_timed(buf1, timer6, mode=DAC.CIRCULAR)
pyb.udelay(2000)
dac2.write_timed(buf2, timer6, mode=DAC.CIRCULAR)

to give the result:

https://www.proximuscloud.be/web/app/sh ... r7ZqUiMk0i

andreo
Posts: 13
Joined: Fri Mar 02, 2018 6:46 pm
Location: Belgium

Re: opposite polarities on the 2 DACs

Post by andreo » Sun Mar 04, 2018 6:42 am

Hi pythoncoder,

One more test:
whatever the order I start the write_timed's in, DAC 1 before DAC 2 or the reverse,
it's always the DAC 2 pin X6 signal that is inverted.

At least there is no random behavior, and we can consider that as a "not yet documented feature", not a bug.

User avatar
pythoncoder
Posts: 4664
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

I can't replicate this

Post by pythoncoder » Sun Mar 04, 2018 7:23 am

I think there must be something wrong with your code. Please try the following:

Code: Select all

from math import sin, pi
from pyb import DAC, Timer
from array import array
import utime

_BUFLEN = const(128)

tx = array('H', (2048 + int(2047 * sin(2 * pi * i / _BUFLEN)) for i in range(_BUFLEN)))

tim_dac = Timer(8)
tim_dac.init(freq=60*_BUFLEN)

dac1 = DAC(1, bits=12)
dac2 = DAC(2, bits=12)

dac1.write_timed(tx, tim_dac, mode=DAC.CIRCULAR)  # X5
utime.sleep_ms(2)  # should give 45 degree phase shift
dac2.write_timed(tx, tim_dac, mode=DAC.CIRCULAR)  # X6
I am getting two 60Hz sinewaves with 45° (2ms) phase shift, as expected. If the DACs had opposing polarity the phase shift would be 225°.
Peter Hinch

andreo
Posts: 13
Joined: Fri Mar 02, 2018 6:46 pm
Location: Belgium

Re: opposite polarities on the 2 DACs

Post by andreo » Sun Mar 04, 2018 6:02 pm

Hi pythoncoder,
Thanks a lot for your answer.

Surprising behavior from my pyboard...

When I used your code for the first time, it worked as expected.

A few minutes later, I got back the same inverted signal, and I couldn't get your result anymore.

I did reset fully the filesystem, working only from flash, power without USB, ...
and I always get the following result with inverted DAC 2:
https://www.proximuscloud.be/web/app/sh ... rfZSjh1D6Q

But I can still work with the pyboard, that I purchased at the MicropPython boot at FOSDEM 2018 Brussels.

(What I'm really doing is to create longer signals prepared as binary files, simulating glitches that I catch/record by programming the Bitscope. Using DMA to generate the signal is very good for that.)

Best evening,

Andre

User avatar
pythoncoder
Posts: 4664
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: opposite polarities on the 2 DACs

Post by pythoncoder » Mon Mar 05, 2018 5:37 am

Hi Andre,
I didn't entirely follow your post.
But I can still work with the pyboard, that I purchased at the MicropPython boot at FOSDEM 2018 Brussels.
Are you saying that you have two Pyboards and the fault occurs on one but not the other?

At the moment I'm baffled as to how this can occur given that my test script uses the same timer and data array for both DAC's. I've also looked at dac.c and can't see an obvious potential cause. I'll try again to replicate this.

Pete
Peter Hinch

andreo
Posts: 13
Joined: Fri Mar 02, 2018 6:46 pm
Location: Belgium

Re: opposite polarities on the 2 DACs

Post by andreo » Mon Mar 05, 2018 10:33 am

Hi Peter,

Sorry about that misleading sentence: I have only one pyboard.
But as it behaves always the same way, I can still work with it.

I spent all the morning changing parameters, 8 to 12 bits, all available timers for DAC,...
and reading dac.c.
Same results.
But using DAC.write, instead of write_timed, both outputs have same polarities !

I could just buy a second pyboard, but I see my favorite shop (https://shop.mchobby.be/)
is now selling also the Adafruit HUZZAH32-ESP32 that seems to have the same DAC + DMA features and MicroPython.
(https://learn.adafruit.com/adafruit-huz ... 32-feather)
Wich one to buy ?

(This very small shop mchobby.be is in Waterloo (Belgium), a few hundred meters north of the the Lion, exactly where the Duke of Wellington was commanding the allied army in 1815.)

Have a good day,

Andre

User avatar
pythoncoder
Posts: 4664
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: opposite polarities on the 2 DACs

Post by pythoncoder » Mon Mar 05, 2018 11:30 am

Hi Andre,
I've tried quite hard to replicate this and failed. I ran my script 20 times (with a soft reset after each). I then repeated on a second Pyboard. It worked perfectly every time with the results viewed on a scope. Are you sure there isn't some kind of measurement issue here? It often pays to be suspicious. You could set the data array in my script to a fixed value and check the scope readings with a meter.

As for boards the Pyboard beats ESPx boards unless you need the WiFi capability. I've not studied their DACs in much detail but ESPx boards have poor ADC's. They also have a rather slow response to interrupts owing to the WiFi stack running in the background. For hardware interfacing the STM processors are better and the pyb module is more fully featured than machine. For example machine.RTC is not implemented on ESP32. The official ESP32 port is quite new, whereas the Pyboard port has ~4 years of development under its belt. So it's very robust.

Pete
Peter Hinch

andreo
Posts: 13
Joined: Fri Mar 02, 2018 6:46 pm
Location: Belgium

Re: opposite polarities on the 2 DACs

Post by andreo » Mon Mar 05, 2018 2:47 pm

Hi Peter,

Here are the results:
https://www.proximuscloud.be/web/app/sh ... 8s7ru3yYNX

I used a signal with a strong DC component.
My good old meter is telling me the same as the scope and the Bitscope meter.

Same with only one channel at a time.

dma.c is not saying more than dac.c.

I'll buy a second pyboard to check.
At FOSDEM, the lady at the MicroPython boot was showing very nice pyboards with a lithium battery
and a small graphics display, all the size of the board. Really cute, but not for sales there.

I'll just sit and cry ... and write some python.

Best regards,

Andre

Post Reply