Skip to content

Commit a039a68

Browse files
committed
core: arduino: analogWrite: Fix DAC output calculation
- Correct max value calculation - Add guard for DAC entry is not defined case - initialize only if DAC not initialized Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
1 parent 04e105e commit a039a68

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

cores/arduino/Arduino.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ void analogReadResolution(int bits);
119119
#define DAC_ENUMS(n, p, i) DAC##i = i,
120120

121121
enum dacPins {
122-
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), dac_channels, DAC_ENUMS) NUM_OF_DACS
122+
#if DT_PROP_LEN_OR(DT_PATH(zephyr_user), dac_channels, 0) > 0
123+
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), dac_channels, DAC_ENUMS)
124+
#endif
125+
NUM_OF_DACS
123126
};
124127

125128
#endif

cores/arduino/zephyrCommon.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,13 @@ static const struct device *const dac_dev = DEVICE_DT_GET(DAC_NODE);
201201
.buffered = true, \
202202
},
203203

204+
#if DT_PROP_LEN_OR(DT_PATH(zephyr_user), dac_channels, 0) > 0
204205
static const struct dac_channel_cfg dac_ch_cfg[] = {
205206
DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), dac_channels, DAC_CHANNEL_DEFINE)};
206207

208+
static bool dac_channel_initialized[NUM_OF_DACS];
209+
#endif
210+
207211
#endif
208212

209213
#endif // CONFIG_DAC
@@ -346,18 +350,39 @@ void analogWrite(pin_size_t pinNumber, int value) {
346350

347351
#ifdef CONFIG_DAC
348352
void analogWrite(enum dacPins dacName, int value) {
353+
#if DT_PROP_LEN_OR(DT_PATH(zephyr_user), dac_channels, 0) > 0
354+
const int maxInput = BIT(_analog_write_resolution) - 1U;
355+
int ret = 0;
356+
349357
if (dacName >= NUM_OF_DACS) {
350358
return;
351359
}
352360

353-
// TODO: add reverse map to find pin name from DAC* define
354-
// In the meantime, consider A0 == DAC0
355-
_reinit_peripheral_if_needed((pin_size_t)(dacName + A0), dac_dev);
356-
dac_channel_setup(dac_dev, &dac_ch_cfg[dacName]);
361+
if (!dac_channel_initialized[dacName]) {
362+
if (!device_is_ready(dac_dev)) {
363+
return;
364+
}
357365

358-
const int max_dac_value = 1U << dac_ch_cfg[dacName].resolution;
359-
dac_write_value(dac_dev, dac_ch_cfg[dacName].channel_id,
360-
map(value, 0, 1 << _analog_write_resolution, 0, max_dac_value));
366+
// TODO: add reverse map to find pin name from DAC* define
367+
// In the meantime, consider A0 == DAC0
368+
_reinit_peripheral_if_needed((pin_size_t)(dacName + A0), dac_dev);
369+
ret = dac_channel_setup(dac_dev, &dac_ch_cfg[dacName]);
370+
if (ret != 0) {
371+
return;
372+
}
373+
dac_channel_initialized[dacName] = true;
374+
}
375+
376+
value = CLAMP(value, 0, maxInput);
377+
378+
const int max_dac_value = BIT(dac_ch_cfg[dacName].resolution) - 1;
379+
const uint32_t output = map(value, 0, maxInput, 0, max_dac_value);
380+
381+
(void)dac_write_value(dac_dev, dac_ch_cfg[dacName].channel_id, output);
382+
#else
383+
ARG_UNUSED(dacName);
384+
ARG_UNUSED(value);
385+
#endif
361386
}
362387
#endif
363388

0 commit comments

Comments
 (0)