esc_dc.c 6.02 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2013

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */
/*
	esc_dc.c
	
	Distributed clock & functions.
 */

#include <time.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
27
28
29
#include <ec/esc_dc.h>
#include <ec/esc_registers.h>
#include <log/log.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

#define ESC_DC_INIT_LOOPCOUNT	100

int esc_dc_init_system_time(struct ec_dgram_addr *addr)
{
	struct timespec t;
	uint64_t leval64, local_time;
	int64_t offset;
	uint32_t leval32;
	uint32_t mean;
	bool smaller;
	int i;

	addr->addr.position.off = ESC_ADDR_MAP_DC_SYSTEM_TIME_OFFSET;
	ec_datagram_read(addr, &leval64, 8);
	log_send(LOG_T_DEBUG, "\told DC system time offset: %"PRId64, 
	    le64toh(leval64));

	/* reset offset */
	leval64 = 0;
	addr->addr.position.off = ESC_ADDR_MAP_DC_SYSTEM_TIME_OFFSET;
	ec_datagram_write(addr, &leval64, 8);

	/* Read system time without offset (= local_time @ slave) */
	addr->addr.position.off = ESC_ADDR_MAP_DC_SYSTEM_TIME;
	ec_datagram_read(addr, &leval64, 8);
	local_time = le64toh(leval64);

	/* Write new time offset to device */
	clock_gettime(0, &t);
	offset = 
	    (ESC_DC_EPOCH + (int64_t)t.tv_sec) * 1000*1000*1000 +
	    (int64_t)t.tv_nsec -
	    local_time;
	leval64 = htole64(offset);
	addr->addr.position.off = ESC_ADDR_MAP_DC_SYSTEM_TIME_OFFSET;
	ec_datagram_write(addr, &leval64, 8);

	log_send(LOG_T_DEBUG, "\tDC local time: %"PRIu64, 
	    local_time);
	log_send(LOG_T_DEBUG, "\tDC system time offset: %"PRId64, 
	    le64toh(leval64));

	/* Write our time to the slave */
	for (i = 0; i < ESC_DC_INIT_LOOPCOUNT; i++) {
		clock_gettime(0, &t);
		leval64 = htole64(
		    (ESC_DC_EPOCH + (int64_t)t.tv_sec) * 1000*1000*1000 +
		    (int64_t)t.tv_nsec);
		addr->addr.position.off = ESC_ADDR_MAP_DC_SYSTEM_TIME;
		ec_datagram_write(addr, &leval64, 8);
	}
	
	/* Check system time difference */
	addr->addr.position.off = ESC_ADDR_MAP_DC_SYSTEM_TIME_DIFFERENCE;
	ec_datagram_read(addr, &leval32, 4);
	mean = le32toh(leval32) & ESC_DC_SYSTEM_TIME_DIFFERENCE_MEAN_MASK;
	smaller = le32toh(leval32) & ESC_DC_SYSTEM_TIME_DIFFERENCE_SMALLER;
	log_send(LOG_T_DEBUG, 
	    "\tDC system time difference mean: %"PRIu32" ns %s than system time",
	    mean, smaller ? "smaller" : "greater");

	return 0;
}

int esc_dc_init(struct ec_dgram_addr *addr, struct esc_dc_sync *sync)
{
	uint8_t val8;
	uint64_t leval64;
	uint32_t leval32;
	uint16_t leval16;
	uint16_t pulse_length;
	struct timespec t;
	int r = 0;
	
	log_send(LOG_T_DEBUG, "Configure & initialise DC");
	
	addr->addr.position.off = ESC_ADDR_MAP_FEATURES;
	r = ec_datagram_read(addr, &leval16, 2);
	if (r != 2)
		return -1;
		
	if (!(le16toh(leval16) & ESC_FEATURE_DISTRIBUTED_CLOCKS))
	{
		log_send(LOG_T_DEBUG, "\tSlave does not support DC");
		return 0;
	}

	/* Deactivate any old sync settings */
	addr->addr.position.off = ESC_ADDR_MAP_DC_ACTIVATION_REGISTER;
	val8 = 0;
	ec_datagram_write(addr, &val8, 1);

	/* initialize the distributed clock */
	esc_dc_init_system_time(addr);


	addr->addr.position.off = ESC_ADDR_MAP_DC_PULSE_LENGTH;
	r = ec_datagram_read(addr, &leval16, 2);
	if (r != 2)
		return -1;
	pulse_length = le16toh(leval16);

	if (sync->sync_out_active) {
		log_send(LOG_T_DEBUG, "\tEnable Sync Out");
	}
	if (sync->sync0_active) {
		log_send(LOG_T_DEBUG, "\tEnable SYNC0 with %d ns cycle and %d ns shift",
		    sync->sync0_cycle, sync->sync0_shift);
		addr->addr.position.off = ESC_ADDR_MAP_DC_SYNC0_CYCLE_TIME;
		leval32 = htole32(sync->sync0_cycle);
		r = ec_datagram_write(addr, &leval32, 4);
		
		// TODO: is this always allowed? does positive/negative factor
		// mean anything here???
		if (!pulse_length)
			pulse_length = 100;
		addr->addr.position.off = ESC_ADDR_MAP_DC_PULSE_LENGTH;
		leval16 = htole16(pulse_length);
		r = ec_datagram_write(addr, &leval16, 2);
		log_send(LOG_T_DEBUG, "\tSync pulse length: %d ns", pulse_length * 10);

		/* start at begin of next second ( + sync0 shift )*/
		clock_gettime(0, &t);
		leval64 = htole64(
		    (ESC_DC_EPOCH + (uint64_t)t.tv_sec + 2) * 1000*1000*1000 +
		    (int64_t)sync->sync0_shift
		);
		addr->addr.position.off = ESC_ADDR_MAP_DC_START_TIME_CYCLIC_OPERATION;
		ec_datagram_write(addr, &leval64, 8);
		log_send(LOG_T_DEBUG, "\tSYNC0 start time: %"PRIu64,
		    le64toh(leval64));

	}
	if (sync->sync1_active) {
		log_send(LOG_T_DEBUG, "\tEnable SYNC1 with %d ns shift",
		    sync->sync1_shift);
		addr->addr.position.off = ESC_ADDR_MAP_DC_SYNC1_CYCLE_TIME;
		leval32 = htole32(sync->sync1_shift);
		r = ec_datagram_write(addr, &leval32, 4);
	}
	
	/* Configure distributed clock */
	addr->addr.position.off = ESC_ADDR_MAP_DC_CYCLIC_UNIT_CONTROL;
	val8 = (sync->latch0_pdi ? ESC_DC_CYCLIC_UNIT_CONTROL_LATCH0_PDI : 0) |
	    (sync->latch1_pdi ? ESC_DC_CYCLIC_UNIT_CONTROL_LATCH1_PDI : 0);
	log_send(LOG_T_DEBUG, "\tCyclic control register: 0x%02x", val8);
	ec_datagram_write(addr, &val8, 1);


	addr->addr.position.off = ESC_ADDR_MAP_DC_ACTIVATION_REGISTER;
	val8 = 
/*	    ESC_DC_ACTIVATION_REGISTER_START_TIME_PLAUSIBLE_CHECK | 
	    ESC_DC_ACTIVATION_REGISTER_AUTO_ACTIVATION |
*/	    (sync->sync_out_active ? ESC_DC_ACTIVATION_REGISTER_SYNC_OUT_ACTIVE : 0) |
	    (sync->sync0_active ? ESC_DC_ACTIVATION_REGISTER_SYNC0_ACTIVE : 0) |
	    (sync->sync1_active ? ESC_DC_ACTIVATION_REGISTER_SYNC1_ACTIVE : 0);
	log_send(LOG_T_DEBUG, "\tActivation register: 0x%02x", val8);
	ec_datagram_write(addr, &val8, 1);


	addr->addr.position.off = ESC_ADDR_MAP_DC_ACTIVATION_STATUS;
	r = ec_datagram_read(addr, &val8, 1);
	log_send(LOG_T_DEBUG, "\tDC activation status: 0x%02x", val8);

	return r;
}