po_hi_driver_rasta_1553_brmlib.c 11.7 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
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
/*
 * This is a part of PolyORB-HI-C distribution, a minimal
 * middleware written for generated code from AADL models.
 * You should use it with the Ocarina toolsuite.
 *
 * Copyright (C) 2010, European Space Agency
 */

/*
 * This code has been greatly inspired by GAISLER examples.
 */

#include <deployment.h>
/* Generated code header */

#ifdef __PO_HI_NEED_DRIVER_1553_RASTA

#include <rtems.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <b1553brm.h>
#include <drivers/po_hi_driver_rasta_1553_brmlib.h>

/* The stupid rtems name to errno table, in fact I hate it.... :)
 *
rtems_assoc_t errno_assoc[] = {
    { "OK",                 RTEMS_SUCCESSFUL,                0 },
    { "BUSY",               RTEMS_RESOURCE_IN_USE,           EBUSY },
    { "INVALID NAME",       RTEMS_INVALID_NAME,              EINVAL },
    { "NOT IMPLEMENTED",    RTEMS_NOT_IMPLEMENTED,           ENOSYS },
    { "TIMEOUT",            RTEMS_TIMEOUT,                   ETIMEDOUT },
    { "NO MEMORY",          RTEMS_NO_MEMORY,                 ENOMEM },
    { "NO DEVICE",          RTEMS_UNSATISFIED,               ENODEV },
    { "INVALID NUMBER",     RTEMS_INVALID_NUMBER,            EBADF},
    { "NOT RESOURCE OWNER", RTEMS_NOT_OWNER_OF_RESOURCE,     EPERM},
    { "IO ERROR",           RTEMS_IO_ERROR,                  EIO},
    { 0, 0, 0 },
};
*/

__po_hi_c_driver_rasta_1553_brm_t __po_hi_c_driver_1553_rasta_brmlib_open(char *driver_name)
{
	int fd;
	__po_hi_c_driver_rasta_1553_brm_t ret = NULL;
	
	printf("__po_hi_c_driver_1553_rasta_brmlib_open: Opening driver %s\n",driver_name);
	
	fd = open(driver_name,O_RDWR);
	if ( fd >= 0 ){
		printf("__po_hi_c_driver_1553_rasta_brmlib_open: allocating memory %d\n",sizeof(*ret));
		ret = calloc(sizeof(*ret),1);
		ret->fd = fd;
		/* Initial state of driver */
		ret->mode = BRM_MODE_RT;
	}else{
		if ( errno == ENODEV ){
			printf("__po_hi_c_driver_1553_rasta_brmlib_open: driver %s doesn't exist\n",driver_name);
		}else if ( errno == EBUSY ){
			printf("__po_hi_c_driver_1553_rasta_brmlib_open: channel already taken\n");
		}else{
			printf("__po_hi_c_driver_1553_rasta_brmlib_open: errno: %d, ret: %d\n",errno,fd);
		}
	}
	
	return ret;
}

void __po_hi_c_driver_1553_rasta_brmlib_close(__po_hi_c_driver_rasta_1553_brm_t chan){
	if ( !chan || (chan->fd<0) )
		return;
	close(chan->fd);
	free(chan);
}

int __po_hi_c_driver_1553_rasta_brmlib_rt_send_multiple(__po_hi_c_driver_rasta_1553_brm_t chan, struct rt_msg *msgs, int msgcnt){
	int ret;
	if ( !chan || !msgs || (msgcnt<0) )
		return -1;
	
	if ( msgcnt == 0 )
		return 0;
	
	ret = write(chan->fd,msgs,msgcnt);
	if ( ret < 0 ){
		/* something went wrong 
		 * OR in non-blocking mode
		 * that would block
		 */
		if ( !chan->txblk && (errno == EBUSY) ){
			/* would block ==> 0 sent is ok */
			return 0;
		}
			
		if ( errno == EINVAL ){
			/* CAN must be started before receiving */
			printf("__po_hi_c_driver_1553_rasta_brmlib_rt_send_multiple: input descriptor numbering error\n");
			return -1;
		}

		printf("__po_hi_c_driver_1553_rasta_brmlib_send_multiple: error in write, errno: %d, returned: %d\n",errno,ret);
		return -1;
	}
	
	/* sent all of them */
	return ret;
}

int __po_hi_c_driver_1553_rasta_brmlib_rt_send(__po_hi_c_driver_rasta_1553_brm_t chan, struct rt_msg *msg){
	return __po_hi_c_driver_1553_rasta_brmlib_rt_send_multiple(chan,msg,1);
}

int __po_hi_c_driver_1553_rasta_brmlib_recv_multiple(__po_hi_c_driver_rasta_1553_brm_t chan, void *msgs, int msglen){
	int ret;
	
	if ( !chan || !msgs || (msglen<0) )
		return -1;
	
	if ( msglen == 0 )
		return 0;
	
	errno = 0;
	ret = read(chan->fd,msgs,msglen);
	if ( ret < 0 ){
		/* something went wrong 
		 * OR in non-blocking mode
		 * that would block
		 */
		if ( !chan->rxblk && (errno == EBUSY) ){
			return 0;
		}
		
		printf("__po_hi_c_driver_1553_rasta_brmlib_recv_multiple: error in read, errno: %d, returned: %d\n",errno,ret);
		return -1;
	}
	
	/* message count is returned, not byte count */
	return ret;
}

int __po_hi_c_driver_1553_rasta_brmlib_rt_recv_multiple(__po_hi_c_driver_rasta_1553_brm_t chan, struct rt_msg *msgs, int msgcnt)
{
	if ( !chan || (chan->mode!=BRM_MODE_RT) )
		return -1;
	
	/* Read the messages */
	return __po_hi_c_driver_1553_rasta_brmlib_recv_multiple(chan,(void *)msgs,msgcnt);
}

int __po_hi_c_driver_1553_rasta_brmlib_bm_recv_multiple(__po_hi_c_driver_rasta_1553_brm_t chan, struct bm_msg *msgs, int msgcnt)
{
	if ( !chan || (chan->mode!=BRM_MODE_BM) )
		return -1;
	
	/* Read the messages */
	return __po_hi_c_driver_1553_rasta_brmlib_recv_multiple(chan,(void *)msgs,msgcnt);
}

int __po_hi_c_driver_1553_rasta_brmlib_rt_recv(__po_hi_c_driver_rasta_1553_brm_t chan, struct rt_msg *msg){
	return __po_hi_c_driver_1553_rasta_brmlib_rt_recv_multiple(chan,msg,1);
}

int __po_hi_c_driver_1553_rasta_brmlib_bm_recv(__po_hi_c_driver_rasta_1553_brm_t chan, struct bm_msg *msg){
	return __po_hi_c_driver_1553_rasta_brmlib_bm_recv_multiple(chan,msg,1);
}


int __po_hi_c_driver_1553_rasta_brmlib_set_mode(__po_hi_c_driver_rasta_1553_brm_t chan, unsigned int mode){
	int ret;
	unsigned int arg = mode;
	
	if ( !chan )
		return -1;
	
	ret = ioctl(chan->fd,BRM_SET_MODE,&arg);
	if ( ret < 0 ){
				
		if ( errno == EINVAL ){
			printf("__po_hi_c_driver_1553_rasta_brmlib_set_mode: invalid mode: %d\n",arg);
			return -2;
		}
	
		if ( errno == ENOMEM ){
			/* started */
			printf("__po_hi_c_driver_1553_rasta_brmlib_set_mode: not enough memory\n");
			return -3;
		}
		
		/* unhandeled errors */
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_mode: failed, errno: %d, ret: %d\n",errno,ret);
		return -1;
	}
	
	/* Save mode */
	chan->mode = mode;
	
	return 0;
}

int __po_hi_c_driver_1553_rasta_brmlib_set_bus(__po_hi_c_driver_rasta_1553_brm_t chan, unsigned int bus){
	int ret;
	unsigned int arg = bus;
	if ( !chan )
		return -1;
		
	/* only for RT mode */
	if ( chan->mode != BRM_MODE_RT ){
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_bus: Only possible to set bus in RT mode\n");
		return -2; /* fast EINVAL... */
	}
	
	ret = ioctl(chan->fd,BRM_SET_BUS,&arg);
	if ( ret < 0 ){
				
		if ( errno == EINVAL ){
			printf("__po_hi_c_driver_1553_rasta_brmlib_set_bus: invalid bus: %d\n",arg);
			return -2;
		}
		
		/* unhandeled errors */
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_bus: failed, errno: %d, ret: %d\n",errno,ret);
		return -1;
	}
	return 0;
}

int __po_hi_c_driver_1553_rasta_brmlib_set_msg_timeout(__po_hi_c_driver_rasta_1553_brm_t chan, unsigned int timeout){
	int ret;
	unsigned int arg = timeout;
	
	if ( !chan )
		return -1;
	
	if ( !((chan->mode==BRM_MODE_BM) || (chan->mode==BRM_MODE_BC)) ){
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_msg_timeout: Only possible to set bus in BC & BM mode\n");
		return -2;
	}
	
	ret = ioctl(chan->fd,BRM_SET_MSGTO,&arg);
	if ( ret < 0 ){
		if ( errno == EBUSY ){
			/* started */
			printf("__po_hi_c_driver_1553_rasta_brmlib_set_msg_timeout: started\n");
			return -2;
		}
		
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_msg_timeout: failed, errno: %d, ret: %d\n",errno,ret);
		return -1;
	}
	
	return 0;
}

int __po_hi_c_driver_1553_rasta_brmlib_set_rt_addr(__po_hi_c_driver_rasta_1553_brm_t chan, unsigned int address){
	int ret;
	unsigned int arg = address;
	
	if ( !chan )
		return -1;
	
	if ( chan->mode != BRM_MODE_RT ){
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_rt_addr: not in RT mode\n");
		return -2;
	}
	
	ret = ioctl(chan->fd,BRM_SET_RT_ADDR,&arg);
	if ( ret < 0 ){
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_rt_addr: failed, errno: %d, ret: %d\n",errno,ret);
		return -1;
	}
	
	return 0;
}

int __po_hi_c_driver_1553_rasta_brmlib_set_std(__po_hi_c_driver_rasta_1553_brm_t chan, int std){
	int ret;
	unsigned int arg = std;
	
	if ( !chan )
		return -1;
	
	ret = ioctl(chan->fd,BRM_SET_STD,&arg);
	if ( ret < 0 ){
	
		if ( errno == EINVAL ){
			/* started */
			printf("__po_hi_c_driver_1553_rasta_brmlib_set_std: new standard not valid: %d\n",arg);
			return -2;
		}
		
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_filter: failed, errno: %d, ret: %d\n",errno,ret);
		return -1;
	}
	
	return 0;
}


int __po_hi_c_driver_1553_rasta_brmlib_set_txblock(__po_hi_c_driver_rasta_1553_brm_t chan, int txblocking){
	unsigned int arg = (txblocking) ? 1 : 0;
	int ret;
	
	if ( !chan )
		return -1;
	
	ret = ioctl(chan->fd,BRM_TX_BLOCK,&arg);
	if ( ret < 0 ){
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_txblock: failed, errno: %d, ret: %d\n",errno,ret);
		return -1;
	}
	/* remember blocking state */
	chan->txblk = arg;
	return 0;
}


int __po_hi_c_driver_1553_rasta_brmlib_set_rxblock(__po_hi_c_driver_rasta_1553_brm_t chan, int rxblocking){
	unsigned int arg = (rxblocking) ? 1 : 0;
	int ret;
	
	if ( !chan )
		return -1;
	
	ret = ioctl(chan->fd,BRM_RX_BLOCK,&arg);
	if ( ret < 0 ){
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_rxblock: failed, errno: %d, ret: %d\n",errno,ret);
		return -1;
	}
	/* remember blocking state */
	chan->rxblk = arg;
	return 0;
}

int __po_hi_c_driver_1553_rasta_brmlib_set_block(__po_hi_c_driver_rasta_1553_brm_t chan, int txblocking, int rxblocking){
	int ret;
	ret = __po_hi_c_driver_1553_rasta_brmlib_set_txblock(chan,txblocking);
	if ( !ret ){
		return __po_hi_c_driver_1553_rasta_brmlib_set_rxblock(chan,rxblocking);
	}
	return ret;
}

int __po_hi_c_driver_1553_rasta_brmlib_set_broadcast(__po_hi_c_driver_rasta_1553_brm_t chan, int broadcast){
	unsigned int arg = (broadcast) ? 1 : 0;
	int ret;
	
	if ( !chan )
		return -1;
	
	ret = ioctl(chan->fd,BRM_SET_BCE,&arg);
	if ( ret < 0 ){
		printf("__po_hi_c_driver_1553_rasta_brmlib_set_broadcast: failed, errno: %d, ret: %d\n",errno,ret);
		return -1;
	}
	
	/* remember broadcast state */
	chan->broadcast = arg;
	return 0;
}

int __po_hi_c_driver_1553_rasta_brmlib_bc_dolist(__po_hi_c_driver_rasta_1553_brm_t chan, struct bc_msg *msgs){
	int ret;
	
	ret = ioctl(chan->fd, BRM_DO_LIST, msgs);
	if ( ret < 0 ){
		if ( errno == EINVAL ){
			printf("__po_hi_c_driver_1553_rasta_brmlib_bc_dolist: not in BC mode\n");
			return -2;
		}

		if ( errno == EBUSY ){
			printf("__po_hi_c_driver_1553_rasta_brmlib_bc_dolist: busy\n");
			return -3;
		}
		
		printf("__po_hi_c_driver_1553_rasta_brmlib_bc_dolist: errno %d, ret: %d\n",errno,ret);
		return -1;
	}
	return 0;
}

int __po_hi_c_driver_1553_rasta_brmlib_bc_dolist_wait(__po_hi_c_driver_rasta_1553_brm_t chan){
	int ret;
	unsigned int result;
	
	ret = ioctl(chan->fd, BRM_LIST_DONE, &result);
	if ( ret < 0 ){
		if ( errno == EINVAL ){
			printf("__po_hi_c_driver_1553_rasta_brmlib_bc_dolist: not in BC mode\n");
			return -2;
		}

		if ( errno == EBUSY ){
			printf("__po_hi_c_driver_1553_rasta_brmlib_bc_dolist: busy\n");
			return -3;
		}
		
		printf("__po_hi_c_driver_1553_rasta_brmlib_bc_dolist: errno %d, ret: %d\n",errno,ret);
		return -1;
	}
	
	return result;
}

void __po_hi_c_driver_rasta_1553_print_rt_msg(int i, struct rt_msg *msg){
	int k, wc;
	wc = msg->miw >> 11;
	printf("MSG[%d]: miw: 0x%x, time: 0x%x, desc: 0x%x, len: %d\n  ",i,msg->miw,msg->time,msg->desc,wc);
	/* print data */			
	for (k = 0; k < wc; k++){
		if ( isalnum(msg->data[k]) ){
  		printf("0x%x (%c)",msg->data[k],msg->data[k]);
		}else{
			printf("0x%x (.)",msg->data[k]);
		}
	}
	if ( k > 0 )
		printf("\n");
}

void __po_hi_c_driver_rasta_1553_print_bm_msg(int i, struct bm_msg *msg){
	int k,wc,tr,desc;
	wc = msg->cw1 & 0x1f;	
	desc = (msg->cw1 >> 5) & 0x1f;
	tr = msg->cw1 & 0x0400;

	printf("MSG[%d]: miw: 0x%x, cw1: 0x%x, cw2: 0x%x, desc: %d\n",i,msg->miw,msg->cw1,msg->cw2,desc);
	printf("         sw1: 0x%x, sw2: 0x%x, tim: 0x%x, len: %d\n",msg->sw1,msg->sw2,msg->time,wc);
	
	/* no message data in BC transmit commands */
	if ( tr )
		return;
	
	printf("         ");
	
	/* print data */			
	for (k = 0; k<wc; k++){
		if ( isalnum(msg->data[k]) ){
  		printf("0x%x (%c)",msg->data[k],msg->data[k]);
		}else{
			printf("0x%x (.)",msg->data[k]);
		}
	}
	if ( k > 0 )
		printf("\n");
}

#endif