From a52551babe6cee31f0d08cab4012d910a23a664f Mon Sep 17 00:00:00 2001 From: pmantoine Date: Fri, 8 Apr 2022 12:35:19 +0800 Subject: [PATCH] SAMD timer code --- DCCTimerSAMD.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/DCCTimerSAMD.cpp b/DCCTimerSAMD.cpp index b3324a9..deff05b 100644 --- a/DCCTimerSAMD.cpp +++ b/DCCTimerSAMD.cpp @@ -88,6 +88,18 @@ void DCCTimer::begin(INTERRUPT_CALLBACK callback) { TCC0->WAVE.reg = TCC_WAVE_WAVEGEN_NPWM; // Select NPWM as waveform while (TCC0->SYNCBUSY.bit.WAVE); // Wait for sync TCC0->INTENSET.reg = TCC_INTENSET_OVF; // Interrupt on overflow + + // PMA - set the frequency + TCC0->CTRLA.reg |= TCC_CTRLA_PRESCALER(TCC_CTRLA_PRESCALER_DIV1_Val); + unsigned long cycles = F_CPU / 10000000 * 58; // 58uS to cycles + unsigned long pwmPeriod = cycles / 2; + TCC0->PER.reg = pwmPeriod; + while (TCC0->SYNCBUSY.bit.PER); + + // PMA - start it + TCC0->CTRLA.bit.ENABLE = 1; + while (TCC0->SYNCBUSY.bit.ENABLE); + NVIC_EnableIRQ((IRQn_Type)TCC0_IRQn); // Enable the interrupt interrupts(); }