5 #if defined(ARDUINO_ARCH_SAMD)
8 #ifdef USE_DEFAULT_ENABLE_IR_IN
9 #error Must undef USE_DEFAULT_ENABLE_IR_IN
18 static void setTimerFrequency(
int frequencyHz) {
19 int compareValue = (
SYSCLOCK / (TIMER_PRESCALER_DIV * frequencyHz)) - 1;
21 TcCount16* TC = (TcCount16*) TC3;
24 TC->COUNT.reg = map(TC->COUNT.reg, 0, TC->CC[0].reg, 0, compareValue);
25 TC->CC[0].reg = compareValue;
30 while (TC->STATUS.bit.SYNCBUSY == 1)
34 static void startTimer() {
35 REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID_TCC2_TC3);
36 while (GCLK->STATUS.bit.SYNCBUSY == 1)
39 TcCount16* TC = (TcCount16*) TC3;
42 TC->CTRLA.reg &= ~TC_CTRLA_ENABLE;
44 while (TC->STATUS.bit.SYNCBUSY == 1);
46 TC->CTRLA.reg = TC_CTRLA_SWRST;
49 while (TC->CTRLA.bit.SWRST)
55 TC->CTRLA.reg |= TC_CTRLA_MODE_COUNT16 | TC_CTRLA_WAVEGEN_MFRQ | TC_CTRLA_PRESCALER_DIV64 | TC_CTRLA_ENABLE;
61 TC->INTENSET.bit.MC0 = 1;
63 NVIC_EnableIRQ (TC3_IRQn);
86 TC3->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE;
91 void TC3_Handler(
void) {
92 TcCount16* TC = (TcCount16*) TC3;
95 if (TC->INTFLAG.bit.MC0 == 1) {
96 TC->INTFLAG.bit.MC0 = 1;
101 #endif // defined(ARDUINO_ARCH_SAMD)