Skip to content

Commit 5cf84a4

Browse files
committed
wiring_analog.c: factored out SYNCBUSY loops
1 parent a594bc3 commit 5cf84a4

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

cores/arduino/wiring_analog.c

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,32 @@ extern "C" {
2727
static int _readResolution = 10;
2828
static int _writeResolution = 10;
2929

30+
// Wait for synchronization of registers between the clock domains
31+
static __inline__ void syncADC() __attribute__((always_inline, unused));
32+
static void syncADC() {
33+
while (ADC->STATUS.bit.SYNCBUSY == 1)
34+
;
35+
}
36+
37+
// Wait for synchronization of registers between the clock domains
38+
static __inline__ void syncDAC() __attribute__((always_inline, unused));
39+
static void syncDAC() {
40+
while (DAC->STATUS.bit.SYNCBUSY == 1)
41+
;
42+
}
43+
3044
void analogReadResolution( int res )
3145
{
46+
syncADC();
3247
switch ( res )
3348
{
3449
case 12:
35-
while( ADC->STATUS.bit.SYNCBUSY == 1 );
3650
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_12BIT_Val;
3751
break;
3852
case 8:
39-
while( ADC->STATUS.bit.SYNCBUSY == 1 );
4053
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_8BIT_Val;
4154
break;
4255
default:
43-
while( ADC->STATUS.bit.SYNCBUSY == 1 );
4456
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_10BIT_Val;
4557
break;
4658
}
@@ -77,7 +89,7 @@ static inline uint32_t mapResolution( uint32_t value, uint32_t from, uint32_t to
7789
*/
7890
void analogReference( eAnalogReference ulMode )
7991
{
80-
while ( ADC->STATUS.bit.SYNCBUSY == 1 );
92+
syncADC();
8193
switch ( ulMode )
8294
{
8395
case AR_INTERNAL:
@@ -122,15 +134,15 @@ uint32_t analogRead( uint32_t ulPin )
122134

123135
if (ulPin == A0) // Disable DAC, if analogWrite(A0,dval) used previously the DAC is enabled
124136
{
125-
while ( DAC->STATUS.bit.SYNCBUSY == 1 );
137+
syncDAC();
126138
DAC->CTRLA.bit.ENABLE = 0x00; // Disable DAC
127139
//DAC->CTRLB.bit.EOEN = 0x00; // The DAC output is turned off.
128-
while ( DAC->STATUS.bit.SYNCBUSY == 1 );
140+
syncDAC();
129141
}
130142

131143
pinPeripheral(ulPin, g_APinDescription[ulPin].ulPinType);
132144

133-
while ( ADC->STATUS.bit.SYNCBUSY == 1 );
145+
syncADC();
134146
ADC->INPUTCTRL.bit.MUXPOS = g_APinDescription[ulPin].ulADCChannelNumber; // Selection for the positive ADC input
135147

136148
// Control A
@@ -145,28 +157,27 @@ uint32_t analogRead( uint32_t ulPin )
145157
* Before enabling the ADC, the asynchronous clock source must be selected and enabled, and the ADC reference must be
146158
* configured. The first conversion after the reference is changed must not be used.
147159
*/
148-
while ( ADC->STATUS.bit.SYNCBUSY == 1 );
160+
syncADC();
149161
ADC->CTRLA.bit.ENABLE = 0x01; // Enable ADC
150-
while ( ADC->STATUS.bit.SYNCBUSY == 1 );
151162

152163
// Start conversion
153-
while ( ADC->STATUS.bit.SYNCBUSY == 1 );
164+
syncADC();
154165
ADC->SWTRIG.bit.START = 1;
155166

156167
// Clear the Data Ready flag
157168
ADC->INTFLAG.bit.RESRDY = 1;
158169

159170
// Start conversion again, since The first conversion after the reference is changed must not be used.
160-
while ( ADC->STATUS.bit.SYNCBUSY == 1 );
171+
syncADC();
161172
ADC->SWTRIG.bit.START = 1;
162173

163174
// Store the value
164175
while ( ADC->INTFLAG.bit.RESRDY == 0 ); // Waiting for conversion to complete
165176
valueRead = ADC->RESULT.reg;
166177

167-
while ( ADC->STATUS.bit.SYNCBUSY == 1 );
178+
syncADC();
168179
ADC->CTRLA.bit.ENABLE = 0x00; // Disable ADC
169-
while ( ADC->STATUS.bit.SYNCBUSY == 1 );
180+
syncADC();
170181

171182
return valueRead;
172183
}
@@ -192,11 +203,11 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
192203
return;
193204
}
194205

195-
while ( DAC->STATUS.bit.SYNCBUSY == 1 );
206+
syncDAC();
196207
DAC->DATA.reg = ulValue & 0x3FF; // DAC on 10 bits.
197-
while ( DAC->STATUS.bit.SYNCBUSY == 1 );
208+
syncDAC();
198209
DAC->CTRLA.bit.ENABLE = 0x01; //Enable ADC
199-
while ( DAC->STATUS.bit.SYNCBUSY == 1 );
210+
syncDAC();
200211
return ;
201212
}
202213

0 commit comments

Comments
 (0)