← index #9563PR #17770
Related · high · value 7.109
QUERY · ISSUE

dac.write_timed() does not work with NUCLEO_G474RE

openby anchung-chenopened 2022-10-10updated 2023-07-01
bugport-stm32

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
    }
CANDIDATE · PULL REQUEST

stm32: Fix DAC issue for MCUs those have D-Cache.

mergedby yn386opened 2025-07-27updated 2025-08-18
port-stm32

Summary

The following code does not work with NUCLEO-F767ZI.
Incorrect wave may be output on PA4. STM32H7 has same issue.

import math
from pyb import DAC

# create a buffer containing a sine-wave
buf = bytearray(100)
for i in range(len(buf)):
    buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))

# output the sine-wave at 600Hz
dac = DAC(1, bits=8)
dac.write_timed(buf, 600 * len(buf), mode=DAC.CIRCULAR)

For MCUs which has D-Cache (such as STM32F7, STM32H7), clean D-cache before starting DMA.
For more details, please refer follwing document:
https://www.st.com/resource/en/application_note/DM00272913.pdf

This PR cleans D-cache before starting DMA.

Testing

Tested code above with:

  • NUCLEO-F767ZI (STM32F767ZI)
  • WeAct H743VI (STM32H743VI, PR: #17766)

-> Both boards output wave as expected.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied