cancel
Showing results for 
Search instead for 
Did you mean: 

Interrupts unexpectedly fired!

ashley23
Associate II
Posted on September 16, 2010 at 07:16

Interrupts unexpectedly fired!

2 REPLIES 2
Posted on May 17, 2011 at 14:07

I don't like the order you initialize things, the clocks to the peripherals, including GPIOD, need to be up and running before you prod the hardware. It is after all synchronous.

uint8_t CAN1_Init(void)

{

    uint8_t retVal = CANINITFAILED;

// Initialize clocks into hardware BEFORE configuring them!

   

    // Enable CAN 1 Clock

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

    // Enable AFIO and GPIOD clock

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD, ENABLE);

    CAN_DeInit(CAN1);

 

    GPIO_InitTypeDef GPIO_InitStructure;

    // Configure CAN Tx (PD.01) as push-pull

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    // Configure CAN Rx (PD.00) as input floating

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    // Remap CAN1 to PD0 and PD1

    AFIO->MAPR &= ~(AFIO_MAPR_CAN_REMAP);   

    AFIO->MAPR |= AFIO_MAPR_CAN_REMAP_REMAP3;

 

    // Configure for 1MBit

    CAN_InitTypeDef ci;

    CAN_StructInit(&ci);

    ci.CAN_Prescaler = 4;

    ci.CAN_SJW = CAN_SJW_1tq;

    ci.CAN_BS1 = CAN_BS1_6tq;

    ci.CAN_BS2 = CAN_BS2_2tq;    

    ci.CAN_TXFP = ENABLE;

   

    retVal = CAN_Init(CAN1, &ci);

   

    if(retVal != CANINITFAILED)

    {

        NVIC_InitTypeDef NVIC_InitStructure;  

        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // 1:3

        // Tx Interrupt channel

        NVIC_InitStructure.NVIC_IRQChannel = CAN1_TX_IRQn;

        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

        NVIC_Init(&NVIC_InitStructure);

        // Peripheral tx interrupt enable

        CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);       

    }

   

    return retVal;

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ashley23
Associate II
Posted on May 17, 2011 at 14:07

Yeah, that is probably a good point.  Actually the GPIOD and AFIO clocks are already up and running from previous peripherals and I should of removed the AFIO clock init function call.  The CAN clock is enabled before any access to the CAN peripherals registers.

Anyways, that doesn't really answer my questions about the interrupts as in practice all clocks are up and running and I did retry it after moving the CAN clock init to earlier and the same thing still happens...