esc_mailbox.c 15.6 KB
Newer Older
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), 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/>.

 */
/*
	esc_mailbox.c
	
	Code for communication via a mailbox
 */

#include <stdio.h>
#include <string.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
27
#include <time.h>
28

Jeroen Vreeken's avatar
Jeroen Vreeken committed
29
30
31
32
33
#include <ec/ec.h>
#include <ec/esc_mailbox.h>
#include <ec/esc_timeout.h>
#include <ec/esc_registers.h>
#include <log/log.h>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#define ESC_MAILBOX_TYPE_ERROR	0x0


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;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
49
50
	bool wr_error;
	bool rd_error;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
	void (*error_handler)(void *msg, size_t len);
};


struct esc_mailbox_hdr {
	uint16_t length;
	uint16_t address;
	uint8_t chan_prio;
	uint8_t type_ctr;
} __packed;

static void esc_error_handler_default(void *msg, size_t len)
{
	unsigned char *msgb = msg;
	int i;
	char msgstr[100 + len];
	int pos = 0;
	
	pos = sprintf(msgstr, "Mailbox error message:");
	for (i = 0; i < len; i++) {
		pos += sprintf(msgstr + pos, " 0x%02x", msgb[i]);
	}
73
	log_send(LOG_T_ERROR, "%s", msgstr);
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
}

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;
	}
	
	log_send(LOG_T_DEBUG, "Claiming syncmanager %d for mailbox write",
	    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);
	log_send(LOG_T_DEBUG, "write address: %04zx", 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;
	log_send(LOG_T_DEBUG, "Write Control byte: 0x%02x", 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);
	log_send(LOG_T_DEBUG, "Write status byte: 0x%02x", 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;
	}
	
	log_send(LOG_T_DEBUG, "Claiming syncmanager %d for mailbox read",
	    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);
	log_send(LOG_T_DEBUG, "read address: %04zx", 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;
	log_send(LOG_T_DEBUG, "Read Control byte: 0x%02x", 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);
	log_send(LOG_T_DEBUG, "Read status byte: 0x%02x", 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;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
219
	mailbox = calloc(1, sizeof(struct esc_mailbox));
220
221
222
223
224
225
226
227
228
229
230
231
232
233
	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;
		}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
234
235
		mailbox->wr_error = false;
		mailbox->rd_error = false;
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
		mailbox->error_handler = esc_error_handler_default;
	}
	
	return mailbox;

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

void esc_mailbox_reinit(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;
	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;
		start_write = 0x1000;
		len_write = 0x0100;
	} else {
		sync_manager_write = wr->sm;
		start_write = wr->start;
		len_write = wr->len;
	}
	
	log_send(LOG_T_DEBUG, "Claiming syncmanager %d for mailbox write",
	    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);
	log_send(LOG_T_DEBUG, "write address: %04zx", 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;
	log_send(LOG_T_DEBUG, "Write Control byte: 0x%02x", 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);
	log_send(LOG_T_DEBUG, "Write status byte: 0x%02x", 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;
		start_read = 0x1100;
		len_read = 0x0100;
	} else {
		sync_manager_read = rd->sm;
		start_read = rd->start;
		len_read = rd->len;
	}
	
	log_send(LOG_T_DEBUG, "Claiming syncmanager %d for mailbox read",
	    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);
	log_send(LOG_T_DEBUG, "read address: %04zx", 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;
	log_send(LOG_T_DEBUG, "Read Control byte: 0x%02x", 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);
	log_send(LOG_T_DEBUG, "Read status byte: 0x%02x", 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;
	
}

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 timespec *timeout)
{
	struct esc_mailbox_hdr *hdr;
	uint8_t val8;
	int ret;
	struct timespec t_start;
	
	if (mailbox->len_wr == 0) {
		log_send(LOG_T_ERROR, "len_wr == 0");
		return -1;
	}
	if (mailbox->len_wr < sizeof(struct esc_mailbox_hdr) + len ) {
		log_send(LOG_T_ERROR, "mailbox->len_wr to small: %d < %zu",
		    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 */
	clock_gettime(CLOCK_MONOTONIC, &t_start);
	do {
		ret = ec_datagram_read(&mailbox->status_wr, &val8,
		    sizeof(val8));
		if (ret != sizeof(val8)) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
436
437
438
439
440
			if (!mailbox->wr_error) {
				log_send(LOG_T_ERROR, 
				    "could not read write-mailbox status");
				mailbox->wr_error = true;
			}
441
442
443
444
445
446
447
448
449
			return -1;
		}
	} while ((val8 & 0x08) && !esc_timeout_expired(&t_start, timeout));

	if (!(val8 & 0x08)) {
		/* write mailbox contents */
		ret = ec_datagram_write(&mailbox->data_wr, mailbox->buffer_wr,
		    mailbox->len_wr);
		if (ret != mailbox->len_wr) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
450
451
452
453
454
			if (!mailbox->wr_error) {
				log_send(LOG_T_ERROR,
				   "could not write mailbox contents");
				mailbox->wr_error = true;
			}
455
456
457
458
459
460
461
			return -1;
		}
		ret = len;
	} else {
		ret = 0;
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
462
463
464
465
466
467
	if (mailbox->wr_error) {
		log_send(LOG_T_WARNING, "Mailbox is writeable again");
	}
	mailbox->wr_error = false;
	
	
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
	return ret;
}

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

	/* wait untill mailbox full */
	clock_gettime(CLOCK_MONOTONIC, &t_start);
	do {
		ret = ec_datagram_read(&mailbox->status_rd, &val8, sizeof(val8));
		if (ret != sizeof(val8)) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
497
498
499
500
501
			if (!mailbox->rd_error) {
				log_send(LOG_T_ERROR, 
				    "Could not read read-mailbox status");
				mailbox->rd_error = true;
			}
502
503
504
505
506
			return -1;
		}
	} while (!(val8 & 0x08) && !esc_timeout_expired(&t_start, timeout));

	if (!(val8 & 0x08)) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
507
508
		clen = 0;
		goto err_nodata;
509
510
511
512
513
514
	}

	/* read mailbox contents */
	ret = ec_datagram_read(&mailbox->data_rd, mailbox->buffer_rd,
	    mailbox->len_rd);
	if (ret != mailbox->len_rd) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
515
516
517
518
519
		if (!mailbox->rd_error) {
			log_send(LOG_T_ERROR,
			    "Could not read mailbox contents");
			mailbox->rd_error = true;
		}
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
		return -1;
	}

	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;
	
	if (*type == ESC_MAILBOX_TYPE_ERROR) {
		mailbox->error_handler(
		    mailbox->buffer_rd + sizeof(struct esc_mailbox_hdr),
		    hdr->length);
	}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
536
537
538
539
540
541
err_nodata:
	if (mailbox->rd_error) {
		log_send(LOG_T_WARNING, "Mailbox is readable again");
		mailbox->rd_error = false;
	}

542
543
544
	return clen;
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
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


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) {
		log_send(LOG_T_ERROR, "Could not read ESC information");
		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) {
			log_send(LOG_T_ERROR, 
			    "Could not read syncmanger%d activate register", 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;
}