MX Foundation 4
ar429_rx_acquisition_trigger.c
/*******************************************************************************
//
// File:
// ar429_rx_acquisition_trigger.c
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This demo illustrates how to use the trigger functionality
// to start acquisition on a receive channel.
//
// The program first starts a basic periodic transmission using the periodic
// scheduling service. The acquisition process is then started and waits for
// bit 10 value of label 07 to be 1.
//
// Hardware Requirements:
// - MAXT Flex or PCI-500 carrier
// - Loopback between first TX and RX ARINC 429 channels.
//
*******************************************************************************/
#include "example.h"
#include <time.h>
#define LOOPBACK
#define LOCAL
#define BUFFER_SIZE 1*1024*1024 // 1 MiB
#define LABEL 7
#define SDI 0
uint32 RX429ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A429_DATAREC * rec429);
uint32 TX429PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER txBuffer, MXF_A429_DATAREC* rec429);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device = 0;
HMXF_CHANNEL rxChannel = 0, txChannel = 0;
HMXF_BUFFER rxBuffer = 0;
HMXF_BUFFER txBuffer = 0;
MXF_A429_DATAREC* rec429 = NULL;
uint64 dev, mod, port;
HMXF_COND_LIST condList = 0;
// Connect to services local or remote
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
if (rc)
{
printf("Failed to connect; rc=0x%08x", rc);
printf("\nPress a key to terminate\n");
getchar();
return 0;
}
// Initialize the server
printf("\nStarting\n");
rc = mxfSystemInit(server);
// Get the first device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
//Obtain the first A429 Rx and Tx channels
if (!rc)
rc = mxfChannelGet(server, MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, 0, &rxChannel);
if (!rc)
rc = mxfChannelGet(server, MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 0, &txChannel);
// Set timebase to computer time 64-bit microseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_USEC);
// Get the physical port location
if (!rc)
{
rc = mxfChannelLocationGet(rxChannel, &dev, &mod, &port);
if (!rc)
printf("Acquisition Channel (RX) location=%llu.%llu.%llu\n", dev, mod, port);
}
if (!rc)
{
rc = mxfChannelLocationGet(txChannel, &dev, &mod, &port);
if (!rc)
printf("Transmitter Channel (TX) location=%llu.%llu.%llu\n", dev, mod, port);
}
//Activate loopback before transmission and reception
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate RX acquisition buffer
if (!rc)
rc = mxfRxAcqBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Allocate TX Periodic Update buffer (1MiB)
if (!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, LABEL, BUFFER_SIZE, &txBuffer, NULL);
// Allocate host buffer
if (!rc)
{
rec429 = (MXF_A429_DATAREC*)malloc(BUFFER_SIZE);
if (!rec429)
rc = MAXT_ERROR_MEM;
}
// Configure trigger
// Create the condition list
if (!rc)
rc = mxfRxAcqTrigConditionListAlloc(server, &condList);
// The condition will be triggered on bit 10 value of LABEL/SDI
if (!rc)
{
condParam.mask = 0x000007ff;
condParam.data = (1 << 10) | (SDI << 8) | LABEL;
condParam.offset = 0;
condParam.options = MXF_RXACQ_TRIG_COND_RDATA_OPTIONS_EQUAL;
rc = mxfRxAcqTrigConditionAdd(condList, MXF_RXACQ_TRIG_COND_ID_RDATA_DW, &condParam);
}
// Set the trigger with a pretrig count of 3 which allows some records to be read before the condition was triggered.
// For the A429 protocol this number is rounded to the closest internal acquisition buffer size.
if (!rc)
rc = mxfRxAcqTrigSet(rxBuffer, condList, 3);
// Start the acquisition process
if (!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_CIRCULAR);
if (!rc)
{
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
printf("\nAcquisition started\n\r");
}
// Start 429 data transmission
if (!rc)
{
rc = TX429PeriodicScheduling(txChannel, txBuffer, rec429);
if (!rc)
{
// Read 429 data in acquisition buffer
rc = RX429ReadAcquisitionData(rxBuffer, rec429);
}
}
// Stop and flush unread data
if (!rc)
rc = mxfRxAcqStop(rxBuffer);
if (!rc)
{
rc = mxfRxAcqClear(rxBuffer);
if (!rc)
printf("\nAcquisition stopped\n\r");
}
// Free trigger condition list
if (!rc)
// Catch any previous error
if (rc)
{
char buffer[256];
rc = mxfSystemErrorStringGet(server, rc, sizeof(buffer), buffer);
printf("%s\n", buffer);
}
printf("\nTerminating\n");
// Free all buffers and terminate
if (txBuffer)
if (rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
if (rec429)
free(rec429);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
/***************************************************************************************************************/
// TX429PeriodicScheduling
/***************************************************************************************************************/
uint32 TX429PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER txBuffer, MXF_A429_DATAREC* rec429)
{
HMXF_SCHED schedule;
HMXF_SCHED_MSG msg = 0;
uint64 data = 0, ssm = 1, parity = VMXF_A429_PARITY_ODD;
uint32 rc;
// Set the A429 records and create the schedule
// Create the periodic scheduler
rc = mxfTxPeriodicScheduleNew(txChannel, &schedule);
// Set scheduling values: Rate=10 ms (.1sec), Phase=0 us
if (!rc)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 10000LL, 0, &msg);
// Define the number of buffer for the list and link to it
if (!rc)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 1, 0, &txBuffer);
// Set record for the buffer: ARINC 429 LABEL=05, SDI=0
if (!rc)
{
rec429->timeTag = 0;
rec429->control = 0;
rec429->repeatCount = 1;
rec429->reserved = 0;
rc = mxfA429ArwCompose(LABEL, SDI, data, ssm, parity, &rec429->data);
if (!rc)
rc = mxfTxPeriodicUpdateMsgWrite(txBuffer, 1, rec429);
}
// Run the scheduler, update records
if (!rc)
{
printf("Running periodic transmission, please wait...\n\r");
// Run the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Wait 0.5 second
if (!rc)
{
mxfSleep(500);
// fire trigger
rec429->control = 0;
data = 1;
rc = mxfA429ArwCompose(LABEL, SDI, data, ssm, parity, &rec429->data);
if (!rc)
rc = mxfTxPeriodicUpdateMsgWrite(txBuffer, 1, rec429);
}
// Wait 0.5 second
if (!rc)
{
mxfSleep(500);
rc = mxfTxPeriodicScheduleFree(schedule);
}
if (!rc)
printf("\n\rTransmission stopped\n\r");
return rc;
}
/***************************************************************************************************************/
// 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, trigTime = 0;
uint32 rc;
time_t timeTemp;
struct tm* ptm;
char szTime[1024];
uint64 msec, usec;
// Check trigger happened
rc = mxfRxAcqBufferStatusGet(rxBuffer, &status, NULL, NULL, NULL);
if (!rc)
{
if (status & MXF_RXACQ_STATUS_TRIG_OCCURRED)
{
// Display the acquisition trigger time
rc = mxfRxAcqTrigTimeGet(rxBuffer, &trigTime);
if (!rc)
{
timeTemp = trigTime / 1000000;
ptm = localtime(&timeTemp);
strftime(szTime, sizeof(szTime), "%Y-%m-%d %H:%M:%S", ptm);
usec = trigTime % 1000;
timeTemp = trigTime / 1000;
msec = timeTemp % 1000;
printf("Event triggered at %s:%03llu:%03llu\n", szTime, msec, usec);
}
}
else
printf("Trigger not fired\n");
}
// Read and display records
rc = mxfRxAcqRead(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] %s\n",
j, rec->timeTag, parity, ssm, data, sdi, label, trigTime == rec->timeTag ? "*" : "");
mxfNextRecordPtrGet(MXF_CLASS_A429, MXF_RECTYPE_DATAREC, rec, (void**)& rec);
}
return rc;
}
Updated 10/23/2023