MX Foundation 4
ar708_aperiodic.c
/*******************************************************************************
//
// File:
// ar708_aperiodic.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 to use the different types of aperiodic transmissions
// with ARINC 708 channels.
//
// Hardware Requirements:
// - MAXT Flex with A708 option.
// - Loopback between TX and RX channels if internal loopback is not used.
//
*******************************************************************************/
#include "example.h"
//#define LOOPBACK
//#define LOCAL
#define BUFFER_SIZE 4096 // 4KB
#define MAX_TX_RECORDS_TO_TRANSMIT 4
void DisplayDataArray(uint64 recNum, MXF_A708_DATAREC* rec);
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A708_DATAREC* rxHostBuffer);
uint32 WaitQueueEmpty(HMXF_BUFFER txBuffer);
uint32 StartAperiodicTransmissionDefault(HMXF_BUFFER txBuffer, MXF_A708_DATAREC* txHostBuffer);
uint32 StartAperiodicTransmissionAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_A708_DATAREC* txHostBuffer);
uint32 StartAperiodicTransmissionRecordAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_A708_DATAREC* txHostBuffer);
uint32 StartAperiodicTransmissionRecordRelative(HMXF_BUFFER txBuffer, MXF_A708_DATAREC* txHostBuffer);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
uint64 moduleCount=0, channelCount=0;
HMXF_CHANNEL rxChannel=0;
HMXF_CHANNEL txChannel=0;
HMXF_BUFFER rxBuffer=0;
HMXF_BUFFER txBuffer=0;
size_t txBufferSize=0;
size_t rxBufferSize=0;
MXF_A708_DATAREC* rxHostBuffer=0;
MXF_A708_DATAREC* txHostBuffer=0;
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;
}
// Initializes MX Foundation library
if (!rc)
{
printf("Starting ...\n");
rc = mxfSystemInit(server);
}
// Gets the device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Gets handle of first ARINC 708 module
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A708_EH, 1, &moduleCount, &module);
// If module not found, return an error
if(!rc && !moduleCount)
rc = MAXT_ERROR_NOT_FOUND;
// Activates the ARINC 708 module (because the MIL-STD-1553 is by default activated, except on FM1553-2)
if (!rc)
{
rc = mxfAttributeUint64Set(module, KMXF_A708_MODULE_ACTIVE, TRUE);
if (rc == MAXT_ERROR_NOT_SUPPORTED) // Attribute not supported on FM1553-2, simply ignore
rc = MAXT_SUCCESS;
}
// Gets handle of first ARINC 708 RX channel
if(!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A708, MXF_SCLASS_RX_CHANNEL, 1, &channelCount, &rxChannel);
// Gets handle of first ARINC 708 TX channel
if (!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A708, MXF_SCLASS_TX_CHANNEL, 1, &channelCount, &txChannel);
// Enables loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_A708_TX_RX_TEST_LB , VMXF_ENABLE);
#endif
// Allocates 4 KB buffer for tx data
if(!rc)
{
txBufferSize = 4*1024;
// Allocates TX Aperiodic static buffer for HIGH priority queue
rc = mxfTxAperiodicBufferAlloc(txChannel, MXF_TXAPERIODIC_PRIORITY_HIGH, txBufferSize, &txBuffer, NULL);
// Host buffer allocation
if(!rc)
{
txHostBuffer = (MXF_A708_DATAREC*)calloc(1, txBufferSize);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocates 4 KB buffer for RX data
if(!rc)
{
rxBufferSize = 4*1024;
// Allocates RX acquisition static buffer
rc = mxfRxAcqBufferAlloc(rxChannel, rxBufferSize, &rxBuffer, NULL);
// Host buffer allocation
if(!rc)
{
rxHostBuffer = (MXF_A708_DATAREC*)calloc(1, rxBufferSize);
if(!rxHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Sets timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// 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);
}
// 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)
{
// Start ARINC 708 data transmission
rc = StartAperiodicTransmissionDefault(txBuffer, txHostBuffer);
if (!rc)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (!rc)
rc = StartAperiodicTransmissionAbsolute(device, txBuffer, txHostBuffer);
if (!rc)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (!rc)
rc = StartAperiodicTransmissionRecordAbsolute(device, txBuffer, txHostBuffer);
if (!rc)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (!rc)
rc = StartAperiodicTransmissionRecordRelative(txBuffer, rxHostBuffer);
if (!rc)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
}
// Stops acquisition
if(!rc)
rc = mxfRxAcqStop(rxBuffer);
// Frees device and host buffers
if (rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer)
if(txHostBuffer)
free(txHostBuffer);
if(rxHostBuffer)
free(rxHostBuffer);
if(rc)
{
char errorString[200];
if(mxfSystemErrorStringGet(server, rc, sizeof(errorString), errorString))
sprintf (errorString,"ERROR # 0x%X", rc);
printf("%s\n\r", errorString);
}
printf("Terminating ...\n");
// Unloads MX Foundation library
// Disconnects from MX Foundation library
printf("\nPress a key to terminate\n");
getchar();
return rc;
}
/***************************************************************************************************************/
// ReadAcquisitionData
/***************************************************************************************************************/
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A708_DATAREC* rxHostBuffer)
{
uint64 status, msgsCount, bytesCount;
uint32 rc=0;
// Read and display records
printf("Receiving ...\n");
rc = mxfA708RxAcqRead(rxBuffer, 0, BUFFER_SIZE, &status, &msgsCount, &bytesCount, rxHostBuffer);
if(!rc)
printf("String received count = %llu \n", msgsCount);
// Displays received strings
if(!rc)
DisplayDataArray(msgsCount, rxHostBuffer);
if(rc)
printf("Acquisition read failed; rc=0x%08x\n\r", rc);
return rc;
}
/***************************************************************************************************************/
// WaitQueueEmpty
/***************************************************************************************************************/
uint32 WaitQueueEmpty(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;
}
/***************************************************************************************************************/
// StartAperiodicTransmissionDefault
/***************************************************************************************************************/
uint32 StartAperiodicTransmissionDefault(HMXF_BUFFER txBuffer, MXF_A708_DATAREC* txHostBuffer)
{
MXF_A708_DATAREC* rec=txHostBuffer;
uint64 data, word;
uint32 rc=0;
uint32 delay100ms = 100000000;
// In the example below the record are sent back-to-back 100 ms in the future.
printf("\nAperiodic transmission (Relative Start Time-Default)\n");
//Prepares records to send for basic transmission/reception test
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = 0;
rec->control = 0;
rec->repeatCount = 1;
rec->manchesterBitErr = 0;
rec->dataSize = 200; //200 bytes corresponds to 1600 bits (see ARINC 708 specification)
for(word=0; word < rec->dataSize/2; word++)
{
switch(word)
{
case 0:
rec->data[word] = 055; // Label
break;
case 1:
case 2:
case 3:
rec->data[word] = 0x0000;
break;
default:
rec->data[word] = (uint16)(0x0101*data);
}
}
rc = mxfA708NextDataRecordPtrGet(rec, &rec);
}
//if (!rc)
// DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
if(!rc)
{
printf("Transmitting ...\n");
// Transmits strings on relative start time
rc = mxfA708TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, delay100ms, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
// Wait TX completion
if (!rc)
rc = WaitQueueEmpty(txBuffer);
return rc;
}
/***************************************************************************************************************/
// StartAperiodicTransmissionAbsolute
/***************************************************************************************************************/
uint32 StartAperiodicTransmissionAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_A708_DATAREC* txHostBuffer)
{
MXF_A708_DATAREC* rec=txHostBuffer;
uint32 rc;
uint32 delay100ms = 100000000;
uint64 currentTime;
uint64 data, word;
// 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 freely (no time clock control)
// with a start transmission time based on the device clock + 100ms in the future.
printf("\nAperiodic transmission (Absolute)\n");
if(!rc)
{
//Prepares records to send for basic transmission/reception test
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = 0;
rec->control = 0;
rec->repeatCount = 1;
rec->manchesterBitErr = 0;
rec->dataSize = 200; //200 bytes corresponds to 1600 bits (see ARINC 708 specification)
for(word=0; word < rec->dataSize/2; word++)
{
switch(word)
{
case 0:
rec->data[word] = 055; // Label
break;
case 1:
case 2:
case 3:
rec->data[word] = 0x0000;
break;
default:
rec->data[word] = (uint16)(0x0101*data);
}
}
rc = mxfA708NextDataRecordPtrGet(rec, &rec);
}
}
//if (!rc)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
// Transmit the array of records
if(!rc)
{
printf("Transmitting ...\n");
rc = mxfA708TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_ABSOLUTE_START_TIME, currentTime+delay100ms, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
// Wait TX completion
if (!rc)
rc = WaitQueueEmpty(txBuffer);
return rc;
}
/***************************************************************************************************************/
// StartAperiodicTransmissionRecordAbsolute
/***************************************************************************************************************/
uint32 StartAperiodicTransmissionRecordAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_A708_DATAREC* txHostBuffer)
{
MXF_A708_DATAREC* rec=txHostBuffer;
uint64 currentTime;
uint32 rc;
uint64 data, word;
// 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+=100000000;
// Set the record
// The scheduling timetag is as follow:
// clock current time + 100 ms
// Each record are sent with a timetag of 5 ms interval.
if(!rc)
{
//Prepares records to send for basic transmission/reception test
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = currentTime+(data*5000000);
rec->control = 0;
rec->repeatCount = 1;
rec->manchesterBitErr = 0;
rec->dataSize = 200; //200 bytes corresponds to 1600 bits (see ARINC 708 specification)
for(word=0; word < rec->dataSize/2; word++)
{
switch(word)
{
case 0:
rec->data[word] = 055; // Label
break;
case 1:
case 2:
case 3:
rec->data[word] = 0x0000;
break;
default:
rec->data[word] = (uint16)(0x0101*data);
}
}
rc = mxfA708NextDataRecordPtrGet(rec, &rec);
}
}
//if (!rc)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
// Transmit the array of records
if(!rc)
{
printf("Transmitting ...\n");
rc = mxfA708TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
// Wait TX completion
if (!rc)
rc = WaitQueueEmpty(txBuffer);
return rc;
}
/***************************************************************************************************************/
// StartAperiodicTransmissionRecordRelative
/***************************************************************************************************************/
uint32 StartAperiodicTransmissionRecordRelative(HMXF_BUFFER txBuffer, MXF_A708_DATAREC* txHostBuffer)
{
MXF_A708_DATAREC* rec=txHostBuffer;
uint64 currentTime;
uint32 rc=0;
uint64 data, word;
// 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
// The scheduling timetag is as follow:
// clock current time + 100 ms
// Each record are sent with a timetag a 5000000 nsec (5 ms) interval.
if(!rc)
{
//Prepares records to send for basic transmission/reception test
for(data=0, currentTime=100000000; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = currentTime+(data*5000000);
rec->control = 0;
rec->repeatCount = 1;
rec->manchesterBitErr = 0;
rec->dataSize = 200; //200 bytes corresponds to 1600 bits (see ARINC 708 specification)
for(word=0; word < rec->dataSize/2; word++)
{
switch(word)
{
case 0:
rec->data[word] = 055; // Label
break;
case 1:
case 2:
case 3:
rec->data[word] = 0x0000;
break;
default:
rec->data[word] = (uint16)(0x0101*data);
}
}
rc = mxfA708NextDataRecordPtrGet(rec, &rec);
}
}
//if (!rc)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
// Transmit the array of records
if(!rc)
{
printf("Transmitting ...\n");
rc = mxfA708TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
// Wait TX completion
if (!rc)
rc = WaitQueueEmpty(txBuffer);
return rc;
}
void DisplayDataArray(uint64 recNum, MXF_A708_DATAREC* rec)
{
uint64 iRec,
iData;
printf("\n");
for(iRec=0; iRec < recNum; iRec++)
{
printf("%03llu %010llu 0x%08x %02u ", iRec, p->timeTag, p->control, p->dataSize);
for(iData=0; iData < p->dataSize/2; iData++)
{
printf("%04x ", p->data[iData]);
if(!((iData+1)%8) && (iData+1 < p->dataSize/2))
printf("\n ");
}
printf("\n");
}
}
Updated 10/23/2023