Skip to content

Commit 64de2e4

Browse files
committed
SAMD driver - pwm freq settable also on SAMD21
1 parent 494ced5 commit 64de2e4

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/drivers/hardware_specific/samd21_mcu.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ void configureSAMDClock() {
178178
* faster won't be possible without sacrificing resolution.
179179
*/
180180
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+
181190
// TODO for the moment we ignore the frequency...
182191
if (!tccConfigured[tccConfig.tcc.tccn]) {
183192
uint32_t GCLK_CLKCTRL_ID_ofthistcc = -1;
@@ -235,12 +244,12 @@ void configureTCC(tccConfiguration& tccConfig, long pwm_frequency, bool negate,
235244

236245
if (hw6pwm>0.0) {
237246
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);
240249
syncTCC(tcc); // wait for sync
241250
}
242251

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
244253
while ( tcc->SYNCBUSY.bit.PER == 1 ); // wait for sync
245254

246255
// set all channels to 0%
@@ -262,7 +271,8 @@ void configureTCC(tccConfiguration& tccConfig, long pwm_frequency, bool negate,
262271
SIMPLEFOC_SAMD_DEBUG_SERIAL.print(tccConfig.tcc.chan);
263272
SIMPLEFOC_SAMD_DEBUG_SERIAL.print("[");
264273
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);
266276
#endif
267277
}
268278
}
@@ -280,8 +290,8 @@ void configureTCC(tccConfiguration& tccConfig, long pwm_frequency, bool negate,
280290

281291
if (hw6pwm>0.0) {
282292
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);
285295
syncTCC(tcc); // wait for sync
286296
}
287297

@@ -295,7 +305,8 @@ void configureTCC(tccConfiguration& tccConfig, long pwm_frequency, bool negate,
295305
SIMPLEFOC_SAMD_DEBUG_SERIAL.print(tccConfig.tcc.chan);
296306
SIMPLEFOC_SAMD_DEBUG_SERIAL.print("[");
297307
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);
299310
#endif
300311
}
301312

@@ -316,8 +327,8 @@ void writeSAMDDutyCycle(tccConfiguration* info, float dc) {
316327
// uint32_t chanbit = 0x1<<(TCC_SYNCBUSY_CC0_Pos+chan);
317328
// while ( (tcc->SYNCBUSY.reg & chanbit) > 0 );
318329
// 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);
321332
// while ( (tcc->SYNCBUSY.vec.CCB & (0x1<<chan)) > 0 );
322333
// tcc->STATUS.vec.CCBV |= (0x1<<chan);
323334
// while ( tcc->SYNCBUSY.bit.STATUS > 0 );

0 commit comments

Comments
 (0)