cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 CAN1 Issue

schalk
Associate II
Posted on August 11, 2012 at 21:22

Hi,

I'm new to this forum and was wondering if someone could maybe assist me with this problem:

I'm using the STM32F407IGT6 device for one of my projects. I want to use both CAN busses on the device. Currently CAN2 is working but CAN1 is not working, the funny thing however is that I've used the CAN1 on the STM32F207 before and did not struggle with CAN1.

I have a CAN analyser so it is fairly easy to look at messages etc.

This is my CAN header detail, obviously currently i just comment out the CAN engine i don't want to use.

(Please see attaced file for code as i am struggling to get it neat here)

So to summarise, when i enable CAN2 I can easily transmit and receive messages. When i enable CAN1 though, nothing is transmitted, and the can message is said to be pending..

I would really appreciate some assistance. Thank you in advance.

#stm32-can
3 REPLIES 3
Posted on August 12, 2012 at 20:48

DOCX files are useless for most of your audience, attach as a .C/.TXT (ie ASCII) or paste in a a code block (paint brush [<>] icon)

I think I observed the use of the wrong clock in a previous edit.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
schalk
Associate II
Posted on August 12, 2012 at 21:02

//Defines 
//#define USE_CAN1 //Isolated CAN Bus 
#define USE_CAN2 //Normal CAN Bus 
#ifdef USE_CAN1 
#define CANx CAN1 
#define CAN_CLK RCC_APB1Periph_CAN1 
#define CAN_RX_PIN GPIO_Pin_8 
#define CAN_TX_PIN GPIO_Pin_9 
#define CAN_GPIO_PORT GPIOB 
#define CAN_GPIO_CLK RCC_AHB1Periph_GPIOB 
#define CAN_AF_PORT GPIO_AF_CAN1 
#define CAN_RX_SOURCE GPIO_PinSource8 
#define CAN_TX_SOURCE GPIO_PinSource9 
#else /*USE_CAN2*/ 
#define CANx CAN2 
#define CAN_CLK (RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2) 
#define CAN_RX_PIN GPIO_Pin_12 
#define CAN_TX_PIN GPIO_Pin_13 
#define CAN_GPIO_PORT GPIOB 
#define CAN_GPIO_CLK RCC_AHB1Periph_GPIOB 
#define CAN_AF_PORT GPIO_AF_CAN2 
#define CAN_RX_SOURCE GPIO_PinSource12 
#define CAN_TX_SOURCE GPIO_PinSource13 
#endif /* USE_CAN1 */ 
Then my CAN Setup looks like this: 
/** 
* @brief Setup the CAN. 
* @param None 
* @retval None 
*/ 
void CAN_Setup(void) 
{ 
/* NVIC configuration */ 
NVIC_Config(); 
/* CAN configuration */ 
CAN_Config(); 
} 
/** 
* @brief Configures the CAN. 
* @param None 
* @retval None 
*/ 
void CAN_Config(void) 
{ 
GPIO_InitTypeDef GPIO_InitStructure; 
/* CAN GPIOs configuration **************************************************/ 
/* Enable GPIO clock */ 
RCC_AHB1PeriphClockCmd(CAN_GPIO_CLK, ENABLE); 
/* Connect CAN pins to AF9 */ 
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT); 
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT); 
/* Configure CAN RX and TX pins */ 
GPIO_InitStructure.GPIO_Pin = CAN_RX_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(CAN_GPIO_PORT, &GPIO_InitStructure); 
/* CAN configuration ********************************************************/ 
/* Enable CAN clock */ 
RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE); 
/* CAN register init */ 
CAN_DeInit(CANx); 
/* CAN cell init */ 
CAN_InitStructure.CAN_TTCM = DISABLE; 
CAN_InitStructure.CAN_ABOM = DISABLE; 
CAN_InitStructure.CAN_AWUM = DISABLE; 
CAN_InitStructure.CAN_NART = DISABLE; 
CAN_InitStructure.CAN_RFLM = DISABLE; 
CAN_InitStructure.CAN_TXFP = DISABLE; 
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; 
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; 
/* CAN Baudrate = 250Kbps (CAN clocked at 42 MHz) */ 
CAN_InitStructure.CAN_BS1 = CAN_BS1_12tq; 
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq; 
CAN_InitStructure.CAN_Prescaler = 8; 
CAN_Init(CANx, &CAN_InitStructure); 
/* CAN filter init */ 
#ifdef USE_CAN1 
CAN_FilterInitStructure.CAN_FilterNumber = 0; 
#else /* USE_CAN2 */ 
CAN_FilterInitStructure.CAN_FilterNumber = 14; 
#endif /* USE_CAN1 */ 
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 Structure preparation */ 
TxMessage.StdId = 0x321; 
TxMessage.ExtId = 0x01; 
TxMessage.RTR = CAN_RTR_DATA; 
TxMessage.IDE = CAN_ID_STD; 
TxMessage.DLC = 1; 
/* Enable FIFO 0 message pending Interrupt */ 
CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE); 
} 
/** 
* @brief Configures the NVIC for CAN. 
* @param None 
* @retval None 
*/ 
void NVIC_Config(void) 
{ 
NVIC_InitTypeDef NVIC_InitStructure; 
#ifdef USE_CAN1 
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn; 
#else /* USE_CAN2 */ 
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn; 
#endif /* USE_CAN1 */ 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure); 
} 

Thank you Clive, i did not know how to add code. I saw the post regarding the CAN Clock, but my clock is definately right because CAN2 works fine, the CAN clock is 42MHz, in that post they were using the assumption of a 30MHz clock.
schalk
Associate II
Posted on August 13, 2012 at 08:23

Hi Guys

Never mind, this problem was all due to my own stupidity. I have opto's on my TX and RX lines, and these operate from 5V. So the output type of the IO should be defined as Open drain and not Push Pull.

My apologies for wasting any anyone's time.

The CAN bus is now working properly, both CAN1 and CAN2.