Skip to main content
Updated sample code, tested and working properly. The linked Arduino forum entry has been removed as it contains more examples of non-working code (and wrong assumptions) than otherwise. Added reference to the SAM D21/DA1 Family datasheet.
Source Link

There is a command in the controlControl B register of the TCC peripherals, which is called "Force COUNT read synchronization". It seems

  • READSYNC – Force a read synchronization of COUNT

The value of the counter cannot be read without issuingCOUNT register is not valid until this command is issued and fully executed.

For exampleSample code, this worked for metested on a SAMD21E17A device:

while// (TCC0->SYNCBUSY.bit.CTRLB);Step 1: Issue READYSYNC command TCC0->CTRLBSET.bit.CMDreg = TCC_CTRLBSET_CMD_READSYNC_Val;TCC_CTRLBSET_CMD_READSYNC;  // Step 2: Wait until the command is fully executed while (TCC0->SYNCBUSY.bit.CTRLB);   // or while (TCC0->SYNCBUSY.bit.COUNTreg);   // Step 3: Now we can read the value of the COUNT register int count = TCC0->COUNT.reg; 

After this, theThe resulting value of the counter can be read fromvariable TCC0->COUNT.bit.COUNTcount. I would assume that the vaule I read was corresponds to the value it hadof the counter when I issued the TCC0->CTRLBSET.bit.CMD = TCC_CTRLBSET_CMD_READSYNC_Val;READSYNC command was actually executed.

More info can be found in this (yet another) Arduino forum topic:For additional information, check chapter 31 of the https://forum.arduino.cc/index.php?topic=396804.30

It seems the Arduino forums are not as well organized regarding duplicate questions as SE.SAM D21/DA1 Family datasheet

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.

There is a command in the Control B register of the TCC peripherals,

  • READSYNC – Force a read synchronization of COUNT

The value of the COUNT register is not valid until this command is issued and fully executed.

Sample code, tested on a SAMD21E17A device:

// Step 1: Issue READYSYNC command TCC0->CTRLBSET.reg = TCC_CTRLBSET_CMD_READSYNC;  // Step 2: Wait until the command is fully executed while (TCC0->SYNCBUSY.bit.CTRLB); // or while (TCC0->SYNCBUSY.reg);   // Step 3: Now we can read the value of the COUNT register int count = TCC0->COUNT.reg; 

The resulting value of the variable count corresponds to the value of the counter when the READSYNC command was actually executed.

For additional information, check chapter 31 of the SAM D21/DA1 Family datasheet

Source Link
vsz
  • 2.6k
  • 1
  • 19
  • 35

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.