MX Foundation 4
mil1553_bm.c
/*****************************************************************************
//
## File:
## mil1553_bm.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 basic usage of MIL-STD-1553 bus monitor.
// Can be run with mil1553_bc and mil1553_rt at the same time.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#include "../../inc/example.h"
#define LOCAL
#define LOOPBACK
int main(void)
{
uint32 rc;
uint64 deviceCount=0;
uint64 moduleCount=0;
uint64 channelCount=0;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL bm=0;
HMXF_BUFFER rx=0;
uint32 rxBufferSize=0;
MXF_MIL1553_DATAREC* rxBuffer=NULL;
uint64 rxAcqStatus;
uint64 msgCount;
uint64 byteCount;
uint64 rxRec;
uint32 loop=0;
uint64 address, subAddress, dir, wordCount;
uint64 card=0, mod=0, port=0;
uint32 data;
char errorString[200];
#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)
{
rc = mxfSystemInit(server);
if(rc == MAXT_ERROR_ANOTHER_PROCESS_RUNNING)
{
// if another MX Foundation application is running, only load MX-Foundation library, otherwise reset cards first
rc = mxfSystemResourcesInit(server, 0);
}
}
// Get handle of first Flex MIL-STD-1553 Bus monitor channel
if(!rc)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_ALL, 1, &deviceCount, &device);
if (!rc && deviceCount)
{
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_BM_CHANNEL, 1, &channelCount, &bm);
// If not found, return an error
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
// Diplay BM channel location
if(!rc)
rc = mxfChannelLocationGet(bm, &card, &mod, &port);
if (!rc)
printf("BM location is Device:%llu Module:%llu Port:%llu\n\r", card, mod, port);
#ifdef LOOPBACK
// Enable internal loopback
if(!rc)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate 100KB buffer for rx data
if(!rc)
{
rxBufferSize = 100*1024;
// Device allocation
rc = mxfRxAcqBufferAlloc(bm, rxBufferSize, &rx, NULL);
if(rc == MAXT_ERROR_BUFFER_ALLOCATED)
rc = mxfRxAcqBufferGet(bm, &rx);
// 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 BM acquisition
if(!rc)
{
printf("Starting BM\n\r");
rc = mxfRxAcqStart(rx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
}
// Read and display received messages
if(!rc)
{
do
{
rc = mxfMIL1553RxAcqRead(rx, 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxBuffer);
if(rc)
break;
rxRec1553 = (MXF_MIL1553_DATAREC*)rxBuffer;
for(rxRec=0; rxRec<msgCount && !rc; rxRec++)
{
rc = mxfMIL1553DataRecordDecompose(bm, 1, rxRec1553, &msgInfo);
if(!rc)
{
rc = mxfMIL1553CommandDecompose(rxRec1553->data[0], &address, &subAddress, &dir, &wordCount);
if(!rc)
{
printf("\n\r%llu:\t", rxRec1553->timeTag);
switch(msgInfo.msgType)
{
case MXF_MIL1553_MSGINFO_TYPE_BCRT:
printf("BC to RT%llu SA%llu WC%llu (0x%04x)\n\r", address, subAddress, wordCount, rxRec1553->data[0]);
printf("\t\tBC data:");
for(data=0; data<msgInfo.dataWordCount; data++)
{
if(data && !(data%4))
printf("\n\r\t\t\t");
printf(" 0x%04x", rxRec1553->data[msgInfo.dataIndex + data]);
}
printf("\n\r");
if(msgInfo.statusIndex[0] != 0xffff)
printf("\t\tRT status: 0x%04x\n\r", rxRec1553->data[msgInfo.statusIndex[0]]);
break;
case MXF_MIL1553_MSGINFO_TYPE_RTBC:
printf("RT%llu SA%llu WC%llu to BC (0x%04x)\n\r", address, subAddress, wordCount, rxRec1553->data[0]);
if(msgInfo.statusIndex[0] != 0xffff)
{
printf("\t\tRT status: 0x%04x\n\r", rxRec1553->data[msgInfo.statusIndex[0]]);
printf("\t\tRT data:");
for(data=0; data<msgInfo.dataWordCount; data++)
{
if(data && !(data%4))
printf("\n\r\t\t\t");
printf(" 0x%04x", rxRec1553->data[msgInfo.dataIndex + data]);
}
printf("\n\r");
}
break;
case MXF_MIL1553_MSGINFO_TYPE_MODECODE_TXDATA:
printf("BC Mode Command %llu to RT%llu SA%llu (0x%04x)\n\r", wordCount, address, subAddress, rxRec1553->data[0]);
if(msgInfo.statusIndex[0] != 0xffff)
{
printf("\t\tRT status: 0x%04x\n\r", rxRec1553->data[msgInfo.statusIndex[0]]);
printf("\t\tRT data: 0x%04x\n\r", rxRec1553->data[msgInfo.dataIndex]);
}
break;
}
}
}
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(rxRec1553, &rxRec1553);
}
mxfSleep(500);
loop++;
}while(loop < 200);
}
// Stop acquisition
if(!rc)
{
printf("Stopping BM\n\r");
rc = mxfRxAcqStop(rx);
}
// free buffers
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
if (rx)
{
if (rc)
printf("Free buffer failed !\n\r");
}
// Unload MX Foundation library
rc = mxfSystemTerminate(server);
if (rc == MAXT_ERROR_ANOTHER_PROCESS_RUNNING)
{
// if another MX Foundation application is running, only release the resource, otherwise reset everything
rc = mxfSystemResourcesRelease(server, 0);
}
printf("\n\rPress enter to terminate\n\r");
getchar();
return 0;
}
Updated 03/30/2022