@@ -178,6 +178,15 @@ void configureSAMDClock() {
178
178
* faster won't be possible without sacrificing resolution.
179
179
*/
180
180
void configureTCC (tccConfiguration& tccConfig, long pwm_frequency, bool negate, float hw6pwm) {
181
+
182
+ long pwm_resolution = (24000000 ) / pwm_frequency;
183
+ if (pwm_resolution>SIMPLEFOC_SAMD_MAX_PWM_RESOLUTION)
184
+ pwm_resolution = SIMPLEFOC_SAMD_MAX_PWM_RESOLUTION;
185
+ if (pwm_resolution<SIMPLEFOC_SAMD_MIN_PWM_RESOLUTION)
186
+ pwm_resolution = SIMPLEFOC_SAMD_MIN_PWM_RESOLUTION;
187
+ // store for later use
188
+ tccConfig.pwm_res = pwm_resolution;
189
+
181
190
// TODO for the moment we ignore the frequency...
182
191
if (!tccConfigured[tccConfig.tcc .tccn ]) {
183
192
uint32_t GCLK_CLKCTRL_ID_ofthistcc = -1 ;
@@ -235,12 +244,12 @@ void configureTCC(tccConfiguration& tccConfig, long pwm_frequency, bool negate,
235
244
236
245
if (hw6pwm>0.0 ) {
237
246
tcc->WEXCTRL .vec .DTIEN |= (1 <<tccConfig.tcc .chan );
238
- tcc->WEXCTRL .bit .DTLS = hw6pwm*(SIMPLEFOC_SAMD_PWM_RESOLUTION -1 );
239
- tcc->WEXCTRL .bit .DTHS = hw6pwm*(SIMPLEFOC_SAMD_PWM_RESOLUTION -1 );
247
+ tcc->WEXCTRL .bit .DTLS = hw6pwm*(pwm_resolution -1 );
248
+ tcc->WEXCTRL .bit .DTHS = hw6pwm*(pwm_resolution -1 );
240
249
syncTCC (tcc); // wait for sync
241
250
}
242
251
243
- tcc->PER .reg = SIMPLEFOC_SAMD_PWM_RESOLUTION - 1 ; // Set counter Top using the PER register
252
+ tcc->PER .reg = pwm_resolution - 1 ; // Set counter Top using the PER register
244
253
while ( tcc->SYNCBUSY .bit .PER == 1 ); // wait for sync
245
254
246
255
// set all channels to 0%
@@ -262,7 +271,8 @@ void configureTCC(tccConfiguration& tccConfig, long pwm_frequency, bool negate,
262
271
SIMPLEFOC_SAMD_DEBUG_SERIAL.print (tccConfig.tcc .chan );
263
272
SIMPLEFOC_SAMD_DEBUG_SERIAL.print (" [" );
264
273
SIMPLEFOC_SAMD_DEBUG_SERIAL.print (tccConfig.wo );
265
- SIMPLEFOC_SAMD_DEBUG_SERIAL.println (" ]" );
274
+ SIMPLEFOC_SAMD_DEBUG_SERIAL.print (" ] pwm res " );
275
+ SIMPLEFOC_SAMD_DEBUG_SERIAL.println (pwm_resolution);
266
276
#endif
267
277
}
268
278
}
@@ -280,8 +290,8 @@ void configureTCC(tccConfiguration& tccConfig, long pwm_frequency, bool negate,
280
290
281
291
if (hw6pwm>0.0 ) {
282
292
tcc->WEXCTRL .vec .DTIEN |= (1 <<tccConfig.tcc .chan );
283
- tcc->WEXCTRL .bit .DTLS = hw6pwm*(SIMPLEFOC_SAMD_PWM_RESOLUTION -1 );
284
- tcc->WEXCTRL .bit .DTHS = hw6pwm*(SIMPLEFOC_SAMD_PWM_RESOLUTION -1 );
293
+ tcc->WEXCTRL .bit .DTLS = hw6pwm*(pwm_resolution -1 );
294
+ tcc->WEXCTRL .bit .DTHS = hw6pwm*(pwm_resolution -1 );
285
295
syncTCC (tcc); // wait for sync
286
296
}
287
297
@@ -295,7 +305,8 @@ void configureTCC(tccConfiguration& tccConfig, long pwm_frequency, bool negate,
295
305
SIMPLEFOC_SAMD_DEBUG_SERIAL.print (tccConfig.tcc .chan );
296
306
SIMPLEFOC_SAMD_DEBUG_SERIAL.print (" [" );
297
307
SIMPLEFOC_SAMD_DEBUG_SERIAL.print (tccConfig.wo );
298
- SIMPLEFOC_SAMD_DEBUG_SERIAL.println (" ]" );
308
+ SIMPLEFOC_SAMD_DEBUG_SERIAL.print (" ] pwm res " );
309
+ SIMPLEFOC_SAMD_DEBUG_SERIAL.println (pwm_resolution);
299
310
#endif
300
311
}
301
312
@@ -316,8 +327,8 @@ void writeSAMDDutyCycle(tccConfiguration* info, float dc) {
316
327
// uint32_t chanbit = 0x1<<(TCC_SYNCBUSY_CC0_Pos+chan);
317
328
// while ( (tcc->SYNCBUSY.reg & chanbit) > 0 );
318
329
// set via CCB
319
- while ( (tcc->SYNCBUSY .vec .CC & (0x1 <<chan)) > 0 );
320
- tcc->CCB [chan].reg = (uint32_t )((SIMPLEFOC_SAMD_PWM_RESOLUTION -1 ) * dc);
330
+ // while ( (tcc->SYNCBUSY.vec.CC & (0x1<<chan)) > 0 );
331
+ tcc->CCB [chan].reg = (uint32_t )((info-> pwm_res -1 ) * dc);
321
332
// while ( (tcc->SYNCBUSY.vec.CCB & (0x1<<chan)) > 0 );
322
333
// tcc->STATUS.vec.CCBV |= (0x1<<chan);
323
334
// while ( tcc->SYNCBUSY.bit.STATUS > 0 );
0 commit comments