MX Foundation 4
mil1553_ebr.cs
/*****************************************************************************
//
## File:
## mil1553_ebr.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example demonstrates the configuration of MIL-STD-1553 channel
// for EBR operation, the usage of NO-OP, command update and repeat
// count functionality. Acquisition mode is set to linear.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti-1553 (FM1553-4).
//
*****************************************************************************/
//#define LOCAL
#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_ebr
{
private const UInt64 BUF_SA3_RX = 0;
private const UInt64 BUF_SA3_TX = 1;
private const UInt64 BUF_SA3_MC = 2;
private const UInt64 BUF_SA5_RX = 3;
private const UInt64 RT_ADRS = 0;
private const UInt64 M_ADDRESS = 5;
private const UInt64 M_SUBADDR = 3;
static void Main(string[] args)
{
UInt32 rc;
UInt64 deviceCount = 0;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt64 server;
var device = new UInt64[1];
var module = new UInt64[2];
var bc = new UInt64[1];
UInt64 rt = 0;
UInt64 bm = 0;
var bcBufferTx = new UInt64[4];
UInt64 bcBufferRx = 0;
var rtBuffer = new UInt64[4];
MXF_MSGID_MIL1553[] bcMsgID =
{
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_RX, address = RT_ADRS, subAddress = 3 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_TX, address = RT_ADRS, subAddress = 3 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_MODECODE, address = RT_ADRS, subAddress = 16 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_RX, address = RT_ADRS, subAddress = 5 }
};
MXF_MSGID_MIL1553[] rtMsgID =
{
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_RX, address = 0, subAddress = 3 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_TX, address = 0, subAddress = 3 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_MODECODE, address = 0, subAddress = 16 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_RX, address = 0, subAddress = 5 }
};
UInt32 txBufferSize = 0;
IntPtr txBuffer = IntPtr.Zero;
txRec1553.data = new UInt16[36];
UInt32 rxBufferSize = 0;
IntPtr rxBuffer = IntPtr.Zero;
var rxRec1553 = new MXF_MIL1553_DATAREC[1];
IntPtr recPtr = IntPtr.Zero;
var minorFrame = new MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG[3];
UInt64 bus = MXF_MIL1553_BUS_A | MXF_MIL1553_BUS_B;
UInt64 rxAcqStatus = 0;
UInt64 count = 0, msgCount = 0, status = 0, pendingBytes = 0;
UInt64 byteCount = 0;
UInt64 rxRec;
UInt32 loop = 0;
UInt64 address, subAddress, dir, wordCount, indexBuffer;
UInt32 data;
UInt32 msg;
var msgInfo = new MXF_MIL1553_MSGINFO[1];
var errorString = new StringBuilder(200);
#if LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", Convert.ToUInt64(false), out server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", Convert.ToUInt64(false), out server);
#endif
// Initialize MX Foundation library
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting ...\n\r");
rc = mxfSystemInit(server);
}
//Get a FM1553-4 or FX1553-4 device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_FLEXMULTI_1553, 1, out deviceCount, device);
if (rc == MAXT_SUCCESS && deviceCount == 0)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_FLEX1553_PCIE, 1, out deviceCount, device);
// Get a MIL1553 module handle
if (rc == MAXT_SUCCESS && deviceCount != 0)
{
rc = mxfDeviceModuleAllGet(device[0], MXF_MODULE_MIL1553MRT_EH, 2, out moduleCount, module);
//If not a FM1553-4 or FX1553-4 , return an error
if (moduleCount != 2)
{
Console.Write("This example only works for FM1553-4 or FX1553-4\n");
rc = MAXT_ERROR_MODEL;
}
}
//If no device found, return an error
else if (rc == MAXT_SUCCESS && deviceCount == 0)
rc = MAXT_ERROR_MODEL;
if (rc == MAXT_SUCCESS && moduleCount > 0)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, 1, out channelCount, bc);
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Get handle of first Flex MIL-STD-1553 remote terminal channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 2 + RT_ADRS, out rt);
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 0, out bm);
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(bc[0], KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate 1KB buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = 1024;
// Flex allocation
for (msg = 0; msg < 4 && rc == MAXT_SUCCESS; msg++)
{
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc[0], msg, txBufferSize, out bcBufferTx[msg], IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt, msg, txBufferSize, out rtBuffer[msg], IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt, rtMsgID[msg].type, rtMsgID[msg].subAddress, bus, rtBuffer[msg]);
}
// Host allocation5
if (rc == MAXT_SUCCESS)
{
try
{
txBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Allocate 10KB buffer for rx data
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 1 * 1024;
// Flex allocation
rc = mxfRxAcqBufferAlloc(bc[0], rxBufferSize, out bcBufferRx, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferStatusGet(bcBufferRx, out status, out count, out pendingBytes, out msgCount);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
rxBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Configure to EBR
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_INTERFACE, VMXF_MIL1553_INTERFACE_RS485);
// Set Timebase to RTC nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Configure NO-OP error injection
if (rc == MAXT_SUCCESS)
{
err.errors = new UInt16[34];
err.errors[0] = MXF_MIL1553_ERRORID_NOOP;
rc = mxfMIL1553ErrorInjectionSet(bc[0], 0, ref err);
}
// Set the minor frame #0 using 3 Commands
if (rc == MAXT_SUCCESS)
{
// Command #0 : Address 0, Subaddress 3, RX, 16 words
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA3_RX].address, bcMsgID[BUF_SA3_RX].subAddress, bcMsgID[BUF_SA3_RX].type, 16, out minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[BUF_SA3_RX];
// Command #1 : Address 0, Subaddress 3, TX, 16 words
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA3_TX].address, bcMsgID[BUF_SA3_TX].subAddress, bcMsgID[BUF_SA3_TX].type, 16, out minorFrame[1].command);
minorFrame[1].modulo = 1;
minorFrame[1].buffer = bcBufferTx[BUF_SA3_TX];
}
// Command #2 : Address 0, Mode Command 16, TX
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA3_MC].address, 0, MXF_MIL1553_MSGTYPE_TX, bcMsgID[BUF_SA3_MC].subAddress, out minorFrame[2].command);
minorFrame[2].modulo = 1;
minorFrame[2].buffer = bcBufferTx[BUF_SA3_MC];
}
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc[0], 0, 0, 3, minorFrame, IntPtr.Zero);
}
// Set the minor frame #1 using 1 Command, Repeat count of 2
if (rc == MAXT_SUCCESS)
{
minorFrame[0] = new MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG();
mfProp.modulo = 1;
mfProp.repeatCount = 2;
// Command #0 : Address 0, Subaddress 5, RX, 4 words
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA5_RX].address, bcMsgID[BUF_SA5_RX].subAddress, bcMsgID[BUF_SA5_RX].type, 4, out minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[BUF_SA5_RX];
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc[0], 0, 1, 1, minorFrame, ref mfProp);
}
recPtr = txBuffer;
// Set BC data for address 0, subaddress 3, RX
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 2; // repeat count of 2
txRec1553.dataSize = 34; // 34 bytes (command + 16 words)
txRec1553.data[0] = 0x0000; // Not used
txRec1553.data[1] = 0x0000;
txRec1553.data[2] = 0x1111;
txRec1553.data[3] = 0x2222;
txRec1553.data[4] = 0x3333;
txRec1553.data[5] = 0x4444;
txRec1553.data[6] = 0x5555;
txRec1553.data[7] = 0x6666;
txRec1553.data[8] = 0x7777;
txRec1553.data[9] = 0x8888;
txRec1553.data[10] = 0x9999;
txRec1553.data[11] = 0xAAAA;
txRec1553.data[12] = 0xBBBB;
txRec1553.data[13] = 0xCCCC;
txRec1553.data[14] = 0xDDDD;
txRec1553.data[15] = 0xEEEE;
txRec1553.data[16] = 0xFFFF;
Marshal.StructureToPtr(txRec1553, recPtr, true);
mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
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;
Marshal.StructureToPtr(txRec1553, recPtr, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[BUF_SA3_RX], 2, txBuffer);
}
// Set BC data for address 0, subaddress 3, TX
if (rc == MAXT_SUCCESS)
{
recPtr = txBuffer;
txRec1553.repeatCount = 1;
txRec1553.dataSize = 0;
for (int i = 0; i < 36; i++)
txRec1553.data[i] = 0;
Marshal.StructureToPtr(txRec1553, recPtr, true);
mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 0; // NO-OP
Marshal.StructureToPtr(txRec1553, recPtr, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[BUF_SA3_TX], 2, txBuffer);
}
// Set BC data for address 0, MC 16, TX
if (rc == MAXT_SUCCESS)
{
recPtr = txBuffer;
txRec1553.repeatCount = 1;
txRec1553.dataSize = 0;
txRec1553.control = 0;
for (int i = 0; i < 36; i++)
txRec1553.data[i] = 0;
Marshal.StructureToPtr(txRec1553, recPtr, true);
mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
// Update command to address 5, MC 18, TX. RT will not respond because this address is not enabled
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_UPDATE_COMMAND;
mxfMIL1553CommandCompose(5, 0, MXF_MIL1553_MSGTYPE_TX, 18, out txRec1553.data[0]);
Marshal.StructureToPtr(txRec1553, recPtr, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[BUF_SA3_MC], 2, txBuffer);
}
// Set BC data for address 0, subaddress 5, RX
if (rc == MAXT_SUCCESS)
{
recPtr = txBuffer;
txRec1553.repeatCount = 2; // repeat count of 2
txRec1553.dataSize = 34; //10 bytes (command + 4 words)
txRec1553.control = 0;
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = 0x1234;
txRec1553.data[2] = 0x5678;
txRec1553.data[3] = 0x9ABC;
txRec1553.data[4] = 0xDEF0;
Marshal.StructureToPtr(txRec1553, recPtr, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[BUF_SA5_RX], 1, txBuffer);
}
// Start messages queues
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting BC and RT\n\r");
rc = mxfMIL1553RtEnableSet(rt, VMXF_ENABLE);
}
// Start BC acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(bcBufferRx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Start the major frame with 250 msec rate
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameStart(bc[0], 0, 250000000, IntPtr.Zero);
// Read and display received messages
if (rc == MAXT_SUCCESS)
{
do
{
rc = mxfMIL1553RxAcqRead(bcBufferRx, 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxBuffer);
recPtr = rxBuffer;
for (rxRec = 0; rxRec < msgCount && rc == MAXT_SUCCESS; rxRec++)
{
rxRec1553[0] = (MXF_MIL1553_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_MIL1553_DATAREC));
rc = mxfMIL1553DataRecordDecompose(bc[0], 1, rxRec1553, msgInfo);
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandDecompose(rxRec1553[0].data[0], out address, out subAddress, out dir, out wordCount);
if (rc == MAXT_SUCCESS)
{
Console.Write("\n\r{0}:\t", rxRec1553[0].timeTag);
switch (msgInfo[0].msgType)
{
case MXF_MIL1553_MSGINFO_TYPE_BCRT:
Console.Write("BC to RT{0} SA{1} WC{2} (0x{3:x4})\n\r", address, subAddress, wordCount, rxRec1553[0].data[0]);
Console.Write("\t\tRT status: 0x{0:x4}\n\r", rxRec1553[0].data[msgInfo[0].statusIndex[0]]);
break;
case MXF_MIL1553_MSGINFO_TYPE_RTBC:
Console.Write("RT{0} SA{1} WC{2} to BC (0x{3:x4})\n\r", address, subAddress, wordCount, rxRec1553[0].data[0]);
Console.Write("\t\tRT status: 0x{0:x4}\n\r", rxRec1553[0].data[msgInfo[0].statusIndex[0]]);
Console.Write("\t\tRT data:");
for (data = 0; data < msgInfo[0].dataWordCount; data++)
{
if (data > 0 && (data % 4) == 0)
Console.Write("\n\r\t\t\t");
Console.Write(" 0x{0:x4}", rxRec1553[0].data[msgInfo[0].dataIndex + data]);
}
Console.Write("\n\r");
break;
case MXF_MIL1553_MSGINFO_TYPE_MODECODE_TXDATA:
Console.Write("BC Mode Command {0} to RT{1} SA{2} (0x{3:x4})\n\r", wordCount, address, subAddress, rxRec1553[0].data[0]);
if (msgInfo[0].statusIndex[0] != 0xffff)
{
Console.Write("\t\tRT status: 0x{0:x4}\n\r", rxRec1553[0].data[msgInfo[0].statusIndex[0]]);
Console.Write("\t\tRT data: 0x{0:x4}\n\r", rxRec1553[0].data[msgInfo[0].dataIndex]);
}
break;
}
}
}
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
mxfSleep(500);
loop++;
} while (loop < 5);
}
// Stop acquisition
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqStop(bcBufferRx);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqClear(bcBufferRx);
}
// Stop the major frame
if (rc == MAXT_SUCCESS)
{
Console.Write("Stopping BC and RT\n\r");
rc = mxfTxPeriodicMajorFrameStop(bc[0], 0, 0);
}
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtEnableSet(rt, VMXF_DISABLE);
Console.Write("Terminating\n\r");
// free buffers
if (txBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txBuffer);
if (rxBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxBuffer);
if (rc != MAXT_SUCCESS)
{
if (mxfSystemErrorStringGet(server, rc, (UInt32)errorString.Capacity, errorString) != MAXT_SUCCESS)
{
errorString.Clear();
errorString.Append(string.Format("ERROR # 0x{0:x8}", rc));
}
Console.Write(errorString + "\n\r");
}
// Free all buffers and terminate
for (indexBuffer = 0; indexBuffer < 4 && bcBufferTx[indexBuffer] > 0; indexBuffer++)
{
if (bcBufferTx[indexBuffer] != 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(bcBufferTx[indexBuffer]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
for (indexBuffer = 0; indexBuffer < 4 && rtBuffer[indexBuffer] > 0; indexBuffer++)
{
if (rtBuffer[indexBuffer] != 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(rtBuffer[indexBuffer]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
if (bcBufferRx > 0)
{
rc = mxfRxAcqBufferFree(bcBufferRx);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
// Unload MX Foundation library
Console.Write("\n\rPress enter to terminate\n\r");
Console.ReadKey();
return;
}
}
}
Updated 10/23/2023