Skip to content

Commit

Permalink
Move Host uart_server code into driver Component, and default to cons…
Browse files Browse the repository at this point in the history
…ole redirection if no ports enabled

For simple applications, avoids the need to run an external telnet instance to see serial port output
  • Loading branch information
mikee47 committed Jun 28, 2019
1 parent 7a7776c commit 0126e4e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,34 @@ unsigned CUartServer::portBase = 10000;

static CUartServer* uartServers[UART_COUNT];

// Redirect the main serial port to console output
static void redirectToConsole()
{
auto onNotify = [](uart_t* uart, uart_notify_code_t code) {
if(code == UART_NOTIFY_AFTER_WRITE){
size_t avail;
void* data;
while((avail = uart->tx_buffer->getReadData(data)) != 0) {
host_nputs(static_cast<const char*>(data), avail);
uart->tx_buffer->skipRead(avail);
}
}
};
uart_set_notify(UART0, onNotify);
}

void CUartServer::startup(const UartServerConfig& config)
{
if(config.portBase != 0) {
portBase = config.portBase;
}

// If no ports have been enabled then redirect port 0 output to host console
if(config.enableMask == 0) {
redirectToConsole();
return;
}

auto notify = [](uart_t* uart, uart_notify_code_t code) {
auto server = uartServers[uart->uart_nr];
if(server) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#pragma once

#include <driver/uart.h>
#include "sockets.h"
#include "threads.h"
#include <hostlib/sockets.h>
#include <hostlib/threads.h>

#define UART_SOCKET_PORT_BASE 10000 ///< Port for UART0

Expand Down
5 changes: 5 additions & 0 deletions Sming/Arch/Host/Components/hostlib/hostmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ void host_printfp(const char* fmt, const char* pretty_function, ...)
host_puts(buffer);
}

size_t host_nputs(const char* str, size_t length)
{
return fwrite(str, 1, length, stderr);
}

void host_puts(const char* str)
{
fputs(str, stderr);
Expand Down
3 changes: 3 additions & 0 deletions Sming/Arch/Host/Components/hostlib/hostmsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@

#pragma once

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

void host_printf(const char* fmt, ...);
void host_printfp(const char* fmt, const char* pretty_function, ...);
size_t host_nputs(const char* str, size_t length);
void host_puts(const char* str);

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion Sming/Arch/Host/Components/hostlib/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "except.h"
#include "options.h"
#include <spi_flash/flashmem.h>
#include "uart_server.h"
#include <driver/uart_server.h>
#include <BitManipulations.h>
#include <esp_timer_legacy.h>
#include <esp_tasks.h>
Expand Down

0 comments on commit 0126e4e

Please sign in to comment.