0

I'm trying to use the DAC on PA4 of the STM32F4 Discovery board with Zephyr.

In order to do that, I have created the following dtoverlay file. I have tried a handful of different overlay configurations here and this is what I have landed with since it didn't appear as if any DAC was configured in the main dts file for the stm32f4_disco board:

/ {
    soc {
        dac1: dac@40007400 {
            compatible = "st,stm32-dac";
            reg = <0x40007400 0x400>;
            status = "okay";
            label = "DAC_1";
            clocks = <&rcc STM32_CLOCK_BUS_APB1 0x20000000>;
            pinctrl-0 = <&dac_out1_pa4>;
            pinctrl-names = "default";
        };
    };
};

I have also enabled the DAC in the prj.conf by adding the following:

CONFIG_DAC=y
CONFIG_DAC_STM32=y

Here is the program I'm trying to build:

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/dac.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DAC_DEVICE_NODE DT_NODELABEL(dac1)
#define DAC_CHANNEL_ID 0 // Use DAC_OUT1 (PA4) as the output pin
#define DAC_RESOLUTION 12 // 12-bit resolution

static const struct device *const dac_dev = DEVICE_DT_GET(DAC_DEVICE_NODE);

int main(void) {

  if (!dac_dev) {
    printk("Cannot find %s!\n", DAC_DEVICE_NODE);
    return 1;
  }

  struct dac_channel_cfg channel_cfg = {
    .channel_id = DAC_CHANNEL_ID,
    .resolution = DAC_RESOLUTION,
  };


  if (dac_channel_setup(dac_dev, &channel_cfg) != 0) {
    printk("Setting up of channel %d failed!\n", DAC_CHANNEL_ID);
    return 1;
  }

  /*
   .... do something.. 
  */

  return 0;
}

However, when I build it, I get the following error:

$ west build -b stm32f4_disco -p auto
[28/40] Building C object CMakeFiles/app.dir/src/main.c.obj
FAILED: CMakeFiles/app.dir/src/main.c.obj 
ccache /home/mitch/bin/zephyr-sdk-0.16.0/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc -DCORE_CM4 -DHSE_VALUE=8000000 -DKERNEL -DSTM32F407xx -DUSE_FULL_LL_DRIVER -DUSE_HAL_DRIVER -D__PROGRAM_START -D__ZEPHYR__=1 -I/home/mitch/git/myproject/zephyr/include -I/home/mitch/git/myproject/build/zephyr/include/generated -I/home/mitch/git/myproject/zephyr/soc/arm/st_stm32/stm32f4 -I/home/mitch/git/myproject/zephyr/drivers -I/home/mitch/git/myproject/zephyr/soc/arm/st_stm32/common -I/home/mitch/git/myproject/modules/hal/cmsis/CMSIS/Core/Include -I/home/mitch/git/myproject/modules/hal/stm32/stm32cube/stm32f4xx/soc -I/home/mitch/git/myproject/modules/hal/stm32/stm32cube/stm32f4xx/drivers/include -I/home/mitch/git/myproject/modules/hal/stm32/stm32cube/stm32f4xx/drivers/include/Legacy -I/home/mitch/git/myproject/modules/hal/stm32/stm32cube/common_ll/include -isystem /home/mitch/git/myproject/zephyr/lib/libc/minimal/include -isystem /home/mitch/bin/zephyr-sdk-0.16.0/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/include -isystem /home/mitch/bin/zephyr-sdk-0.16.0/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/include-fixed -fno-strict-aliasing -Os -imacros /home/mitch/git/myproject/build/zephyr/include/generated/autoconf.h -ffreestanding -fno-common -g -gdwarf-4 -fdiagnostics-color=always -mcpu=cortex-m4 -mthumb -mabi=aapcs --sysroot=/home/mitch/bin/zephyr-sdk-0.16.0/arm-zephyr-eabi/arm-zephyr-eabi -imacros /home/mitch/git/myproject/zephyr/include/zephyr/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-pointer-sign -Wpointer-arith -Wexpansion-to-defined -Wno-unused-but-set-variable -Werror=implicit-int -fno-pic -fno-pie -fno-asynchronous-unwind-tables -fno-reorder-functions --param=min-pagesize=0 -fno-defer-pop -fmacro-prefix-map=/home/mitch/git/myproject=CMAKE_SOURCE_DIR -fmacro-prefix-map=/home/mitch/git/myproject/zephyr=ZEPHYR_BASE -fmacro-prefix-map=/home/mitch/git/myproject=WEST_TOPDIR -ffunction-sections -fdata-sections -std=c99 -nostdinc -MD -MT CMakeFiles/app.dir/src/main.c.obj -MF CMakeFiles/app.dir/src/main.c.obj.d -o CMakeFiles/app.dir/src/main.c.obj -c /home/mitch/git/myproject/src/main.c
In file included from /home/mitch/git/myproject/zephyr/include/zephyr/devicetree.h:19,
                 from /home/mitch/git/myproject/zephyr/include/zephyr/arch/arm/aarch32/arch.h:20,
                 from /home/mitch/git/myproject/zephyr/include/zephyr/arch/cpu.h:19,
                 from /home/mitch/git/myproject/zephyr/include/zephyr/kernel_includes.h:33,
                 from /home/mitch/git/myproject/zephyr/include/zephyr/kernel.h:17,
                 from /home/mitch/git/myproject/src/main.c:1:
/home/mitch/git/myproject/src/main.c: In function 'main':
/home/mitch/git/myproject/build/zephyr/include/generated/devicetree_generated.h:4816:34: error: 'DT_N_S_soc_S_dac_40007400' undeclared (first use in this function); did you mean 'DT_N_S_soc_S_dac_40007400_ORD'?
 4816 | #define DT_N_NODELABEL_dac1      DT_N_S_soc_S_dac_40007400
      |                                  ^~~~~~~~~~~~~~~~~~~~~~~~~
/home/mitch/git/myproject/zephyr/include/zephyr/devicetree.h:4096:24: note: in expansion of macro 'DT_N_NODELABEL_dac1'
 4096 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
/home/mitch/git/myproject/zephyr/include/zephyr/devicetree.h:192:29: note: in expansion of macro 'DT_CAT'
  192 | #define DT_NODELABEL(label) DT_CAT(DT_N_NODELABEL_, label)
      |                             ^~~~~~
/home/mitch/git/myproject/src/main.c:13:25: note: in expansion of macro 'DT_NODELABEL'
   13 | #define DAC_DEVICE_NODE DT_NODELABEL(dac1)
      |                         ^~~~~~~~~~~~
/home/mitch/git/myproject/src/main.c:103:33: note: in expansion of macro 'DAC_DEVICE_NODE'
  103 |     printk("Cannot find %s!\n", DAC_DEVICE_NODE);
      |                                 ^~~~~~~~~~~~~~~
/home/mitch/git/myproject/build/zephyr/include/generated/devicetree_generated.h:4816:34: note: each undeclared identifier is reported only once for each function it appears in
 4816 | #define DT_N_NODELABEL_dac1      DT_N_S_soc_S_dac_40007400
      |                                  ^~~~~~~~~~~~~~~~~~~~~~~~~
/home/mitch/git/myproject/zephyr/include/zephyr/devicetree.h:4096:24: note: in expansion of macro 'DT_N_NODELABEL_dac1'
 4096 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
/home/mitch/git/myproject/zephyr/include/zephyr/devicetree.h:192:29: note: in expansion of macro 'DT_CAT'
  192 | #define DT_NODELABEL(label) DT_CAT(DT_N_NODELABEL_, label)
      |                             ^~~~~~
/home/mitch/git/myproject/src/main.c:13:25: note: in expansion of macro 'DT_NODELABEL'
   13 | #define DAC_DEVICE_NODE DT_NODELABEL(dac1)
      |                         ^~~~~~~~~~~~
/home/mitch/git/myproject/src/main.c:103:33: note: in expansion of macro 'DAC_DEVICE_NODE'
  103 |     printk("Cannot find %s!\n", DAC_DEVICE_NODE);
      |                                 ^~~~~~~~~~~~~~~
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/mitch/git/myproject/build

This seems to be caused by the ability to find that device node in the the device tree binary. But I haven't been able to figure out why. The board's documentation does say that there is DAC support on PA4 and PA5.

Would appreciate any help on this. Thanks

1 Answer 1

1

The problem is thatDAC_DEVICE_NODE is being used as a string when it is really a node identifier. You should use DEVICE_DT_NAME(DAC_DEVICE_NODE):

if (!dac_dev) {
    // No need to use %s since DEVICE_DT_NAME gives a string literal at compile time
    printk("Cannot find " DEVICE_DT_NAME(DAC_DEVICE_NODE) "!\n");
    return 1;
}

In fact, dac_dev cannot be NULL as it would fail to link if DEVICE_DT_GET(DAC_DEVICE_NODE) was unable to locate a valid node. You can use DEVICE_DT_GET_OR_NULL(DAC_DEVICE_NODE) if you need different behavior for some reason. See: https://docs.zephyrproject.org/apidoc/latest/group__device__model.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.