2016-07-12 02:12:58 +00:00
|
|
|
//
|
|
|
|
// DigitalPhaseLockedLoop.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 11/07/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-07-12 02:12:58 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef DigitalPhaseLockedLoop_hpp
|
|
|
|
#define DigitalPhaseLockedLoop_hpp
|
|
|
|
|
2020-01-12 22:25:21 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cassert>
|
2016-07-13 00:23:56 +00:00
|
|
|
#include <memory>
|
2016-07-28 15:32:14 +00:00
|
|
|
#include <vector>
|
2017-07-26 00:20:55 +00:00
|
|
|
|
2017-09-23 02:39:23 +00:00
|
|
|
#include "../../../ClockReceiver/ClockReceiver.hpp"
|
2016-07-13 00:23:56 +00:00
|
|
|
|
2016-07-12 02:12:58 +00:00
|
|
|
namespace Storage {
|
|
|
|
|
2020-01-12 22:25:21 +00:00
|
|
|
/*!
|
|
|
|
Template parameters:
|
|
|
|
|
2020-01-12 22:45:02 +00:00
|
|
|
@c bit_handler A class that must implement a method, digital_phase_locked_loop_output_bit(int) for receving bits from the DPLL.
|
2020-01-12 22:25:21 +00:00
|
|
|
@c length_of_history The number of historic pulses to consider in locking to phase.
|
|
|
|
*/
|
2020-01-12 22:33:34 +00:00
|
|
|
template <typename BitHandler, size_t length_of_history = 3> class DigitalPhaseLockedLoop {
|
2016-07-12 02:12:58 +00:00
|
|
|
public:
|
|
|
|
/*!
|
|
|
|
Instantiates a @c DigitalPhaseLockedLoop.
|
|
|
|
|
|
|
|
@param clocks_per_bit The expected number of cycles between each bit of input.
|
|
|
|
*/
|
2020-01-12 22:45:02 +00:00
|
|
|
DigitalPhaseLockedLoop(int clocks_per_bit, BitHandler &handler) :
|
|
|
|
bit_handler_(handler), window_length_(clocks_per_bit), clocks_per_bit_(clocks_per_bit) {}
|
2020-01-12 22:25:21 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Changes the expected window length.
|
|
|
|
*/
|
|
|
|
void set_clocks_per_bit(int clocks_per_bit) {
|
|
|
|
window_length_ = clocks_per_bit_ = clocks_per_bit;
|
|
|
|
}
|
2016-07-12 02:12:58 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Runs the loop, impliedly posting no pulses during that period.
|
|
|
|
|
|
|
|
@c number_of_cycles The time to run the loop for.
|
|
|
|
*/
|
2020-01-12 22:25:21 +00:00
|
|
|
void run_for(const Cycles cycles) {
|
|
|
|
offset_ += cycles.as_integral();
|
|
|
|
phase_ += cycles.as_integral();
|
|
|
|
if(phase_ >= window_length_) {
|
|
|
|
auto windows_crossed = phase_ / window_length_;
|
|
|
|
|
2020-01-12 22:45:02 +00:00
|
|
|
// Check whether this triggers any 0s.
|
|
|
|
if(window_was_filled_) --windows_crossed;
|
|
|
|
for(int c = 0; c < windows_crossed; c++)
|
|
|
|
bit_handler_.digital_phase_locked_loop_output_bit(0);
|
2020-01-12 22:25:21 +00:00
|
|
|
|
|
|
|
window_was_filled_ = false;
|
|
|
|
phase_ %= window_length_;
|
|
|
|
}
|
|
|
|
}
|
2016-07-12 02:12:58 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Announces a pulse at the current time.
|
|
|
|
*/
|
2020-01-12 22:25:21 +00:00
|
|
|
void add_pulse() {
|
|
|
|
if(!window_was_filled_) {
|
2020-01-12 22:45:02 +00:00
|
|
|
bit_handler_.digital_phase_locked_loop_output_bit(1);
|
2020-01-12 22:25:21 +00:00
|
|
|
window_was_filled_ = true;
|
|
|
|
post_phase_offset(phase_, offset_);
|
|
|
|
offset_ = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-07-12 02:12:58 +00:00
|
|
|
|
|
|
|
private:
|
2020-01-12 22:45:02 +00:00
|
|
|
BitHandler &bit_handler_;
|
2020-01-12 22:25:21 +00:00
|
|
|
|
|
|
|
void post_phase_offset(Cycles::IntType new_phase, Cycles::IntType new_offset) {
|
|
|
|
// Erase the effect of whatever is currently in this slot.
|
|
|
|
total_divisor_ -= offset_history_[offset_history_pointer_].divisor;
|
|
|
|
total_spacing_ -= offset_history_[offset_history_pointer_].spacing;
|
|
|
|
|
|
|
|
// Fill in the new fields.
|
2020-01-13 03:18:31 +00:00
|
|
|
const auto multiple = std::max((new_offset + (clocks_per_bit_ >> 1)) / clocks_per_bit_, Cycles::IntType(1));
|
2020-01-12 22:25:21 +00:00
|
|
|
offset_history_[offset_history_pointer_].divisor = multiple;
|
|
|
|
offset_history_[offset_history_pointer_].spacing = new_offset;
|
|
|
|
|
|
|
|
// Add in the new values;
|
|
|
|
total_divisor_ += offset_history_[offset_history_pointer_].divisor;
|
|
|
|
total_spacing_ += offset_history_[offset_history_pointer_].spacing;
|
|
|
|
|
|
|
|
// Advance the write slot.
|
|
|
|
offset_history_pointer_ = (offset_history_pointer_ + 1) % offset_history_.size();
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2020-01-13 03:18:31 +00:00
|
|
|
Cycles::IntType td = 0, ts = 0;
|
2020-01-12 22:25:21 +00:00
|
|
|
for(auto offset: offset_history_) {
|
2020-01-13 03:18:31 +00:00
|
|
|
td += offset.divisor;
|
|
|
|
ts += offset.spacing;
|
2020-01-12 22:25:21 +00:00
|
|
|
}
|
2020-01-13 03:18:31 +00:00
|
|
|
assert(ts == total_spacing_);
|
|
|
|
assert(td == total_divisor_);
|
2020-01-12 22:25:21 +00:00
|
|
|
#endif
|
|
|
|
|
2020-01-13 03:18:31 +00:00
|
|
|
// In net: use an unweighted average of the stored offsets to compute current window size,
|
|
|
|
// bucketing them by rounding to the nearest multiple of the base clocks per bit
|
2020-01-29 04:25:21 +00:00
|
|
|
window_length_ = std::max(total_spacing_ / total_divisor_, Cycles::IntType(1));
|
2020-01-13 03:18:31 +00:00
|
|
|
|
2020-01-12 22:25:21 +00:00
|
|
|
// Also apply a difference to phase, use a simple spring mechanism as a lowpass filter.
|
|
|
|
const auto error = new_phase - (window_length_ >> 1);
|
|
|
|
phase_ -= (error + 1) >> 1;
|
|
|
|
}
|
2016-07-13 00:23:56 +00:00
|
|
|
|
2020-01-12 22:25:21 +00:00
|
|
|
struct LoggedOffset {
|
|
|
|
Cycles::IntType divisor = 1, spacing = 1;
|
|
|
|
};
|
|
|
|
std::array<LoggedOffset, length_of_history> offset_history_;
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t offset_history_pointer_ = 0;
|
2020-01-12 22:25:21 +00:00
|
|
|
|
|
|
|
Cycles::IntType total_spacing_ = length_of_history;
|
|
|
|
Cycles::IntType total_divisor_ = length_of_history;
|
2017-07-16 20:49:04 +00:00
|
|
|
|
2019-10-30 02:36:29 +00:00
|
|
|
Cycles::IntType phase_ = 0;
|
|
|
|
Cycles::IntType window_length_ = 0;
|
2020-01-12 22:25:21 +00:00
|
|
|
|
|
|
|
Cycles::IntType offset_ = 0;
|
2017-10-18 02:13:37 +00:00
|
|
|
bool window_was_filled_ = false;
|
2016-07-13 00:23:56 +00:00
|
|
|
|
2017-10-18 02:13:37 +00:00
|
|
|
int clocks_per_bit_ = 0;
|
2016-07-12 02:12:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DigitalPhaseLockedLoop_hpp */
|