[core] Add ESP inline library

This commit is contained in:
Kuba Szczodrzyński
2023-03-20 21:29:57 +01:00
parent c51bf0b7db
commit 63ac7b365d
9 changed files with 162 additions and 7 deletions

View File

@@ -0,0 +1,121 @@
/* Copyright (c) Kuba Szczodrzyński 2023-03-20.() */
#include <Arduino.h>
#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

View File

@@ -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;
}

View File

@@ -3,6 +3,7 @@
#pragma once
#include <Arduino.h>
#include <ESP.h>
#include <OTA.h>
#include <WDT.h>

View File

@@ -7,4 +7,5 @@
LibreTuya LT;
LibreTuyaOTA OTA;
LibreTuyaWDT WDT;
EspClass ESP;
FlashClass Flash;

View File

@@ -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);

View File

@@ -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, ...) { \

View File

@@ -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);
}

View File

@@ -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).

View File

@@ -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.