ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
lt_component.cpp
Go to the documentation of this file.
1#include "lt_component.h"
2
3#ifdef USE_LIBRETINY
4
5#include <cinttypes>
6#include <cstring>
7
9#include "esphome/core/log.h"
10
11#if defined(USE_LIBRETINY_VARIANT_BK7238) && defined(USE_OTA)
12#include <flashdb.h>
13#endif
14
15namespace esphome::libretiny {
16
17ESPHOME_LOG_TAG(TAG, "libretiny");
18
19void LTComponent::dump_config() {
20 ESP_LOGCONFIG(TAG,
21 "LibreTiny:\n"
22 " Version: %s\n"
23 " Loglevel: %u",
24 &LT_BANNER_STR[10], LT_LOGLEVEL);
25#if defined(__OPTIMIZE_SIZE__) && __OPTIMIZE_LEVEL__ > 0 && __OPTIMIZE_LEVEL__ <= 3
26 ESP_LOGCONFIG(TAG, " Optimization: -Os, SDK: -O" STRINGIFY_MACRO(__OPTIMIZE_LEVEL__));
27#endif
28
29#if defined(USE_LN882X) && defined(USE_WIFI)
30 // The SDK falls back to this MAC when the board layout's KV store holds none.
31 static constexpr uint8_t SDK_DEFAULT_MAC[MAC_ADDRESS_SIZE] = {0x00, 0x50, 0xC2, 0x5E, 0x10, 0x88};
32 uint8_t mac[MAC_ADDRESS_SIZE];
34 if (memcmp(mac, SDK_DEFAULT_MAC, MAC_ADDRESS_SIZE) == 0) {
35 ESP_LOGE(TAG, "MAC is the LN882H SDK default 00:50:C2:5E:10:88; only one device with it can be on a network. "
36 "On a Tuya module, set board: to its Tuya board (e.g. wl2s, wl2h-u) and flash over serial");
37 }
38#endif
39
40#if defined(USE_BK72XX) && defined(USE_WIFI)
41 // The SDK silently runs the radio uncalibrated when the layout's calibration partition lacks the TLV table.
42 static constexpr uint32_t RF_CAL_TLV_MAGIC = 0x00564C54; // "TLV\0" read as little endian
43 static constexpr uint32_t RF_CAL_OFFSETS[] = {FLASH_CALIBRATION_OFFSET, 0x1D0000, 0x1E0000, 0x1E3000};
44 uint32_t found = 0;
45 for (uint32_t offset : RF_CAL_OFFSETS) {
46 uint32_t magic;
47 if (lt_flash_read(offset, (uint8_t *) &magic, sizeof(magic)) == sizeof(magic) && magic == RF_CAL_TLV_MAGIC) {
48 found = offset;
49 break;
50 }
51 }
52 if (found == 0) {
53 ESP_LOGE(TAG, "No RF calibration found; Wi-Fi may be unreliable. Check that board: matches this module");
54 } else if (found != FLASH_CALIBRATION_OFFSET) {
55 ESP_LOGE(TAG,
56 "board: layout wrong for this module, RF calibration is at 0x%06" PRIX32 " not 0x%06" PRIX32
57 "; reflash over serial",
58 found, (uint32_t) FLASH_CALIBRATION_OFFSET);
59 }
60#endif
61
62#if defined(USE_LIBRETINY_VARIANT_BK7238) && defined(USE_OTA) && \
63 (FLASH_DOWNLOAD_OFFSET == 0x132000 || FLASH_DOWNLOAD_OFFSET == 0x12C000)
64 // The other layout's bootloader, known by the CRC-32 of its first 257 bytes as bk7231tools identifies it,
65 // applies updates from its own download offset, so this build's updates are never applied.
66#if FLASH_DOWNLOAD_OFFSET == 0x132000
67 static constexpr uint32_t OTHER_BOOTLOADER_CRC = 0xF42F8C32;
68 static constexpr const char *OTHER_BOOTLOADER = "Tuya T1 2.0.0; set board: generic-bk7238-tuya";
69#else
70 static constexpr uint32_t OTHER_BOOTLOADER_CRC = 0x6BEB0924;
71 static constexpr const char *OTHER_BOOTLOADER = "Beken 1.0.14; set board: generic-bk7238";
72#endif
73 uint8_t bootloader[257];
74 if (lt_flash_read(0, bootloader, sizeof(bootloader)) == sizeof(bootloader) &&
75 fdb_calc_crc32(0, bootloader, sizeof(bootloader)) == OTHER_BOOTLOADER_CRC) {
76 ESP_LOGE(TAG, "OTA updates are not applied: bootloader is %s, upload, then reset (the upload reports an error)",
77 OTHER_BOOTLOADER);
78 }
79#endif
80
81#ifdef USE_TEXT_SENSOR
82 PUBLISH_STATE(this->version_text_sensor_, &LT_BANNER_STR[10]);
83#endif // USE_TEXT_SENSOR
84}
85
87
88} // namespace esphome::libretiny
89
90#endif // USE_LIBRETINY
virtual float get_setup_priority() const
priority of setup().
Definition component.cpp:82
ESPHOME_LOG_TAG(TAG, "libretiny.crash")
constexpr float LATE
For components that should be initialized at the very end of the setup process.
Definition component.h:59
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
Definition helpers.cpp:71
static void uint32_t