1 /*
2 * Copyright (c) 2020 Libre Solar Technologies GmbH
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
8 #include <zephyr/drivers/dac.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/ztest.h>
11
12 #if defined(CONFIG_BOARD_NUCLEO_F091RC) || \
13 defined(CONFIG_BOARD_NUCLEO_F207ZG) || \
14 defined(CONFIG_BOARD_STM32F3_DISCO) || \
15 defined(CONFIG_BOARD_NUCLEO_F429ZI) || \
16 defined(CONFIG_BOARD_NUCLEO_F746ZG) || \
17 defined(CONFIG_BOARD_NUCLEO_F767ZI) || \
18 defined(CONFIG_BOARD_NUCLEO_F722ZE) || \
19 defined(CONFIG_BOARD_NUCLEO_G071RB) || \
20 defined(CONFIG_BOARD_NUCLEO_G431RB) || \
21 defined(CONFIG_BOARD_NUCLEO_G474RE) || \
22 defined(CONFIG_BOARD_NUCLEO_H743ZI) || \
23 defined(CONFIG_BOARD_NUCLEO_L073RZ) || \
24 defined(CONFIG_BOARD_NUCLEO_L152RE) || \
25 defined(CONFIG_BOARD_DISCO_L475_IOT1) || \
26 defined(CONFIG_BOARD_NUCLEO_L552ZE_Q) || \
27 defined(CONFIG_BOARD_STM32L562E_DK) || \
28 defined(CONFIG_BOARD_STM32H573I_DK) || \
29 defined(CONFIG_BOARD_STM32U083C_DK) || \
30 defined(CONFIG_BOARD_B_U585I_IOT02A) || \
31 defined(CONFIG_BOARD_NUCLEO_U083RC) || \
32 defined(CONFIG_BOARD_NUCLEO_U575ZI_Q) || \
33 defined(CONFIG_BOARD_NUCLEO_U5A5ZJ_Q) || \
34 defined(CONFIG_BOARD_NUCLEO_WL55JC) || \
35 defined(CONFIG_BOARD_RONOTH_LODEV)
36
37 #define DAC_DEVICE_NODE DT_NODELABEL(dac1)
38 #define DAC_CHANNEL_ID 1
39 #define DAC_RESOLUTION 12
40
41 #elif defined(CONFIG_BOARD_NUCLEO_H563ZI)
42
43 #define DAC_DEVICE_NODE DT_NODELABEL(dac1)
44 #define DAC_CHANNEL_ID 2
45 #define DAC_RESOLUTION 12
46
47 #elif defined(CONFIG_BOARD_TWR_KE18F) || \
48 defined(CONFIG_BOARD_FRDM_K64F) || \
49 defined(CONFIG_BOARD_FRDM_K22F) || \
50 defined(CONFIG_BOARD_FRDM_MCXN947) || \
51 defined(CONFIG_BOARD_FRDM_MCXA156) || \
52 defined(CONFIG_BOARD_SEEEDUINO_XIAO) || \
53 defined(CONFIG_BOARD_ARDUINO_MKRZERO) || \
54 defined(CONFIG_BOARD_ARDUINO_ZERO) || \
55 defined(CONFIG_BOARD_LPCXPRESSO55S36)
56
57 #define DAC_DEVICE_NODE DT_NODELABEL(dac0)
58 #define DAC_RESOLUTION 12
59 #define DAC_CHANNEL_ID 0
60
61 #elif defined(CONFIG_BOARD_BL652_DVK) || \
62 defined(CONFIG_BOARD_BL653_DVK) || \
63 defined(CONFIG_BOARD_BL654_DVK) || \
64 defined(CONFIG_BOARD_BL5340_DVK)
65 /* Note external DAC MCP4725 is not populated on BL652_DVK, BL653_DVK and
66 * BL654_DVK at factory
67 */
68 #define DAC_DEVICE_NODE DT_NODELABEL(dac0)
69 #define DAC_RESOLUTION 12
70 #define DAC_CHANNEL_ID 0
71
72 #elif defined(CONFIG_BOARD_ESP32_DEVKITC_WROOM) || \
73 defined(CONFIG_BOARD_ESP32_DEVKITC_WROVER) || \
74 defined(CONFIG_BOARD_ESP_WROVER_KIT) || \
75 defined(CONFIG_BOARD_ESP32S2_SAOLA) || \
76 defined(CONFIG_BOARD_ESP32S2_DEVKITC) || \
77 defined(CONFIG_BOARD_GD32A503V_EVAL) || \
78 defined(CONFIG_BOARD_GD32E103V_EVAL) || \
79 defined(CONFIG_BOARD_GD32F450I_EVAL) || \
80 defined(CONFIG_BOARD_GD32F450Z_EVAL) || \
81 defined(CONFIG_BOARD_GD32F470I_EVAL) || \
82 defined(CONFIG_BOARD_YD_ESP32)
83
84 #define DAC_DEVICE_NODE DT_NODELABEL(dac)
85 #define DAC_RESOLUTION 12
86 #define DAC_CHANNEL_ID 0
87
88 #elif defined(CONFIG_BOARD_SAM_E70_XPLAINED) || \
89 defined(CONFIG_BOARD_SAM_V71_XULT)
90
91 #define DAC_DEVICE_NODE DT_NODELABEL(dacc)
92 #define DAC_RESOLUTION 12
93 #define DAC_CHANNEL_ID 0
94
95 #elif defined(CONFIG_BOARD_RD_RW612_BGA) || \
96 defined(CONFIG_BOARD_FRDM_RW612)
97
98 #define DAC_DEVICE_NODE DT_NODELABEL(dac0)
99 #define DAC_RESOLUTION 10
100 #define DAC_CHANNEL_ID 0
101
102 #else
103 #error "Unsupported board."
104 #endif
105
106 static const struct dac_channel_cfg dac_ch_cfg = {
107 .channel_id = DAC_CHANNEL_ID,
108 .resolution = DAC_RESOLUTION,
109 .buffered = true
110 };
111
get_dac_device(void)112 const struct device *get_dac_device(void)
113 {
114 return DEVICE_DT_GET(DAC_DEVICE_NODE);
115 }
116
init_dac(void)117 static const struct device *init_dac(void)
118 {
119 int ret;
120 const struct device *const dac_dev = DEVICE_DT_GET(DAC_DEVICE_NODE);
121
122 zassert_true(device_is_ready(dac_dev), "DAC device is not ready");
123
124 ret = dac_channel_setup(dac_dev, &dac_ch_cfg);
125 zassert_ok(ret, "Setting up of the first channel failed with code %d", ret);
126
127 return dac_dev;
128 }
129
130 /*
131 * test_dac_write_value
132 */
ZTEST(dac,test_task_write_value)133 ZTEST(dac, test_task_write_value)
134 {
135 int ret;
136
137 const struct device *dac_dev = init_dac();
138
139 /* write a value of half the full scale resolution */
140 ret = dac_write_value(dac_dev, DAC_CHANNEL_ID,
141 (1U << DAC_RESOLUTION) / 2);
142 zassert_ok(ret, "dac_write_value() failed with code %d", ret);
143 }
144
dac_setup(void)145 static void *dac_setup(void)
146 {
147 k_object_access_grant(get_dac_device(), k_current_get());
148
149 return NULL;
150 }
151
152 ZTEST_SUITE(dac, NULL, dac_setup, NULL, NULL, NULL);
153