wrc_main.c 9.59 KB
Newer Older
1 2 3
#include <stdio.h>
#include <inttypes.h>

4 5
#include <stdarg.h>

6
#include "syscon.h"
7 8 9 10
#include "uart.h"
#include "endpoint.h"
#include "minic.h"
#include "pps_gen.h"
11
#include "ptpd.h"
12
#include "ptpd_netif.h"
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
13
#include "i2c.h"
14
//#include "eeprom.h"
15
#include "onewire.h"
16
#include "softpll_ng.h"
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
17 18 19 20



RunTimeOpts rtOpts = {
21
    .ifaceName = { "wr0" },
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
   .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,

   /**************** White Rabbit *************************/
41 42 43 44 45 46
   .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,
47 48 49
   .phyCalibrationRequired = FALSE,
	.disableFallbackIfWRFails = TRUE,
         
50 51 52 53 54 55 56 57 58 59 60
/*SLAVE only or MASTER only*/
#ifdef WRPC_SLAVE
   .primarySource = FALSE,
   .wrConfig      = WR_S_ONLY,
   .masterOnly    = FALSE,
#endif
#ifdef WRPC_MASTER
   .primarySource = TRUE,
   .wrConfig      = WR_M_ONLY,
   .masterOnly    = TRUE,
#endif
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
61 62
   /********************************************************/
};
63 64
static   PtpPortDS *ptpPortDS;
static   PtpClockDS ptpClockDS;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
65

66

67 68
///////////////////////////////////
//Calibration data (from EEPROM if available)
69
int32_t sfp_alpha = -73622176;  //default value if could not read EEPROM
70 71
int32_t sfp_deltaTx = 0;
int32_t sfp_deltaRx = 0;
72 73
uint32_t cal_phase_transition = 7000;
///////////////////////////////////
74

75
#include "ptp-noposix/libptpnetif/ptpd_netif.h"
76 77


78 79
//void test_transition()
//{
80 81 82 83
//    wr_socket_t *sock;
//    wr_sockaddr_t bindaddr;
//    const mac_addr_t PTP_MULTICAST_ADDR = {0x01, 0x1b, 0x19, 0, 0, 0};
//    
84 85 86
//    int phase = 0;
//    
//   softpll_enable();
87
//   
88 89
//   while(!softpll_check_lock()) timer_delay(1000);
//
90 91 92 93 94 95 96 97
//
//    bindaddr.family = PTPD_SOCK_RAW_ETHERNET;// socket type
//    bindaddr.ethertype = 0x88f7;         // PTPv2
//    memcpy(bindaddr.mac, PTP_MULTICAST_ADDR, sizeof(mac_addr_t));
//     
//    // Create one socket for event and general messages (WR lower level layer requires that
//    sock = ptpd_netif_create_socket(PTPD_SOCK_RAW_ETHERNET, 0, &bindaddr);
//         
98 99
//    for(;;)
//    {	struct hw_timestamp hwts;
100
//        wr_timestamp_t t_rx;
101
//        uint8_t buf_hdr[18], buf[128];
102 103 104
//       	update_rx_queues();
//	
//	    if(ptpd_netif_recvfrom(sock, &bindaddr, buf, 128, &t_rx) > 0)
105
//	
106
////	if(minic_rx_frame(buf_hdr, buf, 128, &hwts) > 0)
107
//	{
108
//	    printf("phase %d ahead %d TS %d:%d\n", phase,0,t_rx.nsec, t_rx.phase);
109 110 111 112
//	    phase+=100;
//	    softpll_set_phase(phase);
//	    timer_delay(10);
//	}
113
////    	mprintf(".");
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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
//    }
//}

//int last_btn0;
//int button_pressed()
//{
//	int p;
//	int btn0 = gpio_in(GPIO_BTN1);
//	p=!btn0 && last_btn0;
//	last_btn0 = btn0;
//	return p;
//}

//void rx_test()
//{
//  uint8_t mac[]= {0x1, 0x1b, 0x19, 0,0,0};
//  uint16_t buf[100];
//  wr_socket_t *sock;
//  wr_sockaddr_t addr;
//
//  memcpy(addr.mac, mac, 6);
//  addr.ethertype = 0x88f7;
//
//  ptpd_netif_init();
//  sock = ptpd_netif_create_socket(PTPD_SOCK_RAW_ETHERNET, 0, &addr);
//  mprintf("Sock @ %x\n", sock);
//  wrc_extra_debug = 0;
//  for(;;)
//  {
//   	update_rx_queues();
//  	int n = ptpd_netif_recvfrom(sock, &addr, buf, sizeof(buf), NULL);
//  	if(n>0) 
//  	{
//  		uint16_t sum = 0 ,i, rx;
//  		rx=n;
//  		n= buf[0];
//	  	for(i=1;i<n;i++) sum+=buf[i]; 
//	  	mprintf("%x %x\n", sum, buf[n]);
//	  	if(sum != buf[n])
//	  	{
//	  		mprintf("****************** ERR: rxed %d size %d\n", rx, n);
//	  	}
//	  	
//    }
//    timer_delay(10);
//    mprintf(".");
//  }
//}
162

Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
163
#if 1
164
int get_sfp_id(char *sfp_pn)
165 166 167 168
{
  uint8_t data, sum=0;
  uint8_t i;

169
//  wait until SFP signalizes its presence
170 171
  while( gpio_in(GPIO_SFP_DET) );

172
//  mprintf("wr_core: SFP present\n");
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  mi2c_init(WRPC_SFP_I2C);

  mi2c_start(WRPC_SFP_I2C);
  mi2c_put_byte(WRPC_SFP_I2C, 0xA0);
  mi2c_put_byte(WRPC_SFP_I2C, 0x00);
  mi2c_repeat_start(WRPC_SFP_I2C); 
  mi2c_put_byte(WRPC_SFP_I2C, 0xA1);
  mi2c_get_byte(WRPC_SFP_I2C, &data, 1);
  mi2c_stop(WRPC_SFP_I2C);

  sum = data;

  mi2c_start(WRPC_SFP_I2C);
  mi2c_put_byte(WRPC_SFP_I2C, 0xA1);
  for(i=1; i<63; ++i)
  {
    mi2c_get_byte(WRPC_SFP_I2C, &data, 0);
    sum = (uint8_t) ((uint16_t)sum + data) & 0xff;
    if(i>=40 && i<=55)    //Part Number
      sfp_pn[i-40] = data;
  }
  mi2c_get_byte(WRPC_SFP_I2C, &data, 1);  //final word, checksum
  mi2c_stop(WRPC_SFP_I2C);

197 198 199 200
  if(sum == data) 
      return 0;
  
  return -1;
201 202
}
#endif
203

204
void wrc_initialize()
205
{
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
206 207
  int ret, i;
  uint8_t mac_addr[6], ds18_id[8] = {0,0,0,0,0,0,0,0};
208
  char sfp_pn[17];
209
	
210
	uart_init();
211
	timer_init(1);
212

213 214
	uart_write_string(__FILE__ " is up (compiled on "
			  __DATE__ " " __TIME__ ")\n");
215
    
216
	mprintf("wr_core: starting up (press G to launch the GUI and D for extra debug messages)....\n");
217
  //SFP
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
218
#if 1
219
  if( get_sfp_id(sfp_pn) >= 0)
220
  {
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
221 222 223 224
    //mprintf("Found SFP transceiver ID: ");
    for(i=0;i<16;i++)
      mprintf("%c", sfp_pn[i]);
    mprintf("\n");
225 226 227 228 229 230
    
    if( !access_eeprom(sfp_pn, &sfp_alpha, &sfp_deltaTx, &sfp_deltaRx) )
    {
      mprintf("SFP: alpha=%d, deltaTx=%d, deltaRx=%d\n", sfp_alpha, sfp_deltaTx, sfp_deltaRx);
    }
    
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
231
  }
232 233
#endif

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
234
#if 1
235 236 237
  //Generate MAC address
  ow_init();
  if( ds18x_read_serial(ds18_id) == 0 ) 
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
238 239 240
    TRACE_DEV("Found DS18xx sensor: %x:%x:%x:%x:%x:%x:%x:%x\n",
        ds18_id[7], ds18_id[6], ds18_id[5], ds18_id[4], 
        ds18_id[3], ds18_id[2], ds18_id[1], ds18_id[0]);
241
  else
242
    TRACE_DEV("DS18B20 not found\n");
243
#endif
244

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
245 246 247 248 249 250
  mac_addr[0] = 0x08;   //
  mac_addr[1] = 0x00;   // CERN OUI
  mac_addr[2] = 0x30;   //  
  mac_addr[3] = ds18_id[3]; // www.maxim-ic.com
  mac_addr[4] = ds18_id[2]; // APPLICATION NOTE 186  
  mac_addr[5] = ds18_id[1]; // Creating Global Identifiers Using 1-Wire® Devices
251

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
252 253 254
  TRACE_DEV("wr_core: local MAC address: %x:%x:%x:%x:%x:%x\n", mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5]);
  ep_init(mac_addr);
  ep_enable(1, 1);
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
255

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
256 257
  minic_init();
  pps_gen_init();
258

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
259
  netStartup();
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
260

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
261 262
  ptpPortDS = ptpdStartup(0, NULL, &ret, &rtOpts, &ptpClockDS);
  initDataClock(&rtOpts, &ptpClockDS);
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
263

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
264
  displayConfigINFO(&rtOpts);
265 266 267 268

  //initialize sockets
  if(!netInit(&ptpPortDS->netPath, &rtOpts, ptpPortDS))
  {
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
269 270
    PTPD_TRACE(TRACE_WRPC, NULL,"failed to initialize network\n");
    return;
271 272
  }
  ptpPortDS->linkUP = FALSE;
273
}
274

275 276 277 278
#define LINK_WENT_UP 1
#define LINK_WENT_DOWN 2
#define LINK_UP 3
#define LINK_DOWN 4
279 280 281 282

int wrc_check_link()
{
	static int prev_link_state = -1;
283
	int link_state = ep_link_up(NULL);
284 285
	int rv = 0;

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  if(!prev_link_state && link_state)
  {
    TRACE_DEV("Link up.\n");
    gpio_out(GPIO_LED_LINK, 1);
    rv = LINK_WENT_UP;
  } 
  else if(prev_link_state && !link_state)
  {
    TRACE_DEV("Link down.\n");
    gpio_out(GPIO_LED_LINK, 0);
    rv = LINK_WENT_DOWN;
  }  else rv = (link_state ? LINK_UP : LINK_DOWN);
  prev_link_state = link_state;

  return rv;
301
}
302

303 304
int wrc_extra_debug = 0;
int wrc_gui_mode = 1;
305

306 307 308
void wrc_debug_printf(int subsys, const char *fmt, ...)
{
	va_list ap;
309
	
310
	if(wrc_gui_mode) return;
311
	
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	va_start(ap, fmt);
	
	if(wrc_extra_debug || (!wrc_extra_debug && (subsys & TRACE_SERVO)))
	 	vprintf(fmt, ap);
	
	va_end(ap);
}

static int wrc_enable_tracking = 1;
int wrc_man_phase = 0;

void wrc_handle_input()
{
 	if(uart_poll())
 	{
 		int x = uart_read_byte();
 		
 		switch(x)
 		{
 		 	case 'g':
 		 		wrc_gui_mode = 1 - wrc_gui_mode;
 		 		if(!wrc_gui_mode)
 		 		{
 		 			m_term_clear();
 		 			wrc_debug_printf(0, "Exiting GUI mode\n");
 		 		}
 		 	break;
 		 	
 		 	case 'd':
 		 		wrc_extra_debug =  1 - wrc_extra_debug;

Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
343
//				wrc_debug_printf(0,"Verbose debug %s.\n", wrc_extra_debug ? "enabled" : "disabled");
344 345 346 347 348 349 350
				break;
 		 		
 		 	
 		 	case 't':
 		 		wrc_enable_tracking = 1 - wrc_enable_tracking;
			 	wr_servo_enable_tracking(wrc_enable_tracking);

Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
351
//				wrc_debug_printf(0,"Phase tracking %s.\n", wrc_enable_tracking ? "enabled" : "disabled");
352 353 354 355 356
				break;

			case '+':
			case '-':
				wrc_man_phase += (x=='+' ? 100 : -100);
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
357
//				wrc_debug_printf(0,"Manual phase adjust: %d\n", wrc_man_phase);
358 359 360 361 362 363 364 365 366 367
				wr_servo_man_adjust_phase(wrc_man_phase);
				break;
 		}
 	}
}

extern volatile int irq_cnt;

int main(void)
{
368 369 370
  wrc_extra_debug = 1;
  wrc_gui_mode = 0;

371 372 373
  wrc_initialize();


374
#if WRPC_MASTER
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
375
  spll_init(SPLL_MODE_FREE_RUNNING_MASTER, 0, 1);
376 377 378 379
  //spll_init(SPLL_MODE_GRAND_MASTER, 0, 1);
#else
  spll_init(SPLL_MODE_SLAVE, 0, 1);
#endif
380

381 382 383 384 385
	for(;;)
	{
		wrc_handle_input();
		if(wrc_gui_mode)
			wrc_mon_gui();
386 387
		else
			wrc_log_stats();
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
388

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
389
    int l_status = wrc_check_link();
390

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
391 392 393 394 395 396 397
    switch (l_status)
    {
      case LINK_UP:
        update_rx_queues();
        break;

      case LINK_WENT_DOWN:
398
#if WRPC_MASTER
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
399
        spll_init(SPLL_MODE_FREE_RUNNING_MASTER, 0, 1);
400 401 402 403
        //spll_init(SPLL_MODE_GRAND_MASTER, 0, 1);
#else
        spll_init(SPLL_MODE_SLAVE, 0, 1);
#endif
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
404 405 406
        break;
    }        

407
    singlePortLoop(&rtOpts, ptpPortDS, 0);// RunTimeOpts *rtOpts, PtpPortDS *ptpPortDS, int portIndex)
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
408
    sharedPortsLoop(ptpPortDS);
409 410 411 412

    spll_update_aux_clocks();
    // 		delay(1000);
  }
413 414
}