Skip to content

DAC driver

OwnTech DAC driver Zephyr Module is in charge of STM32 G4 DAC peripheral.

Driver capabilities

The DAC driver module allows you to control DACs on the STM32 G4 board. Currently, only DAC 1, DAC 2 and DAC 3 are supported.

The following functions are provided by the DAC driver module:

How to use

Getting access to the driver

To use the DAC driver module, you have to include "dac.h" in your source file:

Zephyr uses pointers to structures to interact with drivers. The required pointer can be obtained using the following functions:

Using one of these pointers, the user is able to interact with the driver functions of the specific peripheral. The pointer must be provided as first argument of all driver-related functions.

dac_set_const_value()

This function is used to configure a DAC as a constant generator. It just takes as an argument the channel on which the value has to be generated and the value to generate:

Note that after setting the initial value, the channel must be started using dac_start(). To change the value during the course of your program, this function can be called without having to stop the channel.

dac_set_function(), dac_function_update_reset() and dac_function_update_step()

These functions are used to configure a generator function of the DAC. dac_set_function() provides the initial configuration of the function, while dac_function_update_reset() and dac_function_update_step() allow to change the values of the function.

Notes:

  • dac_set_function() must be called called before starting the channel with dac_start(), while the other two must only be called to change the values while the function is running.
  • Calling the value-changing functions if dac_set_function() has not been called will result in call being ignored.
  • For now, the dac_set_function() function is tightly linked to the STM32 LL macros, and requires including stm32_ll_dac.h.

The dac_set_function() function takes a structure parameter, defined as follows:

Allowed values are:

  • dac_function: dac_function_noise, dac_function_triangle or dac_function_sawtooth. NOTE: Only Sawtooth function is implemented for now.
  • trigger_source and step_trigger_source: either LL_DAC_TRIG_SOFTWARE or any macro with prefix LL_DAC_TRIG_EXT_ defined in stm32_ll_dac.h
  • polarity: LL_DAC_SAWTOOTH_POLARITY_DECREMENT or LL_DAC_SAWTOOTH_POLARITY_INCREMENT.
  • reset_data: integer between 0x000 and 0xFFF.
  • step_data: 12.4 bit format between 0x000.0 and 0xFFF.F (without the .).

For more information about these values, please refer to RM0440, sections 22.4.2 and 22.4.9 to 22.4.11.

Example

dac_pin_configure()

This function is used to set the pin configuration of the DAC output. It must be called before starting the channel with dac_start(). For now, this function is tightly linked to the STM32 LL macros, and requires including stm32_ll_dac.h.

The functions takes a structure parameter, defined as follows:

Allowed values are:

  • pin_connect: LL_DAC_OUTPUT_CONNECT_GPIO or LL_DAC_OUTPUT_CONNECT_INTERNAL.
  • pin_buffer_enable: LL_DAC_OUTPUT_BUFFER_ENABLE or LL_DAC_OUTPUT_BUFFER_DISABLE.

For more information about these values, please refer to RM0440, section 22.4.2.

Example

dac_start() and dac_stop()

These function are used to start and stop a channel.

A channel can be started after its initial configuration has been made, using either dac_set_const_value() or dac_set_function(), and if required dac_pin_configure().

Full configuration example

Technical information

While Zephyr provides a DAC driver, it does not provide advanced function support, only constant value set. This driver is intended at supporting STM32G4 DAC functions, such as Sawtooth generation. In order to avoid conflict with Zephyr DAC driver, the configuration option CONFIG_DAC must not be set to y in prj.conf.

This driver has a Zephyr-style implementation. It does use the standard DEVICE_DT_DEFINE() macro that allows it to be loaded automatically if the module is enabled and the device is activated in the device tree. It also provides a board-agnostic layer that would allow, if wanted, to write DAC drivers for other boards.

The only difference with a Zephyr driver would be that it explicitly and individually checks for the presence and status of dac1, dac2 and dac3 nodes in the device tree instead of being fully generic and run as many drivers as there are compatible nodes.

Limitations

While enums are provided for sawtooth, noise and triangle functions, only sawtooth is supported by this driver at the time.

Moreover, configuring the Sawtooth function or pin state requires including stm32_ll_dac.h for now, as the driver directly uses macros defined in this file. It will be made more generic in a future release.

Enabling or disabling the module

This module is loaded by default. The user explicitly has to set CONFIG_OWNTECH_DAC_DRIVER=n in prj.conf to prevent the module from loading.