@@ -42,6 +42,18 @@ static void syncDAC() {
42
42
;
43
43
}
44
44
45
+ // Wait for synchronization of registers between the clock domains
46
+ static __inline__ void syncTC_8 (Tc * TCx ) __attribute__((always_inline , unused ));
47
+ static void syncTC_8 (Tc * TCx ) {
48
+ while (TCx -> COUNT8 .STATUS .bit .SYNCBUSY );
49
+ }
50
+
51
+ // Wait for synchronization of registers between the clock domains
52
+ static __inline__ void syncTCC (Tcc * TCCx ) __attribute__((always_inline , unused ));
53
+ static void syncTCC (Tcc * TCCx ) {
54
+ while (TCCx -> SYNCBUSY .reg & TCC_SYNCBUSY_MASK );
55
+ }
56
+
45
57
void analogReadResolution ( int res )
46
58
{
47
59
_readResolution = res ;
@@ -236,8 +248,6 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
236
248
case 1 : // TCC1
237
249
// Enable GCLK for TCC0 and TCC1 (timer counter input clock)
238
250
GCLK -> CLKCTRL .reg = (uint16_t ) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID ( GCM_TCC0_TCC1 )) ;
239
-
240
- while ( GCLK -> STATUS .bit .SYNCBUSY == 1 ) ;
241
251
break ;
242
252
243
253
case 2 : // TCC2
@@ -259,6 +269,8 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
259
269
break ;
260
270
}
261
271
272
+ while ( GCLK -> STATUS .bit .SYNCBUSY == 1 ) ;
273
+
262
274
ulValue = mapResolution (ulValue , _writeResolution , 8 );
263
275
264
276
// Set PORT
@@ -268,30 +280,39 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
268
280
269
281
// Disable TCx
270
282
TCx -> COUNT8 .CTRLA .reg &= ~TC_CTRLA_ENABLE ;
283
+ syncTC_8 (TCx );
271
284
// Set Timer counter Mode to 8 bits
272
285
TCx -> COUNT8 .CTRLA .reg |= TC_CTRLA_MODE_COUNT8 ;
273
286
// Set TCx as normal PWM
274
287
TCx -> COUNT8 .CTRLA .reg |= TC_CTRLA_WAVEGEN_NPWM ;
275
288
// Set TCx in waveform mode Normal PWM
276
289
TCx -> COUNT8 .CC [Channelx ].reg = (uint8_t ) ulValue ;
290
+ syncTC_8 (TCx );
277
291
// Set PER to maximum counter value (resolution : 0xFF)
278
292
TCx -> COUNT8 .PER .reg = 0xFF ;
293
+ syncTC_8 (TCx );
279
294
// Enable TCx
280
295
TCx -> COUNT8 .CTRLA .reg |= TC_CTRLA_ENABLE ;
296
+ syncTC_8 (TCx );
281
297
}
282
298
else
283
299
{
284
300
// -- Configure TCC
285
301
// Disable TCCx
286
302
TCCx -> CTRLA .reg &= ~TCC_CTRLA_ENABLE ;
303
+ syncTCC (TCCx );
287
304
// Set TCx as normal PWM
288
305
TCCx -> WAVE .reg |= TCC_WAVE_WAVEGEN_NPWM ;
306
+ syncTCC (TCCx );
289
307
// Set TCx in waveform mode Normal PWM
290
308
TCCx -> CC [Channelx ].reg = (uint32_t )ulValue ;
309
+ syncTCC (TCCx );
291
310
// Set PER to maximum counter value (resolution : 0xFF)
292
311
TCCx -> PER .reg = 0xFF ;
312
+ syncTCC (TCCx );
293
313
// Enable TCCx
294
314
TCCx -> CTRLA .reg |= TCC_CTRLA_ENABLE ;
315
+ syncTCC (TCCx );
295
316
}
296
317
297
318
return ;
0 commit comments