From dca767066a1403a0676b9cfe9130713279891fff Mon Sep 17 00:00:00 2001 From: Andrew Kay Date: Thu, 9 Jul 2020 18:44:50 -0500 Subject: [PATCH] Bridge the old and the new --- interface2/firmware/include/CoaxTransceiver.h | 21 +++++ interface2/firmware/include/NewCoax.h | 20 +++++ interface2/firmware/src/CoaxTransceiver.cpp | 41 +++++++++ interface2/firmware/src/NewCoax.cpp | 89 ++++++++++++++++--- interface2/firmware/src/main.cpp | 27 +----- 5 files changed, 160 insertions(+), 38 deletions(-) create mode 100644 interface2/firmware/include/CoaxTransceiver.h create mode 100644 interface2/firmware/src/CoaxTransceiver.cpp mode change 100644 => 120000 interface2/firmware/src/main.cpp diff --git a/interface2/firmware/include/CoaxTransceiver.h b/interface2/firmware/include/CoaxTransceiver.h new file mode 100644 index 0000000..ca4a84c --- /dev/null +++ b/interface2/firmware/include/CoaxTransceiver.h @@ -0,0 +1,21 @@ +// Copyright (c) 2020, Andrew Kay +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +#pragma once + +class CoaxTransceiver { +public: + static void setup(); + static int /* ssize_t */ transmitReceive(uint16_t *transmitBuffer, size_t transmitBufferCount, uint16_t *receiveBuffer, size_t receiveBufferSize, uint16_t receiveTimeout); +}; diff --git a/interface2/firmware/include/NewCoax.h b/interface2/firmware/include/NewCoax.h index 11eed7d..038b34b 100644 --- a/interface2/firmware/include/NewCoax.h +++ b/interface2/firmware/include/NewCoax.h @@ -1,11 +1,31 @@ #pragma once +#define ERROR_TX_RECEIVER_ACTIVE -1 + #define ERROR_RX_RECEIVER_ACTIVE -5 #define ERROR_RX_TIMEOUT -2 #define ERROR_RX_OVERFLOW -3 +class NewCoaxReceiver; class NewCoaxDataBus; +class NewCoaxTransmitter +{ +public: + NewCoaxTransmitter(NewCoaxDataBus &dataBus, NewCoaxReceiver &receiver) : + _dataBus(dataBus), _receiver(receiver) { }; + + void begin(); + int transmit(uint16_t *buffer, size_t bufferCount); + +private: + NewCoaxDataBus &_dataBus; + NewCoaxReceiver &_receiver; + + void reset(); + void write(uint16_t word); +}; + enum NewCoaxReceiverState { Disabled, Idle, Receiving, Received }; class NewCoaxReceiver diff --git a/interface2/firmware/src/CoaxTransceiver.cpp b/interface2/firmware/src/CoaxTransceiver.cpp new file mode 100644 index 0000000..a4c3743 --- /dev/null +++ b/interface2/firmware/src/CoaxTransceiver.cpp @@ -0,0 +1,41 @@ +// Copyright (c) 2020, Andrew Kay +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +#include + +#include "NewCoax.h" +#include "CoaxTransceiver.h" + +NewCoaxDataBus dataBus; +NewCoaxReceiver receiver(dataBus); +NewCoaxTransmitter transmitter(dataBus, receiver); + +void CoaxTransceiver::setup() +{ + dataBus.setMode(INPUT); + + receiver.begin(); + transmitter.begin(); +} + +int /* ssize_t */ CoaxTransceiver::transmitReceive(uint16_t *transmitBuffer, size_t transmitBufferCount, uint16_t *receiveBuffer, size_t receiveBufferSize, uint16_t receiveTimeout) +{ + int returnValue = transmitter.transmit(transmitBuffer, transmitBufferCount); + + if (returnValue < 0) { + return returnValue; + } + + return receiver.receive(receiveBuffer, receiveBufferSize, receiveTimeout); +} diff --git a/interface2/firmware/src/NewCoax.cpp b/interface2/firmware/src/NewCoax.cpp index 47156bc..e44c7b2 100644 --- a/interface2/firmware/src/NewCoax.cpp +++ b/interface2/firmware/src/NewCoax.cpp @@ -2,12 +2,17 @@ #include -//#define RX_ENABLE_PIN 4 -#define RX_RESET_PIN 4 -#define RX_ACTIVE_PIN 5 -#define RX_ERROR_PIN 6 -#define RX_DATA_AVAILABLE_PIN 7 -#define RX_READ_PIN 8 +#define RESET_PIN 4 + +#define TX_ACTIVE_PIN 5 +#define TX_LOAD_PIN 6 +#define TX_FULL_PIN 7 + +#define RX_ENABLE_PIN 8 +#define RX_ACTIVE_PIN 9 +#define RX_ERROR_PIN 10 +#define RX_DATA_AVAILABLE_PIN 11 +#define RX_READ_PIN 12 #define DATA_BUS_START_PIN 14 #define DATA_BUS_END_PIN 23 @@ -42,12 +47,72 @@ void rxDataAvailableInterrupt() receiver->dataAvailableInterrupt(); } +void NewCoaxTransmitter::begin() +{ + pinMode(RESET_PIN, OUTPUT); + + pinMode(TX_ACTIVE_PIN, INPUT); + pinMode(TX_LOAD_PIN, OUTPUT); + pinMode(TX_FULL_PIN, INPUT); +} + +int NewCoaxTransmitter::transmit(uint16_t *buffer, size_t bufferCount) +{ + if (digitalRead(RX_ACTIVE_PIN)) { + return ERROR_TX_RECEIVER_ACTIVE; + } + + _receiver.disable(); + + _dataBus.setMode(OUTPUT); + + for (int index = 0; index < bufferCount; index++) { + uint16_t word = _dataBus.encode(buffer[index]); + + while (digitalRead(TX_FULL_PIN)) { + // NOP + } + + write(word); + + // TODO: the Teensy, too fast! + delayMicroseconds(2); + } + + while (digitalRead(TX_ACTIVE_PIN)) { + // NOP + } + + _dataBus.setMode(INPUT); + + return bufferCount; +} + +void NewCoaxTransmitter::reset() +{ + digitalWrite(RESET_PIN, HIGH); + delayMicroseconds(1); + digitalWrite(RESET_PIN, LOW); +} + +inline void NewCoaxTransmitter::write(uint16_t word) +{ + digitalWrite(TX_LOAD_PIN, HIGH); + + _dataBus.write(word); + + delayMicroseconds(1); + + digitalWrite(TX_LOAD_PIN, LOW); +} + void NewCoaxReceiver::begin() { receiver = this; - //pinMode(RX_ENABLE_PIN, OUTPUT); - pinMode(RX_RESET_PIN, OUTPUT); + pinMode(RESET_PIN, OUTPUT); + + pinMode(RX_ENABLE_PIN, OUTPUT); pinMode(RX_ACTIVE_PIN, INPUT); pinMode(RX_ERROR_PIN, INPUT); pinMode(RX_DATA_AVAILABLE_PIN, INPUT); @@ -64,12 +129,12 @@ void NewCoaxReceiver::enable() _state = Idle; - //digitalWrite(RX_ENABLE_PIN, HIGH); + digitalWrite(RX_ENABLE_PIN, HIGH); } void NewCoaxReceiver::disable() { - //digitalWrite(RX_ENABLE_PIN, LOW); + digitalWrite(RX_ENABLE_PIN, LOW); _state = Disabled; } @@ -181,9 +246,9 @@ void NewCoaxReceiver::errorInterrupt() void NewCoaxReceiver::reset() { - digitalWrite(RX_RESET_PIN, HIGH); + digitalWrite(RESET_PIN, HIGH); delayMicroseconds(1); - digitalWrite(RX_RESET_PIN, LOW); + digitalWrite(RESET_PIN, LOW); } inline uint16_t NewCoaxReceiver::read() diff --git a/interface2/firmware/src/main.cpp b/interface2/firmware/src/main.cpp deleted file mode 100644 index ca49ba7..0000000 --- a/interface2/firmware/src/main.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include - -#include - -NewCoaxDataBus dataBus; -NewCoaxReceiver receiver(dataBus); - -uint16_t buffer[1024]; - -void setup() -{ - Serial.begin(115200); - - while (Serial.available() > 0) { - Serial.read(); - } - - receiver.begin(); -} - -void loop() -{ - int count = receiver.receive(buffer, 1024, 1000); - - Serial.println(count); -} diff --git a/interface2/firmware/src/main.cpp b/interface2/firmware/src/main.cpp new file mode 120000 index 0000000..9db435b --- /dev/null +++ b/interface2/firmware/src/main.cpp @@ -0,0 +1 @@ +../../../interface1/firmware/src/main.cpp \ No newline at end of file