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: Gaetan Perrot <gaetanperrotpro@gmail.com>
This commit is contained in:
Gaetan Perrot 2023-12-25 14:20:30 +09:00 committed by Fabio Baltieri
parent fc8b53bf10
commit 82cd7ad9b6
2 changed files with 12 additions and 0 deletions

View file

@ -107,6 +107,11 @@ static int dac_sam_write_value(const struct device *dev, uint8_t channel,
return -EINVAL; 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); k_sem_take(&dev_data->dac_channels[channel].sem, K_FOREVER);
/* Trigger conversion */ /* Trigger conversion */

View file

@ -11,6 +11,8 @@
#include <zephyr/drivers/dac.h> #include <zephyr/drivers/dac.h>
#include <zephyr/drivers/pinctrl.h> #include <zephyr/drivers/pinctrl.h>
#include <soc.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 * 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; const struct dac_sam0_cfg *const cfg = dev->config;
Dac *regs = cfg->regs; Dac *regs = cfg->regs;
if (value >= BIT(12)) {
LOG_ERR("value %d out of range", value);
return -EINVAL;
}
regs->DATA.reg = (uint16_t)value; regs->DATA.reg = (uint16_t)value;
return 0; return 0;