electrolama / llama-bsl

Python cross-platform script to upload firmware via the serial boot loader onto the CC13xx, CC2538 and CC26xx SoC.
BSD 3-Clause "New" or "Revised" License
10 stars 1 forks source link

Automatically start BSL mode on ITead SONOFF Zigbee 3.0 USB Dongle Plus stick adapters #11

Closed Hedda closed 1 year ago

Hedda commented 2 years ago

Nice with an option to automatically start BSL mode on ITead SONOFF Zigbee 3.0 USB Dongle Plus stick adapters?

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

ITead SONOFF Zigbee 3.0 USB Dongle Plus has a metal enclosure so currently the user need to remove screws to open it in order to access the BOOT button to activate BSL mode. Would be great if couyld activate BSL mode via software instead.

https://electrolama.com/radio-docs/bsl/#auto-bsl

At least automatic boot to bootloader is currently not working with the SONOFF Zigbee 3.0 USB Dongle Plus from ITead (and the main difference with it compared to zzh or zzhp is that the Sonoff USB Plus dongle is using CP2102N USB-to-UART bridge chip).

So today Auto BSL is not working in llama-bsl (or cc2538-bsl) with this Sonoff USB Plus dongle, but a modified uartLog.py script attached in Sonoff doc manages to automatically enter BSL mode without the user having to manually enter whether the script should use active high signals to enter bootloader or inverts the use of RTS and DTR to enter bootloader (i.e. both active low). So we at least know that the Sonoff USB Plus dongle hardware does support Auto BSL.

https://sonoff.tech/wp-content/uploads/2021/09/Zigbee-3.0-USB-dongle-plus-firmware-flashing-1-1.docx

Attatched: uartLog.zip

I have confirmed that running the attached uartLog.py script does indeed make ITead's Sonoff Zigbee 3.0 Plus adapter enter bootloader mode and after that I could flash it directly with llama-bsl.py -p COM5 -b 115200 -evw CC1352P2_CC2652P_launchpad_coordinator_20210708.hex without having open its enclosure and pressing the BRL button which I think is very convenient. The script also lists all active COM ports on MS Windows (tested on Windows 10) and have you enter the number of the COM port, but note that the script is hardcoded for Windows COM ports.


""" 测试mcu进入boot """

def setDTRState(serialPort, state):
    serialPort.setDTR(state)

def setRTSState(serialPort, state):
    serialPort.setRTS(state)
    # Work-around for adapters on Windows using the usbser.sys driver:
    # generate a dummy change to DTR so that the set-control-line-state
    # request is sent with the updated RTS state and the same DTR state
    # 为了绕过Windows系统下驱动的问题,同时更新 RTS 和 DTR状态
    serialPort.setDTR(serialPort.dtr)

def enterBoot(serialPort, delay=False):
    # delay is a workaround for bugs with the most common auto reset
    # circuit and Windows, if the EN pin on the dev board does not have
    # enough capacitance.
    # 如果开发板上EN管脚没有足够电容
    last_error = None

    # issue reset-to-bootloader:
    # RTS = either CH_PD/EN or nRESET (both active low = chip in reset
    # DTR = GPIO0 (active low = boot to flasher)
    #
    # DTR & RTS are active low signals,
    # ie True = pin @ 0V, False = pin @ VCC.
    setDTRState(serialPort, False)  # IO0=HIGH
    # setDTRState(serialPort, True)   # IO0=LOW
    setRTSState(serialPort, True)  # EN=LOW, chip in reset
    time.sleep(0.1)
    if delay:
        # Some chips are more likely to trigger the esp32r0
        # watchdog reset silicon bug if they're held with EN=LOW
        # for a longer period
        time.sleep(1.2)

    setDTRState(serialPort, True)  # IO0=LOW
    setRTSState(serialPort, False)  # EN=HIGH, chip out of reset
    if delay:
        # Sleep longer after reset.
        # This workaround only works on revision 0 ESP32 chips,
        # it exploits a silicon bug spurious watchdog reset.
        time.sleep(0.4)  # allow watchdog reset to occur
    time.sleep(1)

    setDTRState(serialPort, False)  # IO0=HIGH, done
    setRTSState(serialPort, False)
    time.sleep(1)

    return last_error
Hedda commented 2 years ago

Please see discussion in upstream cc2538-bsl by JelmerT here -> https://github.com/JelmerT/cc2538-bsl/issues/113

omerk commented 2 years ago

Hi @Hedda, I don't have that hardware so can't really test it here.

I'll happily merge a PR from someone who can, as long as it doesn't disturb the operation of all the other boards.

JelmerT commented 2 years ago

I added a PR that should support this HW https://github.com/JelmerT/cc2538-bsl/pull/114

Hedda commented 2 years ago

I added a PR that should support this HW JelmerT#114

@omerk Maybe adapter model USB discovery as discussed in https://github.com/electrolama/llama-bsl/issues/9 could be used to automatically enable that "sonoff" parameter?

FYI, ITead said they would add a unique Product Description String in future batches but there is, unfortunately, no news on it yet.

That is, got a reply from ITead that they will write to EEPROM in CP2102N to change the default “CP2102 USB to UART Bridge Controller” text in the Product Description String value field to "Sonoff Zigbee 3.0 USB Dongle Plus” in their next batch of dongles.

Hedda commented 2 years ago

FYI, ITead said they would add a unique Product Description String in future batches but there is, unfortunately, no news on it yet.

That is, got a reply from ITead that they will write to EEPROM in CP2102N to change the default “CP2102 USB to UART Bridge Controller” text in the Product Description String value field to “Sonoff Zigbee 3.0 USB Dongle Plus” in their next batch of dongles.

FYI, ITead now posted a script that should let users themselves update the product description string value to “Sonoff Zigbee 3.0 USB Dongle Plus” on already shipped Sonoff dongles to supposedly match the same description already written to CP2102N EEPROM the latest batch of their dongles:

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

Tested that program myself on a Windows 10 computer and it did not work for me (program just closed without any messages).

Sad that ITead did not instead choose to release a script tool based on this open-source cp210x-program by VCTLabs:

https://github.com/VCTLabs/cp210x-program

Again, the point of having a custom product description in USB-to-UART chip is to allow USB auto-discovery, like the example in:

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

Hedda commented 2 years ago

@omerk JelmerT has now merged https://github.com/JelmerT/cc2538-bsl/pull/114 so wondering if could update this patch from upstream cc2538-bsl to llama-bsl?

https://github.com/JelmerT/cc2538-bsl/pull/114

https://github.com/JelmerT/cc2538-bsl/pull/118

Off-topic; FYI, while merged there is not yet a new release version of cc2538-bsl on GitHub or PyPI so not easy for users to get it:

https://github.com/JelmerT/cc2538-bsl/issues/89

https://github.com/JelmerT/cc2538-bsl/issues/103

omerk commented 1 year ago

We don't have plans to add this on llama-bsl as we are focusing on customisations for Electrolama designs but happy to accept a PR if anyone were to raise it.