1►
flexcan简介
flexcan模块是一个通信控制器,扩展了can fd功能,遵循iso 11898-1标准、can fd和can 2.0b协议规范。 can协议主要被设计用作车载串行总线,满足实时处理,带宽要求,车辆在电磁干扰环境下的可靠操作,该模块支持标准和扩展帧,支持最大64字节有效负载,传输速率高达 8mbps,并且具有非常灵活的用于传输和接收的邮箱系统和rxfifo接收机制。邮箱系统由 32 个报文缓冲区(mb)组成。
2►
flexcan的功能框图
flexcan 的功能框图如下图1所示,包括用于存储报文缓冲区(mb)、接收全局掩码寄存器、接收私有掩码寄存器、接收(rx) fifo 过滤器以及接收 fifo 标识符过滤器的内存。
图1 flexcan功能框图
如上图1所示为flexcan的功能框图,协议引擎(pe)子模块管理 can 总线上的串行通信:
请求存取 ram 接收和传输帧
验收接收到的报文
进行错误处理
检测 can fd 报文
控制器主机接口(chi)子模块负责选择接收和传输的报文缓冲区,以及对报文的仲裁和 id 匹配算法。
总线接口单元(biu)子模块控制内部接口总线的访问,建立与 cpu 和其他模块的连接。时钟、地址和数据总线、中断输出、 dma 都通过 biu 进行访问。
3►
flexcan的时钟
如下图2所示为产生 pe 时钟的电路结构。时钟源选择位(can_ctrl1.clksrc)定义了内部时钟为异步时钟或同步时钟。其中,同步时钟为外设时钟(由apb1总线时钟提供);异步时钟的时钟源可选(细节请参考 rcc 章节 rcc_cfgr2寄存器)。为保证可靠运行,应在模块禁止模式时(can_mcr.mdis 置位)选择时钟源。
图2 flexcan的pe时钟结构
4►
flexcan的协议时序
flexcan支持多种方式来设置 can 协议所要求的位时序参数。控制寄存器 1(can_ctrl1)有各种用于控制位时序参数的字段: presdiv、 propseg、 pseg1、 pseg2 和 rjw。 can 位时序寄存器(can_cbt)扩展了 can_ctrl1 中 can 位时序变量的范围。 can_fdcbt 提供了用于 brs 置位的 can fd 帧数据段的位时序变量。
can fd 使能时,应始终置位 can_cbt.btf 或 can_ctrl2.bte,并在 can_cbt 中配置can 位时序变量。
presdiv字段(及其扩展范围 epresdiv 和用于 can fd 报文数据段的 fdpresdiv)定义了
串行时钟(sclock)的预分频(见下列方程)。串行时钟的周期定义了用于构成 can 波形的时间单位 tq(time quantum)。 tq 为 can 引擎所能处理的最小时间单元。
比特率定义了接收或传输 can 报文的速率,公式如下:
flexcan的位时间可细分为三个部分:
同步段(sync_seg)
1tq 的固定长度;信号边沿出现在该段内。
时间段 1
包括 can 标准的传播段和相位段1。该段可通过设置 can_ctrl1 寄存器的propseg和pseg1 字段来编程,其总和(+2)为 2 ~ 16tq。当 can_cbt.btf 被置位时,flexcan 使用来自 can_cbt 寄存器的epropseg和epseg1 字段,其总和(+2)为 2~ 96tq 。对于brs置位的can fd报文,flexcan使用can_fdcbt寄存器的fdpropseg 和 fdpseg1 字段,其总和为 2 ~ 39tq。
时间段 2
can 标准的相位段2。该段可通过设置can_ctrl1寄存器的pseg2字段来编程,其值(+1)为 2 ~ 8tq。当can_cbt.btf被置位时, flexcan使用来自can_cbt寄存器的epseg2字段,其值(+1)为 2 ~ 32tq。对于brs置位的 can fd 报文, flexcan使用can_fdcbt寄存器的fdpseg2字段,其值(+1)为 2 ~ 8tq。时间段2不能小于信息处理时间(ipt),ipt 在 flexcan 中为 2tq。
注意事项:
fpresdiv 定义了 brs 置位的 can fd 帧数据比特率部分的pe时钟频率和串行时钟(sclock)频率之间的比率。 sclock 周期定义了can fd协议数据比特率的 tq。sclock 频率 = pe 时钟频率 /(fpresdiv + 1)注:为避免处理fd帧时出错, fpresdiv 和presdiv(can_cbt 或 can_ctrl1)请使用相同的值。fpresdiv 只能在冻结模式下写入,其他模式下被硬件锁定。
如下图3所示为flexcan位时间内的段使用 can_ctrl1位时序变量的经典 can 格式。
图3
如下图4所示为flexcan fd位时间内的段,使用can fd格式的can_cbt和can_fdcbt位时序变量,其中flexcan fd的仲裁段使用经典can格式配置位时间,即使用can_cbt位时序变量用于配置仲裁段的位时间,flexcan fd的可变速率的位时间使用can_fdcbt位时序变量配置。
图4
flexcan的语法说明如下表1所示:
表1
当采用can位作为持续时间的衡量标准时(例如,评估报文中的can位事件),一个 can 位的外设时钟个数(numclkbit)为:
其中:
numclkbit 为一个 can 位的外设时钟个数
fsys 为系统(chi)时钟频率,单位 hz
pseg1 为 can_ctrl1.pseg1 的值
pseg2 为 can_ctrl1.pseg2 的值
propseg 为 can_ctrl1.propseg 的值
presdiv 为 can_ctrl1.presdiv 的值
上述公式也适用于 can 位时序寄存器(can_cbt)所述的 can 位时间变量。
因此,flexcan fd仲裁段使用经典can位的速率计算公式为:
flexcan fd可变速率数据段的计算公式为:
fcanclk为pe时钟,单位hz
bitraten是由can 标称位时间变量计算出的 can 位速率,单位bps
bitratef是由can 数据位时间变量计算出的can位速率,单位bps
epseg1为can_cbt.epseg1的值(也可使用 can_ctrl1.pseg1)
epseg2 为 can_cbt.epseg2 的值(也可使用 can_ctrl1.pseg2)
epropseg 为can_cbt.epropseg 的值(也可使用 can_ctrl1.propseg)
epresdiv 为can_cbt.epresdiv 的值(也可使用 can_ctrl1.presdiv)
那么can fd帧标称比特率相应的每个can位的外设时钟数量cpcbn为:
can fd 帧数据比特率相应的每个can位的外设时钟数量cpcbf为:
因此在已知flexcan fd时钟和预分频系数以及波特率的情况下可以计算出flexcan fd的标称位时间tqn总数和可变速率数据位时间tqf总数,计算公式分别如下所示:
fcanclk / bitraten x (epresdiv+1) = [1+(epseg1+1)+(epseg2+1)+(epropseg+1)]
fcanclk / bitratef x (fpresdiv+1) = [1+(fpseg1+1)+(fpseg2+1)+fpropseg]
根据以上flexcan fd的位时序可知可以分别根据标称位时间tqn总数和可变速率数据位时间tqf总数分别计算出其标称位时间的采样点和可变速率数据位时间的采样点,即flexcan fd标称位时间的采样点的计算公式为:
flexcan_samplepoint = (sync_seg+(epropseg+1) +(epseg1+1) )/ (sync_seg+(epropseg+1) +(epseg1+1)+ (epseg2+1))
flexcan fd可变速率位时间的采样点的计算公式为:
flexcan_fdsamplepoint = (sync_seg+fpropseg+(fpseg1+1) )/ (sync_seg+fpropseg+(fpseg1+1)+ (fpseg2+1))
5►
flexcan fd位时间采样点的计算
根据以上4章节flexcan的位时间描述以及flexcan fd位时间和采样点的计算公式推导,在给定flexcan fd时钟和波特率以及分频系数的情况下,可以使用flexcan的库函数通过循环遍历的方式实现自动计算flexcan fd的位时间的采样点。
flexcan fd库函数定义的协议时序代码如下所示:
typedef struct _flexcan_timing_config { u16 predivider; /*!< clock pre-scaler division factor. */ u8 rjumpwidth; /*!< re-sync jump width. */ u8 phaseseg1; /*!< phase segment 1. */ u8 phaseseg2; /*!< phase segment 2. */ u8 propseg; /*!< propagation segment. */ /*!< flexcan has flexible data rate. */ uint16_t fpredivider; /*!< fast clock pre-scaler division factor. */ uint8_t frjumpwidth; /*!< fast re-sync jump width. */ uint8_t fphaseseg1; /*!< fast phase segment 1. */ uint8_t fphaseseg2; /*!< fast phase segment 2. */ uint8_t fpropseg; /*!predivider = 0u; ptimingconfig->fpredivider = 0u; if(flexcan_calculateimprovedtimingvaluesbyfdcbt(baudratefd, sourceclock_hz, ptimingconfig)) { if(flexcan_calculateimprovedtimingvaluesbycbt(baudrate, sourceclock_hz, ptimingconfig)) { fgret = true; } } return (fgret);} static bool flexcan_calculateimprovedtimingvaluesbyfdcbt(uint32_t baudrate, uint32_t sourceclock_hz, flexcan_timing_config_t *ptimingconfig){ uint32_t clk; /* the clock is tqnumb x baudratefd. */ uint32_t tqnum; /* numbers of tq. */ bool fgret = false; tqnum = fdcbt_max_time_quanta; /* auto improved protocal timing. */ do { clk = baudrate * tqnum; if (clk > sourceclock_hz) { continue; /* tqnum too large, clk has been exceed sourceclock_hz. */ } if ((sourceclock_hz / clk * clk) != sourceclock_hz) { continue; /* non-supporting: the frequency of clock source is not divisible by target baud rate, the user should change a divisible baud rate. */ } /* make sure the new calculated divider value is greater than the previous one. */ if (ptimingconfig->fpredivider > ((uint16_t)(sourceclock_hz / clk) - 1u)) { continue; } else { ptimingconfig->fpredivider = (uint16_t)(sourceclock_hz / clk) - 1u; } if (ptimingconfig->fpredivider > max_fpresdiv) { break; /* the frequency of source clock is too large or the baud rate is too small, the pre-divider could not handle it. */ } /* try to get the best timing configuration. */ if (flexcan_fdgetsegmentswithbrs(baudrate, tqnum, ptimingconfig)) { fgret = true; break; } } while(--tqnum >= fdcbt_min_time_quanta); return (fgret);}static bool flexcan_fdgetsegmentswithbrs(uint32_t baudratebrs, uint32_t tqnum, flexcan_timing_config_t *ptimingconfig){ uint32_t ideal_sp; uint32_t p1; bool fgret = false; /* get ideal sample point. */ if (baudratebrs >= 1000000u) { ideal_sp = ideal_sp_low; } else if (baudratebrs >= 800000u) { ideal_sp = ideal_sp_mid; } else { ideal_sp = ideal_sp_high; } /* distribute time quanta. */ p1 = tqnum * (uint32_t)ideal_sp; ptimingconfig->fpropseg = (uint8_t)(p1 / (uint32_t)ideal_sp_factor - 2u); if (ptimingconfig->fpropseg fpropseg > max_fpropseg) { ptimingconfig->fphaseseg1 = ptimingconfig->fpropseg - max_fpropseg; ptimingconfig->fpropseg = max_fpropseg; } else { ptimingconfig->fphaseseg1 = 0; } if(ptimingconfig->fphaseseg1 fpropseg + ptimingconfig->fphaseseg1) fphaseseg2 = (uint8_t)tqnum - (ptimingconfig->fphaseseg1 + ptimingconfig->fpropseg + 3u); if (ptimingconfig->fphaseseg2 fphaseseg1 fphaseseg2) && (ptimingconfig->fpropseg > (ptimingconfig->fphaseseg2 - ptimingconfig->fphaseseg1))) { ptimingconfig->fpropseg -= (ptimingconfig->fphaseseg2 - ptimingconfig->fphaseseg1); ptimingconfig->fphaseseg1 = ptimingconfig->fphaseseg2; } /* subtract one tq for sync seg. */ /* sjw is 20% of total tq, rounded to nearest int. */ ptimingconfig->frjumpwidth = ((uint8_t)tqnum + 4u) / 5u - 1u; if (ptimingconfig->frjumpwidth > max_frjw) { ptimingconfig->frjumpwidth = max_frjw; } fgret = true; } } } } return (fgret);}static bool flexcan_calculateimprovedtimingvaluesbycbt(uint32_t baudrate, uint32_t sourceclock_hz, flexcan_timing_config_t *ptimingconfig){ uint32_t clk; /* the clock is tqnumb x baudratefd. */ uint32_t tqnum; /* numbers of tq. */ bool fgret = false; tqnum = cbt_max_time_quanta; /* auto improved protocal timing. */ do { clk = baudrate * tqnum; if (clk > sourceclock_hz) { continue; /* tqnum too large, clk has been exceed sourceclock_hz. */ } if ((sourceclock_hz / clk * clk) != sourceclock_hz) { continue; /* non-supporting: the frequency of clock source is not divisible by target baud rate, the user should change a divisible baud rate. */ } /* make sure the new calculated divider value is greater than the previous one. */ if (ptimingconfig->predivider > ((uint16_t)(sourceclock_hz / clk) - 1u)) { continue; } else { ptimingconfig->predivider = (uint16_t)(sourceclock_hz / clk) - 1u; } /* to minimize errors when processing fd frames, try to calculate the same value for fpresdiv and presdiv (in cbt). */ if (ptimingconfig->predivider != ptimingconfig->fpredivider) { continue; } if (ptimingconfig->predivider > max_epresdiv) { break; /* the frequency of source clock is too large or the baud rate is too small, the pre-divider could not handle it. */ } /* try to get the best timing configuration. */ if (flexcan_fdgetsegments(baudrate, tqnum, ptimingconfig)) { fgret = true; break; } } while(--tqnum >= cbt_min_time_quanta); return (fgret);}
flexcan fd位时间采样点应用举例,本文示例以mm32f0160的flexcan fd外设为例,其中flexcan fd的标称波特率为500k,可变速率波特率为2mhz。fcan的时钟为72mhz,把flexcan fd的fcan时钟,标称波特率500k,可变速率波特率2mhz代入库函数flexcan_fdcalculateimprovedtimingvalues分别计算得出flexcan fd标称波特率和可变速率波特率的位时间参数如下:
标称波特率的位时间参数:
predivider = 0x01;
propseg = 0x34;
phaseseg1 = 0x08;
phaseseg2 = 0x08;
rjumpwidth = 0x0e
可变速率波特率位时间参数:
fpredivider = 0x01;
fpropseg = 0x07;
fphaseseg1 = 0x04;
fphaseseg2 = 0x04;
frjumpwidth = 0x03
注:再同步参数rjumpwidth 和frjumpwidth不参与flexcanfd采样点的计算
又因为在已知flexcan fd时钟和预分频系数,以及波特率的情况下可以计算出flexcan fd的标称位时间tqn总数和可变速率数据位时间tqf总数,计算公式分别如下所示:
标称位时间tqn总数:
fcanclk / bitraten x (epresdiv+1) = [1+(epseg1+1)+(epseg2+1)+(epropseg+1)]
代入自动计算得出的标称波特率500k的位时间参数,计算出tqn:
tqn = 72000000/500000x(1+1) = 72
可变速率数据位时间tqf总数:
fcanclk / bitratef x (fpresdiv+1) = [1+(fpseg1+1)+(fpseg2+1)+fpropseg]
代入自动计算得出的可变波特率2mhz的位时间参数,计算出tqf:
tqf = 72000000/2000000x(1+1) = 18
根据以上flexcan fd使用库自动计算出的位时间参数,以及标称位时间tqn总数和可变速率位时间tqf总数,可以分别计算出其标称位时间的采样点和可变速率数据位时间的采样点,即flexcan fd标称位时间的采样点的计算公式为:
flexcan_samplepoint = (sync_seg+(epropseg+1) +(epseg1+1) )/ (sync_seg+(epropseg+1) +(epseg1+1)+ (epseg2+1))
代入标称波特率500k的位时间参数计算标称波特率的位时间的采样点如下:
flexcan_samplepoint =(1+(52+1)+(8+1))/(1+(52+1)+(8+1)+(8+1))= 87.5%
flexcan fd可变速率位时间的采样点的计算公式为:
flexcan_fdsamplepoint = (sync_seg+fpropseg+(fpseg1+1) )/ (sync_seg+fpropseg+(fpseg1+1)+ (fpseg2+1))
代入可变速率2mhz波特率的位时间参数计算可变速率的位时间的采样点如下:
flexcan_fdsamplepoint = (1+7+(4+1))/(1+7+(4+1)+(4+1))= 72%
can采样点一般设置在75%—80%之间,具体要根据can通信波特率大小配置。当波特率不超过500k时,建议采样点设置在87.5%;当波特率大小在500k—800k之间的时候,建议采样点设置在80%;当波特率大于800k的时候,建议采样点设置在75%。以上建议并不是绝对的,需根据应用环境复杂程度通过调整flexcan fd的再同步参数rjumpwidth和frjumpwidth使得phaseseg1和phaseseg2以及fphaseseg1和fphaseseg2在标称波特率和可变波特率的位时间上前移或后移n(0-15)个tq值来微调采样点(虽然rjumpwidth和frjumpwidth不参与采样点计算)。
注意事项:
关于采样点的配置的一些额外建议:当用户在实际车用环境中出现flexcan无正常收发数据时除了根据错误标志查找问题定位问题外,还可使用专业的can诊断仪测试出多节点总线上的rjumpwidth和frjumpwidth,phaseseg1和phaseseg2以及fphaseseg1和fphaseseg2参数,参考这些位时间参数值来调整适配rjumpwidth和frjumpwidth,phaseseg1和phaseseg2以及fphaseseg1和fphaseseg2参数到can通信的合适的采样点。
浅谈先科学习机的功能及特点
两脚晶振和四脚晶振在使用电路上有什么区别吗?
苹果传与英特尔洽谈收购基频芯片业务 目前暂中断
饿了么正式宣布收购百度外卖 后者人员架构不变以独立品牌运营
诺基亚的“数字孪生”等技术将作为平台,联手为临港制造业服务
MM32F0160的FlexCAN FD位时间采样点如何计算?
中国、欧盟启动新一轮的反垄断审查,存储三巨头将面临全球反垄断制裁
我国仪器仪表2012发展方向
智能家居设备和可穿戴设备改变老年人生活
小米魅族涨价后,oppo和vivo手机也要涨价了!
Sense搭载的Silk平台 获得软硬件开发资源
小米Pocophone F1 Lite曝光采用了骁龙660处理器具有很高的性价比
广西移动将在2020年内实现全区所有城市的5G精品网络覆盖
哪种电阻更适合被用作为取样电阻?
【51】基于51单片机的蓝牙控制小车的简单实现(有源代码,无图)
拉绳位移传感器安装_拉绳位移传感器使用方法
过电压保护器和避雷器的区别
获评中国教育照明25强,利尔达引领教育照明行业“智变”
深鉴科技亮相华为全联接大会 同华为云达成深度合作
华为最好的一款手机, 并不是华为P10!也不是华为mate9而是相对低价的华为荣耀v9