esc.c 20.1 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
265
266
267
268
269
270
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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
426
427
428
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2009
	Copyright Stichting C.A. Muller Radioastronomiestation, 2009   

	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 <string.h>
#include <stdio.h>
#include <time.h>

#include "esc.h"
#include "ec.h"
#include "canopen.h"

struct esc_mailbox {
	struct ec_dgram_addr status_rd;
	struct ec_dgram_addr data_rd;
	struct ec_dgram_addr status_wr;
	struct ec_dgram_addr data_wr;
	int ctr;
	int len_rd;
	int len_wr;
	unsigned char *buffer_rd;
	unsigned char *buffer_wr;
};


struct esc_mailbox_hdr {
	uint16_t length;
	uint16_t address;
	uint8_t chan_prio;
	uint8_t type_ctr;
} __attribute((__packed__));

#define ESC_MAILBOX_TYPE_ERROR	0x0
#define ESC_MAILBOX_TYPE_ADS	0x1
#define ESC_MAILBOX_TYPE_EOE	0x2
#define ESC_MAILBOX_TYPE_COE	0x3
#define ESC_MAILBOX_TYPE_FOE	0x4
#define ESC_MAILBOX_TYPE_SOE	0x5
#define ESC_MAILBOX_TYPE_VOE	0xf


ssize_t esc_esi_eeprom_read(struct ec_dgram_addr *ec_addr, 
    void *buffer, size_t offset, size_t size)
{
	int r;
	uint8_t val8;
	uint16_t val16, status;
	uint32_t val32;
	int wordsize;
	size_t readsize = 0;
	
	/* Make sure ecat side has eeprom control */
	do {
		ec_addr->addr.position.off = ESC_ADDR_MAP_ESI_EEPROM_CONFIG;
		r = ec_datagram_read(ec_addr, &val8, 1);
		if (val8 & ESC_ESI_EEPROM_CONFIG_PDI && r == 1) {
			uint8_t resetval8 = ESC_ESI_EEPROM_CONFIG_FORCE_ECAT;
			r = ec_datagram_write(ec_addr, &resetval8, 1);
		}
	} while (val8 & ESC_ESI_EEPROM_CONFIG_PDI && r == 1);
	if (r != 1)
		return -1;

	/* get status */
	ec_addr->addr.position.off = ESC_ADDR_MAP_ESI_EEPROM_CONTROL_STATUS;
	r = ec_datagram_read(ec_addr, &val16, 2);
	if (r != 2)
		return -1;
	
	status = le16toh(val16);
	if (status & ESC_ESI_EEPROM_CONTROL_STATUS_READ_8B) {
		wordsize = 8;
	} else {
		wordsize = 4;
	}

	while (size) {
		uint8_t data[8];
		size_t copysize;
		size_t copyoffset;
		
		/* set address */
		val32 = htole32(offset / (wordsize / 2));
		ec_addr->addr.position.off = ESC_ADDR_MAP_ESI_EEPROM_ADDRESS;
		r = ec_datagram_write(ec_addr, &val32, 4);
		
		/* Wait for other stuff to finish */
		do {
			r = ec_datagram_read(ec_addr, &val16, 2);
		} while (le16toh(val16) & ESC_ESI_EEPROM_CONTROL_STATUS_BUSY && r == 2);

		/* Issue read command */
		ec_addr->addr.position.off = ESC_ADDR_MAP_ESI_EEPROM_CONTROL_STATUS;
		val16 = htole16(ESC_ESI_EEPROM_CONTROL_STATUS_CMD_READ);
		r = ec_datagram_write(ec_addr, &val16, 2);
		
		/* Wait for read to finish */
		do {
			r = ec_datagram_read(ec_addr, &val16, 2);
		} while (le16toh(val16) & ESC_ESI_EEPROM_CONTROL_STATUS_BUSY && r == 2);
		
		/* make sure nothing is fucked up.... */
		status = le16toh(val16);
		if (status & ESC_ESI_EEPROM_CONTROL_STATUS_CHKSUM_ERR ||
		    status & ESC_ESI_EEPROM_CONTROL_STATUS_NOT_LOADED ||
		    status & ESC_ESI_EEPROM_CONTROL_STATUS_ACK_ERR ||
		    status & ESC_ESI_EEPROM_CONTROL_STATUS_WRITE_ERR) {
			return -1;
		}

		/* read data and copy to client buffer */
		ec_addr->addr.position.off = ESC_ADDR_MAP_ESI_EEPROM_DATA;
		r = ec_datagram_read(ec_addr, data, wordsize);
		if (r != wordsize)
			break;
		
		copysize = size;
		if (copysize > wordsize)
			copysize = wordsize;
		copyoffset = offset & (wordsize -1);
		if (copysize + copyoffset > wordsize)
			copysize = wordsize - offset;
		memcpy(buffer, &data[copyoffset], copysize);
		size -= copysize;
		offset += copysize;
		buffer += copysize;
		readsize += copysize;
	}
	
	return readsize;
}


static uint32_t esc_esi_read32(struct ec_dgram_addr *ec_addr, size_t offset)
{
	uint32_t val32;
	
	esc_esi_eeprom_read(ec_addr, &val32, offset, sizeof(uint32_t));
	
	return le32toh(val32);
}

uint32_t esc_esi_vendorid_get(struct ec_dgram_addr *ec_addr)
{
	return esc_esi_read32(ec_addr, ESC_ESI_ADDR_VENDORID);
}

uint32_t esc_esi_productcode_get(struct ec_dgram_addr *ec_addr)
{
	return esc_esi_read32(ec_addr, ESC_ESI_ADDR_PRODUCTCODE);
}

uint32_t esc_esi_revisionno_get(struct ec_dgram_addr *ec_addr)
{
	return esc_esi_read32(ec_addr, ESC_ESI_ADDR_REVISIONNO);
}

uint32_t esc_esi_serialno_get(struct ec_dgram_addr *ec_addr)
{
	return esc_esi_read32(ec_addr, ESC_ESI_ADDR_SERIALNO);
}


int esc_al_state_get(struct ec_dgram_addr *addr)
{
	uint16_t state;
	struct ec_dgram_addr state_add;
	int ret;
	
	memcpy(&state_add, addr, sizeof(state_add));
	state_add.addr.position.off = ESC_ADDR_MAP_AL_STATUS;
	ret = ec_datagram_read(&state_add, &state, 2);

	if (ret == 2) {
		return le16toh(state) & 0x000f;
	} else
		return -1;
}

int esc_al_state_set(struct ec_dgram_addr *addr, int newstate)
{
	uint16_t state;
	struct ec_dgram_addr state_add;
	int ret;
	
	memcpy(&state_add, addr, sizeof(state_add));
	state_add.addr.position.off = ESC_ADDR_MAP_AL_CONTROL;
	state = htole16(newstate);
	do {
		ret = ec_datagram_write(&state_add, &state, sizeof(uint16_t));
	} while (ret == 0);
	
	printf("set state: %d datagram write: %d\n", newstate, ret);
	if (ret == 2) {
		while (1) {
			int status;
			
			state = esc_al_state_get(addr);
			if (state == newstate)
				return newstate;
			else
				printf("state: %d != %d\n", state, newstate);
			status = esc_al_status_code_get(addr);
			if (status != 0) {
				printf("state now: %d status code: 0x%x\n",
				    state, status);
				esc_al_error_ack(addr);
				
				return -1;
			}
		}
	} else
		return -1;
}

int esc_al_status_code_get(struct ec_dgram_addr *addr)
{
	uint16_t state;
	struct ec_dgram_addr state_add;
	int ret;
	
	memcpy(&state_add, addr, sizeof(state_add));
	state_add.addr.position.off = ESC_ADDR_MAP_AL_STATUS;
	ret = ec_datagram_read(&state_add, &state, 2);
	if (ret != 2)
		goto err_get_state;
	
	if (le16toh(state) & ESC_ADDR_MAP_AL_STATUS_ERR_IND) {
		state_add.addr.position.off = ESC_ADDR_MAP_AL_STATUS_CODE;
		ret = ec_datagram_read(&state_add, &state, 2);
		state = le16toh(state);
	} else {
		state = 0;
	}
	
	return state;

err_get_state:
	return -1;
}

int esc_al_error_ack(struct ec_dgram_addr *addr)
{
	uint16_t ctrl;
	struct ec_dgram_addr state_add;
	int ret;
	
	memcpy(&state_add, addr, sizeof(state_add));

	ret = esc_al_state_get(&state_add);
	if (ret < 0)
		return -1;
	ctrl = ret;
	
	state_add.addr.position.off = ESC_ADDR_MAP_AL_CONTROL;
	ctrl |= htole16(ESC_ADDR_MAP_AL_CONTROL_ERROR_ACK);
	ret = ec_datagram_write(&state_add, &ctrl, 2);
	
	if (ret == 2)
		return 0;
	else
		return -1;
}

int esc_init(struct ec_dgram_addr *addr)
{
	uint8_t nr_syncmgr;
	int ret, i;

	addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGERS_SUPPORTED;
	ret = ec_datagram_read(addr, &nr_syncmgr, 1);
	if (ret != 1) {
		printf("Could not read ESC information\n");
		return -1;
	}

	printf("ESC supports %d SyncManagers\n", nr_syncmgr);
	/* De-activate all sync managers. */
	for (i = 0; i < nr_syncmgr; i++) {
		uint16_t off = ESC_ADDR_MAP_SYNCMANAGER_NR(i);
		uint8_t val8;
		uint16_t val16;
		
		addr->addr.position.off = off | ESC_ADDR_MAP_SYNCMANAGER_ACTIVATE;
		val8 = 0;
		
		ret = ec_datagram_write(addr, &val8, 1);
		if (ret != 1) {
			printf("Could not deactivate sync manager %d\n", i);
			return -1;
		}

		val16 = 0;
		addr->addr.position.off = off | ESC_ADDR_MAP_SYNCMANAGER_LENGTH;
			ec_datagram_write(addr, &val16, 2);
	}

	return 0;
}

int esc_syncmanager_claim(struct ec_dgram_addr *addr)
{
	int ret, i;
	int nr = -1;
	uint8_t nr_syncmgr;

	addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGERS_SUPPORTED;
	ret = ec_datagram_read(addr, &nr_syncmgr, 1);
	if (ret != 1) {
		printf("Could not read ESC information\n");
		return -1;
	}

	for (i = 0; i < nr_syncmgr; i++) {
		uint16_t off = ESC_ADDR_MAP_SYNCMANAGER_NR(i);
		uint8_t val8;
		
		addr->addr.position.off = off | ESC_ADDR_MAP_SYNCMANAGER_ACTIVATE;
		
		ret = ec_datagram_read(addr, &val8, 1);
		if (ret != 1) {
			printf("Could not read syncmanger%d activate register\n", i);
			return -1;
		}
		if (!(val8 & ESC_SYNCMANAGER_ACTIVATE_ENABLE)) {
			uint16_t val16;

			addr->addr.position.off = off |
			    ESC_ADDR_MAP_SYNCMANAGER_LENGTH;
			ec_datagram_read(addr, &val16, 2);
			if (val16 == 0) {
				nr = i;
				break;
			}
		}
	}

	return nr;
}

struct esc_mailbox *esc_mailbox_create(struct ec_dgram_addr *node,
    struct esc_syncmanager_info *rd, struct esc_syncmanager_info *wr)
{
	int sync_manager_write, sync_manager_read;
	size_t start_write, start_read;
	size_t len_write, len_read;
	struct esc_mailbox mb, *mailbox = NULL;
	uint8_t val8;
	uint16_t leval16;
	
	/* init write sync manager */
	if (!wr) {
		sync_manager_write = esc_syncmanager_claim(node);
		if (sync_manager_write < 0)
			return NULL;
		start_write = 0x1000;
		len_write = 0x0100;
	} else {
		sync_manager_write = wr->sm;
		start_write = wr->start;
		len_write = wr->len;
	}
	
	printf("Claiming syncmanager %d for mailbox write\n", sync_manager_write);
	
	/* mailbox write (master->slave) */	

	node->addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_write) |
	    ESC_ADDR_MAP_SYNCMANAGER_ACTIVATE;
	val8 = 0x00;
	ec_datagram_write(node, &val8, 1);
	
	node->addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_write) |
	    ESC_ADDR_MAP_SYNCMANAGER_PHYSICAL_START;
	leval16 = htole16(start_write);
	ec_datagram_write(node, &leval16, 2);
printf("write address: %04zx\n", start_write);
	node->addr.position.off =
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_write) | 
	    ESC_ADDR_MAP_SYNCMANAGER_LENGTH;
	leval16 = htole16(len_write);
	ec_datagram_write(node, &leval16, 2);
	
	node->addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_write) | 
	    ESC_ADDR_MAP_SYNCMANAGER_CONTROL;
	val8 = 
	    ESC_SYNCMANAGER_CONTROL_MAILBOX | 
	    ESC_SYNCMANAGER_CONTROL_WRITE |
	    ESC_SYNCMANAGER_CONTROL_INT_PDI;
printf("Write Control byte: 0x%02x\n", val8);
	ec_datagram_write(node, &val8, 1);

	node->addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_write) | 
	    ESC_ADDR_MAP_SYNCMANAGER_STATUS;
	ec_datagram_read(node, &val8, 1);
printf("Write status byte: 0x%02x\n", val8);
	
	node->addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_write) | 
	    ESC_ADDR_MAP_SYNCMANAGER_ACTIVATE;
	val8 = ESC_SYNCMANAGER_ACTIVATE_ENABLE;
	ec_datagram_write(node, &val8, 1);
 

	/* init read sync manager */ 
	if (!rd) {
		sync_manager_read = esc_syncmanager_claim(node);
		if (sync_manager_read < 0)
			return NULL;
		start_read = 0x1100;
		len_read = 0x0100;
	} else {
		sync_manager_read = rd->sm;
		start_read = rd->start;
		len_read = rd->len;
	}
	
	printf("Claiming syncmanager %d for mailbox read\n", sync_manager_read);
	
	/* mailbox read (slave->master)  */
	
	node->addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_read) | 
	    ESC_ADDR_MAP_SYNCMANAGER_ACTIVATE;
	val8 = 0x00;
	ec_datagram_write(node, &val8, 1);
	
	node->addr.position.off =
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_read) | 
	    ESC_ADDR_MAP_SYNCMANAGER_PHYSICAL_START;
	leval16 = htole16(start_read);
	ec_datagram_write(node, &leval16, 2);
printf("read address: %04zx\n", start_read);
	
	node->addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_read) | 
	    ESC_ADDR_MAP_SYNCMANAGER_LENGTH;
	leval16 = htole16(len_read);
	ec_datagram_write(node, &leval16, 2);
	
	node->addr.position.off =
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_read) | 
	    ESC_ADDR_MAP_SYNCMANAGER_CONTROL;
	val8 = 
	    ESC_SYNCMANAGER_CONTROL_MAILBOX | 
	    ESC_SYNCMANAGER_CONTROL_INT_PDI;
printf("Read Control byte: 0x%02x\n", val8);
	ec_datagram_write(node, &val8, 1);
	

	node->addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_read) | 
	    ESC_ADDR_MAP_SYNCMANAGER_STATUS;
	ec_datagram_read(node, &val8, 1);
printf("Read status byte: 0x%02x\n", val8);

	node->addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_read) | 
	    ESC_ADDR_MAP_SYNCMANAGER_ACTIVATE;
	val8 = ESC_SYNCMANAGER_ACTIVATE_ENABLE;
	ec_datagram_write(node, &val8, 1);

	memcpy(&mb.status_rd, node, sizeof(struct ec_dgram_addr));
	mb.status_rd.addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_read) | 
	    ESC_ADDR_MAP_SYNCMANAGER_STATUS;
	memcpy(&mb.status_wr, node, sizeof(struct ec_dgram_addr));
	mb.status_wr.addr.position.off = 
	    ESC_ADDR_MAP_SYNCMANAGER_NR(sync_manager_write) | 
	    ESC_ADDR_MAP_SYNCMANAGER_STATUS;
	mb.len_rd = len_read;
	memcpy(&mb.data_rd, node, sizeof(struct ec_dgram_addr));
	mb.data_rd.addr.position.off = start_read;
	mb.len_wr = len_write;
	memcpy(&mb.data_wr, node, sizeof(struct ec_dgram_addr));
	mb.data_wr.addr.position.off = start_write;
	
	mailbox = malloc(sizeof(struct esc_mailbox));
	if (mailbox) {
		memcpy(mailbox, &mb, sizeof(struct esc_mailbox));
		if (mailbox->len_rd) {
			mailbox->buffer_rd = malloc(mailbox->len_rd);
			if (!mailbox->buffer_rd)
				goto err_buffer_rd;
		} else {
			mailbox->buffer_rd = NULL;
		}
		if (mailbox->len_wr) {
			mailbox->buffer_wr = malloc(mailbox->len_wr);
			if (!mailbox->buffer_wr)
				goto err_buffer_wr;
		}
	}
	
	return mailbox;

err_buffer_wr:
	if (mailbox->buffer_rd)
		free(mailbox->buffer_rd);
err_buffer_rd:
	free(mailbox);
	return NULL;
}

void esc_mailbox_destroy(struct esc_mailbox *mailbox)
{
	/* todo: deactivate sync managers */
	
	free(mailbox->buffer_rd);
	free(mailbox->buffer_wr);
	free(mailbox);
}

ssize_t esc_mailbox_write(struct esc_mailbox *mailbox, int type,
    void *data, size_t len)
{
	struct esc_mailbox_hdr *hdr;
	uint8_t val8;
	int ret;
	
	if (mailbox->len_wr == 0) {
		printf("len_wr == 0\n");
		return -1;
	}
	if (mailbox->len_wr < sizeof(struct esc_mailbox_hdr) + len ) {
		printf("mailbox->len_wr to small: %d < %zu\n",
		    mailbox->len_wr, sizeof(struct esc_mailbox_hdr) + len);
		return -1;
	}
		
	hdr = (struct esc_mailbox_hdr *)mailbox->buffer_wr;
	hdr->length = htole16(len);
	hdr->address = htole16(0x0);
	hdr->chan_prio = 0;
	hdr->type_ctr = type | (mailbox->ctr << 4);
	memcpy(mailbox->buffer_wr + sizeof(struct esc_mailbox_hdr), data, len);

	mailbox->ctr++;
	
	/* wait till mailbox is empty */
	while (1) {
		ret = ec_datagram_write(&mailbox->data_wr, mailbox->buffer_wr,
		    mailbox->len_wr);
		if (ret > 0) {
			break;
		}
		do {
			ret = ec_datagram_read(&mailbox->status_wr, &val8,
			    sizeof(val8));
			if (ret != sizeof(val8)) {
				printf("could not read mailbox status\n");
				return -1;
			}
		} while (val8 & 0x08);
	}
	
	return len;
}

ssize_t esc_mailbox_read(struct esc_mailbox *mailbox, int *type,
    void *data, size_t len)
{
	struct esc_mailbox_hdr *hdr;
	int ret;
	size_t clen;
	uint8_t val8;
	
	if (mailbox->len_rd == 0) {
		printf("len_rd == 0\n");
		return -1;
	}
	if (mailbox->len_rd < sizeof(struct esc_mailbox_hdr) + len ) {
		printf("len_rd to short %d < %zu\n",
		    mailbox->len_rd, sizeof(struct esc_mailbox_hdr) + len);
		return -1;
	}
	
	hdr = (struct esc_mailbox_hdr *)mailbox->buffer_rd;

	/* wait till mailbox is full */
	while (1/*todo add timeout*/) {
		ret = ec_datagram_read(&mailbox->data_rd, mailbox->buffer_rd,
		    mailbox->len_rd);
		if (ret > 0) {
			break;
		}
		do {
			ret = ec_datagram_read(&mailbox->status_rd, &val8, sizeof(val8));
			if (ret != sizeof(val8)) {
				printf("Could not read mailbox status\n");
				return -1;
			}
		} while (!(val8 & 0x08));
	}

	clen = le16toh(hdr->length);
	if (clen > len)
		clen = len;
	
	memcpy(data, mailbox->buffer_rd + sizeof(struct esc_mailbox_hdr), clen);
	*type = hdr->type_ctr & 0x0f;

	return clen;
}


struct canopen_dev_priv {
	struct esc_mailbox *mbox;
};

static ssize_t esc_coe_send(struct canopen_dev *dev, void *req, size_t len)
{
	int ret;
	unsigned char coereq[10];
	
	memcpy(coereq + 2, req, len);
	coereq[0] = 0x00;
	coereq[1] = 0x20;
	
	ret = esc_mailbox_write(dev->priv->mbox, ESC_MAILBOX_TYPE_COE, coereq, len+2);

	if (ret > 2)
		return ret - 2;
	return ret;
}

static ssize_t esc_coe_recv(struct canopen_dev *dev, void *res, size_t len)
{
	int type;
	int ret;
	unsigned char coeres[10];
	
	ret = esc_mailbox_read(dev->priv->mbox, &type, coeres, len + 2);

	if (ret > 2) {
		memcpy(res, coeres + 2, ret - 2);
		return ret - 2;
	}
	return ret;
}


struct canopen_dev *esc_coe_create(struct esc_mailbox *mailbox)
{
	struct canopen_dev *dev;
	
	dev = malloc(sizeof(struct canopen_dev));
	if (!dev)
		goto err_malloc;
	dev->priv = malloc(sizeof(struct canopen_dev_priv));
	if (!dev->priv)
		goto err_priv;
	
	dev->priv->mbox = mailbox;
	dev->send_request = &esc_coe_send;
	dev->recv_response = &esc_coe_recv;

	return dev;

err_priv:
	free(dev);
err_malloc:
	return NULL;
}

void esc_coe_destroy(struct canopen_dev *coe)
{
	free(coe->priv);
	free(coe);
}

int esc_dc_init(struct ec_dgram_addr *addr)
{
	uint8_t val8;
	uint64_t leval64;
	struct timespec t;
	
	/* Configure distributed clock and sync0 (4ms cycle time) */
	addr->addr.position.off = ESC_ADDR_MAP_DC_CYCLIC_UNIT_CONTROL;
	val8 = 0x00;
	ec_datagram_write(addr, &val8, 1);

	addr->addr.position.off = ESC_ADDR_MAP_DC_ACTIVATION_REGISTER;
	val8 = 
	    ESC_DC_ACTIVATION_REGISTER_SYNC_OUT_ACTIVE |
	    ESC_DC_ACTIVATION_REGISTER_SYNC0_ACTIVE;
	ec_datagram_write(addr, &val8, 1);


	addr->addr.position.off = ESC_ADDR_MAP_DC_START_TIME_CYCLIC_OPERATION;
	clock_gettime(0, &t);
	leval64 = htole64((ESC_DC_EPOCH + (uint64_t)t.tv_sec) * 1000*1000*1000);
	ec_datagram_write(addr, &leval64, 8);

	return 0;
}

int esc_dc_sync0_cycle_time(struct ec_dgram_addr *addr, unsigned long ns)
{
	int r;
	uint32_t leval32;
	
	addr->addr.position.off = ESC_ADDR_MAP_DC_SYNC0_CYCLE_TIME;
	leval32 = htole32(ns);
	r = ec_datagram_write(addr, &leval32, 4);

	if (r == 4)
		return 0;
	else
		return -1;
}

int esc_pdo_tx_set(struct ec_dgram_addr *addr,
    struct esc_syncmanager_info *sm, unsigned char **data,
    bool watchdog)
{
	uint8_t val8;
	uint16_t leval16;

	addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGER_NR(sm->sm) |
	   ESC_ADDR_MAP_SYNCMANAGER_ACTIVATE;
	val8 = 0x00;
	ec_datagram_write(addr, &val8, 1);

	addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGER_NR(sm->sm) |
	    ESC_ADDR_MAP_SYNCMANAGER_PHYSICAL_START;
	leval16= htole16(sm->start);
	ec_datagram_write(addr, &leval16, 2);
	
	/* Length */
	addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGER_NR(sm->sm) |
	    ESC_ADDR_MAP_SYNCMANAGER_LENGTH;
	leval16 = htole16(sm->len);
	ec_datagram_write(addr, &leval16, 2);
	
	addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGER_NR(sm->sm) |
	    ESC_ADDR_MAP_SYNCMANAGER_CONTROL;
	val8 = 
	    ESC_SYNCMANAGER_CONTROL_WRITE |
	    ESC_SYNCMANAGER_CONTROL_INT_PDI;
	if (watchdog)
		val8 |= ESC_SYNCMANAGER_CONTROL_WATCHDOG_ENABLE;
printf("PDO tx Control byte: 0x%02x\n", val8);
	ec_datagram_write(addr, &val8, 1);
	

	if (sm->len) {
		addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGER_NR(sm->sm) |
		    ESC_ADDR_MAP_SYNCMANAGER_ACTIVATE;
		val8 = ESC_SYNCMANAGER_ACTIVATE_ENABLE;
		ec_datagram_write(addr, &val8, 1);

		if (data)
		{
			addr->addr.position.off = sm->start;
			ec_tx_pdo_add(addr, data, sm->len);
		}
	}

	return 0;
}

int esc_pdo_rx_set(struct ec_dgram_addr *addr,
    struct esc_syncmanager_info *sm, unsigned char **data)
{
	uint8_t val8;
	uint16_t leval16;
	
	addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGER_NR(sm->sm) |
	   ESC_ADDR_MAP_SYNCMANAGER_ACTIVATE;
	val8 = 0x00;
	ec_datagram_write(addr, &val8, 1);

	addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGER_NR(sm->sm) |
	    ESC_ADDR_MAP_SYNCMANAGER_PHYSICAL_START;
	leval16= htole16(sm->start);
	ec_datagram_write(addr, &leval16, 2);
	
	/* Length */
	addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGER_NR(sm->sm) |
	    ESC_ADDR_MAP_SYNCMANAGER_LENGTH;
	leval16 = htole16(sm->len);
	ec_datagram_write(addr, &leval16, 2);
	
	addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGER_NR(sm->sm) |
	    ESC_ADDR_MAP_SYNCMANAGER_CONTROL;
	val8 = ESC_SYNCMANAGER_CONTROL_INT_PDI;
printf("PDO rx Control byte: 0x%02x\n", val8);
	ec_datagram_write(addr, &val8, 1);
	
	if (sm->len) {
		addr->addr.position.off = ESC_ADDR_MAP_SYNCMANAGER_NR(sm->sm) |
		    ESC_ADDR_MAP_SYNCMANAGER_ACTIVATE;
		val8 = ESC_SYNCMANAGER_ACTIVATE_ENABLE;
		ec_datagram_write(addr, &val8, 1);

		addr->addr.position.off = sm->start;

		ec_rx_pdo_add(addr, data, sm->len);
	}
	
	return 0;
}