2019-10-12 22:19:55 +00:00
|
|
|
//
|
|
|
|
// SerialPort.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 12/10/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef SerialPort_hpp
|
|
|
|
#define SerialPort_hpp
|
|
|
|
|
2019-10-13 03:14:29 +00:00
|
|
|
#include <vector>
|
2019-10-17 03:21:14 +00:00
|
|
|
#include "../../Storage/Storage.hpp"
|
2019-10-30 02:36:29 +00:00
|
|
|
#include "../../ClockReceiver/ClockReceiver.hpp"
|
2019-11-09 20:00:12 +00:00
|
|
|
#include "../../ClockReceiver/ForceInline.hpp"
|
2019-10-12 22:19:55 +00:00
|
|
|
|
2019-10-13 03:14:29 +00:00
|
|
|
namespace Serial {
|
2019-10-12 22:19:55 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@c Line connects a single reader and a single writer, allowing timestamped events to be
|
|
|
|
published and consumed, potentially with a clock conversion in between. It allows line
|
|
|
|
levels to be written and read in larger collections.
|
|
|
|
|
|
|
|
It is assumed that the owner of the reader and writer will ensure that the reader will never
|
|
|
|
get ahead of the writer. If the writer posts events behind the reader they will simply be
|
|
|
|
given instanteous effect.
|
|
|
|
*/
|
|
|
|
class Line {
|
|
|
|
public:
|
2019-10-30 02:36:29 +00:00
|
|
|
void set_writer_clock_rate(HalfCycles clock_rate);
|
2019-10-14 00:41:08 +00:00
|
|
|
|
2019-10-13 03:14:29 +00:00
|
|
|
/// Advances the read position by @c cycles relative to the writer's
|
|
|
|
/// clock rate.
|
2019-10-30 02:36:29 +00:00
|
|
|
void advance_writer(HalfCycles cycles);
|
2019-10-12 22:19:55 +00:00
|
|
|
|
2019-10-13 03:14:29 +00:00
|
|
|
/// Sets the line to @c level.
|
|
|
|
void write(bool level);
|
2019-10-12 22:19:55 +00:00
|
|
|
|
2019-10-13 03:14:29 +00:00
|
|
|
/// Enqueues @c count level changes, the first occurring immediately
|
2019-10-12 22:19:55 +00:00
|
|
|
/// after the final event currently posted and each subsequent event
|
2019-10-13 03:14:29 +00:00
|
|
|
/// occurring @c cycles after the previous. An additional gap of @c cycles
|
|
|
|
/// is scheduled after the final output. The levels to output are
|
2019-10-12 22:19:55 +00:00
|
|
|
/// taken from @c levels which is read from lsb to msb. @c cycles is
|
|
|
|
/// relative to the writer's clock rate.
|
2019-10-30 02:36:29 +00:00
|
|
|
void write(HalfCycles cycles, int count, int levels);
|
2019-10-12 22:19:55 +00:00
|
|
|
|
2019-10-13 03:14:29 +00:00
|
|
|
/// @returns the number of cycles until currently enqueued write data is exhausted.
|
2019-11-09 20:00:12 +00:00
|
|
|
forceinline HalfCycles write_data_time_remaining() const {
|
|
|
|
return HalfCycles(remaining_delays_);
|
|
|
|
}
|
2019-10-13 03:14:29 +00:00
|
|
|
|
2019-10-21 02:10:05 +00:00
|
|
|
/// @returns the number of cycles left until it is guaranteed that a passive reader
|
|
|
|
/// has received all currently-enqueued bits.
|
2019-11-09 20:00:12 +00:00
|
|
|
forceinline HalfCycles transmission_data_time_remaining() const {
|
|
|
|
return HalfCycles(remaining_delays_ + transmission_extra_);
|
|
|
|
}
|
2019-10-21 02:10:05 +00:00
|
|
|
|
2019-10-13 03:14:29 +00:00
|
|
|
/// Eliminates all future write states, leaving the output at whatever it is now.
|
|
|
|
void reset_writing();
|
2019-10-12 22:19:55 +00:00
|
|
|
|
2019-10-13 03:14:29 +00:00
|
|
|
/// @returns The instantaneous level of this line.
|
2019-10-12 22:19:55 +00:00
|
|
|
bool read();
|
2019-10-13 03:14:29 +00:00
|
|
|
|
2019-10-17 03:21:14 +00:00
|
|
|
struct ReadDelegate {
|
2019-10-18 03:34:39 +00:00
|
|
|
virtual bool serial_line_did_produce_bit(Line *line, int bit) = 0;
|
2019-10-17 03:21:14 +00:00
|
|
|
};
|
2019-10-18 03:34:39 +00:00
|
|
|
/*!
|
|
|
|
Sets a read delegate, which will receive samples of the output level every
|
|
|
|
@c bit_lengths of a second apart subject to a state machine:
|
|
|
|
|
|
|
|
* initially no bits will be delivered;
|
|
|
|
* when a zero level is first detected, the line will wait half a bit's length, then start
|
|
|
|
sampling at single-bit intervals, passing each bit to the delegate while it returns @c true;
|
|
|
|
* as soon as the delegate returns @c false, the line will return to the initial state.
|
|
|
|
*/
|
|
|
|
void set_read_delegate(ReadDelegate *delegate, Storage::Time bit_length);
|
2019-10-17 03:21:14 +00:00
|
|
|
|
2019-10-13 03:14:29 +00:00
|
|
|
private:
|
|
|
|
struct Event {
|
|
|
|
enum Type {
|
|
|
|
Delay, SetHigh, SetLow
|
|
|
|
} type;
|
|
|
|
int delay;
|
|
|
|
};
|
|
|
|
std::vector<Event> events_;
|
2019-10-30 02:36:29 +00:00
|
|
|
HalfCycles::IntType remaining_delays_ = 0;
|
|
|
|
HalfCycles::IntType transmission_extra_ = 0;
|
2019-10-22 00:18:33 +00:00
|
|
|
bool level_ = true;
|
2019-10-30 02:36:29 +00:00
|
|
|
HalfCycles clock_rate_ = 0;
|
2019-10-17 03:21:14 +00:00
|
|
|
|
|
|
|
ReadDelegate *read_delegate_ = nullptr;
|
2019-10-18 03:34:39 +00:00
|
|
|
Storage::Time read_delegate_bit_length_, time_left_in_bit_;
|
2019-10-17 03:21:14 +00:00
|
|
|
int write_cycles_since_delegate_call_ = 0;
|
2019-10-18 03:34:39 +00:00
|
|
|
enum class ReadDelegatePhase {
|
|
|
|
WaitingForZero,
|
|
|
|
Serialising
|
|
|
|
} read_delegate_phase_ = ReadDelegatePhase::WaitingForZero;
|
|
|
|
|
|
|
|
void update_delegate(bool level);
|
2019-10-30 02:36:29 +00:00
|
|
|
HalfCycles::IntType minimum_write_cycles_for_read_delegate_bit();
|
2019-10-12 22:19:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Defines an RS-232-esque srial port.
|
|
|
|
*/
|
|
|
|
class Port {
|
|
|
|
public:
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* SerialPort_hpp */
|