wrc_ptp.c 3.67 KB
Newer Older
1 2 3
#include <stdio.h>
#include <inttypes.h>
#include <errno.h>
4
#include <wrc.h>
5 6 7 8 9 10

#include "ptpd.h"
#include "ptpd_netif.h"
#include "timer.h"
#include "softpll_ng.h"
#include "wrc_ptp.h"
11
#include "pps_gen.h"
12
#include "uart.h"
13 14

static RunTimeOpts rtOpts = {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	.ifaceName = {"wr1"},
	.announceInterval = DEFAULT_ANNOUNCE_INTERVAL,
	.syncInterval = DEFAULT_SYNC_INTERVAL,
	.clockQuality.clockAccuracy = DEFAULT_CLOCK_ACCURACY,
	.clockQuality.clockClass = DEFAULT_CLOCK_CLASS,
	.clockQuality.offsetScaledLogVariance = DEFAULT_CLOCK_VARIANCE,
	.priority1 = DEFAULT_PRIORITY1,
	.priority2 = DEFAULT_PRIORITY2,
	.domainNumber = DEFAULT_DOMAIN_NUMBER,
	.currentUtcOffset = DEFAULT_UTC_OFFSET,
	.noResetClock = DEFAULT_NO_RESET_CLOCK,
	.noAdjust = NO_ADJUST,
	.inboundLatency.nanoseconds = DEFAULT_INBOUND_LATENCY,
	.outboundLatency.nanoseconds = DEFAULT_OUTBOUND_LATENCY,
	.s = DEFAULT_DELAY_S,
	.ap = DEFAULT_AP,
	.ai = DEFAULT_AI,
	.max_foreign_records = DEFAULT_MAX_FOREIGN_RECORDS,
33 34

   /**************** White Rabbit *************************/
35 36 37 38 39 40 41
	.autoPortDiscovery = FALSE,	/*if TRUE: automagically discovers how many ports we have (and how many up-s); else takes from .portNumber */
	.portNumber = 1,
	.calPeriod = WR_DEFAULT_CAL_PERIOD,
	.E2E_mode = TRUE,
	.wrStateRetry = WR_DEFAULT_STATE_REPEAT,
	.wrStateTimeout = WR_DEFAULT_STATE_TIMEOUT_MS,
	.phyCalibrationRequired = FALSE,
42
	.disableFallbackIfWRFails = TRUE,
43

44 45 46
	.primarySource = FALSE,
	.wrConfig = WR_S_ONLY,
	.masterOnly = FALSE,
47 48 49 50

   /********************************************************/
};

51 52
static PtpPortDS *ptpPortDS;
static PtpClockDS ptpClockDS;
53
static int ptp_enabled = 0, ptp_mode = WRC_MODE_UNKNOWN;
54 55 56 57

int wrc_ptp_init()
{
	Integer16 ret;
58

59
	netStartup();
60

61 62
	ptpPortDS = ptpdStartup(0, NULL, &ret, &rtOpts, &ptpClockDS);
	initDataClock(&rtOpts, &ptpClockDS);
63

64 65 66 67 68
	//initialize sockets
	if (!netInit(&ptpPortDS->netPath, &rtOpts, ptpPortDS)) {
		PTPD_TRACE(TRACE_WRPC, NULL, "failed to initialize network\n");
		return -1;
	}
69

70 71 72
	ptpPortDS->linkUP = FALSE;
	ptp_enabled = 0;
	return 0;
73 74 75 76 77 78 79 80 81
}

#define LOCK_TIMEOUT_FM (4 * TICS_PER_SECOND)
#define LOCK_TIMEOUT_GM (60 * TICS_PER_SECOND)

int wrc_ptp_set_mode(int mode)
{
	uint32_t start_tics, lock_timeout = 0;

82
	ptp_mode = 0;
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	wrc_ptp_stop();

	switch (mode) {
	case WRC_MODE_GM:
		rtOpts.primarySource = TRUE;
		rtOpts.wrConfig = WR_M_ONLY;
		rtOpts.masterOnly = TRUE;
		spll_init(SPLL_MODE_GRAND_MASTER, 0, 1);
		lock_timeout = LOCK_TIMEOUT_GM;
		break;

	case WRC_MODE_MASTER:
		rtOpts.primarySource = FALSE;
		rtOpts.wrConfig = WR_M_ONLY;
		rtOpts.masterOnly = TRUE;
		spll_init(SPLL_MODE_FREE_RUNNING_MASTER, 0, 1);
		lock_timeout = LOCK_TIMEOUT_FM;
		break;
102 103

	case WRC_MODE_SLAVE:
104 105 106 107 108
		rtOpts.primarySource = FALSE;
		rtOpts.wrConfig = WR_S_ONLY;
		rtOpts.masterOnly = FALSE;
		spll_init(SPLL_MODE_SLAVE, 0, 1);
		break;
109 110
	}

111
	initDataClock(&rtOpts, &ptpClockDS);
112

113
	start_tics = timer_get_tics();
114

115
	mprintf("Locking PLL");
116 117 118

	pps_gen_enable_output(0);

119
	while (!spll_check_lock(0) && lock_timeout) {
120 121
		timer_delay(TICS_PER_SECOND);
		mprintf(".");
122
		if (timer_get_tics() - start_tics > lock_timeout) {
123 124
			mprintf("\nLock timeout.\n");
			return -ETIMEDOUT;
125
		} else if (uart_read_byte() == 27) {
126 127 128 129
			mprintf("\n");
			return -EINTR;
		}
	}
130

131
	if (mode == WRC_MODE_MASTER || mode == WRC_MODE_GM)
132
		pps_gen_enable_output(1);
133

134
	mprintf("\n");
135
	ptp_mode = mode;
136 137 138
	return 0;
}

139 140 141 142 143
int wrc_ptp_get_mode()
{
	return ptp_mode;
}

144 145
int wrc_ptp_start()
{
146 147 148
	ptpPortDS->linkUP = FALSE;
	wr_servo_reset();
	initDataClock(&rtOpts, &ptpClockDS);
149

150 151
	ptp_enabled = 1;
	return 0;
152 153 154 155 156
}

int wrc_ptp_stop()
{
	ptp_enabled = 0;
157
	wr_servo_reset();
158 159 160 161 162
	return 0;
}

int wrc_ptp_update()
{
163 164 165
	if (ptp_enabled) {
		singlePortLoop(&rtOpts, ptpPortDS, 0);
		sharedPortsLoop(ptpPortDS);
166 167 168
	}
	return 0;
}