Skip to content

stm32: Fix 12bit DAC issue on STM32H5. #17743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2025

Conversation

yn386
Copy link
Contributor

@yn386 yn386 commented Jul 22, 2025

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.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, this looks good to me (but I did not test it).

For STM32H5, to use 12-bit DAC, the DMA parameter should set:
- Actual DMA source datawidth to CTR1.
- The length is the amount of data to be transferred from source to
  destination in bytes.

Also, this commit modifies the (dummy) definition of DMA_CIRCULAR for
STM32H5 to prevent conflict with data width specification.

Signed-off-by: Yuuki NAGAO <wf.yn386@gmail.com>
@dpgeorge dpgeorge force-pushed the fix-12bit-dac-issue branch from 68d1661 to 1b35116 Compare August 18, 2025 03:12
@dpgeorge dpgeorge merged commit 1b35116 into micropython:master Aug 18, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants