Coder Social home page Coder Social logo

vctlabs / cp210x-program Goto Github PK

View Code? Open in Web Editor NEW
49.0 6.0 21.0 188 KB

EEPROM tool for Silabs CP210x USB-Serial adapter

License: GNU Lesser General Public License v2.1

Python 66.05% Shell 2.25% Makefile 1.20% C 30.50%
usb serial-communication cp210x

cp210x-program's Introduction

cp210x-program

CI Status Wheel Status Release Status Pylint Status

GitHub tag License Python Pylint score

pre-commit

The goal of this library is to provide access to the EEPROM of an Silabs CP210x under Linux.

Warning

THE LEGACY VERSION OF cp210x-program IS NOT FULLY TESTED, AND MAY RENDER YOUR CP210x USELESS OR DESTROY IT. Be aware that the current (legacy) version was only tested on the original CP2102 part. Similarly, the cp2102N programmer in ext/badge is only "lightly" tested here. Be warned...

Quick Start

The CP210x is a series of USB-to-serial chip used in a lot of USB devices (similar to FTDIs and PL2303). Certain CP210x devices have an EEPROM on the chip which can be programmed via USB, while others only have the OTP EPROM (which cannot be reprogrammed; see Model notes). Silabs provides various source code examples for Windows and Linux, and multiple drivers for Windows.

The original cp210x-program uses results from monitoring the USB bus when the windows library programs the CP210x device. The windows library was not disassembled for the original protocol analysis.

Dependencies

  • Python >= 3
  • PyUSB

For the external cp2102N tool:

  • GNU or clang toolchain
  • make
  • libusb-1.0

Since libusb is available on most Linux, Mac OS X and FreeBSD cp210x-program should run flawlessly on these platforms. Currently it is tested in Github CI on Linux (Ubuntu), MacOS, and Windows (note the latter relies on the base GNU libraries and tools installed on the Github CI runners).

If cp210x-program should run as non-root user, add the udev rule found in doc/45-cp210x-programming.rules to /etc/udev/rules.d. When devices with already programmed IDs are reprogrammed at this IDs to 45-cp210x-programming.rules.

Install deps on Ubuntu:

$ sudo apt install python3-usb

Usage

Read EEPROM content into hexfile:

$ cp210x-program --read-cp210x -f eeprom-content.hex

Show EEPROM content from device 002 on bus 001:

$ cp210x-program --read-cp210x -m 001/002

Hint: The bus and device number can be queried using the `lsusb -t` command, which is available on most linux distributions.

Write some data to device with vendor id 0x10C4 and product id 0xEA62:

$ cp210x-program --write-cp210x -m 10C4:EA62 \
               --set-product-string="Product String" \
               --set-max-power=100 \
               --set-bus-powered=no

Write default data to device:

$ cp210x-program --write-cp210x -F testdata/cp2102-orig.hex

This is for example required when the baud rate table is corrupted and the CP210x always uses 500kBit/sec as baudrate.

TODO

  • (re)Test on CP2102 and CP2103 (legacy parts)
  • read config blob from CP2102N (new part)

Dev tools

Local tool dependencies to aid in development; install both tools for maximum enjoyment.

Tox

As long as you have git and at least Python 3.6, then you can install and use tox. After cloning the repository, you can run the repo checks with the tox command. It will build a virtual python environment for each installed version of python with all the python dependencies and run the specified commands, eg:

$ git clone https://github.com/VCTLabs/cp210x-program
$ cd cp210x-program/
$ tox -e py

The above will run the default test commands using the (local) default Python version. To specify the Python version and host OS type, run something like:

$ tox -e py39-linux

To build and check the Python package, run:

$ tox -e deploy,check

Full list of additional tox commands:

  • tox -e deploy will build the python packages and run package checks
  • tox -e check will install the wheel package from above and run the script
  • tox -e lint will run pylint (somewhat less permissive than PEP8/flake8 checks)
  • tox -e readhex will install the wheel package and try to read from a legacy device
  • tox -e read will read the bulk USB descriptors from a device with SiLabs vendor ID
  • tox -e badge will build the external cp2102N tool => /ext/badge/bin/
  • tox -e clean will clean the above bin directory

For the tox -e badge command, you may want to set the CC variable either in your shell environment or on the command-line, eg:

$ CC=gcc tox -e badge

Pre-commit

This repo is now pre-commit enabled for python/rst source and file-type linting. The checks run automatically on commit and will fail the commit (if not clean) and perform simple file corrections. If the mypy check fails on commit, you must first fix any fatal errors for the commit to succeed. That said, pre-commit does nothing if you don't install it first (both the program itself and the hooks in your local repository copy).

You will need to install pre-commit before contributing any changes; installing it using your system's package manager is recommended, otherwise install with pip into your usual virtual environment using something like:

$ sudo emerge pre-commit  --or--
$ pip install pre-commit

then install it into the repo you just cloned:

$ git clone https://github.com/VCTLabs/cp210x-program
$ cd cp210x-program/
$ pre-commit install

It's usually a good idea to update the hooks to the latest version:

$ pre-commit autoupdate

Most (but not all) of the pre-commit checks will make corrections for you, however, some will only report errors, so these you will need to correct manually.

Automatic-fix checks include ffffff, isort, autoflake, the json/yaml/xml format checks, and the miscellaneous file fixers. If any of these fail, you can review the changes with git diff and just add them to your commit and continue.

If any of the mypy or rst source checks fail, you will get a report, and you must fix any errors before you can continue adding/committing.

To see a "replay" of any rst check errors, run:

$ pre-commit run rst-backticks -a
$ pre-commit run rst-directive-colons -a
$ pre-commit run rst-inline-touching-normal -a

To run all pre-commit checks manually, try:

$ pre-commit run -a

Device Info

Updated vendor links and basic model info; see the individual product descriptions and data sheets for details.

Model notes

Taken from the respective device Data Sheets:

  • CP2101 - EEPROM (512 byte) may work
  • CP2102 - EEPROM (1024 byte) should work
  • CP2103 - EEPROM (1024 byte) should work
  • CP2104 - EPROM only (1024 byte, not re-programmable)
  • CP2105 - EPROM only (296 byte, not re-programmable)
  • CP2109 - EPROM only (1024 byte, not re-programmable)
  • CP2102N - EEPROM (960 byte) will not work with legacy cp210x-program

The following table from AN721 shows the default SiLabs USB device IDs; note third-party manufacturers often do not reprogram with their own vendor/product IDs.

CP120x device IDs

Links

License

The python package 'cp210x' and the python script 'cp210x-program' are provided under the terms of the GNU LGPL. See LICENSE. Otherwise, anything under the ext directory tree has its own license/copyrights.

Current external sources:

cp210x-program's People

Contributors

hedda avatar hramrach avatar johoelzl avatar jpewdev avatar laf0rge avatar sarnold avatar sjlc avatar twischer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

cp210x-program's Issues

Make a new release

With the conversion to python3 and even some basic tests it seems to be the perfect time for tagging a new version of cp210x-program.

Fails to read ini config files

./cp210x-program -I testdata/cp2102-orig.ini --write-cp210x

Traceback (most recent call last):
File "/home/timo/Beschreibungen/HOWTOs/usb2serial/cp210x-program/./cp210x-program", line 239, in
main()
File "/home/timo/Beschreibungen/HOWTOs/usb2serial/cp210x-program/./cp210x-program", line 235, in main
options.action(options)
File "/home/timo/Beschreibungen/HOWTOs/usb2serial/cp210x-program/./cp210x-program", line 169, in write_cp210x
values = valuefile.read_file(input_file(options.ini_input))
File "/home/timo/Beschreibungen/HOWTOs/usb2serial/cp210x-program/cp210x/valuefile.py", line 87, in read_file
cp.readfp(fp)
File "/usr/lib/python3.9/configparser.py", line 763, in readfp
self.read_file(fp, source=filename)
File "/usr/lib/python3.9/configparser.py", line 718, in read_file
self._read(f, source)
File "/usr/lib/python3.9/configparser.py", line 1093, in _read
raise DuplicateOptionError(sectname, optname,
configparser.DuplicateOptionError: While reading from 'testdata/cp2102-orig.ini' [line 15]: option '1500000' in section 'baudrate table' already exists

Add .py file extenstion to cp210x-program so rename it to cp210x-program.py

Please consider adding .py file extension to cp210x-program so rename it from "p210x-program" to "cp210x-program.py".

Having a .py file extension on all Python programs at least makes running them more convenient on Windows OS platforms.

(I am not an Apple computer user but I believe that the same argument is also valid for Mac OS operating systems too?).

Should not "PY" be the preferred standard file format and file extension name on all Python script programs?

https://fileinfo.com/extension/py

https://www.tutorialspoint.com/What-do-the-python-file-extensions-pyc-pyd-pyo-stand-for

py: The input source code that you've written.

.pyc: The compiled bytecode. If you import a module, python will build a *.pyc file that contains the bytecode to make importing it again later easier (and faster).

.pyo: A *.pyc file that was created while optimizations (-O) was on.

.pyd: A windows dll file for Python. http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll

Confirm working or not writing new product string to CP2102N on “ITead Sonoff Zigbee 3.0 USB Dongle Plus” adapter?

Can anyone confirm if cp210x-program works for writing product string to CP2102N on “ITead Sonoff Zigbee 3.0 USB Dongle Plus”?

https://itead.cc/product/sonoff-zigbee-3-0-usb-dongle-plus/

https://sonoff.tech/product/diy-smart-switch/sonoff-dongle-plus/

ITead missed writing a custom product description string to CP2102N EEPROM on in their first few batches of ITead Sonoff Zigbee 3.0 USB Dongle Plus adapter so they were shipped without the custom product description that users want and need as a unique identity for automatic discovery in application software.

The latest batch has however been confirmed to have “Sonoff Zigbee 3.0 USB Dongle Plus” (without underscores) already written as Product Description String value to CP2102N EEPROM, and they also added "ITead" as manufacturer, (Note! Not VID or PID).

ITead has released a script that can supposedly write the same “Sonoff Zigbee 3.0 USB Dongle Plus” text as Product Description String value to CP2102N EEPROM to earlier shipped adapters as is now written to the later batches of this adapter but unfortunately closed source and no one has yet confirmed that tool working, and regardless I think many users would be hesitant to run such closed source program on their Windows computer:

https://www.facebook.com/SONOFF.official/posts/2927251720919807

The main reason why want this product string is written is so the USB adapter can be automatically discovered by Home Assistant OS. There is uses the product description string value as a unique identifier in an auto-detection feature for a Zigbee integration:

https://www.home-assistant.io/integrations/zha/#discovery-via-usb-or-zeroconf

https://www.home-assistant.io/integrations/usb/

https://community.home-assistant.io/t/community-help-wanted-to-whitelist-all-compatible-zigbee-and-z-wave-usb-adapters-for-automatic-discovery-in-home-assistant-os/344412

FYI, more information about using that ITead Sonoff Zigbee 3.0 USB Dongle Plus adapter in Home Assistant's ZHA integration here:

https://community.home-assistant.io/t/sonoff-zigbee-3-0-usb-dongle-plus-by-itead-is-based-on-texas-instruments-cc2652p-can-be-pre-ordered-for-10-99/340705/

is doc wrong?

<h3>Read EEPROM content into hexfile:</h3>
<pre>$ cp210x-program --read-cp210x -F eeprom-content.hex

Is this correct? I think it should be in this way (because -F reads from file and -f writes to file):

<h3>Read EEPROM content into hexfile:</h3>
<pre>$ cp210x-program --read-cp210x -f eeprom-content.hex

I see this error in files README and doc/index.html.

Readme should advise on how to distinguish CP210{1..4}

First, thanks for your effort!
The tables with the vendor and product IDs all list CP210{1..4} the same, so the readme should advise on how to distinguish them. Even a negative acknowledgement like

There's currently no known way to distinguish them via software, so you have to rely on the seller's product descriptions and packaging.

would help the reader save time in research.

usb.core.NoBackendError: No backend available

running:
./venv/bin/cp210x-program.py
gets:

Traceback (most recent call last):
  File "/root/homebridge/./venv/bin/cp210x-program.py", line 247, in <module>
    main()
  File "/root/homebridge/./venv/bin/cp210x-program.py", line 243, in main
    options.action(options)
  File "/root/homebridge/./venv/bin/cp210x-program.py", line 139, in read_cp210x
    usbdev = find_device(options.match)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/homebridge/./venv/bin/cp210x-program.py", line 133, in find_device
    for dev in cp210x.Cp210xProgrammer.list_devices(usb_patterns):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/homebridge/venv/lib/python3.11/site-packages/cp210x/cp210x.py", line 196, in list_devices
    return usb.core.find(find_all=True, custom_match=Cp210xMatch(patterns))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/homebridge/venv/lib/python3.11/site-packages/usb/core.py", line 1309, in find
    raise NoBackendError('No backend available')
usb.core.NoBackendError: No backend available

But there is:

lsusb -t
/:  Bus 001.Port 001: Dev 001, Class=root_hub, Driver=xhci-hcd/2p, 480M
/:  Bus 002.Port 001: Dev 001, Class=root_hub, Driver=xhci-hcd/1p, 5000M
/:  Bus 003.Port 001: Dev 001, Class=root_hub, Driver=xhci-hcd/2p, 480M
    |__ Port 001: Dev 009, If 0, Class=Vendor Specific Class, Driver=[none], 480M
    |__ Port 001: Dev 009, If 1, Class=Vendor Specific Class, Driver=[none], 480M
    |__ Port 002: Dev 008, If 0, Class=Vendor Specific Class, Driver=cp210x, 12M.       <<<<<<<<<<<<<<<<
/:  Bus 004.Port 001: Dev 001, Class=root_hub, Driver=xhci-hcd/1p, 5000M

Pipe Error 32?

I am getting a pipe error (32) when I execute cp210x-program. According to both the manufacturer and lsusb, the device is a CP2102. cp210x-program works perfectly with other adapters.

ssokol@fbintel:~/cp210x-program$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 8087:0a2a Intel Corp.
Bus 001 Device 005: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP2102/CP2109 UART Bridge Controller [CP210x family]
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
ssokol@fbintel:~/cp210x-program$ sudo ./cp210x-program
[Errno 32] Pipe error
ssokol@fbintel:~/cp210x-program$

Any suggestions or thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.