Skip to content
Snippets Groups Projects
wrc_main.c 3.59 KiB
Newer Older
#include <stdio.h>
#include <inttypes.h>

#include "syscon.h"
#include "uart.h"
#include "endpoint.h"
#include "minic.h"
#include "pps_gen.h"
#include "ptpd.h"
#include "ptpd_netif.h"
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
#include "i2c.h"
#include "softpll_ng.h"
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
#include "lib/ipv4.h"
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed

///////////////////////////////////
//Calibration data (from EEPROM if available)
int32_t sfp_alpha = -73622176;  //default value if could not read EEPROM
int32_t sfp_deltaTx = 46407;  //default value if could not read EEPROM
int32_t sfp_deltaRx = 177093; //default value if could not read EEPROM
uint32_t cal_phase_transition = 2394; //7000;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
  int ret, i;
  uint8_t mac_addr[6], ds18_id[8] = {0,0,0,0,0,0,0,0};
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
  mac_addr[0] = 0x08;   //
  mac_addr[1] = 0x00;   // CERN OUI
  mac_addr[2] = 0x30;   //  
  mac_addr[3] = 0xDE;   // fallback MAC if get_persistent_mac fails
  mac_addr[4] = 0xAD;
  mac_addr[5] = 0x42;
  
  own_scanbus(ONEWIRE_PORT);
  if (get_persistent_mac(ONEWIRE_PORT, mac_addr) == -1) {
    mprintf("Unable to determine MAC address\n");
  }
  TRACE_DEV("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]);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
  ep_init(mac_addr);
  ep_enable(1, 1);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
  minic_init();
  pps_gen_init();
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
  ipv4_init("wru1");
  arp_init("wru1");
#define LINK_WENT_UP 1
#define LINK_WENT_DOWN 2
#define LINK_UP 3
#define LINK_DOWN 4

int wrc_check_link()
{
	static int prev_link_state = -1;
	int link_state = ep_link_up(NULL);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
  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;
int wrc_extra_debug = 0;
void wrc_debug_printf(int subsys, const char *fmt, ...)
{
	va_list ap;
	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;
		{
			wrc_mon_gui();
			if(uart_read_byte() == 27)
			{
				shell_init();
    else if(wrc_ui_mode == UI_STAT_MODE)
    {
      wrc_log_stats(0);
			if(uart_read_byte() == 27)
			{
				shell_init();
				wrc_ui_mode = UI_SHELL_MODE;
			}	
    }
  wrc_extra_debug = 1;
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
  shell_init();
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
  wrc_ptp_set_mode(WRC_MODE_SLAVE);
  wrc_ptp_start();

  //try to read and execute init script from EEPROM
  shell_boot_script();
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
  
  for (;;)
  {
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
    int l_status = wrc_check_link();
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
    switch (l_status)
    {
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
      case LINK_WENT_UP:
        needIP = 1;
        break;
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
      case LINK_UP:
        update_rx_queues();
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
        ipv4_poll();
        arp_poll();
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
        break;

      case LINK_WENT_DOWN:
        if( wrc_ptp_get_mode() == WRC_MODE_SLAVE )
          spll_init(SPLL_MODE_FREE_RUNNING_MASTER, 0, 1);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
        break;
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
    }
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
    ui_update();
    wrc_ptp_update();