From b073290989db3a7fedbe56435f1538c775d525fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Tue, 23 May 2023 19:54:00 +0200 Subject: [PATCH] [realtek-ambz2] Implement base C API --- cores/beken-72xx/base/lt_api.c | 4 - cores/common/base/api/lt_device.h | 2 +- cores/common/base/lt_api.c | 30 +++-- cores/common/base/lt_main.c | 2 +- cores/common/base/lt_types.h | 9 +- cores/realtek-amb/base/fixups/basic_types.h | 8 ++ .../realtek-amb/base/fixups/platform_stdlib.h | 6 +- cores/realtek-amb/base/lt_api.c | 42 +++++++ .../base/port/fal_flash_ambz_port.c | 10 +- cores/realtek-amb/base/sdk_private.h | 18 +-- cores/realtek-ambz/base/lt_api.c | 15 --- cores/realtek-ambz/base/sdk_extern.h | 14 +++ cores/realtek-ambz2/base/fixups/strproc.c | 4 +- cores/realtek-ambz2/base/lt_api.c | 106 +++++++++++++++++- cores/realtek-ambz2/base/sdk_extern.h | 26 +++++ docs/resources/documents.md | 2 +- 16 files changed, 246 insertions(+), 52 deletions(-) create mode 100644 cores/realtek-amb/base/lt_api.c create mode 100644 cores/realtek-ambz/base/sdk_extern.h create mode 100644 cores/realtek-ambz2/base/sdk_extern.h diff --git a/cores/beken-72xx/base/lt_api.c b/cores/beken-72xx/base/lt_api.c index c8994e9..1191cb2 100644 --- a/cores/beken-72xx/base/lt_api.c +++ b/cores/beken-72xx/base/lt_api.c @@ -29,10 +29,6 @@ lt_cpu_model_t lt_cpu_get_model() { return CPU_MODEL_ENUM(FAMILY, chipId); } -uint32_t lt_cpu_get_unique_id() { - return lt_cpu_get_mac_id(); -} - uint32_t lt_cpu_get_mac_id() { uint8_t mac[6]; cfg_load_mac(mac); // force loading MAC from TLV (ignore user-set WiFi MAC) diff --git a/cores/common/base/api/lt_device.h b/cores/common/base/api/lt_device.h index d57560e..36e9a14 100644 --- a/cores/common/base/api/lt_device.h +++ b/cores/common/base/api/lt_device.h @@ -15,7 +15,7 @@ const char *lt_get_version(); const char *lt_get_board_code(); /** - * @brief Get device friendly name in format "LT--". + * @brief Get device friendly name in format "LT--". * Can be used as hostname. */ const char *lt_get_device_name(); diff --git a/cores/common/base/lt_api.c b/cores/common/base/lt_api.c index 7259fd0..df0e15d 100644 --- a/cores/common/base/lt_api.c +++ b/cores/common/base/lt_api.c @@ -35,6 +35,10 @@ const char *lt_cpu_get_model_code() { return STRINGIFY_MACRO(MCULC); } +__attribute__((weak)) uint32_t lt_cpu_get_unique_id() { + return lt_cpu_get_mac_id(); +} + __attribute__((weak)) uint8_t lt_cpu_get_core_count() { return 1; } @@ -85,6 +89,12 @@ const char *lt_get_device_name() { return device_name; } +__attribute__((weak)) void lt_reboot() { + // The Watchdog Way + lt_wdt_enable(1L); + while (1) {} +} + __attribute__((weak)) bool lt_reboot_wdt() { if (!lt_wdt_enable(1L)) return false; @@ -95,24 +105,30 @@ __attribute__((weak)) bool lt_reboot_download_mode() { return false; } +__attribute__((weak)) lt_reboot_reason_t lt_get_reboot_reason() { + return REBOOT_REASON_UNKNOWN; +} + const char *lt_get_reboot_reason_name(lt_reboot_reason_t reason) { if (!reason) reason = lt_get_reboot_reason(); switch (reason) { - case RESET_REASON_POWER: + case REBOOT_REASON_POWER: return "Power-On"; - case RESET_REASON_BROWNOUT: + case REBOOT_REASON_BROWNOUT: return "Brownout"; - case RESET_REASON_HARDWARE: + case REBOOT_REASON_HARDWARE: return "HW Reboot"; - case RESET_REASON_SOFTWARE: + case REBOOT_REASON_SOFTWARE: return "SW Reboot"; - case RESET_REASON_WATCHDOG: + case REBOOT_REASON_WATCHDOG: return "WDT Reset"; - case RESET_REASON_CRASH: + case REBOOT_REASON_CRASH: return "Crash"; - case RESET_REASON_SLEEP: + case REBOOT_REASON_SLEEP: return "Sleep Wakeup"; + case REBOOT_REASON_DEBUGGER: + return "Debugger"; default: return "Unknown"; } diff --git a/cores/common/base/lt_main.c b/cores/common/base/lt_main.c index 92a4bd0..c66eefe 100644 --- a/cores/common/base/lt_main.c +++ b/cores/common/base/lt_main.c @@ -20,7 +20,7 @@ int lt_main(void) { // initialize C library __libc_init_array(); // inform about the reset reason - LT_I("Reset reason: %u", lt_get_reboot_reason()); + LT_I("Reset reason: %s", lt_get_reboot_reason_name(0)); // initialize FAL fal_init(); // provide root partition diff --git a/cores/common/base/lt_types.h b/cores/common/base/lt_types.h index 1f280f1..8d65618 100644 --- a/cores/common/base/lt_types.h +++ b/cores/common/base/lt_types.h @@ -40,8 +40,10 @@ typedef enum { RTL8711BU = CPU_MODEL(F_RTL8710B, 0xFC), // CHIPID_8711BG / QFN68 MX1290 = RTL8710BN, MX1290V2 = RTL8710BX, - // Realtek AmebaZ2 - RTL8720CF = CPU_MODEL(F_RTL8720C, 0x00), // TODO + // Realtek AmebaZ2 (chip_id << 2 | flash_mode) + RTL8720CM = CPU_MODEL(F_RTL8720C, 0xEC), // 0xFB << 2 | 0 + RTL8720CF = CPU_MODEL(F_RTL8720C, 0xED), // 0xFB << 2 | 1 + RTL8720CX = RTL8720CM, // Beken 72XX BK7231T = CPU_MODEL(F_BK7231U, 0x1A), // *SCTRL_CHIP_ID = 0x7231a BK7231N = CPU_MODEL(F_BK7231N, 0x1C), // *SCTRL_CHIP_ID = 0x7231c @@ -63,7 +65,8 @@ typedef enum { REBOOT_REASON_WATCHDOG = 6, REBOOT_REASON_CRASH = 7, REBOOT_REASON_SLEEP = 8, - REBOOT_REASON_MAX = 9, + REBOOT_REASON_DEBUGGER = 9, + REBOOT_REASON_MAX = 10, } lt_reboot_reason_t; /** diff --git a/cores/realtek-amb/base/fixups/basic_types.h b/cores/realtek-amb/base/fixups/basic_types.h index 600279d..8b00424 100644 --- a/cores/realtek-amb/base/fixups/basic_types.h +++ b/cores/realtek-amb/base/fixups/basic_types.h @@ -4,5 +4,13 @@ #ifdef ARDUINO #define boolean boolean_rtl #endif + +#include + +#ifndef bool +#define bool bool +#endif + #include_next "basic_types.h" + #undef boolean diff --git a/cores/realtek-amb/base/fixups/platform_stdlib.h b/cores/realtek-amb/base/fixups/platform_stdlib.h index 1d7af6b..170a1ab 100644 --- a/cores/realtek-amb/base/fixups/platform_stdlib.h +++ b/cores/realtek-amb/base/fixups/platform_stdlib.h @@ -16,9 +16,11 @@ #include #include "basic_types.h" // fixup: replaces typedef boolean for Arduino compatibility -#ifdef LT_RTL8710B -#include "memproc.h" // fixup: redirects to stdlib +#if __has_include() +#include // fixup: redirects to stdlib #endif +#if __has_include() #include "strproc.h" // fixup: redirects to stdlib +#endif #include "diag.h" diff --git a/cores/realtek-amb/base/lt_api.c b/cores/realtek-amb/base/lt_api.c new file mode 100644 index 0000000..8e2ff6a --- /dev/null +++ b/cores/realtek-amb/base/lt_api.c @@ -0,0 +1,42 @@ +/* Copyright (c) Kuba Szczodrzyński 2023-05-23. */ + +#include +#include + +/*______ _ _ + | ____| | | | + | |__ | | __ _ ___| |__ + | __| | |/ _` / __| '_ \ + | | | | (_| \__ \ | | | + |_| |_|\__,_|___/_| |*/ +lt_flash_id_t lt_flash_get_id() { + lt_flash_id_t id; + uint8_t idBytes[3]; + flash_read_id(<_flash_obj, idBytes, 3); + id.manufacturer_id = idBytes[0]; + id.chip_id = idBytes[1]; + id.chip_size_id = idBytes[2]; + return id; +} + +/*_ __ _ _ _ + \ \ / / | | | | | | + \ \ /\ / /_ _| |_ ___| |__ __| | ___ __ _ + \ \/ \/ / _` | __/ __| '_ \ / _` |/ _ \ / _` | + \ /\ / (_| | || (__| | | | (_| | (_) | (_| | + \/ \/ \__,_|\__\___|_| |_|\__,_|\___/ \__, | + __/ | + |___*/ +bool lt_wdt_enable(uint32_t timeout) { + watchdog_init(timeout); + watchdog_start(); + return true; +} + +void lt_wdt_disable() { + watchdog_stop(); +} + +void lt_wdt_feed() { + watchdog_refresh(); +} diff --git a/cores/realtek-amb/base/port/fal_flash_ambz_port.c b/cores/realtek-amb/base/port/fal_flash_ambz_port.c index 8eb033d..9cf5f0a 100644 --- a/cores/realtek-amb/base/port/fal_flash_ambz_port.c +++ b/cores/realtek-amb/base/port/fal_flash_ambz_port.c @@ -7,26 +7,26 @@ #define FLASH_ERASE_MIN_SIZE (4 * 1024) -static flash_t flash_obj; +flash_t lt_flash_obj; static int init() { - flash_get_status(&flash_obj); + flash_get_status(<_flash_obj); return 0; } static int read(long offset, uint8_t *buf, size_t size) { - return size * flash_stream_read(&flash_obj, offset, size, buf); + return size * flash_stream_read(<_flash_obj, offset, size, buf); } static int write(long offset, const uint8_t *buf, size_t size) { - return size * flash_stream_write(&flash_obj, offset, size, (uint8_t *)buf); + return size * flash_stream_write(<_flash_obj, offset, size, (uint8_t *)buf); } static int erase(long offset, size_t size) { offset &= ~(FLASH_ERASE_MIN_SIZE - 1); size = ((size - 1) / FLASH_ERASE_MIN_SIZE) + 1; for (uint16_t i = 0; i < size; i++) { - flash_erase_sector(&flash_obj, offset + i * FLASH_ERASE_MIN_SIZE); + flash_erase_sector(<_flash_obj, offset + i * FLASH_ERASE_MIN_SIZE); } return size * FLASH_ERASE_MIN_SIZE; } diff --git a/cores/realtek-amb/base/sdk_private.h b/cores/realtek-amb/base/sdk_private.h index ffda530..aaf51af 100644 --- a/cores/realtek-amb/base/sdk_private.h +++ b/cores/realtek-amb/base/sdk_private.h @@ -21,6 +21,7 @@ extern "C" { #endif #if LT_RTL8720C #include +#include #include #endif @@ -30,6 +31,10 @@ extern "C" { #include #include +#include +#if LT_RTL8720C +#include +#endif #include #include #include @@ -43,6 +48,10 @@ extern "C" { #include #include +#if __has_include() +#include +#endif + // remove previously defined workarounds #undef PinMode @@ -67,14 +76,7 @@ extern "C" { #define _write __rtl_write #define _sbrk __rtl_sbrk -#define delay_us wait_us - -extern void wait_us(int us); -extern int LOGUART_SetBaud(uint32_t BaudRate); // from fixups/log_uart.c -extern void DumpForOneBytes(void *addr, int cnt); // cnt max 0x70! -extern void SystemCoreClockUpdate(void); - -extern int _sscanf_patch(const char *buf, const char *fmt, ...); +extern flash_t lt_flash_obj; #ifdef __cplusplus } // extern "C" diff --git a/cores/realtek-ambz/base/lt_api.c b/cores/realtek-ambz/base/lt_api.c index a225a14..94624e6 100644 --- a/cores/realtek-ambz/base/lt_api.c +++ b/cores/realtek-ambz/base/lt_api.c @@ -27,10 +27,6 @@ lt_cpu_model_t lt_cpu_get_model() { return CPU_MODEL_ENUM(FAMILY, chipId); } -uint32_t lt_cpu_get_unique_id() { - return lt_cpu_get_mac_id(); -} - uint32_t lt_cpu_get_mac_id() { uint32_t chipId = 0; uint8_t *id = (uint8_t *)&chipId; @@ -61,12 +57,6 @@ uint32_t lt_cpu_get_freq() { | | | |/ _ \ \ / / |/ __/ _ \ | |__| | __/\ V /| | (_| __/ |_____/ \___| \_/ |_|\___\__*/ -void lt_reboot() { - // The Watchdog Way - lt_wdt_enable(1L); - while (1) {} -} - bool lt_reboot_download_mode() { // mww 0x40000138 0x8 HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_NORESET_FF, 0x08); @@ -76,11 +66,6 @@ bool lt_reboot_download_mode() { return true; } -lt_reboot_reason_t lt_get_reboot_reason() { - // TODO - return REBOOT_REASON_UNKNOWN; -} - void lt_gpio_recover() { // PA14 and PA15 are apparently unusable with SWD enabled sys_jtag_off(); diff --git a/cores/realtek-ambz/base/sdk_extern.h b/cores/realtek-ambz/base/sdk_extern.h new file mode 100644 index 0000000..b03191a --- /dev/null +++ b/cores/realtek-ambz/base/sdk_extern.h @@ -0,0 +1,14 @@ +/* Copyright (c) Kuba Szczodrzyński 2023-05-23. */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +int LOGUART_SetBaud(uint32_t BaudRate); // from fixups/log_uart.c +extern void DumpForOneBytes(void *addr, int cnt); // cnt max 0x70! + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/cores/realtek-ambz2/base/fixups/strproc.c b/cores/realtek-ambz2/base/fixups/strproc.c index 18d9de8..3feac76 100644 --- a/cores/realtek-ambz2/base/fixups/strproc.c +++ b/cores/realtek-ambz2/base/fixups/strproc.c @@ -151,7 +151,7 @@ unsigned int atoui(const char *num) { return strproc_stubs.atoui(num); } -size_t strnlen(const char *s, size_t count) { +__attribute__((weak)) size_t strnlen(const char *s, size_t count) { return strproc_stubs.strnlen(s, count); } @@ -163,6 +163,6 @@ int strnicmp(const char *s1, const char *s2, size_t len) { return strproc_stubs.strnicmp(s1, s2, len); } -char *strsep(char **s, const char *ct) { +__attribute__((weak)) char *strsep(char **s, const char *ct) { return strproc_stubs.strsep(s, ct); } diff --git a/cores/realtek-ambz2/base/lt_api.c b/cores/realtek-ambz2/base/lt_api.c index 53b838c..658a4a9 100644 --- a/cores/realtek-ambz2/base/lt_api.c +++ b/cores/realtek-ambz2/base/lt_api.c @@ -13,7 +13,107 @@ void lt_init_family() { lt_uart_port = LT_UART_DEFAULT_PORT; } -lt_reboot_reason_t lt_get_reboot_reason() { - // TODO - return REBOOT_REASON_UNKNOWN; +/* _____ _____ _ _ + / ____| __ \| | | | + | | | |__) | | | | + | | | ___/| | | | + | |____| | | |__| | + \_____|_| \____*/ +lt_cpu_model_t lt_cpu_get_model() { + uint32_t *addr = (uint32_t *)0x40000038; + uint8_t flash_mode = (addr[0] >> 5) & 0b11; + uint32_t chip_id = 0; + hal_get_chip_id(&chip_id); + chip_id <<= 2; + return CPU_MODEL_ENUM(FAMILY, (chip_id & 0xFF) | flash_mode); +} + +uint32_t lt_cpu_get_mac_id() { + uint8_t mac[3]; + efuse_logical_read(0x11A + 3, 3, mac); + return (mac[0] << 0) | (mac[1] << 8) | (mac[2] << 16); +} + +const char *lt_cpu_get_core_type() { + return "ARM Cortex-M4"; +} + +uint32_t lt_cpu_get_freq() { + return hal_syson_query_sys_clk(); +} + +/*_____ _ + | __ \ (_) + | | | | _____ ___ ___ ___ + | | | |/ _ \ \ / / |/ __/ _ \ + | |__| | __/\ V /| | (_| __/ + |_____/ \___| \_/ |_|\___\__*/ +void lt_reboot() { + sys_cpu_reset(); + while (1) {} +} + +bool lt_reboot_download_mode() { + sys_uart_download_mode(); + while (1) {} + return true; +} + +lt_reboot_reason_t lt_get_reboot_reason() { + hal_reset_reason_t reason = -1; + rtl8710c_reset_reason_get(&reason); + switch (reason) { + case HAL_RESET_REASON_POWER_ON: + return REBOOT_REASON_POWER; + case HAL_RESET_REASON_SOFTWARE: + return REBOOT_REASON_SOFTWARE; + case HAL_RESET_REASON_WATCHDOG: + return REBOOT_REASON_WATCHDOG; + case HAL_RESET_REASON_JTAG: + return REBOOT_REASON_DEBUGGER; + default: + return REBOOT_REASON_UNKNOWN; + } +} + +void lt_gpio_recover() { + sys_jtag_off(); +} + +/*__ __ + | \/ | + | \ / | ___ _ __ ___ ___ _ __ _ _ + | |\/| |/ _ \ '_ ` _ \ / _ \| '__| | | | + | | | | __/ | | | | | (_) | | | |_| | + |_| |_|\___|_| |_| |_|\___/|_| \__, | + __/ | + |__*/ +uint32_t lt_ram_get_size() { + return 256 * 1024; +} + +/* ____ _______ + / __ \__ __|/\ + | | | | | | / \ + | | | | | | / /\ \ + | |__| | | |/ ____ \ + \____/ |_/_/ \*/ +lt_ota_type_t lt_ota_get_type() { + return OTA_TYPE_DUAL; +} + +bool lt_ota_is_valid(uint8_t index) { + return false; +} + +uint8_t lt_ota_dual_get_current() { + return 0; +} + +uint8_t lt_ota_dual_get_stored() { + return 0; +} + +bool lt_ota_switch(bool revert) { + return false; } diff --git a/cores/realtek-ambz2/base/sdk_extern.h b/cores/realtek-ambz2/base/sdk_extern.h new file mode 100644 index 0000000..c0270ce --- /dev/null +++ b/cores/realtek-ambz2/base/sdk_extern.h @@ -0,0 +1,26 @@ +/* Copyright (c) Kuba Szczodrzyński 2023-05-23. */ + +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// SDK +void software_reset(); +void sys_uart_download_mode(); +void sys_download_mode(uint8_t mode); + +// blobs +bool Hal_GetPhyEfuseMACAddr(uint32_t xFFFFE5BB, uint8_t *buf); +bool rtw_efuse_map_read(uint32_t xFFFFE5BB, uint32_t offset, uint32_t length, uint8_t *buf); +uint32_t efuse_OneByteRead(uint32_t x33300000, uint32_t addr, uint8_t *buf, uint32_t ldo); +uint32_t EFUSERead8(uint32_t x33300000, uint32_t addr, uint8_t *buf, uint32_t ldo); +uint32_t hal_get_chip_id(uint32_t *id); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/docs/resources/documents.md b/docs/resources/documents.md index 16dd7d5..96bf276 100644 --- a/docs/resources/documents.md +++ b/docs/resources/documents.md @@ -17,4 +17,4 @@ UM0201 | [Ameba Common BT Application User Manual EN](https://raw.githubusercont   | Found elsewhere AN0400 | [Ameba-D Application Note_v3_watermark](https://files.seeedstudio.com/products/102110419/Basic%20documents/AN0400%20Ameba-D%20Application%20Note_v3_watermark.pdf) AN0500 | [Realtek Ameba-ZII application note](https://www.e-paper-display.com/99IOT/00015797-AN0500-Realtek-Ameba-ZII-application-note.en_233850.pdf) -  | [Realtek Ameba-ZII datasheet v0.8](https://www.e-paper-display.com/Ameba-Z_II_DataSheet_v0r8_RTL8720Cx_20190424%29.pdf) +  | [Realtek Ameba-ZII datasheet v0.8](https://web.archive.org/web/20230523175304if_/https://cetest02.cn-bj.ufileos.com/100001_2110255103/RTL872xZ2%20IC%20Datasheet.pdf)