MX Foundation 4
ar708_buffer_threshold.cs
/************************************************************************************************
//
// File:
// ar708_buffer_threshold.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 illustrates how to manage buffer thresholds with asynchronous events.
// The demo will transmit and record the messages using asynchronous
// events.
//
// Hardware Requirements:
// - MAXT FlexMulti-1553 or FlexMulti-629 with A708 option.
// - Loopback between TX and RX channels if internal loopback is not used.
//
**************************************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar708_example
{
class ar708_buffer_threshold
{
private const int DURATION_ms = 6000;
private const int TXALMOSTFULL = 100;
private const int TXALMOSTEMPTY = 30;
private const int RXALMOSTFULL = 10;
private const int RXALMOSTEMPTY = 5;
private const int SCANFIELD = 0x7FF8;
private const int LABELFIELD = 0x00FF;
private const int CTRLACCEPFIELD = 0x0300;
private const int MODEFIELD = 0x1C00;
private const int TILTFIELD1 = 0xE000;
private const int TILTFIELD2 = 0x000F;
private const int RANGEFIELD = 0xFC00;
private const int GAINFIELD = 0x03F0;
private const int DATAACCEPTFIELD = 0x00006;
private const int BINFIELD = 0x00007;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
var module = new UInt64[1];
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 asyncEvent = 0;
UInt64 rxBuffer = 0;
var txBuffer = new UInt64[1];
var RXasyncEventInfo = new MXF_ASYNCEVENT_CONDITION[1];
var TXasyncEventInfo = new MXF_ASYNCEVENT_CONDITION[1];
IntPtr rxHostBuffer = IntPtr.Zero;
IntPtr txHostBuffer = IntPtr.Zero;
UInt64 moduleCount = 0, channelCount = 0;
uint txBufferSize, rxBufferSize;
UInt64 schedule = 0;
UInt64 msgSched;
MXF_ASYNCEVENT_HANDLER _asyncEventHandler = asyncEventHandler;
// Connect to services and initialize environment
#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
if (rc != MAXT_SUCCESS)
{
Console.Write("Failed to connect; rc=0x{0:x8}", rc);
Console.WriteLine();
Console.WriteLine("Press a key to terminate");
Console.Read();
return;
}
// Initialize the server
Console.WriteLine();
Console.WriteLine("Starting");
rc = mxfSystemInit(server);
// Get the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
//Get Module handle
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A708_EH, 1, out moduleCount, module);
// If module not found, return an error
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Activates the ARINC 708 module (because the MIL-STD-1553 is by default activated, except on FM1553-2)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_A708_MODULE_ACTIVE, Convert.ToUInt64(true));
if (rc == MAXT_ERROR_NOT_SUPPORTED)
rc = MAXT_SUCCESS;
//Get handle of first ARINC 708 RX Channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A708, MXF_SCLASS_RX_CHANNEL, 1, out channelCount, rxChannel);
//Get handle of first A708 Tx Channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A708, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, txChannel);
// Enable loopback
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A708_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
//Allocates 4KB buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = 720 * 200;
//Allocates Tx Aperiodic static buffer for HIGH priority queue
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], 0, txBufferSize, out txBuffer[0], IntPtr.Zero);
//Host buffer allocation
if (rc == MAXT_SUCCESS)
{
try
{
txHostBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
//Allocate 4kb for Rx data
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 720 * 200;
//Allocates Rx acquisition static buffer
rc = mxfRxAcqBufferAlloc(rxChannel[0], rxBufferSize, out rxBuffer, IntPtr.Zero);
try
{
rxHostBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
//Sets timebase to RTC nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Set the event handler
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventHandlerInit(server, asyncEventHandler, txHostBuffer, out asyncEvent);
// Set the RX async event condition
if (rc == MAXT_SUCCESS)
{
RXasyncEventInfo[0].condID = MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD;
RXasyncEventInfo[0].condition.rxAcqBufferThreshold.buffer = rxBuffer;
RXasyncEventInfo[0].condition.rxAcqBufferThreshold.almostFull = RXALMOSTFULL;
RXasyncEventInfo[0].condition.rxAcqBufferThreshold.almostEmpty = RXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(true), Convert.ToUInt64(RXasyncEventInfo.Length), RXasyncEventInfo);
}
// Set acquisition mode
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Start acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Acquisition started");
// Set the TX async event condition
if (rc == MAXT_SUCCESS)
{
TXasyncEventInfo[0].condID = MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD;
TXasyncEventInfo[0].condition.txPeriodicUpdateMsgBufferThreshold.channel = txChannel[0];
TXasyncEventInfo[0].condition.txPeriodicUpdateMsgBufferThreshold.almostFull = TXALMOSTFULL;
TXasyncEventInfo[0].condition.txPeriodicUpdateMsgBufferThreshold.almostEmpty = TXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(true), 1, TXasyncEventInfo);
}
// Select message for async event condition
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventTxPeriodicUpdateMsgSelectSet(asyncEvent, txChannel[0], MXF_MSG_SELECT_ONLY, Convert.ToUInt64(txBuffer.Length), txBuffer);
// Set the Periodic Scheduler
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleNew(txChannel[0], out schedule);
// Set scheduling values: Rate=5 ms, Phase=0 us
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleSingleMsgAdd(schedule, 5000000, 0, txBuffer[0], out msgSched);
// Run the schedule
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleRun(schedule);
// Send messages for 2 seconds
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Running transmission...");
mxfSleep(DURATION_ms);
rc = mxfTxPeriodicScheduleFree(schedule);
}
// Stop acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
// Disable conditions
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(false), 1, RXasyncEventInfo);
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(false), 1, TXasyncEventInfo);
// Terminate async event handler
if (rc == MAXT_SUCCESS)
// Catch any previous failing function
if (rc != MAXT_SUCCESS)
{
StringBuilder buffer = new StringBuilder(256);
if (mxfSystemErrorStringGet(server, rc, 256, buffer) != MAXT_SUCCESS)
buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
Console.WriteLine(buffer);
}
//Frees all buffers
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer[0] != 0)
// Terminate
if (txHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txHostBuffer);
if (rxHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxHostBuffer);
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
//****************************************************************************************************************
// Asynchronous Event Handler
//****************************************************************************************************************
private static UInt32 asyncEventHandler(UInt64 asyncEvent, IntPtr context)
{
UInt64 maxCount = 64;
var pendingList = new MXF_ASYNCEVENT_PENDING_INFO[maxCount];
UInt64 pendingCount;
UInt32 rc;
// Get the list of pending events to process
rc = mxfAsyncEventPendingGet(asyncEvent, maxCount, out pendingCount, pendingList);
for (UInt64 i = 0; (rc == MAXT_SUCCESS) && (i < pendingCount); i++)
{
switch (pendingList[i].condID)
{
case MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD:
// An almost empty condition was detected...
updateMsgs(pendingList[i].condition.txPeriodicUpdateMsgBufferThreshold.buffer, context);
break;
case MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
readAcquisition(pendingList[i].condition.rxAcqBufferThreshold.buffer, context);
break;
default:
Console.Write("Unknown condID {0})", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Periodic Transmission
//****************************************************************************************************************
private static UInt32 updateMsgs(UInt64 buffer, IntPtr txHostBuffer)
{
UInt32 rc = 0;
UInt32 i, bin;
IntPtr recPtr = txHostBuffer;
UInt32 TXAsyncEvents = 0;
UInt16 scanAngle = 0, binValue = 0;
rec.data = new UInt16[128];
// Refills the FIFO
for (i = 0; rc == MAXT_SUCCESS && i < TXALMOSTFULL; i++, scanAngle++)
{
if (scanAngle > 720)
{
scanAngle = 0;
binValue++;
}
rec.timeTag = 0;
rec.control = 0;
rec.dataSize = 200; //200 bytes corresponds to 1600 bits (see ARINC 708 specification)
rec.repeatCount = 1;
rec.data[0] = 0;
rec.data[1] = 0;
rec.data[2] = 0;
rec.data[3] = 0;
setLabel(rec, 055); // Bits 0-7 : Label
setCtrlAccept(rec, 0x3); // Bits 8-9 : Cntrol Accept
setMode(rec, 1); // Mode Set 1
setTilt(rec, 0);
setGain(rec, -16);
setRange(rec, 80);
setDataAccept(rec, 3);
setScanAngle(rec, ((double)scanAngle * 0.25) + 270);
for (bin = 1; bin <= 512; bin++)
{
setBin(rec, (ushort)bin, binValue);
}
if (rc == MAXT_SUCCESS)
{
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfA708NextDataRecordPtrGet(recPtr, out recPtr);
}
}
// Add more data to the buffer
if (rc == MAXT_SUCCESS)
rc = mxfA708TxPeriodicUpdateMsgWrite(buffer, TXALMOSTFULL, txHostBuffer);
//if(!rc)
//DisplayDataArray(TXALMOSTFULL, txHostBuffer);
if (rc != MAXT_SUCCESS)
Console.WriteLine("Periodic Update failed");
else
Console.WriteLine("\nAsync Event {0} - Writing {1} records", ++TXAsyncEvents, i);
return rc;
}
//****************************************************************************************************************
// Acquisition Reception
//****************************************************************************************************************
private static UInt32 readAcquisition(UInt64 buffer, IntPtr hostBuffer)
{
UInt64 status, msgsCount, bytesCount;
uint bufferSize = (RXALMOSTFULL * 200);
UInt32 rc;
// Read and display records
rc = mxfA429RxAcqRead(buffer, 0, bufferSize, out status, out msgsCount, out bytesCount, hostBuffer);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Read {0} messages", msgsCount);
//if (rc == MAXT_SUCCESS)
// DisplayDataArray(msgsCount, rxHostBuffer);
if (rc != MAXT_SUCCESS)
Console.WriteLine("Acquisition read failed; rc=0x{0:x8}\n", rc);
return rc;
}
/********************************************************************************************************************/
//DisplayDataArray
/********************************************************************************************************************/
private static void DisplayDataArray(UInt64 recNum, IntPtr rxHostBuffer)
{
UInt64 iRec, iData;
IntPtr p = rxHostBuffer;
Console.WriteLine();
for (iRec = 0; iRec < recNum; iRec++)
{
rec = (MXF_A708_DATAREC)Marshal.PtrToStructure(p, typeof(MXF_A708_DATAREC));
Console.WriteLine("{0}: Timetag: {1} Control: {2:x2} Size: {3}", iRec, rec.timeTag, rec.control, rec.dataSize);
Console.Write("Data: ");
for (iData = 0; iData < rec.dataSize / 2; iData++)
{
Console.WriteLine("{0:x2}", rec.data[iData]);
}
Console.WriteLine();
}
}
private static void setLabel(MXF_A708_DATAREC rec, uint label)
{
rec.data[0] = (UInt16)(rec.data[0] & ~LABELFIELD | (UInt16)label);
}
private static void setCtrlAccept(MXF_A708_DATAREC rec, UInt16 ctrlAccept)
{
rec.data[0] = (UInt16)(rec.data[0] & ~CTRLACCEPFIELD | (UInt16)(CTRLACCEPFIELD & (ctrlAccept << 8)));
}
private static void setMode(MXF_A708_DATAREC rec, UInt16 mode)
{
rec.data[1] = (UInt16)((rec.data[1] & ~MODEFIELD) | (UInt16)(MODEFIELD & (mode << 10)));
}
private static void setTilt(MXF_A708_DATAREC rec, double tilt)
{
rec.data[1] = (UInt16)((rec.data[1] & ~TILTFIELD1) | (UInt16)(TILTFIELD1 & ((UInt16)(tilt / 0.25) << 13)));
rec.data[2] = (UInt16)((rec.data[2] & ~TILTFIELD2) | (UInt16)(TILTFIELD2 & ((UInt16)(tilt / 0.25) >> 3)));
}
private static void setGain(MXF_A708_DATAREC rec, Int16 gain)
{
rec.data[2] = (UInt16)((rec.data[2] & ~GAINFIELD) | (UInt16)(GAINFIELD & ((gain / -1) << 4)));
}
private static void setRange(MXF_A708_DATAREC rec, UInt16 range)
{
rec.data[2] = (UInt16)((rec.data[2] & ~RANGEFIELD) | (UInt16)(RANGEFIELD & ((range/5) << 10)));
}
private static void setDataAccept(MXF_A708_DATAREC rec, UInt16 dataAccept)
{
rec.data[3] = (UInt16)((rec.data[3] & ~DATAACCEPTFIELD) | (UInt16)(DATAACCEPTFIELD & (dataAccept<<1)));
}
private static void setScanAngle(MXF_A708_DATAREC rec, double angle)
{
rec.data[3] = (UInt16)((rec.data[3] & ~SCANFIELD) | (UInt16)(SCANFIELD & ((UInt16)(angle/0.0879) << 3)));
}
private static void setBin(MXF_A708_DATAREC rec, UInt16 bin, UInt16 val)
{
UInt16 word_index = (ushort)(((bin - 1) * 3 + 64) / 16);
UInt16 startbit = (ushort)(((bin - 1) * 3) % 16);
UInt16 bin_field;
if (startbit < 14)
{
bin_field = (ushort)(BINFIELD << (startbit));
rec.data[word_index] = (UInt16)((rec.data[word_index] & ~bin_field) | (UInt16)(bin_field & (val << startbit)));
}
if (startbit >= 14)
{
bin_field = (ushort)(BINFIELD << startbit);
rec.data[word_index] = (UInt16)((rec.data[word_index] & ~bin_field) | (UInt16)(bin_field & (val << startbit)));
bin_field = (ushort)(BINFIELD >> (16 - startbit));
rec.data[word_index + 1] = (UInt16)((rec.data[word_index + 1] & ~bin_field) | (UInt16)(bin_field & (val >> (16-startbit))));
}
}
}
}
Updated 10/23/2023