Skip to content

Commit

Permalink
need to enable both DAC channels at once
Browse files Browse the repository at this point in the history
  • Loading branch information
joeycastillo committed Oct 30, 2023
1 parent d31a3ec commit b60c7ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
15 changes: 11 additions & 4 deletions peripherals/dac.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,23 @@ void dac_init(void) {
#endif
}

void dac_enable(uint16_t channel) {
void dac_enable(dac_channel_mask_t channelmask) {
#if defined(_SAMD21_) || defined(_SAMD11_)
(void) channel;
// Chips with a single DAC don't need to enable individual channels
#else // SAM L21 / SAM D51
DAC->CTRLA.bit.ENABLE = 0;
_dac_sync();
DAC->DACCTRL[channel].bit.ENABLE = 1;
DAC->DACCTRL[channel].bit.CCTRL = DAC_DACCTRL_CCTRL_CC1M_Val;
DAC->DACCTRL[channel].bit.REFRESH = 2;
if (channelmask & 1) {
DAC->DACCTRL[0].bit.ENABLE = 1;
DAC->DACCTRL[0].bit.CCTRL = DAC_DACCTRL_CCTRL_CC1M_Val;
DAC->DACCTRL[0].bit.REFRESH = 2;
}
if (channelmask & 2) {
DAC->DACCTRL[1].bit.ENABLE = 1;
DAC->DACCTRL[1].bit.CCTRL = DAC_DACCTRL_CCTRL_CC1M_Val;
DAC->DACCTRL[1].bit.REFRESH = 2;
}
#endif
DAC->CTRLA.bit.ENABLE = 1;
_dac_sync();
Expand Down
15 changes: 13 additions & 2 deletions peripherals/dac.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
#include <stdint.h>
#include <stdbool.h>

// channel mask enum for dac_enable
typedef enum {
DAC_CHANNEL_NONE = 0,
DAC_CHANNEL_0 = 1,
DAC_CHANNEL_1 = 2,
DAC_CHANNEL_BOTH = 3
} dac_channel_mask_t;

/**
* @brief Initializes the DAC peripheral, but does not enable it.
* @param channel The DAC channel to enable.
Expand All @@ -39,9 +47,12 @@
void dac_init(void);

/**
* @brief Enables the given DAC channel.
* @brief Enables the DAC channels specified in the mask, and disables others.
* @param channelmask A bitmask of the channels to enable:
* 1 for channel 0, 2 for channel 1, 3 for both channels.
* For MCUs with only one DAC channel, this parameter is ignored.
*/
void dac_enable(uint16_t channel);
void dac_enable(dac_channel_mask_t channelmask);

/**
* @brief Set the analog value of the DAC.
Expand Down

0 comments on commit b60c7ef

Please sign in to comment.