MX Foundation 4
ar429_embedded_discrete.cs
/*******************************************************************************
//
// File:
// ar429_embedded_discrete.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This demo shows how an ARINC 429 onboard application can transmit
// data when a triggered edge discrete input is received.
//
// The embedded program transmit one ARINC 429 label 004 at each discrete input #0
// rising edge detected. The transmission is sent on timetag, 25 msec
// later of discrete edge timetag.
//
// The embedded program "ar429_embedded_side_discrete.c" is required by this program
// and compiled executable (ar429_embedded_side_discrete-flex.trap) must be located in
// the same folder as the host application executable.
//
// Hardware Requirements:
// - MAXT Flex with loopback between first TX and RX Discrete pin and one TX ARINC 429 Enhanced channel.
//
*******************************************************************************/
//#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar429_example
{
class ar429_embedded_discrete
{
const int BUFFER_SIZE = 4096; // 4KB
// User Commands
const int USER_COMMAND_ID_START = 0;
const int USER_COMMAND_ID_STOP = 1;
const int C50ms = 50 * 1000 * 1000;
const int C10ms = 10 * 1000 * 1000;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
UInt64[] moduleDIO = new UInt64[1];
UInt64[] module429 = new UInt64[1];
UInt64 count = 0;
UInt64[] txDIO = new UInt64[1], rxDIO = new UInt64[1], tx429 = new UInt64[1], rx429 = new UInt64[1];
UInt64 txDIOBuffer = 0;
UInt64 tx429Buffer = 0;
UInt64 rx429Buffer = 0;
UInt64 dev, mod = 0, port = 0;
var parameters = new UInt32[3];
IntPtr rec429 = IntPtr.Zero;
// Connect to services and initialize environment
#if LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", Convert.ToUInt32(false), out server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", 0, 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.ReadKey();
return;
}
// Initialize the library
Console.Write("\nStarting\n");
rc = mxfSystemInitAttributeUint64CallbackHandler(server, EmbeddedMemoryAllocationHandler);
if (rc == MAXT_SUCCESS)
rc = mxfSystemInit(server);
// Get the first device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Get first module discrete
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_DIOFIFO_EH, 1, out count, moduleDIO);
// Get first discrete TX channel
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfModuleChannelAllGet(moduleDIO[0], MXF_CLASS_DISCRETE, MXF_SCLASS_TX_CHANNEL, 1, out count, txDIO);
// Get first discrete RX channel
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfModuleChannelAllGet(moduleDIO[0], MXF_CLASS_DISCRETE, MXF_SCLASS_RX_CHANNEL, 1, out count, rxDIO);
// If discrete channel not found, return an error
if (rc == MAXT_SUCCESS && count == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Get first module ARINC 429
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MULTI_EH, 1, out count, module429);
// Get the first ARINC 429 TX channel
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfModuleChannelAllGet(module429[0], MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, 1, out count, tx429);
// Get the first ARINC 429 RX channel
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfModuleChannelAllGet(module429[0], MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, 1, out count, rx429);
// If A429 channel not found, return an error
if (rc == MAXT_SUCCESS && count == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Set timebase to 64-bit microseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Enable discrete pin #0 rising edge detection
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxDIO[0], KMXF_DISCRETE_RX_EDGE_FALLING, 0x0000);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxDIO[0], KMXF_DISCRETE_RX_EDGE_RISING, 0x0001);
//Activate loopback before transmission and reception
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxDIO[0], KMXF_DISCRETE_TX_RX_TEST_LB, VMXF_ENABLE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rx429[0], KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate Aperiodic TX static buffer for HIGH priority queue
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicBufferAlloc(txDIO[0], MXF_TXAPERIODIC_PRIORITY_HIGH, BUFFER_SIZE, out txDIOBuffer, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicBufferAlloc(tx429[0], MXF_TXAPERIODIC_PRIORITY_HIGH, BUFFER_SIZE, out tx429Buffer, IntPtr.Zero);
// Allocate RX A429 acquisition buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(rx429[0], BUFFER_SIZE, out rx429Buffer, IntPtr.Zero);
// Allocate host buffer
if (rc == MAXT_SUCCESS)
{
try
{
rec429 = Marshal.AllocHGlobal((int)BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Start the acquisition process
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rx429Buffer, MXF_RXACQ_MODE_LINEAR);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqStart(rx429Buffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
Console.Write("\nAcquisition started\n\r");
}
// Download from the host (Client Side) the embedded application
if (rc == MAXT_SUCCESS)
rc = mxfEmbeddedCodeDownload(device, "ar429_embedded_side_discrete-flex.trap");
// Enable Embedded Handler
if (rc == MAXT_SUCCESS)
rc = mxfEmbeddedHandlerEnableSet(rxDIO[0], MXF_EMBEDDED_HANDLER_ID_RX, VMXF_ENABLE, 0);
if (rc == MAXT_SUCCESS)
rc = mxfChannelLocationGet(tx429[0], out dev, out mod, out port);
// Set the parameters to pass
if (rc == MAXT_SUCCESS)
{
parameters[0] = (UInt32)mod;
parameters[1] = (UInt32)port;
parameters[2] = (uint)MXF_TXAPERIODIC_PRIORITY_HIGH;
// Start the embedded code
rc = mxfEmbeddedCommandSend(device, USER_COMMAND_ID_START, (UInt64)(parameters.GetLength(0) * 4), parameters);
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Sending Periodic Pulses\n");
rc = TXDioSendAperiodicPulses(device, txDIOBuffer);
}
// Stop the embedded code
if (rc == MAXT_SUCCESS)
rc = mxfEmbeddedCommandSend(device, USER_COMMAND_ID_STOP, 0, IntPtr.Zero);
// Stop and read A429 acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rx429Buffer);
if (rc == MAXT_SUCCESS)
rc = RX429ReadAcquisitionData(rx429Buffer, rec429);
// 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");
}
Console.Write("Stopping\n");
//Frees all buffers
if (tx429Buffer > 0)
if (txDIOBuffer > 0)
if (rx429Buffer > 0)
mxfRxAcqBufferFree(rx429Buffer);
if (rec429 != IntPtr.Zero)
Marshal.FreeHGlobal(rec429);
// Terminate
Console.Write("\nPress enter to terminate\n");
Console.ReadKey();
return;
}
/******************************************************************************/
// EmbeddedMemoryHandler
/******************************************************************************/
private static UInt32 EmbeddedMemoryAllocationHandler(UInt64 server, UInt64 cardIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
{
// Allocate the maximum space available for the embedded application
UInt64 embedded_memory_size = 65536; // 64K
if (attrib == KMXF_DEVICE_EMBEDDED_CODEANDDATA_SIZE)
{
Console.Write("Reserving {0}K of memory for embedded application\n", embedded_memory_size / 1024);
value = embedded_memory_size;
return 1;
}
return 0;
}
/******************************************************************************/
// TXDioSendAperiodicPulses
/******************************************************************************/
private static UInt32 TXDioSendAperiodicPulses(UInt64 device, UInt64 txDIOBuffer)
{
var dioRec = new MXF_DISCRETE_DATAREC[2];
IntPtr recPtr = IntPtr.Zero;
IntPtr tempPtr = IntPtr.Zero;
UInt64 timer;
UInt64 pulseCount;
UInt32 rc;
// Set DIO record
rc = mxfDeviceTimerGet(device, out timer);
if (rc != MAXT_SUCCESS)
return rc;
try
{
recPtr = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
tempPtr = recPtr;
timer += C50ms;
dioRec[0].timeTag = timer;
dioRec[0].control = 0;
dioRec[0].repeatCount = 1;
dioRec[0].highDuration = 0;
dioRec[0].lowDuration = 0;
dioRec[0].data = 0x0001;
dioRec[0].edge = 0xFFFF;
Marshal.StructureToPtr(dioRec[0], tempPtr, true);
timer += C10ms;
dioRec[1].timeTag = timer;
dioRec[1].control = 0;
dioRec[1].repeatCount = 1;
dioRec[1].highDuration = 0;
dioRec[1].lowDuration = 0;
dioRec[1].data = 0x0000;
dioRec[1].edge = 0xFFFF;
mxfDiscreteNextDataRecordPtrGet(tempPtr, out tempPtr);
Marshal.StructureToPtr(dioRec[1], tempPtr, true);
// Simulate 10 pulses on discrete output (10 msec high, 50 msec low)
for (pulseCount = 0; rc == MAXT_SUCCESS && pulseCount < 10; pulseCount++)
{
rc = mxfDiscreteTxAperiodicWrite(txDIOBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, 2, recPtr);
if (rc != MAXT_SUCCESS)
{
Console.Write("mxfDiscreteTxAperiodicWrite failed ! rc=0x{0:x8}\n (pulse={1})", rc, pulseCount);
break;
}
tempPtr = recPtr;
timer += C50ms;
dioRec[0].timeTag = timer;
dioRec[0].control = 0;
Marshal.StructureToPtr(dioRec[0], tempPtr, true);
mxfDiscreteNextDataRecordPtrGet(tempPtr, out tempPtr);
timer += C10ms;
dioRec[1].timeTag = timer;
dioRec[1].control = 0;
Marshal.StructureToPtr(dioRec[1], tempPtr, true);
}
// Wait TX completion
mxfSleep(1000);
Marshal.FreeHGlobal(recPtr);
return rc;
}
/***************************************************************************************************************/
// RX429ReadAcquisitionData
/***************************************************************************************************************/
private static UInt32 RX429ReadAcquisitionData(UInt64 rxBuffer, IntPtr rec429)
{
IntPtr tempPtr = rec429;
UInt64 status, msgsCount, bytesCount;
UInt64 label, sdi, data, ssm, parity;
UInt64 j;
UInt32 rc;
// Read and display records
rc = mxfA429RxAcqRead(rxBuffer, 0, BUFFER_SIZE, out status, out msgsCount, out bytesCount, rec429);
for (j = 0; j < msgsCount && rc == MAXT_SUCCESS; j++)
{
rec = (MXF_A429_DATAREC)Marshal.PtrToStructure(tempPtr, typeof(MXF_A429_DATAREC));
mxfA429ArwDecompose(rec.data, out label, out sdi, out data, out ssm, out parity);
Console.Write("{0:00}: Timetag {1} - ARINC word=[{2},{3},{4:X5},{5},{6:000}]\n",
j, rec.timeTag, parity, ssm, data, sdi, label);
mxfA429NextDataRecordPtrGet(tempPtr, out tempPtr);
}
return rc;
}
}
}
Updated 03/30/2022