1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-28 16:01:18 +01:00

Merge pull request #21548 from benpicco/cpu/samd5x-dac

cpu/sam0_common: fix DAC on SAM D5x/E5x
This commit is contained in:
crasbe 2025-06-11 11:27:52 +00:00 committed by GitHub
commit 18c666a15a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 13 deletions

View File

@ -377,10 +377,10 @@ static const adc_conf_chan_t adc_channels[] = {
*/
/* Must not exceed 12 MHz */
#define DAC_CLOCK SAM0_GCLK_TIMER
/* Use external reference voltage on PA03 */
/* (You have to manually connect PA03 with Vcc) */
#ifndef DAC_VREF
/* Internal reference only gives 1V */
#define DAC_VREF DAC_CTRLB_REFSEL_VREFPU
#define DAC_VREF DAC_CTRLB_REFSEL_INTREF
#endif
/** @} */
/**

View File

@ -23,6 +23,7 @@
#include "cpu.h"
#include "periph/dac.h"
#include "periph/gpio.h"
#include "macros/units.h"
#define DAC_VAL(in) (in >> (16 - DAC_RES_BITS))
@ -66,15 +67,15 @@ static inline void _sync(void)
#ifdef DAC_DACCTRL_CCTRL_Msk
static uint32_t _get_CCTRL(uint32_t freq)
{
if (freq < 1200000) {
if (freq <= KHZ(1200)) {
return DAC_DACCTRL_CCTRL_CC100K;
}
if (freq < 6000000) {
if (freq <= MHZ(6)) {
return DAC_DACCTRL_CCTRL_CC1M;
}
if (freq < 12000000) {
if (freq <= MHZ(12)) {
return DAC_DACCTRL_CCTRL_CC12M;
}
@ -109,8 +110,8 @@ int8_t dac_init(dac_t line)
_dac_init_clock(line);
/* Settings can only be changed when DAC is disabled */
DAC->CTRLA.reg &= ~DAC_CTRLA_ENABLE;
/* Settings can only be changed when DAC is disabled, reset config */
DAC->CTRLA.reg = DAC_CTRLA_SWRST;
_sync();
#ifdef DAC_DACCTRL_ENABLE
@ -125,7 +126,7 @@ int8_t dac_init(dac_t line)
#endif
;
DAC->CTRLA.reg |= DAC_CTRLA_ENABLE;
DAC->CTRLA.reg = DAC_CTRLA_ENABLE;
_sync();
#ifdef DAC_STATUS_READY
@ -140,14 +141,14 @@ int8_t dac_init(dac_t line)
void dac_set(dac_t line, uint16_t value)
{
#ifdef DAC_SYNCBUSY_DATA1
/* DAC has multiple outputs */
const uint32_t mask = (1 << (DAC_SYNCBUSY_DATA_Pos + line));
while (DAC->SYNCBUSY.reg & mask) {}
const uint32_t mask = (1 << (DAC_INTFLAG_EMPTY_Pos + line));
while (!(DAC->INTFLAG.reg & mask)) {}
/* DAC has multiple outputs */
DAC->DATA[line].reg = DAC_VAL(value);
#else
/* DAC has only one output */
(void) line;
(void)line;
_sync();
DAC->DATA.reg = DAC_VAL(value);