wrc_main.c 4.46 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 <w1.h>
17
#include "syscon.h"
18 19 20 21
#include "uart.h"
#include "endpoint.h"
#include "minic.h"
#include "pps_gen.h"
22
#include "ptpd_netif.h"
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
23
#include "i2c.h"
24
#include "storage.h"
25
#include "softpll_ng.h"
26
#include "onewire.h"
27
#include "pps_gen.h"
28
#include "shell.h"
Wesley W. Terpstra's avatar
Wesley W. Terpstra committed
29
#include "lib/ipv4.h"
30
#include "rxts_calibrator.h"
31
#include "flash.h"
Grzegorz Daniluk's avatar
Grzegorz Daniluk committed
32

33
#include "wrc_ptp.h"
34
#include "system_checks.h"
35

36
int wrc_ui_mode = UI_SHELL_MODE;
37
int wrc_ui_refperiod = TICS_PER_SECOND; /* 1 sec */
38
int wrc_phase_tracking = 1;
39

40 41
///////////////////////////////////
//Calibration data (from EEPROM if available)
42
int32_t sfp_alpha = 73622176;	//default values if could not read EEPROM
43 44 45
int32_t sfp_deltaTx = 46407;
int32_t sfp_deltaRx = 167843;
uint32_t cal_phase_transition = 2389;
46

47
static void wrc_initialize()
48
{
49
	uint8_t mac_addr[6];
50

51
	sdb_find_devices();
52 53
	uart_init_sw();
	uart_init_hw();
54

55
	mprintf("WR Core: starting up...\n");
56

57
	timer_init(1);
58
	wrpc_w1_init();
59 60
	wrpc_w1_bus.detail = ONEWIRE_PORT;
	w1_scan_bus(&wrpc_w1_bus);
61

62 63
	/*initialize flash*/
	flash_init();
64 65
	/*initialize I2C bus*/
	mi2c_init(WRPC_FMC_I2C);
66 67
	/*init storage (Flash / W1 EEPROM / I2C EEPROM*/
	storage_init(WRPC_FMC_I2C, FMC_EEPROM_ADR);
68

69 70 71
	mac_addr[0] = 0x08;	//
	mac_addr[1] = 0x00;	// CERN OUI
	mac_addr[2] = 0x30;	//
72

73 74 75 76 77 78 79 80 81
	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;	//
	}
82

83 84 85
	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]);
86

87 88
	ep_init(mac_addr);
	ep_enable(1, 1);
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
89

90
	minic_init();
91
	shw_pps_gen_init();
92
	wrc_ptp_init();
93 94
	//try reading t24 phase transition from EEPROM
	calib_t24p(WRC_MODE_MASTER, &cal_phase_transition);
95
	spll_very_init();
96

97
#ifdef CONFIG_ETHERBONE
98 99
	ipv4_init("wru1");
	arp_init("wru1");
100
#endif
101
}
102

103 104 105 106
#define LINK_WENT_UP 1
#define LINK_WENT_DOWN 2
#define LINK_UP 3
#define LINK_DOWN 4
107

108
static int wrc_check_link()
109 110
{
	static int prev_link_state = -1;
111
	int link_state = ep_link_up(NULL);
112 113
	int rv = 0;

114 115 116 117 118 119 120 121 122 123 124 125 126
	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;
127
}
128

129 130 131
void wrc_debug_printf(int subsys, const char *fmt, ...)
{
	va_list ap;
132

133 134
	if (wrc_ui_mode)
		return;
135

136
	va_start(ap, fmt);
137

138
	if (subsys & (1 << 5) /* was: TRACE_SERVO -- see commit message */)
139
		vprintf(fmt, ap);
140

141 142 143 144 145
	va_end(ap);
}

int wrc_man_phase = 0;

146
static void ui_update()
147 148
{

149 150
	if (wrc_ui_mode == UI_GUI_MODE) {
		wrc_mon_gui();
151
		if (uart_read_byte() == 27 || wrc_ui_refperiod == 0) {
152 153
			shell_init();
			wrc_ui_mode = UI_SHELL_MODE;
154
		}
155 156
	} else if (wrc_ui_mode == UI_STAT_MODE) {
		wrc_log_stats(0);
157
		if (uart_read_byte() == 27 || wrc_ui_refperiod == 0) {
158 159 160 161 162
			shell_init();
			wrc_ui_mode = UI_SHELL_MODE;
		}
	} else
		shell_interactive();
163 164

}
165

166 167
/* initialize functions to be called after reset in check_reset function */
void init_hw_after_reset(void)
168
{
169 170
	/* Ok, now init the devices so we can printf and delay */
	sdb_find_devices();
171 172
	uart_init_sw();
	uart_init_hw();
173 174 175
	timer_init(1);
}

176 177
int main(void)
{
178
	check_reset();
179
	wrc_ui_mode = UI_SHELL_MODE;
180
	_endram = ENDRAM_MAGIC;
181

182
	wrc_initialize();
183
	usleep_init();
184
	shell_init();
185

186 187
	wrc_ptp_set_mode(WRC_MODE_SLAVE);
	wrc_ptp_start();
188

189 190
	//try to read and execute init script from EEPROM
	shell_boot_script();
191

192 193
	for (;;) {
		int l_status = wrc_check_link();
194

195
		switch (l_status) {
196
#ifdef CONFIG_ETHERBONE
197 198 199
		case LINK_WENT_UP:
			needIP = 1;
			break;
200
#endif
201

202 203
		case LINK_UP:
			update_rx_queues();
204
#ifdef CONFIG_ETHERBONE
205 206
			ipv4_poll();
			arp_poll();
207
#endif
208 209 210
			break;

		case LINK_WENT_DOWN:
211
			if (wrc_ptp_get_mode() == WRC_MODE_SLAVE) {
212
				spll_init(SPLL_MODE_FREE_RUNNING_MASTER, 0, 1);
213 214
				shw_pps_gen_enable_output(0);
			}
215 216 217 218 219
			break;
		}

		ui_update();
		wrc_ptp_update();
220
		spll_update();
221
		check_stack();
222
	}
223
}