星期六, 07 07月 2018 01:58

FlexCAN Bit Timing Calculation

男作者
给本文评分
(1 投票)
FlexCAN 支持CAN协议要求的多种方式来计算位时间,本文是一种基本的位时间关系的一种基本介绍,波特率的计算步骤.
1. 简介
FlexCAN bit timing-1.jpg

The clock dependency can be described based on the following figure.
Starting from a data frame transferred on the bus. A desired baudrate tells how many bits are sent on the network per second. The Nominal Bit Rate (baudrate) is uniform throughout the network and is given by:
波特率的定义公式:

baudrate = 1 / tNBT       (1)

The tNBT represents a period of the Nominal Bit Time (NBT), i.e. single bit transfered on the bus. The NBT is separated into four non-overlaping segments, SYNC_SEG, PROP_SEG, PHASE_SEG1 and PHASE_SEG2. Each of these segments is an integer multiple of Time Quantum (tQ).

tNBT= tQ+PROP_SEG* tQ + PHASE_SEG1* tQ + PHASE_SEG2* tQ = NBT * tQ             (2)

下表概述了每一段可能的设定值.
FlexCAN bit timing-2.jpg

Note: 函数 MAX( , ) 返回两个数中的较大值.
The duration of the segment PHASE_SEG1 may be between 1 and 8 Time Quanta if one sample per bit is selected and may be between 2 and 8 Time Quanta if three samples per bit are selected. If three samples per bit are chosen, the most frequently sampled value is taken as the bit value.
The duration of segment PHASE_SEG2 must be equal to PHASE_SEG1, unless PHASE_SEG1 is less than the Information Processing Time (IPT), in which case PHASE_SEG2 must be equal to the Information Processing Time. The Information Processing Time is equal to 2 Time Quanta
From the table, it would appear that the minimum number of Time Quanta per bit (NBT ) is 5. However, many CAN controllers require a minimum of 8 Time Quanta per bit. The maximum number of Time Quanta per bit is 25.
A Time Quantum is the atomic unit of time handled by the CAN engine and it is given by the Serial Clock (Sclock) period as

tQ = 1/Sclock = 1/[CPI_clock /(PRESDIV+1)].            (3)

The CPI_clock is the CAN Protocol Interface clock, can be either the peripheral clock (driven by the FMPLL) or the crystal oscillator clock. The PRESDIV controls a Prescaler that generates the Serial Clock (Sclock)
FlexCAN bit timing-3.jpg

The clock source selection is done by the CLK_SRC bit of the CTRL register. Moreover this
register contains other fields used to control bit timing parameters as was mentioned above: PRESDIV, PROPSEG, PSEG1, PSEG2 and RJW. The SMP specifies if one or three samples per bit is used.
FlexCAN bit timing-4.jpg

These parameters have to be properly calculated/selected to meet the CAN specification.
The following chapter provides a step-by-step guide for determining the optimum bit timing parameters which satisfy the requirements for proper bit sampling. Besides the desired baudrate and selected CPI clock a physical interface parameters and bus length is also needed to determine propagation segment duration.

2 计算步骤

STEP 1: Determine minimum permissible time for the PROP_SEG segment
Obtain the maximum propagation delay of the physical interface for both the transmitter and the receiver from the manufacturers data sheet. Calculate the propagation delay of the bus by multiplying the maximum length of the bus by the signal propagation delay of the bus cable. Use these values to calculate tPROP_SEG using equation

tPROP_SEG = 2(tBUS + tTX + tRX                                                           (4)

STEP 2: Choose CPI_clock, the Prescaler and NBT
Determine Prescaler and a Nominal Bit Time (NBT ). Basically you can combine equations (1), (2) and (3) into

NBT * Prescaler = CPI_clock / baud rate                                                 (5)

As already mentioned NBT may be integer number in range 8 to 25 and Prescaler in range 1 to 256. Find those combinations of NBT * Prescaler which is equal to CPI_clock /baud rate. There may be a lot of possible combinations. If none can be found choose different CPI_clock.
STEP 3: Calculate PROP_SEG duration
The number of time quanta required for the PROP_SEG segment are calculated based on chosen Prescaler value

PROP_SEG = ROUND_UP(tPROP_SEG / tQ) =ROUND_UP(tPROP_SEG*CPI_clock/Prescaler)                               (6)

where the function ROUND_UP( ) returns the argument rounded up to the next integer value.
 
STEP 4: 确定 PHASE_SEG1, PHASE_SEG2
From the number of Time Quanta per bit obtained in Step 2, subtract the PROP_SEG value calculated in Step 3 and subtract 1 tQ for SYNC_SEG.
If the remaining number is an odd number greater than 3 then add one to the PROP_SEG value and recalculate.
If the remaining number is equal to 3 then PHASE_SEG1 = 1 and PHASE_SEG2 = 2 and only one sample per bit may be chosen. Otherwise divide the remaining number by two and assign the result to PHASE_SEG1 and PHASE_SEG2.
 
STEP 5: 确定 RJW
RJW is chosen as the smaller of 4 and PHASE_SEG1
 
STEP 6: 计算可允许的晶振误差
There are two clock tolerance requirements which must be satisfied.

( 2 * Δf) * 10 * tNBT tRJW                                                                               (8)
( 2 * Δf) * (13 * tNBT – tPHASE_SEG2) MIN(tPHASE_SEG1, tPHASE_SEG2)             (9)

其中函数 MIN( , ) 返回两个数中的较小值. (Δf :要计算的可接受的误差值)
As the required oscillator tollerance select the smaller of the calculated values.

3 示例
Calculate the bit segments for the following system constraints:
• Bit rate = 500k bit per second
Bus length = 10m
Bus propagation delay = 5 x 10-9  (s/m)
Physical Interface (PCA82C250) transmitter plus receiver propagation delay = 150ns at 85 °C
CPI_clock = 40 MHz
STEP 1:
Physical delay of bus = 10m x 5 ns/m = 50 (ns)
tPROP_SEG = 2(50 + 150) = 400 (ns)
STEP 2:
NBT * Prescaler = CPI_clock /baud rate = 40 MHz / 500kbps = 80
There can be 4 possibilities:
NBT = 20, Prescaler = 4
NBT = 16, Prescaler = 5
NBT = 10, Prescaler = 8
NBT = 8, Prescaler = 10
Lets choose the first one
STEP 3:
PROP_SEG = ROUND_UP(400ns * 40MHz / 4) = 4
STEP 4:
NBT – 1 – PROP_SEG = 20 – 1 – 4 = 15
As the result is odd increase PROP_SEG and recalculate
20 – 1 – 5 = 14
Divide the number by two and assign the result to PHASE_SEG1 and PHASE_SEG2.
PHASE_SEG1 = 7 and PHASE_SEG2 = 7
STEP 5:
RJW is chosen as the smaller of 4 and PHASE_SEG1, so RJW = 4
STEP 6:
( 2 * Δf) * 10 * tNBT tRJW
( 2 * Δf) * 10 * 20 f 0.01 = 1%    (tNBT=NBT*tQ; tRJW=tRJW *tQ, So tQ )
( 2 * Δf) * (13 * tNBT – tPHASE_SEG2) MIN(tPHASE_SEG1, tPHASE_SEG2)
( 2 * Δf) * (13 * 20 – 7) Δf 0.0138 = 1.38%
required oscillator tollerance is 1%
In summary:
Prescaler = 4
Nominal Bit Time = 20
PROP_SEG = 5
PHASE_SEG1 = 7
PHASE_SEG2 = 7
RJW = 4
Oscillator tolerance = 1%
Similarly repeate steps 3-6 for next NBT, Prescaler values given in step 2.
Calculated bit timing parameters are given in following table.
FlexCAN bit timing-5.jpg
查看 21655 最后修改日期 星期六, 07 07月 2018 18:49
本栏更多文章: KWP2000协议分析 »