MX Foundation 4
ar429_embedded_timer.c
/*******************************************************************************
//
// File:
// ar429_embedded_timer.c
//
// Copyright (c) MAX Technologies Inc. 1988-2015, 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.
//
*******************************************************************************/
#include "example.h"
#define LOOPBACK
#define LOCAL
#define BUFFER_SIZE 4096 // 4KB
// User Commands
#define USER_COMMAND_ID_START_TIMER 0
#define USER_COMMAND_ID_STOP_TIMER 1
uint32 EmbeddedMemoryAllocationHandler(HMXF_SERVER server, uint64 cardIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
uint32 RX429ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A429_DATAREC *rec429);
/******************************************************************************/
// main
/******************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
uint64 count=0;
HMXF_CHANNEL hTx429=0;
HMXF_CHANNEL hRx429=0;
HMXF_BUFFER tx429Buffer=0;
HMXF_BUFFER rx429Buffer=0;
uint64 dev, mod=0, port=0;
uint32 params[3];
uint64 stopTime;
MXF_A429_DATAREC *rec429=NULL;
uint64 deviceType=0, moduleType=0;
// Connect to services and initialize environment
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
if(rc!=MAXT_SUCCESS)
{
printf("Failed to connect; rc=0x%08x", rc);
printf("\nPress a key to terminate\n");
getchar();
return 0;
}
// Initialize the library
printf("\nStarting\n");
rc = mxfSystemInitAttributeUint64CallbackHandler(server, EmbeddedMemoryAllocationHandler);
if(!rc)
rc = mxfSystemInit(server);
// Get the first device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Get first module ARINC 429
if(!rc)
{
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A429_EH, 1, &count, &module);
if (!rc && !count)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_FLEX429, 1, &count, &module);
}
// Get the first ARINC 429 TX channel
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, 1, &count, &hTx429);
// Get the first ARINC 429 RX channel
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, 1, &count, &hRx429);
// If A429 channel not found, return an error
if(!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
//Activate loopback before transmission and reception
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(hRx429, KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
if(!rc)
rc = mxfAttributeUint64Get(device, KMXF_DEVICE_TYPE, &deviceType);
if(!rc)
rc = mxfAttributeUint64Get(module, KMXF_MODULE_TYPE, &moduleType);
// Allocate RX acquisition buffer
if (!rc)
rc = mxfRxAcqBufferAlloc(hRx429, BUFFER_SIZE, &rx429Buffer, NULL);
// Allocate Aperiodic TX static buffer for HIGH priority queue
if (!rc)
rc = mxfTxAperiodicBufferAlloc(hTx429, MXF_TXAPERIODIC_PRIORITY_HIGH, BUFFER_SIZE, &tx429Buffer, NULL);
// Allocate host buffer
if (!rc)
{
rec429 = (MXF_A429_DATAREC*)malloc(BUFFER_SIZE);
if (!rec429)
rc = MAXT_ERROR_MEM;
}
if (!rc)
{
rc = mxfRxAcqStart(rx429Buffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
printf("Acquisition started\n\r");
}
// Download from the host (Client Side) the embedded application
if (!rc)
{
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)
rc = mxfChannelLocationGet(hTx429, &dev, &mod, &port);
// Set the parameters to pass
if (!rc)
{
params[0] = (uint32)mod;
params[1] = (uint32)port;
params[2] = MXF_TXAPERIODIC_PRIORITY_HIGH;
// Start the embedded code
rc = mxfEmbeddedCommandSend(device, USER_COMMAND_ID_START_TIMER, sizeof(params), params);
}
// Wait 1000 ms (1 second)
mxfSleep(1000);
// Stop the embedded code
if(!rc)
rc = mxfEmbeddedCommandSend(device, USER_COMMAND_ID_STOP_TIMER, 0, NULL);
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(rx429Buffer);
if (!rc)
{
rc = mxfRxAcqStopTimeGet(rx429Buffer, &stopTime);
if (!rc)
printf("Acquisition stopped at %llu\n\r", stopTime);
}
// Read data sent by the embedded program
if(!rc)
rc = RX429ReadAcquisitionData(rx429Buffer, rec429);
// Catch any previous failing function
if(rc)
{
char buffer[256];
if(mxfSystemErrorStringGet(server, rc, sizeof(buffer), buffer))
sprintf (buffer,"ERROR # 0x%08X", rc);
printf("%s\n\r", buffer);
}
//Frees all buffers
if (rx429Buffer)
mxfRxAcqBufferFree(rx429Buffer);
if(tx429Buffer)
printf("Stopping\n");
if (rec429)
free(rec429);
// Terminate
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
/******************************************************************************/
// EmbeddedMemoryHandler
/******************************************************************************/
uint32 EmbeddedMemoryAllocationHandler(HMXF_SERVER server, uint64 cardIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value)
{
// Allocate the maximum space available for the embedded application
uint64 embedded_memory_size=65536; // 64K
(void)server;
(void)cardIndex;
(void)moduleIndex;
(void)channelIndex;
if (attrib==KMXF_DEVICE_EMBEDDED_CODEANDDATA_SIZE)
{
printf("Reserving %lluK of memory for embedded application\n", embedded_memory_size/1024);
*value = embedded_memory_size;
return TRUE;
}
return FALSE;
}
/***************************************************************************************************************/
// RX429ReadAcquisitionData
/***************************************************************************************************************/
uint32 RX429ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A429_DATAREC *rec429)
{
MXF_A429_DATAREC* rec=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, &status, &msgsCount, &bytesCount, rec429);
for (j=0; j<msgsCount && !rc; j++)
{
mxfA429ArwDecompose(rec->data, &label, &sdi, &data, &ssm, &parity);
printf("%02llu: Timetag %llu - ARINC word=[%lld,%lld,%05llX,%lld,%03llo]\n",
j, rec->timeTag, parity, ssm, data, sdi, label);
}
return rc;
}
Updated 10/23/2023