[beken-72xx] Add preliminary Arduino framework core

This commit is contained in:
Kuba Szczodrzyński
2022-06-19 23:47:44 +02:00
parent 848df066c4
commit 0dda769d0f
16 changed files with 363 additions and 31 deletions

View File

@@ -12,6 +12,7 @@
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/kuba2k2/platform/libretuya.svg)](https://registry.platformio.org/platforms/kuba2k2/libretuya)
![RTL8710BN](https://img.shields.io/badge/-rtl8710bn-blue)
![BK7231](https://img.shields.io/badge/-bk7231-blue)
</div>
@@ -40,33 +41,33 @@ See [Boards & CPU list](https://kuba2k2.github.io/libretuya/docs/supported/).
Note: this list will probably change with each functionality update.
&nbsp; | `realtek-ambz`
--------------------|---------------
Core functions | ✔️
GPIO/PWM/IRQ | ✔️/✔️/✔️
Analog input | ✔️
UART I/O | ✔️
Flash I/O | ✔️
**CORE LIBRARIES** |
SoftwareSerial | ❌
SPI | ❌
Wire | ❗
**OTHER LIBRARIES** |
Wi-Fi STA/AP/Mixed | ✔️
Wi-Fi Events | ✔️
TCP Client (SSL) | ✔️ (✔️)
TCP Server | ✔️
IPv6 | ❌
HTTP Client (SSL) | ✔️ (✔️)
HTTP Server | ✔️
NVS / Preferences | ❌
SPIFFS | ❌
BLE | -
NTP | ❌
OTA | ✔️
MDNS | ✔️
MQTT | ✅
SD | ❌
&nbsp; | `realtek-ambz` | `beken-72xx`
--------------------|----------------|-------------
Core functions | ✔️ | ✔️
GPIO/PWM/IRQ | ✔️/✔️/✔️ | ❌/❌/❌
Analog input | ✔️ | ❌
UART I/O | ✔️ | ❌
Flash I/O | ✔️ | ✔️
**CORE LIBRARIES** | |
SoftwareSerial | ❌ | ❌
SPI | ❌ | ❌
Wire | ❗ | ❌
**OTHER LIBRARIES** | |
Wi-Fi STA/AP/Mixed | ✔️ | ❌
Wi-Fi Events | ✔️ | ❌
TCP Client (SSL) | ✔️ (✔️) | ❌
TCP Server | ✔️ | ❌
IPv6 | ❌ | ❌
HTTP Client (SSL) | ✔️ (✔️) | ❌
HTTP Server | ✔️ | ❌
NVS / Preferences | ❌ | ❌
SPIFFS | ❌ | ❌
BLE | - | ❌
NTP | ❌ | ❌
OTA | ✔️ | ❌
MDNS | ✔️ | ❌
MQTT | ✅ | ❌
SD | ❌ | ❌
Symbols:

View File

@@ -0,0 +1,15 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-14. */
#pragma once
#ifdef __cplusplus
#include "WCharacterFixup.h"
#endif
#include <api/ArduinoAPI.h>
#include <core/LibreTuyaAPI.h>
// Include family-specific code
#include "WVariant.h"
// Include board variant
#include "variant.h"

View File

@@ -0,0 +1,121 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
#include <LibreTuyaAPI.h>
// can't include <flash.h> as it collides with <Flash.h> on Windows -_-
#define REG_FLASH_BASE 0x00803000
#define REG_FLASH_OPERATE_SW (REG_FLASH_BASE + 0 * 4)
#define REG_FLASH_RDID (REG_FLASH_BASE + 4 * 4)
#define FLASH_BUSY_SW (0x01UL << 31)
#define FLASH_WP_VALUE (0x01UL << 30)
#define FLASH_OP_SW (0x01UL << 29)
#define FLASH_OP_TYPE_POS 24
#define FLASH_OP_RDID 20
extern "C" {
#include <flash_pub.h>
#include <sys_ctrl.h>
#include <sys_rtos.h>
}
void LibreTuya::restart() {
// TODO
}
/* CPU-related */
ChipType LibreTuya::getChipType() {
uint8_t chipId = *(uint8_t *)(SCTRL_CHIP_ID);
return CHIP_TYPE_ENUM(FAMILY, chipId);
}
const char *LibreTuya::getChipModel() {
return STRINGIFY_MACRO(MCU);
}
uint32_t LibreTuya::getChipId() {
// TODO determine if this really is unique
uint32_t chipId = REG_READ(SCTRL_DEVICE_ID);
chipId &= 0xFFFFFF;
return chipId;
}
uint8_t LibreTuya::getChipCores() {
return 1;
}
const char *LibreTuya::getChipCoreType() {
return "ARM968E-S";
}
uint32_t LibreTuya::getCpuFreq() {
// TODO
return 0;
}
uint32_t LibreTuya::getCycleCount() {
// TODO
return 0;
}
/* Flash memory utilities */
FlashId LibreTuya::getFlashChipId() {
uint32_t data = (FLASH_OP_RDID << FLASH_OP_TYPE_POS) | FLASH_OP_SW | FLASH_WP_VALUE;
REG_WRITE(REG_FLASH_OPERATE_SW, data);
while (REG_READ(REG_FLASH_OPERATE_SW) & FLASH_BUSY_SW) {}
FlashId id = {
.manufacturerId = REG_RD8(REG_FLASH_RDID, 2),
.chipId = REG_RD8(REG_FLASH_RDID, 1),
.chipSizeId = REG_RD8(REG_FLASH_RDID, 0),
};
return id;
}
/* 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::otaGetStoredIndex() {
return 1;
}
bool LibreTuya::otaSupportsDual() {
return false;
}
bool LibreTuya::otaHasImage1() {
return true;
}
bool LibreTuya::otaHasImage2() {
return false;
}
bool LibreTuya::otaSwitch(bool force) {
return true;
}
/* Global instance */
LibreTuya LT;

View File

@@ -0,0 +1,14 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-18. */
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "sdk_extern.h"
#include "sdk_mem.h"
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -0,0 +1,25 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
#include <Arduino.h>
extern "C" {
#include <rtos_pub.h>
#include <sys_rtos.h>
}
beken_thread_t mainThread;
bool startMainTask() {
OSStatus ret = rtos_create_thread(
&mainThread,
THD_APPLICATION_PRIORITY,
"main",
(beken_thread_function_t)main_task,
4096,
NULL
);
if (ret != kNoErr)
return false;
vTaskStartScheduler();
return true;
}

View File

@@ -0,0 +1,19 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-18. */
#pragma once
// for printf() etc (they are wrapped anyway)
#include <stdio.h>
// most stuff is here
#include <include.h>
// for os_printf
#include <uart_pub.h>
// for GPIO names
#include <gpio_pub.h>
// conflict with stl_algobase.h
#undef min
#undef max
extern unsigned char __disable_bk_printf;

View File

@@ -0,0 +1,20 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-18. */
#pragma once
#include <stddef.h>
// Beken SDK is actually pretty good, in terms of declaring
// stdlib functions properly! So no need for any #define hell.
#include <mem_pub.h>
// All the MemMang functions are in stdlib, just wrapped
// during linking.
#include <stdlib.h>
// for memcpy etc.
#include <string.h>
// ...except zalloc, which is apparently not in the stdlib
#define zalloc os_zalloc
#define LT_HEAP_FUNC xPortGetFreeHeapSize

View File

@@ -0,0 +1,21 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
#include <Arduino.h>
#include <rtos_pub.h>
#include <sys_rtos.h>
// TODO work out this conflicting with func/misc/target_util.c
/* void delay(unsigned long ms) {
rtos_delay_milliseconds(ms);
} */
void delayMicroseconds(unsigned int us) {}
uint32_t millis() {
return xTaskGetTickCount() * portTICK_PERIOD_MS;
}
void yield() {
vTaskDelay(1);
taskYIELD();
}

View File

@@ -0,0 +1,45 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
#include <sdk_extern.h>
#include <fal.h>
#include <flash_pub.h>
#define FLASH_ERASE_MIN_SIZE (4 * 1024)
extern uint32_t flash_ctrl(uint32_t cmd, void *param);
static int init() {
__disable_bk_printf = 1;
flash_init();
__disable_bk_printf = 0;
return 0;
}
static int read(long offset, uint8_t *buf, size_t size) {
flash_read((char *)buf, size, offset);
return size;
}
static int write(long offset, const uint8_t *buf, size_t size) {
flash_write((char *)buf, size, offset);
return size;
}
static int erase(long offset, size_t size) {
size = ((size - 1) / FLASH_ERASE_MIN_SIZE) + 1;
for (uint16_t i = 0; i < size; i++) {
uint32_t addr = offset + i * FLASH_ERASE_MIN_SIZE;
flash_ctrl(CMD_FLASH_ERASE_SECTOR, &addr);
}
return size * FLASH_ERASE_MIN_SIZE;
}
const struct fal_flash_dev flash0 = {
.name = FAL_FLASH_DEV_NAME,
.addr = 0x0,
.len = FLASH_LENGTH,
.blk_size = FLASH_ERASE_MIN_SIZE,
.ops = {init, read, write, erase},
.write_gran = 1,
};

View File

@@ -0,0 +1,11 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
#include <stdarg.h>
#include <stdio.h>
// include bk_send_byte()
#include <uart_pub.h>
void putchar_(char c) {
uart_write_byte(uart_print_port, c);
}

View File

@@ -17,7 +17,8 @@
]
},
"frameworks": [
"beken-72xx-sdk"
"beken-72xx-sdk",
"beken-72xx-arduino"
],
"upload": {
"maximum_ram_size": 262144,

View File

@@ -0,0 +1,28 @@
# Copyright (c) Kuba Szczodrzyński 2022-06-14.
from SCons.Script import DefaultEnvironment
env = DefaultEnvironment()
# SDK options
env.Replace(
LIB_BDK_DRIVER_SKIP=[
# using printf library wrappers instead
"uart/printf.c",
]
)
env.SConscript("beken-72xx-sdk.py", exports="env")
env.SConscript("../arduino-common.py", exports="env")
# Flags
env.Append(
CCFLAGS=[
"-Wno-write-strings",
"-Wno-char-subscripts",
"-Wno-missing-braces",
],
)
# Build all libraries
env.BuildLibraries()

View File

@@ -87,7 +87,6 @@ env.Append(
"-marm",
"-mthumb-interwork",
"-g",
"-nostdlib",
"-nostartfiles",
"--specs=nano.specs",
"-Wl,--gc-sections",
@@ -104,6 +103,7 @@ env.Append(
"-Wl,-wrap,snprintf",
"-Wl,-wrap,sprintf",
"-Wl,-wrap,vsnprintf",
# for disabling os_printf() output
"-Wl,-wrap,bk_printf",
],
)

View File

@@ -6,7 +6,7 @@ Realtek Ameba1 | `-`
[Realtek AmebaZ](https://www.amebaiot.com/en/amebaz/) | `realtek-ambz` | `ambz` | `RTL8710B` (0x22E0D6FC) | ✔️ | `framework-realtek-amb1` ([amb1_sdk](https://github.com/ambiot/amb1_sdk))
Realtek AmebaZ2 | `-` | `-` | `RTL8720C` (0xE08F7564) | ❌ | -
Realtek AmebaD | `-` | `-` | `RTL8720D` (0x3379CFE2) | ❌ | -
[Beken 7231T](http://www.bekencorp.com/en/goods/detail/cid/7.html) | `beken-7231t` (`beken-72xx`) | `bk7231t` (`bk72xx`) | `BK7231T` (0x675A40B0) | | `framework-beken-bdk` ([bdk_freertos](https://github.com/bekencorp/bdk_freertos))
[Beken 7231T](http://www.bekencorp.com/en/goods/detail/cid/7.html) | `beken-7231t` (`beken-72xx`) | `bk7231t` (`bk72xx`) | `BK7231T` (0x675A40B0) | ✔️ | `framework-beken-bdk` ([bdk_freertos](https://github.com/bekencorp/bdk_freertos))
Beken 7231N | `-` | `-` | `BK7231N` (0x7B3EF230) | ❌ | -
Boufallo 602 | `-` | `-` | `BL602` (0xDE1270B7) | ❌ | -
Xradiotech 809 | `-` | `-` | `XR809` (0x51E903A8) | ❌ | -

View File

@@ -26,6 +26,11 @@
"title": "Beken 72XX - SDK",
"package": "framework-beken-bdk",
"script": "builder/frameworks/beken-72xx-sdk.py"
},
"beken-72xx-arduino": {
"title": "Beken 72XX - Arduino",
"package": "framework-beken-bdk",
"script": "builder/frameworks/beken-72xx-arduino.py"
}
},
"packages": {

View File

@@ -32,7 +32,13 @@ class Family:
def has_arduino_core(self) -> bool:
if not self.name:
return False
return isdir(join(dirname(__file__), "..", "..", "arduino", self.name))
if isdir(join(dirname(__file__), "..", "..", "arduino", self.name)):
return True
if not self.parent:
return False
if isdir(join(dirname(__file__), "..", "..", "arduino", self.parent)):
return True
return False
def dict(self) -> dict:
return dict(