Atmel START / ASF4 example with TC in 32 bit mode?

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

Lars kindly helped me get my TC counter counting (see https://community.atmel.com/foru...), but I realized that TC0 is only reporting 16 bit values, even though I've specified 32 bit mode in the Atmel START configuration.

 

I can verify that TC0.CTRLA == 0x0000000A  (Mode == 2, Enable = True).  But when I examine TC0.COUNT using the debugger in COUNT32 mode, only the low-order 16 bits count up before rolling over -- the high-order 16 bits are always zero.  My test program is simple:

#include <atmel_start.h>
#include <stdint.h>
#include <stdio.h>

uint32_t read_tc0() {
	hri_tc_write_CTRLB_CMD_bf(TC0, TC_CTRLBSET_CMD_READSYNC_Val);
	return hri_tccount32_read_COUNT_reg(TC0);
}

int main(void) {
	uint32_t c0 = 0xffffffff;
	atmel_start_init();

	while (1) {
		uint32_t c1 = read_tc0();
		if (c0 != c1) {
			printf("c0=%lx, c1=%lx\r\n", c0, c1);
			c0 = c1;
		}
	}
}

... but the results look like this -- you can see it wrapping around after it gets to 0xffff rather than 0xffffffff as desired:

...
c0=f82e, c1=fab4
c0=fab4, c1=fd3b
c0=fd3b, c1=ffc2
c0=ffc2, c1=248
c0=248, c1=4d0
c0=4d0, c1=733
c0=733, c1=972
...

So: can anyone point me to an example of an Atmel START / ASF4 project that uses TC0 in 32 bit mode, ideally for the SAME54?  Or tell me what I'm probably missing?

 

Thanks in advance...

 

This topic has a solution.

- rdp

 

Last Edited: Fri. May 24, 2019 - 11:43 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Following a pattern found in http://microchip.force.com/microchipknowledge/articles/en_US/FAQ/SAM-D-L-C---TC-Capture-Example-using-Atmel-START, I found something that works -- see below.  

 

But this begs the question: if I ask Atmel START to configure a register in 32bit mode, shouldn't it generate code that initializes TC1 as well as TC0?  For maintainability, I avoid modifying code generated by Atmel START (except for the main() function, of course...)

 

Anyway, I added two lines to TIMER_0_CLOCK_init(), defined in driver_init.c as shown below.  Now, TC0 in 32 bit mode uses all 32 bits.

 

If there's a way to coax Atmel START into generating the proper code, I'd very much like to know.

 

void TIMER_0_CLOCK_init(void)
{
	hri_mclk_set_APBAMASK_TC0_bit(MCLK);
	hri_mclk_set_APBAMASK_TC1_bit(MCLK);  // added
	hri_gclk_write_PCHCTRL_reg(GCLK,
                                   TC0_GCLK_ID,
	                           CONF_GCLK_TC0_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
	hri_gclk_write_PCHCTRL_reg(GCLK,      // added
                                   TC1_GCLK_ID,
                                   CONF_GCLK_TC0_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
}

 

- rdp

 

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

Thank you very much fearless_fool. Based on your explanation, I also found the appropriate place in the data sheet. I am only annoyed that the ASF4 configurator does not do this automatically. Thanks to your hotfix, everything works fine.

ATSAMC21
ATSAME51
ATMEGA