cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with CAN config and Clock STM32F407VG

aurelien
Associate
Posted on July 17, 2013 at 11:35

Hi,

I have a problem with my CAN configuration, I try to interface the STM32 with a NI USB/CAN. The problem is the baudrate configuration I think, see my code below :

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_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 = 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 = 1MBps (CAN clocked at 30 MHz) */
CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 5;
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.StdId = 0x0;
TxMessage.ExtId = 0x01;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
/* Enable FIFO 0 message pending Interrupt */
CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);

I config with :


 CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;

 CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;

 CAN_InitStructure.CAN_Prescaler = 5;

In order to have a baudrate @ 400kb/s, this calculation is base on a frequency of 42MHz for the PCLK1. If I check the HCKL, the value is 134222613Hz (check with RCC_GetClockFreq) not 168MHz, I don't understand why? Same think for SYSCLK, the value is 5370011984Hz??? I perform a SystemInit to reset the clocks but nothing works. Does someone have an idea ?
2 REPLIES 2
Posted on July 17, 2013 at 13:45

You should probably look at system_stm32f4xx.c make sure the code is using the right external frequency settings. The STM32F4-Discovery uses an 8 MHz crystal, most of the code is designed for use on the EVAL series boards with 25 MHz crystals. You will need to fix the source, HSE_VALUE and PLL settings.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
aurelien
Associate
Posted on July 18, 2013 at 11:14

Ok,

Now I have selected : the source => HSE Set HSE Value to 8MHz Set PCLK1 to HSE/4 Now HSE=160MHz, PCLK1=40MHz. I have set my CAN config to :


 CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;

 CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;

 CAN_InitStructure.CAN_Prescaler = 5;

In order to have 400kbs/s, but It does'nt work, I don't recieve any message? The thing is that, It used to work last week, I don't know why It doesn't work now !