Skip to content

Commit

Permalink
[#110] More Uart channel updates, with UartChannel example
Browse files Browse the repository at this point in the history
  • Loading branch information
susanw1 committed Nov 9, 2023
1 parent 3f20163 commit 72a30ea
Show file tree
Hide file tree
Showing 21 changed files with 386 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ commands:
'@type': number
required: no
responseFields:
- key: C
- key: N
name: channelCount
description: the number of channels the device has
typeDefinition:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ name: Uart
id: 7
version: 0.0.1
description: UART Connectivity for Serial communication
longDescription: >
This module supports configuring and using multiple device UARTs, either as plain p2p data send/receive connection,
or as a Zscript channel (for receiving commands from upstream), or as a downstream address connection.
It supports two mechanisms for baud-rate configuration: a menu set of options (0-n), or actual frequency (using the big
field).
commands:
- name: capabilities
Expand Down Expand Up @@ -75,7 +81,7 @@ commands:
required: yes
- key: +
name: baudRate
description: the baud rate option requested in Hz (or maximum rate supported if not given)
description: the baud rate menu option requested in Hz (or maximum rate supported if not given)
typeDefinition:
'@type': bytes
required: yes
Expand Down Expand Up @@ -320,7 +326,7 @@ commands:
'@type': number
required: yes
- key: F
name: frequency
name: frequencySelection
description: which frequency option should be selected from the 'menu'
typeDefinition:
'@type': number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,42 @@ class ZscriptParams {
return (uint16_t) millis();
}
typedef uint8_t tokenBufferSize_t;
static constexpr uint8_t lockByteCount = 1;

static const uint8_t tcpChannelCount = 1;
static const uint16_t tcpLocalPort = 23;
static const uint8_t tcpBufferSize = 128;
static constexpr uint8_t pinCount = 21;

static const uint8_t udpChannelCount = 1;
static const uint16_t udpLocalPort = 8888;
static const uint8_t udpBufferSize = 128;
static constexpr uint16_t i2cBufferSize = 128;
static constexpr uint16_t i2cChannelOutputBufferSize = 32;
static constexpr uint16_t i2cAddressingReadBlockLength = 8;
static constexpr uint16_t i2cAlertInPin = 2;
static constexpr uint16_t i2cAlertOutPin = 9;
static constexpr uint8_t i2cChannelAddress = 0x60;

static const uint8_t lockByteCount = 1;
static const uint16_t serialBufferSize = 128;
static const uint16_t i2cBufferSize = 128;
static constexpr uint16_t uartCount = 1;
static constexpr uint16_t uartRxBufferSize = 0;
static constexpr uint16_t uartTxBufferSize = 0;
static constexpr uint32_t uartSupportedFreqs[] = {9600, 115200};
static constexpr uint16_t uartChannelCount = 1;
static constexpr uint16_t uartChannelInterface = 0;
static constexpr uint16_t uartChannelBufferSize = 128;

static const uint16_t i2cChannelOutputBufferSize = 32;
static const uint16_t I2cAddressingReadBlockLength = 8;
static const uint16_t i2cAlertInPin = 2;
static const uint16_t i2cAlertOutPin = 9;
static const uint8_t i2cChannelAddress = 0x60;

static const uint16_t nonActivatedChannelTimeout = 5000;
static constexpr uint8_t servoCount = 1;
//static constexpr uint8_t servoPins[1];

static const uint8_t pinCount = 21;
static constexpr uint8_t udpChannelCount = 1;
static constexpr uint16_t udpLocalPort = 8888;
static constexpr uint8_t udpBufferSize = 128;

static const uint8_t servoCount = 1;
//static const uint8_t servoPins[1];
static constexpr uint8_t tcpChannelCount = 1;
static constexpr uint16_t tcpLocalPort = 23;
static constexpr uint8_t tcpBufferSize = 128;

static constexpr uint16_t nonActivatedChannelTimeout = 5000;
};
//const uint8_t ZscriptParams::servoPins[1] = {9};

constexpr uint32_t ZscriptParams::uartSupportedFreqs[];
//constexpr uint8_t ZscriptParams::servoPins[1];

#endif /* SRC_MAIN_CPP_ARDUINO_CONVERTER_FILES_ZSCRIPTDEFAULTPARAMETERS_HPP_ */

Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ArduinoZscriptBasicSetup {
+ ZscriptParams::udpChannelCount
#endif
#ifdef ZSCRIPT_HAVE_TCP_CHANNEL
+ZscriptParams::tcpChannelCount
+ ZscriptParams::tcpChannelCount
#endif
];
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,26 +227,26 @@ class I2cChannel : public ZscriptChannel<ZP> {
}
}

void channelInfo(ZscriptCommandContext<ZP> ctx) {
CommandOutStream<ZP> out = ctx.getOutStream();
out.writeField('N', 0);
out.writeField('M', 0x5);
}

void channelSetup(ZscriptCommandContext<ZP> ctx) {
uint16_t addr;
if (ctx.getField('A', &addr)) {
if (addr >= 128) {
ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE);
return;
}
out.setAddr((uint8_t) addr);
}
if (ctx.hasField('P')) {
uint8_t index = this->parser.getChannelIndex();
PersistenceSystem<ZP>::writeSection(PersistenceSystem<ZP>::getNotifChannelIdOffset(), 1, &index);
}
}
// void channelInfo(ZscriptCommandContext<ZP> ctx) {
// CommandOutStream<ZP> ctxOut = ctx.getOutStream();
// ctxOut.writeField('N', 0);
// ctxOut.writeField('M', 0x5);
// }
//
// void channelSetup(ZscriptCommandContext<ZP> ctx) {
// uint16_t addr;
// if (ctx.getField('A', &addr)) {
// if (addr >= 128) {
// ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE);
// return;
// }
// out.setAddr((uint8_t) addr);
// }
// if (ctx.hasField('P')) {
// uint8_t index = this->parser.getChannelIndex();
// PersistenceSystem<ZP>::writeSection(PersistenceSystem<ZP>::getNotifChannelIdOffset(), 1, &index);
// }
// }

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class ZscriptI2cChannelInfoCommand {


static void execute(ZscriptCommandContext<ZP> ctx) {
uint16_t channel;
if (ctx.getField(ParamChannel__C, &channel)) {
if (channel != 0) {
uint16_t channelIndex;
if (ctx.getField(ParamChannel__C, &channelIndex)) {
if (channelIndex != 0) {
ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ class ZscriptI2cChannelSetupCommand {


static void execute(ZscriptCommandContext<ZP> ctx) {
uint16_t channel;
if (ctx.getField(ParamChannel__C, &channel)) {
if (channel != 0) {
uint16_t channelIndex;
if (ctx.getField(ParamChannel__C, &channelIndex)) {
if (channelIndex != 0) {
ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE);
return;
}
}
uint16_t address;
if (ctx.getField(ParamAddress__A, &address)) {
if (channel > 0x80) {
if (address > 0x80) {
ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ class UartModule : public ZscriptModule<ZP> {
static void setup() {
#ifdef ZSCRIPT_HAVE_UART_CHANNEL
channel.setup();
channel.setAddress(ZscriptParams::i2cChannelAddress);
channel.setBananaAddress(ZscriptParams::i2cChannelAddress);
#else
Serial.begin(9600);
Serial.begin(ZP::uartSupportedFreqs[0]);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
namespace Zscript {

template<class ZP>
class ZscriptSerialOutStream : public AbstractOutStream<ZP> {
class ZscriptUartOutStream : public AbstractOutStream<ZP> {
private:
bool openB = false;

public:
ZscriptSerialOutStream() {
ZscriptUartOutStream() {
}

void open(uint8_t source) {
Expand Down Expand Up @@ -56,11 +56,11 @@ class ZscriptSerialOutStream : public AbstractOutStream<ZP> {

template<class ZP>
class UartChannel : public ZscriptChannel<ZP> {
ZscriptSerialOutStream<ZP> out;
ZscriptUartOutStream<ZP> out;
GenericCore::TokenRingBuffer<ZP> tBuffer;
ZscriptTokenizer<ZP> tokenizer;

uint8_t buffer[ZP::serialBufferSize];
uint8_t buffer[ZP::uartChannelBufferSize];
uint8_t tmp = 0;
bool usingTmp = false;

Expand All @@ -70,18 +70,20 @@ class UartChannel : public ZscriptChannel<ZP> {
}

UartChannel() :
ZscriptChannel<ZP>(&out, &tBuffer, 0x7, true), tBuffer(buffer, ZP::serialBufferSize), tokenizer(tBuffer.getWriter(), 2) {
ZscriptChannel<ZP>(&out, &tBuffer, 0x7, true), tBuffer(buffer, ZP::uartChannelBufferSize), tokenizer(tBuffer.getWriter(), 2) {
}

bool setupStartupNotificationChannel() {
return true;
}

void channelInfo(ZscriptCommandContext<ZP> ctx) {
CommandOutStream<ZP> out = ctx.getOutStream();
out.writeField('N', 0);
out.writeField('M', 0x7);
out.writeField('I', 0);
CommandOutStream<ZP> ctxOut = ctx.getOutStream();
// RespChannelCount__N
ctxOut.writeField('N', 0);
// ??
ctxOut.writeField('M', 0x7);
ctxOut.writeField('I', 0);
}

void channelSetup(ZscriptCommandContext<ZP> ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#define SRC_MAIN_CPP_ARDUINO_UART_MODULE_COMMANDS_ZSCRIPTUARTCAPABILITIESCOMMAND_HPP_

#include <zscript/modules/ZscriptCommand.hpp>
#include "UartUtil.hpp"
#include <net/zscript/model/components/ZscriptStatus.hpp>
#include <net/zscript/model/modules/base/UartModule.hpp>

#define COMMAND_EXISTS_0070 EXISTENCE_MARKER_UTIL
Expand All @@ -18,56 +20,36 @@ namespace Zscript {
namespace uart_module {
namespace cmd_capabilities {

using namespace uart_module;

template<class ZP>
class ZscriptUartCapabilitiesCommand {
public:
static constexpr uint8_t CODE = 0x0;

static constexpr uint8_t ReqFrequencySelection__F = 'F';
static void execute(ZscriptCommandContext<ZP> ctx) {
CommandOutStream<ZP> out = ctx.getOutStream();

static constexpr uint8_t RespCommandsSet__C = 'C';
static constexpr uint8_t RespNotificationsSupported__N = 'N';
/** Flag (Optional): whether addressing via the serial interface is available */
static constexpr uint8_t RespAddressingSupported__A = 'A';
/** Number (Required): how many interfaces are available */
static constexpr uint8_t RespInterfaceCount__I = 'I';
/** Number (Optional): how many frequencies are supported as a &#39;menu&#39; of frequencies - if not present, no preset frequencies are supported */
static constexpr uint8_t RespFrequenciesSupported__F = 'F';
/** Bitset (Required): a set of more specific capabilities of the module */
static constexpr uint8_t RespBitsetCapabilities__B = 'B';
/** Number (Required): how many bytes of buffer have been assigned for received data */
static constexpr uint8_t RespRxBufferSize__R = 'R';
/** Number (Required): how many bytes of buffer have been assigned for transmitted data */
static constexpr uint8_t RespTxBufferSize__T = 'T';
/** Bytes (Required): the baud rate option requested in Hz (or maximum rate supported if not given) */
static constexpr uint8_t RespBaudRate__Bytes = '+';
constexpr uint8_t freqCount = sizeof(ZP::uartSupportedFreqs) / sizeof(ZP::uartSupportedFreqs[0]);

uint16_t freqIndex;
if (!ctx.getFieldCheckLimit(ReqFrequencySelection__F, freqCount, freqCount - 1, &freqIndex)) {
return;
}

UartUtil<ZP>::writeFrequencySelection(out, freqIndex);

// static constexpr char RespCommands__C = 'C';
// static constexpr char RespNotificationsSupported__N = 'N';
// static constexpr char RespAddressingSupported__A = 'A';
// static constexpr char RespInterfaceCount__I = 'I';
// static constexpr char RespFrequenciesSupported__F = 'F';
// static constexpr char RespBitsetCapabilities__B = 'B';
//
static void execute(ZscriptCommandContext<ZP> ctx) {
// implements just a single Serial port at this time. Should be expanded to more...
CommandOutStream<ZP> out = ctx.getOutStream();
out.writeField(RespCommandsSet__C, MODULE_CAPABILITIES(007));
out.writeField(RespInterfaceCount__I, 1);
out.writeField(RespFrequenciesSupported__F, 1);
#ifdef ZSCRIPT_HAVE_UART_MODULE
// out.writeField(RespBitsetCapabilities__B, RespBitsetCapabilities__LowSpeedSupported | RespBitsetCapabilities__SmBusAddressResolution);
#else
// out.writeField(RespBitsetCapabilities__B, RespBitsetCapabilities__LowSpeedSupported);
#endif
out.writeField(RespInterfaceCount__I, ZP::uartCount);
out.writeField(RespFrequenciesSupported__F, freqCount);
out.writeField(RespBitsetCapabilities__B, static_cast<uint16_t>(RespBitsetCapabilities_Values::parityOn_field)
| static_cast<uint16_t>(RespBitsetCapabilities_Values::doubleStop_field));
out.writeField(RespRxBufferSize__R, ZP::uartRxBufferSize);
out.writeField(RespTxBufferSize__T, ZP::uartTxBufferSize);
#ifdef ZSCRIPT_UART_SUPPORT_NOTIFICATIONS
out.writeField(RespNotificationsSupported__N, 0);
#endif
#ifdef ZSCRIPT_SUPPORT_ADDRESSING
out.writeField(RespAddressingSupported__A, 0);
#endif
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,25 @@ class UartModule;
namespace uart_module {
namespace cmd_channel_info {

using namespace uart_module;


template<class ZP>
class ZscriptUartChannelInfoCommand {
public:
static constexpr uint8_t CODE = 0xc;

static void execute(ZscriptCommandContext<ZP> ctx) {
uint16_t channel;
if (ctx.getField(ReqChannel__C, &channel)) {
if (channel != 0) {
ctx.status(ResponseStatus::VALUE_OUT_OF_RANGE);
return;
}
uint16_t localChannelIndex;
if (!ctx.getFieldCheckLimit(ReqChannel__C, Zscript<ZP>::zscript.getChannelCount(), ctx.getChannelIndex(), &localChannelIndex)) {
return;
}

constexpr uint8_t freqCount = sizeof(ZP::uartSupportedFreqs) / sizeof(ZP::uartSupportedFreqs[0]);
uint16_t freqIndex;
if (!ctx.getFieldCheckLimit(ReqFrequencySelection__F, freqCount, freqCount - 1, &freqIndex)) {
return;
}

CommandOutStream<ZP> out = ctx.getOutStream();
// Just one UART channel supported currently
out.writeField(RespChannelCount__N, 1);
out.writeField(RespChannel__C, UartModule<ZP>::channel.getParser()->getChannelIndex());
out.writeField(RespInterface__I, 0);
Expand Down
Loading

0 comments on commit 72a30ea

Please sign in to comment.