STM32F4 Discovery + CANBus
Moderator: paolo.gai
STM32F4 Discovery + CANBus
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
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
Re: STM32F4 Discovery + CANBus
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
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
Re: STM32F4 Discovery + CANBus
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
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
Re: STM32F4 Discovery + CANBus
Thanks for sharing your code when it will be ready... let us know if you have problems.
Ciao,
Paolo
Ciao,
Paolo
Re: STM32F4 Discovery + CANBus
Here is my promised Code for CAN.
I tested it with two Boards and they can send and receive a message.
First the Init:
The receive Task:
The send Task:
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
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;
}
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
}
Code: Select all
TASK(TaskSend)
{
CAN_Transmit( CAN1, &canMessage );
if( CAN_TransmitStatus( CAN1, 0 ))
STM_EVAL_LEDToggle( LED4 ); //Green LED when message was send
}
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
Re: STM32F4 Discovery + CANBus
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
When it halts, try to understand in which routine it stops. probably it is in some interrupt handler...
Paolo
Re: STM32F4 Discovery + CANBus
These are the entries in the conf.oil files for the interrupt.
And this is my can_handler in the main file. LED5 should light if the handler is called.
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
Code: Select all
ISR systick_handler {
CATEGORY = 2;
ENTRY = "SYSTICK";
PRIORITY = 2;
};
ISR can_handler {
CATEGORY = 2;
ENTRY = "CAN1_RX0";
PRIORITY = 1;
};
Code: Select all
ISR2( can_handler)
{
STM_EVAL_LEDToggle( LED5 );
}
The system stops somewhere between "Can message incoming" and "Execute Interrupt handle" because LED5 never get turn on
Re: STM32F4 Discovery + CANBus
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
Paolo
Re: STM32F4 Discovery + CANBus
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
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
Re: STM32F4 Discovery + CANBus
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
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
Re: STM32F4 Discovery + CANBus
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?
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?
Re: STM32F4 Discovery + CANBus
I guess everything depends on what the high priority tasks do. If they execute forever then the send task will be never executed.
PJ
PJ
Re: STM32F4 Discovery + CANBus
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)
thanks and best regards!
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);