3 small questions regarding Sam4n

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

Physikant
Posts: 3
Joined: Sat Oct 11, 2014 1:31 am

3 small questions regarding Sam4n

Thu Nov 06, 2014 1:43 pm

Hello,

I have 3 basic questions regarding interrupts and the RTC. I'm programming in SAM-GCC and don't want to use ASF.

1) After activating and triggering an interrupt, how can I find out in the handler which interrupt was triggered? The NVIC handlers are just for a device (I want to use TC0 and UART/USART), but how can I tell which special interrupt of the used device was triggered? Is there a register where a flag is set? I can't find it in the DS.

2) Related to the first question: I tried to use the compare match interrupt of the TC0. What happens is that the interrupt is always triggered and the SAM isn't leaving the interrupt handler anymore, since the flag is always set. Here is the (absolutely basic) code snippet for that:

Code: Select all

//configure PA0 as TC0-Output
PIOA->PIO_PDR = PIO_PDR_P0; 
PIOA->PIO_PUDR = PIO_PUDR_P0; 
PIOA->PIO_PPDDR = PIO_PPDDR_P0; 
PIOA->PIO_MDDR = PIO_MDDR_P0; 
PIOA->PIO_ABCDSR[0] = PIO_ABCDSR_P0; 
//done

PMC->PMC_PCER0 = PMC_PCER0_PID23; //activate TC0
REG_TC0_CCR0 =  TC_CCR_CLKEN     //enable the clock
	|TC_CCR_SWTRG   //software trigger: perform restart
	;
REG_TC0_CMR0 =  TC_CMR_WAVE  //activate waveform mode
	|TC_CMR_TCCLKS_TIMER_CLOCK4 //use MCK/128
	|TC_CMR_WAVSEL_UP_RC	 //count up to RC and reset
	|TC_CMR_ACPA_SET	  //set output pin with compare A
	|TC_CMR_ACPC_CLEAR	  //clear output pin with compare C
	;
REG_TC0_RA0 = 1024; //compare A: 1024
REG_TC0_RC0 = 4096; //compare C: 4096

REG_TC0_IER0 =  TC_IER_CPAS;   //enable RA Compare Interrupt

NVIC_EnableIRQ(23); //enable NVIC for TC0

void TC0_Handler( void )
{
	NVIC_ClearPendingIRQ(23); //that was just a test
	toggle_led();
}
The SAM4n8 is running at 100MHz MCK. Checking PA0 with an oscilloscope shows the chosen waveform, so everything ok here. Checking the LED with an oscilloscope shows that the LED is toggled every ~1µs, so as fast as it gets. Why is the interrupt never cleared?

3) Is it possible to run the RTC from master clock/ main clock / SysTick?

Sorry for these being pretty basic, but the documentation for this stuff is pretty poor in my opinion and it's hard to find examples on the internet :(
Thanks in advance!
Best regards,
Niko
Physikant
Posts: 3
Joined: Sat Oct 11, 2014 1:31 am

Re: 3 small questions regarding Sam4n

Sat Nov 29, 2014 4:45 pm

I was just wondering why I've got no answer yet.
Especially the first question should be a simple and pretty common information? I thought using interrupts is basic stuff...
The third question is somehow solved since I'm pretty sure now that using the mck/master clock as a source for the internal RTC is impossible.

I hope someone finds a few seconds two give me at least an answer to the first question :(

Best regards,
Nikolas
jarnoldbrown
Posts: 36
Joined: Thu Aug 22, 2013 5:31 pm

Re: 3 small questions regarding Sam4n

Mon Dec 01, 2014 2:43 pm

"1) After activating and triggering an interrupt, how can I find out in the handler which interrupt was triggered? The NVIC handlers are just for a device (I want to use TC0 and UART/USART), but how can I tell which special interrupt of the used device was triggered? Is there a register where a flag is set? I can't find it in the DS."

Yes, any device that can cause interrupts for different events will have an interrupt status register that will tell you which event(s) caused the interrrupt.
Have you written the appropriate interrupt handler routine for the peripheral generating the interrupt? If so, why not post it.

[Edit] Sorry, just re-read your post. I don't think you need to clear anything in the NVIC(I certainly don't in my SAM4S project). You do, however, need to read that datasheet to see what operation will clear the interrupt flag in the timer/counter.

[Edit again] After reading the datasheet, I agree that there is no specifc mention of clearing the interrupt. My guess, however, is that reading the status register clears all the bits in the status register, and thus clears the interrupt.
bptech
Posts: 25
Joined: Fri Jul 18, 2014 3:08 pm

Re: 3 small questions regarding Sam4n

Mon Dec 01, 2014 4:42 pm

Seems to be a recurring theme - "I don't want to use ASF but <complex inner detail question about something easily solved by using ASF>. If you don't want to use ASF that is fine, a little hard to understand why but you can do it. However most of these questions are answered in the ASF itself. You can do a free download of AtmelStudio, open a sample program for your chip, and look at the component files for example src\asf\SAM\utils\cmsis\sam4s\include\<name of your chip>.h Every port, peripheral location, all the base addresses, everything you need to access the hardware at the raw level is there customized for your chip. There are weak prototype interrupt handlers that you can override to make your custom interrupt handler. The other files in the cmsis directory provide interfaces to all the features of the chip.

It is hard to imagine how much work it would entail to rewrite all that and make it work just from the data sheet. I don't know why anyone would try. If you are using IAR or raw GCC or Linux you can still use the ASF headers and drivers, AtmeStudio just makes it easier. I like AtmelStudio (though I can sympathize with those who hate it).

In AtmelStudio the work flow is: find a sample program close to what you are trying to do. If you already have a solution of your own partly done you can add it to your solution by right-clicking on the Solution up at the top of the Solution Explorer and click on add, then select Example Project. Be sure to right-click on your project and select Set as Startup Project after this so your next compile is still your project and not the new example. Now you can explore the example, copy and paste parts of it as needed into your project, explore how it works using right-click and Goto Implementation on variables and macros, or Refactor- Find References. You can walk all the way back into the hardware register level by clicking on Goto Implementation like a three-year-old repeating "but why Daddy?" over and over.

Reading the comments and online documentation may not be too helpful, but following the actual implementation in an actual real world example is.
pruby
Posts: 1
Joined: Tue Dec 02, 2014 10:10 am

Re: 3 small questions regarding Sam4n

Tue Dec 02, 2014 10:24 am

Hey, don't have a useful answer but in a similar boat here. I work nearly exclusively in Ubuntu, have a Windows box but avoid running it, as it's horribly out of date. Unfortunately, there's next to no support from Atmel for non-Windows development. Installed Atmel Studio, but don't want to use day to day.

I found an open source IDE, Em::Blocks , which seems to have all the right headers. I've also been extracting the device headers (which have an open source licence) from Atmel Studio in to a repository, with a simple Makefile to string it together. Can compile code examples, not up to flashing yet. Would be helpful if you can cross-compare what I've got so far:

http://goddard.net.nz/cgi-bin/gitweb.cg ... ;a=summary
git clone git://goddard.net.nz/arm-base.git
jharley
Posts: 238
Joined: Thu Dec 06, 2012 6:40 am

Re: 3 small questions regarding Sam4n

Mon Dec 15, 2014 3:07 pm

My, possibly faulty, assumption is that the majority are students that are doing homework and have been told not to use ASF. :lol:
jharley
Posts: 238
Joined: Thu Dec 06, 2012 6:40 am

Re: 3 small questions regarding Sam4n

Fri Jul 17, 2015 3:41 pm

Perhaps this is from a "not invented here" mentality or a deep desire to understand the inner workings of the processor, or as some have said ... the code is difficult to follow.
I agree with bptech's response above, no one says you have to use ASF but it does hold answers to the majority of the posters questions, and yes it will take some work to sort through it.

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 2 guests