ale-leehrtz has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-e1d/+/44230?usp=email ) Change subject: add SD-RI-PLL PID controller ...................................................................... add SD-RI-PLL PID controller Add a small user-space PID controller for the SD-RI-PLL (software- defined, reference-independent PLL) that recovers the E1 clock of an E1-over-IP line in software: it takes the IP->E1 FIFO fill level as a proxy for the phase error between the remote E1 clock and the local DAHDI coretimer, and computes a rate correction ("fudge") for the coretimer, which then runs at (1 + fudge / 2^32). The controller has anti-windup on the integral and clamps its output to roughly +/-465 ppm. The default gains and the FIFO setpoint of 248 frames are the ones validated on a live OCTOI link in a community network for the ISPCS 2026 paper, see the related ticket. This patch only adds the controller; it is hooked up to the DAHDI trunkdev driver in a follow-up patch. Related: OS#7116 Change-Id: I246c3680f310f1f7326d348c8a803bbe86359059 --- M src/Makefile.am A src/pll_pid.c A src/pll_pid.h 3 files changed, 171 insertions(+), 2 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-e1d refs/changes/30/44230/1 diff --git a/src/Makefile.am b/src/Makefile.am index 30f0c79..94f9644 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -25,6 +25,7 @@ e1d.h \ ice1usb_proto.h \ log.h \ + pll_pid.h \ usb.h \ e1gen/crc4itu.h \ e1gen/osmo_e1f.h \ @@ -78,6 +79,6 @@ $(NULL) if ENABLE_DAHDI_TRUNKDEV -osmo_e1d_SOURCES += dahdi_trunkdev.c -osmo_e1gen_SOURCES += dahdi_trunkdev.c +osmo_e1d_SOURCES += dahdi_trunkdev.c pll_pid.c +osmo_e1gen_SOURCES += dahdi_trunkdev.c pll_pid.c endif diff --git a/src/pll_pid.c b/src/pll_pid.c new file mode 100644 index 0000000..80b576e --- /dev/null +++ b/src/pll_pid.c @@ -0,0 +1,100 @@ +/* + * pll_pid.c + * + * Software-defined, reference-independent PLL (SD-RI-PLL) for E1 timing: + * user-space PID controller. See pll_pid.h for the overall design. + * + * (C) 2025-2026 by C. Alejandra Carreon-Jimenez <alejandra.carreon@ale-leehrtz.org> + * + * All Rights Reserved + * + * SPDX-License-Identifier: GPL-2.0+ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "pll_pid.h" + +void pll_pid_init(struct pll_pid_controller *pid, float kp, float ki, float kd, float target_fifo) +{ + pid->kp = kp; + pid->ki = ki; + pid->kd = kd; + pid->target = target_fifo; + pid->integral = 0.0f; + pid->last_error = 0.0f; + pid->fudge = 0; + pid->tau_f = 0.0f; + pid->err_f = 0.0f; + pid->have_err_f = 0; +} + +/* Run one control step. 'current_fifo' is the measured IP->E1 FIFO + * occupancy; the returned value is the signed fudge to hand to the kernel + * (see the (1 + fudge/2^32) rate correction described in pll_pid.h). */ +int32_t pll_pid_update(struct pll_pid_controller *pid, int32_t current_fifo) +{ + float error = pid->target - (float)current_fifo; + float derivative; + float adjustment; + + /* smoothed error for P and D; identical to 'error' when tau_f == 0 */ + if (pid->tau_f > 0.0f && pid->have_err_f) + pid->err_f += (error - pid->err_f) / (pid->tau_f + 1.0f); + else + pid->err_f = error; + pid->have_err_f = 1; + derivative = pid->err_f - pid->last_error; + + /* Integrate with anti-windup: bound the accumulator so the integral + * term alone cannot exceed the output clamp. Without this the + * accumulator can run away while the output is saturated. */ + pid->integral += error; + if (pid->ki > 0.0f) { + float integral_limit = (float)PLL_PID_FUDGE_LIMIT / pid->ki; + + if (pid->integral > integral_limit) + pid->integral = integral_limit; + else if (pid->integral < -integral_limit) + pid->integral = -integral_limit; + } + + pid->last_error = pid->err_f; + + adjustment = pid->kp * pid->err_f + + pid->ki * pid->integral + + pid->kd * derivative; + + /* The coretimer applies (1 + fudge/2^32); a positive FIFO error means + * the E1 side is draining too fast, so we slow the clock (negative + * fudge). */ + adjustment = -adjustment; + + if (adjustment > (float)PLL_PID_FUDGE_LIMIT) + adjustment = (float)PLL_PID_FUDGE_LIMIT; + else if (adjustment < -(float)PLL_PID_FUDGE_LIMIT) + adjustment = -(float)PLL_PID_FUDGE_LIMIT; + + pid->fudge = (int32_t)adjustment; + return pid->fudge; +} + +void pll_pid_set_gains(struct pll_pid_controller *pid, float kp, float ki, float tau_f) +{ + /* current output, before the sign flip applied in pll_pid_update() */ + float adjustment = -(float)pid->fudge; + + pid->kp = kp; + pid->ki = ki; + pid->tau_f = tau_f; + if (ki > 0.0f) + pid->integral = (adjustment - kp * pid->err_f) / ki; +} diff --git a/src/pll_pid.h b/src/pll_pid.h new file mode 100644 index 0000000..7769049 --- /dev/null +++ b/src/pll_pid.h @@ -0,0 +1,68 @@ +/* + * pll_pid.h + * + * Software-defined, reference-independent PLL (SD-RI-PLL) for E1 timing. + * + * User-space PID controller that steers the DAHDI coretimer clock so the + * IP->E1 FIFO occupancy tracks a setpoint. The FIFO level is used as a + * proxy for the accumulated phase error between the incoming (network) and + * outgoing (E1) rates; the controller output ("fudge") is pushed to the + * kernel via the DAHDI_SW_CLOCK_SET ioctl, where the coretimer applies a + * multiplicative rate correction of (1 + fudge / 2^32). + * + * (C) 2025-2026 by C. Alejandra Carreon-Jimenez <alejandra.carreon@ale-leehrtz.org> + * + * All Rights Reserved + * + * SPDX-License-Identifier: GPL-2.0+ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#pragma once + +#include <stdint.h> + +/* Default control parameters, as validated on a live OCTOI link (and used in + * the ISPCS 2026 paper, Table II). The derivative gain is small because the + * FIFO signal carries network and scheduler jitter. All of them can be + * overridden via the VTY ('pll pid-gains', 'pll target-fifo'). */ +#define PLL_PID_DEFAULT_KP 1.0f +#define PLL_PID_DEFAULT_KI 0.05f +#define PLL_PID_DEFAULT_KD 0.1f +#define PLL_PID_DEFAULT_TARGET_FIFO 248.0f + +/* Output clamp on the fudge value. fudge/2^32 is a fractional rate, so + * this bounds the correction to roughly +/-465 ppm: well outside the pull + * range of a compliant E1 clock, while still preventing controller runaway. */ +#define PLL_PID_FUDGE_LIMIT 2000000 + +struct pll_pid_controller { + float kp; + float ki; + float kd; + float integral; + float last_error; + float target; + int32_t fudge; + /* First-order smoothing of the error for the P and D terms (paper, + * Table II). tau_f = 0 disables it: the controller then behaves + * exactly as the plain PID of Eq. (2). */ + float tau_f; + float err_f; + int have_err_f; +}; + +void pll_pid_init(struct pll_pid_controller *pid, float kp, float ki, float kd, float target_fifo); +int32_t pll_pid_update(struct pll_pid_controller *pid, int32_t current_fifo); +/* Change gains without a jump in the output: the integral is rescaled so the + * next update produces the same fudge for the same error (bumpless transfer). */ +void pll_pid_set_gains(struct pll_pid_controller *pid, float kp, float ki, float tau_f); -- To view, visit https://gerrit.osmocom.org/c/osmo-e1d/+/44230?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: newchange Gerrit-Project: osmo-e1d Gerrit-Branch: master Gerrit-Change-Id: I246c3680f310f1f7326d348c8a803bbe86359059 Gerrit-Change-Number: 44230 Gerrit-PatchSet: 1 Gerrit-Owner: ale-leehrtz <alejandra.carreon@ale-leehrtz.org>