Setting up Interrupts, IRQ Handlers & Dummy Handlers

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

Geno.Schilling
Posts: 12
Joined: Tue Dec 16, 2014 10:24 pm

Setting up Interrupts, IRQ Handlers & Dummy Handlers

Wed Jan 21, 2015 9:33 pm

I am using the 144-pin version of the Atmel ATSAM4E16E micro, the ATMEL Studio 6.2 - SP1 with Atmel ASF version 3.10.1 and the Xplained Pro development board. I’ve used ASF wizard to generate some I2C (TWI) sample code, which I then used as a starting point for my I2C Slave driver. Everything works great in a pulling mode, but I cannot get it working with interrupts. I’ve read every single ASF article and sample code pertaining to I2C/TWI, I’ve searched the internet and posted various questions on Atmel forums, all to no avail.

My problem isn’t with I2C interrupts, I can’t get ANY interrupts working on the ATMEL micro. I didn’t have this problem with the AVRs. I have set up an interrupt handler for I2C that will turn on an LED (the LED code has been verified). But the handler NEVER gets triggered. When polling, I’ve verified that the slave gets the correct data, the correct flags are set in the I2C Status Register (TWI_SR) and I’ve watched the clock and data lines with an oscilloscope and everything is perfect according to the flow charts in the Atmel SAM4Exxx datasheet.

Here’s some sample polling ”pseudo” code. For simplicity, I’ve removed some of the less important lines of code and it might not match the ‘C’ syntax perfectly, but that doesn’t mean I have a type-O:

#include <asf.h>
#include <sam4e.h>

int main (void)
{
board_init();

twi_slave_setup(TWI0,I2C_ADDRESS);

while(1)
{
twi_slave_read(TWI0, myData);
}

return 0;
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Here is a simplified sample of my interrupt code. I NEVER get into the TWI0_Handler function. And if I were to get in, how do I determine which I2C interrupt triggered the handler? Is there data in the RHR buffer? Was it a transmission complete? Was there a NACK? Was there an overrun? All of these interrupts and more are encapsulated in this TWI0 Handler (14 total). I need to be running through the main loop doing various items and when a message comes in asynchronously from the Master I2C unit to me, the slave, and my address matches, I would like an interrupt to trigger and the execution of the code to jump to a function which I create (such as TWI0_Handler). I didn’t think this was a very unusual request for an embedded micro. I’ve done this many times on AVRs, so I’m not sure why it’s so different with the Atmel SAM/ARM.


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include <asf.h>
#include <sam4e.h>
#include <interrupt.h>

int main (void)
{
board_init();

twi_slave_setup(TWI0, I2C_ADDRESS);
twi_set_speed(TWI0, I2C_DESIRED_SPEED, sysclk_get_peripheral_hz());

REG_TWI0_IER = TWI_IER_TXCOMP | TWI_IER_RXRDY | TWI_IER_TXRDY | \
TWI_IER_SVACC | TWI_IER_GACC | TWI_IER_OVRE | \
TWI_IER_NACK | TWI_IER_ARBLST | TWI_IER_SCL_WS | \
TWI_IER_EOSACC | TWI_IER_ENDRX | TWI_IER_ENDTX | \
TWI_IER_RXBUFF | TWI_IER_TXBUFE;

irq_initialize_vectors();
irq_register_handler(TWI0_IRQn, 5);

// Ideally the contents of this function will be in the TWI0 Handler.
// twi_slave_read(TWI0, myData);


return 0;
}

void TWI0_Handler(void)
{
// uint32_t myIntMask1 = NVIC_GetActive(TWI0_IRQn); // This function determines WHICH INTERRUPT TRIGGERED THIS?!?
// uint32_t myIntMask2 = NVIC_GetPendingIRQ(TWI0_IRQn
); // This function determines WHICH INTERRUPT TRIGGERED THIS?!?

NVIC_ClearPendingIRQ(TWI0_IRQn); // I believe I need to clear the pending interrupts once inside the interrupt

// Do Something like turn on an LED to indicate that you’ve got into an interrupt.

ioport_set_pin_level(LED_0_PIN, LED_0_ACTIVE);
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Geno.Schilling
Posts: 12
Joined: Tue Dec 16, 2014 10:24 pm

Re: Setting up Interrupts, IRQ Handlers & Dummy Handlers

Wed Jan 21, 2015 11:14 pm

It's not a duplicate thread! I had posted a similar question earlier, but I didn't get an answer. So I thought that I needed to be more specific and more detailed about the question so people could understand what I was asking. All the responses I got were "Use ASF Wizard" or "Look at ASF Wizard" and that's great for I2C polling, but if I don't plan on having an I2C read function in a while(1) loop then ASF Wizard really isn't any help. It says nothing about using Interrupts for I2C or how to use the TWI0_Handler(). Or TWI1 if you're using the other I2C. So I guess after a second thread (third actually), I'm still no farther then I was last week.
jharley
Posts: 238
Joined: Thu Dec 06, 2012 6:40 am

Re: Setting up Interrupts, IRQ Handlers & Dummy Handlers

Thu Jan 22, 2015 5:04 am

I would start here....
Geno.Schilling wrote:... I can’t get ANY interrupts working on the ATMEL micro.
I haven't used the AVR micros but i assume (possibly incorrectly) that they have a global interrupt mask. Have you verified that the ATMEL micro has interrupts enabled?
Geno.Schilling
Posts: 12
Joined: Tue Dec 16, 2014 10:24 pm

Re: Setting up Interrupts, IRQ Handlers & Dummy Handlers

Thu Jan 22, 2015 6:34 pm

Yes, I did verify that the ATMEL has global interrupts enabled. I used the function cpu_irq_enable() to enable global interrupts and I used cpu_irq_is_enabled() to verify that the global interrupts were indeed enabled. For the particular TWI/I2C interrupts, I used twi_enable_interrupt(TWI0, TWI_IER_SVACC) to enable the desired interrupts. And I read the Interrupt Mask Register, TWI_IMR, to verify that the particular interrupt I wanted was enabled. I also call irq_initialize_vectors() and irq_register_handler(TWI0_IRQn, 1) to set up the interrupt vector table. I was hoping that when TWI_IER_SVACC or whatever interrupt I had enabled was triggered, the handler TWI0_Handler() would be triggered and the instruction pointer would jump to the code in that function. Though I still have had NO luck getting ANY interrupt to trigger.
jharley
Posts: 238
Joined: Thu Dec 06, 2012 6:40 am

Re: Setting up Interrupts, IRQ Handlers & Dummy Handlers

Thu Jan 22, 2015 11:02 pm

Not sure you have found this in your internet search....
http://www.atmel.com/Images/Atmel-42273 ... T07335.pdf

It can be found on this page http://community.arm.com/community/arm- ... m4-devices

I believe you will want to use the twis_set_callback() function to register the handler

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 2 guests