Bridge the old and the new

This commit is contained in:
Andrew Kay
2020-07-09 18:44:50 -05:00
parent 32419dd760
commit dca767066a
5 changed files with 160 additions and 38 deletions

View File

@@ -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);
};

View File

@@ -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

View File

@@ -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 <Arduino.h>
#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);
}

View File

@@ -2,12 +2,17 @@
#include <NewCoax.h>
//#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()

View File

@@ -1,26 +0,0 @@
#include <Arduino.h>
#include <NewCoax.h>
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);
}

View File

@@ -0,0 +1 @@
../../../interface1/firmware/src/main.cpp