diff --git a/arduino/libretuya/core/ChipType.h b/arduino/libretuya/core/ChipType.h new file mode 100644 index 0000000..849685e --- /dev/null +++ b/arduino/libretuya/core/ChipType.h @@ -0,0 +1,23 @@ +/* Copyright (c) Kuba SzczodrzyƄski 2022-05-28. */ + +enum ChipFamily { + // copied from UF2 families (first nibble) + RTL8710A = 0x9F, // Realtek Ameba1 + RTL8710B = 0x22, // Realtek AmebaZ (realtek-ambz) + RTL8720C = 0xE0, // Realtek AmebaZ2 + RTL8720D = 0x33, // Realtek AmebaD + BK7231T = 0x67, // Beken 7231T + BK7231N = 0x7B, // Beken 7231N + BL602 = 0xDE, // Boufallo 602 + XR809 = 0x51, // Xradiotech 809 +}; + +enum ChipType { + // copied from rtl8710b_efuse.h + RTL8710BL = (RTL8710B << 8) | 0xE0, // ??? + RTL8710BN = (RTL8710B << 8) | 0xFF, // CHIPID_8710BN / QFN32 + RTL8710BU = (RTL8710B << 8) | 0xFE, // CHIPID_8710BU / QFN48 + RTL8710BX = (RTL8710B << 8) | 0xFB, // CHIPID_8710BN_L0 / QFN32 + RTL8711BN = (RTL8710B << 8) | 0xFD, // CHIPID_8711BN / QFN48 + RTL8711BU = (RTL8710B << 8) | 0xFC, // CHIPID_8711BG / QFN68 +}; diff --git a/arduino/libretuya/core/LibreTuyaAPI.cpp b/arduino/libretuya/core/LibreTuyaAPI.cpp index cf8ba1c..1a0657c 100644 --- a/arduino/libretuya/core/LibreTuyaAPI.cpp +++ b/arduino/libretuya/core/LibreTuyaAPI.cpp @@ -59,3 +59,37 @@ void hexdump(uint8_t *buf, size_t len, uint32_t offset, uint8_t width) { pos += lineWidth; } } + +/** + * @brief Get LibreTuya version string. + */ +const char *LibreTuya::getVersion() { + return LT_VERSION_STR; +} + +/** + * @brief Get board name. + */ +const char *LibreTuya::getBoard() { + return LT_BOARD_STR; +} + +static char *deviceName = NULL; + +/** + * @brief Get device friendly name in format "LT--". + * Can be used as hostname. + */ +const char *LibreTuya::getDeviceName() { + if (deviceName) + return deviceName; + uint32_t chipId = getChipId(); + uint8_t *id = (uint8_t *)&chipId; + + const char *board = getBoard(); + uint8_t boardLen = strlen(board); + deviceName = (char *)malloc(3 + boardLen + 1 + 6 + 1); + + sprintf(deviceName, "LT-%s-%02x%02x%02x", board, id[0], id[1], id[2]); + return deviceName; +} diff --git a/arduino/libretuya/core/LibreTuyaAPI.h b/arduino/libretuya/core/LibreTuyaAPI.h index 0a20b04..75c6fb8 100644 --- a/arduino/libretuya/core/LibreTuyaAPI.h +++ b/arduino/libretuya/core/LibreTuyaAPI.h @@ -54,6 +54,10 @@ void hexdump(uint8_t *buf, size_t len, uint32_t offset, uint8_t width); // Main class #ifdef __cplusplus + +#include // for flash inline methods +#include + /** * @brief Main LibreTuya API class. * @@ -63,14 +67,100 @@ void hexdump(uint8_t *buf, size_t len, uint32_t offset, uint8_t width); * The class is accessible using the `LT` global object (defined by the platform). */ class LibreTuya { + public: /* Common methods - note: these are documented in LibreTuyaAPI.cpp */ + const char *getVersion(); + const char *getBoard(); + const char *getDeviceName(); - /* Common methods*/ + public: /* Inline methods */ + inline uint32_t getFlashChipSize() { + return Flash.getSize(); + } - public: - /* Platform-defined methods */ + // inline bool flashEraseSector(uint32_t sector) {} + // inline bool flashWrite(uint32_t offset, uint32_t *data, size_t size) {} + // inline bool flashRead(uint32_t offset, uint32_t *data, size_t size) {} + // inline bool partitionEraseRange(const esp_partition_t *partition, uint32_t offset, size_t size) {} + // inline bool partitionWrite(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size) {} + // inline bool partitionRead(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size) {} - public: + public: /* Platform-defined methods */ + /** + * @brief Reboot the CPU. + */ + void restart(); + + public: /* CPU-related */ + /** + * @brief Get CPU model ID. + */ + ChipType getChipType(); + /** + * @brief Get CPU model name as string. + */ + const char *getChipModel(); + /** + * @brief Get CPU unique ID. This may be based on MAC, eFuse, etc. + */ + uint32_t getChipId(); + /** + * @brief Get CPU core count. + */ + uint8_t getChipCores(); + /** + * @brief Get CPU core type name as string. + */ + const char *getChipCoreType(); + /** + * @brief Get CPU frequency in MHz. + */ + uint32_t getCpuFreqMHz(); + /** + * @brief Get CPU cycle count. + */ + inline uint32_t getCycleCount() __attribute__((always_inline)); + + public: /* Memory management */ + /** + * @brief Get total RAM size. + */ + uint32_t getRamSize(); + /** + * @brief Get total heap size. + */ + uint32_t getHeapSize(); + /** + * @brief Get free heap size. + */ + uint32_t getFreeHeap(); + /** + * @brief Get lowest level of free heap memory. + */ + uint32_t getMinFreeHeap(); + /** + * @brief Get largest block of heap that can be allocated at once. + */ + uint32_t getMaxAllocHeap(); + + public: /* OTA-related */ + /** + * @brief Get the currently running firmware OTA index. + */ + uint8_t getOtaRunning(); + /** + * @brief Get the OTA index for updated firmware. + * + * Note: should return 1 for chips without dual-OTA. + */ + uint8_t getOtaTarget(); + /** + * @brief Try to switch OTA index to the other image. + * + * @return false if writing failed or dual-OTA not supported; true otherwise + */ + bool switchOta(); }; extern LibreTuya LT; +extern LibreTuya ESP; #endif diff --git a/builder/utils.py b/builder/utils.py index 68e81e3..311cc8f 100644 --- a/builder/utils.py +++ b/builder/utils.py @@ -49,6 +49,8 @@ def env_add_defaults(env, platform_name: str, sdk_name: str): CPPDEFINES=[ ("LT_VERSION", platform.version), ("LT_BOARD", board.get("build.variant")), + ("F_CPU", board.get("build.f_cpu")), + ("MCU", board.get("build.mcu").upper()), ], )