[beken-72xx] Update base core to new structure

This commit is contained in:
Kuba Szczodrzyński
2023-03-04 11:09:27 +01:00
parent c579219427
commit bc328657aa
58 changed files with 510 additions and 392 deletions

View File

@@ -2,6 +2,10 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
inline void printf_nop(const char *fmt, ...) {}
#define FAL_PRINTF printf_nop
@@ -10,6 +14,10 @@ inline void printf_nop(const char *fmt, ...) {}
// Flash device configuration
extern const struct fal_flash_dev flash0;
#ifdef __cplusplus
} // extern "C"
#endif
#define FAL_FLASH_DEV_NAME "flash0"
#define FAL_FLASH_DEV_TABLE \
@@ -33,6 +41,6 @@ extern const struct fal_flash_dev flash0;
#include <fal_def.h>
/**
* @brief Root partition table, representing the entire flash.
* @brief "Root" partition entry, representing the entire flash.
*/
extern fal_partition_t fal_root_part;

View File

@@ -36,9 +36,8 @@
/* MCU Endian Configuration, default is Little Endian Order. */
// #define FDB_BIG_ENDIAN
#include <printf_config.h>
#if LT_DEBUG_FDB
#include <libretuya.h>
#include <printf/printf.h>
#define FDB_PRINT(...) __wrap_printf(__VA_ARGS__)
#define FDB_DEBUG_ENABLE

View File

@@ -2,7 +2,11 @@
#pragma once
#include <LibreTuyaConfig.h>
#include <lt_config.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#define PRINTF_HAS_DISABLE 1
@@ -127,3 +131,7 @@ void putchar_p(char c, unsigned long port);
int __wrap_##name(char *s, size_t count, const char *format, va_list arg) { \
return vsnprintf(s, count, format, arg); \
}
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -12,18 +12,6 @@
#include <stdlib.h>
#include <string.h>
// C++ standard libraries
#ifdef __cplusplus
#include <algorithm>
#include <cmath>
using ::round;
using std::abs;
using std::isinf;
using std::isnan;
using std::max;
using std::min;
#endif
// LibreTuya version macros
#ifndef LT_VERSION
#define LT_VERSION 1.0.0
@@ -36,25 +24,6 @@ using std::min;
#define LT_VERSION_STR STRINGIFY_MACRO(LT_VERSION)
#define LT_BOARD_STR STRINGIFY_MACRO(LT_BOARD)
// Includes
#include "LibreTuyaClass.h" // global LT class
#include "LibreTuyaCompat.h" // compatibility methods
#include "LibreTuyaConfig.h" // configuration macros
#include "LibreTuyaCustom.h" // family-defined methods (Wiring custom)
#include <Arduino.h>
// C includes
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include "lt_logger.h"
#include "lt_posix_api.h"
#ifdef __cplusplus
} // extern "C"
#endif
// Functional macros
#define LT_BANNER() \
LT_LOG( \
@@ -64,17 +33,18 @@ extern "C" {
"LibreTuya v" LT_VERSION_STR " on " LT_BOARD_STR ", compiled at " __DATE__ " " __TIME__ \
)
#ifdef __cplusplus
String ipToString(const IPAddress &ip);
extern "C" {
void lt_rand_bytes(uint8_t *buf, size_t len);
void hexdump(const uint8_t *buf, size_t len, uint32_t offset = 0, uint8_t width = 16);
}
#else
void lt_rand_bytes(uint8_t *buf, size_t len);
void hexdump(const uint8_t *buf, size_t len, uint32_t offset, uint8_t width);
#endif
// Types & macros
#include "lt_chip.h" // ChipType enum
#include "lt_config.h" // configuration macros
#include "lt_types.h" // other types & enums
// Family-specific macros
#include <lt_family.h>
// Board variant (pin definitions)
#include <variant.h>
// APIs
#include "lt_common_api.h" // common APIs
#include "lt_family_api.h" // family-specific APIs
#include "lt_logger.h" // UART logger utility
#include "lt_posix_api.h" // POSIX compat functions
// printf silencing methods
#include <printf_port.h>

View File

@@ -5,7 +5,7 @@
#define CHIP_TYPE(family, chip_id) (((family >> 24) << 8) | chip_id)
#define CHIP_TYPE_ENUM(family, chip_id) (ChipType) CHIP_TYPE(family, chip_id)
enum ChipFamily {
typedef enum {
// used in UF2 Family ID
F_RTL8710A = 0x9FFFD543, // Realtek Ameba1
F_RTL8710B = 0x22E0D6FC, // Realtek AmebaZ (realtek-ambz)
@@ -15,9 +15,9 @@ enum ChipFamily {
F_BK7231N = 0x7B3EF230, // Beken 7231N
F_BK7251 = 0x6A82CC42, // Beken 7251/7252
F_BL60X = 0xDE1270B7, // Boufallo 602
};
} ChipFamily;
enum ChipType {
typedef enum {
// Realtek AmebaZ
// IDs copied from rtl8710b_efuse.h
RTL8710BL = CHIP_TYPE(F_RTL8710B, 0xE0), // ???
@@ -32,4 +32,4 @@ enum ChipType {
BK7231N = CHIP_TYPE(F_BK7231N, 0x1C), // *SCTRL_CHIP_ID = 0x7231c
BL2028N = CHIP_TYPE(F_BK7231N, 0x1C), // *SCTRL_CHIP_ID = 0x7231c
BK7252 = CHIP_TYPE(F_BK7251, 0x00), // TODO
};
} ChipType;

View File

@@ -1,12 +1,6 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-29. */
#include "LibreTuyaAPI.h"
String ipToString(const IPAddress &ip) {
char szRet[16];
sprintf(szRet, "%hhu.%hhu.%hhu.%hhu", ip[0], ip[1], ip[2], ip[3]);
return String(szRet);
}
#include "lt_common_api.h"
/**
* @brief Generate random bytes using rand().
@@ -14,7 +8,6 @@ String ipToString(const IPAddress &ip) {
* @param buf destination pointer
* @param len how many bytes to generate
*/
extern "C" {
void lt_rand_bytes(uint8_t *buf, size_t len) {
int *data = (int *)buf;
size_t i;
@@ -28,8 +21,6 @@ void lt_rand_bytes(uint8_t *buf, size_t len) {
}
}
#undef putchar
/**
* @brief Print data pointed to by buf in hexdump-like format (hex+ASCII).
*
@@ -44,7 +35,7 @@ void hexdump(const uint8_t *buf, size_t len, uint32_t offset, uint8_t width) {
// print hex offset
printf("%06x ", offset + pos);
// calculate current line width
uint8_t lineWidth = min(width, len - pos);
uint8_t lineWidth = MIN(width, len - pos);
// print hexadecimal representation
for (uint8_t i = 0; i < lineWidth; i++) {
if (i % 8 == 0) {
@@ -62,4 +53,3 @@ void hexdump(const uint8_t *buf, size_t len, uint32_t offset, uint8_t width) {
pos += lineWidth;
}
}
}

View File

@@ -0,0 +1,35 @@
/* Copyright (c) Kuba Szczodrzyński 2023-02-27. */
#pragma once
#include <libretuya.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
// 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; \
})
void lt_rand_bytes(uint8_t *buf, size_t len);
#ifdef __cplusplus
void hexdump(const uint8_t *buf, size_t len, uint32_t offset = 0, uint8_t width = 16);
#else
void hexdump(const uint8_t *buf, size_t len, uint32_t offset, uint8_t width);
#endif
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -0,0 +1,18 @@
/* Copyright (c) Kuba Szczodrzyński 2023-02-27. */
#pragma once
#include <libretuya.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/**
* @brief Get the reason of last chip reset.
*/
ResetReason lt_get_reset_reason();
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -2,10 +2,17 @@
#include "lt_logger.h"
#include <Arduino.h>
#include <printf/printf.h>
#include <stdarg.h>
#include <stdint.h>
#if LT_LOGGER_TASK && LT_HAS_FREERTOS
#if LT_HAS_PRINTF
#include <printf/printf.h>
#include <printf_port.h>
#else
#include <stdio.h>
#endif
#if (LT_LOGGER_TIMESTAMP || LT_LOGGER_TASK) && LT_HAS_FREERTOS
#include <FreeRTOS.h>
#include <task.h>
#endif
@@ -42,7 +49,9 @@ static const uint8_t colors[] = {
};
#endif
#if LIBRETUYA_ARDUINO
unsigned long millis(void);
#endif
#if LT_LOGGER_CALLER
void lt_log(const uint8_t level, const char *caller, const unsigned short line, const char *format, ...) {
@@ -54,7 +63,13 @@ void lt_log(const uint8_t level, const char *format, ...) {
return;
#if LT_LOGGER_TIMESTAMP
#if LIBRETUYA_ARDUINO
float seconds = millis() / 1000.0f;
#elif LT_HAS_FREERTOS
float seconds = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000.0f;
#else
float seconds = 0;
#endif
#if LT_PRINTF_BROKEN
char zero[4] = "\x00\x30\x30";
if (seconds == 0.0f)
@@ -77,9 +92,13 @@ void lt_log(const uint8_t level, const char *format, ...) {
char c_value = '0' + (colors[level] & 0x7);
#endif
#if LT_HAS_PRINTF
fctprintf(
(void (*)(char, void *))putchar_p,
(void *)uart_port,
#else
printf(
#endif
// format:
#if LT_LOGGER_COLOR
"\e[%c;3%cm"
@@ -128,12 +147,21 @@ void lt_log(const uint8_t level, const char *format, ...) {
#endif
);
#if LT_HAS_PRINTF
va_list va_args;
va_start(va_args, format);
vfctprintf((void (*)(char, void *))putchar_p, (void *)uart_port, format, va_args);
va_end(va_args);
putchar_p('\r', uart_port);
putchar_p('\n', uart_port);
#else
va_list va_args;
va_start(va_args, format);
vprintf(format, va_args);
va_end(va_args);
putchar('\r');
putchar('\n');
#endif
}
void lt_log_set_port(uint8_t port) {

View File

@@ -2,8 +2,11 @@
#pragma once
#include "LibreTuyaConfig.h"
#include <stdint.h>
#include <libretuya.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#if LT_LOGGER_CALLER
#define LT_LOG(level, caller, line, ...) lt_log(level, caller, line, __VA_ARGS__)
@@ -168,3 +171,7 @@ void lt_log_disable();
#else
#define LT_ERRNO()
#endif
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -1,74 +1,28 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
#include <Arduino.h>
#include <api/HardwareSerial.h>
#include <libretuya.h>
using namespace arduino;
extern "C" {
#include <fal.h>
fal_partition_t fal_root_part = NULL;
}
// Arduino framework initialization.
// May be redefined by family files.
void initArduino() __attribute__((weak));
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
// Initialize C library
extern "C" void __libc_init_array(void);
void __libc_init_array(void);
// Main app entrypoint
int main(void);
void mainTask(const void *arg) {
setup();
for (;;) {
loop();
if (serialEventRun)
serialEventRun();
yield();
}
}
static unsigned long periodicTasks[] = {0, 0};
void runPeriodicTasks() {
#if LT_LOG_HEAP
if (millis() - periodicTasks[0] > 1000) {
LT_HEAP_I();
periodicTasks[0] = millis();
}
#endif
#if LT_USE_TIME
if (millis() - periodicTasks[1] > 10000) {
gettimeofday(NULL, NULL);
periodicTasks[1] = millis();
}
#endif
}
int main(void) {
int lt_main(void) {
// print a startup banner
LT_BANNER();
// initialize C library
__libc_init_array();
// inform about the reset reason
LT_I("Reset reason: %u", LT.getResetReason());
// initialize Arduino framework
initArduino();
// optionally initialize per-variant code
initVariant();
LT_I("Reset reason: %u", lt_get_reset_reason());
// initialize FAL
fal_init();
// provide root partition
fal_root_part = (fal_partition_t)fal_partition_find("root");
// start the main task and OS kernel
if (!startMainTask()) {
LT_F("Couldn't start the main task");
}
while (1) {}
return 0;
// run the application
return main();
}

View File

@@ -1,7 +1,9 @@
/* Copyright (c) Kuba Szczodrzyński 2022-05-16. */
#include <sys/time.h>
#include <time.h>
extern char *strdup(const char *);
extern int strcasecmp(const char *s1, const char *s2);
extern int strncasecmp(const char *s1, const char *s2, size_t n);
extern char *strptime(const char *buf, const char *fmt, struct tm *tm);

View File

@@ -0,0 +1,15 @@
/* Copyright (c) Kuba Szczodrzyński 2023-02-27. */
#pragma once
typedef enum {
RESET_REASON_UNKNOWN = 0,
RESET_REASON_POWER = 1,
RESET_REASON_BROWNOUT = 2,
RESET_REASON_HARDWARE = 3,
RESET_REASON_SOFTWARE = 4,
RESET_REASON_WATCHDOG = 5,
RESET_REASON_CRASH = 6,
RESET_REASON_SLEEP = 7,
RESET_REASON_MAX = 8,
} ResetReason;

View File

@@ -1,7 +1,8 @@
/* Copyright (c) Kuba Szczodrzyński 2022-05-16. */
#include <stddef.h>
#include <sdk_mem.h>
#include <stdlib.h>
#include <string.h>
__attribute__((weak)) char *strdup(const char *s) {
size_t len = strlen(s) + 1;

View File

@@ -1,7 +0,0 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-29. */
#pragma once
#include <time.h>
extern char *strptime(const char *buf, const char *fmt, struct tm *tm);