[core] Move startup code to libretuya/common

This commit is contained in:
Kuba Szczodrzyński
2022-06-19 13:15:19 +02:00
parent e70f1a591f
commit cae03d54d6
11 changed files with 74 additions and 58 deletions

View File

@@ -0,0 +1,40 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
#include <Arduino.h>
#include <api/HardwareSerial.h>
using namespace arduino;
// 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 main_task(const void *arg) {
setup();
for (;;) {
loop();
if (serialEventRun)
serialEventRun();
yield();
}
}
int main(void) {
// print a startup banner
LT_BANNER();
// initialize Arduino framework
init();
// initialize C library
__libc_init_array();
// optionally initialize per-variant code
initVariant();
// start the main task and OS kernel
startMainTask();
while (1) {}
return 0;
}

View File

@@ -0,0 +1,11 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
#include <Arduino.h>
void serialEvent() __attribute__((weak));
bool Serial_available() __attribute__((weak));
void serialEventRun(void) {
if (Serial_available && serialEvent && Serial_available())
serialEvent();
}

View File

@@ -2,6 +2,11 @@
#pragma once
// C standard libraries
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
// LibreTuya version macros
#ifndef LT_VERSION
#define LT_VERSION 1.0.0

View File

@@ -1,11 +1,24 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-06. */
#pragma once
#include "LibreTuyaAPI.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Run main_task & start OS kernel (family-defined)
*/
extern bool startMainTask();
/**
* @brief Main setup() and loop() task.
* Not to be called directly.
*/
extern void main_task(const void *arg);
#define PIN_NONE (1 << 0)
#define PIN_GPIO (1 << 1)
#define PIN_IRQ (1 << 2)

View File

@@ -2,6 +2,7 @@
#pragma once
#include <Arduino.h>
#include <MD5Impl.h>
// available built-in implementations

View File

@@ -1,14 +1,9 @@
/* Copyright (c) Kuba Szczodrzyński 2022-06-03. */
// check if impl defines LT_MD5_USE_POLARSSL
extern "C" {
#include <MD5Impl.h>
}
#include "MD5.h"
#if LT_MD5_USE_POLARSSL
#include "MD5PolarSSLImpl.h"
extern "C" {
void MD5Init(LT_MD5_CTX_T *context) {

View File

@@ -12,5 +12,3 @@ extern "C" {
#ifdef __cplusplus
} // extern "C"
#endif
#include "MD5.h"

View File

@@ -1,9 +1,5 @@
#pragma once
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif

View File

@@ -22,53 +22,13 @@
#include <Arduino.h>
#include <cmsis_os.h>
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() {}
// Initialize C library
extern "C" void __libc_init_array(void);
osThreadId main_tid = 0;
void main_task(const void *arg) {
setup();
for (;;) {
loop();
if (serialEventRun)
serialEventRun();
yield();
}
}
int main(void) {
LT_BANNER();
init();
__libc_init_array();
initVariant();
bool startMainTask() {
osThreadDef(main_task, osPriorityRealtime, 1, 4096 * 4);
main_tid = osThreadCreate(osThread(main_task), NULL);
osKernelStart();
while (1)
;
return 0;
}
void serialEvent() __attribute__((weak));
bool Serial_available() __attribute__((weak));
void serialEventRun(void) {
if (Serial_available && serialEvent && Serial_available())
serialEvent();
return true;
}
void wait_for_debug() {

View File

@@ -2,9 +2,8 @@
#pragma once
// va_list is declared by SDK and conflicting
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
// disable typedef in basic_types.h
#define boolean boolean_rtl

View File

@@ -2,6 +2,4 @@
#pragma once
#include <sdk_extern.h>
#define LT_MD5_USE_POLARSSL 1