Skip to content

Commit

Permalink
migrate shared code from xsHost to xsHosts
Browse files Browse the repository at this point in the history
  • Loading branch information
phoddie committed Jan 6, 2022
1 parent dc9e936 commit ad4745d
Show file tree
Hide file tree
Showing 15 changed files with 828 additions and 699 deletions.
10 changes: 5 additions & 5 deletions build/devices/esp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2018 Moddable Tech, Inc.
* Copyright (c) 2016-2022 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
Expand All @@ -23,13 +23,13 @@
#include <Arduino.h>
#include "xs.h"
#include "xsHost.h"
#include "xsHosts.h"
#include "modTimer.h"

extern "C" {
#include "user_interface.h" // to get system_soft_wdt_feed

extern void fx_putc(void *refcon, char c);
extern void mc_setup(xsMachine *the);
extern void __wrap_espconn_init(void);
extern uint8_t uart_set_baud(uart_t* uart, int baudrate);
}
Expand Down Expand Up @@ -80,11 +80,11 @@ void setup()
wifi_set_opmode_current(NULL_MODE);

#ifdef mxDebug
gThe = ESP_cloneMachine(0, 0, 0, 0, NULL);
gThe = modCloneMachine(0, 0, 0, 0, NULL);

mc_setup(gThe);
modRunMachineSetup(gThe);
#else
mc_setup(ESP_cloneMachine(0, 0, 0, 0, NULL));
modRunMachineSetup(modCloneMachine(0, 0, 0, 0, NULL));
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions build/devices/esp32/xsProj-esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2020 Moddable Tech, Inc.
# Copyright (c) 2022 Moddable Tech, Inc.
#
# This file is part of the Moddable SDK Tools.
#
Expand All @@ -20,7 +20,7 @@

file(TO_CMAKE_PATH "$ENV{MODDABLE}" ENV_MODDABLE)
set(COMPONENT_SRCS "main.c")
set(COMPONENT_ADD_INCLUDEDIRS ${ENV_MODDABLE}/xs/platforms/esp ${ENV_MODDABLE}/xs/includes ${ENV_MODDABLE}/modules/base/instrumentation)
set(COMPONENT_ADD_INCLUDEDIRS ${ENV_MODDABLE}/xs/platforms/esp ${ENV_MODDABLE}/xs/platforms/mc ${ENV_MODDABLE}/xs/includes ${ENV_MODDABLE}/modules/base/instrumentation)

register_component()

Expand Down
32 changes: 21 additions & 11 deletions build/devices/esp32/xsProj-esp32/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@

#include "xs.h"
#include "xsHost.h"
#include "xsHosts.h"

#include "xsPlatform.h"
// #include "mc.defines.h"

#ifndef MODDEF_XS_TEST
#define MODDEF_XS_TEST 1
#endif
Expand All @@ -64,6 +63,11 @@
extern void fx_putc(void *refcon, char c); //@@
extern void mc_setup(xsMachine *the);

#if 0 == CONFIG_LOG_DEFAULT_LEVEL
#define kStack (((8 * 1024) + XT_STACK_EXTRA_CLIB) / sizeof(StackType_t))
#else
#define kStack (((10 * 1024) + XT_STACK_EXTRA_CLIB) / sizeof(StackType_t))
#endif

#if !MODDEF_XS_TEST
static
Expand Down Expand Up @@ -124,9 +128,9 @@ void loop_task(void *pvParameter)
#endif

while (true) {
gThe = ESP_cloneMachine(0, 0, 0, 0, NULL);
gThe = modCloneMachine(0, 0, 0, 0, NULL);

mc_setup(gThe);
modRunMachineSetup(gThe);

#if MODDEF_XS_TEST
xsMachine *the = gThe;
Expand Down Expand Up @@ -171,24 +175,35 @@ void modLog_transmit(const char *msg)
}

void ESP_put(uint8_t *c, int count) {
//#ifdef mxDebug
uart_write_bytes(USE_UART, (char *)c, count);
//#endif
}

void ESP_putc(int c) {
// #ifdef mxDebug
char cx = c;
uart_write_bytes(USE_UART, &cx, 1);
//#endif

}

int ESP_getc(void) {
//#ifdef mxDebug
uint8_t c;
int err = uart_read_bytes(USE_UART, &c, 1, 0);
return (1 == err) ? c : -1;
//#endif
return -1;
}

uint8_t ESP_isReadable() {
//#ifdef mxDebug
size_t s;
uart_get_buffered_data_len(USE_UART, &s);
return s > 0;
//#endif
return 0;
}

uint8_t ESP_setBaud(int baud) {
Expand Down Expand Up @@ -239,11 +254,6 @@ void app_main() {
uart_driver_install(USE_UART, UART_FIFO_LEN * 2, 0, 0, NULL, 0);
#endif

#if 0 == CONFIG_LOG_DEFAULT_LEVEL
#define kStack (((8 * 1024) + XT_STACK_EXTRA_CLIB) / sizeof(StackType_t))
#else
#define kStack (((10 * 1024) + XT_STACK_EXTRA_CLIB) / sizeof(StackType_t))
#endif

xTaskCreate(loop_task, "main", kStack, NULL, 4, NULL);
xTaskCreate(loop_task, "main", kStack, NULL, 4, NULL);
// xTaskCreatePinnedToCore(loop_task, "main", kStack, NULL, 4, NULL, 0);
}
4 changes: 2 additions & 2 deletions build/devices/esp32/xsProj-esp32s2/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2020 Moddable Tech, Inc.
# Copyright (c) 2020-2022 Moddable Tech, Inc.
#
# This file is part of the Moddable SDK Tools.
#
Expand All @@ -20,7 +20,7 @@

file(TO_CMAKE_PATH "$ENV{MODDABLE}" ENV_MODDABLE)
set(COMPONENT_SRCS "main.c")
set(COMPONENT_ADD_INCLUDEDIRS ${ENV_MODDABLE}/xs/platforms/esp ${ENV_MODDABLE}/xs/includes ${ENV_MODDABLE}/modules/base/instrumentation)
set(COMPONENT_ADD_INCLUDEDIRS ${ENV_MODDABLE}/xs/platforms/esp ${ENV_MODDABLE}/xs/platforms/mc ${ENV_MODDABLE}/xs/includes ${ENV_MODDABLE}/modules/base/instrumentation)

register_component()

Expand Down
7 changes: 3 additions & 4 deletions build/devices/esp32/xsProj-esp32s2/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@

#include "xs.h"
#include "xsHost.h"

#include "xsPlatform.h"
#include "xsHosts.h"

#ifndef DEBUGGER_SPEED
#define DEBUGGER_SPEED 921600
Expand Down Expand Up @@ -141,9 +140,9 @@ void setup(void)
uart_driver_install(USE_UART, UART_FIFO_LEN * 2, 0, 0, NULL, 0);
#endif

gThe = ESP_cloneMachine(0, 0, 0, 0, NULL);
gThe = modCloneMachine(0, 0, 0, 0, NULL);

mc_setup(gThe);
modRunMachineSetup(gThe);

#if CONFIG_TASK_WDT
esp_task_wdt_add(NULL);
Expand Down
4 changes: 2 additions & 2 deletions build/devices/esp32/xsProj-esp32s3/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2020 Moddable Tech, Inc.
# Copyright (c) 2020-2022 Moddable Tech, Inc.
#
# This file is part of the Moddable SDK Tools.
#
Expand All @@ -20,7 +20,7 @@

file(TO_CMAKE_PATH "$ENV{MODDABLE}" ENV_MODDABLE)
set(COMPONENT_SRCS "main.c")
set(COMPONENT_ADD_INCLUDEDIRS ${ENV_MODDABLE}/xs/platforms/esp ${ENV_MODDABLE}/xs/includes ${ENV_MODDABLE}/modules/base/instrumentation)
set(COMPONENT_ADD_INCLUDEDIRS ${ENV_MODDABLE}/xs/platforms/esp ${ENV_MODDABLE}/xs/platforms/mc ${ENV_MODDABLE}/xs/includes ${ENV_MODDABLE}/modules/base/instrumentation)

register_component()

Expand Down
7 changes: 3 additions & 4 deletions build/devices/esp32/xsProj-esp32s3/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@

#include "xs.h"
#include "xsHost.h"

#include "xsPlatform.h"
#include "xsHosts.h"

#ifndef DEBUGGER_SPEED
#define DEBUGGER_SPEED 921600
Expand Down Expand Up @@ -141,9 +140,9 @@ void setup(void)
uart_driver_install(USE_UART, UART_FIFO_LEN * 2, 0, 0, NULL, 0);
#endif

gThe = ESP_cloneMachine(0, 0, 0, 0, NULL);
gThe = modCloneMachine(0, 0, 0, 0, NULL);

mc_setup(gThe);
modRunMachineSetup(gThe);

#if CONFIG_TASK_WDT
esp_task_wdt_add(NULL);
Expand Down
57 changes: 28 additions & 29 deletions modules/base/worker/modWorker.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2021 Moddable Tech, Inc.
* Copyright (c) 2016-2022 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
Expand All @@ -20,6 +20,7 @@

#include "xsmc.h"
#include "xsHost.h"
#include "xsHosts.h"

#include "mc.xs.h" // for xsID_ values

Expand Down Expand Up @@ -54,7 +55,7 @@ struct modWorkerRecord {
uint32_t keyCount;
xsBooleanValue closing;
xsBooleanValue shared;
#if ESP32 || qca4020
#ifdef INC_FREERTOS_H
TaskHandle_t task;
#endif
char module[1];
Expand Down Expand Up @@ -101,7 +102,7 @@ void xs_worker_destructor(void *data)
}

if (1 == useCount) {
#if ESP32 || qca4020
#ifdef INC_FREERTOS_H
if (worker->task) {
vTaskDelete(worker->task);
worker->task = NULL;
Expand Down Expand Up @@ -177,62 +178,60 @@ static void workerConstructor(xsMachine *the, xsBooleanValue shared)

if (target) {
worker->the = target->the;
#ifdef INC_FREERTOS_H
worker->task = target->task;
#endif
modMessagePostToMachine(worker->the, NULL, 0, (modMessageDeliver)workerDeliverConnect, worker);
goto done;
}
}

if (xsmcArgc > 1) {
if (xsmcHas(xsArg(1), xsID_allocation)) {
xsmcGet(xsVar(0), xsArg(1), xsID_allocation);
worker->allocation = xsmcToInteger(xsVar(0));
}
if (xsmcHas(xsArg(1), xsID_stackCount)) {
xsmcGet(xsVar(0), xsArg(1), xsID_stackCount);
worker->stackCount = xsmcToInteger(xsVar(0));
}
if (xsmcHas(xsArg(1), xsID_slotCount)) {
xsmcGet(xsVar(0), xsArg(1), xsID_slotCount);
worker->slotCount = xsmcToInteger(xsVar(0));
}
if (xsmcHas(xsArg(1), xsID_keyCount)) {
xsmcGet(xsVar(0), xsArg(1), xsID_keyCount);
worker->keyCount = xsmcToInteger(xsVar(0));
}
xsmcGet(xsVar(0), xsArg(1), xsID_allocation);
worker->allocation = xsmcToInteger(xsVar(0));

xsmcGet(xsVar(0), xsArg(1), xsID_stackCount);
worker->stackCount = xsmcToInteger(xsVar(0));

xsmcGet(xsVar(0), xsArg(1), xsID_slotCount);
worker->slotCount = xsmcToInteger(xsVar(0));

xsmcGet(xsVar(0), xsArg(1), xsID_keyCount);
worker->keyCount = xsmcToInteger(xsVar(0));
}

#ifdef INC_FREERTOS_H
#if ESP32
#if 0 == CONFIG_LOG_DEFAULT_LEVEL
#define kStack (((5 * 1024) + XT_STACK_EXTRA_CLIB) / sizeof(StackType_t))
#else
#define kStack (((6 * 1024) + XT_STACK_EXTRA_CLIB) / sizeof(StackType_t))
#endif

// xTaskCreatePinnedToCore(workerLoop, worker->module, 4096, worker, 5, &worker->task, xTaskGetAffinity(xTaskGetCurrentTaskHandle()) ? 0 : 1);
xTaskCreate(workerLoop, worker->module, kStack, worker, 8, &worker->task);

modMachineTaskWait(the);
#elif qca4020
#if 0 == CONFIG_LOG_DEFAULT_LEVEL
#define kStack ((9 * 1024) / sizeof(StackType_t))
#else
#define kStack ((10 * 1024) / sizeof(StackType_t))
#endif

// xTaskCreatePinnedToCore(workerLoop, worker->module, 4096, worker, 5, &worker->task, xTaskGetAffinity(xTaskGetCurrentTaskHandle()) ? 0 : 1);
xTaskCreate(workerLoop, worker->module, kStack, worker, 10, &worker->task);
#endif

modMachineTaskWait(the);
#else

#else // !INC_FREERTOS_H
workerStart(worker);
#endif

if (NULL == worker->the) {
#ifdef INC_FREERTOS_H
if (worker->task) {
vTaskDelete(worker->task);
worker->task = NULL;
}
#endif
xsUnknownError("unable to instantiate worker");
}

Expand Down Expand Up @@ -350,7 +349,7 @@ void workerDeliverConnect(xsMachine *the, modWorker worker, uint8_t *message, ui
xsmcSet(xsVar(1), xsID_postMessage, xsVar(3)); // port.postMessage = postMessage
xsCall1(xsGlobal, xsID_onconnect, xsVar(2)); // onconnect(e)

//@@ this eats any exception in onconnect... should be propagated
//@@ this eats any exception in onconnect... should be propagated?
xsEndHost(the);
}

Expand All @@ -359,12 +358,12 @@ int workerStart(modWorker worker)
xsMachine *the;
int result = 0;

the = ESP_cloneMachine(worker->allocation, worker->stackCount, worker->slotCount, worker->keyCount, worker->module);
the = modCloneMachine(worker->allocation, worker->stackCount, worker->slotCount, worker->keyCount, worker->module);
if (!the)
return -1;

#ifdef mxInstrument
espInstrumentMachineBegin(the, workerSampleInstrumentation, 0, NULL, NULL);
modInstrumentMachineBegin(the, workerSampleInstrumentation, 0, NULL, NULL);
#endif

xsBeginHost(the);
Expand Down Expand Up @@ -419,7 +418,7 @@ void workerTerminate(xsMachine *the, modWorker worker, uint8_t *message, uint16_
xs_worker_destructor(worker);
}

#if ESP32 || qca4020
#ifdef INC_FREERTOS_H

void workerLoop(void *pvParameter)
{
Expand Down Expand Up @@ -454,6 +453,6 @@ void workerSampleInstrumentation(modTimer timer, void *refcon, int refconSize)
{
xsMachine *the = *(xsMachine **)refcon;
fxSampleInstrumentation(the, 0, NULL);
espInstrumentMachineReset(the);
modInstrumentMachineReset(the);
}
#endif
Loading

0 comments on commit ad4745d

Please sign in to comment.