From 6832fb933fae1d152f1c605c4645bdd073195853 Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Tue, 6 Jun 2023 22:35:10 +0300 Subject: [PATCH 1/7] util: add sn32 dfu udev rules --- util/udev/50-qmk.rules | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/util/udev/50-qmk.rules b/util/udev/50-qmk.rules index 1cc19b41428c..cc7bb7544398 100644 --- a/util/udev/50-qmk.rules +++ b/util/udev/50-qmk.rules @@ -84,3 +84,8 @@ SUBSYSTEMS=="usb", ATTRS{idVendor}=="28e9", ATTRS{idProduct}=="0189", TAG+="uacc # WB32 DFU SUBSYSTEMS=="usb", ATTRS{idVendor}=="342d", ATTRS{idProduct}=="dfa0", TAG+="uaccess" + +# SN32 DFU +SUBSYSTEMS=="usb", ATTRS{idVendor}=="0c45", ATTRS{idProduct}=="7010", TAG+="uaccess" +SUBSYSTEMS=="usb", ATTRS{idVendor}=="0c45", ATTRS{idProduct}=="7040", TAG+="uaccess" +SUBSYSTEMS=="usb", ATTRS{idVendor}=="0c45", ATTRS{idProduct}=="7900", TAG+="uaccess" From 325fbd55eac35f9b7ceee358b60d43f727357cef Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Tue, 6 Jun 2023 23:00:03 +0300 Subject: [PATCH 2/7] add sn32-dfu to flash utils --- lib/python/qmk/constants.py | 5 +++++ lib/python/qmk/flashers.py | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/python/qmk/constants.py b/lib/python/qmk/constants.py index b5bc0ba68e11..fd2aa2f23010 100644 --- a/lib/python/qmk/constants.py +++ b/lib/python/qmk/constants.py @@ -123,6 +123,11 @@ 'hid-bootloader': { ("03eb", "2067"), # QMK HID ("16c0", "0478") # PJRC halfkay + }, + 'sn32-dfu': { + ("0c45", "7010"), # SN32F260 + ("0c45", "7040"), # SN32F240B + ("0c45", "7900") # SN32F240 } } diff --git a/lib/python/qmk/flashers.py b/lib/python/qmk/flashers.py index 9ecb5e4b9c8f..77452b92fa0a 100644 --- a/lib/python/qmk/flashers.py +++ b/lib/python/qmk/flashers.py @@ -96,7 +96,7 @@ def _find_bootloader(): details = 'halfkay' else: details = 'qmk-hid' - elif bl in {'apm32-dfu', 'gd32v-dfu', 'kiibohd', 'stm32-dfu'}: + elif bl in {'apm32-dfu', 'gd32v-dfu', 'kiibohd', 'stm32-dfu', 'sn32-dfu'}: details = (vid, pid) else: details = None @@ -205,6 +205,14 @@ def _flash_uf2(file): cli.run(['util/uf2conv.py', '--deploy', file], capture_output=False) +def _flash_sonixflasher(details, file): + # SN32F260 + if details[0] == '0c45' and details[1] == '7010': + cli.run(['sonixflasher', '--vidpid', f'{details[0]}:{details[1]}', '--offset', '0x200', '--file', file], capture_output=False) + else: + cli.run(['sonixflasher', '--vidpid', f'{details[0]}:{details[1]}', '--file', file], capture_output=False) + + def flasher(mcu, file): bl, details = _find_bootloader() # Add a small sleep to avoid race conditions @@ -234,6 +242,8 @@ def flasher(mcu, file): _flash_mdloader(file) elif bl == '_uf2_compatible_': _flash_uf2(file) + elif bl == 'sn32-dfu': + _flash_sonixflasher(details, file) else: return (True, "Known bootloader found but flashing not currently supported!") From 0e61abb377261a2a3d8cb710a29b24c377a24cda Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Tue, 6 Jun 2023 23:15:15 +0300 Subject: [PATCH 3/7] util: add sonixflasher on qmk install --- util/install/linux_shared.sh | 13 +++++++++++++ util/qmk_install.sh | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/util/install/linux_shared.sh b/util/install/linux_shared.sh index e060f425f579..ad29e72a69e5 100755 --- a/util/install/linux_shared.sh +++ b/util/install/linux_shared.sh @@ -11,3 +11,16 @@ _qmk_install_bootloadhid() { popd > /dev/null fi } + +# No distros package sonixflasher yet +_qmk_install_sonixflasher() { + if ! command -v sonixflasher > /dev/null; then + latest_tag=$(curl -s https://api.github.com/repos/SonixQMK/SonixFlasherC/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') + wget https://github.com/SonixQMK/SonixFlasherC/archive/refs/tags/${latest_tag}.tar.gz -O - | tar -xz -C /tmp + pushd /tmp/SonixFlasherC-${latest_tag}/ > /dev/null + if make; then + sudo cp sonixflasher /usr/local/bin + fi + popd > /dev/null + fi +} \ No newline at end of file diff --git a/util/qmk_install.sh b/util/qmk_install.sh index 3f49bd255a7e..510724964a95 100755 --- a/util/qmk_install.sh +++ b/util/qmk_install.sh @@ -73,3 +73,7 @@ _qmk_install if type _qmk_install_bootloadhid &>/dev/null; then _qmk_install_bootloadhid fi + +if type _qmk_install_sonixflasher &>/dev/null; then + _qmk_install_sonixflasher +fi From 46be83333fc0da66252c2cb7759a80076091f9be Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Mon, 19 Jun 2023 23:39:54 +0300 Subject: [PATCH 4/7] MSYS2 install: add sonixflasher for sn32 targets --- util/install/msys2.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/util/install/msys2.sh b/util/install/msys2.sh index fa422023ab60..d5aa2559474a 100755 --- a/util/install/msys2.sh +++ b/util/install/msys2.sh @@ -12,7 +12,8 @@ _qmk_install() { base-devel: toolchain:x clang:x python-qmk:x hidapi:x \ avr-binutils:x avr-gcc:x avr-libc:x \ arm-none-eabi-binutils:x arm-none-eabi-gcc:x arm-none-eabi-newlib:x \ - avrdude:x bootloadhid:x dfu-programmer:x dfu-util:x hid-bootloader-cli:x mdloader:x teensy-loader-cli:x wb32-dfu-updater:x + avrdude:x bootloadhid:x dfu-programmer:x dfu-util:x hid-bootloader-cli:x mdloader:x \ + teensy-loader-cli:x wb32-dfu-updater:x sonixflasher:x _qmk_install_drivers } From 6c0fda9712243526531206b73636e1021bfbe21a Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Wed, 18 Sep 2024 19:49:45 +0300 Subject: [PATCH 5/7] macos install: add sonixflasher for sn32 targets --- util/install/macos.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/util/install/macos.sh b/util/install/macos.sh index a1b79fe86839..c9da2ee4d9bd 100755 --- a/util/install/macos.sh +++ b/util/install/macos.sh @@ -29,3 +29,9 @@ _qmk_install() { python3 -m pip install -r $QMK_FIRMWARE_DIR/requirements.txt } + +_qmk_install_sonixflasher() { + echo "Installing sonixflasher" + + brew install sonixqmk/sonixqmk/sonixflasher +} \ No newline at end of file From 0c87bdd80ef2b1a5ffd3525f79bbf1075fdadf81 Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Tue, 13 Jun 2023 12:14:43 +0300 Subject: [PATCH 6/7] sn32: add to flashing docs --- docs/flashing.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docs/flashing.md b/docs/flashing.md index 798331eb23d1..45398bd61957 100644 --- a/docs/flashing.md +++ b/docs/flashing.md @@ -465,3 +465,45 @@ CLI Flashing sequence: 4. Wait for the keyboard to become available 1: This works only if the controller has been flashed with QMK Firmware with `RP2040_BOOTLOADER_DOUBLE_TAP_RESET` defined. + +## SN32 DFU + +All SN32 MCUs, except for 2601 come preloaded with a factory bootloader that cannot be modified nor deleted. + +To ensure compatibility with the SN32-DFU bootloader, make sure this block is present in your `rules.mk` : + +```make +# Bootloader selection +BOOTLOADER = sn32-dfu +``` + +Compatible flashers: + +* [SonixQMKToolbox](https://github.com/SonixQMK/qmk_toolbox/releases) (recommended GUI) +* [sonixflasher](https://github.com/SonixQMK/SonixFlasherC/releases) / `:flash` target in QMK (recommended command line) +* [sonix-flasher](https://github.com/SonixQMK/sonix-flasher/releases) (old GUI - known to cause issues) + ``` + sonixflasher --vidpid 0c45:7040 -f + ``` +1: 260 series of chips have part of the SN32-DFU bootloader in userspace and therefore must be guarded to avoid bricking. Install the [sonix-bootloader](https://github.com/SonixQMK/sonix-keyboard-bootloader) before flashing the firmware +``` +sonixflasher --vidpid 0c45:7010 -j -f + +``` +as a one-shot operation, then flash the firmware with an offset `0x200` +``` +sonixflasher --vidpid 0c45:7010 -o 0x200 -f + +``` + +If using `$ qmk flash` to flash a firmware, the offset is automatically applied if needed. + +Flashing sequence: + +1. Enter the bootloader using any of the following methods: + * Tap the `QK_BOOT` keycode + * If a reset circuit is present, tap the `RESET` button on the PCB; some boards may also have a toggle switch that must be flipped + * Otherwise, you need to bridge `BOOT` to GND (via `BOOT` button or jumper), short `RESET` to GND (via `RESET` button, jumper or by unplugging and replugging USB), and then let go of the `BOOT` bridge +2. Wait for the OS to detect the device +3. Flash a .bin file +4. Wait for the keyboard to become available \ No newline at end of file From 4006f8c36d2a151116a175a66ec7d3e7d02cc552 Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Wed, 18 Sep 2024 21:00:54 +0300 Subject: [PATCH 7/7] sn32: add direct `qmk flash` support build and flash in one line --- platforms/chibios/bootloader.mk | 12 ++++++++++++ platforms/chibios/flash.mk | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/platforms/chibios/bootloader.mk b/platforms/chibios/bootloader.mk index 4871e084475a..e8b9ec94f4ef 100644 --- a/platforms/chibios/bootloader.mk +++ b/platforms/chibios/bootloader.mk @@ -123,6 +123,18 @@ endif ifeq ($(strip $(BOOTLOADER)), sn32-dfu) OPT_DEFS += -DBOOTLOADER_SN32_DFU BOOTLOADER_TYPE = sn32_dfu + + # Options to pass to sonixflasher when flashing + ifeq ($(strip $(MCU_SERIES)), SN32F240) + DFU_ARGS ?= -v 0c45/7900 + endif + ifeq ($(strip $(MCU_SERIES)), SN32F240B) + DFU_ARGS ?= -v 0c45/7040 + endif + ifeq ($(strip $(MCU_SERIES)), SN32F260) + DFU_ARGS ?= -v 0c45/7010 -o 0x200 + endif + endif ifeq ($(strip $(BOOTLOADER_TYPE)),) diff --git a/platforms/chibios/flash.mk b/platforms/chibios/flash.mk index 525f177f9ebb..9fbbffa28196 100644 --- a/platforms/chibios/flash.mk +++ b/platforms/chibios/flash.mk @@ -38,6 +38,12 @@ define EXEC_WB32_DFU_UPDATER $(WB32_DFU_UPDATER) -D $(BUILD_DIR)/$(TARGET).bin && $(WB32_DFU_UPDATER) -R endef +SONIX_FLASHER ?= sonixflasher + +define EXEC_SONIX_FLASHER + $(SONIX_FLASHER) $(DFU_ARGS) -f $(BUILD_DIR)/$(TARGET).bin +endef + dfu-util: $(BUILD_DIR)/$(TARGET).bin cpfirmware sizeafter $(call EXEC_DFU_UTIL) @@ -115,6 +121,8 @@ else ifeq ($(strip $(MCU_FAMILY)),WB32) $(UNSYNC_OUTPUT_CMD) && $(call EXEC_WB32_DFU_UPDATER) else ifeq ($(strip $(MCU_FAMILY)),GD32V) $(UNSYNC_OUTPUT_CMD) && $(call EXEC_DFU_UTIL) +else ifeq ($(strip $(MCU_FAMILY)),SN32) + $(UNSYNC_OUTPUT_CMD) && $(call EXEC_SONIX_FLASHER) else $(PRINT_OK); $(SILENT) || printf "$(MSG_FLASH_BOOTLOADER)" endif