libretiny-eu / libretiny

PlatformIO development platform for IoT modules
http://docs.libretiny.eu/
MIT License
401 stars 59 forks source link

Tenda Beli SP3 smart plug write error. #171

Open gaz99gh opened 1 year ago

gaz99gh commented 1 year ago

I have an RTL8710BX based Tenda Beli SP3 smart plug that is not Tuya. When trying to upload the Esphome uf2 file generated from the 2023.9.0b1 beta, I get this error:

E: ValueError: Failed to write to 0x800B000

I have successfully read the original firmware and the file is 2M and a look at the file shows this:

This flash type is not supported! CHIPID[%08x]

The flash chip is a H25S80BG.

Any ideas on how to proceed ?

tenda-stock2.zip

tenda1s2

kuba2k2 commented 1 year ago

The message you found in the file probably doesn't mean anything.

The writing error, however, might be caused by several problems. How are you powering the device for flashing?

I just tried flashing your firmware dump on a RTL8710BX, and it doesn't start up:

<RTL8195A>ROM:[V0.1]
FLASHRATE:4
BOOT TYPE:0 XTAL:40000000
IMG1 DATA[1128:10002000]
IMG1 ENTRY[800053f:100021ef]
IMG1 ENTER
CHIPID[000000f6]
read_mode idx:3, flash_speed idx:4
calibration_result:[1:29:15][1:1d] 
calibration_result:[2:19:11][1:13] 
calibration_result:[3:0:0][ff:ff] 
calibration_ok:[1:29:15] 
FLASH CALIB[NEW OK]
OTA2 ADDR[8080000]
OTAx SELE[fffffffc]
OTA1 USE
IMG2 DATA[0x8069530:-1:0xffffffff]
IMG2 ADDR Invalid
gaz99gh commented 1 year ago

I have tried both the modified FTDI TTL-232R cable I normally use and my bench PSU. There is no obvious CEN pin so I'm using the power reset while grounding Tx.

The programming test points on the module don't say TX2/RX2 but they are all there is.

The back of the module looks like this: tenda3s

kuba2k2 commented 1 year ago

Try using the rtltool2 flasher from here: https://github.com/hn/ginlong-solis/issues/2#issuecomment-1625731577 Make sure to use the correct OTA2 address, which is 0x80000 for your board (I think).

EDIT: I just noticed something - your flash chip is 1 MiB only, not 2 MiB. You need to use generic-rtl8710bn-2mb-468k board, but with such a small flash chip ESPHome might have trouble fitting there.

Cossid commented 1 year ago

Yeah, this is becoming a theme with realtek, where the last letter of the chip, which is supposed to indicate flash size, doesn't actually match the size of the external flash.

gaz99gh commented 1 year ago

OK, I have reached the limit of my linux command line knowledge !!

I have compiled a new esphome device with the generic-rtl8710bn-2mb-468k board type and saved the file as tenda-test22468.u2f

I have managed to read from the device with rtltool but I get an error when trying to write. rtltool: error: unrecognized arguments: /home/gazza/rtltool/tenda-test22468.u2f

The command I am using is: sudo python /home/gazza/rtltool/rtltool.py -p /dev/ttyUSB0 wf 0x8000000 /home/gazza/rtltool/tenda-test22468.u2f Also what do the arguments mean, I used 0x8000000 as that was what was in the the other issue that was linked but 0x800000 was mentioned above.

kuba2k2 commented 1 year ago

The comment I linked uses a different command - please see this comment specifically, not the whole issue. You should use rtltool2, not rtltool.

Also, the file is uf2 not u2f. Also, you shouldn't be using uf2 for flashing with rtltool2 - use the binary files with offsets, as said in the issue comment above.

0x8000000, 0x800000 and 0x80000 are all different, you have to be careful with the number of zeroes.

gaz99gh commented 1 year ago

Where do I get rtltool2.py I could only find rtltool.py ?

kuba2k2 commented 1 year ago

Here: https://github.com/pvvx/RTL0B_SDK/blob/master/mkb/rtltool.py This requires Python 2. As in the linked comment, run:

python2 ./rtltool2.py -p /dev/ttyUSB0 wf 0xb000 image_0x00B000.ota1.bin
python2 ./rtltool2.py -p /dev/ttyUSB0 wf 0x80000 image_0x080000.ota2.bin

Get the image_* files from the build directory of your firmware.

gaz99gh commented 1 year ago

OK, that is the file I was using and I have confirmed that I am using python2 and I even managed to get the missing python2-serial stuff installed.

Also, when I do a manual download from Esphome 23.9.0b1 I now get options to download the uf2 and the separate binaries.

So I have loaded files and it is up and running, the minimal install is using 81% of the flash, the device is a pretty basic smart plug with no power monitoring so I might work. Will I break anything if it try the generic-rtl8710bn-2mb-788k version ??

Thanks for the help.

INFO ESPHome 2023.9.0b1
INFO Reading configuration /config/esphome/tenda-test22468.yaml...
INFO Starting log output from tenda-test22468.local using esphome API
INFO ESPHome 2023.9.0b1
INFO Reading configuration /config/esphome/tenda-test22468.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing tenda-test22468 (board: generic-rtl8710bn-2mb-468k; framework: arduino; platform: libretiny)
--------------------------------------------------------------------------------
Warning! Non-Git installations are NOT SUPPORTED.
HARDWARE: RTL8710BN 125MHz, 256KB RAM, 468KB Flash
 - framework-arduino-api @ 2022.8.24+sha.237b10a 
 - framework-realtek-amb1 @ 0.0.0+v2022.06.21.sha.c4e44ef 
 - library-flashdb @ 1.2.0+sha.d5c892f 
 - library-freertos @ 8.1.2+sha.776ae6c 
 - library-freertos-port @ 2023.5.23+sha.a917d93 
 - library-lwip @ 2.1.3-amb1+sha.6297b80 
 - library-printf @ 6.1.0+sha.28a79bd 
 - library-uf2ota @ 5.0.0+sha.f955412 
PLATFORM VERSIONS:
 - libretiny @ 1.4.0
 - ltchiptool @ 4.5.1
CUSTOM OPTIONS:
 - fw_name = esphome
 - fw_version = 2023.9.0b1
No dependencies

Archiving .pioenvs/tenda-test22468/libboot_all.a
Archiving .pioenvs/tenda-test22468/libambz_sdk.a
Linking .pioenvs/tenda-test22468/raw_firmware.elf
|-- Image 1: raw_firmware.ota1.elf
|-- Image 2: raw_firmware.ota2.elf
|   |-- image_ota1.0x00B000.bin
|   |   |-- image_ota1.ram_2.r.bin
|   |   |-- image_ota1.xip_image2.bin
|   |   |-- image_ota1.rdp.bin
|   |-- image_ota2.0x080000.bin
|   |   |-- image_ota2.ram_2.r.bin
|   |   |-- image_ota2.xip_image2.bin
|   |   |-- image_ota2.rdp.bin
RAM:   [==        ]  20.1% (used 52592 bytes from 262144 bytes)
Flash: [========  ]  82.1% (used 393356 bytes from 479232 bytes)
Building UF2 OTA image
|-- esphome_2023.9.0b1_generic-rtl8710bn-2mb-468k_rtl8710bn_lt1.4.0.uf2
|-- firmware.uf2
|-- firmware.bin
========================= [SUCCESS] Took 58.93 seconds =========================`
kuba2k2 commented 1 year ago

It's a small flash chip, so 81% usage is normal. Don't try the 788k version, it's too large for this device. Also, you'll break OTA that way.

Do I correctly assume that you have flashed ESPHome successfully and it is running on the device?

gaz99gh commented 1 year ago

Yes I have Esphome running on it with just the default settings and I have been able to do an OTA update.

gaz99gh commented 1 year ago

Is there somewhere to record working hardware and the ESPHome code for them ??

kuba2k2 commented 1 year ago

We do have a special channel on our Discord server, but I don't really know of any other place (apart from sites dedicated to ESP devices).

gaz99gh commented 1 year ago

I think this is complete.

kuba2k2 commented 1 year ago

I understand that using ltchiptool didn't work, but rtltool2 did? If so, I'll keep this issue open to fix this error.

gaz99gh commented 1 year ago

OK, in ltchiptool I was using the defaults but I also had the firmware extension wrong, u2f instead of uf2.

That was incorrect, as there was no pre-compiled kickstarter file for the rtl8710bn-2mb-468k I used ESPHome 2023.99.0b1 to make my own and that is the file I tried to use with ltchiptool.

Do you want me to do any other testing ?