dac.write_timed() does not work with NUCLEO_G474RE
The example code from micropython document does not work with NUCLEO_G474RE.
import math
from array import array
from pyb import DAC
# create a buffer containing a sine-wave, using half-word samples
buf = array('H', 2048 + int(2047 * math.sin(2 * math.pi * i / 128)) for i in range(128))
# output the sine-wave at 400Hz
dac = DAC(1, bits=12)
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
The DAC1 output of PA4 pin (A2 pin on arduino connector) shows no waveform.
After a little research, I found the root cause is that DMA_PDATAALIGN_WORD should be used for the "DMA + DAC" operation of STM32G4 cpu.
Here is my modification of dac.c to make dac.write_timed() work with NUCLEO_G474RE.
STATIC void dac_start_dma(uint32_t dac_channel, const dma_descr_t *dma_descr, uint32_t dma_mode, uint32_t bit_size, uint32_t dac_align, size_t len, void *buf) {
uint32_t dma_align;
if (bit_size == 8) {
dma_align = DMA_MDATAALIGN_BYTE | DMA_PDATAALIGN_BYTE;
} else {
#if defined(STM32G4)
dma_align = DMA_MDATAALIGN_HALFWORD | DMA_PDATAALIGN_WORD;
#else
dma_align = DMA_MDATAALIGN_HALFWORD | DMA_PDATAALIGN_HALFWORD;
#endif
}
stm32: Fix 12bit DAC issue on STM32H5.
Summary
The following code does not work with NUCLEO-H563ZI.
import math
from array import array
from pyb import DAC
# create a buffer containing a sine-wave, using half-word samples
buf = array('H', 2048 + int(2047 * math.sin(2 * math.pi * i / 128)) for i in range(128))
# output the sine-wave at 400Hz
dac = DAC(2, bits=12)
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
For STM32H5, DMA parameter should set:
- Actual DMA source datawidth to CTR1.
- The length is the amount of data to be transferred from source to destiniation in bytes. to use 12bit DAC.
Also, this PR modifies the definition of DMA_CIRCULAR for STM32H5 to prevent conflict with data width specification.
Testing
Tested code above with NUCLEO-H563ZI.
Also, DAC with 8bit still works after applying this PR.
Trade-offs and Alternatives
The value of DAC_CIRCULAR is changed:
from
>>> hex(DAC.CIRTUCLAR)
'0x1'
to
>>> hex(DAC.CIRCULAR)
'0x20000000'
Indeed, it won't be a problem because users do not need to be aware of value of DAC.CIRCULAR in most cases.