Skip to content

Commit

Permalink
Test, simplify init
Browse files Browse the repository at this point in the history
Don't need `host_init`, just wrap `init`
  • Loading branch information
mikee47 committed Apr 21, 2024
1 parent e8d825c commit c733819
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 100 deletions.
30 changes: 0 additions & 30 deletions Sming/Arch/Host/Components/hostlib/include/hostlib/init.h

This file was deleted.

27 changes: 0 additions & 27 deletions Sming/Arch/Host/Components/hostlib/init.cpp

This file was deleted.

5 changes: 3 additions & 2 deletions Sming/Arch/Host/Components/hostlib/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <driver/hw_timer.h>
#include <esp_tasks.h>
#include <stdlib.h>
#include "include/hostlib/init.h"
#include "include/hostlib/emu.h"
#include "include/hostlib/hostlib.h"
#include "include/hostlib/CommandLine.h"
Expand All @@ -43,6 +42,8 @@
#include <host_lwip.h>
#endif

extern void init();

namespace
{
static int exitCode;
Expand Down Expand Up @@ -286,7 +287,7 @@ int main(int argc, char* argv[])

System.initialize();

host_init();
init();

while(!done) {
int due = host_main_loop();
Expand Down
4 changes: 3 additions & 1 deletion Sming/Arch/Host/app.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ TARGET_OUT_0 := $(FW_BASE)/$(APP_NAME)$(TOOL_EXT)

# Hosted Settings
ifneq ($(ENABLE_HOSTED),)
COMPONENTS_AR := $(USER_LIBDIR)/$(CLIB_PREFIX)Hosted-Lib-$(CMP_Hosted-Lib_LIBHASH).a $(COMPONENTS_AR)
COMPONENTS_AR := \
$(CMP_Hosted-Lib_TARGETS) \
$(COMPONENTS_AR)
endif

# Target definitions
Expand Down
8 changes: 6 additions & 2 deletions Sming/Components/Hosted/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ HostEd
The hosted component allows Sming's host emulator to run parts of the commands on an actual microcontroller.
The communication is done via `simplePRC <https://simplerpc.readthedocs.io/>`_ and the microcontroller has to be flashed with a special application.


Overview
--------

Sming's host emulator allows easier debugging and development of embedded applications. This component named "Hosted" extends the host emulator
and facilitates testing functionality that only a real microcontroller can provide as for example digital I/O operations or SPI operations.

Expand All @@ -24,7 +26,7 @@ We need to compile and flash also a special application on the desired microcont
This application will act as an RPC Server and will execute the commands from the host emulator on the microcontroller.

In the ``samples`` directory you will find the sample applications that will turn your microcontroller into
an RCP server.
an RPC server.

The compilation and flashing for ESP32, for example, can be done using the following commands::

Expand All @@ -33,7 +35,8 @@ The compilation and flashing for ESP32, for example, can be done using the follo
make flash

If you replace ``SMING_ARCH=Esp32`` with ``SMING_ARCH=Esp8266`` then the hosted application will be compiled and flashed on a ESP8266 microcontroller.
Make sure to replace the values of  WIFI_SSID and WIFI_PWD with the actual name and password for the Access Point (AP).
Make sure to replace the values of WIFI_SSID and WIFI_PWD with the actual name and password for the Access Point (AP).


Communication
-------------
Expand All @@ -42,6 +45,7 @@ can be done using TCP or serial interface.

The ``transport`` classes are located under ``include/Hosted/Transport``.


Configuration
-------------

Expand Down
16 changes: 10 additions & 6 deletions Sming/Components/Hosted/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ENABLE_HOSTED ?=

ifneq ($(ENABLE_HOSTED),)
COMPONENT_SRCDIRS += init/$(ENABLE_HOSTED)
EXTRA_LDFLAGS := $(call Wrap,host_init)
EXTRA_LDFLAGS := $(call Wrap,_Z4initv)
COMPONENT_DEPENDS += SerialLib
ifeq ($(ENABLE_HOSTED),tcp)
COMPONENT_DEPENDS += Network
Expand All @@ -20,13 +20,17 @@ ifneq ($(ENABLE_HOSTED),)
endif
endif

COMPONENT_RELINK_VARS += HOSTED_SERVER_IP
COMPONENT_RELINK_VARS += \
HOSTED_SERVER_IP \
HOSTED_COM_PORT \
HOSTED_COM_SPEED

COMPONENT_RELINK_VARS += HOSTED_COM_PORT
HOSTED_COM_PORT ?= $(COM_PORT)

COMPONENT_RELINK_VARS += HOSTED_COM_SPEED
HOSTED_COM_SPEED ?= 115200

COMPONENT_CFLAGS = -DHOSTED_SERVER_IP=$(HOSTED_SERVER_IP) -DHOSTED_COM_PORT="\"$(HOSTED_COM_PORT)"\" -DHOSTED_COM_SPEED=$(HOSTED_COM_SPEED)
COMPONENT_CFLAGS = \
-DHOSTED_SERVER_IP=$(HOSTED_SERVER_IP) \
-DHOSTED_COM_PORT="\"$(HOSTED_COM_PORT)"\" \
-DHOSTED_COM_SPEED=$(HOSTED_COM_SPEED)

COMPONENT_CXXFLAGS := $(COMPONENT_CFLAGS)
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BaseTransport

void onData(DataHandler handler)
{
this->handler = handler;
this->handler = std::move(handler);
}

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
#include <Network/TcpServer.h>
#include <Data/Buffer/CircularBuffer.h>

namespace Hosted
{
namespace Transport
namespace Hosted::Transport
{
class TcpClientStream : public Stream
{
public:
TcpClientStream(TcpClient& client, size_t cbufferSize = 1024, size_t threshold = 400)
: cBuffer(cbufferSize), client(client), pendingBytes(0), threshold(threshold)
: cBuffer(cbufferSize), client(client), threshold(threshold)
{
client.setReceiveDelegate(TcpClientDataDelegate(&TcpClientStream::store, this));
}
Expand Down Expand Up @@ -87,7 +85,7 @@ class TcpClientStream : public Stream
private:
CircularBuffer cBuffer;
TcpClient& client;
size_t pendingBytes;
size_t pendingBytes{0};
size_t threshold;

bool store(TcpClient& client, char* data, int size)
Expand All @@ -96,6 +94,4 @@ class TcpClientStream : public Stream
}
};

} // namespace Transport

} // namespace Hosted
} // namespace Hosted::Transport
19 changes: 10 additions & 9 deletions Sming/Components/Hosted/init/serial/InitClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,28 @@
#define HOSTED_COM_SPEED 115200
#endif

Hosted::Client* hostedClient{nullptr};
Hosted::Client* hostedClient;

extern void init();

extern "C" {
void __real_host_init();
void __wrap_host_init();
}
extern "C" void __real__Z4initv();

namespace
{
Hosted::Serial hostedSerial(HOSTED_COM_PORT);

void __wrap_host_init()
extern "C" void __wrap__Z4initv()
{
host_printf("Connecting to: %s ...\r\n", HOSTED_COM_PORT);

bool serialReady = false;
while(!(serialReady = hostedSerial.begin(HOSTED_COM_SPEED))) {
while(!hostedSerial.begin(HOSTED_COM_SPEED)) {
msleep(50);
}

hostedClient = new Hosted::Client(hostedSerial, '>');
hostedClient->getRemoteCommands();
init();

__real__Z4initv();
}

} // namespace
23 changes: 10 additions & 13 deletions Sming/Components/Hosted/init/tcp/InitClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
*
****/

#include <SmingCore.h>
#include <Platform/WifiEvents.h>
#include <Platform/Station.h>
#include <Hosted/Client.h>
#include <Hosted/Transport/TcpClientStream.h>

Hosted::Client* hostedClient{nullptr};
Hosted::Client* hostedClient;

#ifndef WIFI_SSID
#define WIFI_SSID "PleaseEnterSSID" // Put your SSID and password here
Expand All @@ -30,19 +31,14 @@ Hosted::Client* hostedClient{nullptr};
#define REMOTE_IP STRINGIFY(HOSTED_SERVER_IP)
#endif

extern "C" {
void __real_host_init();
void __wrap_host_init();
}

extern void init();
extern "C" void __real__Z4initv();

namespace
{
TcpClient* tcpClient = nullptr;
Hosted::Transport::TcpClientStream* stream = nullptr;
TcpClient* tcpClient;
Hosted::Transport::TcpClientStream* stream;

static void ready(IpAddress ip, IpAddress mask, IpAddress gateway)
void ready(IpAddress ip, IpAddress mask, IpAddress gateway)
{
if(hostedClient != nullptr) {
return;
Expand All @@ -59,12 +55,13 @@ static void ready(IpAddress ip, IpAddress mask, IpAddress gateway)

hostedClient = new Hosted::Client(*stream, '>');
hostedClient->getRemoteCommands();
init();

__real__Z4initv();
}

} // namespace

void __wrap_host_init()
extern "C" void __wrap__Z4initv()
{
WifiEvents.onStationGotIP(ready);
WifiStation.enable(true);
Expand Down
2 changes: 1 addition & 1 deletion Sming/Components/Hosted/samples/tcp/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENABLE_HOSTED :=

# If set the application should connect to a WIFI access point
# otherwise it will set its own access point
COMPONENT_RELINK_VARS := CONNECT_TO_WIFI
CONFIG_VARS := CONNECT_TO_WIFI
CONNECT_TO_WIFI ?= 0

APP_CFLAGS = -DCONNECT_TO_WIFI=$(CONNECT_TO_WIFI)

0 comments on commit c733819

Please sign in to comment.