[core] Refactor LT class into C methods
This commit is contained in:
67
cores/common/base/api/lt_cpu.h
Normal file
67
cores/common/base/api/lt_cpu.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2023-03-09. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libretuya.h>
|
||||
|
||||
/**
|
||||
* @brief Get CPU family ID (in ChipFamily enumeration).
|
||||
*/
|
||||
lt_cpu_family_t lt_get_cpu_family();
|
||||
|
||||
/**
|
||||
* @brief Get CPU family name as string.
|
||||
*/
|
||||
const char *lt_get_cpu_family_name();
|
||||
|
||||
/**
|
||||
* @brief Get CPU model ID (in ChipType enumeration).
|
||||
*/
|
||||
lt_cpu_model_t lt_get_cpu_model();
|
||||
|
||||
/**
|
||||
* @brief Get CPU model name as string (uppercase).
|
||||
*/
|
||||
const char *lt_get_cpu_model_name();
|
||||
|
||||
/**
|
||||
* @brief Get CPU model name as string (lowercase).
|
||||
*/
|
||||
const char *lt_get_cpu_model_code();
|
||||
|
||||
/**
|
||||
* @brief Get CPU unique ID. This may be based on MAC, eFuse, etc. (family-specific).
|
||||
* Note: the number is 24-bit (with the MSB being zero).
|
||||
*/
|
||||
uint32_t lt_get_cpu_unique_id();
|
||||
|
||||
/**
|
||||
* @brief Get CPU ID based on the last three octets of MAC address.
|
||||
* Note: the number is 24-bit (with the MSB being zero).
|
||||
*/
|
||||
uint32_t lt_get_cpu_mac_id();
|
||||
|
||||
/**
|
||||
* @brief Get CPU core count.
|
||||
*/
|
||||
uint8_t lt_get_cpu_core_count();
|
||||
|
||||
/**
|
||||
* @brief Get CPU core type name as string.
|
||||
*/
|
||||
const char *lt_get_cpu_core_type();
|
||||
|
||||
/**
|
||||
* @brief Get CPU frequency in Hz.
|
||||
*/
|
||||
uint32_t lt_get_cpu_freq();
|
||||
|
||||
/**
|
||||
* @brief Get CPU frequency in MHz.
|
||||
*/
|
||||
uint32_t lt_get_cpu_freq_mhz();
|
||||
|
||||
/**
|
||||
* @brief Get CPU cycle count.
|
||||
*/
|
||||
uint32_t lt_get_cpu_cycle_count();
|
||||
58
cores/common/base/api/lt_device.h
Normal file
58
cores/common/base/api/lt_device.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2023-03-09. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libretuya.h>
|
||||
|
||||
/**
|
||||
* @brief Get LibreTuya version string.
|
||||
*/
|
||||
const char *lt_get_version();
|
||||
|
||||
/**
|
||||
* @brief Get board code.
|
||||
*/
|
||||
const char *lt_get_board_code();
|
||||
|
||||
/**
|
||||
* @brief Get device friendly name in format "LT-<family code>-<MAC ID>".
|
||||
* Can be used as hostname.
|
||||
*/
|
||||
const char *lt_get_device_name();
|
||||
|
||||
/**
|
||||
* @brief Reboot the CPU.
|
||||
*/
|
||||
void lt_reboot();
|
||||
|
||||
/**
|
||||
* @brief Reboot the CPU with a watchdog timeout (if possible).
|
||||
*
|
||||
* @return whether WDT reboot is possible
|
||||
*/
|
||||
bool lt_reboot_wdt();
|
||||
|
||||
/**
|
||||
* @brief Reboot the CPU and stay in download mode (if possible).
|
||||
*
|
||||
* @return whether download-mode reboot is possible
|
||||
*/
|
||||
bool lt_reboot_download_mode();
|
||||
|
||||
/**
|
||||
* @brief Get the reason of last chip reboot.
|
||||
*/
|
||||
lt_reboot_reason_t lt_get_reboot_reason();
|
||||
|
||||
/**
|
||||
* @brief Get a textual representation of a reboot reason.
|
||||
*
|
||||
* @param reason value to convert to text, pass 0 to read from lt_reboot_get_reason()
|
||||
*/
|
||||
const char *lt_get_reboot_reason_name(lt_reboot_reason_t reason);
|
||||
|
||||
/**
|
||||
* @brief Reconfigure GPIO pins used for debugging
|
||||
* (SWD/JTAG), so that they can be used as normal I/O.
|
||||
*/
|
||||
void lt_gpio_recover();
|
||||
55
cores/common/base/api/lt_flash.h
Normal file
55
cores/common/base/api/lt_flash.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2023-03-09. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libretuya.h>
|
||||
|
||||
/**
|
||||
* @brief Read flash chip ID and return a lt_flash_id_t struct.
|
||||
*/
|
||||
lt_flash_id_t lt_flash_get_id();
|
||||
|
||||
/**
|
||||
* @brief Get flash chip total size.
|
||||
*
|
||||
* The default implementation uses the least significant
|
||||
* byte of the chip ID to determine the size.
|
||||
*/
|
||||
uint32_t lt_flash_get_size();
|
||||
|
||||
/**
|
||||
* @brief Erase flash area. Flash can only be erased in blocks (usually 4 KiB).
|
||||
*
|
||||
* @param offset starting offset to erase (in bytes); must be multiple of the flash chip's block size
|
||||
* @param length length of data to erase (in bytes); will be rounded up to block size
|
||||
* @return whether erasing was successful
|
||||
*/
|
||||
bool lt_flash_erase(uint32_t offset, size_t length);
|
||||
|
||||
/**
|
||||
* @brief Erase a single block of flash (usually 4 KiB).
|
||||
*
|
||||
* @param offset offset of the block (in bytes); must be multiple of the flash chip's block size
|
||||
* @return whether erasing was successful
|
||||
*/
|
||||
bool lt_flash_erase_block(uint32_t offset);
|
||||
|
||||
/**
|
||||
* @brief Read data from the flash.
|
||||
*
|
||||
* @param offset starting offset (in bytes)
|
||||
* @param data pointer to where to store the data
|
||||
* @param length length of data to read
|
||||
* @return whether reading was successful (i.e. all bytes were successfully read)
|
||||
*/
|
||||
bool lt_flash_read(uint32_t offset, uint8_t *data, size_t length);
|
||||
|
||||
/**
|
||||
* @brief Write data to the flash.
|
||||
*
|
||||
* @param offset starting offset (in bytes)
|
||||
* @param data pointer to data to write
|
||||
* @param length length of data to write
|
||||
* @return whether writing was successful (i.e. all bytes were successfully written)
|
||||
*/
|
||||
bool lt_flash_write(uint32_t offset, uint8_t *data, size_t length);
|
||||
26
cores/common/base/api/lt_init.h
Normal file
26
cores/common/base/api/lt_init.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2023-02-27. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libretuya.h>
|
||||
|
||||
/**
|
||||
* @brief Initialize the family core (optional).
|
||||
* This method is family-specific; the family core can do whatever it wants to.
|
||||
* This method is empty if not implemented, and shouldn't be called manually.
|
||||
*/
|
||||
void lt_init_family() __attribute__((weak));
|
||||
|
||||
/**
|
||||
* @brief Initialize the board (variant).
|
||||
* This method is empty if not implemented (which is usually the case),
|
||||
* and shouldn't be called manually.
|
||||
*/
|
||||
void lt_init_variant() __attribute__((weak));
|
||||
|
||||
/**
|
||||
* @brief Initialize the family's Arduino core (optional).
|
||||
* This method is family-specific; the family core can do whatever it wants to.
|
||||
* This method is empty if not implemented, and shouldn't be called manually.
|
||||
*/
|
||||
void lt_init_arduino() __attribute__((weak));
|
||||
30
cores/common/base/api/lt_mem.h
Normal file
30
cores/common/base/api/lt_mem.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2023-03-09. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libretuya.h>
|
||||
|
||||
/**
|
||||
* @brief Get total RAM size.
|
||||
*/
|
||||
uint32_t lt_get_ram_size();
|
||||
|
||||
/**
|
||||
* @brief Get total heap size.
|
||||
*/
|
||||
uint32_t lt_get_heap_size();
|
||||
|
||||
/**
|
||||
* @brief Get free heap size.
|
||||
*/
|
||||
uint32_t lt_get_heap_free();
|
||||
|
||||
/**
|
||||
* @brief Get lowest level of free heap memory.
|
||||
*/
|
||||
uint32_t lt_get_heap_min_free();
|
||||
|
||||
/**
|
||||
* @brief Get largest block of heap that can be allocated at once.
|
||||
*/
|
||||
uint32_t lt_get_heap_max_alloc();
|
||||
60
cores/common/base/api/lt_ota.h
Normal file
60
cores/common/base/api/lt_ota.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2023-03-09. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libretuya.h>
|
||||
#include <uf2ota/uf2types.h>
|
||||
|
||||
/**
|
||||
* @brief Get OTA type of the device's chip.
|
||||
*/
|
||||
lt_ota_type_t lt_ota_get_type();
|
||||
|
||||
/**
|
||||
* @brief Check if the specified OTA image is valid.
|
||||
*
|
||||
* @param index OTA index to check; 0 for single-OTA chips, 1 or 2 for dual-OTA chips
|
||||
* @return true if index is valid for the chip's OTA type, and there is a valid image; false otherwise
|
||||
*/
|
||||
bool lt_ota_is_valid(uint8_t index);
|
||||
|
||||
/**
|
||||
* @brief Check if OTA rollback is possible (switching the stored index to another partition).
|
||||
*
|
||||
* Note that this is not the same as "switching" OTA with revert=true.
|
||||
*
|
||||
* @return true if 2nd image is valid and the chip is dual-OTA; false otherwise
|
||||
*/
|
||||
bool lt_ota_can_rollback();
|
||||
|
||||
/**
|
||||
* @brief Get the currently running firmware's OTA index.
|
||||
*
|
||||
* @return OTA index if dual-OTA is supported, 0 otherwise
|
||||
*/
|
||||
uint8_t lt_ota_dual_get_current();
|
||||
|
||||
/**
|
||||
* @brief Read the currently active OTA index, i.e. the one that will boot upon restart.
|
||||
*
|
||||
* @return OTA index if dual-OTA is supported, 0 otherwise
|
||||
*/
|
||||
uint8_t lt_ota_dual_get_stored();
|
||||
|
||||
/**
|
||||
* @brief Check which UF2 OTA scheme should be used for applying firmware updates.
|
||||
*
|
||||
* @return OTA scheme of the target partition
|
||||
*/
|
||||
uf2_ota_scheme_t lt_ota_get_uf2_scheme();
|
||||
|
||||
/**
|
||||
* @brief Try to switch OTA index to the other image. For single-OTA chips, only check if the upgrade image is valid.
|
||||
*
|
||||
* This can be used to "activate" the upgrade after flashing.
|
||||
*
|
||||
* @param revert switch if (and only if) the other image is already marked as active (i.e.
|
||||
* switch back to the running image)
|
||||
* @return false if the second image (or upgrade image) is not valid; false if writing failed; true otherwise
|
||||
*/
|
||||
bool lt_ota_switch(bool revert);
|
||||
47
cores/common/base/api/lt_utils.h
Normal file
47
cores/common/base/api/lt_utils.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2023-02-27. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libretuya.h>
|
||||
|
||||
// https://stackoverflow.com/a/3437484
|
||||
#define MAX(a, b) \
|
||||
({ \
|
||||
__typeof__(a) _a = (a); \
|
||||
__typeof__(b) _b = (b); \
|
||||
_a > _b ? _a : _b; \
|
||||
})
|
||||
#define MIN(a, b) \
|
||||
({ \
|
||||
__typeof__(a) _a = (a); \
|
||||
__typeof__(b) _b = (b); \
|
||||
_a < _b ? _a : _b; \
|
||||
})
|
||||
|
||||
/**
|
||||
* @brief Generate random bytes using rand().
|
||||
*
|
||||
* @param buf destination pointer
|
||||
* @param len how many bytes to generate
|
||||
*/
|
||||
void lt_rand_bytes(uint8_t *buf, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Print data pointed to by buf in hexdump-like format (hex+ASCII).
|
||||
*
|
||||
* @param buf source pointer
|
||||
* @param len how many bytes to print
|
||||
* @param offset increment printed offset by this value
|
||||
* @param width how many bytes on a line
|
||||
*/
|
||||
void hexdump(
|
||||
const uint8_t *buf,
|
||||
size_t len,
|
||||
#ifdef __cplusplus
|
||||
uint32_t offset = 0,
|
||||
uint8_t width = 16
|
||||
#else
|
||||
uint32_t offset,
|
||||
uint8_t width
|
||||
#endif
|
||||
);
|
||||
23
cores/common/base/api/lt_wdt.h
Normal file
23
cores/common/base/api/lt_wdt.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2023-03-09. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libretuya.h>
|
||||
|
||||
/**
|
||||
* @brief Enable the hardware watchdog.
|
||||
*
|
||||
* @param timeout watchdog timeout, milliseconds
|
||||
* @return whether the chip has a hardware watchdog
|
||||
*/
|
||||
bool lt_wdt_enable(uint32_t timeout);
|
||||
|
||||
/**
|
||||
* @brief Disable the hardware watchdog.
|
||||
*/
|
||||
void lt_wdt_disable();
|
||||
|
||||
/**
|
||||
* @brief Feed/reset the hardware watchdog timer.
|
||||
*/
|
||||
void lt_wdt_feed();
|
||||
Reference in New Issue
Block a user