MX Foundation 4
mil1553_aperiodic.c
/*****************************************************************************
//
## File:
## mil1553_aperiodic.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 commands with MIL-STD-1553 BC channel and record using BC.
// To record the messages with the BC, you need to connect RT0 to your hardware.
// Use hybrid mode if available.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#include "example.h"
#define NUM_REC_TX 10
#define LOCAL
//#define LOOPBACK
int main(void)
{
uint32 rc;
uint64 deviceCount;
uint64 moduleCount=0;
uint64 channelCount=0;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL bc=0;
HMXF_BUFFER bcBufferTx=0;
HMXF_BUFFER bcBufferRx=0;
uint32 txBufferSize;
MXF_MIL1553_DATAREC* txBuffer=NULL;
uint32 rxBufferSize=0;
MXF_MIL1553_DATAREC* rxBuffer=NULL;
uint64 card, mod, port;
uint64 cmd;
uint64 rxAcqStatus;
uint64 msgCount;
uint64 byteCount;
uint64 rxRec;
uint32 data;
uint64 options;
char errorString[200];
// Connect 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
// Initialize MX Foundation library
if(!rc)
{
printf("Starting ...\n");
rc = mxfSystemInit(server);
}
// Get handle of first MIL-STD-1553 Bus controller channel
if(!rc)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_ALL, 1, &deviceCount, &device);
// Try with MXF_MODULE_MIL1553MRT_EH and if not found, try with MXF_MODULE_MIL1553MRT
if(!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MIL1553MRT_EH, 1, &moduleCount, &module);
if (!rc && !moduleCount)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MIL1553MRT, 1, &moduleCount, &module);
if(!rc && moduleCount)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, 1, &channelCount, &bc);
// If module or channel not found, return an error
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
// Show channel locations for BC
rc = mxfChannelLocationGet(bc, &card, &mod, &port);
if (!rc)
printf("BC location is Device:%llu Module:%llu Port:%llu\n", card, mod, port);
// Use Hybrid Mode with RT0 set to 1553A if available
if (!rc)
{
rc = mxfAttributeUint64Get(module, KMXF_MIL1553_MODULE_OPTIONS, &options);
if(!rc && (options & VMXF_MIL1553_MODULE_OPTIONS_HYBRID_MODE))
{
rc = mxfAttributeUint64Set(bc, KMXF_MIL1553_MILSTD1553_REV, VMXF_MIL1553_MILSTD1553_REVHYBRID);
if (!rc)
rc = mxfMIL1553RtsHybridRevSet(bc, 0x00000001);
}
}
#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;
}
}
// Allocate 10KB buffer for rx data
if(!rc)
{
rxBufferSize = 10*1024;
// Device allocation
rc = mxfRxAcqBufferAlloc(bc, rxBufferSize, &bcBufferRx, NULL);
// Host allocation
if(!rc)
{
rxBuffer = (MXF_MIL1553_DATAREC*)malloc(rxBufferSize);
if(!rxBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Set timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Start BC acquisition
if(!rc)
rc = mxfRxAcqStart(bcBufferRx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Send NUM_REC_TX commands
txRec1553 = txBuffer;
for(cmd=0; cmd<NUM_REC_TX && !rc; cmd++)
{
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
// send two commands in alternance
if(cmd % 2)
{
txRec1553->timeTag = 0;
txRec1553->repeatCount = 1;
txRec1553->control = 0;
txRec1553->dataSize = 10; //10 bytes (command + 4 words)
mxfMIL1553CommandCompose(0, 3, MXF_MIL1553_MSGTYPE_RX, 4, &txRec1553->data[0]);
txRec1553->data[1] = 0x0000;
txRec1553->data[2] = 0x1111;
txRec1553->data[3] = 0x2222;
txRec1553->data[4] = 0x3333;
}
else
{
txRec1553->timeTag = 0;
txRec1553->repeatCount = 1;
txRec1553->dataSize = 10; //10 bytes (command + 4 words)
mxfMIL1553CommandCompose(0, 5, MXF_MIL1553_MSGTYPE_RX, 4, &txRec1553->data[0]);
txRec1553->data[1] = 0x3333;
txRec1553->data[2] = 0x2222;
txRec1553->data[3] = 0x1111;
txRec1553->data[4] = 0x0000;
}
mxfMIL1553NextDataRecordPtrGet(txRec1553, &txRec1553);
}
// Send commands
if (!rc)
rc = mxfMIL1553TxAperiodicWrite(bcBufferTx, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, cmd, txBuffer);
// Read and display received messages for 5 seconds
if(!rc)
{
// Wait a little for commands to be transmitted
mxfSleep(1000);
rc = mxfMIL1553RxAcqRead(bcBufferRx, 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxBuffer);
rxRec1553 = rxBuffer;
for(rxRec=0; rxRec<msgCount && !rc; rxRec++)
{
printf("\n\r%llu:\t", rxRec1553->timeTag);
printf("Control: 0x%08x", rxRec1553->control);
for(data=0; data<rxRec1553->dataSize/2; data++)
{
if(data && !(data%5))
printf("\n\r\t\t\t");
printf(" 0x%04x", rxRec1553->data[data]);
}
printf("\n\r");
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(rxRec1553, &rxRec1553);
}
}
// Clear aperiodic buffer
if(!rc)
rc = mxfTxAperiodicClear(bcBufferTx, 0);
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(bcBufferRx);
// Clear acquisition
if(!rc)
rc = mxfRxAcqClear(bcBufferRx);
// free 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);
}
// Free all buffers and terminate
if (device)
// Unload MX Foundation library
printf("\n\rPress enter to terminate\n\r");
getchar();
return 0;
}
Updated 09/17/2023