MX Foundation 4
mil1553_rt_rt.c
/*****************************************************************************
//
## File:
## mil1553_rt_rt.c
//
// Copyright (c) MAX Technologies Inc. 1988-2016, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example demonstrates the usage of simple aperiodic transmission
// to transmit a command of type RT-RT and record using BC.
// First RT is RT #5 and second RT is RT #7 for example. (RT5 SA1 -> RT7 SA1)
// Then we do an acquisition to be able to read the status of each RT and also
// the timetags between records.
// We enabled the RT for test purposes but if you have your RTs connected, you
// obviously don't need to.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#include "../../inc/example.h"
#define NUM_REC_TX 10
#define LOCAL
#define LOOPBACK
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_CHANNEL bc=0, rt5=0, rt7=0, bm = 0;
HMXF_BUFFER bcBufferTx=0;
HMXF_BUFFER bmBufferRx=0;
HMXF_BUFFER rt5Buffer=0, rt7Buffer=0;
uint32 txBufferSize;
MXF_MIL1553_DATAREC* txBuffer=NULL;
MXF_MIL1553_DATAREC* txRec1553;
uint32 rxBufferSize=0;
MXF_MIL1553_DATAREC* rxBuffer=NULL;
MXF_MIL1553_DATAREC* rxRec1553;
uint64 cmd;
uint64 rxAcqStatus;
uint64 msgCount;
uint64 byteCount;
uint64 rxRec;
uint64 address1, subAddress1, dir1, wordCount1;
uint64 address2=0, subAddress2=0, dir2, wordCount2;
char errorString[200];
// Connects to services and initialize environment
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
// Initializes MX Foundation library
if(!rc)
{
printf("Starting ...\n");
rc = mxfSystemInit(server);
}
//Gets handle of the first BM
if(!rc)
rc = mxfChannelGet(server, MXF_CLASS_MIL1553, MXF_SCLASS_BM_CHANNEL, MXF_MODULE_ALL, 0, &bm);
//Gets handle of the first BC
if(!rc)
rc = mxfChannelGet(server, MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, MXF_MODULE_ALL, 0, &bc);
// Gets handle of MIL1553 remote terminal 5 channel
if(!rc)
rc = mxfChannelGet(server, MXF_CLASS_MIL1553, MXF_SCLASS_RT_CHANNEL, MXF_MODULE_ALL, 5, &rt5);
// Gets handle of MIL1553 remote terminal 7 channel
if(!rc)
rc = mxfChannelGet(server, MXF_CLASS_MIL1553, MXF_SCLASS_RT_CHANNEL, MXF_MODULE_ALL, 7, &rt7);
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(bc, KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate enough buffer for at least NUM_REC_TX tx data
if(!rc)
{
txBufferSize = NUM_REC_TX * sizeof(MXF_MIL1553_DATAREC);
// Device allocation
rc = mxfTxAperiodicBufferAlloc(bc, MXF_TXAPERIODIC_PRIORITY_LOW, txBufferSize, &bcBufferTx, NULL);
// Host allocation
if(!rc)
{
txBuffer = (MXF_MIL1553_DATAREC*)malloc(txBufferSize);
if(!txBuffer)
rc = MAXT_ERROR_MEM;
}
// Necessary to enable the RTs
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt5, 0, txBufferSize, &rt5Buffer, NULL); // RT buffer for RT5 SA1 TX
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt7, 0, 0, &rt7Buffer, NULL); // RT buffer for RT7 SA1 RX
}
// Enables the different RTs
if(!rc)
rc = mxfMIL1553RtSubsystemEnableSet(rt5, MXF_MIL1553_MSGTYPE_TX, 1, MXF_MIL1553_BUS_A|MXF_MIL1553_BUS_B, rt5Buffer);
if(!rc)
rc = mxfMIL1553RtEnableSet(rt5, TRUE);
if(!rc)
rc = mxfMIL1553RtSubsystemEnableSet(rt7, MXF_MIL1553_MSGTYPE_RX, 1, MXF_MIL1553_BUS_A|MXF_MIL1553_BUS_B, rt7Buffer);
if(!rc)
rc = mxfMIL1553RtEnableSet(rt7, TRUE);
// Allocates 10KB buffer for rx data
if(!rc)
{
rxBufferSize = 10*1024;
// Device allocation
rc = mxfRxAcqBufferAlloc(bm, rxBufferSize, &bmBufferRx, NULL);
// Host allocation
if(!rc)
{
rxBuffer = (MXF_MIL1553_DATAREC*)malloc(rxBufferSize);
if(!rxBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Sets timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Starts BM acquisition
if(!rc)
rc = mxfRxAcqStart(bmBufferRx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Sends NUM_REC_TX commands
txRec1553 = txBuffer;
for(cmd=0; cmd<NUM_REC_TX; cmd++) //we send 10 times the same command
{
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
//Command #0
txRec1553->timeTag = 0;
txRec1553->repeatCount = 1;
txRec1553->control = MXF_MIL1553_TXAPERIODIC_REC_CTRL_RT_RT;
txRec1553->dataSize = 4; //4 bytes (2 commands)
mxfMIL1553CommandCompose(7, 1, MXF_MIL1553_MSGTYPE_RX, 0, &txRec1553->data[0]); //RT7 SA1
//Command #1
mxfMIL1553CommandCompose(5, 1, MXF_MIL1553_MSGTYPE_TX, 0, &txRec1553->data[1]); //RT5 SA1
mxfMIL1553NextDataRecordPtrGet(txRec1553, &txRec1553);
}
// Sends commands
printf("Transmitting...\n");
rc = mxfMIL1553TxAperiodicWrite(bcBufferTx, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, cmd, txBuffer);
// Reads and displays received messages for 5 seconds
if(!rc)
{
// Waits a little for commands to be transmitted
mxfSleep(1000);
printf("Reading...\n");
rc = mxfMIL1553RxAcqRead(bmBufferRx, 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxBuffer);
rxRec1553 = rxBuffer;
for(rxRec=0; rxRec<msgCount && !rc; rxRec++)
{
rc = mxfMIL1553DataRecordDecompose(bm, 1, rxRec1553, &msgInfo);
if(!rc)
{
rc = mxfMIL1553CommandDecompose(rxRec1553->data[1], &address1, &subAddress1, &dir1, &wordCount1);
if(!rc)
rc = mxfMIL1553CommandDecompose(rxRec1553->data[0], &address2, &subAddress2, &dir2, &wordCount2);
if(!rc)
{
printf("\n\r%llu:\t", rxRec1553->timeTag);
switch(msgInfo.msgType)
{
case MXF_MIL1553_MSGINFO_TYPE_RTRT:
printf("RT%llu SA%llu to RT%llu SA%llu WC%llu (0x%04x)\n\r", address1, subAddress1, address2, subAddress2, wordCount1, rxRec1553->data[0]);
if(msgInfo.statusIndex[0] != 0xffff)
printf("\t\tRT%llu SA%llu status: 0x%04x\n\r", address1, subAddress1, rxRec1553->data[msgInfo.statusIndex[0]]);
if(msgInfo.statusIndex[1] != 0xffff)
printf("\t\tRT%llu SA%llu status: 0x%04x\n\r", address2, subAddress2, rxRec1553->data[msgInfo.statusIndex[1]]);
break;
}
}
}
// Gets next msg
rc = mxfMIL1553NextDataRecordPtrGet(rxRec1553, &rxRec1553);
}
}
// Clears aperiodic buffer
if(!rc)
rc = mxfTxAperiodicClear(bcBufferTx, 0);
// Stops RTs
if(!rc)
rc = mxfMIL1553RtEnableSet(rt5, FALSE);
if(!rc)
rc = mxfMIL1553RtEnableSet(rt7, FALSE);
// Stops acquisition
if(!rc)
rc = mxfRxAcqStop(bmBufferRx);
// Clears acquisition
if(!rc)
rc = mxfRxAcqClear(bmBufferRx);
// Frees buffers
if(txBuffer)
free(txBuffer);
if(rxBuffer)
free(rxBuffer);
if(rc)
{
if(mxfSystemErrorStringGet(server, rc, sizeof(errorString), errorString))
sprintf (errorString,"ERROR # 0x%08X", rc);
printf("%s\n\r", errorString);
}
// Frees all buffers and terminate
if(bcBufferTx)
if(bmBufferRx)
mxfRxAcqBufferFree(bmBufferRx);
if(rt5Buffer)
if(rt7Buffer)
// Unloads MX Foundation library
printf("\n\rPress enter to terminate\n\r");
getchar();
return 0;
}
Updated 03/30/2022