cancel
Showing results for 
Search instead for 
Did you mean: 

Solved: STM32F4Discovery CAN Bus timing problems :/

Just Matt
Associate III
Posted on December 27, 2013 at 00:52

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);

#can-bus-bit-timing
3 REPLIES 3
Posted on December 27, 2013 at 02:18

Well no source in the download.

My suspicious is the chip isn't running at the speed you think it is. I'd wager you have a system_stm32f4xx.c set up for a board with a 25 MHz HSE crystal. Make sure you fix the PLL settings for the 8 MHz crystal used on the STM32F4-Discovery, and that the define for HSE_VALUE also reflects the actual speed.

If you want to check internal frequencies direct the out via the MCOx pins, and scope it.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Just Matt
Associate III
Posted on December 27, 2013 at 03:14

Hey! Thanks for your reply.

The source is in the dropbox link above, its a rar file (Use winrar) under the Can Bus Example folder. Its a uvision project.

I will double check those settings, but i'm fairly certain they are correct. I have checked the rccClocks in code and PCLK1 is running at 42MHZ. (See the attached screenshot to this message)

Thanks!

-Matt

EDIT: After looking at the system_stm32f4xx.c file. I think that the values are incorrect, but I'm not sure. Changing theHSE_VALUE to the correct value 8000000 then I check therccClocks and they are way off. I'm now officially confused and not sure exactly what values to change :)

Am I correct in assuming that PLL_N should be 8 and HSE_VALUE should be 8000000? Thanks in advance :\

EDIT #2: I belive that did it!.. Just a quick check on the LA, and that gives me 2us in the smallest area. clive1, thank you very very very much! I feel a little stupid now :)

________________

Attachments :

BusSpeeds.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzkU&d=%2Fa%2F0X0000000bP0%2FK9vT_8M3udj_TmQhy2LJAV4FY4lAVuQNTwmKzUnjIJU&asPdf=false
Posted on December 27, 2013 at 03:58

Glad you got it figured out.

/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M 25 // 25 for 25 MHz HSE, 8 or 8 MHz HSE, for 16 for 16 MHz HSI
Looking to get a comparison frequency in the 1-2 MHz range

I'm familiar with WinRAR and Keil, but I'm still not seeing any source in your archive, perhaps someone else can cross check, and confirm.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..