2019-06-12 02:54 AM
Hi,
Following is my init code for stm32f4 CAN Bus Normal mode. I've Calculated the bus to work on bit rate of 500 kbps & 16 Tq's. If i connect two discovery boards with transceivers(TJA1050) on both sides, i can see CAN data getting transmitted & received. But, If i connect CAN Bus Analyzer to the harness i'm not seeing correct CAN Bus data traffic in my PC. I doubt my clock settings.
Kindly anyone confirm am i setting the CAN Bus bit rate of 500 kbps properly with my initialization?
System Clock source details(following values received usingfunction RCC_GetClocksFreq():
-------------------------------------------------------------------------------------------------------------------------
RCC Clock Source : 0x08 (PLL used as System clock)
SYSCLK_Frequency: 168000000
HCLK_Frequency: 168000000
PCLK1_Frequency: 42000000
PCLK2_Frequency: 84000000
Init Code:
--------------
void can1_init()
{
can1_low_level_init();
hcan1.CAN_Prescaler = 21;
hcan1.CAN_Mode = CAN_Mode_Normal;
hcan1.CAN_SJW = CAN_SJW_1tq;
hcan1.CAN_BS1 = CAN_BS1_12tq;
hcan1.CAN_BS2 = CAN_BS2_4tq;
hcan1.CAN_ABOM = DISABLE;
hcan1.CAN_AWUM = DISABLE;
hcan1.CAN_RFLM = DISABLE;
hcan1.CAN_TTCM = DISABLE;
hcan1.CAN_TXFP = DISABLE;
hcan1.CAN_NART = DISABLE;
CAN_Init(CAN1, &hcan1);
pTxM.DLC = 8;
pTxM.ExtId = 0x0200;
pTxM.IDE = CAN_ID_EXT;
pTxM.RTR = CAN_RTR_DATA;
pTxM.Data[0] = 0x00;
}
Solved! Go to Solution.
2019-06-12 10:23 AM
Although the example here is 2 MHz / 17 = 117647 bps
42,000,000 / 500,000 = 84
84/6 = 14
Perhaps try 1+10+3 or 1+11+2, prescale 6
2019-06-12 09:27 AM
With prescaler 21, your "CAN-clock" tics with 2 MHz (42000000 / 21). your Tq is then 500 ns.
If you have 16 Tqs per bit, the bit rate is 2 MHz/16 = 125 kbps.
2019-06-12 10:23 AM
Although the example here is 2 MHz / 17 = 117647 bps
42,000,000 / 500,000 = 84
84/6 = 14
Perhaps try 1+10+3 or 1+11+2, prescale 6
2019-06-12 09:21 PM
Note, that the first phase is always 1 Tq - by the CAN specs and by the reference manual. It's not set in code, but it's there by default.
you only set the combined second-third phase and the fourth phase lengths.
2019-06-14 01:04 AM
@turboscrew @Community member Thank you. I misunderstood CAN bus clock source as 168 MHz instead of 42 MHz.