STM32 CAN RX Interrupt

Forum related to ERIKA Enterprise and RT-Druid version 2

Moderator: paolo.gai

Locked
who

STM32 CAN RX Interrupt

Post by who »

Hi,
I am trying to get the CAN interface running on my STM32F407 MCU. I found http://www.erika-enterprise.com/forum/v ... f=4&t=1035thread and got CAN with polling to work. Now I want to use the CAN RX interrupt. But how to do this?

Method 1

Code: Select all

ISR CAN1_RX0_IRQHandler {
      CATEGORY = 1;
      ENTRY = "CAN1_RX0";
      PRIORITY = 1;
   };

Code: Select all

ISR1( CAN1_RX0_IRQHandler ) {
	CanRxMsg RxMessage;
	CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
	send_char(RxMessage.Data[0]);
	CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);
}

int main(void) {
	SystemInit();
	EE_system_init();
	EE_systick_set_period(MILLISECONDS_TO_TICKS(1, SystemCoreClock));
	EE_systick_enable_int();
	EE_systick_start();

	gpio_init();
	can_init();
	usart_init();

	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
	NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0xf;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	while (1) {
	}
}

void can_init(void) {
	GPIO_InitTypeDef GPIO_InitStructure;
	CAN_InitTypeDef CAN_InitStructure;
	CAN_FilterInitTypeDef CAN_FilterInitStructure;

	/* Enable GPIO clock */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH | RCC_AHB1Periph_GPIOI, ENABLE);

	/* Connect CAN pins */
	GPIO_PinAFConfig(GPIOH, GPIO_PinSource13, GPIO_AF_CAN1);
	GPIO_PinAFConfig(GPIOI, GPIO_PinSource9, GPIO_AF_CAN1);

	/* Configure CAN RX and TX pins */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOH, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_Init(GPIOI, &GPIO_InitStructure);

	/* CAN register init */
	CAN_DeInit(CAN1);
	CAN_StructInit(&CAN_InitStructure);

	/* CAN 1 cell init */
	CAN_InitStructure.CAN_Prescaler = 64;
	CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
	CAN_InitStructure.CAN_SJW = CAN_SJW_2tq;
	CAN_InitStructure.CAN_BS1 = CAN_BS1_11tq;
	CAN_InitStructure.CAN_BS2 = CAN_BS2_4tq;
	CAN_InitStructure.CAN_TTCM = DISABLE;
	CAN_InitStructure.CAN_ABOM = DISABLE;
	CAN_InitStructure.CAN_AWUM = DISABLE;
	CAN_InitStructure.CAN_NART = ENABLE;
	CAN_InitStructure.CAN_RFLM = DISABLE;
	CAN_InitStructure.CAN_TXFP = DISABLE;
	CAN_Init(CAN1, &CAN_InitStructure);

	/* CAN1 filter init, accept every message */
	CAN_FilterInitStructure.CAN_FilterNumber = 0; // 0..13 for CAN1, 14..27 for CAN2
	CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
	CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
	CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
	CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
	CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
	CAN_FilterInit(&CAN_FilterInitStructure);

	/* Message Init */
	canMessage.StdId = 0x123;
	canMessage.ExtId = 0;
	canMessage.RTR = CAN_RTR_DATA;
	canMessage.IDE = CAN_ID_STD;
	canMessage.DLC = 1;

	canMessage.Data[0] = 0x78;

	CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
When using ERIKA, this is not working. However, when doing this (adding NVID CAN RX Interrupt and writing IrqHandler) it works on the bare metal board.

Method 2

Code: Select all

ISR can_handler{
      CATEGORY = 2;
      ENTRY = "CAN1_RX0";
      PRIORITY = 1;
   };

Code: Select all

ISR2( can_handler) {
	CanRxMsg RxMessage;
	CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
	send_char(RxMessage.Data[0]);
	CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);
}
//main and can init same as above, but without NVIC configuration
This is also not working. When using a debugger, the ISR never gets called.

Which method should be used with ERIKA and how to get this to run? As mentioned, when using polling I am receiving the messages and I see the can messages on the RX pin with a oscilloscope.

Kind regards
who

Re: STM32 CAN RX Interrupt

Post by who »

Problem solved, had a hard fault error caused by the tick interrupt
Locked