wrc_main.c 3.84 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * This work is part of the White Rabbit project
 *
 * Copyright (C) 2011,2012 CERN (www.cern.ch)
 * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
 * Author: Grzegorz Daniluk <grzegorz.daniluk@cern.ch>
 *
 * Released according to the GNU GPL, version 2 or any later version.
 */
10 11 12
#include <stdio.h>
#include <inttypes.h>

13 14
#include <stdarg.h>

15
#include <wrc.h>
16
#include "syscon.h"
17 18 19 20
#include "uart.h"
#include "endpoint.h"
#include "minic.h"
#include "pps_gen.h"
21
#include "ptpd.h"
22
#include "ptpd_netif.h"
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
23
#include "i2c.h"
24
//#include "eeprom.h"
25
#include "softpll_ng.h"
26
#include "onewire.h"
27 28
#include "pps_gen.h"
#include "sockitowm.h"
29
#include "shell.h"
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
30
#include "lib/ipv4.h"
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
31

32
#include "wrc_ptp.h"
33

34
int wrc_ui_mode = UI_SHELL_MODE;
35

36 37
///////////////////////////////////
//Calibration data (from EEPROM if available)
38
int32_t sfp_alpha = -73622176;	//default values if could not read EEPROM
39 40 41
int32_t sfp_deltaTx = 46407;
int32_t sfp_deltaRx = 167843;
uint32_t cal_phase_transition = 2389;
42

43
void wrc_initialize()
44
{
45
	uint8_t mac_addr[6];
46

47 48
	sdb_find_devices();
	uart_init();
49

50
	mprintf("WR Core: starting up...\n");
51

52 53
	timer_init(1);
	owInit();
54

55 56 57
	mac_addr[0] = 0x08;	//
	mac_addr[1] = 0x00;	// CERN OUI
	mac_addr[2] = 0x30;	//
58

59 60 61 62 63 64 65 66 67 68
	own_scanbus(ONEWIRE_PORT);
	if (get_persistent_mac(ONEWIRE_PORT, mac_addr) == -1) {
		mprintf("Unable to determine MAC address\n");
		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;	//
	}
69

70 71 72
	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]);
73

74 75
	ep_init(mac_addr);
	ep_enable(1, 1);
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
76

77 78 79
	minic_init();
	pps_gen_init();
	wrc_ptp_init();
80

81
#ifdef CONFIG_ETHERBONE
82 83
	ipv4_init("wru1");
	arp_init("wru1");
84
#endif
85
}
86

87 88 89 90
#define LINK_WENT_UP 1
#define LINK_WENT_DOWN 2
#define LINK_UP 3
#define LINK_DOWN 4
91 92 93 94

int wrc_check_link()
{
	static int prev_link_state = -1;
95
	int link_state = ep_link_up(NULL);
96 97
	int rv = 0;

98 99 100 101 102 103 104 105 106 107 108 109 110
	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;
111
}
112

113
int wrc_extra_debug = 0;
114

115 116 117
void wrc_debug_printf(int subsys, const char *fmt, ...)
{
	va_list ap;
118

119 120
	if (wrc_ui_mode)
		return;
121

122
	va_start(ap, fmt);
123

124 125
	if (wrc_extra_debug || (!wrc_extra_debug && (subsys & TRACE_SERVO)))
		vprintf(fmt, ap);
126

127 128 129 130 131
	va_end(ap);
}

int wrc_man_phase = 0;

132
static void ui_update()
133 134
{

135 136 137 138 139
	if (wrc_ui_mode == UI_GUI_MODE) {
		wrc_mon_gui();
		if (uart_read_byte() == 27) {
			shell_init();
			wrc_ui_mode = UI_SHELL_MODE;
140
		}
141 142 143 144 145 146 147 148
	} 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;
		}
	} else
		shell_interactive();
149 150

}
151

152 153 154 155 156 157 158 159 160 161 162
extern uint32_t _endram;
#define ENDRAM_MAGIC 0xbadc0ffe

void check_stack(void)
{
	while (_endram != ENDRAM_MAGIC) {
		mprintf("Stack overflow!\n");
		timer_delay(1000);
	}
}

163 164
int main(void)
{
165 166
	wrc_extra_debug = 1;
	wrc_ui_mode = UI_SHELL_MODE;
167
	_endram = ENDRAM_MAGIC;
168

169 170
	wrc_initialize();
	shell_init();
171

172 173
	wrc_ptp_set_mode(WRC_MODE_SLAVE);
	wrc_ptp_start();
174

175 176
	//try to read and execute init script from EEPROM
	shell_boot_script();
177

178 179
	for (;;) {
		int l_status = wrc_check_link();
180

181
		switch (l_status) {
182
#ifdef CONFIG_ETHERBONE
183 184 185
		case LINK_WENT_UP:
			needIP = 1;
			break;
186
#endif
187

188 189
		case LINK_UP:
			update_rx_queues();
190
#ifdef CONFIG_ETHERBONE
191 192
			ipv4_poll();
			arp_poll();
193
#endif
194 195 196 197 198 199 200 201 202 203 204
			break;

		case LINK_WENT_DOWN:
			if (wrc_ptp_get_mode() == WRC_MODE_SLAVE)
				spll_init(SPLL_MODE_FREE_RUNNING_MASTER, 0, 1);
			break;
		}

		ui_update();
		wrc_ptp_update();
		spll_update_aux_clocks();
205
		check_stack();
206
	}
207
}