Skip to content

stm32: Add DAC feature for STM32G0. #17763

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 25, 2025

Summary

This PR adds DAC feature for STM32G0.
DAC.write() and DAC.write_timed() is now available.

Testing

The following code works with NUCLEO-G01BRE.
A sine wave is output via PA4 when ch=1, PA5 when ch=2.

import math
from pyb import DAC

ch = 1

# 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(ch, bits=8)
dac.write_timed(buf, 600 * len(buf), mode=DAC.CIRCULAR)
import math
from array import array
from pyb import DAC

ch = 2

# 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(ch, bits=12)
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)

Trade-offs and Alternatives

This PR increases code size by 1800 Bytes.

Before:

   text	   data	    bss	    dec	    hex	filename
 297528	    108	   4004	 301640	  49a48	build-NUCLEO_G0B1RE/firmware.elf

After:

   text	   data	    bss	    dec	    hex	filename
 299312	    108	   4020	 303440	  4a150	build-NUCLEO_G0B1RE/firmware.elf

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.

A nice addition, thank you!

DAC.write() and DAC.write_timed() are now available on STM32G0.

Tested on NUCLEO_G0B1RE.

Signed-off-by: Yuuki NAGAO <wf.yn386@gmail.com>
@dpgeorge dpgeorge force-pushed the add-dac-feature-for-g0 branch from dd68215 to 152a078 Compare August 18, 2025 03:18
@dpgeorge dpgeorge merged commit 152a078 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