MX Foundation 4
mil1553_rtErrorInjections.cs
/*****************************************************************************
//
## File:
## mil1553_rtErrorInjections.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 :
// - override status error
// - do not respond error
// - respond on error error
//
// The minor frame for this example is built as follows :
// BC -> RT20 SA3 override status error injection (RT side)
// BC -> RT10 SA3 do not respond error injection (RT side)
// BC -> RT30 SA3 word count error injection (BC side) + respond on error injection (RT side)
//
//
// As you can see in the minor frame described above, an override status error is injected on the first
// command. This will rise a MXF_MIL1553_STATUS_MSGERR flag in the status index of the MXF_MIL1553_MSGINFO
// structure obtained with the function mxfMIL1553DataRecordDecompose() in acquisition.
//
// Then, the "do not respond error" is injected in the second command. It is more simple to check this error:
// you just have to look at the record control to see if the MXF_MIL1553_RX_REC_CTRL_MSG_NO_RESPONSE flag is
// risen.
//
// On the third command, it is a bit more tricky. We inject a word count error on the BC side so the RT will detect
// an error and we also inject a "respond on error" error on the RT side. That is why we receive an unexpected message
// after the third command, also displayed on the console screen.
//
// We chose a word count error because this error will not affect the type of the command (will still be MXF_MIL1553_MSGINFO_TYPE_BCRT)
// when for example if you choose a parity error, your command will be seen as MXF_MIL1553_MSGINFO_TYPE_UNEXPECTED.
//
// When an error is injected in a command, in 1553, the RT will not respond and not transmit a status.
// For example in the third command, we inject a word count error, so the RT will not respond. That is
// why you should see on the console screen the message "RT response Error" for this command.
//
// It is normal that the datasize for the third command is 15 instead of 32 words because, as previously said,
// a word count error is injected on the BC side.
//
// 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_exemple
{
public class mil1553_rtErrorInjections
{
const UInt32 RT_ADRS = 20;
const UInt32 RT_SUBADRS = 3;
const UInt32 NB_BC_BUFFERS = 3;
const UInt32 NB_RT_BUFFERS = 3;
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, pendingBytes, freeBytes;
UInt64 byteCount, indexBuffer;
UInt64 rxRec;
UInt32 loop = 0;
UInt64 address, subAddress, dir, wordCount;
UInt64 deviceCount = 0;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt32 rc;
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 (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 2 + (RT_ADRS + 10), out rt[2]);
#if LOOPBACK
if (rc == MAXT_SUCCESS)
// Enable internal loopback
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 status error on RT20
if (rc == MAXT_SUCCESS)
{
for (int i = 0; i < 34; i++)
err.errors[i] = 0;
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_STATUS_OV, 0, out err.errors[0]);
if (rc == MAXT_SUCCESS)
{
err.errors[33] = MXF_MIL1553_STATUS_MSGERR; //rises the message error bit
rc = mxfMIL1553ErrorInjectionSet(rt[0], 0, ref err);
}
}
// Configure injection #1, do not respond error on RT10
if (rc == MAXT_SUCCESS)
{
for (int i = 0; i < 34; i++)
err.errors[i] = 0;
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_DONOTRESPOND, 0, out err.errors[0]);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553ErrorInjectionSet(rt[1], 1, ref err);
}
// Configure injection #2, respond on error error on RT30
if (rc == MAXT_SUCCESS)
{
for (int i = 0; i < 34; i++)
err.errors[i] = 0;
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_RESPONDONERROR, 0, out err.errors[0]);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553ErrorInjectionSet(rt[2], 2, ref err);
}
// Configure injection #3, word count error
if (rc == MAXT_SUCCESS)
{
for (int i = 0; i < 34; i++)
err.errors[i] = 0;
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_WORDCNT, 16, out err.errors[0]);
rc = mxfMIL1553ErrorInjectionSet(bc[0], 3, 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 10, Subaddress 3, RX, 32 words.
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553CommandCompose(RT_ADRS - 10, RT_SUBADRS, MXF_MIL1553_MSGTYPE_RX, 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 30, Subaddress 3, RX, 32 words.
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553CommandCompose(RT_ADRS + 10, RT_SUBADRS, MXF_MIL1553_MSGTYPE_RX, 0, out minorFrame[2].command); // specification : 0 means 32 words
minorFrame[2].modulo = 1;
minorFrame[2].buffer = bcBufferTx[2];
minorFrame[2].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, 3, minorFrame, ref propertiesMinorFrame);
}
}
// Set BC data for address 20, subaddress 3, RX
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = 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 * index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[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 status error
txRec1553.dataSize = 0;
for (index = 0; index < 36; index++)
txRec1553.data[index] = 0;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[0], 1, txBuffer);
}
// Set BC data for address 10, subaddress 3, RX
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = 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 * index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[1], 1, txBuffer);
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 1; // do not respond error
txRec1553.dataSize = 0;
for (index = 0; index < 36; index++)
txRec1553.data[index] = 0;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[1], 1, txBuffer);
}
// Set BC data for address 30, subaddress 3, RX
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 3; // word 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 * index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[2], 1, txBuffer);
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 2; // respond on error error
txRec1553.dataSize = 0;
for (index = 0; index < 36; index++)
txRec1553.data[index] = 0;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[2], 1, txBuffer);
}
// Start messages queues
Console.Write("Starting BC - RT\n\r");
for (index = 0; index < NB_RT_BUFFERS && rc == MAXT_SUCCESS; index++)
{
rc = mxfMIL1553RtEnableSet(rt[index], 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=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(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);
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);
if ((rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_NO_RESPONSE) == 0)
{
Console.Write("\t\tRT status: Override {0}\n\r", (rxRec1553[0].data[msgInfo[0].statusIndex[0]] & MXF_MIL1553_STATUS_MSGERR) > 0 ? "Injected" : "Not injected");
Console.Write("\t\tRT response OK\n\r");
}
else
Console.Write("\t\tRT response Error\n\r");
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_UNEXPECTED:
Console.Write("BC to RT{0} SA{1} (unexpected command) Bus{2} (0x{3:x4}) Control : 0x{4:x8}\n\r", address, subAddress, (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_BUS_B) > 0 ? "B" : "A", rxRec1553[0].data[0], rxRec1553[0].control);
if ((rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_NO_RESPONSE) > 0)
{
Console.Write("\t\tRT response Error\n\r");
}
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);
}
for (index = 0; index < NB_RT_BUFFERS && rc == MAXT_SUCCESS; index++)
{
rc = mxfMIL1553RtEnableSet(rt[index], VMXF_DISABLE);
}
// 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; 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; 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 03/30/2022