
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!
Cool? Ranking DIY Helpful post? Buy me a coffee.