2016-07-12 02:12:58 +00:00
|
|
|
//
|
|
|
|
// DigitalPhaseLockedLoop.cpp
|
|
|
|
// 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
|
|
|
//
|
|
|
|
|
|
|
|
#include "DigitalPhaseLockedLoop.hpp"
|
2016-07-27 20:24:24 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
2016-07-13 00:23:56 +00:00
|
|
|
|
|
|
|
using namespace Storage;
|
|
|
|
|
2017-11-11 20:28:40 +00:00
|
|
|
DigitalPhaseLockedLoop::DigitalPhaseLockedLoop(int clocks_per_bit, std::size_t length_of_history) :
|
2017-07-16 20:49:04 +00:00
|
|
|
offset_history_(length_of_history, 0),
|
2017-10-18 02:13:37 +00:00
|
|
|
window_length_(clocks_per_bit),
|
|
|
|
clocks_per_bit_(clocks_per_bit) {}
|
2016-07-13 00:23:56 +00:00
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void DigitalPhaseLockedLoop::run_for(const Cycles cycles) {
|
2019-10-30 02:36:29 +00:00
|
|
|
offset_ += cycles.as_integral();
|
|
|
|
phase_ += cycles.as_integral();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(phase_ >= window_length_) {
|
2019-10-30 02:36:29 +00:00
|
|
|
auto windows_crossed = phase_ / window_length_;
|
2016-08-03 01:28:50 +00:00
|
|
|
|
2016-07-28 18:35:39 +00:00
|
|
|
// check whether this triggers any 0s, if anybody cares
|
2017-03-26 18:34:47 +00:00
|
|
|
if(delegate_) {
|
2019-10-30 02:36:29 +00:00
|
|
|
if(window_was_filled_) --windows_crossed;
|
2016-07-28 18:35:39 +00:00
|
|
|
for(int c = 0; c < windows_crossed; c++)
|
2016-12-03 16:59:28 +00:00
|
|
|
delegate_->digital_phase_locked_loop_output_bit(0);
|
2016-07-13 00:23:56 +00:00
|
|
|
}
|
2016-07-28 18:35:39 +00:00
|
|
|
|
2016-12-03 16:59:28 +00:00
|
|
|
window_was_filled_ = false;
|
|
|
|
phase_ %= window_length_;
|
2016-07-13 00:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void DigitalPhaseLockedLoop::add_pulse() {
|
|
|
|
if(!window_was_filled_) {
|
2016-12-03 16:59:28 +00:00
|
|
|
if(delegate_) delegate_->digital_phase_locked_loop_output_bit(1);
|
|
|
|
window_was_filled_ = true;
|
2017-07-16 20:49:04 +00:00
|
|
|
post_phase_offset(phase_, offset_);
|
|
|
|
offset_ = 0;
|
2016-07-14 23:42:01 +00:00
|
|
|
}
|
2016-07-28 15:32:14 +00:00
|
|
|
}
|
2016-07-14 23:42:01 +00:00
|
|
|
|
2019-10-30 02:36:29 +00:00
|
|
|
void DigitalPhaseLockedLoop::post_phase_offset(Cycles::IntType new_phase, Cycles::IntType new_offset) {
|
2017-07-22 00:58:17 +00:00
|
|
|
offset_history_[offset_history_pointer_] = new_offset;
|
2017-07-16 23:03:50 +00:00
|
|
|
offset_history_pointer_ = (offset_history_pointer_ + 1) % offset_history_.size();
|
2017-07-16 20:49:04 +00:00
|
|
|
|
2017-07-16 23:03:50 +00:00
|
|
|
// 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
|
2019-10-30 02:36:29 +00:00
|
|
|
Cycles::IntType total_spacing = 0;
|
|
|
|
Cycles::IntType total_divisor = 0;
|
|
|
|
for(auto offset : offset_history_) {
|
|
|
|
auto multiple = (offset + (clocks_per_bit_ >> 1)) / clocks_per_bit_;
|
2017-07-16 20:49:04 +00:00
|
|
|
if(!multiple) continue;
|
|
|
|
total_divisor += multiple;
|
|
|
|
total_spacing += offset;
|
|
|
|
}
|
2017-07-16 21:12:12 +00:00
|
|
|
if(total_divisor) {
|
|
|
|
window_length_ = total_spacing / total_divisor;
|
|
|
|
}
|
2017-07-16 20:49:04 +00:00
|
|
|
|
2019-10-30 02:36:29 +00:00
|
|
|
auto error = new_phase - (window_length_ >> 1);
|
2017-07-16 20:49:04 +00:00
|
|
|
|
2016-07-28 15:32:14 +00:00
|
|
|
// use a simple spring mechanism as a lowpass filter for phase
|
2016-12-03 16:59:28 +00:00
|
|
|
phase_ -= (error + 1) >> 1;
|
2016-07-13 00:23:56 +00:00
|
|
|
}
|