MX Foundation 4
mil1553_bc_buserror_trigger.cs
/*****************************************************************************
//
// File:
// mil1553_bc_buserror_trigger.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 Bus Error trigger of
// MIL-STD-1553 bus controller. The discrete input is used to monitor
// the trigger pins. The error injection feature is used on BC data
// to simulate all bus errors.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe (FX1553-4) or FlexMulti 1553 (FM1553-4)
// - FLEX discrete output #0 must be connected to discrete input #0
// - FLEX discrete output #1 must be connected to discrete input #1
//
*****************************************************************************/
//#define LOCAL
#define LOOPBACK
//#define ACQUISITION
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_bc_buserror_trigger
{
private const UInt64 M_ADDRESS = 5;
private const UInt64 M_SUBADDR = 3;
static void Main(string[] args)
{
UInt64 server;
var device = new UInt64[1];
UInt64 mil1553Module = 0;
var discreteModule = new UInt64[1];
var module = new UInt64[2];
UInt64 bc = 0;
UInt64 bm = 0;
UInt64 discrete = 0;
UInt64 rxDiscreteBuffer = 0;
UInt64 txMsg1553Buffer = 0;
var minorFrame = new MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG[1];
var txRec1553 = new MXF_MIL1553_DATAREC();
IntPtr recPtr = IntPtr.Zero;
#if ACQUISITION
UInt64 acq1553Buffer = 0;
var rxRec1553 = new MXF_MIL1553_DATAREC();
UInt64 rate;
UInt64 last = 0;
#else
UInt64 samp1553Buffer = 0;
MXF_MIL1553_SAMPREC sampRec1553;
#endif
var rxRecDiscrete = new MXF_DISCRETE_DATAREC();
UInt32 rc;
UInt64 discreteCount = 0;
UInt32 txDataSize;
IntPtr txData = IntPtr.Zero;
UInt32 rxDataSize = 0;
IntPtr rxData = IntPtr.Zero;
UInt64 rxAcqStatus;
UInt64 msgCount;
UInt64 byteCount;
UInt64 rxRec;
UInt64 loop = 0, deviceCount = 0, moduleCount = 0;
UInt64 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
// Initialize MX Foundation library
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting ...\n\r");
rc = mxfSystemInit(server);
}
// Initialize MX Foundation library
if (rc == MAXT_SUCCESS)
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;
// Get handle of first module
mil1553Module = module[0];
//Get handle of Discrete module
if (rc == MAXT_SUCCESS)
{
rc = mxfDeviceModuleAllGet(device[0], MXF_MODULE_DIOFIFO_EH, 1, out discreteCount, discreteModule);
if (rc == MAXT_SUCCESS && discreteCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
}
// Get handle of first MIL1553 Bus controller channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(mil1553Module, 1, out bc);
// Get handle of first MIL1553 Bus monitor channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(mil1553Module, 0, out bm);
// Get handle of discrete input channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(discreteModule[0], 1, out discrete);
// Set timebase to RTC nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Configure to EBR
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_INTERFACE, VMXF_MIL1553_INTERFACE_RS485);
#if LOOPBACK
// Enable internal loopback
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(discrete, KMXF_DISCRETE_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Configure discrete
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(discrete, KMXF_DISCRETE_RX_PULSE_WIDTH_FILTER, 10);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(discrete, KMXF_DISCRETE_RX_FIFO_SAMP_PERIOD, 10);
// Enable channel statistics
if (rc == MAXT_SUCCESS)
rc = mxfChannelStatisticEnableSet(bm, VMXF_ENABLE);
if (rc == MAXT_SUCCESS)
{
// Enable discrete pin #0/1 rising edge detection
rc = mxfAttributeUint64Set(discrete, KMXF_DISCRETE_RX_EDGE_FALLING, 0x0000);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(discrete, KMXF_DISCRETE_RX_EDGE_RISING, 0x0003);
}
if (rc == MAXT_SUCCESS)
{
// Enable discrete trigger on bus error of BUS A & B on discrete #0
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSA_ERROR, VMXF_ENABLE, 0);
if (rc == MAXT_SUCCESS)
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSB_ERROR, VMXF_ENABLE, 0);
}
if (rc == MAXT_SUCCESS)
{
// Enable discrete trigger on protocol error of BUS A & B on discrete #1
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSA_PROTOCOL_ERROR, VMXF_ENABLE, 1);
if (rc == MAXT_SUCCESS)
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSB_PROTOCOL_ERROR, VMXF_ENABLE, 1);
}
// Allocate 1KB buffer for tx data
if (rc == MAXT_SUCCESS)
{
txDataSize = 1024;
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, 0, txDataSize, out txMsg1553Buffer, IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
txData = Marshal.AllocHGlobal((int)txDataSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
rxDataSize = 10 * 1024;
#if ACQUISITION
// Allocate 10KB buffer for 1553 acquisition data
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(bm, rxDataSize, out acq1553Buffer, IntPtr.Zero);
#else
// Allocate 10KB buffer for 1553 sampling data
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingBufferAlloc(bm, rxDataSize, out samp1553Buffer, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingKilltimeSet(samp1553Buffer, 2000000000);
#endif
// Allocate 10KB buffer for discrete rx data
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(discrete, rxDataSize, out rxDiscreteBuffer, IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
rxData = Marshal.AllocHGlobal((int)rxDataSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// 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, 4, out minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = txMsg1553Buffer;
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc, 0, 0, 1, minorFrame, IntPtr.Zero);
}
// Set default data for address 5, subaddress 3
if (rc == MAXT_SUCCESS)
{
txRec1553 = new MXF_MIL1553_DATAREC();
txRec1553.data = new UInt16[36];
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10; //10 bytes (command + 4 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = 0x0000;
txRec1553.data[2] = 0x1111;
txRec1553.data[3] = 0x2222;
txRec1553.data[4] = 0x3333;
Marshal.StructureToPtr(txRec1553, txData, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(txMsg1553Buffer, 1, txData);
}
// Inject bus error (parity, bit count, manchester, data gap, sync) after 1st transmission
if (rc == MAXT_SUCCESS)
{
Error.errors = new UInt16[34];
UInt32 errorIndex = 0;
mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_BITCNT, 17, out Error.errors[0]);//Bit count = 17 bit
mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_PAR, 0, out Error.errors[1]);//Parity error
mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_MANCHBIPHASEDATA, 8, out Error.errors[2]);//Manchester error on bit 8
mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_WORDGAP, 3, out Error.errors[3]);//Data gap of 3 us
mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_MANCHSYNCLEVEL, 0x4, out Error.errors[4]);//SYNC zero crossing + 1/2 bit
rc = mxfMIL1553ErrorInjectionSet(bc, errorIndex, ref Error);
if (rc == MAXT_SUCCESS)
{
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = (UInt16)errorIndex;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(txMsg1553Buffer, 1, txData);
}
}
#if ACQUISITION
// Start BM acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(acq1553Buffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
#else
// Start BM sampling
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingStart(samp1553Buffer);
#endif
// Start Discrete acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxDiscreteBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Start the major frame with the clock mode (on rising edge)
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameStart(bc, 0, 500 * 1000 * 1000, IntPtr.Zero); //500 msec
if (rc == MAXT_SUCCESS)
mxfSleep(1000);
// Read and display received messages
if (rc == MAXT_SUCCESS)
{
loop = 0;
do
{
#if ACQUISITION
rc = mxfMIL1553RxAcqRead(acq1553Buffer, 0, rxDataSize, out rxAcqStatus, out msgCount, out byteCount, rxData);
rxRec1553 = new MXF_MIL1553_DATAREC();
recPtr = rxData;
for (rxRec = 0; rc == MAXT_SUCCESS && rxRec < msgCount; rxRec++)
{
rxRec1553 = (MXF_MIL1553_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_MIL1553_DATAREC));
rate = last > 0 ? ((rxRec1553.timeTag - last) / 1000000) : 0;//msec
last = rxRec1553.timeTag;
Console.Write("{0} ", rxRec1553.timeTag);
Console.Write("{0:3} ", rate);
Console.Write("{0:x8} ", rxRec1553.control);
Console.Write("{0:0000} ", (rxRec1553.dataSize / 2) - 1);
for (data = 0; data < Math.Min(8, rxRec1553.dataSize / 2); data++)
{
Console.Write("{0:X4} ", rxRec1553.data[data]);
}
Console.Write("\n\r");
// Get next msg
mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
#else
rc = mxfMIL1553RxSamplingRead(samp1553Buffer, MXF_RXSAMPLING_FLAG_DEFAULT, 0, rxDataSize, out msgCount, out byteCount, rxData);
sampRec1553 = new MXF_MIL1553_SAMPREC();
recPtr = rxData;
for (rxRec = 0; rc == MAXT_SUCCESS && rxRec < msgCount; rxRec++)
{
sampRec1553 = (MXF_MIL1553_SAMPREC)Marshal.PtrToStructure(recPtr, typeof(MXF_MIL1553_SAMPREC));
Console.Write("{0} ", sampRec1553.timeTag);
Console.Write("{0:000} ", sampRec1553.rate);
Console.Write("{0:x8} ", sampRec1553.control);
Console.Write("{0:0000} ", (sampRec1553.dataSize / 2) - 1);
for (data = 0; data < Math.Min(5, sampRec1553.dataSize / 2); data++)
{
Console.Write("{0:X4} ", sampRec1553.data[data]);
}
Console.Write("\n\r");
// Get next msg
}
#endif
// Read DISCRETE acquisition queue
if (rc == MAXT_SUCCESS)
rc = mxfDiscreteRxAcqRead(rxDiscreteBuffer, 0, rxDataSize, out rxAcqStatus, out msgCount, out byteCount, rxData);
rxRecDiscrete = new MXF_DISCRETE_DATAREC();
recPtr = rxData;
for (rxRec = 0; rc == MAXT_SUCCESS && rxRec < msgCount; rxRec++)
{
rxRecDiscrete = (MXF_DISCRETE_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_DISCRETE_DATAREC));
if (rxRecDiscrete.edge > 0) //skip initial value
{
Console.Write("DISCRETE -> {0} ", rxRecDiscrete.timeTag);
Console.Write("{0:X4} ", rxRecDiscrete.edge);
Console.Write("{0:X4} ", rxRecDiscrete.data);
Console.Write("\n\r");
}
mxfDiscreteNextDataRecordPtrGet(recPtr, out recPtr);
}
mxfSleep(500);
loop++;
}
while (loop < 5);
}
if (rc == MAXT_SUCCESS)
{
//Disable discrete trigger on bus error
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSA_ERROR, VMXF_DISABLE, 0);
if (rc == MAXT_SUCCESS)
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSB_ERROR, VMXF_DISABLE, 0);
}
if (rc == MAXT_SUCCESS)
{
//Disable discrete trigger on protocol error
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSA_PROTOCOL_ERROR, VMXF_DISABLE, 0);
if (rc == MAXT_SUCCESS)
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSB_PROTOCOL_ERROR, VMXF_DISABLE, 0);
}
Console.Write("Stopping\n\r");
// Stop the major frame
#if ACQUISITION
// Stop acquisition
mxfRxAcqStop(acq1553Buffer);
mxfRxAcqClear(acq1553Buffer);
#else
// Stop sampling
mxfRxSamplingStop(samp1553Buffer);
#endif
mxfRxAcqStop(rxDiscreteBuffer);
mxfRxAcqClear(rxDiscreteBuffer);
if (txData != IntPtr.Zero)
Marshal.FreeHGlobal(txData);
if (rxData != IntPtr.Zero)
Marshal.FreeHGlobal(rxData);
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
if (txMsg1553Buffer > 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(txMsg1553Buffer);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
#if ACQUISITION
if (acq1553Buffer > 0)
{
rc = mxfRxAcqBufferFree(acq1553Buffer);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
#else
if (samp1553Buffer > 0)
{
rc = mxfRxSamplingBufferFree(samp1553Buffer);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
#endif
if (rxDiscreteBuffer > 0)
{
rc = mxfRxAcqBufferFree(rxDiscreteBuffer);
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