`BitTiming.from_sample_point` rejects valid timing solutions due to hardcoded register limits
まだ誰も着手していません。
評価
- 難易度
- 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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
hardbyte/python-can のほかの issue
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
hardbyte/python-can#2077 · コメント 1 件 · リアクション 1 件 ·
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
hardbyte/python-can#1922 · リアクション 1 件 ·
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 30/100
hardbyte/python-can#2102 ·
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
hardbyte/python-can#2092 ·
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
hardbyte/python-can#2091 ·
hardbyte/python-can の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
use-agent-os/agent-os#3314 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
BasedHardware/omi#15662 · コメント 1 件 ·
-
documentation help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
AiursoftWeb/AnduinOS-2#19 ·