42void ConfigurePeriodicInterrupt(uint32_t frequencyHz);
46extern "C" void TCC2_0_Handler(
void) __attribute__((
47 alias(
"PeriodicInterrupt")));
52#define PERIODIC_INTERRUPT_PRIORITY 4
53#define ACK_PERIODIC_INTERRUPT TCC2->INTFLAG.reg = TCC_INTFLAG_MASK
57uint32_t interruptFreqHz = 4;
66extern "C" void PeriodicInterrupt(
void) {
69 ConnectorLed.
State(ledState);
72 ACK_PERIODIC_INTERRUPT;
76 ConfigurePeriodicInterrupt(interruptFreqHz);
79void ConfigurePeriodicInterrupt(uint32_t frequencyHz) {
86 TCC2->CTRLA.bit.ENABLE = 0;
90 TCC2->CTRLA.bit.SWRST = 1;
91 while (TCC2->CTRLA.bit.SWRST) {
97 NVIC_DisableIRQ(TCC2_0_IRQn);
103 uint32_t period = (
CPU_CLK + frequencyHz / 2) / frequencyHz;
106 period =
max(period, 1U);
110 for (prescale = TCC_CTRLA_PRESCALER_DIV1_Val;
111 prescale < TCC_CTRLA_PRESCALER_DIV16_Val && (period - 1) > UINT16_MAX;
113 period = period >> 1;
117 for (; prescale < TCC_CTRLA_PRESCALER_DIV1024_Val && (period - 1) > UINT16_MAX;
119 period = period >> 2;
123 if (period > UINT16_MAX) {
124 TCC2->PER.reg = UINT16_MAX;
127 TCC2->PER.reg = period - 1;
129 TCC2->CTRLA.bit.PRESCALER = prescale;
132 TCC2->INTENSET.bit.OVF = 1;
134 TCC2->CTRLA.bit.ENABLE = 1;
137 NVIC_SetPriority(TCC2_0_IRQn, PERIODIC_INTERRUPT_PRIORITY);
138 NVIC_EnableIRQ(TCC2_0_IRQn);
#define CPU_CLK
Definition SysTiming.h:36
#define CLOCK_ENABLE(BUS, BIT)
Definition SysUtils.h:135
#define SYNCBUSY_WAIT(PER, BITMASK)
Definition SysUtils.h:127
#define max(a, b)
Definition SysUtils.h:180
int16_t State() override
Get LED's last sampled state.