From 63ac7b365de7daae0784e059303d202ea2789462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Mon, 20 Mar 2023 21:29:57 +0100 Subject: [PATCH] [core] Add ESP inline library --- .../common/arduino/libraries/inline/ESP/ESP.h | 121 ++++++++++++++++++ .../arduino/libraries/inline/Flash/Flash.h | 2 +- cores/common/arduino/libraries/inline/LT/LT.h | 1 + .../arduino/libraries/inline/Singletons.cpp | 1 + cores/common/base/api/lt_flash.h | 2 +- cores/common/base/config/printf_config.h | 24 +++- cores/common/base/lt_api.c | 2 +- docs/dev/config.md | 2 +- docs/dev/lt-api.md | 14 ++ 9 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 cores/common/arduino/libraries/inline/ESP/ESP.h diff --git a/cores/common/arduino/libraries/inline/ESP/ESP.h b/cores/common/arduino/libraries/inline/ESP/ESP.h new file mode 100644 index 0000000..03a202c --- /dev/null +++ b/cores/common/arduino/libraries/inline/ESP/ESP.h @@ -0,0 +1,121 @@ +/* Copyright (c) Kuba SzczodrzyƄski 2023-03-20.() */ + +#include + +#ifdef __cplusplus + +/** + * @brief ESP Arduino Core compatibility class. + * + * This class only consists of inline functions, which + * wrap the LibreTuya C API (lt_api.h). Refer to the docs of the C API + * for more information. + * + * The class is accessible using the `ESP` global object. + */ +class EspClass { + public: + /** @copydoc lt_wdt_enable() */ + inline void wdtEnable(uint32_t timeout_ms = 0) { lt_wdt_enable(timeout_ms); } + + /** @copydoc lt_wdt_disable() */ + inline void wdtDisable() { lt_wdt_disable(); } + + /** @copydoc lt_wdt_feed() */ + inline void wdtFeed() { lt_wdt_feed(); } + + /** @copydoc lt_reboot() */ + inline void reset() { lt_reboot(); } + + /** @copydoc lt_reboot() */ + inline void restart() { lt_reboot(); } + + /** @copydoc lt_reboot_download_mode() */ + inline void rebootIntoUartDownloadMode() { lt_reboot_download_mode(); } + + inline uint16_t getVcc() { return 3300; } + + /** @copydoc lt_cpu_get_mac_id() */ + inline uint32_t getChipId() { return lt_cpu_get_mac_id(); } + + /** @copydoc lt_heap_get_free() */ + inline uint32_t getFreeHeap() { return lt_heap_get_free(); } + + /** @copydoc lt_heap_get_max_alloc() */ + inline uint16_t getMaxFreeBlockSize() { return lt_heap_get_max_alloc(); } + + /** @copydoc LT_VERSION_STR() */ + inline const char *getSdkVersion() { return LT_VERSION_STR; } + + /** @copydoc LT_VERSION_STR() */ + inline String getCoreVersion() { return LT_VERSION_STR; } + + /** @copydoc LT_BANNER_STR() */ + inline String getFullVersion() { return LT_BANNER_STR; } + + inline uint8_t getBootVersion() { return 0; } + + inline uint8_t getBootMode() { return 0; } + + /** @copydoc lt_cpu_get_freq_mhz() */ + inline uint8_t getCpuFreqMHz() { return lt_cpu_get_freq_mhz(); } + + /** @copydoc lt_flash_get_id() */ + inline uint32_t getFlashChipId() { + lt_flash_id_t id = lt_flash_get_id(); + return id.manufacturer_id << 16 | id.chip_id << 7 | id.chip_size_id; + } + + /** @copydoc lt_flash_get_id() */ + inline uint8_t getFlashChipVendorId() { return lt_flash_get_id().manufacturer_id; } + + /** @copydoc lt_flash_get_size() */ + inline uint32_t getFlashChipRealSize() { return lt_flash_get_size(); } + + /** @copydoc lt_flash_get_size() */ + inline uint32_t getFlashChipSize() { return lt_flash_get_size(); } + + inline uint32_t getFlashChipMode() { return 0xFF; } + + /** @copydoc lt_flash_get_size() */ + inline uint32_t getFlashChipSizeByChipId() { return lt_flash_get_size(); } + + /** @copydoc lt_flash_erase_block() */ + inline bool flashEraseSector(uint32_t sector) { return lt_flash_erase_block(sector); } + + /** @copydoc lt_flash_write() */ + inline bool flashWrite(uint32_t address, const uint8_t *data, size_t size) { + return lt_flash_write(address, data, size) == size; + } + + /** @copydoc lt_flash_read() */ + inline bool flashRead(uint32_t address, uint8_t *data, size_t size) { + return lt_flash_read(address, data, size) == size; + } + + /** @copydoc lt_get_reboot_reason_name() */ + inline String getResetReason() { return lt_get_reboot_reason_name(lt_get_reboot_reason()); } + + /** @copydoc lt_get_reboot_reason_name() */ + inline String getResetInfo() { return lt_get_reboot_reason_name(lt_get_reboot_reason()); } + + /** @copydoc lt_rand_bytes() */ + inline uint8_t *random(uint8_t *resultArray, const size_t outputSizeBytes) { + lt_rand_bytes(resultArray, (size_t)outputSizeBytes); + return resultArray; + } + + /** @copydoc lt_rand_bytes() */ + inline uint32_t random() { + uint32_t i; + lt_rand_bytes((uint8_t *)&i, 4); + return i; + } + + /** @copydoc lt_cpu_get_cycle_count() */ + inline uint32_t getCycleCount() { return lt_cpu_get_cycle_count(); } +}; + +extern EspClass ESP; + +#endif diff --git a/cores/common/arduino/libraries/inline/Flash/Flash.h b/cores/common/arduino/libraries/inline/Flash/Flash.h index 7a4290e..248e45d 100644 --- a/cores/common/arduino/libraries/inline/Flash/Flash.h +++ b/cores/common/arduino/libraries/inline/Flash/Flash.h @@ -27,7 +27,7 @@ class FlashClass { } /** @copydoc lt_flash_write() */ - inline bool writeBlock(uint32_t offset, uint8_t *data, size_t length) { + inline bool writeBlock(uint32_t offset, const uint8_t *data, size_t length) { // return lt_flash_write(offset, data, length) == length; } diff --git a/cores/common/arduino/libraries/inline/LT/LT.h b/cores/common/arduino/libraries/inline/LT/LT.h index a284a5d..258a94a 100644 --- a/cores/common/arduino/libraries/inline/LT/LT.h +++ b/cores/common/arduino/libraries/inline/LT/LT.h @@ -3,6 +3,7 @@ #pragma once #include +#include #include #include diff --git a/cores/common/arduino/libraries/inline/Singletons.cpp b/cores/common/arduino/libraries/inline/Singletons.cpp index cfb6106..7ea51ee 100644 --- a/cores/common/arduino/libraries/inline/Singletons.cpp +++ b/cores/common/arduino/libraries/inline/Singletons.cpp @@ -7,4 +7,5 @@ LibreTuya LT; LibreTuyaOTA OTA; LibreTuyaWDT WDT; +EspClass ESP; FlashClass Flash; diff --git a/cores/common/base/api/lt_flash.h b/cores/common/base/api/lt_flash.h index 0551907..7876d5c 100644 --- a/cores/common/base/api/lt_flash.h +++ b/cores/common/base/api/lt_flash.h @@ -52,4 +52,4 @@ uint32_t lt_flash_read(uint32_t offset, uint8_t *data, size_t length); * @param length length of data to write * @return length of data successfully written (should equal 'length') */ -uint32_t lt_flash_write(uint32_t offset, uint8_t *data, size_t length); +uint32_t lt_flash_write(uint32_t offset, const uint8_t *data, size_t length); diff --git a/cores/common/base/config/printf_config.h b/cores/common/base/config/printf_config.h index 2b0d921..2588c06 100644 --- a/cores/common/base/config/printf_config.h +++ b/cores/common/base/config/printf_config.h @@ -68,7 +68,25 @@ void putchar_p(char c, unsigned long port); #endif // LT_UART_SILENT_ENABLED && !LT_UART_SILENT_ALL -#if LT_UART_SILENT_ALL +#if !LT_UART_SILENT_ENABLED + +#define WRAP_PRINTF(name) \ + WRAP_DISABLE_DECL(name) \ + int __wrap_##name(const char *format, ...) { \ + va_list va; \ + va_start(va, format); \ + const int ret = vprintf(format, va); \ + va_end(va); \ + return ret; \ + } + +#define WRAP_VPRINTF(name) \ + WRAP_DISABLE_DECL(name) \ + int __wrap_##name(const char *format, va_list arg) { \ + return vprintf(format, arg); \ + } + +#elif LT_UART_SILENT_ALL #define WRAP_PRINTF(name) \ WRAP_DISABLE_DECL(name) \ @@ -82,7 +100,7 @@ void putchar_p(char c, unsigned long port); return 0; \ } -#else // !LT_UART_SILENT_ALL +#else // !LT_UART_SILENT_ENABLED || !LT_UART_SILENT_ALL #define WRAP_PRINTF(name) \ WRAP_DISABLE_DECL(name) \ @@ -102,7 +120,7 @@ void putchar_p(char c, unsigned long port); return vprintf(format, arg); \ } -#endif // !LT_UART_SILENT_ALL +#endif // !LT_UART_SILENT_ENABLED || !LT_UART_SILENT_ALL #define WRAP_SPRINTF(name) \ int __wrap_##name(char *s, const char *format, ...) { \ diff --git a/cores/common/base/lt_api.c b/cores/common/base/lt_api.c index 0ea6995..d8af241 100644 --- a/cores/common/base/lt_api.c +++ b/cores/common/base/lt_api.c @@ -152,7 +152,7 @@ uint32_t lt_flash_read(uint32_t offset, uint8_t *data, size_t length) { return fal_partition_read(fal_root_part, offset, data, length); } -uint32_t lt_flash_write(uint32_t offset, uint8_t *data, size_t length) { +uint32_t lt_flash_write(uint32_t offset, const uint8_t *data, size_t length) { return fal_partition_write(fal_root_part, offset, data, length); } diff --git a/docs/dev/config.md b/docs/dev/config.md index 465de17..a2b40b4 100644 --- a/docs/dev/config.md +++ b/docs/dev/config.md @@ -83,7 +83,7 @@ Options for controlling default UART log output. - `LT_UART_DEFAULT_LOGGER` (unset) - override default output port for LT logger only - `LT_UART_DEFAULT_SERIAL` (unset) - override default output port for `Serial` class (without a number) - `LT_UART_SILENT_ENABLED` (1) - enable auto-silencing of SDK "loggers"; this makes the serial output much more readable, but can hide some error messages -- `LT_UART_SILENT_ALL` (0) - disable all SDK output (LT output and logger still work) +- `LT_UART_SILENT_ALL` (0) - disable all SDK output (LT output and logger still work); since v1.0.0 this has no effect if `LT_UART_SILENT_ENABLED` is 0 !!! info Values 0, 1 and 2 correspond to physical UART port numbers (refer to board pinout for the available ports). diff --git a/docs/dev/lt-api.md b/docs/dev/lt-api.md index c3ac648..8f50b9d 100644 --- a/docs/dev/lt-api.md +++ b/docs/dev/lt-api.md @@ -138,6 +138,20 @@ This API is available using: end="# Detailed Description" %} +### ESP (compatibility class) + +{% + include-markdown "../../ltapi/class_esp_class.md" + start="# Detailed Description\n" + end="## Public Functions Documentation" +%} + +{% + include-markdown "../../ltapi/class_esp_class.md" + start="## Public Functions\n" + end="# Detailed Description" +%} + ### Arduino custom API These functions extend the standard Wiring (Arduino) library, to provide additional features.