Skip to content

Commit

Permalink
Enable Host access to physical COM ports (#2326)
Browse files Browse the repository at this point in the history
This PR extends the Host serial port emulation to include local physical devices using the `--device` command-line option. For example:

```
make run HOST_UART_OPTIONS="--uart=0 --device=/dev/ttyUSB0"
```

The `--device` option must follow the `--uart` option. Another example:

```
make run HOST_UART_OPTIONS="--uart=0 --device=/dev/ttyUSB0 --uart=1 --device=/dev/ttyUSB1"
```

The port is opened when `uart_init` gets called. The default baud rate is whatever the application has requested. This can be overridden as follows:

```
make run HOST_UART_OPTIONS="--uart=0 --device=/dev/ttyUSB0 --baud=921600"
```
  • Loading branch information
mikee47 authored May 5, 2021
1 parent dc0747c commit 1123cdf
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 81 deletions.
8 changes: 4 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
path = Sming/Arch/Host/Components/lwip/lwip
url = https://github.com/lwip-tcpip/lwip
ignore = dirty
[submodule "seriallib"]
path = Sming/Arch/Host/Components/SerialLib/seriallib
url = https://github.com/imabot2/serialib.git
ignore = dirty



Expand Down Expand Up @@ -255,10 +259,6 @@
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
7 changes: 7 additions & 0 deletions Sming/Arch/Host/Components/SerialLib/SerialLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "seriallib/lib/serialib.h"

class SerialDevice : public serialib
{
};
4 changes: 4 additions & 0 deletions Sming/Arch/Host/Components/SerialLib/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
COMPONENT_SUBMODULES += seriallib

COMPONENT_SRCDIRS := seriallib/lib
COMPONENT_INCDIRS := .
22 changes: 22 additions & 0 deletions Sming/Arch/Host/Components/SerialLib/seriallib.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
diff --git a/lib/serialib.cpp b/lib/serialib.cpp
index 8dbe52f..dc25811 100644
--- a/lib/serialib.cpp
+++ b/lib/serialib.cpp
@@ -135,7 +135,7 @@ char serialib::openDevice(const char *Device,const unsigned int Bauds)
case 115200 : dcbSerialParams.BaudRate=CBR_115200; break;
case 128000 : dcbSerialParams.BaudRate=CBR_128000; break;
case 256000 : dcbSerialParams.BaudRate=CBR_256000; break;
- default : return -4;
+ default : dcbSerialParams.BaudRate=Bauds;
}
// 8 bit data
dcbSerialParams.ByteSize=8;
@@ -609,7 +609,7 @@ int serialib::available()
// Device errors
DWORD commErrors;
// Device status
- COMSTAT commStatus;
+ COMSTAT commStatus{};
// Read status
ClearCommError(hSerial, &commErrors, &commStatus);
// Return the number of pending bytes
4 changes: 3 additions & 1 deletion Sming/Arch/Host/Components/driver/component.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
COMPONENT_INCDIRS := include

COMPONENT_DEPENDS := arch_driver
COMPONENT_DEPENDS := \
arch_driver \
SerialLib

COMPONENT_VARS += USE_US_TIMER
USE_US_TIMER ?= 1
Expand Down
32 changes: 27 additions & 5 deletions Sming/Arch/Host/Components/driver/uart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Host UART driver
Introduction
------------

Implements a UART driver to connect via TCP sockets, allowing terminal emulation using telnet.
Implements a UART driver to connect via TCP socket, allowing terminal emulation using telnet,
or directly to local host serial device (e.g. /dev/ttyUSB0, COM4, etc.)

By default, output to UART0 is sent to the console and keyboard input is written to the UART0 receive queue.
If emulation is enabled on any ports then this behaviour is disabled.
Expand Down Expand Up @@ -44,11 +45,9 @@ Build variables

Allows full customisation of UART command-line options for ``make run``.

You should not need to change this for normal use.


Usage
-----
TCP port emulation
------------------

Set required ports for emulation using the :envvar:`ENABLE_HOST_UARTID`, then execute ``make run``.

Expand Down Expand Up @@ -79,3 +78,26 @@ output at startup.)
Port numbers are allocated sequentially from 10000. If you want to use
different port numbers, set :envvar:`HOST_UART_PORTBASE`.


Physical device connection
--------------------------

Override :envvar:`HOST_UART_OPTIONS` adding the `--device` option. For example::

make run HOST_UART_OPTIONS="--uart=0 --device=/dev/ttyUSB0"

The ``--device`` option must follow the ``--uart`` option. Another example::

make run HOST_UART_OPTIONS="--uart=0 --device=/dev/ttyUSB0 --uart=1 --device=/dev/ttyUSB1"

The port is opened when ``uart_init()`` gets called.

The default baud rate is whatever the application has requested. This can be overridden as follows::

make run HOST_UART_OPTIONS="--uart=0 --device=/dev/ttyUSB0 --baud=921600"

For Windows, substitute the appropriate device name, e.g. ``COM4`` instead of ``/dev/ttyUSB0``.

.. note::

If necessary, add ``ENABLE_HOST_UARTID=`` to prevent telnet windows from being created.
Loading

0 comments on commit 1123cdf

Please sign in to comment.