Commit 82cd7ad9 authored by Gaetan Perrot's avatar Gaetan Perrot Committed by Fabio Baltieri
Browse files

drivers: dac: sam: Add max value check



Add max size check to dac sam and sam0
There is no size check in dac_sam_write_value and dac_sam0_write_value.
Besides, the ret value should also be different.
Fixes #65021

signed-off-by: default avatarGaetan Perrot <gaetanperrotpro@gmail.com>
parent fc8b53bf
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -107,6 +107,11 @@ static int dac_sam_write_value(const struct device *dev, uint8_t channel,
		return -EINVAL;
	}

	if (value >= BIT(12)) {
		LOG_ERR("value %d out of range", value);
		return -EINVAL;
	}

	k_sem_take(&dev_data->dac_channels[channel].sem, K_FOREVER);

	/* Trigger conversion */
+7 −0
Original line number Diff line number Diff line
@@ -11,6 +11,8 @@
#include <zephyr/drivers/dac.h>
#include <zephyr/drivers/pinctrl.h>
#include <soc.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(dac_sam0, CONFIG_DAC_LOG_LEVEL);

/*
 * Maps between the DTS reference property names and register values.  Note that
@@ -37,6 +39,11 @@ static int dac_sam0_write_value(const struct device *dev, uint8_t channel,
	const struct dac_sam0_cfg *const cfg = dev->config;
	Dac *regs = cfg->regs;

	if (value >= BIT(12)) {
		LOG_ERR("value %d out of range", value);
		return -EINVAL;
	}

	regs->DATA.reg = (uint16_t)value;

	return 0;