0
\$\begingroup\$

I use an ATSAMD21 to generate a custom signal with the help of a PWM generator.

Basically, I set up TCC0 as a PWM generator, and use its overflow interrupt to set up the CC registers for the next pulse. It all works fine, the signal looks as it should look like. However, as it is important for the interrupt to occur before the shortest possible polarity change in my signal, I wanted to check the interrupt latency, to detect a possible failure case. That should be as simple as checking the COUNT register at the beginning of the interrupt, shouldn't it?

However, the COUNT register always reads as zero.

It doesn't matter whether I use REG_TCC0_COUNT or TCC0->COUNT.bit.COUNT, it always reads as 0. I can be sure that the actual value of the counter is not zero, because the timer is set to a very fast clock generator, and on the oscilloscope I can see that the typical interrupt latency is where the counter should already be around 100.

Interestingly, I can write to the COUNT register. If I do so, I can verify that the next pulse is shortened by the respective amount. And after the write, if I read it back, I read the value I just wrote.

As the same processor is used in the Arduino Zero, this issue was already asked on the Arduino forums a few years ago, but there is still no answer:

One of the proposed solutions was to wait for the SYNCBUSY to be cleared, but it didn't work, neither for the poster on the Arduino forum, nor for me.

\$\endgroup\$
1
\$\begingroup\$

There is a command in the control B register, which is called "Force COUNT read synchronization". It seems the counter cannot be read without issuing this command.

For example, this worked for me:

while (TCC0->SYNCBUSY.bit.CTRLB);
TCC0->CTRLBSET.bit.CMD = TCC_CTRLBSET_CMD_READSYNC_Val;
while (TCC0->SYNCBUSY.bit.CTRLB);
while (TCC0->SYNCBUSY.bit.COUNT);

After this, the value of the counter can be read from TCC0->COUNT.bit.COUNT. I would assume that the vaule I read was the value it had when I issued the TCC0->CTRLBSET.bit.CMD = TCC_CTRLBSET_CMD_READSYNC_Val; command.

More info can be found in this (yet another) Arduino forum topic: https://forum.arduino.cc/index.php?topic=396804.30

It seems the Arduino forums are not as well organized regarding duplicate questions as SE.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.