Bug 265533 - uchcom: baud rate is not correct for CH34x
Summary: uchcom: baud rate is not correct for CH34x
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL: https://github.com/nospam2000/ch341-b...
Keywords:
Depends on:
Blocks:
 
Reported: 2022-07-31 12:40 UTC by Tomoyuki Sakurai
Modified: 2022-08-02 16:39 UTC (History)
1 user (show)

See Also:


Attachments
PoC patch that fixed the issue (3.41 KB, patch)
2022-07-31 12:42 UTC, Tomoyuki Sakurai
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tomoyuki Sakurai 2022-07-31 12:40:59 UTC
the symptom is that, when uploading code with avrdude, sometimes the result is successful, but not always.

I have dozens of Chinese clones of arduino nano with CH340C. uploading fails on about 6 of 10. usually ends up with `expect=0xWHATEVER, resp=0xfc`. during the process, some commands get proper replies, such as `Device signature = 0x1e950f (probably m328p)`, others do not. this seems like a baud rate issue.

the Linux kernel driver had a similar issue in the past. but recent kernel does not, which was verified on Fedora 36. avrdude on the Fedora can upload code to all the problematic nano boards.

after reading this report on GitHub, i was convinced that the uchcom(4) has issues.
https://github.com/nospam2000/ch341-baudrate-calculation

when `uchcom_calc_divider_settings()` is replaced with the one in NetBSD, all boards work. Sometimes, I am facing issues when avrdude verifies the flash content, but I am not yet sure what is wrong. could not find common denominators yet. at least, uploading always works with old bootloader (boud rate 57600), and new bootloader (115200).
Comment 1 Tomoyuki Sakurai 2022-07-31 12:42:35 UTC
Created attachment 235574 [details]
PoC patch that fixed the issue

here is my local patch that fixed the issue.
Comment 2 Tomoyuki Sakurai 2022-08-01 07:07:03 UTC
"always work" is bit too early to say. although success rate is far better than before, i am seeing failures.
Comment 3 Tomoyuki Sakurai 2022-08-01 10:57:29 UTC
checked all the failed boards on Fedora. none worked. it is likely that they are malfunctioning not because of issues in device driver. at least, the patch saved some.
Comment 4 Tomoyuki Sakurai 2022-08-02 01:29:51 UTC
tested other CH340 chips. works fine with CH340G (115200).