Skip to content

Commit fb768df

Browse files
committed
samd: AnalogOut: Better handle boards which IGNOREd analog pins
1 parent 194d99f commit fb768df

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

ports/atmel-samd/common-hal/analogio/AnalogOut.c

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,36 @@
4444
#include "hpl/pm/hpl_pm_base.h"
4545
#endif
4646

47+
#define HAVE_ANALOGOUT ( \
48+
(defined(PIN_PA02) && !defined(IGNORE_PA02)) || \
49+
(defined(SAM_D5X_E5X) && defined(PIN_PA05) && !defined(IGNORE_PA05)) \
50+
)
51+
4752
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
4853
const mcu_pin_obj_t *pin) {
49-
#if defined(SAMD21) && !defined(PIN_PA02)
54+
#if !HAVE_ANALOGOUT
5055
mp_raise_NotImplementedError(translate("No DAC on chip"));
5156
#else
52-
if (pin->number != PIN_PA02
53-
#ifdef SAM_D5X_E5X
54-
&& pin->number != PIN_PA05
57+
58+
int channel = -1;
59+
60+
#if defined(PIN_PA02) && !defined(IGNORE_PIN_PA02)
61+
if (pin->number != PIN_PA02) {
62+
channel = 0;
63+
}
64+
#endif
65+
#if defined(PIN_PA05) && defined(PIN_PA05) && !defined(IGNORE_PIN_PA05)
66+
if (pin->number != PIN_PA05) {
67+
channel = 1;
68+
}
5569
#endif
56-
) {
70+
71+
if(channel == -1) {
5772
mp_raise_ValueError(translate("AnalogOut not supported on given pin"));
5873
return;
5974
}
6075

61-
self->channel = 0;
62-
#ifdef SAM_D5X_E5X
63-
if (pin->number == PIN_PA05) {
64-
self->channel = 1;
65-
}
66-
#endif
76+
self->channel = channel;
6777

6878
#ifdef SAM_D5X_E5X
6979
hri_mclk_set_APBDMASK_DAC_bit(MCLK);
@@ -105,11 +115,15 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
105115
}
106116

107117
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
118+
#if !HAVE_ANALOGOUT
119+
return false;
120+
#else
108121
return self->deinited;
122+
#endif
109123
}
110124

111125
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
112-
#if (defined(SAMD21) && defined(PIN_PA02)) || defined(SAM_D5X_E5X)
126+
#if HAVE_ANALOGOUT
113127
if (common_hal_analogio_analogout_deinited(self)) {
114128
return;
115129
}
@@ -130,20 +144,19 @@ void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
130144

131145
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
132146
uint16_t value) {
133-
#if defined(SAMD21) && !defined(PIN_PA02)
134-
return;
135-
#endif
147+
#if HAVE_ANALOGOUT
136148
// Input is 16 bit so make sure and set LEFTADJ to 1 so it takes the top
137149
// bits. This is currently done in asf4_conf/*/hpl_dac_config.h.
138150
dac_sync_write(&self->descriptor, self->channel, &value, 1);
151+
#endif
139152
}
140153

141154
void analogout_reset(void) {
142155
// audioout_reset also resets the DAC, and does a smooth ramp down to avoid clicks
143156
// if it was enabled, so do that instead if AudioOut is enabled.
144157
#if CIRCUITPY_AUDIOIO
145158
audioout_reset();
146-
#else
159+
#elif HAVE_ANALOGOUT
147160
#ifdef SAMD21
148161
while (DAC->STATUS.reg & DAC_STATUS_SYNCBUSY) {}
149162
#endif

0 commit comments

Comments
 (0)