STM32F4Discovery Can Baud Rate Timing Issues :/ - Page 1
Forum section: electronics / general

author: ZeroAviation
author_rank: Contributor
timestamp: December 26, 2013, 11:56:46 pm
content: Fellow EEVBloggers,Disclaimer: I'm a new engineer (green).I've been tinkering 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) The goal baud rate is 500kbsCAN_SJW = CAN_SJW_1tq; // synchronization jump width = 1CAN_BS1 = CAN_BS1_14tq; //14CAN_BS2 = CAN_BS2_6tq; //6CAN_Prescaler = 4; // baudrate 500 kbpsFor some reason the transmitted CAN signal is off by a severe amount, I cannot figure out why :/ The goal baud rate is 500kbsAs 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.rarHere 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); //PD0GPIO_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 = DISABLEDCAN_InitStructure.CAN_ABOM = DISABLE; // automatic bus-off management mode = DISABLEDCAN_InitStructure.CAN_AWUM = DISABLE; // automatic wake-up mode = DISABLEDCAN_InitStructure.CAN_NART = ENABLE; // non-automatic retransmission mode = ENABLE (To prevent endless spam)CAN_InitStructure.CAN_RFLM = DISABLE; // receive FIFO locked mode = DISABLEDCAN_InitStructure.CAN_TXFP = DISABLE; // transmit FIFO priority = DISABLEDCAN_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);

author: AndreasF
author_rank: Frequent Contributor
timestamp: December 27, 2013, 11:52:06 am
content: The settings for the CAN timing look good to me for a peripheral clock of 42Mhz, so the question is, how confident are you that it is actually 42Mhz?My suspicion is that the settings for the main oscillator are not correct. I've seen other people have that problem with the Discovery boards. The issue stems from the fact that the default start-up routine assumes an external crystal frequency of 25MHz, but the actual crystal frequency is often 8Mhz. Therefore, the system assumes it's running approximately 3 times faster than it is actually running. In your case, multiplying the baud rate you measured by 3 would give you 489 kbs, which is roughly in the target region.And shouldn't the "CANx" in the CAN_Init() function actually be either CAN1 or CAN2 (according to your pin config, it would be CAN1)?(PS: I haven't looked at your full code - apparently that's not easy to do if you don't have uVision itself.)[edited to add my suspicion regarding crystal frequency]

author: ZeroAviation
author_rank: Contributor
timestamp: December 27, 2013, 05:41:28 pm
content: Quote from: AndreasF on December 27, 2013, 11:52:06 amYou Sir are 100% correct. I assumed that the Startup would take in account the 8mhz on the Discovery, but I was wrong (Trap for new players)After changing PLL_M and HSI_Value to 8mhz values works like a charm.Thanks!-Matt <---New player.