LN882H UART flashing protocol documentation - Windows LN882H flasher with fast flash read

LN882H is a WiFi & BT Wireless ARM Cortex-M4F SOC found in some IoT devices. LN882H features a simple UART flashing protocol that can be used to read and write flash memory contents, usually as part of the firmware change process or for development purposes.
LN882H flashing protocol is mostly based on simple, plain-text ASCII commands, however, the flash data transfer is done via YModem.
LN882H flashing protocol details
First LN882H must be put in bootloader mode, this requires grounding a specific pin, as we've shown in previous guide. Once reboot is done (either via power off/on or RESET), LN882H will print a default identity string at 115200 baud:
Code: C#
This can be used to establish initial configuration and determine firmware version. Then it can be used to upload a compiled binary blob that will be stored in RAM and act like a temporary bootloader for our further operations:
Code: C#
Bootloader is sent via YModem protocol, which will not be covered here.
In this case, the uploaded file is LN882H_RAM_BIN.bin from LN882H flashing tools. We can see the source code of similar loader in the SDK as well:
https://github.com/openshwprojects/OpenLN882H/tree/master/project/bootcode/ramcode_dl
The ramcode_ln882h.uvprojx can be used to compile RAM code in Keil.
The main commands supported by RAM code can be seen in cmd_mode.c:
Code: C / C++
Those are the commands we can send over the UART once RAM code is running.
So, let's consider some simple process, like full flash erase:
Code: C#
First the connection is established and RAM code is loaded. Then flash_erase_all is called, which will internally do just as simple ASCII command:
Code: C#
It just sends a command and consumes the output (command echo and results).
So, if we want to see the ferase_all implementation, we should head to aforementioned table instead:
Code: C / C++
which, in this case is:
Code: C / C++
Code: C / C++
Code: C / C++
The same logic applies to other operations, like MAC address or GPIOs access:
Code: C#
Now, let's focus on more interesting things. First, flash write. That's how OpenLN882H can be flashed:
Code: C#
The procedure just changes the baud and sends the upload data via aforementioned RAM code, and then just sends the file through the same YModem protocol which was used to upload RAM code itself.
The receival is done in file_mode.c:
https://github.com/openshwprojects/OpenLN882H.../project/bootcode/ramcode_dl/main/file_mode.c
Now, flash read is more interesting.
Flash dump was originally done in a slow, round-about manner:
Code: C / C++
Flash was dumped as ASCII text (3 characters per byte - two hex digits and space):
Code: C / C++
This is very slow, but luckily, our contributor @insmod has recently changed the dump logic to use, among others, raw binary format:
https://github.com/NonPIayerCharacter/OpenLN8...mmit/fc62bd2392bcd022b57b5ead14b04084a2456bfe

The following code uses 16-bit CRC for error checking, which was already implemented in the SDK.
Here is the matching flash read code on C# side:
Code: C#
As you can see, there is no more ASCII string processing and everything is done at raw bytes data. Futhermore, CRC check is also done to make sure that received data is correct.
By the way, it's also interesting to see how baud rate set is implemented - rather than doing it via UART command, the RAM code itself is modified:
Code: C#
The offsets where baud settings are stored were figured by comparing multiple builds with the different baud settings.
Our simple LN882H flashing tool for Windows[/quote]
Flasher is available for download here:
https://github.com/openshwprojects/SharpLN882HTool
It's already compiled for Windows.
Package contains sample scripts/commands:
- sample_dump_at_baud_460800.bat - sample firmware read (backup)
- sample_erase.bat - sample flash erase (not really needed)
- sample_write.bat - sample flash write (OpenLN882H binary, also included)
- sample_terminal.bat - sample terminal mode
Please open related .bat files for more details.
Remember to put LN882H must be put in bootloader mode, as we've shown in previous guide.
Summary
This is how we managed to create improved C# tool for easy LN882H flashing with binaries automatically build by Github actions. You can download it here:
https://github.com/openshwprojects/SharpLN882HTool
Soon we'll take next step and integrate this features into our Easy Flasher, so stay tuned!
Special thanks for @insmod for flash read optimization and @divadiow for testing!
Comments
v37. yay! not tested anything yet. but.. https://obrazki.elektroda.pl/7282192100_1753166050_bigthumb.jpg [Read more]
It's just a placeholder, for now. Soon it will work, I'll post updates here. Added after 2 [hours] 45 [minutes]: @divadiow reading works I think https://obrazki.elektroda.pl/7200903800_1753176001_bigthumb.jpg... [Read more]
whaat. how. program closes at this point at any baud for me https://obrazki.elektroda.pl/6689736100_1753176311_thumb.jpg [Read more]
No stack trace, nothing? [Read more]
report.wer from event viewer. no error in EF before quit Version=1 EventType=CLR20r3 EventTime=133976509853857211 ReportType=2 Consent=1 UploadTime=133976509857691172 ReportStatus=268435456 ReportIdentifier=20058170-146a-45d7-b887-5dd25540909f Wow64Host=34404 OriginalFilename=BK7231Flasher.exe AppSessionGuid=0000f898-0001-0134-79f1-5001edfadb01 TargetAppId=W:000620c606e4b70915f5e0705ee7e93d43ee00000000!0000ff46005f4b1271d03b695cb4c2816f64cd012f89!BK7231Flasher.exe TargetAppVer=2092//06//17:05:02:07!0!BK7231Flasher.exe BootId=4294967295 TargetAsId=133410 UserImpactVector=269484304 IsFatal=4294967295 EtwNonCollectReason=4 Response.BucketId=ee6749fcd2675b8e3495bcf209c6c301 Response.BucketTable=5 Response.LegacyBucketId=1483299400020574977 Response.type=4 Sig[0].Name=Problem... [Read more]
I will check in a moment https://obrazki.elektroda.pl/4714922100_1753180849_bigthumb.jpg Added after 14 [minutes]: pS: it looks like Problem Signature 09: System.IO.IOException for yo... [Read more]
yes. researching. [Read more]
Either it can't open file or can't open serial I guess... but RAMcode for flash read is embedded into binary by @insmod , I will do the same for flash later, for now i've added it as file. Maybe I should... [Read more]
v41 https://obrazki.elektroda.pl/7509775500_1753184054_thumb.jpg https://obrazki.elektroda.pl/8244838600_1753184020_thumb.jpg Added after 8 [minutes]: 115200 read success htt... [Read more]
I am removing those strange 5 second delays now. It seems to work without them well. Well, how could it not, considering that it still just waits for given reply in a loop... so, if there is no reply in... [Read more]
Perhaps this is .NET 3.5 problem? It can't handle $ string and {}? [Read more]
That fragment was just me forgetting $ while adding more information to the string. [Read more]
Ah, ok. I just never had to work with .net version lower than 4.5 And at work, we use the most recent one. [Read more]
I am still investigating but my findings are very strange, I'm not even sure if it's not a fluke... Look. You know this fragment, right? That's where you RAM code sends the flash size, first 4 bytes. ... [Read more]
Does flash writing work with this: https://www.elektroda.com/rtvforum/viewtopic.php?p=21609680#21609680 [Read more]
I didn't check yet, I have to travel for a moment, I will check on another machine shortly. I carry LN882H with me. Currently I am fairly certain that read at bauds different than 115200 works only... [Read more]
This ramcode is just like the one used for writing/erase, but with custom read algorithm. That is why i'm asking if it can write, because for me - it froze on baud change, while writing "baudrate x" manually... [Read more]
I didn't manage to do that much reliable testing but my general subjective feeling was that both read and write works only at 115200 baud. I didn't look into it at first because I assumed it's due another... [Read more]
with this + v45 I have written at 115200 baud to blank flash and OpenLN boots https://obrazki.elektroda.pl/1246302300_1753210314_thumb.jpg higher baud does not begin https://obrazk... [Read more]