Commit 728a6638 authored by julien.delange's avatar julien.delange
Browse files

import brmlib from gaisler to handle 1553 packets



git-svn-id: https://tecsw.estec.esa.int/svn/taste/trunk/po-hi-c@614 129961e7-ef38-4bb5-a8f7-c9a525a55882
parent fe10ce13
......@@ -2,6 +2,7 @@ AUTOMAKE_OPTIONS = no-dependencies
EXTRA_DIST = $(srcdir)/po_hi_driver_linux_serial.h \
$(srcdir)/po_hi_driver_rasta_spacewire.h \
$(srcdir)/po_hi_driver_rasta_1553.h \
$(srcdir)/po_hi_driver_rasta_1553_brmlib.h \
$(srcdir)/po_hi_driver_rasta_serial.h \
$(srcdir)/po_hi_driver_sockets.h \
$(srcdir)/po_hi_rtems_utils.h
......
#ifndef __PO_HI_DRIVER_RASTA_1553_BRM_LIB_H__
#define __PO_HI_DRIVER_RASTA_1553_BRM_LIB_H__
#include <deployment.h>
/* Generated code header */
#ifdef __PO_HI_NEED_DRIVER_1553_RASTA
/*
* This code has been greatly inspired by GAISLER examples.
*/
#include <bsp.h>
#include <rtems/libio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sched.h>
#include <ctype.h>
#include <rtems/bspIo.h>
#include <b1553brm.h>
typedef struct {
int fd;
int mode; /* defaults to RT */
int txblk;
int rxblk;
int broadcast;
} __po_hi_c_driver_rasta_1553_brm_s;
typedef __po_hi_c_driver_rasta_1553_brm_s *__po_hi_c_driver_rasta_1553_brm_t;
/*
* return file descriptor
*/
__po_hi_c_driver_rasta_1553_brm_t __po_hi_c_driver_1553_rasta_brmlib_open(char *driver_name);
void __po_hi_c_driver_1553_rasta_brmlib_close(__po_hi_c_driver_rasta_1553_brm_t 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 __po_hi_c_driver_1553_rasta_brmlib_rt_send(__po_hi_c_driver_rasta_1553_brm_t chan, struct rt_msg *msg);
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);
int __po_hi_c_driver_1553_rasta_brmlib_rt_recv(__po_hi_c_driver_rasta_1553_brm_t chan, struct rt_msg *msg);
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);
int __po_hi_c_driver_1553_rasta_brmlib_bm_recv(__po_hi_c_driver_rasta_1553_brm_t chan, struct bm_msg *msg);
/* start execute a command list */
int __po_hi_c_driver_1553_rasta_brmlib_bc_dolist(__po_hi_c_driver_rasta_1553_brm_t chan, struct bc_msg *msgs);
/* wait for command list the finish the execution */
int __po_hi_c_driver_1553_rasta_brmlib_bc_dolist_wait(__po_hi_c_driver_rasta_1553_brm_t chan);
/* mode = 0,1,2
* 0 = BC
* 1 = RT
* 2 = BM
*/
int __po_hi_c_driver_1553_rasta_brmlib_set_mode(__po_hi_c_driver_rasta_1553_brm_t chan, unsigned int mode);
/* bus=0,1,2,3
* 0 = none
* 1 = bus A
* 2 = bus B
* 3 = bus A and B
*/
int __po_hi_c_driver_1553_rasta_brmlib_set_bus(__po_hi_c_driver_rasta_1553_brm_t chan, unsigned int bus);
int __po_hi_c_driver_1553_rasta_brmlib_set_txblock(__po_hi_c_driver_rasta_1553_brm_t chan, int txblocking);
int __po_hi_c_driver_1553_rasta_brmlib_set_rxblock(__po_hi_c_driver_rasta_1553_brm_t chan, int rxblocking);
int __po_hi_c_driver_1553_rasta_brmlib_set_block(__po_hi_c_driver_rasta_1553_brm_t chan, int txblocking, int rxblocking);
int __po_hi_c_driver_1553_rasta_brmlib_set_broadcast(__po_hi_c_driver_rasta_1553_brm_t chan, int broadcast);
int __po_hi_c_driver_1553_rasta_brmlib_set_std(__po_hi_c_driver_rasta_1553_brm_t chan, int std);
int __po_hi_c_driver_1553_rasta_brmlib_set_rt_addr(__po_hi_c_driver_rasta_1553_brm_t chan, unsigned int address);
int __po_hi_c_driver_1553_rasta_brmlib_set_msg_timeout(__po_hi_c_driver_rasta_1553_brm_t chan, unsigned int timeout);
/* DEBUG FUNCTIONS */
void __po_hi_c_driver_rasta_1553_print_rt_msg(int i, struct rt_msg *msg);
void __po_hi_c_driver_rasta_1553_print_bm_msg(int i, struct bm_msg *msg);
#endif
#endif
......@@ -85,6 +85,7 @@ ifeq ($(NEED_TRANSPORT), yes)
drivers/po_hi_driver_sockets.o \
drivers/po_hi_driver_rasta_serial.o \
drivers/po_hi_driver_rasta_1553.o \
drivers/po_hi_driver_rasta_1553_brmlib.o \
drivers/po_hi_driver_rasta_spacewire.o \
$(ACTUAL_TRANSPORT) \
po_hi_transport.o $(TARGET_TRANSPORT_SOURCES)
......
AUTOMAKE_OPTIONS = no-dependencies
C_FILES = $(srcdir)/po_hi_driver_linux_serial.c \
$(srcdir)/po_hi_driver_sockets.c \
$(srcdir)/po_hi_driver_rasta_spacewire.c \
$(srcdir)/po_hi_driver_rasta_1553.c \
C_FILES = $(srcdir)/po_hi_driver_linux_serial.c \
$(srcdir)/po_hi_driver_sockets.c \
$(srcdir)/po_hi_driver_rasta_spacewire.c \
$(srcdir)/po_hi_driver_rasta_1553.c \
$(srcdir)/po_hi_driver_rasta_1553_brmlib.c \
$(srcdir)/po_hi_driver_rasta_serial.c
csrc = ${shell $(CYGPATH_U) '$(OCARINA_RUNTIME)/polyorb-hi-c/src/drivers'}
......
/*
* 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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment