samples: dac: move HW config to /zephyr,user

Require board overlays to configure the sample in the /zephyr,user
node, and access the device with DEVICE_DT_GET instead of
device_get_binding(). Update the documentation and move existing board
configuration to overlays.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2021-04-26 12:47:29 -07:00 committed by Carles Cufí
commit baf93a1821
14 changed files with 104 additions and 32 deletions

View file

@ -13,6 +13,10 @@ Building and Running
The DAC output is defined in the board's devicetree and pinmux file. The DAC output is defined in the board's devicetree and pinmux file.
The board's :ref:`/zephyr,user <dt-zephyr-user>` node must have ``dac``,
``dac-channel-id``, and ``dac-resolution`` properties set. See the predefined
overlays in :zephyr_file:`samples/drivers/dac/boards` for examples.
Building and Running for ST Nucleo L073RZ Building and Running for ST Nucleo L073RZ
========================================= =========================================
The sample can be built and executed for the The sample can be built and executed for the

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac0>;
dac-channel-id = <0>;
dac-resolution = <10>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac0>;
dac-channel-id = <0>;
dac-resolution = <12>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac0>;
dac-channel-id = <0>;
dac-resolution = <12>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac1>;
dac-channel-id = <1>;
dac-resolution = <12>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac1>;
dac-channel-id = <1>;
dac-resolution = <12>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac1>;
dac-channel-id = <1>;
dac-resolution = <12>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac1>;
dac-channel-id = <1>;
dac-resolution = <12>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac1>;
dac-channel-id = <1>;
dac-resolution = <12>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac1>;
dac-channel-id = <1>;
dac-resolution = <12>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac1>;
dac-channel-id = <1>;
dac-resolution = <12>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac1>;
dac-channel-id = <1>;
dac-resolution = <12>;
};
};

View file

@ -0,0 +1,7 @@
/ {
zephyr,user {
dac = <&dac0>;
dac-channel-id = <0>;
dac-resolution = <12>;
};
};

View file

@ -8,37 +8,23 @@
#include <sys/printk.h> #include <sys/printk.h>
#include <drivers/dac.h> #include <drivers/dac.h>
#if defined(CONFIG_BOARD_NUCLEO_F091RC) || \ #define ZEPHYR_USER_NODE DT_PATH(zephyr_user)
defined(CONFIG_BOARD_NUCLEO_G071RB) || \
defined(CONFIG_BOARD_NUCLEO_G431RB) || \ #if (DT_NODE_HAS_PROP(ZEPHYR_USER_NODE, dac) && \
defined(CONFIG_BOARD_NUCLEO_L073RZ) || \ DT_NODE_HAS_PROP(ZEPHYR_USER_NODE, dac_channel_id) && \
defined(CONFIG_BOARD_NUCLEO_L152RE) || \ DT_NODE_HAS_PROP(ZEPHYR_USER_NODE, dac_resolution))
defined(CONFIG_BOARD_NUCLEO_F429ZI) || \ #define DAC_NODE DT_PHANDLE(ZEPHYR_USER_NODE, dac)
defined(CONFIG_BOARD_NUCLEO_F767ZI) || \ #define DAC_CHANNEL_ID DT_PROP(ZEPHYR_USER_NODE, dac_channel_id)
defined(CONFIG_BOARD_RONOTH_LODEV) #define DAC_RESOLUTION DT_PROP(ZEPHYR_USER_NODE, dac_resolution)
#define DAC_DEVICE_NAME DT_LABEL(DT_NODELABEL(dac1))
#define DAC_CHANNEL_ID 1
#define DAC_RESOLUTION 12
#elif defined(CONFIG_BOARD_TWR_KE18F)
#define DAC_DEVICE_NAME DT_LABEL(DT_NODELABEL(dac0))
#define DAC_CHANNEL_ID 0
#define DAC_RESOLUTION 12
#elif defined(CONFIG_BOARD_FRDM_K64F)
#define DAC_DEVICE_NAME DT_LABEL(DT_NODELABEL(dac0))
#define DAC_CHANNEL_ID 0
#define DAC_RESOLUTION 12
#elif defined(CONFIG_BOARD_FRDM_K22F)
#define DAC_DEVICE_NAME DT_LABEL(DT_NODELABEL(dac0))
#define DAC_CHANNEL_ID 0
#define DAC_RESOLUTION 12
#elif defined(CONFIG_BOARD_ARDUINO_ZERO)
#define DAC_DEVICE_NAME DT_LABEL(DT_NODELABEL(dac0))
#define DAC_CHANNEL_ID 0
#define DAC_RESOLUTION 10
#else #else
#error "Unsupported board." #error "Unsupported board: see README and check /zephyr,user node"
#define DAC_NODE DT_INVALID_NODE
#define DAC_CHANNEL_ID 0
#define DAC_RESOLUTION 0
#endif #endif
static const struct device *dac_dev = DEVICE_DT_GET(DAC_NODE);
static const struct dac_channel_cfg dac_ch_cfg = { static const struct dac_channel_cfg dac_ch_cfg = {
.channel_id = DAC_CHANNEL_ID, .channel_id = DAC_CHANNEL_ID,
.resolution = DAC_RESOLUTION .resolution = DAC_RESOLUTION
@ -46,10 +32,8 @@ static const struct dac_channel_cfg dac_ch_cfg = {
void main(void) void main(void)
{ {
const struct device *dac_dev = device_get_binding(DAC_DEVICE_NAME); if (!device_is_ready(dac_dev)) {
printk("DAC device %s is not ready\n", dac_dev->name);
if (!dac_dev) {
printk("Cannot get DAC device\n");
return; return;
} }