Reputation: 586
What is the logic of a canbus baudrate configuration as follows, where the numbers are determined ?
CAN_InitStructure.CAN_BS1 = CAN_BS1_2tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler = 16;
CAN_Init(CAN1, &CAN_InitStructure);
Upvotes: 0
Views: 1949
Reputation: 215255
It's a bit of a pain to configure baudrate for CAN, but all CAN controllers work quite similar.
You wish to have the sample point per bit as near the ideal location of 87.5% (as per CAN in Automation/CANopen recommendations). To get it there, you need to tell how many TQ (time quanta) there are before the sample point, and after it.
The length of each TQ is determined by the CAN controller's clock, derived and divided from the system clock, the length of each TQ being 1 clock cycle. 16 TQ is usually ideal, so you'll want to pick a divisor after that. If you run system clock at 16MHz, you can pick a divisor by 16 to get 1MHz CAN clock. Keep in mind that internal RC oscillator is a no-go for CAN! You must use external crystal or oscillator for sufficient accuracy.
The segments before the sample point are usually called sync segment (fixed 1 TQ), propagation segment and phase segment 1 - terminology is a bit different from controller to controller. These segments together should make up 87.5% of the desired baudrate, and the segment after the sample point, usually called phase segment 2, should make up the rest.
Syncronization jump width (SJW) has nothing to do with the baudrate calculation itself, but it is related. SJW determines how many TQ the clock is allowed to deviate for the given baudrate - for 800kbps or 1000kbps you'd set it to 3, otherwise 1 should work.
Upvotes: 2
Reputation: 68034
You forgot about
CAN_InitStructure.SyncJumpWidth = CAN_SJW_1TQ;
The bitrate will be:
BITRATE = (CANCLOCK / CAN_InitStructure.CAN_Prescaler) / (1 + CAN_InitStructure.CAN_BS1 + CAN_InitStructure.CAN_BS2)
Where CANCLOCK is the CAN peripheral clock in Hz. It depends on your clock tree configuration.
Your time quanta frequency is TQF = CANCLOCK / CAN_InitStructure.CAN_Prescaler
Time quanta itself TQ = 1/TQF
(in seconds)
The bit time is BITTIME = TQ * (CAN_InitStructure.SyncJumpWidth + CAN_InitStructure.CAN_BS1 + CAN_InitStructure.CAN_BS2)
Remember that xxxx_3tq means 3 TQs
Upvotes: 3