Commit 8870a699 authored by Martin Jäger's avatar Martin Jäger Committed by Carles Cufi
Browse files

samples: drivers: dac: Add API example



This example generates a sawtooth signal to show how to use the DAC.

Signed-off-by: default avatarMartin Jäger <martin@libre.solar>
parent 65e12e05
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.13.1)

find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
project(DAC)

target_sources(app PRIVATE src/main.c)
+32 −0
Original line number Diff line number Diff line
.. _dac-sample:

Digital-to-Analog Converter (DAC)
#################################

Overview
********

This sample demonstrates how to use the DAC driver API.

Building and Running
********************

The DAC output is defined in the board's devicetree and pinmux file.

Only board nucleo_l073rz is supported for now.

Sample output
=============

You should see a sawtooth signal with an amplitude of the DAC reference
voltage and a period of approx. 4 seconds at the DAC output pin specified
by the board.

The following output is printed:

.. code-block:: console

   DAC internal reference voltage: 3300 mV
   Generating sawtooth signal at DAC channel 1.

.. note:: If the DAC is not supported, the output will be an error message.
+1 −0
Original line number Diff line number Diff line
CONFIG_DAC=y
+11 −0
Original line number Diff line number Diff line
sample:
  name: DAC driver sample
tests:
  sample.drivers.dac:
    tags: DAC
    depends_on: dac
    harness: console
    harness_config:
      type: one_line
      regex:
        - "Generating sawtooth signal at DAC channel ([0-9]*)."
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2020 Libre Solar Technologies GmbH
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/dac.h>

#define DAC_DEVICE_NAME		DT_LABEL(DT_ALIAS(dac1))
#define DAC_CHANNEL_ID		1
#define DAC_RESOLUTION		12

static const struct dac_channel_cfg dac_ch_cfg = {
	.channel_id  = DAC_CHANNEL_ID,
	.resolution  = DAC_RESOLUTION
};

void main(void)
{
	struct device *dac_dev = device_get_binding(DAC_DEVICE_NAME);

	if (!dac_dev) {
		printk("Cannot get DAC device\n");
		return;
	}

	int ret = dac_channel_setup(dac_dev, &dac_ch_cfg);

	if (ret != 0) {
		printk("Setting up of DAC channel failed with code %d\n", ret);
		return;
	}

	printk("Generating sawtooth signal at DAC channel %d.\n",
		DAC_CHANNEL_ID);
	while (1) {
		/* Number of valid DAC values, e.g. 4096 for 12-bit DAC */
		const int dac_values = 1U << DAC_RESOLUTION;

		/*
		 * 1 msec sleep leads to about 4 sec signal period for 12-bit
		 * DACs. For DACs with lower resolution, sleep time needs to
		 * be increased.
		 * Make sure to sleep at least 1 msec even for future 16-bit
		 * DACs (lowering signal frequency).
		 */
		const int sleep_time = 4096 / dac_values > 0 ?
			4096 / dac_values : 1;

		for (int i = 0; i < dac_values; i++) {
			dac_write_value(dac_dev, DAC_CHANNEL_ID, i);
			k_sleep(K_MSEC(sleep_time));
		}
	}
}