`BitTiming.from_sample_point` rejects valid timing solutions due to hardcoded register limits
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 72/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 冷清
- 技术栈
- python
- 领域
- embedded-iot
调研方向
从 bit_timing.py 中的 BitTiming.from_sample_point 开始,跟踪其对 iterate_from_sample_point 的调用,然后检查 _restrict_to_minimum_range 和 _validate。验证调用方提供的寄存器限制允许 STM32G431 示例生成 brp=40、tseg1=13 和 tseg2=2,同时现有的默认限制保持当前行为不变。
由索引模型根据 Issue 内容生成。
描述
Description
BitTiming.from_sample_point cannot find valid bit timings for some clock/bitrate combinations. The method calls iterate_from_sample_point, which constructs each BitTiming with strict=True. This parameter cannot be changed by the caller.
The _validate method enforces hardcoded register limits:
tseg1≤ 16tseg2≤ 8brp≤ 64
The strict mode tightens brp further to 32. These limits follow the CAN 2.0 minimum register specification. Many modern controllers support larger register ranges.
When a clock/bitrate combination produces timing values that all exceed at least one of these limits, the solver rejects every solution and raises ValueError.
Example
STM32G431 with candlelight v2.5 firmware from https://github.com/Elmue/CANable-2.5-firmware-Slcan-and-Candlelight has a 160 MHz CAN clock. The FDCAN peripheral supports brp up to 512 and tseg1 up to 256.
from can import BitTiming
bt = BitTiming.from_sample_point(f_clock=160_000_000, bitrate=250_000, sample_point=87.5)
# Raises: ValueError: No suitable bit timings found.
The valid solution is brp=40, tseg1=13, tseg2=2 (sample point = 87.5% exact). The solver finds this combination but rejects it because brp=40 exceeds the strict limit of 32.
All lower prescaler values produce tseg1 values that exceed 16:
| brp | tseg1 | tseg2 | sample point | rejected by |
|---|---|---|---|---|
| 16 | 34 | 5 | 87.50% | tseg1 > 16 |
| 20 | 27 | 4 | 87.50% | tseg1 > 16 |
| 32 | 17 | 2 | 90.00% | tseg1 > 16 |
| 40 | 13 | 2 | 87.50% | brp > 32 (strict) |
Affected code
bit_timing.py—iterate_from_sample_pointpassesstrict=Truewith no option to overridebit_timing.py—_restrict_to_minimum_rangelimitsbrpto 32bit_timing.py—_validatelimitstseg1to 16,tseg2to 8,brpto 64
Suggested fix
Add optional limit parameters to from_sample_point and iterate_from_sample_point:
@classmethod
def from_sample_point(
cls,
f_clock: int,
bitrate: int,
sample_point: float = 69.0,
tseg1_max: int = 16,
tseg2_max: int = 8,
brp_max: int = 64,
) -> "BitTiming":
Pass these limits through to _validate instead of using hardcoded values. This approach lets callers supply the actual register ranges reported by the hardware. The defaults remain unchanged, so existing behavior is not affected.
The gs_usb interface could populate these limits automatically from the device's GS_USB_BREQ_BT_CONST capability response, which already reports tseg1_max, tseg2_max, and brp_max.
Workaround
Construct the BitTiming object directly. The constructor defaults to strict=False:
bt = BitTiming(f_clock=160_000_000, brp=40, tseg1=13, tseg2=2, sjw=2)
Affected version
4.6.1 (also present on main)
- 主要语言
- Python
- 星标
- 1.6k
- 派生
- 697
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
hardbyte/python-can 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 88/100
hardbyte/python-can#2103 ·
-
bug
难度 1/5 1 小时以内 新手友好度 78/100
hardbyte/python-can#2077 · 1 条评论 · 1 个 reaction ·
-
bug
难度 1/5 1 小时以内 新手友好度 68/100
hardbyte/python-can#1922 · 1 个 reaction ·
-
enhancement
难度 5/5 一周以上 新手友好度 30/100
hardbyte/python-can#2102 ·
-
bug
难度 3/5 1-2 天 新手友好度 68/100
hardbyte/python-can#2092 ·
查看 hardbyte/python-can 的全部 Issue
相似的 Issue
-
essnmx good first issue
难度 1/5 1 小时以内 新手友好度 95/100
-
难度 2/5 1-3 小时 新手友好度 65/100
syfoud/Simulated_Scepter#174 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
Giskard-AI/giskard-oss#2840 · 1 条评论 ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success 未关闭area: repo bug perceived difficulty: 2
难度 2/5 1-3 小时 新手友好度 70/100
-
难度 2/5 1-3 小时 新手友好度 75/100
yeti-platform/yeti#1380 ·