@@ -27,20 +27,32 @@ extern "C" {
27
27
static int _readResolution = 10 ;
28
28
static int _writeResolution = 10 ;
29
29
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
+
30
44
void analogReadResolution ( int res )
31
45
{
46
+ syncADC ();
32
47
switch ( res )
33
48
{
34
49
case 12 :
35
- while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
36
50
ADC -> CTRLB .bit .RESSEL = ADC_CTRLB_RESSEL_12BIT_Val ;
37
51
break ;
38
52
case 8 :
39
- while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
40
53
ADC -> CTRLB .bit .RESSEL = ADC_CTRLB_RESSEL_8BIT_Val ;
41
54
break ;
42
55
default :
43
- while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
44
56
ADC -> CTRLB .bit .RESSEL = ADC_CTRLB_RESSEL_10BIT_Val ;
45
57
break ;
46
58
}
@@ -77,7 +89,7 @@ static inline uint32_t mapResolution( uint32_t value, uint32_t from, uint32_t to
77
89
*/
78
90
void analogReference ( eAnalogReference ulMode )
79
91
{
80
- while ( ADC -> STATUS . bit . SYNCBUSY == 1 );
92
+ syncADC ( );
81
93
switch ( ulMode )
82
94
{
83
95
case AR_INTERNAL :
@@ -122,15 +134,15 @@ uint32_t analogRead( uint32_t ulPin )
122
134
123
135
if (ulPin == A0 ) // Disable DAC, if analogWrite(A0,dval) used previously the DAC is enabled
124
136
{
125
- while ( DAC -> STATUS . bit . SYNCBUSY == 1 );
137
+ syncDAC ( );
126
138
DAC -> CTRLA .bit .ENABLE = 0x00 ; // Disable DAC
127
139
//DAC->CTRLB.bit.EOEN = 0x00; // The DAC output is turned off.
128
- while ( DAC -> STATUS . bit . SYNCBUSY == 1 );
140
+ syncDAC ( );
129
141
}
130
142
131
143
pinPeripheral (ulPin , g_APinDescription [ulPin ].ulPinType );
132
144
133
- while ( ADC -> STATUS . bit . SYNCBUSY == 1 );
145
+ syncADC ( );
134
146
ADC -> INPUTCTRL .bit .MUXPOS = g_APinDescription [ulPin ].ulADCChannelNumber ; // Selection for the positive ADC input
135
147
136
148
// Control A
@@ -145,28 +157,27 @@ uint32_t analogRead( uint32_t ulPin )
145
157
* Before enabling the ADC, the asynchronous clock source must be selected and enabled, and the ADC reference must be
146
158
* configured. The first conversion after the reference is changed must not be used.
147
159
*/
148
- while ( ADC -> STATUS . bit . SYNCBUSY == 1 );
160
+ syncADC ( );
149
161
ADC -> CTRLA .bit .ENABLE = 0x01 ; // Enable ADC
150
- while ( ADC -> STATUS .bit .SYNCBUSY == 1 );
151
162
152
163
// Start conversion
153
- while ( ADC -> STATUS . bit . SYNCBUSY == 1 );
164
+ syncADC ( );
154
165
ADC -> SWTRIG .bit .START = 1 ;
155
166
156
167
// Clear the Data Ready flag
157
168
ADC -> INTFLAG .bit .RESRDY = 1 ;
158
169
159
170
// 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 ( );
161
172
ADC -> SWTRIG .bit .START = 1 ;
162
173
163
174
// Store the value
164
175
while ( ADC -> INTFLAG .bit .RESRDY == 0 ); // Waiting for conversion to complete
165
176
valueRead = ADC -> RESULT .reg ;
166
177
167
- while ( ADC -> STATUS . bit . SYNCBUSY == 1 );
178
+ syncADC ( );
168
179
ADC -> CTRLA .bit .ENABLE = 0x00 ; // Disable ADC
169
- while ( ADC -> STATUS . bit . SYNCBUSY == 1 );
180
+ syncADC ( );
170
181
171
182
return valueRead ;
172
183
}
@@ -192,11 +203,11 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
192
203
return ;
193
204
}
194
205
195
- while ( DAC -> STATUS . bit . SYNCBUSY == 1 );
206
+ syncDAC ( );
196
207
DAC -> DATA .reg = ulValue & 0x3FF ; // DAC on 10 bits.
197
- while ( DAC -> STATUS . bit . SYNCBUSY == 1 );
208
+ syncDAC ( );
198
209
DAC -> CTRLA .bit .ENABLE = 0x01 ; //Enable ADC
199
- while ( DAC -> STATUS . bit . SYNCBUSY == 1 );
210
+ syncDAC ( );
200
211
return ;
201
212
}
202
213
0 commit comments