MX Foundation 4
mil1553_bc_extclock.cs
/*****************************************************************************
//
// File:
// mil1553_bc_extclock.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 basic usage of MIL-STD-1553 bus controller
// using external clock. The discrete input #4 [rising edge] is used to start
// transmission of each minor frame.
//
// Can be run with mil1553_rt and mil1553_bm at the same time.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti with module 1553 and Discrete.
//
*****************************************************************************/
//#define LOCAL
#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_bc_extclock
{
private const UInt64 M_ADDRESS = 5;
private const UInt64 M_SUBADDR = 3;
static void Main(string[] args)
{
UInt64 server;
UInt64 device = 0;
var mil1553Module = new UInt64[1];
var discreteModule = new UInt64[1];
UInt64 bc = 0;
UInt64 discreteOut = 0;
#if LOOPBACK
UInt64 discreteIn = 0;
#endif
UInt64 acq1553Buffer = 0;
UInt64 txDiscreteBuffer = 0;
UInt64 txMsg1553Buffer = 0;
UInt64 txMsg1553Buffer2 = 0;
var minorFrame = new MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG[3];
var txRec1553 = new MXF_MIL1553_DATAREC();
txRec1553.data = new UInt16[36];
var rxRec1553 = new MXF_MIL1553_DATAREC[1];
var msgInfo = new MXF_MIL1553_MSGINFO[1];
UInt32 rc;
UInt64 appCount;
UInt64 moduleCount = 0;
UInt32 txBufferSize = 0;
IntPtr txBuffer = IntPtr.Zero;
UInt32 rxBufferSize = 0;
IntPtr rxBuffer = IntPtr.Zero;
IntPtr recPtr = IntPtr.Zero;
UInt64 rxAcqStatus;
UInt64 msgCount;
UInt64 byteCount;
UInt64 rxRec;
UInt64 loop = 0;
UInt64 address, subAddress, dir, wordCount;
UInt32 data;
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
Console.Write("Starting ...\n\r");
// Initialize MX Foundation library
if (rc == MAXT_SUCCESS)
{
rc = mxfSystemApplicationCountGet(server, out appCount);
if (rc == MAXT_SUCCESS)
{
// if other MX Foundation application running, only load MX-Foundation library, otherwise reset cards first
if (appCount > 0)
rc = mxfSystemResourcesInit(server, 0);
else
rc = mxfSystemInit(server);
}
}
//Get handle of first device
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
//Get handle of first 1553 module
if (rc == MAXT_SUCCESS)
{
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MIL1553MRT_EH, 1, out moduleCount, mil1553Module);
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
}
//Get handle of Discrete module
if (rc == MAXT_SUCCESS)
{
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_DIOFIFO_EH, 1, out moduleCount, discreteModule);
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
}
// Get handle of first MIL1553 Bus controller channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(mil1553Module[0], 1, out bc);
// Get handle of discrete output channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(discreteModule[0], 0, out discreteOut);
#if LOOPBACK
// Get handle of discrete input channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(discreteModule[0], 1, out discreteIn);
// Enable internal loopback
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(bc, KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(discreteIn, KMXF_DISCRETE_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate 1KB buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = 64 * 1024;
// Device allocation
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, 0, txBufferSize, out txMsg1553Buffer, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, 1, 0, out txMsg1553Buffer2, 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;
// Device allocation
rc = mxfRxAcqBufferAlloc(bc, rxBufferSize, out acq1553Buffer, 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);
// Clear the major frame
if (rc == MAXT_SUCCESS)
// Set the minor frame #0 using 3 Commands
if (rc == MAXT_SUCCESS)
{
// Command #0 : Address 5, Subaddress 3, RX, 16 words
rc = mxfMIL1553CommandCompose(M_ADDRESS, M_SUBADDR, MXF_MIL1553_COMMAND_DIR_RX, 16, out minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = txMsg1553Buffer;
// Command #1 : Address 5, Subaddress 3, TX, 16 words
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(M_ADDRESS, M_SUBADDR, MXF_MIL1553_COMMAND_DIR_TX, 16, out minorFrame[1].command);
minorFrame[1].modulo = 1;
minorFrame[1].buffer = txMsg1553Buffer2;
}
// Command #2 : Address 5, Mode Command 16, TX
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(M_ADDRESS, 0, MXF_MIL1553_COMMAND_DIR_TX, 16, out minorFrame[2].command);
minorFrame[2].modulo = 1;
minorFrame[2].buffer = txMsg1553Buffer2;
}
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc, 0, 0, 3, minorFrame, IntPtr.Zero);
}
// Set default data for address 5, subaddress 3
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
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, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(txMsg1553Buffer, 1, txBuffer);
}
// Start BC acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(acq1553Buffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Start the major frame with the clock mode (on rising edge)
if (rc == MAXT_SUCCESS)
{
var majorProperties = new MXF_MIL1553_TXPERIODIC_MJRFRAME_PROPERTIES();
majorProperties.options = MXF_MIL1553_TXPERIODIC_MJRFRAME_PROPERTIES_OPT_EXTCLOCK_MODE_RISING;
rc = mxfMIL1553TxPeriodicMajorFrameStart(bc, 0, 0, ref majorProperties);
}
if (rc == MAXT_SUCCESS)
{
var dioRec = new MXF_DISCRETE_DATAREC[2];
UInt32 pulseCount;
UInt64 timer = 0;
UInt64 c50ms = 50000000;
// Allocate 1KB buffer for discrete tx data
rc = mxfTxAperiodicBufferAlloc(discreteOut, MXF_TXAPERIODIC_PRIORITY_NORMAL, txBufferSize, out txDiscreteBuffer, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceTimerGet(device, out timer);
if (rc == MAXT_SUCCESS)
{
// Simulate 100 pulse on discrete output at each 100 msec
for (pulseCount = 0; rc == MAXT_SUCCESS && pulseCount < 100; pulseCount++)
{
recPtr = txBuffer;
timer += c50ms;
dioRec[0].timeTag = timer;
dioRec[0].repeatCount = 1;
dioRec[0].control = 0;
dioRec[0].edge = 0xFFFF;
dioRec[0].data = 0x0010; //BC #0 = DISCRETE INPUT #4
dioRec[0].highDuration = 0;
dioRec[0].lowDuration = 0;
Marshal.StructureToPtr(dioRec[0], recPtr, true);
rc = mxfDiscreteNextDataRecordPtrGet(recPtr, out recPtr);
timer += c50ms;
dioRec[1].timeTag = timer;
dioRec[1].repeatCount = 1;
dioRec[1].control = 0;
dioRec[1].edge = 0xFFFF;
dioRec[1].data = 0x0000;
dioRec[1].highDuration = 0;
dioRec[1].lowDuration = 0;
Marshal.StructureToPtr(dioRec[1], recPtr, true);
if (rc == MAXT_SUCCESS)
rc = mxfDiscreteTxAperiodicWrite(txDiscreteBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, 2, txBuffer);
if (rc != MAXT_SUCCESS)
Console.Write("mxfDiscreteTxAperiodicWrite() error; rc=0x{0:x8} (count={1})\n\r", rc, pulseCount);
}
}
}
// Read and display received messages
if (rc == MAXT_SUCCESS)
{
do
{
rc = mxfMIL1553RxAcqRead(acq1553Buffer, 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxBuffer);
if (rc != MAXT_SUCCESS)
Console.Write("mxfMIL1553RxAcqRead() error; rc=0x{0:x8}\n\r", rc);
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, 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) && (msgInfo[0].dataIndex != 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 < 25);
}
Console.Write("Stopping\n\r");
// Stop acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(acq1553Buffer);
// Stop the major frame
if (rc == MAXT_SUCCESS)
// 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");
}
// Unload MX Foundation library
rc = mxfSystemApplicationCountGet(server, out appCount);
if (rc == MAXT_SUCCESS)
{
// if an other application running, only release the resource, otherwise reset everything
if (appCount > 1)
else
{
// Free all buffers and terminate
if (txMsg1553Buffer > 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(txMsg1553Buffer);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (txMsg1553Buffer2 > 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(txMsg1553Buffer2);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (acq1553Buffer > 0)
{
rc = mxfRxAcqBufferFree(acq1553Buffer);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (txDiscreteBuffer > 0)
{
rc = mxfTxAperiodicBufferFree(txDiscreteBuffer);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
}
Console.Write("\nPress enter to terminate\n\r");
Console.ReadKey();
return;
}
}
}
Updated 03/30/2022