All of lore.kernel.org
 help / color / mirror / Atom feed
* ch341 garbage read with 5.5.x kernel
@ 2020-02-04 22:02 jakub nantl
  2020-02-05  7:43 ` Johan Hovold
  0 siblings, 1 reply; 6+ messages in thread
From: jakub nantl @ 2020-02-04 22:02 UTC (permalink / raw)
  To: linux-usb

Hi,

I have arduino nano (ch341) connected to my pc and after upgrading
kernel to 5.5.x  I am getting garbage instead of text while reading it
(with both 5.5.1 and 5.5.2 kernels):

Feb  4 09:24:20 sopa read.pl[2070]: StX.XXA(aurXXXŅstXC#021XX XJXR
FuX,#027XX#005
Feb  4 09:24:20 sopa read.pl[2070]: XtX,+XX HXX#026XX go.XXRXXXXXng*Xery
XXX5XUXiXY'XX4
Feb  4 09:24:20 sopa read.pl[2070]: XP5逮#013XXteXX11XS4
Feb  4 09:24:20 sopa read.pl[2070]:
XP5逮#013XXhuXZ.XX=6SHX#005XAXXXpXKVX}XXt=MXXj
Feb  4 09:24:20 sopa read.pl[2070]: DXUu X#013X-XXXXXeXNMSX
Feb  4 09:24:20 sopa read.pl[2070]: XP)}XNXC

with 5.4.17 I get:

Feb  4 22:43:10 sopa read.pl[2040]: Started (auriol_last)
Feb  4 22:43:11 sopa read.pl[2040]: Reporting every 300s
Feb  4 22:43:11 sopa read.pl[2040]: Uptime: 60
Feb  4 22:43:11 sopa read.pl[2040]: DATA: sopa-temp=11.21
Feb  4 22:43:11 sopa read.pl[2040]: DATA: sopa-humidity=60
Feb  4 22:43:11 sopa read.pl[2040]: DATA: sopa-dewpoint=3.68
Feb  4 22:43:11 sopa read.pl[2040]: DATA: sopa-pressure=97136
Feb  4 22:43:11 sopa read.pl[2040]: DATA_END

any suggestions?

jn



00:15.0 USB controller: Intel Corporation Device 31a8 (rev 03) (prog-if
30 [XHCI])
    Subsystem: ASRock Incorporation Device 31a8
    Flags: bus master, medium devsel, latency 0, IRQ 126
    Memory at a1300000 (64-bit, non-prefetchable) [size=64K]
    Capabilities: [70] Power Management version 2
    Capabilities: [80] MSI: Enable+ Count=1/8 Maskable- 64bit+
    Capabilities: [90] Vendor Specific Information: Len=14 <?>
    Kernel driver in use: xhci_hcd

Bus 001 Device 003: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial
adapter
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass          255 Vendor Specific Class
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0         8
  idVendor           0x1a86 QinHeng Electronics
  idProduct          0x7523 HL-340 USB-Serial adapter
  bcdDevice            2.63
  iManufacturer           0
  iProduct                2 USB2.0-Serial
  iSerial                 0
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           39
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0x80
      (Bus Powered)
    MaxPower              482mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           3
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass      1
      bInterfaceProtocol      2
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0020  1x 32 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0020  1x 32 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval               1
Device Status:     0x0000
  (Bus Powered)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: ch341 garbage read with 5.5.x kernel
  2020-02-04 22:02 ch341 garbage read with 5.5.x kernel jakub nantl
@ 2020-02-05  7:43 ` Johan Hovold
  2020-02-05  7:53   ` jakub nantl
  0 siblings, 1 reply; 6+ messages in thread
From: Johan Hovold @ 2020-02-05  7:43 UTC (permalink / raw)
  To: jakub nantl; +Cc: linux-usb

On Tue, Feb 04, 2020 at 11:02:04PM +0100, jakub nantl wrote:
> Hi,
> 
> I have arduino nano (ch341) connected to my pc and after upgrading
> kernel to 5.5.x  I am getting garbage instead of text while reading it
> (with both 5.5.1 and 5.5.2 kernels):
> 
> Feb  4 09:24:20 sopa read.pl[2070]: StX.XXA(aurXXXŅstXC#021XX XJXR
> FuX,#027XX#005
> Feb  4 09:24:20 sopa read.pl[2070]: XtX,+XX HXX#026XX go.XXRXXXXXng*Xery
> XXX5XUXiXY'XX4
> Feb  4 09:24:20 sopa read.pl[2070]: XP5逮#013XXteXX11XS4
> Feb  4 09:24:20 sopa read.pl[2070]:
> XP5逮#013XXhuXZ.XX=6SHX#005XAXXXpXKVX}XXt=MXXj
> Feb  4 09:24:20 sopa read.pl[2070]: DXUu X#013X-XXXXXeXNMSX
> Feb  4 09:24:20 sopa read.pl[2070]: XP)}XNXC
> 
> with 5.4.17 I get:
> 
> Feb  4 22:43:10 sopa read.pl[2040]: Started (auriol_last)
> Feb  4 22:43:11 sopa read.pl[2040]: Reporting every 300s
> Feb  4 22:43:11 sopa read.pl[2040]: Uptime: 60
> Feb  4 22:43:11 sopa read.pl[2040]: DATA: sopa-temp=11.21
> Feb  4 22:43:11 sopa read.pl[2040]: DATA: sopa-humidity=60
> Feb  4 22:43:11 sopa read.pl[2040]: DATA: sopa-dewpoint=3.68
> Feb  4 22:43:11 sopa read.pl[2040]: DATA: sopa-pressure=97136
> Feb  4 22:43:11 sopa read.pl[2040]: DATA_END
> 
> any suggestions?

There were some fixes to the baudrate handling that went into 5.5 that
are likely related to this.

These changes provide more accurate output rates, but I have since
received one report that it may inadvertently have made the device more
sensitive to errors in the input rate. In that case, the reporter
switched to a baudrate that matches his actual rate which was 117647
rather than 115200 (i.e. 2.1% error) and that addressed the problem.

Which baudrate are you using here?

Johan

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: ch341 garbage read with 5.5.x kernel
  2020-02-05  7:43 ` Johan Hovold
@ 2020-02-05  7:53   ` jakub nantl
  2020-02-05  8:50     ` Johan Hovold
  0 siblings, 1 reply; 6+ messages in thread
From: jakub nantl @ 2020-02-05  7:53 UTC (permalink / raw)
  To: Johan Hovold; +Cc: linux-usb

On 05. 02. 20 8:43, Johan Hovold wrote:
> There were some fixes to the baudrate handling that went into 5.5 that
> are likely related to this.
>
> These changes provide more accurate output rates, but I have since
> received one report that it may inadvertently have made the device more
> sensitive to errors in the input rate. In that case, the reporter
> switched to a baudrate that matches his actual rate which was 117647
> rather than 115200 (i.e. 2.1% error) and that addressed the problem.
>
> Which baudrate are you using here?

my baudrate is 115200, how can I get "my" actual rate ?

jn


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: ch341 garbage read with 5.5.x kernel
  2020-02-05  7:53   ` jakub nantl
@ 2020-02-05  8:50     ` Johan Hovold
  2020-02-05 12:31       ` jakub nantl
  0 siblings, 1 reply; 6+ messages in thread
From: Johan Hovold @ 2020-02-05  8:50 UTC (permalink / raw)
  To: jakub nantl; +Cc: Johan Hovold, linux-usb

On Wed, Feb 05, 2020 at 08:53:55AM +0100, jakub nantl wrote:
> On 05. 02. 20 8:43, Johan Hovold wrote:
> > There were some fixes to the baudrate handling that went into 5.5 that
> > are likely related to this.
> >
> > These changes provide more accurate output rates, but I have since
> > received one report that it may inadvertently have made the device more
> > sensitive to errors in the input rate. In that case, the reporter
> > switched to a baudrate that matches his actual rate which was 117647
> > rather than 115200 (i.e. 2.1% error) and that addressed the problem.
> >
> > Which baudrate are you using here?
> 
> my baudrate is 115200, how can I get "my" actual rate ?

You can always use a scope or logic analyser, but you can derive it
theoretically if you know what oscillator frequency the arduino is
using. You should be able to find more details in the datasheet for the
MCU in question.

For example, if you look at section 19.11 in the ATmega238p datasheet
you see that with a 16 MHz clock you may end up with a -3.5% or 2.1%
error at 115200. The latter is likely the 117647 rate mentioned above.

That said, I have a hypothesis for how we may get the best of both
worlds here.

Can you try the below patch which restores a component included in the
first version of the new algorithm, but which I ultimately deemed
redundant?

Johan


From 0b833ae9dde819ffb41c8d16d3591484d1eab04c Mon Sep 17 00:00:00 2001
From: Johan Hovold <johan@kernel.org>
Date: Wed, 5 Feb 2020 09:33:15 +0100
Subject: [PATCH] USB: serial: ch341: prefer lower divisors

Although it was assumed to not make a difference, not using the factor 2
prescaler appears to make the receiver more susceptible for errors in
the input rate. Specifically, there are reports of problems with devices
that cannot generate a 115200 rate with a smaller error than 2.1% (e.g.
117647 bps).

So whenever possible, enable the factor 2 prescaler and halve the
divisor in order to use settings closer to that of the previous
algorithm.

Fixes: 35714565089e ("USB: serial: ch341: reimplement line-speed handling")
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/usb/serial/ch341.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
index df582fe855f0..86686e60238a 100644
--- a/drivers/usb/serial/ch341.c
+++ b/drivers/usb/serial/ch341.c
@@ -205,6 +205,12 @@ static int ch341_get_divisor(speed_t speed)
 			16 * speed - 16 * CH341_CLKRATE / (clk_div * (div + 1)))
 		div++;
 
+	/* Prefer lower base clock (fact = 0) if even divisor. */
+	if (fact == 1 && div % 2 == 0) {
+		div >>= 1;
+		fact = 0;
+	}
+
 	return (0x100 - div) << 8 | fact << 2 | ps;
 }
 
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: ch341 garbage read with 5.5.x kernel
  2020-02-05  8:50     ` Johan Hovold
@ 2020-02-05 12:31       ` jakub nantl
  2020-02-05 12:35         ` Johan Hovold
  0 siblings, 1 reply; 6+ messages in thread
From: jakub nantl @ 2020-02-05 12:31 UTC (permalink / raw)
  To: Johan Hovold; +Cc: linux-usb

On 05. 02. 20 9:50, Johan Hovold wrote:
> Can you try the below patch which restores a component included in the
> first version of the new algorithm, but which I ultimately deemed
> redundant?

Hello Johan,

your patch has helped, thanks...

jn


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: ch341 garbage read with 5.5.x kernel
  2020-02-05 12:31       ` jakub nantl
@ 2020-02-05 12:35         ` Johan Hovold
  0 siblings, 0 replies; 6+ messages in thread
From: Johan Hovold @ 2020-02-05 12:35 UTC (permalink / raw)
  To: jakub nantl; +Cc: Johan Hovold, linux-usb

On Wed, Feb 05, 2020 at 01:31:04PM +0100, jakub nantl wrote:
> On 05. 02. 20 9:50, Johan Hovold wrote:
> > Can you try the below patch which restores a component included in the
> > first version of the new algorithm, but which I ultimately deemed
> > redundant?
> 
> Hello Johan,
> 
> your patch has helped, thanks...

Great, thanks for confirming. I'll see to that this gets fixed in stable
ASAP.

Where you able to determine which rate your Arduino actually uses for
115200 by the way?

Johan

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-02-05 12:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-04 22:02 ch341 garbage read with 5.5.x kernel jakub nantl
2020-02-05  7:43 ` Johan Hovold
2020-02-05  7:53   ` jakub nantl
2020-02-05  8:50     ` Johan Hovold
2020-02-05 12:31       ` jakub nantl
2020-02-05 12:35         ` Johan Hovold

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.