MX Foundation 4
mil1553_cond_branch_2.cs
/*****************************************************************************
//
## File:
## mil1553_cond_branch_2.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 usage of message conditional branch
// option with discrete input with MIL-STD-1553 BC channel.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti.
//
*****************************************************************************/
//#define LOCAL
#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_exemple
{
public class mil1553_cond_branch_2
{
const UInt64 BUF_SA3_RX = 0;
const UInt64 BUF_SA3_TX = 1;
const UInt64 BUF_SA5_RX = 2;
const UInt64 BUF_SA5_TX = 3;
const UInt16 RT_ADRS = 5;
static void Main(string[] args)
{
UInt32 rc;
UInt64 deviceCount = 0;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt64 server = 0;
var device = new UInt64[1];
var module = new UInt64[1];
var bc = new UInt64[1];
var bm = new UInt64[1];
var discIn = new UInt64[1];
var discOut = new UInt64[1];
var bcBufferTx = new UInt64[4];
var bcBufferNoOp = new UInt64();
var bmBufferRx = new UInt64();
var bcMsgID = new MXF_MSGID_MIL1553[]
{
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_RX, address = RT_ADRS, subAddress = 3, reserved = 0 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_TX, address = RT_ADRS, subAddress = 3, reserved = 0 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_RX, address = RT_ADRS, subAddress = 5, reserved = 0 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_TX, address = RT_ADRS, subAddress = 5, reserved = 0 }
};
UInt32 txBufferSize = 1024;
IntPtr txBuffer = IntPtr.Zero;
var txRec1553 = new MXF_MIL1553_DATAREC();
txRec1553.data = new UInt16[36];
UInt32 rxBufferSize = 10 * 1024;
IntPtr rxBuffer = IntPtr.Zero;
var rxRec1553 = new MXF_MIL1553_DATAREC[1];
var minorFrame = new MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG[2];
UInt64 rxAcqStatus = 0;
UInt64 msgCount = 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 handle of first Flex MIL-STD-1553 Bus controller channel
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_ALL, 1, out deviceCount, device);
if (rc == MAXT_SUCCESS && deviceCount > 0)
rc = mxfDeviceModuleAllGet(device[0], MXF_MODULE_MIL1553MRT_EH, 1, out moduleCount, module);
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 bus monitor channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 0, out bm[0]);
// Get handle for discrete channels
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device[0], MXF_MODULE_DIOFIFO_EH, 1, out moduleCount, module);
if (rc == MAXT_SUCCESS && moduleCount > 0)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_DISCRETE, MXF_SCLASS_RX_CHANNEL, 1, out channelCount, discIn);
if (rc == MAXT_SUCCESS && channelCount > 0)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_DISCRETE, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, discOut);
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Set first two pins to 0
if (rc == MAXT_SUCCESS)
rc = mxfDiscreteChannelWrite(discOut[0], 0x3, 0);
// Enable internal loopback
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(discIn[0], KMXF_DISCRETE_TX_RX_TEST_LB, VMXF_ENABLE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(bm[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(bc[0], 1234, 0, out bcBufferNoOp, IntPtr.Zero);
// Host allocation
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 = 10 * 1024;
// Flex allocation
rc = mxfRxAcqBufferAlloc(bm[0], rxBufferSize, out bmBufferRx, IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
rxBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Set timebase to RTC nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Set the minor frame #0 using 1 Commands
if (rc == MAXT_SUCCESS)
{
// Command #0 : Wait for first pin is 1 then branch immediately to minor frame #1
minorFrame[0].buffer = bcBufferNoOp;
minorFrame[0].modulo = 1;
minorFrame[0].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_NOOP_AND_WAITONCOND | MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_NEXTFRAME_NOW;
minorFrame[0].condMask = 0x0001;
minorFrame[0].condData = 0x0001;
minorFrame[0].branchMinorIndex = 1;
minorFrame[0].branchMsgIndex = 0xFFFF;
rc = mxfMIL1553ConditionalBranchOnDiscreteCompose(MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_CONDBRANCH_OPT_AND, out minorFrame[0].condBranch);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc[0], 0, 0, 1, minorFrame, IntPtr.Zero);
}
// Set the minor frame #1 using 2 Commands
if (rc == MAXT_SUCCESS)
{
// Command #0 : Address 5, Subaddress 3, RX, 2 words, Modulo 1
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA3_RX].address, bcMsgID[BUF_SA3_RX].subAddress, bcMsgID[BUF_SA3_RX].type, 2, out minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[BUF_SA3_RX];
// Command #1 : Address 5, Subaddress 3, TX, 2 words, delay of 1 msec from preceding command or start of minor frame
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA3_TX].address, bcMsgID[BUF_SA3_TX].subAddress, bcMsgID[BUF_SA3_TX].type, 2, out minorFrame[1].command);
if (rc == MAXT_SUCCESS)
{
minorFrame[1].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_DELAY;
minorFrame[1].modulo = 1;
minorFrame[1].buffer = bcBufferTx[BUF_SA3_TX];
minorFrame[1].delay = 8000;
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc[0], 0, 1, 2, minorFrame, IntPtr.Zero);
}
}
}
// Set the minor frame #3 using 2 Commands
if (rc == MAXT_SUCCESS)
{
mfProp.options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MINOR_PROPERTIES_OPT_BRANCH_GOTO;
mfProp.modulo = 1;
mfProp.repeatCount = 1;
mfProp.branchIndex = 1;
// Command #0 : Address 5, Subaddress 5, RX, 3 words
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA5_RX].address, bcMsgID[BUF_SA5_RX].subAddress, bcMsgID[BUF_SA5_RX].type, 3, out minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[BUF_SA5_RX];
// Command #1 : Address 5, Subaddress 5, TX, 3 words, delay of 1 msec after end of preceding command, loop to minor frame #1
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA5_TX].address, bcMsgID[BUF_SA5_TX].subAddress, bcMsgID[BUF_SA5_TX].type, 3, out minorFrame[1].command);
if (rc == MAXT_SUCCESS)
{
minorFrame[1].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_GAP;
minorFrame[1].buffer = bcBufferTx[BUF_SA5_TX];
minorFrame[1].modulo = 1;
minorFrame[1].delay = 8000;
}
}
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc[0], 0, 2, 2, minorFrame, ref mfProp);
}
// Set BC data for address 5, subaddress 3, RX
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 10; // repeat count of 10
txRec1553.dataSize = 6; //6 bytes (command + 2 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = 0x0000;
txRec1553.data[2] = 0x1111;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[BUF_SA3_RX], 1, txBuffer);
}
// Set BC data for address 5, subaddress 5, RX
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.dataSize = 8; //8 bytes (command + 3 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = 0xFFFF;
txRec1553.data[2] = 0xEEEE;
txRec1553.data[3] = 0xDDDD;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[BUF_SA5_RX], 1, txBuffer);
}
// Start BC acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(bmBufferRx, 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)
{
IntPtr recPtr = IntPtr.Zero;
do
{
rc = mxfMIL1553RxAcqRead(bmBufferRx, 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\tBC 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");
if (msgInfo[0].statusIndex[0] != 0xffff)
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]);
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:");
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++;
if (rc == MAXT_SUCCESS && loop == 1)
{
// switch to minor frame 1
rc = mxfDiscreteChannelWrite(discOut[0], 0x1, 0x1);
}
} while (loop < 10);
}
// Stop acquisition
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqStop(bmBufferRx);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqClear(bmBufferRx);
}
// Stop the major frame
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicMajorFrameStop(bc[0], 0, 0);
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");
}
}
if (bcBufferNoOp > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (bmBufferRx > 0)
{
rc = mxfRxAcqBufferFree(bmBufferRx);
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 03/30/2022