MX Foundation 4
mil1553_errors.cs
/*****************************************************************************
//
## File:
## mil1553_errors.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 usage of error injection,
// retry functionality and statistics counter with MIL-STD-1553 channel.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti.
//
*****************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_exemple
{
public class mil1553_errors
{
const UInt64 BUF_SA3_RX = 0;
const UInt64 RT_ADRS = 20;
const UInt64 RT_SUBADRS = 3;
const UInt64 RT_ADRS_ERROR = 5;
static void Main(string[] args)
{
UInt64 server;
var device = new UInt64[1];
var module = new UInt64[1];
var bc = new UInt64[1];
var rt = new UInt64[1];
var bcBufferTx = new UInt64[1];
UInt64 bcBufferRx = 0;
var rtBuffer = new UInt64[1];
IntPtr txBuffer = IntPtr.Zero;
UInt32 txBufferSize = 20 * 1024;
var txRec1553 = new MXF_MIL1553_DATAREC();
IntPtr rxBuffer = IntPtr.Zero;
UInt32 rxBufferSize = 20 * 1024;
var rxRec1553 = new MXF_MIL1553_DATAREC[1];
var minorFrame = new MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG[1];
var msgInfo = new MXF_MIL1553_MSGINFO[1];
var err = new MXF_MIL1553_ERROR_INJ();
err.errors = new UInt16[34];
var transGlobal = new MXF_MIL1553_STAT_TRANSACTION_GLOBAL();
var errorGlobal = new MXF_MIL1553_STAT_ERROR_GLOBAL();
var errorString = new StringBuilder(200);
UInt64 bus = MXF_MIL1553_BUS_A | MXF_MIL1553_BUS_B;
UInt64 rxAcqStatus;
UInt64 msgCount = 0;
UInt64 byteCount, indexBuffer;
UInt64 rxRec;
UInt32 loop = 0;
UInt64 address, subAddress, dir, wordCount;
UInt32 msg;
UInt64 deviceCount = 0;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt32 rc;
#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 = mxfModuleChannelAllGet(module[0], MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, 1, out channelCount, bc);
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Get handle of MIL-STD-1553 remote terminal channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 2 + RT_ADRS_ERROR, out rt[0]);
#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 (msg = 0; msg < 1 && rc == MAXT_SUCCESS; msg++)
{
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc[0], msg, txBufferSize, out bcBufferTx[msg], IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt[0], msg, txBufferSize, out rtBuffer[msg], IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt[0], MXF_MIL1553_MSGTYPE_RX, 3, bus, rtBuffer[msg]);
}
// 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[0], rxBufferSize, out bcBufferRx, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferStatusGet(bcBufferRx, out UInt64 status, out UInt64 realMsgCount, out UInt64 pendingBytes, out msgCount);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
rxBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Enable statistics counter
if (rc == MAXT_SUCCESS)
rc = mxfChannelStatisticEnableSet(bc[0], VMXF_ENABLE);
// Enable discrete output pulse on bus error from bus A on discrete output pin #0
if (rc == MAXT_SUCCESS)
rc = mxfChannelDiscreteOutputTriggerEnableSet(bc[0], MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSA_ERROR, VMXF_ENABLE, 0);
// Set timebase to RTC nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Configure injection #0, override address
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_ADDRESS, RT_ADRS_ERROR, out err.errors[0]);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553ErrorInjectionSet(bc[0], 0, ref err);
}
// Configure injection #1, parity error
if (rc == MAXT_SUCCESS)
{
err.errors[0] = MXF_MIL1553_ERRORID_PAR;
rc = mxfMIL1553ErrorInjectionSet(bc[0], 1, ref err);
}
// Set the minor frame #0 using 1 Command
if (rc == MAXT_SUCCESS)
{
// Command #0 : Address 20, Subaddress 3, RX, 3 words. Retry on alternate bus when bus error detected
rc = mxfMIL1553CommandCompose(RT_ADRS, RT_SUBADRS, MXF_MIL1553_MSGTYPE_RX, 3, out minorFrame[0].command);
if (rc == MAXT_SUCCESS)
{
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[BUF_SA3_RX];
minorFrame[0].retryHaltOptions = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_RETRY_ERROR_ENABLE | MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_RETRY_ALT_BUS;
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc[0], 0, 0, 1, minorFrame, IntPtr.Zero);
}
}
//Set BC data for address 20, subaddress 3, RX
if (rc == MAXT_SUCCESS)
{
txRec1553.data = new UInt16[36];
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 0; // override address
txRec1553.dataSize = 8; //8 bytes (command + 3 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = 0x0000;
txRec1553.data[2] = 0x1111;
txRec1553.data[3] = 0x2222;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[BUF_SA3_RX], 1, txBuffer);
}
// Set RT data for address 5, subaddress 3, RX
if (rc == MAXT_SUCCESS)
{
txRec1553.data = new UInt16[36];
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 1; // parity error
txRec1553.dataSize = 8; //8 bytes (command + 3 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = 0x0000;
txRec1553.data[2] = 0x1111;
txRec1553.data[3] = 0x2222;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[BUF_SA3_RX], 1, txBuffer);
}
// Start messages queues
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting BC - RT\n\r");
rc = mxfMIL1553RtEnableSet(rt[0], VMXF_ENABLE);
}
// Start BC acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(bcBufferRx, 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(bcBufferRx, 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[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} Bus{3} (0x{4:x4})\n\r", address, subAddress, wordCount, (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_BUS_B) > 0 ? "B" : "A", rxRec1553[0].data[0]);
Console.Write("\t\tRT status: 0x{0:x4}\n\r", rxRec1553[0].data[msgInfo[0].statusIndex[0]]);
Console.Write("\t\tParity {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_PARITY_ERROR) > 0 ? "Error" : "OK");
break;
}
}
}
// Get next msg
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
mxfSleep(500);
loop++;
} while (loop < 10 && rc == MAXT_SUCCESS);
}
// Stop acquisition
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqStop(bcBufferRx);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqClear(bcBufferRx);
}
// Stop the major frame
if (rc == MAXT_SUCCESS)
{
Console.Write("Stopping BC - RT\n\r");
rc = mxfTxPeriodicMajorFrameStop(bc[0], 0, 0);
}
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtEnableSet(rt[0], VMXF_DISABLE);
// Get total number of transactions
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553StatisticTransactionGlobalGet(bc[0], out transGlobal);
if (rc == MAXT_SUCCESS)
Console.Write("Total number of transactions: {0}\n\r", transGlobal.totalCount);
}
// Get total number of errors
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553StatisticErrorGlobalGet(bc[0], out errorGlobal);
if (rc == MAXT_SUCCESS)
Console.Write("Total number of errors: {0}\n\r", errorGlobal.totalError);
}
// 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 < 1; indexBuffer++)
{
if (bcBufferTx[0] > 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(bcBufferTx[indexBuffer]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
for (indexBuffer = 0; indexBuffer < 1; indexBuffer++)
{
if (rtBuffer[0] > 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(rtBuffer[indexBuffer]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
if (bcBufferRx > 0)
{
rc = mxfRxAcqBufferFree(bcBufferRx);
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