SAMC21 - TCC Match Interrupt Help Needed

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

Hi,

 

after learning how to properly use the multiplexer I have now encountered another problem, that I am struggling to solve on my own.

 

This time I would like to simply generate interrupts, when the TCC counter matches a compare value. Upon interrupt an LED is supposed to be toggled (approx every ~1sec). To verify that the code is running another LED is toggled in the while loop.

 

#include <samc21e17a.h>


void setup_tcc() {

	REG_MCLK_APBCMASK |= MCLK_APBCMASK_TCC0	                /* TCC0 APBC Mask Clock Enable */
			  |  MCLK_APBCMASK_TCC1;		/* TCC1 APBC Mask Clock Enable */

	REG_GCLK_PCHCTRL28 = GCLK_PCHCTRL_GEN_GCLK0		/* Generator Selection: 0 */
			   | GCLK_PCHCTRL_CHEN;			/* Channel Enable */

	REG_TCC0_CTRLA = TCC_CTRLA_SWRST;			/* Software Reset */
	while (REG_TCC0_SYNCBUSY & TCC_SYNCBUSY_SWRST) {};

	REG_TCC0_COUNT = 0;					/* Counter Value */
	while (REG_TCC0_SYNCBUSY & TCC_SYNCBUSY_COUNT) {};

	REG_TCC0_CC1 = 4000000;					/* Channel 0 Compare/Capture Value */
	while (REG_TCC0_SYNCBUSY & TCC_SYNCBUSY_CC1) {};

	REG_TCC0_INTENSET = TCC_INTENSET_MC1;			/* Match or Capture Channel 1 Interrupt Enable */
	NVIC_EnableIRQ(TCC0_IRQn);				/* Interrupt Enable */

	REG_TCC0_CTRLA = TCC_CTRLA_ENABLE;			/* Enable */
	while (REG_TCC0_SYNCBUSY & TCC_SYNCBUSY_ENABLE) {};

}


void TCC0_Handler() {

	REG_PORT_OUTTGL0 = PORT_PA09;
	REG_TCC0_INTFLAG = TCC_INTFLAG_MC1;			/* Clear Interrupt Flag */
}


int main() {

	setup_tcc();

	REG_PORT_DIRSET0 = PORT_PA09;
	REG_PORT_OUTSET0 = PORT_PA09;

	REG_PORT_DIRSET0 = PORT_PA18;
	REG_PORT_OUTSET0 = PORT_PA18;

	uint32_t ind = 0;
	while (1) {
		if (ind == 100000) {
			REG_PORT_OUTTGL0 = PORT_PA18;
			ind = 0;
		}
		ind++;
	}
}

 

What happens is that the while-loop-LED is toggled a couple of times at first and then stops toggling. I assume that this is caused by the interrupt not being handled properly and stopping the whole code? The interrupt-LED never toggles, so I assume that the Interrupt Handler is not entered correctly?

 

As always many thanks for your help!

 

Cheers,

JumpingJack

This topic has a solution.
Last Edited: Sat. Jul 23, 2022 - 12:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

JumpingJack wrote:
Upon interrupt an LED is supposed to be toggled (approx every ~1sec)

It will be about once every 4.2 seconds, the timer keeps counting and the compare will happen once every period (it's 24 bits so 16777216 counts).

 

Works as expected when I try it (with a SAMC21J18) so can't really help with the actual problem.

/Lars

 

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

Lajon wrote:

Works as expected when I try it

 

Mhh, sounds like the problem might lie somewhere in my eclipse configuration. Maybe some interrupt vector header files are missing...

 

Thank you for testing this code out anyways, this will help me find the problem faster!

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have somehow solved this problem. Although I do not understand the solution, here it is:

 

Make sure to add the chip specific start up file, which in my case is "startup_samc21.c", to the path "system/src/" of your eclipse project. And don't use the argument "-nostartfiles" from your linker settings.

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

The startup_samc21.c contained the interrupt vector for all the supported hardware peripherals on the microcontroller. It maps which address to jump when an interrupt signal occurs from a specified peripheral.

 

You didn't include the file before which explained the problem where the TCC interrupt handler does not get called properly. 

I am a two-star programmer, but I prefer to be a one-star programmer. C what I did there?