ec.c 18.3 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
27
28
29
30
31
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007 - 2012
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007 - 2012

	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/>.

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>

#include "controller_sample.h"

#include "ec.h"
#include "ec_int.h"
32
#include "eth.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
33
34
35
#include "log.h"

static int ec_sock;
36
37
38
39
40
41
42
43

struct unaligned_uint64_t {
	uint64_t u64;
} __packed;

struct unaligned_uint16_t {
	uint16_t u16;
} __packed;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
44
45
46
47
48
49

struct ec_pdo_frame {
	unsigned char tx_frame[1500];
	unsigned char rx_frame[1500];
	
	struct ec_frame_hdr *frame_hdr;
50
51
	struct unaligned_uint64_t *time;
	struct unaligned_uint16_t *time_wkc;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
	size_t flen;
	uint32_t nr;
	int tx_users;
	int rx_users;
	int ready;
};

static struct ec_pdo_frame tx_pdo_real = {
	frame_hdr: (struct ec_frame_hdr *)&tx_pdo_real.tx_frame,
	flen: sizeof(struct ec_frame_hdr),
	nr: -1,
	tx_users: 0,
	rx_users: 0,
};
static struct ec_pdo_frame *tx_pdo = &tx_pdo_real;

static struct ec_pdo_frame rx_pdo = {
	frame_hdr: (struct ec_frame_hdr *)&rx_pdo.tx_frame,
	flen: sizeof(struct ec_frame_hdr),
	nr: -1,
	tx_users: 0,
	rx_users: 0,
};

static int rx_pdo_enabled = 1;
static int tx_pdo_enabled = 1;
static int single_pdo = 0;

static int ec_rx_pdo_add_time(void)
{
	struct ec_dgram_hdr *dgram;
	struct ec_dgram_hdr *prev;
84
	struct unaligned_uint16_t *wkc;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
85
86
87
88
	
	rx_pdo.rx_users = ec_slave_count();

	dgram = (struct ec_dgram_hdr *)(rx_pdo.tx_frame + rx_pdo.flen);
89
90
	rx_pdo.time = (struct unaligned_uint64_t *)
	    (rx_pdo.tx_frame + rx_pdo.flen + sizeof(struct ec_dgram_hdr));
Jeroen Vreeken's avatar
Jeroen Vreeken committed
91
92
93
94
	
	rx_pdo.flen += sizeof(struct ec_dgram_hdr);
	rx_pdo.flen += sizeof(uint64_t) + 2;

95
96
	log_send(LOG_T_DEBUG,
	    "Adding %zu bytes for clock distribution to rx_pdo, total pdo is now: %zu bytes",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
97
98
	    sizeof(uint64_t), rx_pdo.flen);

99
100
101
102
103
	wkc = (struct unaligned_uint16_t *)
	    (rx_pdo.tx_frame + rx_pdo.flen - 2);
	wkc->u16 = 0;
	rx_pdo.time_wkc = (struct unaligned_uint16_t *)
	    (rx_pdo.rx_frame + rx_pdo.flen - 2);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

	EC_FRAME_HDR_SET(rx_pdo.frame_hdr,
	    rx_pdo.flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);

	dgram->idx = 0;

	dgram->cmd = EC_DGRAM_HDR_CMD_BWR;
	dgram->addr.position.pos = 0x0000;
	dgram->addr.position.off = ESC_ADDR_MAP_DC_SYSTEM_TIME;

	EC_DGRAM_HDR_SET(dgram, sizeof(uint64_t), 0, 0);

	for (prev = (struct ec_dgram_hdr *)(rx_pdo.tx_frame + sizeof(struct ec_frame_hdr));
	    prev != dgram;
	    prev = ((void*)prev) + EC_DGRAM_HDR_LEN(prev) + sizeof(struct ec_dgram_hdr) + 2) {
		EC_DGRAM_HDR_SET(prev, EC_DGRAM_HDR_LEN(prev), EC_DGRAM_HDR_C(prev), 1);
	}
	
	return 0;
}

int ec_rx_pdo_add(struct ec_dgram_addr *addr, unsigned char **data, size_t len)
{
	struct ec_dgram_hdr *dgram;
	struct ec_dgram_hdr *prev;
130
	struct unaligned_uint16_t *wkc;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
131
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
132
133
134
135
136
137
138
139
140
141
142
143
144
	for (prev = (struct ec_dgram_hdr *)(rx_pdo.tx_frame + sizeof(struct ec_frame_hdr));
	    (size_t)prev < (size_t)rx_pdo.tx_frame + rx_pdo.flen;
	    prev = ((void*)prev) + EC_DGRAM_HDR_LEN(prev) + sizeof(struct ec_dgram_hdr) + 2) {
		if (addr->type == EC_DGRAM_ADDR_AUTO_INC &&
		    prev->cmd == EC_DGRAM_HDR_CMD_APRD &&
		    prev->addr.position.pos == addr->addr.position.pos &&
		    prev->addr.position.off == addr->addr.position.off) {
			printf("Double registration for rx PDO");
			*data = (unsigned char *)prev + sizeof(struct ec_dgram_hdr);
			return 0;
		}
	}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
145
146
147
148
149
150
151
152
153
	dgram = (struct ec_dgram_hdr *)(rx_pdo.tx_frame + rx_pdo.flen);
	*data = rx_pdo.rx_frame + rx_pdo.flen + sizeof(struct ec_dgram_hdr);
	
	rx_pdo.flen += sizeof(struct ec_dgram_hdr);
	rx_pdo.flen += len + 2;

	printf("Adding %zu bytes data to rx_pdo, total pdo is now: %zu bytes\n",
	    len, rx_pdo.flen);

154
155
	wkc = (struct unaligned_uint16_t*)(rx_pdo.tx_frame + rx_pdo.flen - 2);
	wkc->u16 = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

	EC_FRAME_HDR_SET(rx_pdo.frame_hdr,
	    rx_pdo.flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);

	dgram->idx = 0;

	switch (addr->type) {
		case EC_DGRAM_ADDR_AUTO_INC:
			dgram->cmd = EC_DGRAM_HDR_CMD_APRD;
			dgram->addr.position.pos = addr->addr.position.pos;
			dgram->addr.position.off = addr->addr.position.off;
			printf("Address: %x.%x\n", addr->addr.position.pos,
			    addr->addr.position.off);
			break;
		default:
			printf("Addressing mode not implemented!\n");
			return 1;
	}

	EC_DGRAM_HDR_SET(dgram, len, 0, 0);

	for (prev = (struct ec_dgram_hdr *)(rx_pdo.tx_frame + sizeof(struct ec_frame_hdr));
	    prev != dgram;
	    prev = ((void*)prev) + EC_DGRAM_HDR_LEN(prev) + sizeof(struct ec_dgram_hdr) + 2) {
		EC_DGRAM_HDR_SET(prev, EC_DGRAM_HDR_LEN(prev), EC_DGRAM_HDR_C(prev), 1);
	}
	
	return 0;
}

int ec_tx_pdo_add(struct ec_dgram_addr *addr, unsigned char **data, size_t len)
{
	struct ec_dgram_hdr *dgram;
	struct ec_dgram_hdr *prev;
191
	struct unaligned_uint16_t *wkc;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
192
193
194
195
196
197
198
199
200
201
202
203
204

	for (prev = (struct ec_dgram_hdr *)(tx_pdo->tx_frame + sizeof(struct ec_frame_hdr));
	    (size_t)prev < (size_t)tx_pdo->tx_frame + tx_pdo->flen;
	    prev = ((void*)prev) + EC_DGRAM_HDR_LEN(prev) + sizeof(struct ec_dgram_hdr) + 2) {
		if (addr->type == EC_DGRAM_ADDR_AUTO_INC &&
		    prev->cmd == EC_DGRAM_HDR_CMD_APWR &&
		    prev->addr.position.pos == addr->addr.position.pos &&
		    prev->addr.position.off == addr->addr.position.off) {
			printf("Double registration for tx PDO");
			*data = (unsigned char *)prev + sizeof(struct ec_dgram_hdr);
			return 0;
		}
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
205
206
207
208
209
210
211
212
213
214
215
216
217
	
	dgram = (struct ec_dgram_hdr *)(tx_pdo->tx_frame + tx_pdo->flen);
	*data = tx_pdo->tx_frame + tx_pdo->flen + sizeof(struct ec_dgram_hdr);
	
	tx_pdo->flen += sizeof(struct ec_dgram_hdr);
	tx_pdo->flen += len + 2;
	
	tx_pdo->tx_users++;

	printf("Adding %zu bytes data to tx_pdo, total pdo is now: %zu bytes\n",
	    len, tx_pdo->flen);
	printf("tx_pdo has %d users\n", tx_pdo->tx_users);

218
219
	wkc = (struct unaligned_uint16_t*)(tx_pdo->tx_frame + tx_pdo->flen - 2);
	wkc->u16 = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

	EC_FRAME_HDR_SET(tx_pdo->frame_hdr,
	    tx_pdo->flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);

	dgram->idx = 0;

	switch (addr->type) {
		case EC_DGRAM_ADDR_AUTO_INC:
			dgram->cmd = EC_DGRAM_HDR_CMD_APWR;
			dgram->addr.position.pos = addr->addr.position.pos;
			dgram->addr.position.off = addr->addr.position.off;
			break;
		default:
			printf("Addressing mode not implemented!\n");
			return 1;
	}

	EC_DGRAM_HDR_SET(dgram, len, 0, 0);

	for (prev = (struct ec_dgram_hdr *)(tx_pdo->tx_frame + sizeof(struct ec_frame_hdr));
	    prev != dgram;
	    prev = ((void*)prev) + EC_DGRAM_HDR_LEN(prev) + sizeof(struct ec_dgram_hdr) + 2) {
		EC_DGRAM_HDR_SET(prev, EC_DGRAM_HDR_LEN(prev), EC_DGRAM_HDR_C(prev), 1);
	}
	
	return 0;
}

static ssize_t ec_dgram(void *tx_frame, void *rx_frame, size_t len)
{
	int ret;
	struct ec_dgram_hdr *rx_dgram1, *tx_dgram1;
	static uint8_t dgram_idx = 0;
	
	dgram_idx++;

	tx_dgram1 = (struct ec_dgram_hdr *)
	    ((char *)tx_frame +  sizeof(struct ec_frame_hdr));
	rx_dgram1 = (struct ec_dgram_hdr *)
	    ((char *)rx_frame +  sizeof(struct ec_frame_hdr));
	

	tx_dgram1->idx = dgram_idx;

265
	ret = eth_send(ec_sock, tx_frame, len);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
266
267
268
269
	if (ret != len)
		return 1;

	do {
270
		ret = eth_recv(ec_sock, rx_frame, len);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

		if (ret < sizeof(struct ec_frame_hdr)+sizeof(struct ec_dgram_hdr))
			return 1;
	} while (rx_dgram1->idx != dgram_idx);
	
	return 0;

}

int ec_tx_pdo(uint32_t nr)
{
	ssize_t ret;

	if (single_pdo)
		return 0;

	if (!tx_pdo_enabled)
		return 1;

	if (tx_pdo->nr != nr) {
		tx_pdo->ready = 1;
		tx_pdo->nr = nr;
	} else {
		tx_pdo->ready++;
	}
	
	if(tx_pdo->ready < tx_pdo->tx_users) {
			return 0;
	}
	
	ret = ec_dgram(tx_pdo->tx_frame, tx_pdo->rx_frame, tx_pdo->flen);

	return ret;
}

int ec_rx_pdo(uint32_t nr)
{
	ssize_t ret;
	struct timespec t;

	if (!rx_pdo_enabled)
		return 1;

	if (rx_pdo.nr == nr)
		return 0;

	clock_gettime(0, &t);
318
	rx_pdo.time->u64 = htole64(
Jeroen Vreeken's avatar
Jeroen Vreeken committed
319
320
321
322
323
324
325
	    (946684800 + (uint64_t)t.tv_sec) * 1000000000 +
	    (uint64_t)t.tv_nsec
	);

	ret = ec_dgram(rx_pdo.tx_frame, rx_pdo.rx_frame, rx_pdo.flen);

	if (ret) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
326
		log_send(LOG_T_ERROR, "Did not send/receive ethercat frame!");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
327
328
329
		return ret;
	}

330
	if (le16toh(rx_pdo.time_wkc->u16) < rx_pdo.rx_users) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
331
332
		log_send(LOG_T_ERROR,
		    "Only received ack from %d ethercat slaves! (%d expected)",
333
		    le16toh(rx_pdo.time_wkc->u16), rx_pdo.rx_users);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
334
335
336
337
338
339
340
		return 1;
	}
	
	rx_pdo.nr = nr;
	return 0;
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
341
342
343
344
345
346
347
348
349
350
351
352
struct ec_pdo_hook {
	struct ec_pdo_hook *next;
	
	int (*hook)(void *arg);
	void *arg;
};

static struct ec_pdo_hook *ec_rx_pdo_enable_hooks = NULL;
static struct ec_pdo_hook *ec_tx_pdo_enable_hooks = NULL;
static struct ec_pdo_hook *ec_rx_pdo_disable_hooks = NULL;
static struct ec_pdo_hook *ec_tx_pdo_disable_hooks = NULL;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
353
354
int ec_rx_pdo_enable(int enable)
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
355
356
357
358
359
360
361
362
363
	struct ec_pdo_hook *hook;
	bool oldstate = rx_pdo_enabled;

	if (enable && !oldstate) {
		log_send(LOG_T_INFO, "Enabling ethercat rx PDOs");
		for (hook = ec_rx_pdo_enable_hooks; hook; hook = hook->next)
			hook->hook(hook->arg);
	}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
364
	rx_pdo_enabled = enable;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
365
366
367
368
369
370
371
	
	if (!enable && oldstate) {
		log_send(LOG_T_INFO, "Disabling ethercat rx PDOs");
		for (hook = ec_rx_pdo_disable_hooks; hook; hook = hook->next)
			hook->hook(hook->arg);
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
372
373
374
375
376
	return 0;
}

int ec_tx_pdo_enable(int enable)
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
	struct ec_pdo_hook *hook;
	bool oldstate = tx_pdo_enabled;
	
	if (enable && !oldstate) {
		log_send(LOG_T_INFO, "Enabling ethercat tx PDOs");
		for (hook = ec_tx_pdo_enable_hooks; hook; hook = hook->next)
			hook->hook(hook->arg);
	}

	tx_pdo_enabled = enable;
	
	if (!enable && oldstate) {
		log_send(LOG_T_INFO, "Disabling ethercat tx PDOs");
		for (hook = ec_tx_pdo_disable_hooks; hook; hook = hook->next)
			hook->hook(hook->arg);
	}
	
	return 0;
}

int ec_rx_pdo_enable_hook_attach(int (*hook)(void *arg), void *arg)
{
	struct ec_pdo_hook *newhook;
	
	newhook = malloc(sizeof(struct ec_pdo_hook));
	if (!newhook)
		return -1;
	
	newhook->hook = hook;
	newhook->arg = arg;
	newhook->next = ec_rx_pdo_enable_hooks;
	ec_rx_pdo_enable_hooks = newhook;

	return 0;
}

int ec_tx_pdo_enable_hook_attach(int (*hook)(void *arg), void *arg)
{
	struct ec_pdo_hook *newhook;
	
	newhook = malloc(sizeof(struct ec_pdo_hook));
	if (!newhook)
		return -1;
	
	newhook->hook = hook;
	newhook->arg = arg;
	newhook->next = ec_tx_pdo_enable_hooks;
	ec_tx_pdo_enable_hooks = newhook;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
426
427
428
	return 0;
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
int ec_rx_pdo_disable_hook_attach(int (*hook)(void *arg), void *arg)
{
	struct ec_pdo_hook *newhook;
	
	newhook = malloc(sizeof(struct ec_pdo_hook));
	if (!newhook)
		return -1;
	
	newhook->hook = hook;
	newhook->arg = arg;
	newhook->next = ec_rx_pdo_disable_hooks;
	ec_rx_pdo_disable_hooks = newhook;

	return 0;
}

int ec_tx_pdo_disable_hook_attach(int (*hook)(void *arg), void *arg)
{
	struct ec_pdo_hook *newhook;
	
	newhook = malloc(sizeof(struct ec_pdo_hook));
	if (!newhook)
		return -1;
	
	newhook->hook = hook;
	newhook->arg = arg;
	newhook->next = ec_tx_pdo_disable_hooks;
	ec_tx_pdo_disable_hooks = newhook;

	return 0;
}



Jeroen Vreeken's avatar
Jeroen Vreeken committed
463
464
465
466
467
468
469
int ec_slave_count(void)
{
	char frame[1500];
	struct ec_dgram_hdr *dgram;
	struct ec_frame_hdr *frame_hdr = (struct ec_frame_hdr *)frame;
	char *payload;
	ssize_t flen;
470
	struct unaligned_uint16_t *wkc;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
471
472
	int ret;
	
473
	flen = sizeof(struct ec_frame_hdr);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
474
475
476
477
478
	dgram = (struct ec_dgram_hdr *)(frame + flen);
	payload = (frame + flen + sizeof(struct ec_dgram_hdr));	
	flen += sizeof(struct ec_dgram_hdr);
	flen += sizeof(char) + 2;

479
	log_send(LOG_T_DEBUG, "Reading byte 0 of all ethercat slaves");
480
481
	wkc = (struct unaligned_uint16_t *)(frame + flen - 2);
	wkc->u16 = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
	*payload = 0x00;

	EC_FRAME_HDR_SET(frame_hdr,
	    flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);

	dgram->idx = 0;

	dgram->cmd = EC_DGRAM_HDR_CMD_BRD;
	dgram->addr.position.pos = 0x0000;
	dgram->addr.position.off = ESC_ADDR_MAP_TYPE;

	EC_DGRAM_HDR_SET(dgram, sizeof(char), 0, 0);

	ret = ec_dgram(frame, frame, flen);

	if (ret)
	{
		printf("Did not send/receive ethercat frame!\n");
		return -1;
	}
	
504
	return le16toh(wkc->u16);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
}

static struct ec_dgram_addr *datagram_addr;
static void *datagram_buffer;
static size_t datagram_len;
static ssize_t datagram_ret;
static bool datagram_read = false;
static bool datagram_write = false;

static bool sample_running = false;
static pthread_mutex_t datagram_lock;
static sem_t datagram_sync_sem;

static void ec_datagram_sample_init(void *arg)
{
Jeroen Vreeken's avatar
Jeroen Vreeken committed
520
521
522
523
524
525
526
527
528
529
	struct timeval timeout;
	double timeout_sec;

	timeout_sec = controller_sample_period() * 4;
	
	timeout.tv_sec = (int)(timeout_sec);
	timeout.tv_usec = (int)(timeout_sec * 1000000) % 1000000;
	log_send(LOG_T_INFO,
	    "Setting ethercat rx/tx timeout to %d.%06d sec based on %f sec period", 
	    (int)timeout.tv_sec, (int)timeout.tv_usec, controller_sample_period());
530
531

	eth_timeout_set(ec_sock, &timeout, &timeout);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
532
533

	log_send(LOG_T_INFO, "Switching ethercat datagrams to sample-synchronised mode");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
534
535
536
537
538
539
540
541
542
543
544
545
546
	pthread_mutex_init(&datagram_lock, NULL);
	sem_init(&datagram_sync_sem, 0, 0);
	
	sample_running = true;
}

static ssize_t ec_datagram_read_real(struct ec_dgram_addr *addr,
    void *buffer, size_t len)
{
	unsigned char frame[1500];
	struct ec_frame_hdr *frame_hdr;
	struct ec_dgram_hdr *dgram_hdr;
	void *data;
547
	struct unaligned_uint16_t *wkc;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
548
549
550
551
552
553
554
555
556
557
558
559
	size_t flen =
	    sizeof(struct ec_frame_hdr) +
	    sizeof(struct ec_dgram_hdr) +
	    len +
	    sizeof(uint16_t);
	ssize_t ret;

	frame_hdr = (struct ec_frame_hdr*)frame;
	dgram_hdr = (struct ec_dgram_hdr*)(frame + sizeof(struct ec_frame_hdr));
	data = (unsigned char *)dgram_hdr + sizeof(struct ec_dgram_hdr);

	memset(frame, 0, flen);
560
561
	wkc = (struct unaligned_uint16_t*)(frame + flen - sizeof(uint16_t));
	wkc->u16 = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
	
	EC_FRAME_HDR_SET(frame_hdr, flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);
	
	dgram_hdr->idx = 0;

	switch (addr->type) {
		case EC_DGRAM_ADDR_AUTO_INC:
			dgram_hdr->cmd = EC_DGRAM_HDR_CMD_APRD;
			dgram_hdr->addr.position.pos = addr->addr.position.pos;
			dgram_hdr->addr.position.off = addr->addr.position.off;
			break;
		default:
			goto err_add;
	}

	EC_DGRAM_HDR_SET(dgram_hdr, len, 0, 0);
	
	ret = ec_dgram(frame, frame, flen);

	if (ret)
		goto err_sendrecv;

	/* wkc should be updated by slave */
586
	if (le16toh(wkc->u16) == 1) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
		memcpy(buffer, data, len);
		return len;
	} else
		return 0;

err_add:
err_sendrecv:
	return -1;
}

ssize_t ec_datagram_read(struct ec_dgram_addr *addr,
    void *buffer, size_t len)
{
	ssize_t ret;
	
602
	if (!sample_running || controller_sample_context())
Jeroen Vreeken's avatar
Jeroen Vreeken committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
		return ec_datagram_read_real(addr, buffer, len);
	
	pthread_mutex_lock(&datagram_lock);
	
	datagram_addr = addr;
	datagram_buffer = buffer;
	datagram_len = len;
	datagram_read = true;
	
	sem_wait(&datagram_sync_sem);
	
	ret = datagram_ret;
	
	pthread_mutex_unlock(&datagram_lock);

	return ret;
}

static ssize_t ec_datagram_write_real(struct ec_dgram_addr *addr,
    void *buffer, size_t len)
{
	unsigned char frame[1500];
	struct ec_frame_hdr *frame_hdr;
	struct ec_dgram_hdr *dgram_hdr;
	void *data;
628
	struct unaligned_uint16_t *wkc;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
	size_t flen =
	    sizeof(struct ec_frame_hdr) +
	    sizeof(struct ec_dgram_hdr) +
	    len +
	    sizeof(uint16_t);
	ssize_t ret;
//	struct timeval timeout;
//	fd_set fdr;

	frame_hdr = (struct ec_frame_hdr*)frame;
	dgram_hdr = (struct ec_dgram_hdr*)(frame + sizeof(struct ec_frame_hdr));
	data = (unsigned char *)dgram_hdr + sizeof(struct ec_dgram_hdr);

	memset(frame, 0, flen);
643
644
	wkc = (struct unaligned_uint16_t*)(frame + flen - sizeof(uint16_t));
	wkc->u16 = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
	
	EC_FRAME_HDR_SET(frame_hdr, flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);
	
	dgram_hdr->idx = 0;

	switch (addr->type) {
		case EC_DGRAM_ADDR_AUTO_INC:
			dgram_hdr->cmd = EC_DGRAM_HDR_CMD_APWR;
			dgram_hdr->addr.position.pos = addr->addr.position.pos;
			dgram_hdr->addr.position.off = addr->addr.position.off;
			break;
		default:
			goto err_add;
	}

	EC_DGRAM_HDR_SET(dgram_hdr, len, 0, 0);
	
	memcpy(data, buffer, len);
	
	ret = ec_dgram(frame, frame, flen);

	if (ret)
		goto err_sendrecv;

	/* wkc should be updated by slave */
671
	if (le16toh(wkc->u16) == 1) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
		return len;
	} else
		return 0;

err_add:
err_sendrecv:
	return -1;
}

ssize_t ec_datagram_write(struct ec_dgram_addr *addr,
    void *buffer, size_t len)
{
	ssize_t ret;
	
686
	if (!sample_running || controller_sample_context())
Jeroen Vreeken's avatar
Jeroen Vreeken committed
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
		return ec_datagram_write_real(addr, buffer, len);
	
	pthread_mutex_lock(&datagram_lock);
	
	datagram_addr = addr;
	datagram_buffer = buffer;
	datagram_len = len;
	datagram_write = true;
	
	sem_wait(&datagram_sync_sem);
	
	ret = datagram_ret;
	
	pthread_mutex_unlock(&datagram_lock);

	return ret;
}

void ec_datagram_sample(void)
{
	if (datagram_write) {
		datagram_ret = ec_datagram_write_real(
		    datagram_addr,
		    datagram_buffer,
		    datagram_len);
		datagram_write = false;
		sem_post(&datagram_sync_sem);
	} else if (datagram_read) {
		datagram_ret = ec_datagram_read_real(
		    datagram_addr,
		    datagram_buffer,
		    datagram_len);
		datagram_read = false;
		sem_post(&datagram_sync_sem);
	}
}



static int init_done = 0;

static bool init_success = false;

bool ec_initialized(void)
{
	return init_success;
}

735
int ec_init(char *ifname, bool single_cycle)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
736
737
738
739
740
741
742
743
744
745
746
747
748
{
	struct timeval timeout;
	double timeout_sec;

	if (init_done)
		return 0;

	single_pdo = single_cycle;
	if (single_pdo) {
		tx_pdo = &rx_pdo;
		printf("Using a single PDO for rx and tx.\n");
	}
	
749
750
	log_send(LOG_T_DEBUG, 
	    "Going to initialize interface %s for control data", ifname);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
751

752
	ec_sock = eth_open(ifname);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
753
754
755
	if (ec_sock < 0)
		goto err_nosocket;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
756
	timeout_sec = 0.1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
757
758
759
	
	timeout.tv_sec = (int)(timeout_sec);
	timeout.tv_usec = (int)(timeout_sec * 1000000) % 1000000;
760
761
	log_send(LOG_T_DEBUG,
	    "Setting rx/tx timeout to %d.%06d sec based on %f sec default period", 
Jeroen Vreeken's avatar
Jeroen Vreeken committed
762
	    (int)timeout.tv_sec, (int)timeout.tv_usec, controller_sample_period());
763
764

	eth_timeout_set(ec_sock, &timeout, &timeout);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
765
766
767
768
769
770

	if (ec_slave_count() >= 0) {
		init_success = true;
	} else {
		goto err_init;
	}
771
772
	log_send(LOG_T_DEBUG, 
	    "Successfull init, %d slaves found.", ec_slave_count());
Jeroen Vreeken's avatar
Jeroen Vreeken committed
773
774
775
776
777
778
779
780
781
782
783
784
785

	ec_rx_pdo_add_time();

	controller_sample_start_hook(ec_datagram_sample_init, NULL);

	init_done = 1;

	return 0;

err_init:
	close(ec_sock);
	ec_sock = -1;
err_nosocket:
786
	log_send(LOG_T_ERROR, "Failed to initialize Ethercat subsystem");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
787
788
	return -1;
}