[realtek-ambz] Update Arduino core to new structure

This commit is contained in:
Kuba Szczodrzyński
2023-03-02 21:00:29 +01:00
parent dee20c859c
commit fbfd96ecdb
29 changed files with 129 additions and 195 deletions

View File

@@ -35,6 +35,8 @@ Explicit is better than implicit.
### Other
- refactor `SerialClass` to have a shared header `Serial.h` in the common core (and `SerialData`, just like WiFi)
- implement Wire on BK, refactor the API and class
- watchdog API
- `Preferences` library
- test/fix IPv6 on different families

View File

@@ -1,16 +1,10 @@
/* Copyright (c) Kuba Szczodrzyński 2022-05-28. */
#include <LibreTuyaAPI.h>
#include <LT.h>
#include <sdk_private.h>
#include <Flash.h>
extern "C" {
#include <flash_api.h>
#include <rtl8710b.h>
#include <sys_api.h>
#include <wdt_api.h>
}
void LibreTuya::restart() {
// The Watchdog Way
wdtEnable(1L);

View File

@@ -1,6 +1,9 @@
/* Copyright (c) Kuba Szczodrzyński 2022-07-03. */
#include "SerialClass.h"
#include "Serial.h"
#include <Arduino.h>
#include <sdk_private.h>
#if HAS_SERIAL0
SerialClass Serial0(UART0_DEV, UART0_IRQ, PIN_SERIAL0_RX, PIN_SERIAL0_TX);
@@ -12,7 +15,7 @@ SerialClass Serial1(UART1_DEV, UART1_IRQ, PIN_SERIAL1_RX, PIN_SERIAL1_TX);
SerialClass Serial2(UART2_DEV, UART_LOG_IRQ, PIN_SERIAL2_RX, PIN_SERIAL2_TX);
#endif
SerialClass::SerialClass(UART_TypeDef *uart, IRQn irq, pin_size_t rx, pin_size_t tx) {
SerialClass::SerialClass(void *uart, uint8_t irq, pin_size_t rx, pin_size_t tx) {
data.uart = uart;
data.buf = NULL;
this->irq = irq;
@@ -21,17 +24,18 @@ SerialClass::SerialClass(UART_TypeDef *uart, IRQn irq, pin_size_t rx, pin_size_t
}
static uint32_t callback(void *param) {
SerialData *data = (SerialData *)param;
SerialData *data = (SerialData *)param;
UART_TypeDef *uart = (UART_TypeDef *)data->uart;
uint32_t intcr = data->uart->DLH_INTCR;
data->uart->DLH_INTCR = 0;
uint32_t intcr = uart->DLH_INTCR;
uart->DLH_INTCR = 0;
uint8_t c;
UART_CharGet(data->uart, &c);
UART_CharGet(uart, &c);
if (c)
data->buf->store_char(c);
data->uart->DLH_INTCR = intcr;
uart->DLH_INTCR = intcr;
return 0;
}
@@ -51,8 +55,8 @@ void SerialClass::begin(unsigned long baudrate, uint16_t config) {
cfg.Parity = parity;
cfg.ParityType = parityType;
cfg.StopBit = stopBits;
UART_Init(data.uart, &cfg);
UART_SetBaud(data.uart, baudrate);
UART_Init((UART_TypeDef *)data.uart, &cfg);
UART_SetBaud((UART_TypeDef *)data.uart, baudrate);
if (data.buf) {
data.buf->clear();
@@ -88,11 +92,11 @@ int SerialClass::read() {
}
void SerialClass::flush() {
UART_WaitBusy(data.uart, 10);
UART_WaitBusy((UART_TypeDef *)data.uart, 10);
}
size_t SerialClass::write(uint8_t c) {
while (UART_Writable(data.uart) == 0) {}
UART_CharPut(data.uart, c);
while (UART_Writable((UART_TypeDef *)data.uart) == 0) {}
UART_CharPut((UART_TypeDef *)data.uart, c);
return 1;
}

View File

@@ -2,14 +2,14 @@
#pragma once
#include <Arduino.h>
#include <api/ArduinoAPI.h>
#include <api/HardwareSerial.h>
#include <api/RingBuffer.h>
using namespace arduino;
typedef struct {
UART_TypeDef *uart;
void *uart; // UART_TypeDef
RingBuffer *buf;
} SerialData;
@@ -17,12 +17,12 @@ class SerialClass : public HardwareSerial {
private:
// data accessible to IRQ handler
SerialData data;
IRQn irq;
uint8_t irq; // IRQn
pin_size_t rx;
pin_size_t tx;
public:
SerialClass(UART_TypeDef *uart, IRQn irq, pin_size_t rx, pin_size_t tx);
SerialClass(void *uart, uint8_t irq, pin_size_t rx, pin_size_t tx);
inline void begin(unsigned long baudrate) {
begin(baudrate, SERIAL_8N1);

View File

@@ -1,12 +1,7 @@
/* Copyright (c) Kuba Szczodrzyński 2022-07-03. */
#include "SoftwareSerial.h"
extern "C" {
#include <timer_api.h>
} // extern "C"
#include <SoftwareSerial.h>
#include <sdk_private.h>
#define TIMER_MAX 3
#define OBJ ((gtimer_t *)this->param)

View File

@@ -1,5 +0,0 @@
/* Copyright (c) Kuba Szczodrzyński 2022-07-03. */
#pragma once
#include <api/SoftwareSerial.h>

View File

@@ -1,7 +1,8 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
#include "WiFiPriv.h"
#include "WiFiPrivate.h"
// TODO move these to WiFiData
rtw_network_info_t wifi = {0};
rtw_ap_info_t ap = {0};
rtw_wifi_setting_t wifi_setting;
@@ -25,11 +26,15 @@ void reset_wifi_struct(void) {
}
WiFiClass::WiFiClass() {
data.scanSem = xSemaphoreCreateBinary();
data = (WiFiData *)calloc(1, sizeof(WiFiData));
DATA->scanSem = xSemaphoreCreateBinary();
}
WiFiClass::~WiFiClass() {
vSemaphoreDelete(data.scanSem);
vSemaphoreDelete(DATA->scanSem);
free(data);
data = NULL;
}
WiFiAuthMode securityTypeToAuthMode(uint8_t type) {

View File

@@ -1,6 +1,6 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
#include "WiFiPriv.h"
#include "WiFiPrivate.h"
typedef struct {
int count;

View File

@@ -1,18 +0,0 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-23. */
#pragma once
#include <Arduino.h>
extern "C" {
#include <FreeRTOS.h>
#include <semphr.h>
} // extern "C"
typedef struct {
bool initialized;
bool sleep;
SemaphoreHandle_t scanSem;
} WiFiData;

View File

@@ -1,6 +1,6 @@
/* Copyright (c) Kuba Szczodrzyński 2022-05-16. */
#include "WiFiPriv.h"
#include "WiFiPrivate.h"
#include <vector>

View File

@@ -1,6 +1,6 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
#include "WiFiPriv.h"
#include "WiFiPrivate.h"
int32_t WiFiClass::channel() {
int channel = 0;
@@ -13,12 +13,12 @@ bool WiFiClass::modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap) {
__wrap_DiagPrintf_disable();
startWifiTask();
if (!data.initialized) {
if (!DATA->initialized) {
// initialize wifi first
LT_IM(WIFI, "Initializing LwIP");
LwIP_Init();
reset_wifi_struct();
data.initialized = true;
DATA->initialized = true;
}
LT_HEAP_I();
if (getMode()) {
@@ -58,7 +58,7 @@ error:
}
WiFiMode WiFiClass::getMode() {
if (!data.initialized)
if (!DATA->initialized)
return WIFI_MODE_NULL;
return (WiFiMode)wifi_mode;
}
@@ -80,12 +80,12 @@ bool WiFiClass::setSleep(bool enable) {
if (wifi_disable_powersave() != RTW_SUCCESS)
return false;
}
data.sleep = enable;
DATA->sleep = enable;
return true;
}
bool WiFiClass::getSleep() {
return data.sleep;
return DATA->sleep;
}
bool WiFiClass::setTxPower(int power) {

View File

@@ -2,7 +2,8 @@
#pragma once
#include <api/WiFi/WiFi.h>
#include <WiFi.h>
#include <sdk_private.h>
extern "C" {
@@ -45,3 +46,13 @@ extern void handleRtwEvent(uint16_t event, char *data, int len, int flags);
#define NETNAME_STA WLAN0_NAME
#define NETNAME_AP (wifi_mode == WIFI_MODE_APSTA ? WLAN1_NAME : WLAN0_NAME)
typedef struct {
bool initialized;
bool sleep;
SemaphoreHandle_t scanSem;
} WiFiData;
#define DATA ((WiFiData *)data)
#define pDATA ((WiFiData *)pWiFi->data)
#define cDATA ((WiFiData *)cls->data)

View File

@@ -1,6 +1,6 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
#include "WiFiPriv.h"
#include "WiFiPrivate.h"
WiFiStatus
WiFiClass::begin(const char *ssid, const char *passphrase, int32_t channel, const uint8_t *bssid, bool connect) {
@@ -95,7 +95,7 @@ bool WiFiClass::reconnect(const uint8_t *bssid) {
dhcpRet = LwIP_DHCP(0, DHCP_START);
if (dhcpRet == DHCP_ADDRESS_ASSIGNED) {
LT_HEAP_I();
EventInfo *eventInfo = (EventInfo *)zalloc(sizeof(EventInfo));
EventInfo *eventInfo = (EventInfo *)calloc(1, sizeof(EventInfo));
eventInfo->got_ip.if_index = 0;
eventInfo->got_ip.esp_netif = NULL;
eventInfo->got_ip.ip_info.ip.addr = localIP();

View File

@@ -1,6 +1,6 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
#include "WiFiPriv.h"
#include "WiFiPrivate.h"
static rtw_result_t scanHandler(rtw_scan_handler_result_t *result) {
WiFiClass *cls = (WiFiClass *)result->user_data;
@@ -10,7 +10,7 @@ static rtw_result_t scanHandler(rtw_scan_handler_result_t *result) {
if (result->scan_complete == RTW_TRUE) {
scan->running = false;
xSemaphoreGive(cls->data.scanSem);
xSemaphoreGive(cDATA->scanSem);
return RTW_SUCCESS;
}
@@ -48,8 +48,8 @@ int16_t WiFiClass::scanNetworks(bool async, bool showHidden, bool passive, uint3
if (!async) {
LT_IM(WIFI, "Waiting for results");
xSemaphoreTake(data.scanSem, 1); // reset the semaphore quickly
xSemaphoreTake(data.scanSem, pdMS_TO_TICKS(maxMsPerChannel * 20));
xSemaphoreTake(DATA->scanSem, 1); // reset the semaphore quickly
xSemaphoreTake(DATA->scanSem, pdMS_TO_TICKS(maxMsPerChannel * 20));
return scan->count;
}
return WIFI_SCAN_RUNNING;

View File

@@ -2,10 +2,9 @@
#include "Wire.h"
#include <Arduino.h>
#include <sdk_private.h>
extern "C" {
#include <i2c_api.h>
extern int i2c_write_timeout(i2c_t *obj, int address, char *data, int length, int stop, int timeout_ms);
}

View File

@@ -2,16 +2,9 @@
#pragma once
#include <Arduino.h>
#include <HardwareI2C.h>
#include <api/RingBuffer.h>
#include <api/Wire.h>
#ifdef __cplusplus
extern "C" {
#endif
// #include <i2c_api.h>
#ifdef __cplusplus
}
#endif
#if !defined(PIN_WIRE0_SDA) && defined(PIN_WIRE1_SDA)
#define Wire1 Wire
@@ -25,7 +18,7 @@ typedef struct i2c_s i2c_t;
using arduino::RingBuffer;
class TwoWire : public ITwoWire {
class TwoWire : public HardwareI2C {
private:
i2c_t *_i2c = NULL;
uint8_t _idx = 0;
@@ -60,10 +53,10 @@ class TwoWire : public ITwoWire {
int peek();
void flush();
using ITwoWire::begin;
using ITwoWire::endTransmission;
using ITwoWire::requestFrom;
using ITwoWire::write;
using HardwareI2C::begin;
using HardwareI2C::endTransmission;
using HardwareI2C::requestFrom;
using HardwareI2C::write;
using Print::write;
};

View File

@@ -1,36 +1,27 @@
/* Copyright (c) Kuba Szczodrzyński 2022-04-23. */
/* Copyright (c) Kuba Szczodrzyński 2023-03-02. */
#pragma once
// Provide GPIO names to variant.cpp files
#define LT_VARIANT_INCLUDE "sdk_private.h"
#include <stdint.h>
#ifdef __cplusplus
#include "WCharacterFixup.h"
#endif
extern "C" {
#endif // __cplusplus
#define PinMode PinModeArduino // this conflicts with SDK enum
#include <api/ArduinoAPI.h>
#include <core/LibreTuyaAPI.h>
#undef PinMode
extern uint32_t SystemCoreClock;
extern void vPortClearInterruptMask(uint32_t ulNewMaskValue);
extern uint32_t ulPortSetInterruptMask(void);
// Include family-specific code
#include "WVariant.h"
// Include board variant
#include "variant.h"
#define clockCyclesPerMicrosecond() (SystemCoreClock / 1000000L)
#define clockCyclesToMicroseconds(a) (a * 1000L / (SystemCoreClock / 1000L))
#define microsecondsToClockCycles(a) (a * (SystemCoreClock / 1000000L))
// Choose the main UART output port
#ifndef LT_UART_DEFAULT_PORT
#if HAS_SERIAL2
#define LT_UART_DEFAULT_PORT 2
#elif HAS_SERIAL0
#define LT_UART_DEFAULT_PORT 0
#elif HAS_SERIAL1
#define LT_UART_DEFAULT_PORT 1
#else
#error "No serial port is available"
#endif
#endif
#define interrupts() vPortClearInterruptMask(0)
#define noInterrupts() ulPortSetInterruptMask()
// Define available serial ports
#ifdef __cplusplus
#include "SerialClass.h"
#include <core/SerialExtern.h>
} // extern "C"
#endif

View File

@@ -1,5 +0,0 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-03. */
#pragma once
#define LT_MD5_USE_POLARSSL 1

View File

@@ -1,36 +0,0 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "sdk_extern.h"
#include "sdk_mem.h"
#include "sdk_os.h"
#define DEFAULT 1
#define EXTERNAL 0
#define round(x) ((x) >= 0 ? (long)((x) + 0.5) : (long)((x)-0.5))
// Additional Wiring functions
extern uint32_t digitalPinToPort(uint32_t pinNumber);
extern uint32_t digitalPinToBitMask(uint32_t pinNumber);
extern void analogOutputInit(void);
extern void wait_for_debug();
#ifdef __cplusplus
extern "C" uint32_t SystemCoreClock;
#else
extern uint32_t SystemCoreClock;
#endif
#define clockCyclesPerMicrosecond() (SystemCoreClock / 1000000L)
#define clockCyclesToMicroseconds(a) (a * 1000L / (SystemCoreClock / 1000L))
#define microsecondsToClockCycles(a) (a * (SystemCoreClock / 1000000L))
#define interrupts() vPortClearInterruptMask(0)
#define noInterrupts() ulPortSetInterruptMask()
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -6,8 +6,9 @@
#define LT_ARD_HAS_SOFTSERIAL 1
#define LT_ARD_HAS_SERIAL 1
#define LT_ARD_MD5_POLARSSL 1
#define ARDUINO_AMEBA
#define ARDUINO_ARCH_AMBZ
// the SDK declares bool if not defined before
// which conflicts with C++ built-in bool

View File

@@ -1,10 +1,12 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
#include <Arduino.h>
#include <sdk_private.h>
extern "C" {
#include <cmsis_os.h>
#include <core_cm4.h>
osThreadId main_tid = 0;

View File

@@ -17,6 +17,7 @@
*/
#include <Arduino.h>
#include <cmsis_os.h>
#ifndef portNVIC_SYSTICK_CURRENT_VALUE_REG
@@ -88,6 +89,7 @@ unsigned long micros(void) {
}
void yield(void) {
runPeriodicTasks();
vTaskDelay(1);
taskYIELD();
}

View File

@@ -17,17 +17,14 @@
*/
#include <Arduino.h>
#include <analogin_api.h>
#include <analogout_api.h>
#include <gpio_ex_api.h>
#include <pwmout_api.h>
#include <sdk_private.h>
/* ADC */
analogin_t adc1;
analogin_t adc2;
analogin_t adc3;
static analogin_t adc1;
static analogin_t adc2;
static analogin_t adc3;
bool g_adc_enabled[] = {false, false, false};
static bool g_adc_enabled[] = {false, false, false};
// from realtek_amebaz_va0_example/example_sources/adc_vbat/src/main.c
#define AD2MV(ad, offset, gain) (((ad >> 4) - offset) * 1000 / gain)

View File

@@ -1,8 +1,5 @@
#include <Arduino.h>
#include <gpio_api.h>
#include <gpio_irq_api.h>
#include <gpio_irq_ex_api.h>
#include <pwmout_api.h>
#include <sdk_private.h>
extern void *gpio_pin_struct[PINS_COUNT];
@@ -28,7 +25,7 @@ void pinRemoveMode(pin_size_t pinNumber) {
pin->enabled = PIN_NONE;
}
void pinMode(pin_size_t pinNumber, PinModeArduino pinMode) {
void pinMode(pin_size_t pinNumber, PinMode pinMode) {
PinInfo *pin = pinInfo(pinNumber);
if (!pin)
return;
@@ -117,21 +114,3 @@ PinStatus digitalRead(pin_size_t pinNumber) {
gpio_t *gpio = (gpio_t *)gpio_pin_struct[pinNumber];
return gpio_read(gpio);
}
/**************************** Extend API by RTK ***********************************/
uint32_t digitalPinToPort(uint32_t pinNumber) {
if (pinInvalid(pinNumber))
return 0xFFFFFFFF;
uint32_t pin_name = HAL_GPIO_GetPinName(pinTable[pinNumber].gpio);
return HAL_GPIO_GET_PORT_BY_NAME(pin_name);
}
uint32_t digitalPinToBitMask(uint32_t pinNumber) {
if (pinInvalid(pinNumber))
return 0xFFFFFFFF;
uint32_t pin_name = HAL_GPIO_GetPinName(pinTable[pinNumber].gpio);
return 1 << (HAL_GPIO_GET_PIN_BY_NAME(pin_name));
}

View File

@@ -1,7 +1,5 @@
#include <Arduino.h>
#include <gpio_api.h>
#include <gpio_irq_api.h>
#include <gpio_irq_ex_api.h>
#include <sdk_private.h>
extern void *gpio_pin_struct[PINS_COUNT];
static void *gpio_irq_handler_list[PINS_COUNT] = {NULL};

View File

@@ -17,10 +17,7 @@
*/
#include <Arduino.h>
#include <PinNames.h>
#include <gpio_api.h>
#include <objects.h>
#include <us_ticker_api.h>
#include <sdk_private.h>
extern void *gpio_pin_struct[];

View File

@@ -11,6 +11,8 @@ extern "C" {
// disable typedef in basic_types.h
#define boolean boolean_rtl
// fix conflicts with Arduino's PinMode enum
#define PinMode PinModeRTL
#include <strproc.h> // define string macros first
#undef isdigit // then remove them, as they conflict
@@ -22,16 +24,30 @@ extern "C" {
#undef strtoul
#include <ameba_soc.h>
#include <analogin_api.h>
#include <analogout_api.h>
#include <flash_api.h>
#include <gpio_api.h>
#include <gpio_ex_api.h>
#include <gpio_irq_api.h>
#include <gpio_irq_ex_api.h>
#include <i2c_api.h>
#include <main.h>
#include <objects.h>
#include <pwmout_api.h>
#include <rand.h>
#include <rt_lib_rom.h>
#include <rtl8710b.h>
#include <rtl_lib.h>
#include <sys_api.h>
#include <timer_api.h>
#include <us_ticker_api.h>
#include <wait_api.h>
#include <wdt_api.h>
// remove previously defined workaround
// remove previously defined workarounds
#undef boolean
#undef PinMode
// undefine ROM stdio in favor of printf() library (wrappers)
#undef printf

View File

@@ -0,0 +1,6 @@
/* Copyright (c) Kuba Szczodrzyński 2023-03-02. */
// fix conflicting declaration with ArduinoCore-API
#define boolean boolean_rtl
#include_next "basic_types.h"
#undef boolean

View File

@@ -0,0 +1,6 @@
/* Copyright (c) Kuba Szczodrzyński 2023-03-02. */
// section_config.h is in the same directory as basic_types.h
// make the former include a fixup instead
#include "basic_types.h"
#include_next "section_config.h"