@@ -19,8 +19,22 @@ static ZunoGpioBus_t _get_bus_type(uint8_t channel) {
19
19
}
20
20
return (type);
21
21
}
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
+
22
35
#endif
23
36
37
+ #if defined(_SILICON_LABS_32B_SERIES_2)
24
38
/* Public Constructors */
25
39
ZunoDacClass::ZunoDacClass (void ): _vRef(ZunoDacClassRef1V25), _init(false ){
26
40
size_t i;
@@ -76,32 +90,43 @@ bool ZunoDacClass::disable(uint8_t pin) {
76
90
}
77
91
78
92
/* 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) {
82
94
uint8_t n;
83
95
84
- i = 0x0 ;
85
- i_free = 0x0 ;
86
96
n = 0x0 ;
87
97
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;
90
100
break ;
91
101
}
92
102
n++;
93
103
}
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 ;
101
125
}
102
- if (i_free >= (sizeof (this ->_save ) / sizeof (this ->_save [0x0 ])))
103
- return (false );
126
+ i_free++;
104
127
}
128
+ if (i_free >= (sizeof (this ->_save ) / sizeof (this ->_save [0x0 ])))
129
+ return (false );
105
130
return (true );
106
131
}
107
132
@@ -135,32 +160,38 @@ bool ZunoDacClass::_write(ZunoDacClassChannelProcess_t *process) {
135
160
VDAC_InitChannel_TypeDef initChannel;
136
161
ZunoGpioBus_t type;
137
162
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
+ }
138
170
if (this ->_test_free_channel (process) == false )
139
171
return (false );
140
- if (this -> _save [ process->channel ]. pin == UNKNOWN_PIN ) {
172
+ if (process->alt == false ) {
141
173
if ((type = _get_bus_type (process->channel )) == ZunoGpioBusUnknown)
142
174
return (false );
143
175
if (zme_gpio_bus_alloc (process->pin , type) == false )
144
176
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 {
148
186
initChannel.auxOutEnable = true ;
149
187
initChannel.mainOutEnable = false ;
150
188
initChannel.port = process->vdac_port ;
151
189
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 );
163
190
}
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 ;
164
195
i = 0x0 ;
165
196
while (i < (sizeof (this ->_save ) / sizeof (this ->_save [0x0 ]))) {
166
197
if (this ->_save [i].pin != UNKNOWN_PIN) {
@@ -217,4 +248,5 @@ void ZunoDacClass::_dac_init(ZunoDacClassRef_t ref) {
217
248
}
218
249
#endif
219
250
220
- ZunoDacClass DAC = ZunoDacClass();
251
+ ZunoDacClass DAC = ZunoDacClass();
252
+ #endif
0 commit comments