Arduino_core_stm32: How to use hardware DAC with Arduino core

Created on 15 May 2020  路  31Comments  路  Source: stm32duino/Arduino_Core_STM32

is there any way to use STM32 hardware DAC with Arduino core ?

Answered Documentation Question

All 31 comments

Use analogWrite() on a pin with DAC capabilities.

Please use the forum https//www.stm32duino.com for this kind of question.

There should be a good documentation available.

Well, in fact this is the same behavior than the official API reference for DUE, when a DAC is available it is used:
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

A note could be added there:
https://github.com/stm32duino/wiki/wiki/API#analog
Do not hesitate to contribute 馃槈

A note could be added there:
https://github.com/stm32duino/wiki/wiki/API#analog
Do not hesitate to contribute 馃槈

why not, but let's see first why it doesn't work ;)

I have no voltage on the PA4 with the DAC value less than 127 and 3.3 V with the value more than that DAC value ? what is wrong with my code ?

#include <TFT_eSPI.h> 
#include <SPI.h>
#include <Arduino.h>

TFT_eSPI tft = TFT_eSPI();       

uint16_t pw;

void setup(void) 
{
  pinMode(PA4, OUTPUT);
  tft.init();
  tft.setRotation(0);
  tft.fillScreen(TFT_BLACK);
}

void loop()
{
  pw++;
  tft.setCursor(0,10);
  tft.printf("DAC value is : %u",pw);
  analogWrite(PA4,pw);  
  delay(50);
}
[env:genericSTM32F103RE]
platform = ststm32
board = genericSTM32F103RC
framework = arduino

board_build.f_cpu = 72000000L
board_build.core = ststm32
board_build.variant = Generic_F103Rx

upload_protocol = stlink
debug_tool = stlink

build_flags =
  -Os
  -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
  -DUSER_SETUP_LOADED=1
  -DST7789_DRIVER=1
  -DTFT_WIDTH=240
  -DTFT_HEIGHT=240
  -DTFT_DC=PC13
  -DTFT_RST=PC12
  -DLOAD_GLCD=1
  -DLOAD_FONT2=1
  -DLOAD_FONT4=1
  -DLOAD_FONT6=1
  -DLOAD_FONT7=1
  -DLOAD_FONT8=1    
  -DSPI_FREQUENCY=35000000
  -DSTM32=1

I don't know why but as you use PIO, I guess you use roger's core not this one... try with the Arduino IDE and the core version 1.9.0.
Moreover 1.9.0 has been released yesterday with this board support so my guess should be correct 馃槈

I don't know why but as you use PIO, I guess you use roger's core not this one... try with the Arduino IDE and the core version 1.9.0.
Moreover 1.9.0 has been released yesterday with this board support so my guess should be correct 馃槈

well, this line means use this core

board_build.core = ststm32

I don't think the problem is for PIO but I will test the code on Arduino too.

the same problem is with Arduino IDE and the core version 1.9.0.

there must be a bug in the core.

#include <Arduino.h>

uint16_t pw;

void setup(void) 
{
  pinMode(PA4, OUTPUT);
}

void loop()
{
  pw++;
  analogWrite(PA4,pw);  
  delay(50);
}

Could you share the output log with all verbose enabled in the Arduino IDE preferences?

@fpistm
ive checked dac with my nucleo f103re (nucleo-64 103rb with mcu replaced)

uint32_t counter = 0;

void setup() {
  pinMode(PA4, OUTPUT);
}

void loop() {
    analogWrite(PA4, round( 127+128*sin(0.01 * (counter++))));
}

result:
https://imgur.com/uOE47cR

but if i will compile it as f103rb it will be square wave (as expected for medium density)
probably he is somehow trying to run dac while compiling for medium densitty devices

Thanks @stas2z . Yes I think it is a mistake in the core or selected device, that's why I ask for full log.

And as I said PIO used the maple core for generic F103, there is am issue open for this:
https://github.com/platformio/platform-ststm32/issues/339

@fpistm

not exactly
he is right, his defines changing core to this one
but
as PIO still use 1.8.0, and Generic_f103Rx variant have no HAL_DAC_MODULE_ENABLED define for xE like 1.9.0 have, it will not use dac for analogWrite by default

here is mine platformio.ini which compiles and correctly generates sine output

[env:genericSTM32F103RE]
platform = ststm32
board = genericSTM32F103RE
framework = arduino

board_build.core = ststm32
board_build.variant = Generic_F103Rx

build_flags = -DHAL_DAC_MODULE_ENABLED
upload_protocol = stlink
debug_tool = stlink

Ok, I do not use PIO :wink: so my second guess was right the core version is not correct. I thought it was possible to use the git now with pio?

ive never tried to use it from git directly

@fpistm

not exactly
he is right, his defines changing core to this one
but
as PIO still use 1.8.0, and Generic_f103Rx variant have no HAL_DAC_MODULE_ENABLED define for xE like 1.9.0 have, it will not use dac for analogWrite by default

here is mine platformio.ini which compiles and correctly generates sine output

[env:genericSTM32F103RE]
platform = ststm32
board = genericSTM32F103RE
framework = arduino

board_build.core = ststm32
board_build.variant = Generic_F103Rx

build_flags = -DHAL_DAC_MODULE_ENABLED
upload_protocol = stlink
debug_tool = stlink

Good so the problem was because of HAL_DAC_MODULE_ENABLED definition I will test that but as I said, I still have the problem with Core version 1.9.0 in Arduino IDE and high density MCU ?!

@hamed-ta
provide all arduino compile logs
i have no issues with 1.9.0 and dac usage for f103re
HAL_DAC_MODULE_ENABLED is defined for Generic_F103Rx variant and 1.9.0 core in case of high-density mcu

@fpistm
almost offtipic, but

ive tried to use latest core from git directly
unfortunately it's not possible to use it as is cuz pio build script trying to find CMSIS dir inside the core, but it originally packaged with tools

copying arduino's tools/CMSIS/5.5.1 forder contents to framework-arduinoststm32@.../CMSIS solved it and it can be successfully compiled by platformio and generates sine fine without defining HAL_DAC_MODULE_ENABLED in the platformio.ini build_flags


[env:genericSTM32F103RE]
platform = ststm32
board = genericSTM32F103RE
framework = arduino
platform_packages =
     framework-arduinoststm32@https://github.com/stm32duino/Arduino_Core_STM32

board_build.core = stm32
board_build.variant = Generic_F103Rx

upload_protocol = stlink
debug_tool = stlink

Well this is related to pio wow so this should be raised on its Github.

@fpistm
no exactly again) platformio packages use only one repo for whole package, as i can see it's impossible to define package as several gits
so it requires to modify how platformio handle packages or how this core store components
im not sure platformio maintainers will do this only for one package, and also im not sure you will rebuild your core structure for platformio
so this issue can't be solved atm )

@hamed-ta
I think you do not select the right board based on your log (I guess you delete your comment anyway I received it by mail):
fqbn=STM32:stm32:GenF1:pnum=BLUEPILL_F103C6

You have to select the proper "Board part number"
image

@stas2z

so it requires to modify how platformio handle packages or how this core store components
im not sure platformio maintainers will do this only for one package, and also im not sure you will rebuild your core structure for platformio

This is not only for this core, this is related to Arduino core. So yes I will not "rebuild" the core structure as it is inline with Arduino WOW. So, I guess PIO should be able to deal with that thanks the .JSON as it provide support for arduino "SDK" it should be able to deal with Arduino packages dependencies. But this is just my feeling :wink:

@hamed-ta
I think you do not select the right board based on your log (I guess you delete your comment anyway I received it by mail):
fqbn=STM32:stm32:GenF1:pnum=BLUEPILL_F103C6

You have to select the proper "Board part number"
image

Yes, I had chosen the right MCU but I don't know why It was changed after updating the core to version 1.9 and I hadn't noticed that, however I have still problem in PIO to enable dac without having to define HAL_DAC_MODULE_ENABLED, which isn't related to the core and I will check that in PIO community,

thank you.

why It was changed after updating the core to version 1.9

Simply because the menu has changed and so the IDE has reset it to the first item in the menu :wink:

This is not only for this core, this is related to Arduino core. So yes I will not "rebuild" the core structure as it is inline with Arduino WOW. So, I guess PIO should be able to deal with that thanks the .JSON as it provide support for arduino "SDK" it should be able to deal with Arduino packages dependencies. But this is just my feeling

Not exactly again, core is packaged fine except CMSIS, as it required to compile user code
All other "tools" are already present by separate vendor independed packages
for uploading pio use openocd for stlink/jlink and dfu-util for dfu, it doesn't use arduino defined tools at all
arm gcc compiler also packaged separately and used for all arm targets

so the problem with your core and pio only with CMSIS, which is copied to main repo folder by pio maintainers, all other things are ok

so the problem with your core and pio only with CMSIS, which is copied to main repo folder by pio maintainers, all other things are ok

That's my point... Arduino manages dependencies PIO not, this is done manually and not linked to "my" core :wink:.
For example, Arduino SAMD Zero board is dependent to:

            {
              "packager": "arduino",
              "name": "CMSIS",
              "version": "4.5.0"
            },
            {
              "packager": "arduino",
              "name": "CMSIS-Atmel",
              "version": "1.2.0"
            }

So if those packages were updated, this will required to PIO maintainer to also repackage it. OK this is the PIO WOW, anyway I guess it could be possible for core which embed the JSON for PIO to add this dependencies and then PIO could manage it dynamically when user request to use the git. As said only my feeling 馃檮

im agree with you, just explaning how it works atm)
pio have platform (stm32) which includes different "packages", for arduino/mbed/cubemx/opencm etc, so platform installing prepacked "cores" on demand (if you choose board using that package)
upload/debug tools and compiler are installed by platform also on demand
it doesn't parse, it just create folder, unpack package there and use platformio-build.py packaged and use it as an enviroment to do stuff
probably it's possible to add extra action to clone cmsis at first build, dunno, im not a pio and python expert

Note:
I've added a dedicated part to Analog section in the WiKi:
https://github.com/stm32duino/wiki/wiki/API#analogwrite-dac-pwm-or-gpio

@fpistm
sorry to bother you here again, but ive made a little CMSIS workaround for platformio
im not sure it's perfect enuff for PR))) but just as an idea

diff --git a/tools/platformio-build.py b/tools/platformio-build.py
index 68b0dfc..8aa0e29 100644
--- a/tools/platformio-build.py
+++ b/tools/platformio-build.py
@@ -22,7 +22,7 @@ kinds of creative coding, interactive objects, spaces or physical experiences.
 https://github.com/stm32duino/Arduino_Core_STM32
 """

-
+import os
 from os.path import isfile, isdir, join

 from SCons.Script import DefaultEnvironment
@@ -34,8 +34,8 @@ board = env.BoardConfig()
 FRAMEWORK_DIR = platform.get_package_dir("framework-arduinoststm32")
 CMSIS_DIR = join(platform.get_package_dir("framework-arduinoststm32"), "CMSIS", "CMSIS")
 assert isdir(FRAMEWORK_DIR)
-assert isdir(CMSIS_DIR)
-
+if not isdir(CMSIS_DIR):
+   os.system("cd %s && wget -qO- %s | tar -xjf -" %(FRAMEWORK_DIR, "https://github.com/stm32duino/ArduinoModule-CMSIS/releases/download/5.5.1/CMSIS-5.5.1.tar.bz2"))

 mcu = env.BoardConfig().get("build.mcu", "")
 board_name = env.subst("$BOARD")

Honestly, I've no idea.
@valeros any comment on this, please?

Hi @stas2z ! Could you please open issue here https://github.com/platformio/platform-ststm32
We plan to refactor support for this core a bit so it'll be possible to use the upstream version too.
Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Hoel picture Hoel  路  4Comments

romainreignier picture romainreignier  路  7Comments

Adminius picture Adminius  路  6Comments

kwastek picture kwastek  路  4Comments

greiman picture greiman  路  5Comments