2012-02-19 07:11 PM
Im trying to use the CAN interface on the STM32F4 discovery board. I am using the STM code sample which says it configures it to 1Mbit baud rate but this doesnt seem to be the case, with an oscilloscope hooked I am getting a baud rate of 2kBit and a 5us sync pulse (it should be about 250ns for 1Mbit from what I have read.
/* 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 = 1 MBps (CAN clocked at 30 MHz) */
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 2;
CAN_Init(CANx, &CAN_InitStructure);
This is all the standard code with a comment towards the end that it sets it up for 1Mbit with a 30MHz clock, the only thing I can think of is that the 30MHz clock asumption is wrong, I cant see where this would be coming from. From what I can find the APB1 clock should just be running off the HSI clock but this doesnt seem to be the case.
Can anyone point me in the right direction for fixing this? (I am using IAR for my IDE if that makes any difference)
2014-08-23 04:16 PM
what if you have a CAN baud rate of 33.3K? where you cant have a even divisible number?
I am having trouble setting the timing with a APB clk of 42Mhz.thanks, Tony2014-08-23 05:39 PM
So run the PLL/VCO at 333 MHz, the CPU at 166.5 MHz, APB2 at 83.25 MHz and APB1 at 41.625 MHz, CAN at 33.3 KHz
41625000 / 33300 = 1250 25 * 50 = 1250 125 * 10 = 12502014-08-23 06:55 PM
thanks, I working on a project that will auto detect the CAN baud rate without changing the system clocks. I guess I will have to reconfig the PLL during runtime.
thanks,