MX Foundation 4
mil1553_rt.c
/*****************************************************************************
//
## File:
## mil1553_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 basic usage of MIL-STD-1553 remote terminal.
// Can be run with mil1553_bc and mil1553_bm at the same time.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#include "example.h"
#define CMD_TX 0
#define CMD_RX 1
#define CMD_MC 2
#define LOCAL
int main(void)
{
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rt=0;
HMXF_BUFFER tx[3];
HMXF_BUFFER rx=0;
uint64 deviceCount=0;
uint64 moduleCount=0;
uint32 txBufferSize=0;
MXF_MIL1553_DATAREC* txBuffer=NULL;
uint32 rxBufferSize=0;
MXF_MIL1553_DATAREC* rxBuffer=NULL;
uint64 bus=0;
uint64 rxAcqStatus;
uint64 msgCount;
uint64 byteCount;
uint64 rxRec;
uint32 loop=0;
uint64 wordCount;
uint64 card, mod, port, indexBuffer;
uint32 data;
char errorString[200];
uint32 rc;
// Connect to MX Foundation library
#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 MIL-STD-1553 RT5 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 = MAXT_ERROR_NOT_FOUND;
if(!rc)
rc = mxfModuleChannelGet(module, 7, &rt);
if (!rc)
{
// Show channel location for RT
rc = mxfChannelLocationGet(rt, &card, &mod, &port);
if (!rc)
printf("RT location is Device:%llu Module:%llu Port:%llu\n", card, mod, port);
}
// Allocate 1KB buffer for tx data
if(!rc)
{
txBufferSize = 1024;
// RT 5: address=0, subaddress 3 TX
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt, CMD_TX, txBufferSize, &tx[CMD_TX], NULL);
// RT 5: address=0, subaddress 3 RX
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt, CMD_RX, txBufferSize, &tx[CMD_RX], NULL);
// RT 5: Mode Command 16
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt, CMD_MC, txBufferSize, &tx[CMD_MC], NULL);
// Host allocation
if(!rc)
{
txBuffer = (MXF_MIL1553_DATAREC*)malloc(txBufferSize);
if(!txBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocate buffer for Rx data
if(!rc)
{
rxBufferSize = 64*4096;
// Device allocation
rc = mxfRxAcqBufferAlloc(rt, rxBufferSize, &rx, 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);
// Enable the BUS A and B of RT 5 subaddress 3
if(!rc)
{
bus = MXF_MIL1553_BUS_A | MXF_MIL1553_BUS_B;
// TX
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_TX, 3, bus, tx[CMD_TX]);
// RX
if(!rc)
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_RX, 3, bus, tx[CMD_RX]);
}
// Enable the BUS A and B of RT 5 mode command 16
if(!rc)
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_MODECODE, 16, bus, tx[CMD_MC]);
// Set default data for address 5, subaddress 3
if(!rc)
{
txRec1553 = txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->dataSize = 34; //34 bytes (command + 16 words)
txRec1553->data[0] = 0x0000; //Not used
txRec1553->data[1] = 0xFFFF;
txRec1553->data[2] = 0xEEEE;
txRec1553->data[3] = 0xDDDD;
txRec1553->data[4] = 0xCCCC;
txRec1553->data[5] = 0xBBBB;
txRec1553->data[6] = 0xAAAA;
txRec1553->data[7] = 0x9999;
txRec1553->data[8] = 0x8888;
txRec1553->data[9] = 0x7777;
txRec1553->data[10] = 0x6666;
txRec1553->data[11] = 0x5555;
txRec1553->data[12] = 0x4444;
txRec1553->data[13] = 0x3333;
txRec1553->data[14] = 0x2222;
txRec1553->data[15] = 0x1111;
txRec1553->data[16] = 0x0000;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(tx[CMD_TX], 1, txBuffer);
}
// Start messages queues
if(!rc)
{
printf("Starting RT\n\r");
rc = mxfMIL1553RtEnableSet(rt, TRUE);
}
// Start RT acquisition
if(!rc)
rc = mxfRxAcqStart(rx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Read and display received messages
if(!rc)
{
uint64 addr, subAddr, dir;
do
{
rc = mxfMIL1553RxAcqRead(rx, 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxBuffer);
rxRec1553 = rxBuffer;
for(rxRec=0; rxRec<msgCount && !rc; rxRec++)
{
rc = mxfMIL1553DataRecordDecompose(rt, 1, rxRec1553, &msgInfo);
if(!rc)
{
rc = mxfMIL1553CommandDecompose(rxRec1553->data[0], &addr, &subAddr, &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", addr, subAddr, 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");
break;
case MXF_MIL1553_MSGINFO_TYPE_RTBC:
printf("RT%llu SA%llu WC%llu to BC (0x%04x)\n\r", addr, subAddr, wordCount, rxRec1553->data[0]);
break;
case MXF_MIL1553_MSGINFO_TYPE_MODECODE_TXDATA:
printf("BC Mode Command %llu to RT%llu SA%llu (0x%04x)\n\r", wordCount, addr, subAddr, rxRec1553->data[0]);
break;
}
}
}
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(rxRec1553, &rxRec1553);
}
mxfSleep(50);
loop++;
} while(loop < 200);
}
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(rx);
if (!rc)
rc = mxfRxAcqClear(rx);
// Stop RT
if(!rc)
rc = mxfMIL1553RtEnableSet(rt, FALSE);
printf("\nStopping RT\n\r");
// Disable the BUS A and B of RT 5 subaddress 3
if(!rc)
{
bus = 0;
// TX
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_TX, 3, bus, tx[CMD_TX]);
// RX
if(!rc)
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_RX, 3, bus, tx[CMD_RX]);
}
// Disable the BUS A and B of RT 5 mode command 16
if(!rc)
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_MODECODE, 16, bus, tx[CMD_MC]);
// 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
for (indexBuffer = 0; indexBuffer < 3; indexBuffer++)
{
if (tx[indexBuffer])
{
rc = mxfTxPeriodicUpdateMsgBufferFree(tx[indexBuffer]);
if (rc)
printf("Free buffer failed !\n\r");
}
}
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 other MX Foundation application running, only unload MX-Foundation library, otherwise reset cards first
}
printf("\n\rPress enter to terminate\n\r");
getchar();
return 0;
}
Updated 10/23/2023