MX Foundation 4
mil1553_manyErrorInjections.cs
/*****************************************************************************
//
## File:
## mil1553_manyErrorInjections.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 how to inject the following errors :
// - bit count error
// - override error
// - superseding error
//
// The minor frame for this example is built as follows :
// BC -> RT20 SA3 bit count error injection
// RT20 SA3 -> BC override error injection
// RT10 SA3 -> BC superseding error injection
// BC -> RT10 SA3
//
// The main thing in this example is that we followed the SAE specification for simulating a bus switching
// with an override command followed by a superseding command.
// As you will notice when receiving the data (console display), the 3rd command is then received on bus B.
//
// Important to notice : the override error is on the bc side and we applied it on a RT-> BC command, so the
// BM will receive the 32 words because the data are sent by the RT and not the BC. If you setup an override
// error on a BC-> RT command, you will receive only the number of words decided by your override (if you
// override after 8 words, you will only read 8 words of course).
//
// We check the control to see if the errors are well simulated and display the information on the console
// after each command.
//
// We setup the superseding command to be sent with a GAP of 4 bits after the override command. In 1553, 4
// bits take 4 usec to be transmitted. That is why we check the timetags at the end of the program between
// the 2 commands to see if there is actually a 4 usec difference.
//
// When injecting an error on a RT-> BC command like we do in this example, you have to be careful with the
// buffer handling. The RT data will be set with mxfMIL1553TxPeriodicUpdateMsgWrite() using a rt buffer
// allocated before and the RT error injection will be set with mxfMIL1553TxPeriodicUpdateMsgWrite() using a
// bc buffer also allocated at the beginning.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#define LOOPBACK
#define LOCAL
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_manyErrorInjections
{
const UInt32 BITCNT_SHIFT = 18; //bit count error of +2 bits
const UInt32 RT_ADRS = 20;
const UInt32 RT_SUBADRS = 3;
const UInt32 NB_BC_BUFFERS = 4;
const UInt32 NB_RT_BUFFERS = 2;
static void Main(string[] args)
{
UInt64 server;
var device = new UInt64[1];
var module = new UInt64[1];
var bc = new UInt64[1];
var bm = new UInt64[1];
var rt = new UInt64[NB_RT_BUFFERS];
var bcBufferTx = new UInt64[NB_BC_BUFFERS];
UInt64 bmBufferRx = 0;
var rtBuffer = new UInt64[NB_RT_BUFFERS];
UInt32 txBufferSize = 0;
IntPtr txBuffer = IntPtr.Zero;
var txRec1553 = new MXF_MIL1553_DATAREC();
txRec1553.data = new UInt16[36];
UInt32 rxBufferSize = 0;
IntPtr rxBuffer = IntPtr.Zero;
var rxRec1553 = new MXF_MIL1553_DATAREC[1];
var minorFrame = new MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG[NB_BC_BUFFERS];
var msgInfo = new MXF_MIL1553_MSGINFO[1];
var err = new MXF_MIL1553_ERROR_INJ();
err.errors = new UInt16[34];
var errorString = new StringBuilder(200);
UInt64 bus = MXF_MIL1553_BUS_A | MXF_MIL1553_BUS_B;
UInt64 rxAcqStatus;
UInt64 msgCount;
UInt64 byteCount, freeBytes, pendingBytes, indexBuffer;
UInt64 rxRec;
UInt32 loop = 0;
UInt64 address, subAddress, dir, wordCount;
UInt64 deviceCount = 0;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt32 rc;
var timetagArray = new UInt64[NB_BC_BUFFERS];
UInt64 index = 0;
// Connect to MX Foundation library
#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 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 = mxfDeviceModuleAllGet(device[0], MXF_MODULE_MIL1553MRT, 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 = mxfModuleChannelAllGet(module[0], MXF_CLASS_MIL1553, MXF_SCLASS_BM_CHANNEL, 1, out channelCount, bm);
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Get handle of MIL-STD-1553 remote terminal channels
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 2 + RT_ADRS, out rt[0]);
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 2 + (RT_ADRS - 10), out rt[1]);
#if LOOPBACK
// Enable internal 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;
// Allocate buffers and enable bus
for (index = 0; index < NB_BC_BUFFERS && rc == MAXT_SUCCESS; index++)
{
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc[0], index, txBufferSize, out bcBufferTx[index], IntPtr.Zero);
}
for (index = 0; index < NB_RT_BUFFERS && rc == MAXT_SUCCESS; index++)
{
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt[index], index, txBufferSize, out rtBuffer[index], IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt[index], MXF_MIL1553_MSGTYPE_RX, 3, bus, rtBuffer[index]);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt[index], MXF_MIL1553_MSGTYPE_TX, 3, bus, rtBuffer[index]);
}
// 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(bm[0], rxBufferSize, out bmBufferRx, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferStatusGet(bmBufferRx, out rxAcqStatus, out msgCount, out pendingBytes, out freeBytes);
// 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);
// Configure injection #0, override error
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_OVERRIDE, 0x00, out err.errors[0]);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553ErrorInjectionSet(bc[0], 0, ref err);
}
// Configure injection #1, bit count error
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_BITCNT, BITCNT_SHIFT, out err.errors[0]);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553ErrorInjectionSet(bc[0], 1, ref err);
}
// Configure injection #2, superseding error
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_SUPERSEDING, 0, out err.errors[0]);
if (rc == MAXT_SUCCESS)
{
err.errors[33] = 32; // 1/8 bits so it is equivalent to a 4 bits delay
rc = mxfMIL1553ErrorInjectionSet(bc[0], 2, ref err);
}
}
// Set the minor frame #0 using 2 Commands
if (rc == MAXT_SUCCESS)
{
// Command #0 : Address 20, Subaddress 3, RX, 32 words.
rc = mxfMIL1553CommandCompose(RT_ADRS, RT_SUBADRS, MXF_MIL1553_MSGTYPE_RX, 0, out minorFrame[0].command); // specification : 0 means 32 words
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[0];
minorFrame[0].retryHaltOptions = 0;
// Command #1 : Address 20, Subaddress 3, TX, 32 words.
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553CommandCompose(RT_ADRS, RT_SUBADRS, MXF_MIL1553_MSGTYPE_TX, 0, out minorFrame[1].command); // specification : 0 means 32 words
minorFrame[1].modulo = 1;
minorFrame[1].buffer = bcBufferTx[1];
minorFrame[1].retryHaltOptions = 0;
// Command #2 : Address 10, Subaddress 3, TX, 32 words.
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553CommandCompose(RT_ADRS - 10, RT_SUBADRS, MXF_MIL1553_MSGTYPE_TX, 0, out minorFrame[2].command); // specification : 0 means 32 words
minorFrame[2].modulo = 1;
minorFrame[2].buffer = bcBufferTx[3];
minorFrame[2].retryHaltOptions = 0;
minorFrame[2].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_BUS_B;
// Command #3 : Address 10, Subaddress 3, RX, 32 words.
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553CommandCompose(RT_ADRS - 10, RT_SUBADRS, MXF_MIL1553_MSGTYPE_RX, 0, out minorFrame[3].command); // specification : 0 means 32 words
minorFrame[3].modulo = 1;
minorFrame[3].buffer = bcBufferTx[2];
minorFrame[3].retryHaltOptions = 0;
if (rc == MAXT_SUCCESS)
{
var propertiesMinorFrame = new MXF_MIL1553_TXPERIODIC_MJRFRAME_MINOR_PROPERTIES();
propertiesMinorFrame.repeatCount = 1;
propertiesMinorFrame.modulo = 1;
propertiesMinorFrame.options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MINOR_PROPERTIES_OPT_BRANCH_END;
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc[0], 0, 0, 4, minorFrame, ref propertiesMinorFrame);
}
}
// Set BC data for address 20, subaddress 3, RX
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 1; // bit count error
txRec1553.dataSize = 66; //66 bytes (command + 32 words)
txRec1553.data[0] = 0x0000; //Not used
for (index = 1; index < txRec1553.dataSize / 2; index++)
txRec1553.data[index] = (UInt16)(0x0101 * (UInt16)index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[0], 1, txBuffer);
}
// Set RT data for address 20, subaddress 3, TX
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = 0;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 0;
txRec1553.dataSize = 66; //66 bytes (command + 32 words)
txRec1553.data[0] = 0x0000; //Not used
for (index = 1; index < txRec1553.dataSize / 2; index++)
txRec1553.data[index] = (UInt16)(0x0101 * (UInt16)index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[0], 1, txBuffer);
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 0; // override error
txRec1553.dataSize = 0;
for (index = 0; index < 36; index++)
txRec1553.data[index] = 0;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[1], 1, txBuffer);
}
// Set RT data for address 10, subaddress 3, TX
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = 0;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 0;
txRec1553.dataSize = 66; //66 bytes (command + 32 words)
txRec1553.data[0] = 0x0000; //Not used
for (index = 1; index < txRec1553.dataSize / 2; index++)
txRec1553.data[index] = (UInt16)(0x0101 * (UInt16)index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[1], 1, txBuffer);
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 2; // superseding error
txRec1553.dataSize = 0;
for (index = 0; index < 36; index++)
txRec1553.data[index] = 0;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[3], 1, txBuffer);
}
// Set BC data for address 10, subaddress 3, RX
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = 0;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 0;
txRec1553.dataSize = 66; //66 bytes (command + 32 words)
txRec1553.data[0] = 0x0000; //Not used
for (index = 1; index < txRec1553.dataSize / 2; index++)
txRec1553.data[index] = (UInt16)(0x0101 * (UInt16)index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[2], 1, txBuffer);
}
// Start messages queues
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting BC - RT\n\r");
rc = mxfMIL1553RtEnableSet(rt[0], VMXF_ENABLE);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtEnableSet(rt[1], VMXF_ENABLE);
}
// Start BM 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);
if (rc != MAXT_SUCCESS)
Console.Write("mxfMIL1553RxAcqRead() error; rc={0:x8}", 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(bm[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);
timetagArray[rxRec] = rxRec1553[0].timeTag;
switch (msgInfo[0].msgType)
{
case MXF_MIL1553_MSGINFO_TYPE_BCRT:
Console.Write("BC to RT{0} SA{1} WC{2} DataSize : {3} Bus{4} (0x{5:x4}) Control : 0x{6:x8}\n\r", address, subAddress, wordCount, (rxRec1553[0].dataSize / 2) - 2, (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_BUS_B) > 0 ? "B" : "A", rxRec1553[0].data[0], rxRec1553[0].control);
Console.Write("\t\tParity {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_PARITY_ERROR) > 0 ? "Error" : "OK");
Console.Write("\t\tData length {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_DATA_LENGTH_ERROR) > 0 ? "Error" : "OK");
Console.Write("\t\tBit Count {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_EXTRA_BIT_ERROR) > 0 ? "Error" : "OK");
break;
case MXF_MIL1553_MSGINFO_TYPE_RTBC:
Console.Write("RT{0} SA{1} to BC WC{2} DataSize : {3} Bus{4} (0x{5:x4}) Control : 0x{6:x8}\n\r", address, subAddress, wordCount, (rxRec1553[0].dataSize / 2) - 2, (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_BUS_B) > 0 ? "B" : "A", rxRec1553[0].data[0], rxRec1553[0].control);
Console.Write("\t\tParity {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_PARITY_ERROR) > 0 ? "Error" : "OK");
Console.Write("\t\tData length {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_DATA_LENGTH_ERROR) > 0 ? "Error" : "OK");
Console.Write("\t\tBit Count {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_EXTRA_BIT_ERROR) > 0 ? "Error" : "OK");
break;
}
}
}
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
mxfSleep(500);
loop++;
} 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)
{
Console.Write("\nStopping BC - RT\n\r");
rc = mxfTxPeriodicMajorFrameStop(bc[0], 0, 0);
}
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtEnableSet(rt[0], VMXF_DISABLE);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtEnableSet(rt[1], VMXF_DISABLE);
//Displays if we have the wanted gap of 4 microseconds between the command with the override and the command with the superseding error
Console.Write("Timetag Diff between RT20 SA3 -> BC and RT10 SA3 -> BC : {0} usec\n", (timetagArray[2] - timetagArray[1]) / 1000);
// Free buffers
if (txBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txBuffer);
if (rxBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxBuffer);
// Display any previous errors
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 < 1 && 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 < 1 && rtBuffer[indexBuffer] > 0; indexBuffer++)
{
if (rtBuffer[indexBuffer] != 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(rtBuffer[indexBuffer]);
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 10/23/2023