measure_t24p.c 4.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <stdio.h>
#include <inttypes.h>

#include <stdarg.h>
#define INET_ADDRSTRLEN 16

#include "syscon.h"
#include "uart.h"
#include "endpoint.h"
#include "hw/endpoint_mdio.h"
#include "minic.h"
#include "pps_gen.h"
#include "softpll_ng.h"
14 15

#undef PACKED
16 17 18 19
#include "ptpd_netif.h"

#include "ptpd.h"

20
static int get_bitslide(int ep)
21 22 23 24 25 26 27 28 29 30 31
{
	return (pcs_read(16) >> 4) & 0x1f;
}

struct meas_entry {
  	int delta_ns;
  	int phase;
  	int phase_sync;
  	int ahead;
};

32
static void purge_socket(wr_socket_t *sock)
33 34 35 36 37 38 39
{
	wr_sockaddr_t from;
	char buf[128];
	update_rx_queues();
	while(ptpd_netif_recvfrom(sock, &from, buf, 128, NULL) > 0) update_rx_queues();
}

40
static int meas_phase_range(wr_socket_t *sock, int phase_min, int phase_max, int phase_step, struct meas_entry *results)
41 42 43 44 45 46 47
{
	char buf[128];
	wr_timestamp_t ts_tx, ts_rx, ts_sync;
	wr_sockaddr_t from;
	MsgHeader mhdr;
	int setpoint = phase_min, i = 0, phase;
	spll_set_phase_shift(SPLL_ALL_CHANNELS, phase_min);
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	while(spll_shifter_busy(0));
	
	purge_socket(sock);

	i=0;
	while(setpoint <= phase_max)
	{
		ptpd_netif_get_dmtd_phase(sock, &phase);

		update_rx_queues();
		int n = ptpd_netif_recvfrom(sock, &from, buf, 128, &ts_rx);

		
		if(n>0)
		{			
			msgUnpackHeader(buf, &mhdr);
			if(mhdr.messageType == 0)
				ts_sync = ts_rx;
		    else if(mhdr.messageType == 8)
		    {
				MsgFollowUp fup;
				msgUnpackFollowUp(buf, &fup);
				
            mprintf("Shift: %d/%dps [step %dps]        \r", setpoint,phase_max,phase_step);
            results[i].phase = phase;
            results[i].phase_sync = ts_sync.phase;
            results[i].ahead = ts_sync.raw_ahead;
            results[i].delta_ns = fup.preciseOriginTimestamp.nanosecondsField - ts_sync.nsec;
77
            results[i].delta_ns += (fup.preciseOriginTimestamp.secondsField.lsb - ts_sync.sec) * 1000000000;
78 79 80 81 82 83 84 85 86 87 88 89 90 91
            
			setpoint += phase_step;
			spll_set_phase_shift(0, setpoint);
			while(spll_shifter_busy(0));
			purge_socket(sock);

            i++;
			}
		}
	}
	mprintf("\n");
	return i;
}

92
static int find_transition(struct meas_entry *results, int n, int positive)
93 94 95 96 97 98 99 100 101 102 103
{
	int i;
	for(i=0;i<n;i++)
	{
	 	if ((results[i].ahead ^ results[(i+1)%n].ahead) && (results[(i+1)%n].ahead == positive))
	 		return i;
	}
}

extern void ptpd_netif_set_phase_transition(wr_socket_t *sock, int phase);

104
int measure_t24p(int *value)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
{
	wr_socket_t *sock;
	wr_sockaddr_t sock_addr;
	int i, nr;
	struct meas_entry results[128];
	
	spll_enable_ptracker(0, 1);

	sock_addr.family = PTPD_SOCK_RAW_ETHERNET; // socket type
	sock_addr.ethertype = 0x88f7;
	sock_addr.mac[0] = 0x1;
	sock_addr.mac[1] = 0x1b;
	sock_addr.mac[2] = 0x19;
	sock_addr.mac[3] = 0;
	sock_addr.mac[4] = 0;
	sock_addr.mac[5] = 0;

122
	mprintf("LNK: ");
123 124 125 126 127
	while( !ep_link_up(NULL) )
	{
		mprintf(".");
		timer_delay(1000);
    }
128
    mprintf("\nPLL: ");
129 130 131 132

	spll_init(SPLL_MODE_SLAVE, 0, 1);

	while(!spll_check_lock(0));
133
	mprintf("\n");
134 135

	if(ptpd_netif_init() != 0)
136
	 	return -1;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    
	sock = ptpd_netif_create_socket(PTPD_SOCK_RAW_ETHERNET, 0, &sock_addr);
	nr=meas_phase_range(sock, 0, 8000, 1000, results);
	int tplus = find_transition(results, nr, 1);
	int tminus = find_transition(results, nr, 0);

	int approx_plus = results[tplus].phase;
	int approx_minus = results[tminus].phase;
	

	nr=meas_phase_range(sock, approx_plus-1000, approx_plus+1000, 100, results);
	tplus = find_transition(results, nr, 1);
    int phase_plus = results[tplus].phase;
    
	nr=meas_phase_range(sock, approx_minus-1000, approx_minus+1000, 100, results);
	tminus = find_transition(results, nr, 0);
    int phase_minus = results[tminus].phase;
    
	mprintf("Transitions found: positive @ %d ps, negative @ %d ps.\n", phase_plus, phase_minus);
	int ttrans = phase_minus-1000;
	if(ttrans < 0) ttrans+=8000;
	
	mprintf("(t2/t4)_phase_transition = %dps\n", ttrans);
	ptpd_netif_set_phase_transition(sock, ttrans);

	mprintf("Verification... \n");
	nr =meas_phase_range(sock, 0, 16000, 500, results);
	
	for(i=0;i<nr;i++) mprintf("phase_dmtd: %d delta_ns: %d, phase_sync: %d\n", results[i].phase, results[i].delta_ns, results[i].phase_sync);
	

168
	if(value) *value = ttrans;
169

170
	return 0;	
171 172
}