MX Foundation 4
ar429_aperiodic.c
/*******************************************************************************
//
// File:
// ar429_aperiodic.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 shows how to perform a basic aperiodic transmission
// with ARINC 429 data.
//
// Hardware Requirements:
// - MAXT Flex or 500 series carrier
// - Loopback between first TX and RX ARINC 429 Enhanced channels.
//
*******************************************************************************/
#include "example.h"
#define LOOPBACK
#define LOCAL
#define BUFFER_SIZE 4096 // 4KB
#define MAX_TX_RECORDS_TO_TRANSMIT 8
uint32 RX429ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A429_DATAREC * rec429);
uint32 TX429WaitQueueEmpty(HMXF_BUFFER txBuffer);
uint32 TX429StartAperiodicTransmissionDefault(HMXF_BUFFER txBuffer, MXF_A429_DATAREC* rec429);
uint32 TX429StartAperiodicTransmissionAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_A429_DATAREC* rec429);
uint32 TX429StartAperiodicTransmissionRecordAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_A429_DATAREC* rec429);
uint32 TX429StartAperiodicTransmissionRecordRelative(HMXF_BUFFER txBuffer, MXF_A429_DATAREC* rec429);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device = 0;
uint64 count = 0;
HMXF_CHANNEL rxChannel = 0;
HMXF_CHANNEL txChannel = 0;
HMXF_BUFFER rxBuffer = 0;
HMXF_BUFFER txBuffer = 0;
MXF_A429_DATAREC* rec429 = NULL;
uint64 dev, mod, port;
// 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);
if (rc == MAXT_ERROR_ANOTHER_PROCESS_RUNNING)
rc = mxfSystemResourcesInit(server, 0);
// Get the first device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Obtain the first ARINC 429 Protocol RX channel
if (!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, 1, &count, &rxChannel);
// Obtain the first ARINC 429 Protocol TX channel
if (!rc && count)
rc = mxfChannelAllGet(server, MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 1, &count, &txChannel);
// If channel not found, return an error
if (!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Set timebase to 64-bit microseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_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 Aperiodic TX static buffer for HIGH priority queue
if (!rc)
rc = mxfTxAperiodicBufferAlloc(txChannel, MXF_TXAPERIODIC_PRIORITY_HIGH, BUFFER_SIZE, &txBuffer, NULL);
// Allocate host buffer
if (!rc)
{
rec429 = (MXF_A429_DATAREC*)malloc(BUFFER_SIZE);
if (!rec429)
rc = MAXT_ERROR_MEM;
}
// Start the acquisition process
if (!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
if (!rc)
{
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
printf("\nAcquisition started\n\r");
}
if (!rc)
{
// Start 429 data transmission
rc = TX429StartAperiodicTransmissionDefault(txBuffer, rec429);
if (!rc)
rc = RX429ReadAcquisitionData(rxBuffer, rec429);
if (!rc)
rc = TX429StartAperiodicTransmissionAbsolute(device, txBuffer, rec429);
if (!rc)
rc = RX429ReadAcquisitionData(rxBuffer, rec429);
if (!rc)
rc = TX429StartAperiodicTransmissionRecordAbsolute(device, txBuffer, rec429);
if (!rc)
rc = RX429ReadAcquisitionData(rxBuffer, rec429);
if (!rc)
rc = TX429StartAperiodicTransmissionRecordRelative(txBuffer, rec429);
if (!rc)
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");
}
// 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 (rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer)
if (rec429)
free(rec429);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
/***************************************************************************************************************/
// RX429ReadAcquisitionData
/***************************************************************************************************************/
uint32 RX429ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A429_DATAREC* rec429)
{
MXF_A429_DATAREC* recPtr = 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++)
{
rc = mxfA429ArwDecompose(recPtr->data, &label, &sdi, &data, &ssm, &parity);
if (!rc)
{
printf("%02llu: Timetag %llu - ARINC word=[%03llo,%lld,%05llX,%lld,%s]\n",
j, recPtr->timeTag, label, sdi, data, ssm, (parity == VMXF_A429_PARITY_ODD) ? "ODD" : "EVEN");
if (!rc)
rc = mxfA429NextDataRecordPtrGet(recPtr, &recPtr);
}
}
return rc;
}
/***************************************************************************************************************/
// TX429WaitQueueEmpty
/***************************************************************************************************************/
uint32 TX429WaitQueueEmpty(HMXF_BUFFER txBuffer)
{
uint64 msgCount, usedBytes, freeBytes;
uint32 rc;
// Wait till all records are sent on the interface
do
{
rc = mxfTxAperiodicBufferStatusGet(txBuffer, &msgCount, &usedBytes, &freeBytes);
if (rc)
return rc;
} while (msgCount != 0);
// Make sure they are received
mxfSleep(100);
return MAXT_SUCCESS;
}
/***************************************************************************************************************/
// TX429StartAperiodicTransmissionDefault
/***************************************************************************************************************/
uint32 TX429StartAperiodicTransmissionDefault(HMXF_BUFFER txBuffer, MXF_A429_DATAREC* rec429)
{
uint64 label, sdi, data, ssm, parity;
uint32 record;
uint64 delay100ms = 100000;
uint32 rc;
// In the example below the record are sent back-to-back 100 ms in the future.
printf("\nAperiodic transmission (Relative Start Time-Default)\n");
// Set the record
rec = rec429;
for (record = 0; record < MAX_TX_RECORDS_TO_TRANSMIT; record++)
{
// Set each record: ARINC 429 LABEL=005, SDI=1
rec->timeTag = 0;
rec->control = 0;
rec->repeatCount = 1;
rec->reserved = 0;
label = 005;
sdi = 1;
data = record * 8;
ssm = 0;
parity = VMXF_A429_PARITY_ODD;
mxfA429ArwCompose(label, sdi, data, ssm, parity, &rec->data);
}
// Transmit the array of records
rc = mxfA429TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, delay100ms, MAX_TX_RECORDS_TO_TRANSMIT, rec429);
// Wait TX completion
if (!rc)
rc = TX429WaitQueueEmpty(txBuffer);
return rc;
}
/***************************************************************************************************************/
// TX429StartAperiodicTransmissionAbsolute
/***************************************************************************************************************/
uint32 TX429StartAperiodicTransmissionAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_A429_DATAREC* rec429)
{
uint64 label, sdi, data, ssm, parity;
uint32 record, rc;
uint32 delay100ms = 100000;
uint64 currentTime;
// Get the current time
rc = mxfDeviceTimerGet(device, &currentTime);
if (rc)
return rc;
// In the example below the option MXF_TXAPERIODIC_FLAG_ABSOLUTE_START_TIME
// is used. In this case the records are sent back-to-back
// with a start transmission time based on the device clock + 100ms in the future.
printf("\nAperiodic transmission (Absolute)\n");
rec = rec429;
for (record = 0; record < MAX_TX_RECORDS_TO_TRANSMIT; record++)
{
// Set each record: ARINC 429 LABEL=006, SDI=2
rec->timeTag = 0;
rec->control = 0;
rec->repeatCount = 1;
rec->reserved = 0;
label = 6;
sdi = 2;
data = record * 16;
ssm = 0;
parity = VMXF_A429_PARITY_ODD;
mxfA429ArwCompose(label, sdi, data, ssm, parity, &rec->data);
}
// Transmit the array of records
rc = mxfA429TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_ABSOLUTE_START_TIME, currentTime + delay100ms, MAX_TX_RECORDS_TO_TRANSMIT, rec429);
// Wait TX completion
if (!rc)
rc = TX429WaitQueueEmpty(txBuffer);
return rc;
}
/***************************************************************************************************************/
// TX429StartAperiodicTransmissionRecordAbsolute
/***************************************************************************************************************/
uint32 TX429StartAperiodicTransmissionRecordAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_A429_DATAREC* rec429)
{
uint64 label, sdi, data, ssm, parity;
uint32 record;
uint64 currentTime;
uint32 rc;
// In the example below the timetag in the record is set
// and the option MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME
// In this case the records are sent based on the absolute clock
// timing specified in the timetag field for each records.
printf("\nAperiodic transmission (Absolute with timetag)\n");
// Get the current time
rc = mxfDeviceTimerGet(device, &currentTime);
if (rc)
return rc;
currentTime += 100000;
// Set the record
rec = rec429;
for (record = 0; record < MAX_TX_RECORDS_TO_TRANSMIT; record++)
{
// Set each records: ARINC 429 LABEL=007, SDI=1
// The scheduling timetag is as follow:
// clock current time + 100 ms
// Each record is then sent with a 10 ms interval.
rec->timeTag = currentTime + ((record + 1) * 10000LL);
rec->control = 0;
rec->repeatCount = 1;
rec->reserved = 0;
label = 7;
sdi = 1;
data = record * 32;
ssm = 0;
parity = VMXF_A429_PARITY_ODD;
mxfA429ArwCompose(label, sdi, data, ssm, parity, &rec->data);
}
// Transmit the array of records
rc = mxfA429TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, rec429);
// Wait TX completion
if (!rc)
rc = TX429WaitQueueEmpty(txBuffer);
return rc;
}
/***************************************************************************************************************/
// TX429StartAperiodicTransmissionRecordRelative
/***************************************************************************************************************/
uint32 TX429StartAperiodicTransmissionRecordRelative(HMXF_BUFFER txBuffer, MXF_A429_DATAREC* rec429)
{
uint64 label, sdi, data, ssm, parity;
uint32 record;
uint64 currentTime;
uint32 rc;
// In the example below the timetag in the record is set
// and the option MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME.
// The relative time between each records is set in the
// timetag field of the record array.
printf("\nAperiodic transmission (Record Relative)\n");
// Set the record
rec = rec429;
for (record = 0, currentTime = 100000; record < MAX_TX_RECORDS_TO_TRANSMIT; record++)
{
// Set each records: ARINC 429 LABEL=010, SDI=1
// The scheduling timetag is as follow:
// 100 ms offset before first record.
// Each record is then sent with a 10 ms interval.
rec->timeTag = currentTime + ((record + 1) * 10000LL);
rec->control = 0;
rec->repeatCount = 1;
rec->reserved = 0;
label = 010;
sdi = 1;
data = record * 64;
parity = 0;
ssm = 0;
mxfA429ArwCompose(label, sdi, data, ssm, parity, &rec->data);
}
// Transmit the array of records
rc = mxfA429TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, rec429);
// Wait TX completion
if (!rc)
rc = TX429WaitQueueEmpty(txBuffer);
return rc;
}
Updated 10/23/2023