Enable CAN interrupts on Tracker One

Hello community,

I successfully enabled CAN communication on my Tracker One SoM. Now I want an interrupt handler react to incoming CAN transmissions.

In setup I am invoking interrupts(). Is that necessary at all?

After that I am initiating CAN connection with these lines of code:

MCP_CAN canInterface(CAN_CS, SPI1);

void can_init(void){
    // Turn on CAN_5V power
    pinMode(CAN_PWR, OUTPUT);
    digitalWrite(CAN_PWR, HIGH);

    // Set STBY low to enable transmitter and high-speed receiver
    pinMode(CAN_STBY, OUTPUT);
    digitalWrite(CAN_STBY, LOW);

    // Enable the CAN interrupt pin as an input just in case
    pinMode(CAN_INT, INPUT);
    Log.info("Interrupt Attached: %d", attachInterrupt(CAN_INT, CAN_IRQHandler, CHANGE));

    // Hardware reset the CAN controller. Not really necessary, but doesn't hurt.
    pinMode(CAN_RST, OUTPUT);
    digitalWrite(CAN_RST, LOW);
    delay(100);
    digitalWrite(CAN_RST, HIGH);

    // Make sure the last parameter is MCP_20MHZ; this is dependent on the crystal
    // connected to the CAN chip and it's 20 MHz on the Tracker SoM.
    byte status = canInterface.begin(MCP_SIDL, CAN_500KBPS, MCP_20MHZ);
    if(status == CAN_OK) {
        Log.info("CAN initialization succeeded");
    }
    else {
        Log.error("CAN initialization failed %d", status);
    }

    // Change to normal mode to allow messages to be transmitted. If you don't do this,
    // the CAN chip will be in loopback mode.
    canInterface.setMode(MCP_MODE_NORMAL); 
}

and my handler looks like this:

void CAN_IRQHandler(void){
    Log.info("CAN IRQ activated");
}

Can you guys see an issue with that code or if there’s something I am doing not correctly?

Thanks in advance!

I believe the can-mcp25x library used by Tracker Edge does not enable interrupts in the CAN controller by default. You’d need to modify the CAN controller configuration registers to specify which interrupts you want and enable interrupts.

Hi rickkas7,

thanks for the quick response. Since the device is not overly used I decided to use a polling loop to detect changes on the CAN_INT pin, which works flawlessly. Right now this is sufficient for continuing with my project.

Cheers!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.