wrc_main.c 3.58 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 "softpll_ng.h"
16
#include "onewire.h"
17
#include "shell.h"
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
18
#include "lib/ipv4.h"
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
19

20
#include "wrc_ptp.h"
21

22
int wrc_ui_mode = UI_SHELL_MODE;
23

24 25
///////////////////////////////////
//Calibration data (from EEPROM if available)
26 27 28 29
int32_t sfp_alpha = -73622176;  //default values if could not read EEPROM
int32_t sfp_deltaTx = 46407;
int32_t sfp_deltaRx = 167843;
uint32_t cal_phase_transition = 2389;
30

31
void wrc_initialize()
32
{
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
33 34
  int ret, i;
  uint8_t mac_addr[6], ds18_id[8] = {0,0,0,0,0,0,0,0};
35
  char sfp_pn[17];
36 37
  
  sdb_find_devices();
38
  uart_init();
39
  
40
  mprintf("WR Core: starting up...\n");
41
  
42
  timer_init(1);
43
  owInit();
44
  
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
45 46 47
  mac_addr[0] = 0x08;   //
  mac_addr[1] = 0x00;   // CERN OUI
  mac_addr[2] = 0x30;   //  
48
  
49 50
  own_scanbus(ONEWIRE_PORT);
  if (get_persistent_mac(ONEWIRE_PORT, mac_addr) == -1) {
51
    mprintf("Unable to determine MAC address\n");
52 53 54 55 56 57
  	mac_addr[0] = 0x11; //
  	mac_addr[1] = 0x22; // 
  	mac_addr[2] = 0x33; // fallback MAC if get_persistent_mac fails  
  	mac_addr[3] = 0x44; //
  	mac_addr[4] = 0x55; //
  	mac_addr[5] = 0x66; //
58
  }
59

60
  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]);
61

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
62 63
  ep_init(mac_addr);
  ep_enable(1, 1);
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
64

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
65 66
  minic_init();
  pps_gen_init();
67 68
  wrc_ptp_init();
  
69
#if WITH_ETHERBONE
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
70 71
  ipv4_init("wru1");
  arp_init("wru1");
72
#endif
73
}
74

75 76 77 78
#define LINK_WENT_UP 1
#define LINK_WENT_DOWN 2
#define LINK_UP 3
#define LINK_DOWN 4
79 80 81 82

int wrc_check_link()
{
	static int prev_link_state = -1;
83
	int link_state = ep_link_up(NULL);
84 85
	int rv = 0;

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  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;
101
}
102

103
int wrc_extra_debug = 0;
104

105 106 107
void wrc_debug_printf(int subsys, const char *fmt, ...)
{
	va_list ap;
108
	
109
	if(wrc_ui_mode) return;
110
	
111 112 113 114 115 116 117 118 119
	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;
120
static int ptp_enabled = 1;
121 122
int wrc_man_phase = 0;

123
static void ui_update()
124 125
{

126
		if(wrc_ui_mode == UI_GUI_MODE)
127 128 129 130 131
		{
			wrc_mon_gui();
			if(uart_read_byte() == 27)
			{
				shell_init();
132
				wrc_ui_mode = UI_SHELL_MODE;
133 134
			}	
		}
135 136 137 138 139 140 141 142 143
    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;
			}	
    }
144 145 146 147
		else
			shell_interactive();

}
148 149 150

int main(void)
{
151
  wrc_extra_debug = 1;
152
  wrc_ui_mode = UI_SHELL_MODE;
153

154
  wrc_initialize();
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
155
  shell_init();
156

Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
157 158
  wrc_ptp_set_mode(WRC_MODE_SLAVE);
  wrc_ptp_start();
159 160 161

  //try to read and execute init script from EEPROM
  shell_boot_script();
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
162 163 164
  
  for (;;)
  {
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
165
    int l_status = wrc_check_link();
166

Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
167 168
    switch (l_status)
    {
169
#if WITH_ETHERBONE
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
170 171 172
      case LINK_WENT_UP:
        needIP = 1;
        break;
173
#endif
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
174
        
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
175 176
      case LINK_UP:
        update_rx_queues();
177
#if WITH_ETHERBONE
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
178 179
        ipv4_poll();
        arp_poll();
180
#endif
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
181 182 183
        break;

      case LINK_WENT_DOWN:
184 185
        if( wrc_ptp_get_mode() == WRC_MODE_SLAVE )
          spll_init(SPLL_MODE_FREE_RUNNING_MASTER, 0, 1);
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
186
        break;
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
187
    }
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
188

Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
189 190
    ui_update();
    wrc_ptp_update();
191 192
    spll_update_aux_clocks();
  }
193
}