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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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

Signed-off-by: Yuuki NAGAO <wf.yn386@gmail.com>
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy