Trying to read Timer Counter

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone, Im using a ATSAML10E1A, I am using TC0 in 32-bit mode, and I would like to be able to read the COUNT register,  To do this it is necessary to issue the READSYNC command first on CTRBSET register.
But for some reason Im not being able to modify the last 3 bits of this register.
So I cannot issue the READSYNC command, and the COUNT register never update.
I have tried several things, the first one I have tried is:

 

uint32_t time;

hri_tc_set_CTRLB_CMD_bf(TIMER_0.device.hw,TC_CTRLBSET_CMD_READSYNC_Val);
time = ((Tc *)TIMER_0.device.hw)->COUNT32.COUNT.reg;

 

As I said, this did not work, using the debugguer I managed to discover that the CTRBSET register was not changing, later, I tried this instead:

 

char *dir=(0x42001005);    //CTRBSET register direction

*dir=(1<<7);

hri_tc_wait_for_sync(TIMER_0.device.hw,  TC_SYNCBUSY_CTRLB|TC_SYNCBUSY_COUNT);
time = ((Tc *)TIMER_0.device.hw)->COUNT32.COUNT.reg;

 

This didn't work either (the bits in CTRBSET.CMD bits didn't change)

Finally I just tried to change de CTRBSET register to see if I was able to change it.
Using:

 

*dir=1;

worked and indeed change the first bit

*dir=1<<1;

This also worked, the second bit change, this worked for the third and sixth bit, but it didn't worked for the seventh nor eighth, furthermore I tried:

*dir = 0xff;

In a attempt to change all bits in CTRBSET register, but this just changed the first 3 bits, the latest 3 bits didn't change

Does anybody have any idea why this is happening and how I can set the CTRBSET.CMD in order to update the COUNT register?
 

Last Edited: Wed. Apr 27, 2022 - 05:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't think you can expect to notice the change in CTRBSET if you use a debugger because the command is reset when done:

The commands are executed on the next prescaled GCLK_TC clock cycle. When a command has been executed, the CMD bit group will be read back as zero.

Maybe the TC is not running.

/Lars 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the quick reply, I know the TC is running becouse the respective interruption is getting triggered, thus, the TC is running, but I cant get the COUNT register

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried it but I don't have a SAML10. Worked fine on SAML21 which I think has the same TC.

#include <atmel_start.h>
volatile int tick = 0;
volatile uint32_t t;
int main(void)
{
    /* Initializes MCU, drivers and middleware */
    atmel_start_init();

    /* Replace with your application code */
    while (1) {
        hri_tc_set_CTRLB_CMD_bf(TC0,TC_CTRLBSET_CMD_READSYNC_Val);
        hri_tc_wait_for_sync(TC0,  TC_SYNCBUSY_CTRLB|TC_SYNCBUSY_COUNT);
        // char *dir=(0x42002005);    //CTRBSET register direction
        //*dir=(1<<7);
        t = TC0->COUNT32.COUNT.reg;
        tick++;
    }
}

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not sure what was different from what I was trying but this did the trick!!
Thank you so much!!!