logo elektroda
logo elektroda
X
logo elektroda

Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs

p.kaczmarek2 18798 428

TL;DR LABEL_AI_GENERATED

  • BK7231GUIFlashTool v98+ adds CH341-based SPI flashing and recovery for Beken BK7231/BK7252 chips, including bricked devices with overwritten bootloaders.
  • The tool uses CH341 DLL C# bindings to toggle CEN via D2, reset the chip, send 0xD2, then flash Beken chips like generic SPI memory.
  • Connections use P20, P21, P22, and P23 for SCK, CSN, SI, and SO, with CEN wired to D2 and the CH341 jumper set to I2C mode.
  • A sample read identifies JEDEC ID FF-EF-40-16 and a 4096 KB flash, and the same tool can Read, Write, Verify, and Erase firmware.
  • If CH341 is not detected or CEN toggle fails, check driver configuration, jumper position, SPI wiring, and power before trying Beken SPI mode again.
AI summary based on the discussion. May contain errors.
ADVERTISEMENT
Listen:
  • ADVERTISEMENT
  • Shared XRadio flasher hierarchy with range erase

    #332 21926675
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    divadiow wrote:
    XR refactor


    Refactored XRadio support into a shared flasher hierarchy for XR806, XR809, and XR872/XF16: https://github.com/openshwprojects/BK7231GUIFlashTool/pull/140

    -no longer erases whole chip before write - 64kb/4kb range erase to cover size of image to be flashed
    -full erase still does whole chip command
    -OBK config read/write

    tested XR806, XF16 and XR809.

    Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specsBeken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs
  • #333 21926686
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14814
    Help: 659
    Rate: 12986
    So you basically made it more DRY? How many lines of code saved?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • Refactoring saved about 2,035 lines

    #334 21926691
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    Don't Repeat Yourself

    I didn't know what that was.

    p.kaczmarek2 wrote:
    How many lines of code saved?

    no idea!

    Added after 2 [minutes]:

    1,550 lines added
    3,585 lines removed
    Net saving: about 2,035 lines

    Code: Text
    Log in, to see the code
  • #335 21926701
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14814
    Help: 659
    Rate: 12986
    Sounds like a very good refactor, but are you 100% sure that no issues have been introduced? I don't have XR at hand, however I may have one XR809 from the start of my journey, and some TRV thermostat XR as well. Can you double check before we merge it?

    @insmod can you also review this PR?
    Helpful post? Buy me a coffee.
  • #336 21926791
    insmod
    Level 31  
    Posts: 1433
    Help: 168
    Rate: 455
    I've tested it on XR806, read, write and erase continues to work.
  • XR806 and XR809 flashing tests at multiple bauds

    #337 21926795
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    yes. I have done quite a few tests on XR806, XR809, XF16 at different bauds. read, write, read+write, erase. OBK and flashdumps continue to boot as expected.

    Added after 43 [seconds]:

    other
    https://github.com/openshwprojects/BK7231GUIFlashTool/pull/139
    https://github.com/openshwprojects/BK7231GUIFlashTool/pull/138
  • ADVERTISEMENT
  • Native EF parser migration and flash speed gains

    #338 21926877
    insmod
    Level 31  
    Posts: 1433
    Help: 168
    Rate: 455
    I've decided to open PR about fully migrating to native EF parser.
    https://github.com/openshwprojects/BK7231GUIFlashTool/pull/141

    Tested BL602, BL616, ECR6600, RDA5981, RTL8720D, XR806 read/write directly on device.
    TR6260 and RTL8710B read on backups.

    Is TR6260 flasher broken? I wasn't able to progress past baud change. Even if baud is 57600, it wasn't able to do anything.
    115200: Set baud failed, response 254
    57600: Download/read protocol sync failed after baud change

    Some heavy BK decrypt optimization will come after, plus unsafe code will be completely disabled (since there would be no unsafe code).
    Pretty useless, considering how rare it's used. But it was a good learning experience.
    https://github.com/NonPIayerCharacter/BK7231G...mmit/f9c5bc6b956de4652201a5c9fed64e0f9bde458e
    In debug build, about 30% faster (sber dump 150->100ms)
    In release build, about 2-3 times as fast (same dump 80->30ms). For uascent dump, it finds keys in ~8ms. Was ~21ms.
  • Suggests reusing buffers to reduce C# allocations

    #339 21926904
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14814
    Help: 659
    Rate: 12986
    Ok so XRadio refactor has been merged.

    Flags refactor - also merged. The fact that we store them by copy/paste is not perfect but I am not sure if it makes sense to complicate it so they are fetched from main obk repo...

    C# Easy Flash - interesting, they release it, or is it LLM port? I guess it may a bit reduce false alarm ratio for antivirus scanners?

    Regarding optimization of decrypt - yes, improving efficiency is fun, especially in C#. I still see you are doing memory allocations in loop? Buffers could be probably allocated once and reused. I've been doing some heavy C# optimizations back in the day in my private projects, and I remember how I was suprised by the sheer amount of allocated memory by various operations. Granted, memory alloc does not mean slow code, but would you believe that even BinaryReader ReadSingle (if my memory serves me right) allocs? Or at least it used to... in that time. I ended up replacing some of C# libraries with my own faster counterparts. I also remember I had to modify the ZLIB C# library so it works without allocs.
    Helpful post? Buy me a coffee.
  • TR6260 handling may need utpmain-style changes

    #340 21926912
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    p.kaczmarek2 wrote:
    Flags refactor - also merged. The fact that we store them by copy/paste is not perfect but I am not sure if it makes sense to complicate it so they are fetched from main obk repo...


    true, but they don't change that often and it's easy enough to add/fix.

    there are a few other fixes to be made, eg obk config backup over IP - lots of unsupported newer chips.

    anyway. I'm tinkering with TR6260 now. EF works OK for me, but it seems handling can be closer to utpmain flasher code still
  • Optimized BK7231 crypto loops and threading

    #341 21926920
    insmod
    Level 31  
    Posts: 1433
    Help: 168
    Rate: 455
    I adapted hardcoded 32 granularity code made by @divadiow

    I've tried to reduce allocations in loops as much as possible.
    Performance profiler shows that 2 functions use cpu the most:
    Keystream
    https://github.com/NonPIayerCharacter/BK7231G...e458e/BK7231Flasher/Utils/Beken_Crypto.cs#L76
    XorIter
    https://github.com/NonPIayerCharacter/BK7231G...458e/BK7231Flasher/Utils/Beken_Crypto.cs#L106

    And they're already optimized to the limit.
    Plus it's running in a separate thread, so i can't use Span's directly.

    And just today i thought about integrating zlib (specifically miniz) into some flashloaders...
  • #342 21926943
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    insmod wrote:
    And just today i thought about integrating zlib (specifically miniz) into some flashloaders...


    this means sending image to be flashed to device compressed, so quicker?
  • #343 21926947
    insmod
    Level 31  
    Posts: 1433
    Help: 168
    Rate: 455
    >>21926943
    Reverse, to read back flash faster.
    After all, on DA gateway there's 16mb of flash, while OBK image is only about 1mb...
  • #344 21927473
    insmod
    Level 31  
    Posts: 1433
    Help: 168
    Rate: 455
    I've optimized it even further, and it's now 3 times as fast as previous version.
    Overall, it's about 8-10 times faster than the latest release.
    Sber: 80ms->9ms, uascent: 21ms->2ms.
    No heap allocations at all, only preallocated arrays/spans and stack allocations.

    However, i've made it execute synchronously to make use of spans and used a lot of unsafe code.
  • ADVERTISEMENT
  • #345 21927507
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14814
    Help: 659
    Rate: 12986
    I wonder if those methods marked with AggressiveInlining are really inlined? Second question: is NULLability used there?
    Screenshot of C# code showing Encrypt method with HasValue and Value checks highlighted
    Helpful post? Buy me a coffee.
  • Miniz integration needs 300k heap and overclocking

    #347 21928038
    insmod
    Level 31  
    Posts: 1433
    Help: 168
    Rate: 455
    I did manage to integrate miniz into DA stub.
    But it requires a lot of heap (about 300k), and compresses rather slow (either on Z_BEST_SPEED or Z_BEST_COMPRESSION).
    I even tried overclocking, but it didn't help much. Especially since it fails to read at bauds higher than 921600 for me.
    Currently at 625mhz clock speed at 1.0v, BW20 default - 262mhz at 0.9v. Stated maximum - 345mhz at 1.0v.
    Pity there's no way to increase voltage further.

    Without re-clocking, watchdog eventually resets, because compression takes too long at ROM clocks.

    Added after 2 [hours] 52 [minutes]:

    At 921600 baud 4MB RTL8721DA
    Normal read - 59048 ms, full 4MB
    Compressed read - 40532 ms, about 2MB (0x1F9000 in log)

    1500000
    Normal read - 42413 ms
    Compressed read - 34585 ms (when it managed to succeed)

    16M DA
    1500000
    Normal read - 168096 ms
    Compressed read - 95709 ms, about 4MB (0x412000 in log)

    Reduced CPU clock to 262mhz/0.9v to be in spec, since there's not much profit to be had. And while my dev board can handle 600mhz, gateway can't. It can't even handle 500mhz.
    Strangely enough, there were no errors on 16M gateway, connected via half a meter wires, but constant errors on a dev board with direct chip-to-chip connection.

    Added after 49 [minutes]:

    DA at 921600
    262mhz - 45364 ms
    600mhz - 38569 ms

    While a rather significant decrease, not every chip can handle it.
  • #348 21928140
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    pretty cool. :)
  • GD32VW553 port runs faster at 1.5 Mbaud

    #349 21928160
    insmod
    Level 31  
    Posts: 1433
    Help: 168
    Rate: 455
    https://github.com/NonPIayerCharacter/BK7231GUIFlashTool/actions/runs/28231686315

    Added after 2 [hours] 23 [minutes]:

    I ported it to the GD32VW553 as well, and even though it has less RAM (forcing me to use a specific optimization flag for miniz), it runs significantly faster than the RTL8721DA at 600 MHz, while it is running at 160 MHz. All because of faster flash?
    At 1500000 baud
    Normal - 0x400000 38838 ms
    Compressed - 0x18D000 23374 ms

    Added after 3 [hours] 11 [minutes]:

    ECR6600
    1500000:
    Normal - 20621 ms
    Compressed - 0x113000 22081 ms
    921600:
    Normal - 28672 ms
    Compressed - 26571 ms
    115200:
    Normal - 189090 ms
    Compressed - 112898 ms
  • #352 21928526
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    shorter text so checkbox can align with 'custom' button and add compression box tooltip?

    rejig - is the 'BK7231T is WB3S...' text essential? it's a little narrow now.

    EF GUI redesign :)
  • #353 21928527
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14814
    Help: 659
    Rate: 12986
    Well so I guess I can merge. What are the next steps for the flasher?

    Maybe this could get updated?
    https://github.com/openshwprojects/EasyGUIFlashTool
    I mean... port more from C# Flasher.
    Helpful post? Buy me a coffee.
  • #354 21928589
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    p.kaczmarek2 wrote:
    Well so I guess I can merge


    Screenshot of a PowerShell error: path not found while moving the Release folder in a directory structure script.
  • #355 21928950
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14814
    Help: 659
    Rate: 12986
    ah, I see there is a fix already, I'll merge both it and optmization.
    Helpful post? Buy me a coffee.
  • EasyGUIFlashTool update adds variant selection and BK SPI CH341 QIO support

    #356 21929078
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    p.kaczmarek2 wrote:


    I've got PTSD just trying to get RtlZ2 to work on EF2. Maybe codex 5.5 would do a better job now though

    Added after 1 [hours] 30 [minutes]:

    how about this. Do users really know about the variants and this would be useful? useful for testers anyway...

    -change 'download latest from web' to use GH API instead of scraping the http
    -detect if variant exists for selected platform and offers option to review
    -choosing to decline variant review continues to download default build like it does now
    -choosing to review variants gives nice list of choices
    -BK SPI CH341 now offers all Beken QIO instead of failing download


    Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs

    Added after 1 [hours] 44 [minutes]:

    https://github.com/openshwprojects/BK7231GUIFlashTool/pull/145
  • #357 21929375
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14814
    Help: 659
    Rate: 12986
    Looks like great idea but really needs some kind of description of each variant
    Helpful post? Buy me a coffee.
  • Linking variant options to enabled drivers list

    #358 21929378
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    yes, true. I was thinking a link in the variant option box to a new thread that listed what was enabled in each

    in here
    Screenshot of “Firmware variants available” showing OpenBK7231N_QIO_1.18.295.bin and Yes/No/Cancel buttons

    Added after 1 [hours] 24 [minutes]:

    and/or have some kind of enableddrivers.md or something that's generated by build. didn't @max4elektroda already do something like this to add info to the About page?

    I'll find PR

    Added after 1 [minutes]:

    https://github.com/openshwprojects/OpenBK7231T_App/pull/1841

    Added after 17 [minutes]:

    something like https://github.com/openshwprojects/OpenBK7231...ain...divadiow:OpenBK7231T_App:enableddrivers

    https://github.com/divadiow/OpenBK7231T_App/b...29e03e01f00fa683ea4b5/docs/enabledFeatures.md

    Added after 1 [hours] 19 [minutes]:

    better https://github.com/divadiow/OpenBK7231T_App/blob/enableddrivers/docs/enabledFeatures.md

    https://github.com/openshwprojects/OpenBK7231T_App/pull/2147

    Added after 22 [minutes]:

    tweaked

    “Firmware variants available” dialog with buttons: Choose variant, Download default, Cancel.
    “Choose firmware variant” dialog with a list of OpenBK7231N files and Download selected/Cancel buttons

    links to enabledFeatures.md
  • BK7252U 2/4 MB flash read/write/erase fixed

    #359 21930179
    divadiow
    Level 38  
    Posts: 5246
    Help: 451
    Rate: 921
    >>21778541
    BK7252U 2/4mb read/write/erase fixed https://github.com/openshwprojects/BK7231GUIFlashTool/pull/146

    - combined read/write OK
    - 2MB and 4MB flash size detection via wrap-around/mirror probing OK
    - full flash read now includes bootloader via mapped reads
    - CRC verification OK for reads and writes
    - erase-all preserves bootloader and erases from 0x11000 to detected flash end on 2MB/4MB chips
    - writing 2MB/4MB full backup images skips the first 0x11000 bytes in the file, preserving the existing bootloader
    - guarded against writing a 4MB image to a detected 2MB chip before erase/write begins
    - custom read/write paths tested
    - BK7231T regression-checked: wrap-around read, erase, write, custom read/write, and combined read/write still behave as before

    Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Beken BK7231/BK7252 SPI flashing and recovery - new flasher tool and protocol specs Screenshot of BK7231 Easy UART Flasher showing a memory read log. Screenshot of BK7231 Easy UART Flasher showing “Erase complete” and an operation log

    tested on 2mb BK7252UQN48 and 4mb BK7252UQN68 doorbells

    Added after 10 [hours] 38 [minutes]:

    add LN8825+LN882H full chip erase. tested
    https://github.com/openshwprojects/BK7231GUIFlashTool/pull/147
  • #360 21930643
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14814
    Help: 659
    Rate: 12986
    Erase merged.

    BK7231 changes - I see you're saying they are checked, but are they really correct? Hardcoded 4 MB flash? Just making sure.
    Helpful post? Buy me a coffee.
Listen:

Topic summary

LABEL_AI_GENERATED
The discussion centers on BK7231/BK7252 SPI flashing and recovery using BK7231GUIFlashTool v98+, which adds a new SPI-based flashing method requiring only a CH341 SPI programmer. The method is intended for recovering bricked Beken devices with overwritten bootloaders and also works as a general SPI flasher for external flash chips. Participants test and debug the new SPI mode, including COM-port handling, CH341 DLL loading, verify behavior, and Linux/Mono compatibility. The thread then expands into broader flasher development: UART and SPI improvements, flash-size detection, unprotect logic, retry mechanisms, baud-rate limits, and support for additional platforms such as RTL8720D, RTLZ/RTLZ2, BL602, LN882H, W600/W800, ECR6600, and remote flashing over TCP-to-UART. Several fixes are merged, including backup restore, erase/write handling, config read/write, and improved detection and logging.
AI summary based on the discussion. May contain errors.

FAQ LABEL_AI_GENERATED

TL;DR: New BK7231GUIFlashTool v98+ adds SPI flashing for Beken chips; sample 4,096 KB flash detected. “Only a CH341 SPI programmer is required.” Use CH341A D2 to toggle CEN, send 0xD2, then read/write like generic SPI. [Elektroda, p.kaczmarek2, post #21711721]

Why it matters: It lets you recover bricked BK7231/BK7252 devices without a working bootloader, using low‑cost tools.

Who this is for: DIYers, repair techs, and firmware engineers asking how to unbrick or mass‑flash Beken SoCs via SPI with a CH341A and the latest tool.

Quick Facts

What exactly is the new Beken SPI flashing method and what do I need?

The tool drives CH341A as an SPI master, resets BK via CEN on D2, sends 0xD2 to enter BK’s SPI-memory mode, then treats the chip like a standard SPI flash. You need a CH341A (jumper at I2C), 3.3 V power, and wiring for P20–P23 (SCK/CSN/SI/SO) plus CEN. Select “Beken SPI” in BK7231GUIFlashTool v98+. “Only a CH341 SPI programmer is required.” [Elektroda, p.kaczmarek2, post #21711721]

How do I wire CH341A to BK7231/BK7252 for SPI mode?

Connect CH341A SCK→P20, CS0→P21, MOSI→P22 (SI), MISO→P23 (SO), and D2→CEN. Keep MOSI→SI and MISO→SO mapping. Provide GND and target power. Some boards have pads labeled CE/TCK/TMS/TDI/TDO that map to CEN/SCK/CSN/SI/SO respectively. [Elektroda, p.kaczmarek2, post #21711721]

How do I reliably enter BK SPI mode? (3‑step)

  1. Use CH341 D2 to pull CEN low then high to reset the BK chip. 2. Stream 0xD2 bytes repeatedly over SPI. 3. Issue 0x9F and confirm a valid JEDEC response; then proceed to Read/Write/Erase. [Elektroda, p.kaczmarek2, post #21711721]

I get “Failed to open CH341 device” or “Failed to toggle CEN.” What should I check?

Confirm CH341A is jumpered to I2C, recognized by Windows, and the D2 wire is soldered to CEN. Re‑seat USB, power the target, and verify SPI lines. The tool logs these errors when CH341 isn’t detected or CEN can’t be driven; fix wiring or driver, then retry. [Elektroda, p.kaczmarek2, post #21711721]

The app says “missing ch341dll.dll” or quits in SPI mode—how do I fix that?

Place CH341DLL.DLL alongside the GitHub build, or build from source in Visual Studio. Earlier builds could exit if CH341A wasn’t present; recent fixes improved handling, but adding the DLL resolved missing‑library errors during testing. [Elektroda, divadiow, post #21712310]

Do I need to select a COM port for SPI flashing?

No. As of v99, SPI mode does not require a COM port. This was confirmed after fixes; previous v98 prompts were removed. Ensure CH341A is connected; SPI operations run without a serial port. [Elektroda, divadiow, post #21712310]

What does the Verify button do?

Verify compares flash contents against the firmware file currently selected in the tool. Use it after Write or Erase to confirm success. “Verify verifies against selected firmware.” [Elektroda, p.kaczmarek2, post #21712551]

Can this recover a bricked BK7252 camera?

Yes. After wiring CEN and SPI lines, use Beken SPI mode to read/erase/write. The author provides a BK7252 camera recovery example using this exact method with CH341A and the new flasher. [Elektroda, p.kaczmarek2, post #21711721]

Can I use CH341 Programmer or NeoProgrammer once SPI mode is active?

Yes. After the 0xD2 hand‑off, the BK behaves like a generic SPI flash. You can then operate with common SPI flash tools (CH341 Programmer or NeoProgrammer) if you prefer. [Elektroda, p.kaczmarek2, post #21711721]

Linux support: what baud rates are stable under mono?

Mono 6.12 worked for testers. They reported stable operation at 921,600 baud; 1,500,000 worked on Windows, but they lowered to 921,600 on Linux. LAN Scanner and release downloader also worked. [Elektroda, insmod, post #21712663]

The tool shows a 4,096 KB device. Is that normal for these chips?

Yes. A sample JEDEC ID FF‑EF‑40‑16 decoded to 4,096 KB and printed by the tool. That confirms JEDEC read and size decoding in SPI mode. Actual sizes vary by module; always check the tool’s detected flash size. [Elektroda, p.kaczmarek2, post #21711721]

How do I just switch a BK device into SPI mode without reading or writing?

Use the new “Detect” button. It performs a zero‑length custom operation to toggle CEN and send the 0xD2 sync, placing the chip in SPI mode for use with other SPI tools. [Elektroda, p.kaczmarek2, post #21731041]

I flashed many times and now erase fails. What should I try?

Expand Unprotect logic before erase, similar to AsProgrammer. A contributor noted needing stronger Unprotect after many BK7252 cycles. Add an Unprotect/Status-Register release step, then retry the erase/write. [Elektroda, p.kaczmarek2, post #21714737]

What is OpenBeken (OBK) in this context?

OpenBeken is an open‑source firmware used across supported Wi‑Fi MCUs in this ecosystem. The author uses OBK devices for testing and demos, including camera boards and remote flashing concepts. [Elektroda, p.kaczmarek2, post #21714427]

Can I flash a BK device over Wi‑Fi/TCP instead of USB?

Yes, a Wi‑Fi flasher demo proxies UART over TCP and controls CEN via an OBK device. It’s a separate utility that mirrors the BK UART routines, useful when PC‑to‑target wiring is hard. [Elektroda, p.kaczmarek2, post #21720013]

Any known edge cases or failure symptoms I should expect?

If CH341DLL is missing, some GitHub builds previously exited. On mono/Linux, 1,500,000 baud may fail while 921,600 works. Also, after heavy cycling, protection bits may block erase until Unprotect expands. Address each symptom as noted, then retry. [Elektroda, divadiow, post #21712310]
AI summary based on the discussion. May contain errors.
ADVERTISEMENT