cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32f407 Can bus problem

cnrabdullah
Associate II
Posted on November 04, 2014 at 16:40

Hi all,

I am working on a project with stm32f407 and i want to communicate to stm32f407 boards with each other using can communication. I am using sn65hvd transceiver and i made this scheme : 0690X00000605Z0QAI.png i am using 2 circuit like this. Their CANH and CANL are connected other CANH and CANL. i think there is no problem with transceiver part. Probably i am doing something wrong with the code. Here is my code, can anyone tell me where am i doing wrong? I am using can1 tx pb9, and can1 rx pb8. First stm32's tx and rx go receiver and 2 transceiver's canh and canl are common; then output of other transceiver(tx and rx) go other stm32's pb8 and pb9. Here is my code ofreceiverstm32

#include ''can_control.h''
#include ''stm32f4_discovery.h''
#include <
stdint.h
>
#define CAN_CLK RCC_APB1Periph_CAN
#define CAN_RX_PIN GPIO_Pin_8
#define CAN_TX_PIN GPIO_Pin_9
#define CAN_GPIO_PORT GPIOB
#define CAN_GPIO_CLK RCC_AHBPeriph_GPIOB
#define CAN_AF_PORT GPIO_AF_4
#define CAN_RX_SOURCE GPIO_PinSource8
#define CAN_TX_SOURCE GPIO_PinSource9
CanTxMsg TxMessage;
CanRxMsg RxMessage;
static
void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN GPIOs configuration **************************************************/
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Connect CAN pins to AF7 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_CAN1);
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN;
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(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CAN_TX_PIN;
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(GPIOB, &GPIO_InitStructure);
/* NVIC configuration *******************************************************/
NVIC_InitStructure.NVIC_IRQChannel = CAN1_TX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* CAN configuration ********************************************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
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 = ENABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
/* CAN Baudrate = 500kbps (CAN clocked at 48 MHz) */
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
CAN_InitStructure.CAN_Prescaler = 16;
CAN_Init(CAN1, &CAN_InitStructure);
// 48Mhz / ( 8 * ( 1 + 6 + 5) ) = 500Kbps
// 48Mhz / 96
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber = 0;
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);
/* transmit */
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x11;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
/* Enable FIFO 0 message pending Interrupt */
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
void DelayInt(void)
{
volatile int i = 1000000;
while(i--);
}
uint8_t CanSendRes = 0;
void CanControlInit(void)
{
uint8_t TransmitMailbox = 0;
CAN_Config();
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
}

Then here is my code of transmitter Stm32

#include ''can_control.h''
#include ''stm32f4_discovery.h''
#include <
stdint.h
>
#define CAN_CLK RCC_APB1Periph_CAN
#define CAN_RX_PIN GPIO_Pin_8
#define CAN_TX_PIN GPIO_Pin_9
#define CAN_GPIO_PORT GPIOB
#define CAN_GPIO_CLK RCC_AHBPeriph_GPIOB
#define CAN_AF_PORT GPIO_AF_4
#define CAN_RX_SOURCE GPIO_PinSource8
#define CAN_TX_SOURCE GPIO_PinSource9
CanTxMsg TxMessage;
CanRxMsg RxMessage;
static
void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN GPIOs configuration **************************************************/
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Connect CAN pins to AF7 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_CAN1);
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN;
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(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CAN_TX_PIN;
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(GPIOB, &GPIO_InitStructure);
/* NVIC configuration *******************************************************/
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* CAN configuration ********************************************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
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 = ENABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
/* CAN Baudrate = 500kbps (CAN clocked at 48 MHz) */
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
CAN_InitStructure.CAN_Prescaler = 16;
CAN_Init(CAN1, &CAN_InitStructure);
// 48Mhz / ( 8 * ( 1 + 6 + 5) ) = 500Kbps
// 48Mhz / 96
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber = 0;
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);
/* transmit */
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x11;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
/* Enable FIFO 0 message pending Interrupt */
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
void DelayInt(void)
{
volatile int i = 1000000;
while(i--);
}
uint8_t CanSendRes = 0;
void CanControlInit(void)
{
uint32_t i = 0;
uint8_t TransmitMailbox = 0;
CAN_Config();
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x11;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
TxMessage.Data[0] = 0xFF;
TxMessage.Data[1] = 0xFF;
TxMessage.Data[2] = 0xFF;
TxMessage.Data[3] = 0xFF;
TxMessage.Data[4] = 0xAA;
TxMessage.Data[5] = 0xBB;
TxMessage.Data[6] = 0xCB;
TxMessage.Data[7] = 0x24;
while(1)
{
CAN_Transmit(CAN1, &TxMessage);
}
}

When i look at my pb9( tx pin) at scop, i cant see anything meaningfull, i think its just noise. i guess i'm doing something so wrong and cant see it, please help me about my problem i will be so appreciated. (Sorry about my bad english and this long writing :) ) #stm32f2 #stm32 #stm32 #stm32 #can #can #can #bxcan #bxcan
18 REPLIES 18
rm239955
Associate II
Posted on November 24, 2014 at 19:23

Bit of a mess

What is 2+14+6 ?

/* Requires a clock with integer division into APB clock */
CAN_InitStructure.CAN_SJW = CAN_SJW_2tq; // 1+6+7 = 14, 1+14+6 = 21, 1+15+5 = 21
CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = RCC_Clocks.PCLK1_Frequency / (14 * 500000); // quanta by baudrate - 500kbps

This has been broken, figure out what baudrate you're shooting for and get the time quanta straight so you can get the prescaler right
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on November 25, 2014 at 00:52

/* quanta 1+6+7 = 14, 14 * 6 = 84, 42000000 / 84 = 500000 */
/* CAN Baudrate = 500Kbps (CAN clocked at 42 MHz) Prescaler = 6 */
/* Requires a clock with integer division into APB clock */
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // 1+6+7 = 14, 1+14+6 = 21, 1+15+5 = 21
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler = RCC_Clocks.PCLK1_Frequency / (14 * 500000); // quanta by baudrate - 500kbps

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rm239955
Associate II
Posted on November 25, 2014 at 09:43

Thanks for your reply.

As specified I have modified Baud Rate to 500Kbps

But Still I am not able to see the message in PCAN.

The <problem is that the messages are not acknowleding 

Can you please tell how to solve this problem?

Posted on November 25, 2014 at 14:12

Not familiar with PCAN, do you have a receiver node on the bus to generate the acknowledge?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rm239955
Associate II
Posted on November 25, 2014 at 15:54

Ya I have the receiver node but its not acknowledging.

When I try to debug ,I can see the messages are acknowledging but these messages are not shown in PCAN

rm239955
Associate II
Posted on November 25, 2014 at 15:55

Is there any fault in the programming?

Posted on November 25, 2014 at 17:47

Is there any fault in the programming?

I've posted code to the forum that I believe is functionally correct, I'm not looking to keep reinventing the wheel or fix it when people break it.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rm239955
Associate II
Posted on December 04, 2014 at 13:27

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6kG&d=%2Fa%2F0X0000000buT%2Fc1ZyPxwbCKGOvFfgTBwvV7nMxWADEUuR3QqaiXAAA6s&asPdf=false