Skip to content

Commit

Permalink
Feature: hosted serial (#2312)
Browse files Browse the repository at this point in the history
* Adds easier way on Linux and Windows to connect to a real serial port and communicate with the device over it.

* Added control over the DTS and RTS serial lines (pins).

* Updated the documentation.
  • Loading branch information
slaff committed Apr 22, 2021
1 parent 0664104 commit 8d65611
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@
path = Sming/Libraries/RingTone
url = https://github.com/mikee47/RingTone
ignore = dirty
[submodule "Libraries.seriallib"]
path = Sming/Libraries/seriallib/seriallib
url = https://github.com/imabot2/serialib.git
ignore = dirty
[submodule "Libraries.SignalGenerator"]
path = Sming/Libraries/SignalGenerator
url = https://github.com/mikee47/SignalGenerator
Expand Down Expand Up @@ -335,3 +339,4 @@
# END OF .gitmodules
#
#################################################################################################

22 changes: 20 additions & 2 deletions Sming/Components/Hosted/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The communication is done via `simplePRC <https://simplerpc.readthedocs.io/>`_ a

Overview
--------
Sming's host emulator allows easier debugging and development of embedded applications. This component named "HostEd" extends the host emulator
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.

For example in order to run the Basic_Blink application under the host emulator and run the actual blinking of a LED on a microcontroller
Expand Down Expand Up @@ -49,4 +49,22 @@ Configuration

Enables the hosted component. Valid values for the moment are:
- tcp - for communication over TCP network.
- serial - for communication over serial interface
- serial - for communication over serial interface

.. envvar:: HOSTED_SERVER_IP

Default: 192.168.13.1

Used only when ENABLE_HOSTED=tcp is specified. Specifies the IP address of the remote RPC server.

.. envvar:: HOSTED_COM_PORT

Default: /dev/ttyUSB0 or the value of the environment variable COM_PORT if defined

Used only when ENABLE_HOSTED=serial is specified. Specifies which local communication port should be used to connect to the remote RPC server.

.. envvar:: HOSTED_COM_SPEED

Default: 115200

Used only when ENABLE_HOSTED=serial is specified. Specifies the communication baud rate.
12 changes: 11 additions & 1 deletion Sming/Components/Hosted/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ ENABLE_HOSTED ?=
ifneq ($(ENABLE_HOSTED),)
COMPONENT_SRCDIRS += $(COMPONENT_PATH)/init/$(ENABLE_HOSTED)
EXTRA_LDFLAGS := -Wl,-wrap,host_init
COMPONENT_DEPENDS += seriallib
endif

COMPONENT_VARS += HOSTED_SERVER_IP

COMPONENT_CFLAGS = -DHOSTED_SERVER_IP=$(HOSTED_SERVER_IP)
COMPONENT_VARS += HOSTED_COM_PORT
HOSTED_COM_PORT := "/dev/ttyUSB0"
ifneq ($(COM_PORT),)
HOSTED_COM_PORT:=$(COM_PORT)
endif

COMPONENT_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_CXXFLAGS := $(COMPONENT_CFLAGS)
122 changes: 122 additions & 0 deletions Sming/Components/Hosted/include/Hosted/Serial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* Serial.h
*
* @author 2021 Slavey Karadzhov <slav@attachix.com>
*
*
****/

#pragma once

#ifndef ARCH_HOST
#error "Hosted::Serial can be used only on the Host architecture!"
#endif

#include <Stream.h>
#include <WString.h>
#include <serialib.h>

namespace Hosted
{
class Serial : public Stream
{
public:
Serial(const String& ttyDevice) : ttyDevice(ttyDevice)
{
}

~Serial()
{
transport.closeDevice();
}

bool setDtr(bool on)
{
return transport.DTR(on);
}

bool setRts(bool on)
{
return transport.RTS(on);
}

/** @brief Initialise the serial port
* @param baud BAUD rate of the serial port (Default: 9600)
*/
bool begin(uint32_t baud = 9600)
{
char result = transport.openDevice(ttyDevice.c_str(), baud);
if(result == 1) {
return true;
}

debug_w("Hosted::Serial:begin error: %d", result);
return false;
}

size_t write(uint8_t c) override
{
if(transport.writeChar(c)) {
return 1;
}

return 0;
}

int available() override
{
return transport.available();
}

int read() override
{
int ch;
int result = transport.readChar(reinterpret_cast<char*>(&ch), 1);
if(result == 1) {
return ch;
}

return -1;
}

size_t readBytes(char* buffer, size_t length) override
{
int result = transport.readBytes(buffer, length, 100, 100);
if(result > 0) {
return result;
}

return 0;
}

size_t write(const uint8_t* buffer, size_t size)
{
char result = transport.writeBytes(buffer, size);
if(result == 1) {
return size;
}

return 0;
}

int peek() override
{
return -1;
}

void flush() override
{
}

private:
String ttyDevice;

serialib transport;
};

} // namespace Hosted
21 changes: 20 additions & 1 deletion Sming/Components/Hosted/init/serial/InitClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

#include <SmingCore.h>
#include <Hosted/Client.h>
#include <Hosted/Serial.h>

#ifndef HOSTED_COM_PORT
#define HOSTED_COM_PORT "/dev/ttyUSB0"
#endif

#ifndef HOSTED_COM_SPEED
#define HOSTED_COM_SPEED 115200
#endif

Hosted::Client* hostedClient{nullptr};

Expand All @@ -23,10 +32,20 @@ void __real_host_init();
void __wrap_host_init();
}

Hosted::Serial hostedSerial(HOSTED_COM_PORT);

void __wrap_host_init()
{
Serial.begin(115200);
hostedClient = new Hosted::Client(Serial);
Serial.printf("Connecting to: %s ...\n", HOSTED_COM_PORT);

bool serialReady = false;
do {
serialReady = hostedSerial.begin(HOSTED_COM_SPEED);
usleep(200);
} while(!serialReady);

hostedClient = new Hosted::Client(hostedSerial);
hostedClient->getRemoteCommands();
init();
}
6 changes: 6 additions & 0 deletions Sming/Libraries/seriallib/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
COMPONENT_SUBMODULES += seriallib

SERIALLIB_ROOT := $(COMPONENT_PATH)/seriallib

COMPONENT_SRCDIRS := $(SERIALLIB_ROOT)/lib $(COMPONENT_PATH)/src
COMPONENT_INCDIRS := $(COMPONENT_SRCDIRS) $(COMPONENT_PATH)/include
1 change: 1 addition & 0 deletions Sming/Libraries/seriallib/seriallib
Submodule seriallib added at 8e7e23

0 comments on commit 8d65611

Please sign in to comment.