MX Foundation 4
ar717_buffer_threshold.cs
/************************************************************************************************
//
// File:
// ar717_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
// - loopback between first TX and RX MULTI_EH 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 ar717_example
{
class ar717_buffer_threshold
{
const int DURATION_ms = 50000;
const int TXALMOSTFULL = 7;
const int TXALMOSTEMPTY = 3;
const int RXALMOSTFULL = 5;
const int RXALMOSTEMPTY = 2;
const int SUBFRAMESIZE = 64;//word per subframe (64, 128, 256 or 512)
static UInt32 TXAsyncEvents = 0;
public 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, txBuffer = 0;
var RXasyncEventInfo = new MXF_ASYNCEVENT_CONDITION[1];
var TXasyncEventInfo = new MXF_ASYNCEVENT_CONDITION[1];
UInt64 moduleCount = 0, channelCount = 0;
uint bufferSize;
IntPtr hostBuffer = IntPtr.Zero;
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
MXF_ASYNCEVENT_HANDLER _asyncEventHandler = asyncEventHandler;
// Connects 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;
}
// Initializes init callback handler to set first TX and RX channel to A717
// Initializes the server
Console.Write("\nStarting\n");
rc = mxfSystemInit(server);
// Gets the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Gets handle of first multi_eh module
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MULTI_EH, 1, out moduleCount, module);
// If module not found, returns an error
if ((rc == MAXT_SUCCESS) && (moduleCount == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Gets handle of first A717 RX channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A717, MXF_SCLASS_RX_CHANNEL, 1, out channelCount, rxChannel);
// Gets handle of first A717 TX channel
if ((rc == MAXT_SUCCESS) && (channelCount != 0))
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A717, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, txChannel);
// If channel not found, returns an error
if ((rc == MAXT_SUCCESS) && (channelCount == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Enables loopback
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A717_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Sets timebase to 64-bit nanoseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Allocates host buffer
bufferSize = (uint)(TXALMOSTFULL * Marshal.SizeOf(typeof(MXF_A717_DATAREC)));
if (rc == MAXT_SUCCESS)
{
try
{
hostBuffer = Marshal.AllocHGlobal((int)bufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Sets RX & TX channels subframe size
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A717_SUBFRAME_SIZE, SUBFRAMESIZE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_A717_SUBFRAME_SIZE, SUBFRAMESIZE);
// Sets RX & TX channels bit encoding to harvard
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A717_BIT_ENCODING, VMXF_A717_BIT_ENCODING_HARVARDBIPHASE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_A717_BIT_ENCODING, VMXF_A717_BIT_ENCODING_HARVARDBIPHASE);
// Sets RX & TX channels electrical selection to default
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A717_ELECTRICAL_SELECTION, VMXF_A717_ELECTRICAL_SELECT_DEFAULT);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_A717_ELECTRICAL_SELECTION, VMXF_A717_ELECTRICAL_SELECT_DEFAULT);
// Sets the event handler
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventHandlerInit(server, asyncEventHandler, hostBuffer, out asyncEvent);
// Allocates RX acquisition buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(rxChannel[0], bufferSize, out rxBuffer, IntPtr.Zero);
// Sets 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);
}
// Sets acquisition mode
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Starts acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
Console.Write("Acquisition started\n\r");
// Allocates TX Periodic Update buffer
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicBufferAlloc(txChannel[0], MXF_TXAPERIODIC_PRIORITY_HIGH, bufferSize, out txBuffer, IntPtr.Zero);
// Sets the TX async event condition
if (rc == MAXT_SUCCESS)
{
TXasyncEventInfo[0].condID = MXF_ASYNCEVENT_COND_TXAPERIODIC_BUFFER_THRESHOLD;
TXasyncEventInfo[0].condition.txAperiodicBufferThreshold.buffer = txBuffer;
TXasyncEventInfo[0].condition.txAperiodicBufferThreshold.almostFull = TXALMOSTFULL;
TXasyncEventInfo[0].condition.txAperiodicBufferThreshold.almostEmpty = TXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, Convert.ToUInt64(true), 1, TXasyncEventInfo);
}
// Waits for transmission and reading to occur
mxfSleep(DURATION_ms);
// Stops 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);
// Terminates async event handler
if (rc == MAXT_SUCCESS)
// Catches 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)
// Terminates
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 param)
{
var pendingList = new MXF_ASYNCEVENT_PENDING_INFO[64];
UInt64 maxCount = 64, pendingCount;
UInt64 i;
UInt32 rc;
// Gets the list of pending events to process
rc = mxfAsyncEventPendingGet(asyncEvent, maxCount, out pendingCount, pendingList);
for (i = 0; (rc == MAXT_SUCCESS) && (i < pendingCount); i++)
{
switch (pendingList[i].condID)
{
case MXF_ASYNCEVENT_COND_TXAPERIODIC_BUFFER_THRESHOLD:
// An almost empty condition was detected...
writeMsgs(pendingList[i].condition.txAperiodicBufferThreshold.buffer, param);
break;
case MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
readAcquisition(pendingList[i].condition.rxAcqBufferThreshold.buffer, param);
break;
default:
Console.Write("Unknown condID 0x{0:x16})", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Aperiodic Transmission
//****************************************************************************************************************
private static UInt32 writeMsgs(UInt64 buffer, IntPtr hostBuffer)
{
UInt32 rc = 0;
UInt32 i;
IntPtr recPtr = hostBuffer;
UInt64 word;
{
data = new UInt16[8192]
};
// Refills the FIFO
for (i = 0; (rc == MAXT_SUCCESS) && (i < TXALMOSTFULL); i++)
{
rec.timeTag = 0;
rec.control = 0;
rec.dataSize = 2 * (UInt32)SUBFRAMESIZE; // 16 bits per word in subframe
rec.repeatCount = 1;
rec.reserved = 0;
for (word = 0; word < SUBFRAMESIZE; word++)
{
if (word == 0)
{
//1st word of each subframe has to be a sync word
// sync words are:
// - 0x247 for subframe #0
// - 0x5B8 for subframe #1
// - 0xA47 for subframe #2
// - 0xDB8 for subframe #3
switch (i % 4)
{
case 0:
rec.data[word] = 0x247;
break;
case 1:
rec.data[word] = 0x5B8;
break;
case 2:
rec.data[word] = 0xA47;
break;
case 3:
rec.data[word] = 0xDB8;
break;
default:
break;
}
}
else
rec.data[word] = (UInt16)(0x11 * word);
}
if (rc == MAXT_SUCCESS)
{
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfA717NextDataRecordPtrGet(recPtr, out recPtr);
}
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Transmitting ...\n");
// Transmits strings on relative record time
rc = mxfA717TxAperiodicWrite(buffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, TXALMOSTFULL, hostBuffer);
}
if (rc != MAXT_SUCCESS)
Console.Write("Periodic Update failed; rc=0x{0:x8}\n", rc);
else
Console.Write("\nAsync Event {0} - Writing {1} records\n", ++TXAsyncEvents, i);
return rc;
}
/***************************************************************************************************************/
// RX717ReadAcquisitionData
/***************************************************************************************************************/
private static UInt32 readAcquisition(UInt64 rxBuffer, IntPtr rxHostBuffer)
{
IntPtr recPtr = IntPtr.Zero;
UInt64 status, msgsCount, bytesCount;
UInt64 word, data;
UInt32 rc;
uint bufferSize = (uint)(RXALMOSTFULL * Marshal.SizeOf(typeof(MXF_A717_DATAREC)));
{
data = new UInt16[8192]
};
// Reads and display records
rc = mxfA717RxAcqRead(rxBuffer, 0, bufferSize, out status, out msgsCount, out bytesCount, rxHostBuffer);
if (rc == MAXT_SUCCESS)
Console.Write("String received count = {0} \n", msgsCount);
if (rc == MAXT_SUCCESS)
{
// Displays received strings
recPtr = rxHostBuffer;
for (data = 0; (data < msgsCount) && (rc == MAXT_SUCCESS); data++)
{
rec = (MXF_A717_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_A717_DATAREC));
Console.Write("\n{0:D2}: Timetag={1:D12}, Size={2} words\n", data, rec.timeTag, (rec.dataSize) / 2);
for (word = 0; word < SUBFRAMESIZE; word++)
Console.Write("{0:X3} ", rec.data[word]);
Console.Write("\n");
rc = mxfA717NextDataRecordPtrGet(recPtr, out recPtr);
}
}
if (rc != MAXT_SUCCESS)
Console.Write("Acquisition read failed; rc=0x{0:x8}\n", rc);
return rc;
}
private static UInt32 initHandler(UInt64 server, UInt64 deviceIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
{
UInt64 device;
MXF_DEVICE_INFO deviceInfo = new MXF_DEVICE_INFO();
UInt32 rc;
if (attrib == KMXF_CHANNEL_CLASS)
{
rc = mxfSystemDeviceGet(server, deviceIndex, out device);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceInfoGet(device, out deviceInfo);
if ((rc == MAXT_SUCCESS) && (deviceInfo.modules[moduleIndex].type == MXF_MODULE_MULTI_EH))
{
// Sets MXF_MODULE_MULTI_EH first TX and RX channels to A717
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
value = MXF_CLASS_A717;
return Convert.ToUInt32(true);
}
}
}
return Convert.ToUInt32(false);
}
}
}
Updated 10/23/2023