phc

package
v0.0.0-...-a92d130 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package phc contains code to work with PTP Hardware Clock (PHC). It allows getting PHC time via different APIs (syscall, ioctl).

It also provides means to calculate offset between sys clock and PHC.

Index

Constants

View Source
const (
	DefaultTs2PhcIndex     = 3
	DefaultTs2PhcSinkIndex = 0

	PPSPollMaxAttempts = 20
)

PPS related constants

View Source
const DefaultMaxClockFreqPPB = 500000.0

DefaultMaxClockFreqPPB value came from linuxptp project (clockadj.c)

View Source
const (
	// ExtendedNumProbes is the number of samples we request for IoctlPtpSysOffsetExtended
	ExtendedNumProbes = 9
)

Variables

This section is empty.

Functions

func IfaceToPHCDevice

func IfaceToPHCDevice(iface string) (string, error)

IfaceToPHCDevice returns path to PHC device associated with given network card iface

func NewPiServo

func NewPiServo(interval time.Duration, firstStepth time.Duration, stepth time.Duration, device FrequencyGetter, maxFreq float64) (*servo.PiServo, error)

NewPiServo returns a servo.PiServo object configure for synchronizing the given device. maxFreq 0 is equivalent to no maxFreq

func OffsetBetweenDevices

func OffsetBetweenDevices(deviceA, deviceB *os.File) (time.Duration, error)

OffsetBetweenDevices returns estimated difference between two PHC devices

func PPSClockSync

func PPSClockSync(pi ServoController, srcTimestamp time.Time, dstEventTimestamp time.Time, dstDevice DeviceController) error

PPSClockSync adjusts the frequency of the destination device based on the PPS from the ppsSource

func Time

func Time(iface string, method TimeMethod) (time.Time, error)

Time returns time we got from network card

Types

type Device

type Device os.File

Device represents a PHC device

func FromFile

func FromFile(file *os.File) *Device

FromFile returns a *Device corresponding to an *os.File

func (*Device) AdjFreq

func (dev *Device) AdjFreq(freqPPB float64) error

AdjFreq adjusts the PHC clock frequency in PPB

func (*Device) ClockID

func (dev *Device) ClockID() int32

ClockID derives the clock ID from the file descriptor number - see clock_gettime(3), FD_TO_CLOCKID macros

func (*Device) Fd

func (dev *Device) Fd() uintptr

Fd returns the underlying file descriptor

func (*Device) File

func (dev *Device) File() *os.File

File returns the underlying *os.File

func (*Device) FreqPPB

func (dev *Device) FreqPPB() (freqPPB float64, err error)

FreqPPB reads PHC device frequency in PPB (parts per billion)

func (*Device) MaxFreqAdjPPB

func (dev *Device) MaxFreqAdjPPB() (maxFreq float64, err error)

MaxFreqAdjPPB reads max value for frequency adjustments (in PPB) from ptp device

func (*Device) Read

func (dev *Device) Read(buffer []byte) (int, error)

func (*Device) ReadSysoffExtended

func (dev *Device) ReadSysoffExtended() (*PTPSysOffsetExtended, error)

ReadSysoffExtended reads the precise time from the PHC along with SYS time to measure the call delay. The nsamples parameter is set to ExtendedNumProbes.

func (*Device) ReadSysoffExtended1

func (dev *Device) ReadSysoffExtended1() (*PTPSysOffsetExtended, error)

ReadSysoffExtended1 reads the precise time from the PHC along with SYS time to measure the call delay. The samples parameter is set to 1.

func (*Device) ReadSysoffPrecise

func (dev *Device) ReadSysoffPrecise() (*PTPSysOffsetPrecise, error)

ReadSysoffPrecise reads the precise time from the PHC along with SYS time to measure the call delay.

func (*Device) SetTime

func (dev *Device) SetTime(t time.Time) error

SetTime sets the time of the PHC clock

func (*Device) Step

func (dev *Device) Step(step time.Duration) error

Step steps the PHC clock by given duration

func (*Device) Time

func (dev *Device) Time() (time.Time, error)

Time returns time from the PTP device using the clock_gettime syscall

type DeviceController

type DeviceController interface {
	Time() (time.Time, error)

	File() *os.File
	AdjFreq(freq float64) error
	Step(offset time.Duration) error
	Read(buf []byte) (int, error)
	Fd() uintptr
	// contains filtered or unexported methods
}

DeviceController defines a subset of functions to interact with a phc device. Enables mocking.

type FrequencyGetter

type FrequencyGetter interface {
	MaxFreqAdjPPB() (float64, error)
	FreqPPB() (float64, error)
}

FrequencyGetter is an interface for getting PHC frequency and max frequency adjustment

type PPSPoller

type PPSPoller interface {
	PollPPSSink() (time.Time, error)
}

PPSPoller represents a device which can be polled for PPS events

type PPSSink

type PPSSink struct {
	InputPin   uint
	Polarity   uint32
	PulseWidth uint32
	Device     DeviceController
	// contains filtered or unexported fields
}

PPSSink represents a device which is a sink of PPS signals

func PPSSinkFromDevice

func PPSSinkFromDevice(targetDevice DeviceController, pinIndex uint) (*PPSSink, error)

PPSSinkFromDevice configures the targetDevice to be a PPS sink and report PPS timestamp events

func (*PPSSink) PollPPSSink

func (ppsSink *PPSSink) PollPPSSink() (time.Time, error)

PollPPSSink polls the sink for the timestamp of the first available PPS event

type PPSSource

type PPSSource struct {
	PHCDevice DeviceController

	PPSPinIndex uint
	// contains filtered or unexported fields
}

PPSSource represents a PPS source

func ActivatePPSSource

func ActivatePPSSource(dev DeviceController, pinIndex uint) (*PPSSource, error)

ActivatePPSSource configures the PHC device to be a PPS timestamp source

func (*PPSSource) Timestamp

func (ppsSource *PPSSource) Timestamp() (time.Time, error)

Timestamp returns the timestamp of the last PPS output edge from the given PPS source

type PPSSourceState

type PPSSourceState int

PPSSourceState represents the state of a PPS source

const (
	// UnknownStatus is the initial state of a PPS source, which means PPS may or may not be configured
	UnknownStatus PPSSourceState = iota
	// PPSSet means the underlying device is activated as a PPS source
	PPSSet
)

type PTPSysOffsetExtended

type PTPSysOffsetExtended unix.PtpSysOffsetExtended

PTPSysOffsetExtended wraps unix.PtpSysOffsetExtended to add methods

func (*PTPSysOffsetExtended) BestSample

func (extended *PTPSysOffsetExtended) BestSample() SysoffResult

BestSample finds a sample which took the least time to be read; the logic is loosely based on sysoff_estimate from ptp4l sysoff.c

func (*PTPSysOffsetExtended) Sub

Sub returns the estimated difference between two PHC SYS_OFFSET_EXTENDED readings

type PTPSysOffsetPrecise

type PTPSysOffsetPrecise unix.PtpSysOffsetPrecise

PTPSysOffsetPrecise wraps unix.PtpSysOffsetPrecise to add methods

func (*PTPSysOffsetPrecise) Sub

Sub returns the estimated difference between two PHC SYS_OFFSET_PRECISE readings

type PtpClockCaps

type PtpClockCaps = unix.PtpClockCaps

PtpClockCaps is an alias

type PtpClockTime

type PtpClockTime = unix.PtpClockTime

PtpClockTime is an alias

type PtpExttsEvent

type PtpExttsEvent = unix.PtpExttsEvent

PtpExttsEvent is an alias

type PtpExttsRequest

type PtpExttsRequest = unix.PtpExttsRequest

PtpExttsRequest is an alias

type PtpPeroutRequest

type PtpPeroutRequest = unix.PtpPeroutRequest

PtpPeroutRequest is an alias

type ServoController

type ServoController interface {
	Sample(offset int64, localTs uint64) (float64, servo.State)
	Unlock()
}

ServoController abstracts away servo

type SysoffResult

type SysoffResult struct {
	Offset  time.Duration
	Delay   time.Duration
	SysTime time.Time
	PHCTime time.Time
}

SysoffResult is a result of PHC time measurement with related data

func SysoffEstimateBasic

func SysoffEstimateBasic(ts1, rt, ts2 time.Time) SysoffResult

SysoffEstimateBasic logic based on calculate_offset from ptp4l phc_ctl.c

func SysoffFromPrecise

func SysoffFromPrecise(precise *PTPSysOffsetPrecise) SysoffResult

SysoffFromPrecise returns SysoffResult from *PTPSysOffsetPrecise . Code based on sysoff_precise from ptp4l sysoff.c

func TimeAndOffset

func TimeAndOffset(iface string, method TimeMethod) (SysoffResult, error)

TimeAndOffset returns time we got from network card + offset

func TimeAndOffsetFromDevice

func TimeAndOffsetFromDevice(device string, method TimeMethod) (SysoffResult, error)

TimeAndOffsetFromDevice returns time we got from phc device + offset

type TimeMethod

type TimeMethod string

TimeMethod is method we use to get time

const (
	MethodSyscallClockGettime    TimeMethod = "syscall_clock_gettime"
	MethodIoctlSysOffsetExtended TimeMethod = "ioctl_PTP_SYS_OFFSET_EXTENDED"
	MethodIoctlSysOffsetPrecise  TimeMethod = "ioctl_PTP_SYS_OFFSET_PRECISE"
)

Methods we support to get time

type Timestamper

type Timestamper interface {
	Timestamp() (time.Time, error)
}

Timestamper represents a device that can return a Timestamp

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL