MX Foundation 4
ar429_embedded_timer.cs
/*******************************************************************************
//
// File:
// ar429_embedded_timer.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 onboard application can use
// the timer facility to generate A429 traffic.
//
// The embedded program "ar429_embedded_side_timer.c" is required by this program
// and compiled executable (ar429_embedded_side_timer-flex.trap or
// ar429_embedded_side_timer-500s.trap) must be located in
// the same folder as the host application executable.
//
// Hardware Requirements:
// - MAXT Flex with loopback between first TX and RX ARINC 429 Enhanced channels.
//
*******************************************************************************/
//#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar429_example
{
class ar429_embedded_timer
{
// User commands
const int BUFFER_SIZE = 4096; // 4KB
const int USER_COMMAND_ID_START_TIMER = 0;
const int USER_COMMAND_ID_STOP_TIMER = 1;
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
var module = new UInt64[1];
UInt64 count = 0;
var hTx429 = new UInt64[1];
var hRx429 = new UInt64[1];
UInt64 tx429Buffer = 0;
UInt64 rx429Buffer = 0;
UInt64 dev, mod = 0, port = 0;
var parameters = new UInt32[3];
UInt64 stopTime;
IntPtr rec429 = IntPtr.Zero;
UInt64 deviceType = 0;
UInt64 moduleType = 0;
// Connect to MX Foundation library
#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 server
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 ARINC 429
if (rc == MAXT_SUCCESS)
{
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A429_EH, 1, out count, module);
if ((rc == MAXT_SUCCESS) && (count == 0))
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_FLEX429, 1, &count, &module);
}
// Get the first ARINC 429 TX channel
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, 1, out count, hTx429);
// Get the first ARINC 429 RX channel
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, 1, out count, hRx429);
// If A429 channel not found, return an error
if (rc == MAXT_SUCCESS && count == 0)
rc = MAXT_ERROR_NOT_FOUND;
//Activate loopback before transmission and reception
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(hRx429[0], KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Get(device, KMXF_DEVICE_TYPE, out deviceType);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Get(module, KMXF_MODULE_TYPE, out moduleType);
// Allocate RX acquisition buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(hRx429[0], BUFFER_SIZE, out rx429Buffer, IntPtr.Zero);
// Allocate Aperiodic TX static buffer for HIGH priority queue
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicBufferAlloc(hTx429[0], MXF_TXAPERIODIC_PRIORITY_HIGH, BUFFER_SIZE, out tx429Buffer, IntPtr.Zero);
// Allocate host buffer
if (rc == MAXT_SUCCESS)
{
try
{
rec429 = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqStart(rx429Buffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
Console.Write("Acquisition started\n\r");
}
// Download from the host (Client Side) the embedded application
if (rc == MAXT_SUCCESS)
{
if((moduleType == MXF_MODULE_FLEX429) || (deviceType == MXF_DEVICE_FMOB429_PLUS))
rc = mxfEmbeddedCodeDownload(device, "ar429_embedded_side_timer-500s.trap");
else
rc = mxfEmbeddedCodeDownload(device, "ar429_embedded_side_timer-flex.trap");
}
if (rc == MAXT_SUCCESS)
rc = mxfChannelLocationGet(hTx429[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_TIMER, (UInt64)parameters.Length, parameters);
}
// Wait 1000 ms (1 second)
mxfSleep(1000);
// Stop the embedded code
if (rc == MAXT_SUCCESS)
rc = mxfEmbeddedCommandSend(device, USER_COMMAND_ID_STOP_TIMER, 0, IntPtr.Zero);
// Stop acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rx429Buffer);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqStopTimeGet(rx429Buffer, out stopTime);
if (rc == MAXT_SUCCESS)
Console.Write("Acquisition stopped at {0}\n\r", stopTime);
}
// Read data sent by the embedded program
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");
}
//Frees all buffers
if (rx429Buffer > 0)
mxfRxAcqBufferFree(rx429Buffer);
if (tx429Buffer > 0)
Console.Write("Stopping\n");
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 MAXT_SUCCESS;
}
/***************************************************************************************************************/
// RX429ReadAcquisitionData
/***************************************************************************************************************/
private static UInt32 RX429ReadAcquisitionData(UInt64 rxBuffer, IntPtr recPtr429)
{
IntPtr recPtr = recPtr429;
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, recPtr429);
recPtr = recPtr429;
for (j = 0; j < msgsCount && rc == MAXT_SUCCESS; j++)
{
rec = (MXF_A429_DATAREC)Marshal.PtrToStructure(recPtr, 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(recPtr, out recPtr);
}
return rc;
}
}
}
Updated 03/30/2022