AnsweredAssumed Answered

Solved: STM32F4Discovery CAN Bus timing problems :/

Question asked by honeycutt.matt on Dec 27, 2013
Latest reply on Dec 27, 2013 by Clive One
EDIT: Thanks to clive1 my issue has been solved! See last post for how ignorant I was :) Cheers!


Hi,

Disclaimer: I'm a new engineer (green).

I've been evaluating the STM32F4Discovery board for some time. I've been trying to figure out why the CAN Bus timing is not working its way out. 


For PCLK1 being at 42MHZ, I have come up with the following CAN bus timings; (I may be calculating this wrong) My Goal is 500kbs
CAN_SJW = CAN_SJW_1tq; // synchronization jump width = 1
CAN_BS1 = CAN_BS1_14tq; //14
CAN_BS2 = CAN_BS2_6tq; //6
CAN_Prescaler = 4; // baudrate 500 kbps 

For some reason the transmitted CAN signal is off by a severe amount, I cannot figure out why :/ The goal is 500kbs


As you can see in the Logic Dump attached in the link below, the width of the smalles signals is 6.25us. Therefore, 1/6.25 = 163Kbs which is way way off. (This is confirmed by another CAN device on the network auto scaling to the baud rate of the Discovery board)

Any help would greatly be appreciated.

You can download my entire uVision project (including all sources) and both LogicAnalyzer dump and picture.


https://www.dropbox.com/s/jui7t82mhhtjxqh/Can%20Bus%20Example.rar

Here is some sample code. (Complete code in the above rar file)

     /* 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); //PD0
     GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT); //PD1


     /* 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; // time-triggered communication mode = DISABLED
     CAN_InitStructure.CAN_ABOM = DISABLE; // automatic bus-off management mode = DISABLED
     CAN_InitStructure.CAN_AWUM = DISABLE; // automatic wake-up mode = DISABLED
     CAN_InitStructure.CAN_NART = ENABLE; // non-automatic retransmission mode = ENABLE (To prevent endless spam)
     CAN_InitStructure.CAN_RFLM = DISABLE; // receive FIFO locked mode = DISABLED
     CAN_InitStructure.CAN_TXFP = DISABLE; // transmit FIFO priority = DISABLED
     CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
     CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;


     /* CAN Baudrate */
     CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;
     CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
     CAN_InitStructure.CAN_Prescaler = 4;
     CAN_Init(CANx, &CAN_InitStructure);

Outcomes