AminetAminet
Search:
85713 packages online
• About
• Recent
• Browse
• Search
• Upload
• Setup
• Services

driver/other/Poseidon_ch340.lha

Mirror:Random
Showing: m68k-amigaos icongeneric icon
No screenshot available
Short:WCH CH340/CH341 USB serial for Poseidon
Author:"Serkan" blasterreal at gmail.com
Uploader:Serkan <blasterreal gmail com>
Type:driver/other
Version:1.0
Architecture:m68k-amigaos >= 3.1
Date:2026-10-19
Requires:Poseidon 6.x (AROS backport), 68040 or Emu68
Distribution:Aminet
Download:driver/other/Poseidon_ch340.lha - View contents
Readme:driver/other/Poseidon_ch340.readme
Downloads:15

serialch340.class
=================

A Poseidon USB class for serial adapters built around the WCH CH340 and
CH341 chips - the cheap "USB-SERIAL CH340" cables and the USB port of many
Arduino, ESP8266 and ESP32 boards. It gives AmigaOS a serial.device
compatible device, serialch340.device, so terminal programs and anything
else that talks to a serial.device can use these adapters.


Requirements
------------

- Poseidon 6.x, the AROS-lineage backport of Poseidon for AmigaOS 3.x
  (https://github.com/rondoval/poseidon-backport). The class uses the
  Poseidon 6.x class interface; it does not work with the classic Poseidon
  4.x/5.x releases.
- AmigaOS 3.1 or later.
- The binary is built for the 68040 (as are the backport's own 68040
  builds). It was developed and tested on an A1200 with PiStorm/Emu68,
  AmigaOS 3.2.3, xhci.device and poseidon.library 6.1/6.2. To build for
  another CPU, see "Building" below.


Supported adapters
------------------

  1A86:7523  WCH CH340 (the common "USB-SERIAL CH340")
  1A86:7522  WCH CH340K
  1A86:5523  WCH CH341 in serial mode
  4348:5523  WCH CH341 (old vendor ID)
  2184:0057  GW Instek GDM-834x multimeter
  9986:7523  CH340 clone

Other CH34x adapters can be bound by hand in Trident (Devices, select the
adapter, force binding to serialch340.class).


Installation
------------

Double-click the Install icon in the Poseidon_ch340 drawer (it runs the
Install script through IconX), or from a Shell:

  CD Poseidon_ch340
  Execute Install

The script

- copies the class to SYS:Classes/USB/ (an existing one is kept as
  serialch340.class.old),
- asks whether to copy the tools chtest, chxfer and psdlog to C:,
- if the class is not yet known to Poseidon: asks you to unplug all
  CH340/CH341 adapters, then registers the class (C/chclass ADD) and saves
  the Poseidon prefs (C/chclass SAVE; the old prefs are kept as
  ENVARC:Sys/poseidon.prefs.bak),
- if the class is already registered: tells you to reboot to load the new
  version.

Unplug the adapters before registering: Poseidon suspends USB devices that
stay without a driver, and the class cannot take over an adapter that is
already suspended (see "Known problems").

By hand: with the adapters unplugged, copy Classes/USB/serialch340.class
to SYS:Classes/USB/, add it in Trident (Classes, Add) and press Save.

Then plug in the adapter. Trident's message log shows

     serialch340.class  Another serial killer 'USB2.0-Serial' at
                        serialch340.device unit 0!
     serialch340.class  CH34x chip version 31.

The first adapter is unit 0, the second unit 1, and so on - also when two
adapters have the same USB IDs. In a terminal program select the device
"serialch340.device" and the unit number.

Install the class in SYS:Classes/USB, so that it is there when an adapter
is plugged in. See "Known problems" for what happens when it is loaded
later by hand.


Features
--------

- Baud rates from 50 to 2,000,000; any rate in between, computed with the
  same divisor algorithm as the Linux ch341 driver.
- 5, 6, 7 or 8 data bits; parity none, odd, even, mark or space; 1 or 2
  stop bits.
- DTR and RTS are switched on while the unit is open and off when the
  adapter is set up.
- SDCMD_QUERY reports CTS, DSR, DCD and RI (read from the chip) and RTS,
  DTR, and the number of bytes waiting in the read buffer.
- SDCMD_BREAK sends a break of io_BrkTime microseconds.
- CMD_READ, CMD_WRITE (io_Length -1 = NUL terminated), CMD_CLEAR,
  CMD_RESET, CMD_FLUSH, CMD_START, CMD_STOP, SDCMD_SETPARAMS,
  NSCMD_DEVICEQUERY; SERF_EOFMODE with the termination array (taken over
  from the cp210x class, not tested with the CH340).
- Read-ahead buffering: data arriving while no read is pending is kept.
- Several adapters at the same time, one unit each.
- Unplugging the adapter while a unit is open does not crash anything:
  pending reads end with error -2 (IOERR_ABORTED). When the adapter is
  plugged in again, the same open unit continues to work.


Tested
------

All tests on an A1200 with PiStorm/Emu68 (68040), AmigaOS 3.2.3,
Poseidon 6.2 backport, with CH340 adapters (chip version 0x31).

  Binding at boot and hot plug ............................ OK
  Open/close, many times .................................. OK
  SETPARAMS 9600, 115200, 921600, 2000000 baud ............ OK
  Line status (QUERY), DTR/RTS on, BREAK .................. OK
  Loopback (TX wired to RX) at 9600/115200/921600 ......... OK, byte exact
  Reading an ESP32 that prints text at 9600 ............... OK
  Two adapters at once (units 0 and 1) .................... OK
  Unplug and replug while the unit is open and reading .... OK (see below)

Framing, with two adapters wired crosswise (TX to RX, both ways, GND):

  Same settings on both ends, text arrives intact:
    8N1, 7E1, 7O1, 8E1, 8O2, 8M1, 8S1, 7N2 ............... all OK

  Proof that the settings reach the chip: unit 0 sends "ABC" with 7 data
  bits, unit 1 receives as 8N1, so the parity bit appears as bit 7:

    sender   expected     received
    7M1      C1 C2 C3     C1 C2 C3
    7S1      41 42 43     41 42 43
    7E1      41 42 C3     41 42 C3
    7O1      C1 C2 43     C1 C2 43

Throughput: 1 MB of a pattern from one adapter to the other, every byte
checked, 8N1, measured with chxfer:

    baud       result                   bytes/s   line limit
    115200     no loss                    11533      11520
    460800     no loss                    45988      46080
    921600     no loss (both directions)  83500      92160
    1000000    no loss                    90068     100000
    1500000    no loss                   122112     150000
    2000000    ~180 bytes lost per MB    151000     200000

Up to 1.5 Mbaud nothing was lost. At 2 Mbaud the receive side tops out at
about 150 KB/s and, as the CH340 has no flow control, the chip drops what
it cannot deliver. Use 1.5 Mbaud or less for bulk transfers.


Known problems and limits
-------------------------

- Loading the class later onto a suspended adapter hangs.
  Poseidon 6.2 suspends USB devices that stay without a driver for a
  while. If serialch340.class is added by hand (e.g. from RAM:) after an
  adapter has been plugged in long enough to be suspended, the class binds,
  but its task hangs while the adapter is resumed, and only a reboot
  clears it. Both the implicit resume (by the first transfer) and an
  explicit psdResumeDevice() were tried; the cause lies on the resume path
  and was not found. With the class in SYS:Classes/USB this does not
  happen: the class binds when the adapter is enumerated, before Poseidon
  would suspend it.

- No flush of old data on open. The read-ahead keeps receiving while the
  unit is closed, and after a baud rate change the buffer still holds data
  received with the old settings. A program gets that data first. Read and
  discard what SDCMD_QUERY reports as waiting right after opening or after
  SETPARAMS if you do not want it. (Flushing in the driver was tried twice
  and stopped reception or hung on xhci.device, so it was left out.)

- Settings after replugging. When the adapter is unplugged and plugged in
  again while a unit is open, the unit continues, but the chip starts again
  at 9600 8N1 with DTR and RTS off. A program that notices a read error
  -2 should send SDCMD_SETPARAMS again.

- No hardware flow control. The CH340 has none; with SERF_7WIRE the driver
  only keeps DTR and RTS on. XON/XOFF is not handled by the driver either.

- DTR/RTS and development boards. Opening the unit switches DTR and RTS
  on. Boards with an auto-reset circuit (ESP32, ESP8266, many Arduinos) may
  reset when a unit is opened.

- A unit can be opened by one program at a time (SERF_SHARED is not
  supported).

- Removing the class at run time (e.g. with a tool that calls
  psdRemClass) and adding it again in the same session once led to a
  reboot when the adapter was replugged afterwards. Use the normal install
  and reboot to change versions.


Tools
-----

C/chtest - test a unit:

  chtest [UNIT n] [BAUD n] [TEXT "..."] [SECS n] [BREAK] [LOOP]
         [DATABITS 5-8] [PARITY N|E|O|M|S] [STOP 1|2] [HEX] [NOWRITE]
         [DEVICE name]

  Opens the unit, prints the line status, writes TEXT and prints what
  arrives for SECS seconds. With TX wired to RX this is a loopback test.
  LOOP keeps reading after errors (for unplug tests), HEX prints the
  received bytes in hex, NOWRITE only listens.

    chtest UNIT 0 BAUD 9600 SECS 5 NOWRITE

C/chxfer - bulk transfer test between two units wired TX to RX:

  chxfer [FROM n] [TO n] [BAUD n] [SIZE n] [CHUNK n] [DEVICE name]

  Sends SIZE bytes of a known pattern, checks every byte and prints the
  throughput and PASS or FAIL. After a lost byte all following bytes count
  as wrong; compare "sent" and "received" for the real loss.

    chxfer FROM 0 TO 1 BAUD 921600 SIZE 1048576

C/chclass - used by the Install script:

  chclass ADD <path>   add a class to the running Poseidon
  chclass LIST         list the loaded classes
  chclass SAVE         save the Poseidon prefs (ENVARC: and ENV:)

  There is no REMove on purpose; see "Known problems".

C/psdlog - Poseidon's message log without opening Trident:

  psdlog [n]    the last n messages (default 20)
  psdlog DEV    the USB devices, with their suspend state

Source/tests/ has the AmigaDOS scripts used for the framing tests
(match, mismatch).


Building
--------

The source is in Source/. The class is built inside the Poseidon backport
tree, which provides the class skeleton, the headers and the toolchain
container (ghcr.io/rondoval/amiga-build-container). build.sh clones the
backport (revision 0dc2b73), adds the class and builds it with docker:

  ./build.sh            class and tools
  ./build.sh class      class only

The class is built for 68040 with -DM68K_CPU=68040; change M68K_CPU in
build.sh for 68020 or 68060.


History
-------

1.0 (19.10.2026)  First release.


Credits and license
-------------------

serialch340.class is derived from serialcp210x.class by Chris Hodges, part
of Poseidon, as found in the AROS backport by rondoval. The CH340 protocol
follows the Linux ch341 USB serial driver. Changes for the CH340, the
resume fix, the OpenDevice and stale unit fixes, the tools and the tests:
Serkan, 2026.

Distributed under the AROS Public License 1.1, see LICENSE. The source
code is included.


Contents of driver/other/Poseidon_ch340.lha
 PERMSSN    UID  GID    PACKED    SIZE  RATIO METHOD CRC     STAMP          NAME
---------- ----------- ------- ------- ------ ---------- ------------ -------------
drwxrwxrwx     0/0           0       0 ****** -lhd- 0000 Oct 19 09:04 Poseidon_ch340/
drwxrwxrwx     0/0           0       0 ****** -lhd- 0000 Oct 19 09:04 Poseidon_ch340/C/
-rwxrwxrwx     0/0        2417    4584  52.7% -lh5- 05df Oct 19 09:04 Poseidon_ch340/C/chclass
-rwxrwxrwx     0/0        3604    6768  53.3% -lh5- 1eda Oct 19 09:04 Poseidon_ch340/C/chtest
-rwxrwxrwx     0/0        3984    7472  53.3% -lh5- e0aa Oct 19 09:04 Poseidon_ch340/C/chxfer
-rwxrwxrwx     0/0        3348    6252  53.6% -lh5- 66c0 Oct 19 09:04 Poseidon_ch340/C/psdlog
drwxrwxrwx     0/0           0       0 ****** -lhd- 0000 Oct 19 09:04 Poseidon_ch340/Classes/
drwxrwxrwx     0/0           0       0 ****** -lhd- 0000 Oct 19 09:04 Poseidon_ch340/Classes/USB/
-rwxrwxrwx     0/0        4590    8488  54.1% -lh5- 8ba5 Oct 19 09:04 Poseidon_ch340/Classes/USB/serialch340.class
-rwxrwxrwx     0/0         893    2407  37.1% -lh5- 033a Oct 19 09:04 Poseidon_ch340/Install
-rwxrwxrwx     0/0         886    9649   9.2% -lh5- aa72 Oct 19 09:04 Poseidon_ch340/Install.info
-rwxrwxrwx     0/0        7915   24679  32.1% -lh5- d3e5 Oct 19 09:04 Poseidon_ch340/LICENSE
-rwxrwxrwx     0/0        4668   10670  43.7% -lh5- 3392 Oct 19 09:04 Poseidon_ch340/Poseidon_ch340.readme
drwxrwxrwx     0/0           0       0 ****** -lhd- 0000 Oct 19 09:04 Poseidon_ch340/Source/
-rwxrwxrwx     0/0        1975    3876  51.0% -lh5- 9031 Oct 19 09:04 Poseidon_ch340/Source/README.md
-rwxrwxrwx     0/0        1232    2562  48.1% -lh5- 5491 Oct 19 09:04 Poseidon_ch340/Source/build.sh
-rwxrwxrwx     0/0         926    2179  42.5% -lh5- 5fa8 Oct 19 09:04 Poseidon_ch340/Source/chclass.c
-rwxrwxrwx     0/0        2331    6862  34.0% -lh5- 9bf5 Oct 19 09:04 Poseidon_ch340/Source/chtest.c
-rwxrwxrwx     0/0        2616    8021  32.6% -lh5- 0709 Oct 19 09:04 Poseidon_ch340/Source/chxfer.c
-rwxrwxrwx     0/0         869    1924  45.2% -lh5- 4a4c Oct 19 09:04 Poseidon_ch340/Source/psdlog.c
drwxrwxrwx     0/0           0       0 ****** -lhd- 0000 Oct 19 09:04 Poseidon_ch340/Source/src/
-rwxrwxrwx     0/0         324     557  58.2% -lh5- 7713 Oct 19 09:04 Poseidon_ch340/Source/src/CMakeLists.txt
-rwxrwxrwx     0/0        3469   14356  24.2% -lh5- 36e2 Oct 19 09:04 Poseidon_ch340/Source/src/dev.c
-rwxrwxrwx     0/0         800    2083  38.4% -lh5- c79e Oct 19 09:04 Poseidon_ch340/Source/src/dev.h
-rwxrwxrwx     0/0        8027   34017  23.6% -lh5- 2fa1 Oct 19 09:04 Poseidon_ch340/Source/src/serialch340.class.c
-rwxrwxrwx     0/0         543    1305  41.6% -lh5- bb94 Oct 19 09:04 Poseidon_ch340/Source/src/serialch340.class.h
-rwxrwxrwx     0/0        1703    5192  32.8% -lh5- 8b97 Oct 19 09:04 Poseidon_ch340/Source/src/serialch340.h
drwxrwxrwx     0/0           0       0 ****** -lhd- 0000 Oct 19 09:04 Poseidon_ch340/Source/tests/
-rwxrwxrwx     0/0         263     453  58.1% -lh5- 7f51 Oct 19 09:04 Poseidon_ch340/Source/tests/match.amigados
-rwxrwxrwx     0/0         286     457  62.6% -lh5- 71bb Oct 19 09:04 Poseidon_ch340/Source/tests/mismatch.amigados
drwxrwxrwx     0/0           0       0 ****** -lhd- 0000 Oct 19 09:04 Poseidon_ch340/Source/tools/
-rwxrwxrwx     0/0        3064    8041  38.1% -lh5- 0573 Oct 19 09:04 Poseidon_ch340/Source/tools/mkicon.py
---------- ----------- ------- ------- ------ ---------- ------------ -------------
 Total        32 files   60733  172854  35.1%            Oct 19 23:23
Page generated in 0.03 seconds
Aminet © 1992-2024 Urban Müller and the Aminet team. Aminet contact address: <aminetaminet net>