Skip to content

Commit 4f34ea9

Browse files
committed
add alt for vdac
1 parent d4d0226 commit 4f34ea9

File tree

2 files changed

+68
-32
lines changed

2 files changed

+68
-32
lines changed

hardware/arduino/zunoG2/cores/LLCore/zuno_dac.cpp

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,22 @@ static ZunoGpioBus_t _get_bus_type(uint8_t channel) {
1919
}
2020
return (type);
2121
}
22+
23+
static uint8_t _get_bus_alt_channel(ZunoDacClassChannelProcess_t *process) {
24+
uint8_t real_pin;
25+
26+
if (process->vdac_port != vdacChPortB)
27+
return (UNKNOWN_CHANNEL);
28+
if ((real_pin = process->real_pin) == 0x0)
29+
return (0x0);
30+
if (real_pin == 0x1)
31+
return (0x1);
32+
return (UNKNOWN_CHANNEL);
33+
}
34+
2235
#endif
2336

37+
#if defined(_SILICON_LABS_32B_SERIES_2)
2438
/* Public Constructors */
2539
ZunoDacClass::ZunoDacClass(void): _vRef(ZunoDacClassRef1V25), _init(false){
2640
size_t i;
@@ -76,32 +90,43 @@ bool ZunoDacClass::disable(uint8_t pin) {
7690
}
7791

7892
/* Private Methods */
79-
bool ZunoDacClass::_test_free_channel(ZunoDacClassChannelProcess_t *process) {
80-
uint8_t i;
81-
uint8_t i_free;
93+
bool ZunoDacClass::_test_already_channel(ZunoDacClassChannelProcess_t *process) {
8294
uint8_t n;
8395

84-
i = 0x0;
85-
i_free = 0x0;
8696
n = 0x0;
8797
while (n < (sizeof(this->_save) / sizeof(this->_save[0x0]))) {
88-
if (this->_save[n].pin == process[i].pin) {
89-
process[i].channel = n;
98+
if (this->_save[n].pin == process->pin) {
99+
process->channel = n;
90100
break ;
91101
}
92102
n++;
93103
}
94-
if (n >= (sizeof(this->_save) / sizeof(this->_save[0x0]))) {
95-
while (i_free < (sizeof(this->_save) / sizeof(this->_save[0x0]))) {
96-
if (this->_save[i_free].pin == UNKNOWN_PIN) {
97-
process[i].channel = i_free;
98-
break ;
99-
}
100-
i_free++;
104+
if (n < (sizeof(this->_save) / sizeof(this->_save[0x0])))
105+
return (true);
106+
return (false);
107+
}
108+
109+
bool ZunoDacClass::_test_free_channel(ZunoDacClassChannelProcess_t *process) {
110+
uint8_t i_free;
111+
uint8_t alt_channel;
112+
113+
alt_channel = _get_bus_alt_channel(process);
114+
if (alt_channel != UNKNOWN_CHANNEL && alt_channel < (sizeof(this->_save) / sizeof(this->_save[0x0])) && this->_save[alt_channel].pin == UNKNOWN_PIN) {
115+
process->channel = alt_channel;
116+
process->alt = true;
117+
return (true);
118+
}
119+
process->alt = false;
120+
i_free = 0x0;
121+
while (i_free < (sizeof(this->_save) / sizeof(this->_save[0x0]))) {
122+
if (this->_save[i_free].pin == UNKNOWN_PIN) {
123+
process->channel = i_free;
124+
break ;
101125
}
102-
if (i_free >= (sizeof(this->_save) / sizeof(this->_save[0x0])))
103-
return (false);
126+
i_free++;
104127
}
128+
if (i_free >= (sizeof(this->_save) / sizeof(this->_save[0x0])))
129+
return (false);
105130
return (true);
106131
}
107132

@@ -135,32 +160,38 @@ bool ZunoDacClass::_write(ZunoDacClassChannelProcess_t *process) {
135160
VDAC_InitChannel_TypeDef initChannel;
136161
ZunoGpioBus_t type;
137162

163+
if (this->_test_already_channel(process) == true) {
164+
if (this->_save[process->channel].value == process->value)
165+
return (true);
166+
this->_save[process->channel].value = process->value;
167+
VDAC_ChannelOutputSet(VDAC0, process->channel, process->value);
168+
return (true);
169+
}
138170
if (this->_test_free_channel(process) == false)
139171
return (false);
140-
if (this->_save[process->channel].pin == UNKNOWN_PIN) {
172+
if (process->alt == false) {
141173
if ((type = _get_bus_type(process->channel)) == ZunoGpioBusUnknown)
142174
return (false);
143175
if (zme_gpio_bus_alloc(process->pin, type) == false)
144176
return (false);
145-
initChannel = VDAC_INITCHANNEL_DEFAULT;
146-
//initChannel.highCapLoadEnable = false;
147-
//initChannel.powerMode = vdacPowerModeLowPower;
177+
}
178+
initChannel = VDAC_INITCHANNEL_DEFAULT;
179+
//initChannel.highCapLoadEnable = false;
180+
//initChannel.powerMode = vdacPowerModeLowPower;
181+
if (process->alt == true) {
182+
initChannel.auxOutEnable = false;
183+
initChannel.mainOutEnable = true;
184+
}
185+
else {
148186
initChannel.auxOutEnable = true;
149187
initChannel.mainOutEnable = false;
150188
initChannel.port = process->vdac_port;
151189
initChannel.pin = process->real_pin;
152-
VDAC_InitChannel(VDAC0, &initChannel, process->channel);
153-
VDAC_Enable(VDAC0, process->channel, true);
154-
this->_save[process->channel].value = process->value;
155-
this->_save[process->channel].pin = process->pin;
156-
}
157-
else {
158-
if (this->_save[process->channel].value == process->value)
159-
return (true);
160-
this->_save[process->channel].value = process->value;
161-
VDAC_ChannelOutputSet(VDAC0, process->channel, process->value);
162-
return (true);
163190
}
191+
VDAC_InitChannel(VDAC0, &initChannel, process->channel);
192+
VDAC_Enable(VDAC0, process->channel, true);
193+
this->_save[process->channel].value = process->value;
194+
this->_save[process->channel].pin = process->pin;
164195
i = 0x0;
165196
while (i < (sizeof(this->_save) / sizeof(this->_save[0x0]))) {
166197
if (this->_save[i].pin != UNKNOWN_PIN) {
@@ -217,4 +248,5 @@ void ZunoDacClass::_dac_init(ZunoDacClassRef_t ref) {
217248
}
218249
#endif
219250

220-
ZunoDacClass DAC = ZunoDacClass();
251+
ZunoDacClass DAC = ZunoDacClass();
252+
#endif

hardware/arduino/zunoG2/cores/LLCore/zuno_dac.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "em_vdac.h"
55

6+
#if defined(_SILICON_LABS_32B_SERIES_2)
67
typedef enum ZunoDacClassRef_e
78
{
89
ZunoDacClassRef1V25 = vdacRef1V25,
@@ -17,6 +18,7 @@ typedef struct ZunoDacClassChannelProcess_s
1718
#if defined(_SILICON_LABS_32B_SERIES_2)
1819
uint8_t real_pin;
1920
VDAC_ChPortSel_t vdac_port;
21+
bool alt;
2022
#endif
2123
} ZunoDacClassChannelProcess_t;
2224

@@ -35,6 +37,7 @@ class ZunoDacClass {
3537
bool disable(uint8_t pin);
3638

3739
private:
40+
inline bool _test_already_channel(ZunoDacClassChannelProcess_t *process);
3841
inline bool _test_free_channel(ZunoDacClassChannelProcess_t *process);
3942
inline bool _write(ZunoDacClassChannelProcess_t *process);
4043
inline bool _get_values(ZunoDacClassChannelProcess_t *process, uint8_t pin, float v);
@@ -48,5 +51,6 @@ class ZunoDacClass {
4851
};
4952

5053
extern ZunoDacClass DAC;
54+
#endif
5155

5256
#endif//ZUNO_DAC_H

0 commit comments

Comments
 (0)