diff --git a/arduino/realtek-ambz/cores/arduino/LibreTuyaAPI.cpp b/arduino/realtek-ambz/cores/arduino/LibreTuyaAPI.cpp new file mode 100644 index 0000000..0b16814 --- /dev/null +++ b/arduino/realtek-ambz/cores/arduino/LibreTuyaAPI.cpp @@ -0,0 +1,97 @@ +/* Copyright (c) Kuba SzczodrzyƄski 2022-05-28. */ + +#include + +#include +#include + +void LibreTuya::restart() { + sys_reset(); +} + +/* CPU-related */ + +ChipType LibreTuya::getChipType() { + uint8_t chipId; + EFUSE_OneByteReadROM(9902, 0xF8, &chipId, L25EOUTVOLTAGE); + return (ChipType)((RTL8710B << 8) | chipId); +} + +const char *LibreTuya::getChipModel() { + return STRINGIFY_MACRO(MCU); +} + +uint32_t LibreTuya::getChipId() { + uint32_t chipId = 0; + uint8_t *id = (uint8_t *)&chipId; + // 9902 was extracted from ROM disassembly, probably not needed + EFUSE_OneByteReadROM(9902, 0x3B, id + 0, L25EOUTVOLTAGE); + EFUSE_OneByteReadROM(9902, 0x3C, id + 1, L25EOUTVOLTAGE); + EFUSE_OneByteReadROM(9902, 0x3D, id + 2, L25EOUTVOLTAGE); + return chipId; +} + +uint8_t LibreTuya::getChipCores() { + return 1; +} + +const char *LibreTuya::getChipCoreType() { + return "ARM Cortex-M4F"; +} + +uint32_t LibreTuya::getCpuFreqMHz() { + return CPU_ClkGet(false) / 1000000; +} + +inline uint32_t LibreTuya::getCycleCount() { + return microsecondsToClockCycles(micros()); +} + +/* Memory management */ + +uint32_t LibreTuya::getRamSize() { + return 256 * 1024; +} + +uint32_t LibreTuya::getHeapSize() { + return configTOTAL_HEAP_SIZE; +} + +uint32_t LibreTuya::getFreeHeap() { + return xPortGetFreeHeapSize(); +} + +uint32_t LibreTuya::getMinFreeHeap() { + return xPortGetMinimumEverFreeHeapSize(); +} + +uint32_t LibreTuya::getMaxAllocHeap() { + return 0; +} + +/* OTA-related */ + +uint8_t LibreTuya::getOtaRunning() { + uint32_t *otaAddress = (uint32_t *)0x8009000; + if (*otaAddress == 0xFFFFFFFF) + return 1; + uint32_t otaCounter = *((uint32_t *)0x8009004); + // what the- + // "LSB 0 bits number is odd/even" + uint8_t count = 0; + for (uint8_t i = 0; i < 32; i++) { + if ((otaCounter & (1 << i)) == 0) + count++; + } + return 1 + (count % 2); +} + +uint8_t LibreTuya::getOtaTarget() { + return getOtaRunning() ^ 0b11; +} + +bool LibreTuya::switchOta() {} + +/* Global instance */ + +LibreTuya LT; diff --git a/arduino/realtek-ambz/cores/arduino/wiring.c b/arduino/realtek-ambz/cores/arduino/wiring.c index ebe7bd1..6e8c69b 100644 --- a/arduino/realtek-ambz/cores/arduino/wiring.c +++ b/arduino/realtek-ambz/cores/arduino/wiring.c @@ -68,7 +68,7 @@ uint32_t millis(void) { uint32_t micros(void) { uint32_t tick1, tick2; uint32_t us; - uint32_t tick_per_us = 166666; + uint32_t tick_per_us = F_CPU / 1000; if (__get_ipsr__() == 0) { tick1 = xTaskGetTickCount(); diff --git a/builder/frameworks/realtek-ambz-sdk.py b/builder/frameworks/realtek-ambz-sdk.py index 8b4606d..6ac2633 100644 --- a/builder/frameworks/realtek-ambz-sdk.py +++ b/builder/frameworks/realtek-ambz-sdk.py @@ -57,7 +57,6 @@ env.Append( CPPDEFINES=[ "M3", "CONFIG_PLATFORM_8711B", - ("F_CPU", "166000000L"), # LwIP options ("LWIP_TIMEVAL_PRIVATE", "0"), ("LWIP_NETIF_HOSTNAME", "1"), # to support hostname changing