MX Foundation 4
ar429_buffer_threshold.cs
/************************************************************************************************
//
// File:
// ar429_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 a ramp on one label and record the messages using asynchronous
// events. The first record of each update will have a parity error.
//
// Hardware Requirements:
// - MAXT Flex or PCI-500 carrier
// - Loopback between first TX and RX ARINC 429 channels.
//
**************************************************************************************************/
#define LOOPBACK
#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar429_example
{
class ar429_buffer_threshold
{
private const int BUFFER_SIZE = 1 * 1024 * 1024; // 1 MiB
private const int TXALMOSTFULL = 7;
private const int TXALMOSTEMPTY = 3;
private const int RXALMOSTFULL = 5;
private const int RXALMOSTEMPTY = 2;
private const int TX_MSG_LABEL = 5;
private const int TX_MSG_SDI = 0;
private static UInt64 data = 0;
private static UInt32 TXAsyncEvents = 0;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
UInt64 module = 0;
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];
UInt64 schedule = 0;
UInt64 msgSched;
UInt64 count;
UInt64 type = 0;
UInt64 options = 0;
IntPtr hostBuffer = IntPtr.Zero;
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.Write("\nPress a key to terminate\n");
Console.Read();
return;
}
// Initialize the server
Console.Write("\nStarting\n");
rc = mxfSystemInit(server);
// Get the first ARINC 429 Protocol RX channel (RX logical #0)
if (rc == MAXT_SUCCESS)
rc = mxfChannelAllGet(server, MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, 1, out count, rxChannel);
// Obtain the first ARINC 429 Protocol TX channel (TX logical #0)
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfChannelAllGet(server, MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 1, out count, txChannel);
// If channel not found, return an error
if ((rc == MAXT_SUCCESS) && (count == 0))
rc = MAXT_ERROR_NOT_FOUND;
//Get module type
if (rc == MAXT_SUCCESS)
rc = mxfChannelInfoGet(rxChannel[0], out device, out module);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Get(module, KMXF_MODULE_TYPE, out type);
// Set the channels to low speed
if (rc == MAXT_SUCCESS)
{
// If IPM-429
if (type == MXF_MODULE_A429E)
{
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A429_SPEED_SELECT, VMXF_A429_SPEED_SELECT_LOW);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_A429_SPEED_SELECT, VMXF_A429_SPEED_SELECT_LOW);
}
else
{
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A429_SPEED, 12500);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_A429_SPEED, 12500);
}
}
// If Flex429 bidirectionnal, enable Tx port
if ((rc == MAXT_SUCCESS) && (type == MXF_MODULE_FLEX429))
{
rc = mxfAttributeUint64Get(module, KMXF_MODULE_OPTIONS, out options);
if((rc == MAXT_SUCCESS) && ((options & VMXF_A429_MODULE_OPTIONS_BIDIRECTIONAL) == VMXF_A429_MODULE_OPTIONS_BIDIRECTIONAL))
rc = mxfAttributeUint64Set(txChannel, KMXF_A429_TX_PORT_ENABLE, VMXF_ENABLE);
}
// Enable loopback
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Set timebase to 64-bit nanoseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Alloc host buffer
if (rc == MAXT_SUCCESS)
{
try
{
hostBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Set the event handler
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventHandlerInit(server, asyncEventHandler, hostBuffer, out asyncEvent);
// Allocate RX acquisition buffer (1 MiB)
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(rxChannel[0], BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// 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), 1, RXasyncEventInfo);
}
// Set acquisition mode
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Select all labels to be received
if (rc == MAXT_SUCCESS)
rc = mxfA429RxAcqMsgSelectSet(rxBuffer, MXF_MSG_SELECT_ADD, 0, IntPtr.Zero);
// Start acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
Console.Write("Acquisition started\n\r");
// Allocate TX Periodic Update buffer (1MiB)
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], TX_MSG_LABEL, BUFFER_SIZE, out txBuffer[0], IntPtr.Zero);
// 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, 1, txBuffer);
// Set the Periodic Scheduler
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleNew(txChannel[0], out schedule);
// Set scheduling values: Rate=25 ms, Phase=0 us
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleSingleMsgAdd(schedule, 25000000, 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.Write("Running periodic transmission...\n\r");
mxfSleep(2000);
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, (UInt32)buffer.Capacity, buffer) != MAXT_SUCCESS)
{
buffer.Clear();
buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
}
Console.Write(buffer + "\n\r");
}
//Frees all buffers
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer[0] != 0)
// Terminate
if (hostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(hostBuffer);
Console.Write("\nPress enter to terminate\n");
Console.Read();
return;
}
//****************************************************************************************************************
// Asynchronous Event Handler
//****************************************************************************************************************
private static UInt32 asyncEventHandler(UInt64 asyncEvent, IntPtr context)
{
UInt64 maxCount = 64, pendingCount;
var pendingList = new MXF_ASYNCEVENT_PENDING_INFO[maxCount];
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...
rc = updateMsgs(pendingList[i].condition.txPeriodicUpdateMsgBufferThreshold.buffer, context);
break;
case MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
rc = 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 hostBuffer)
{
UInt32 rc = 0;
UInt32 i;
IntPtr recPtr = hostBuffer;
UInt64 label, sdi, ssm, parity;
// Refill the FIFO in order to produce a ramp
for (i = 0; (rc == MAXT_SUCCESS) && (i < TXALMOSTFULL); i++)
{
// Send a parity error on first record
rec.timeTag = 0;
rec.control = (i != 0) ? 0 : MXF_A429_TX_REC_CTRL_PARITY_ERROR;
rec.repeatCount = 1;
rec.reserved = 0;
label = TX_MSG_LABEL;
sdi = TX_MSG_SDI;
data++;
ssm = 0;
parity = VMXF_A429_PARITY_ODD;
rc = mxfA429ArwCompose(label, sdi, data, ssm, parity, out rec.data);
if (rc == MAXT_SUCCESS)
{
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfA429NextDataRecordPtrGet(recPtr, out recPtr);
}
}
// Add more data to the buffer
if (rc == MAXT_SUCCESS)
rc = mxfA429TxPeriodicUpdateMsgWrite(buffer, i, hostBuffer);
if (rc != MAXT_SUCCESS)
Console.Write("\nPeriodic Update failed; rc=0x{0:x8}\n", rc);
else
Console.Write("\nAsync Event {0} - Writing {1} records\n", ++TXAsyncEvents, i);
return rc;
}
//****************************************************************************************************************
// Acquisition Reception
//****************************************************************************************************************
private static UInt32 readAcquisition(UInt64 buffer, IntPtr hostBuffer)
{
IntPtr recPtr = hostBuffer;
UInt64 status, msgsCount, bytesCount;
UInt64 label, sdi, data, ssm, parity;
UInt64 j;
UInt32 rc;
// Read and display records
rc = mxfA429RxAcqRead(buffer, 0, BUFFER_SIZE, out status, out msgsCount, out bytesCount, hostBuffer);
if (rc == MAXT_SUCCESS)
{
Console.Write("Read {0} messages\n\r", msgsCount);
for (j = 0; (rc == MAXT_SUCCESS) && (j < msgsCount); j++)
{
rec = (MXF_A429_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_A429_DATAREC));
rc = mxfA429ArwDecompose(rec.data, out label, out sdi, out data, out ssm, out parity);
if (rc == MAXT_SUCCESS)
{
Console.Write("{0:D2}: Timetag {1}", j, rec.timeTag);
Console.Write(" - Control : {0} ", (rec.control == MXF_A429_RX_REC_CTRL_PARITY_ERROR) ? "PARITY ERROR" : "NO ERROR");
Console.Write("- ARINC word=[{0},{1},{2:x5},{3},{4}]\n", Convert.ToString((int)label, 8).PadLeft(3, '0'), sdi, data, ssm, (parity == VMXF_A429_PARITY_ODD) ? "ODD" : "EVEN");
rc = mxfA429NextDataRecordPtrGet(recPtr, out recPtr);
}
}
}
if (rc != MAXT_SUCCESS)
Console.Write("Acquisition read failed; rc=0x{0:x8}\n", rc);
return rc;
}
}
}
Updated 10/23/2023