ActivatePPSSource configures the PHC device to be a PPS timestamp source
(dev DeviceController, pinIndex uint)
| 111 | |
| 112 | // ActivatePPSSource configures the PHC device to be a PPS timestamp source |
| 113 | func ActivatePPSSource(dev DeviceController, pinIndex uint) (*PPSSource, error) { |
| 114 | err := dev.setPinFunc(pinIndex, unix.PTP_PF_PEROUT, defaultTs2PhcChannel) |
| 115 | if err != nil { |
| 116 | log.Printf("Failed to set PPS Perout on pin index %d, channel %d, PHC %s. Error: %s. Continuing bravely on...", |
| 117 | pinIndex, defaultTs2PhcChannel, dev.File().Name(), err) |
| 118 | } |
| 119 | |
| 120 | ts, err := dev.Time() |
| 121 | if err != nil { |
| 122 | return nil, fmt.Errorf("failed (clock_gettime) on %s", dev.File().Name()) |
| 123 | } |
| 124 | |
| 125 | // Initialize the PTPPeroutRequest struct |
| 126 | peroutRequest := &PtpPeroutRequest{} |
| 127 | peroutRequest.Index = uint32(defaultTs2PhcChannel) |
| 128 | peroutRequest.Period = PtpClockTime{Sec: 1, Nsec: 0} |
| 129 | |
| 130 | // Set flags and pulse width |
| 131 | pulsewidth := defaultPulseWidth |
| 132 | |
| 133 | // TODO: skip this block if pulsewidth unset once pulsewidth is configurable |
| 134 | peroutRequest.Flags |= ptpPeroutDutyCycle |
| 135 | peroutRequest.On = PtpClockTime{ |
| 136 | Sec: int64(pulsewidth / 1e9), |
| 137 | Nsec: pulsewidth % 1e9, |
| 138 | } |
| 139 | |
| 140 | // Set phase or start time |
| 141 | // TODO: reintroduce peroutPhase != -1 condition once peroutPhase is configurable |
| 142 | peroutRequest.StartOrPhase = PtpClockTime{ |
| 143 | Sec: ts.Unix() + ppsStartDelay, |
| 144 | Nsec: 0, |
| 145 | } |
| 146 | |
| 147 | err = dev.setPTPPerout(peroutRequest) |
| 148 | |
| 149 | if err != nil { |
| 150 | peroutRequest.Flags &^= ptpPeroutDutyCycle |
| 151 | peroutRequest.On = PtpClockTime{} |
| 152 | err = dev.setPTPPerout(peroutRequest) |
| 153 | |
| 154 | if err != nil { |
| 155 | return nil, fmt.Errorf("error retrying PTP_PEROUT_REQUEST2 with DUTY_CYCLE flag unset for backwards compatibility, %w", err) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return &PPSSource{PHCDevice: dev, state: PPSSet, PPSPinIndex: pinIndex}, nil |
| 160 | } |
| 161 | |
| 162 | // Timestamp returns the timestamp of the last PPS output edge from the given PPS source |
| 163 | func (ppsSource *PPSSource) Timestamp() (time.Time, error) { |
searching dependent graphs…