STM32F4 Discovery + CANBus

Forum related to ERIKA Enterprise and RT-Druid version 2

Moderator: paolo.gai

Post Reply
smokey
Newbie
Posts: 6
Joined: Tue Sep 02, 2014 8:34 am

STM32F4 Discovery + CANBus

Post by smokey » Tue Sep 02, 2014 10:25 am

Hi,

I want to Programm a Can communication with Erika on a STM32F4 Discoveryboard.
Are there any examples i can use for a easier start, because I´m not used with ERIKA?

I hope someone can help me.

greets

paolo.gai
Administrator
Posts: 875
Joined: Thu Dec 07, 2006 12:11 pm

Re: STM32F4 Discovery + CANBus

Post by paolo.gai » Tue Sep 02, 2014 1:11 pm

I suggest to start with the virtual machine available on

http://www.erika-enterprise.com

There is a video as well as a PDF.

There are plenty of examples also for the STM32F4Discovery! (although I think no CAN example).

If you do some, please share...

are you using an already made hardware skin/daughter board with the CAN transceiver?

Ciao,

PJ

smokey
Newbie
Posts: 6
Joined: Tue Sep 02, 2014 8:34 am

Re: STM32F4 Discovery + CANBus

Post by smokey » Tue Sep 02, 2014 2:33 pm

Thanks for the link.

Eclipse and some examples are already running on my system.

I think i´ve already have a runnin init for a CAN-Bus. Have to check it whit a oszilloscope to make sure it is really running :)
If this is working i will post it.

I made my own daugther board with the transceiver and some termination resistors.

greets

paolo.gai
Administrator
Posts: 875
Joined: Thu Dec 07, 2006 12:11 pm

Re: STM32F4 Discovery + CANBus

Post by paolo.gai » Wed Sep 03, 2014 8:56 am

Thanks for sharing your code when it will be ready... let us know if you have problems.

Ciao,

Paolo

smokey
Newbie
Posts: 6
Joined: Tue Sep 02, 2014 8:34 am

Re: STM32F4 Discovery + CANBus

Post by smokey » Tue Sep 09, 2014 1:11 pm

Here is my promised Code for CAN.
I tested it with two Boards and they can send and receive a message.

First the Init:

Code: Select all

 prvCANInit( void )
{
   GPIO_InitTypeDef 		GPIO_InitStructure;
	CAN_InitTypeDef 		CAN_InitStructure;
	CAN_FilterInitTypeDef  	CAN_FilterInitStructure;
	NVIC_InitTypeDef 		NVIC_InitStructure;


	/* Enable GPIO clock */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

	/* Connect CAN 1 pins to AF9 */
	GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1);
	GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1);

	/* Configure CAN 1 RX and TX pins */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
	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(GPIOD, &GPIO_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] = 5;
	}
The receive Task:

Code: Select all

TASK(TaskReceive)
{
	CAN_Receive( CAN1, CAN_FIFO0, &RxMessage );

	if( RxMessage.Data[0] == 5 )
		STM_EVAL_LEDToggle( LED3 ); //Orange LED if message okay
	else
		STM_EVAL_LEDToggle( LED5 ); //Red LED if message not okay
}
The send Task:

Code: Select all

TASK(TaskSend)
{
	CAN_Transmit( CAN1, &canMessage );

	if( CAN_TransmitStatus( CAN1, 0 ))
		STM_EVAL_LEDToggle( LED4 ); //Green LED when message was send
}
you also need two variables called:
CanTxMsg canMessage;
CanRxMsg RxMessage;

Now i´m trying to recieve the messages via an interrupt.
My problem is, when I init the Interrupts my board doesn´t run anymore.

Do i have to clear the pending interrupts bits or need something other routines?

greets

paolo.gai
Administrator
Posts: 875
Joined: Thu Dec 07, 2006 12:11 pm

Re: STM32F4 Discovery + CANBus

Post by paolo.gai » Thu Sep 11, 2014 1:00 pm

You probably have to define the CAN interrupt handler, as well as handle the CAN message handling inside the interrupt handler.

When it halts, try to understand in which routine it stops. probably it is in some interrupt handler...

Paolo

smokey
Newbie
Posts: 6
Joined: Tue Sep 02, 2014 8:34 am

Re: STM32F4 Discovery + CANBus

Post by smokey » Fri Sep 12, 2014 12:34 pm

These are the entries in the conf.oil files for the interrupt.

Code: Select all

	ISR systick_handler {
		CATEGORY = 2;
		ENTRY = "SYSTICK";
		PRIORITY = 2;
	};
	
		
	ISR can_handler {
		CATEGORY = 2;
		ENTRY = "CAN1_RX0";
		PRIORITY = 1;
	};
And this is my can_handler in the main file. LED5 should light if the handler is called.

Code: Select all

ISR2( can_handler)
{
	STM_EVAL_LEDToggle( LED5 );
}
My problem is, the programm is running as long there is no can mesage incoming. When a message is incoming the system halts.
The system stops somewhere between "Can message incoming" and "Execute Interrupt handle" because LED5 never get turn on

paolo.gai
Administrator
Posts: 875
Joined: Thu Dec 07, 2006 12:11 pm

Re: STM32F4 Discovery + CANBus

Post by paolo.gai » Fri Sep 12, 2014 1:55 pm

I suggest to open the application and see where the system stops. It may be at a hard fault handler, or at other locations. This could help your debug.

Paolo

smokey
Newbie
Posts: 6
Joined: Tue Sep 02, 2014 8:34 am

Re: STM32F4 Discovery + CANBus

Post by smokey » Thu Oct 02, 2014 1:21 pm

Thanks for the reply.
My system is running so far.

But know i have another problem.
Is it possible that a Interrupt doesn´t interrupt a running Task?
I´m asking because if the board is calculating something, now CAN message can be recieved.

To test this I programmed a litte Taks which is preriodically called and is calculation something. In the time, the Task is running, i cannot recieve any CAN Messages. When the Taks is suspended the communication is working.

Maybe someone knows this problem and could help me.
Thanks

paolo.gai
Administrator
Posts: 875
Joined: Thu Dec 07, 2006 12:11 pm

Re: STM32F4 Discovery + CANBus

Post by paolo.gai » Fri Oct 03, 2014 8:48 am

I expect that interrupts will be able to interrupt tasks.

But if teh can message is afterwards handled by another low priority task, then it may happen that the CAN computation is blocked by the fact that the CAN task does not execute.

Also check whether you disabled IRQs in the computational task.

Ciao,

Paolo

smokey
Newbie
Posts: 6
Joined: Tue Sep 02, 2014 8:34 am

Re: STM32F4 Discovery + CANBus

Post by smokey » Thu Oct 09, 2014 2:45 pm

Thanks for your reply.

Okay it seems that the interrupt interrupts the tasks. ;)

But now i have another problem.
I programmed a Queue which is declared as a global structure which two routines. One for writing and one for reading.
If a interrup occures the routine should write the recieved Can message into the queue and activate a task "send".
The activatet task reads the message and sends it back.

It runs well as long there are no other tasks running.
When i add more tasks (with higher priority than the "send" task) nothing is written into the queue.

do i have to "allow" the interrupt to write onto the queue or the lower priority task to read from the queue?

paolo.gai
Administrator
Posts: 875
Joined: Thu Dec 07, 2006 12:11 pm

Re: STM32F4 Discovery + CANBus

Post by paolo.gai » Tue Oct 14, 2014 11:33 am

I guess everything depends on what the high priority tasks do. If they execute forever then the send task will be never executed.

PJ

eddy_k87
Newbie
Posts: 2
Joined: Fri Dec 18, 2015 3:31 pm

Re: STM32F4 Discovery + CANBus

Post by eddy_k87 » Fri Dec 18, 2015 3:54 pm

Hi,

I'm trying to get the CAN-Bus working, but it never triggers. (It only works by polling)
I used the oil/c code from smokey, but still...

1. What is necessary to initialize an ISR(2)??
2. should I initialize the NVIC or does this the ERIKA OS (OIL)

Code: Select all

NVIC_InitTypeDef NVIC_InitStructure;

	NVIC_InitStructure.NVIC_IRQChannel=CAN1_RX0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
thanks and best regards!

eddy_k87
Newbie
Posts: 2
Joined: Fri Dec 18, 2015 3:31 pm

Re: STM32F4 Discovery + CANBus

Post by eddy_k87 » Fri Dec 18, 2015 11:03 pm

...it is working when SYSTICK ISR is deactivated!

Post Reply