MX Foundation 4
ar717_advance.c
/*****************************************************************************
//
// File:
// ar717_advance.c
//
// Copyright (c) MAX Technologies Inc. 1988-2015, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example demonstrates the basic usage of ARINC 717 channel class
// for transmission and reception on four A717 channels. Encoding is set
// to Bipolar RZ (auxiliary output).
//
// Hardware requirements:
// - MAXT FlexMulti.
//
*****************************************************************************/
#include "example.h"
#define MAX_TX_SUBFRAMES_TO_TRANSMIT 4 //minimum 4 to have at least one full frame.
//#define LOCAL
//#define LOOPBACK
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
int main(void)
{
uint32 rc;
HMXF_SERVER server=0;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannels[4];
HMXF_CHANNEL txChannels[4];
HMXF_BUFFER rxBuffer[4];
HMXF_BUFFER txBuffer[4];
uint64 moduleCount=0;
uint64 channelCount=0;
size_t txBufferSize=0;
size_t rxBufferSize=0;
MXF_A717_DATAREC* rxHostBuffer=0;
MXF_A717_DATAREC* txHostBuffer=0;
uint64 rxAcqStatus=0;
uint64 msgCount=0;
uint64 byteCount=0;
uint64 data;
uint64 word, subframeSize=128;
uint64 iChannel, indexBuffer;
// Connects to services and initialize environment
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", TRUE, &server);
#endif
if(rc!=MAXT_SUCCESS)
{
printf("Failed to connect; rc=0x%08x", rc);
getchar();
return 0;
}
// Initializes init callback handler to set all TX and RX channels to A717
rc = mxfSystemInitAttributeUint64CallbackHandler(server, &initHandler);
// 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 multi_eh module
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MULTI_EH, 1, &moduleCount, &module);
// If module not found, returns an error
if(!rc && !moduleCount)
rc = MAXT_ERROR_NOT_FOUND;
// Gets handle of first four A717 RX channel
if(!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A717, MXF_SCLASS_RX_CHANNEL, 4, &channelCount, rxChannels);
// Gets handle of first four A717 TX channel
if (!rc && (channelCount == 4))
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A717, MXF_SCLASS_TX_CHANNEL, 4, &channelCount, txChannels);
// If channel not found, returns an error
if(!rc && (channelCount != 4))
rc = MAXT_ERROR_NOT_FOUND;
// Enables loopback
#ifdef LOOPBACK
for (iChannel = 0; iChannel < 4; iChannel++)
{
if(!rc)
rc = mxfAttributeUint64Set(rxChannels[iChannel], KMXF_A717_TX_RX_TEST_LB , VMXF_ENABLE);
}
#endif
#if (MAX_TX_SUBFRAMES_TO_TRANSMIT < 4)
if(!rc)
printf("Number of subframes to transmit less than 4: synchronization will not occur.\n");
#endif
// Allocates buffers for tx data
if(!rc)
{
txBufferSize = MAX_TX_SUBFRAMES_TO_TRANSMIT*sizeof(MXF_A717_DATAREC);
// Allocates TX Aperiodic static buffers for HIGH priority queue
for (iChannel = 0; iChannel < 4; iChannel++)
{
rc=mxfTxAperiodicBufferAlloc(txChannels[iChannel], MXF_TXAPERIODIC_PRIORITY_HIGH, txBufferSize, &txBuffer[iChannel], NULL);
}
// Host buffer allocation
if(!rc)
{
txHostBuffer = (MXF_A717_DATAREC*) calloc(1, txBufferSize);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocates buffers for rx data
if(!rc)
{
rxBufferSize = MAX_TX_SUBFRAMES_TO_TRANSMIT*sizeof(MXF_A717_DATAREC);
// Allocates RX acquisition static buffer
for (iChannel = 0; iChannel < 4; iChannel++)
{
rc=mxfRxAcqBufferAlloc(rxChannels[iChannel], rxBufferSize, &rxBuffer[iChannel], NULL);
}
// Host buffer allocation
if(!rc)
{
rxHostBuffer = (MXF_A717_DATAREC*) calloc(1, rxBufferSize);
if(!rxHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Sets timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Sets attributes for all channels
for (iChannel = 0; !rc && (iChannel < 4); iChannel++)
{
// Sets RX & TX channels subframe size to 128 words
if(!rc)
rc=mxfAttributeUint64Set(rxChannels[iChannel], KMXF_A717_SUBFRAME_SIZE, subframeSize);
if(!rc)
rc=mxfAttributeUint64Set(txChannels[iChannel], KMXF_A717_SUBFRAME_SIZE, subframeSize);
// Sets RX & TX channels bit encoding to Bipolar RZ (Auxiliary output)
if(!rc)
rc=mxfAttributeUint64Set(rxChannels[iChannel], KMXF_A717_BIT_ENCODING, VMXF_A717_BIT_ENCODING_BIPOLAR_RZ);
if(!rc)
rc=mxfAttributeUint64Set(txChannels[iChannel], KMXF_A717_BIT_ENCODING, VMXF_A717_BIT_ENCODING_BIPOLAR_RZ);
// Sets RX & TX channels electrical selection to default
if(!rc)
rc=mxfAttributeUint64Set(rxChannels[iChannel], KMXF_A717_ELECTRICAL_SELECTION, VMXF_A717_ELECTRICAL_SELECT_DEFAULT);
if(!rc)
rc=mxfAttributeUint64Set(txChannels[iChannel], KMXF_A717_ELECTRICAL_SELECTION, VMXF_A717_ELECTRICAL_SELECT_DEFAULT);
}
// Starts acquisition
for (iChannel = 0; !rc && (iChannel < 4); iChannel++)
rc = mxfRxAcqStart(rxBuffer[iChannel], MXF_RXACQ_FLAG_DEFAULT, 0, 0);
for (iChannel = 0; !rc && (iChannel < 4); iChannel++)
{
printf("Transmitting on channel %llu ...\n", iChannel);
if(!rc)
{
// Prepares string array
rec = txHostBuffer;
for(data=0; data<MAX_TX_SUBFRAMES_TO_TRANSMIT; data++)
{
rec->timeTag = 0;
rec->control = 0;
rec->dataSize = 2 * (uint32)subframeSize; // 16 bits per word in subframe
rec->repeatCount = 1;
rec->reserved = 0;
for(word=0; word < subframeSize ; word++)
{
if (word == 0)
{
//1st word of each subframe has to be a sync word
// sync words are:
// - 0x247 for subframe #0
// - 0x5B8 for subframe #1
// - 0xA47 for subframe #2
// - 0xDB8 for subframe #3
switch (data%4)
{
case 0:
rec->data[word] = 0x247;
break;
case 1:
rec->data[word] = 0x5B8;
break;
case 2:
rec->data[word] = 0xA47;
break;
case 3:
rec->data[word] = 0xDB8;
break;
default:
break;
}
}
else
rec->data[word] = (uint16)(0x11*word);
}
rc = mxfA717NextDataRecordPtrGet(rec, &rec);
}
printf("\n");
}
// Transmits strings on relative record time
if(!rc)
rc = mxfA717TxAperiodicWrite(txBuffer[iChannel], MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_SUBFRAMES_TO_TRANSMIT, txHostBuffer);
}
if(!rc)
mxfSleep((MAX_TX_SUBFRAMES_TO_TRANSMIT+1) * 1000); // 1 second per subframe + 1 second to be sure to exceed the minimum duration to wait.
for (iChannel = 0; !rc && (iChannel < 4); iChannel++)
{
printf("\nReceiving on channel %llu ...\t", iChannel);
// Reads rx buffer
rc = mxfA717RxAcqRead(rxBuffer[iChannel], 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxHostBuffer);
if(!rc)
{
printf("String received count = %llu \n", msgCount);
// Displays received strings
rec = rxHostBuffer;
for(data=0; data<msgCount && !rc; data++)
{
printf("\n%02llu: Timetag=%012llu, Size=%u words\n", data, rec->timeTag, (rec->dataSize)/2);
for(word=0; word < subframeSize ; word++)
printf("%03X ", rec->data[word]);
printf("\n");
}
}
// Stops acquisition
if(!rc)
rc = mxfRxAcqStop(rxBuffer[iChannel]);
}
// Frees device and host buffers
for (indexBuffer = 0; indexBuffer < 4; indexBuffer++)
{
if (rxBuffer[indexBuffer])
{
mxfRxAcqBufferFree(rxBuffer[indexBuffer]);
}
if (txBuffer[indexBuffer])
{
mxfTxAperiodicBufferFree(txBuffer[indexBuffer]);
}
}
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;
}
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value)
{
HMXF_DEVICE device;
MXF_DEVICE_INFO deviceInfo;
uint32 rc;
channelIndex=channelIndex;
if(attrib == KMXF_CHANNEL_CLASS)
{
rc = mxfSystemDeviceGet(server, deviceIndex, &device);
if (!rc)
rc = mxfDeviceInfoGet(device, &deviceInfo);
if(!rc && (deviceInfo.modules[moduleIndex].type == MXF_MODULE_MULTI_EH))
{
// Sets all MXF_MODULE_MULTI_EH TX and RX channels to A717
*value = MXF_CLASS_A717;
return TRUE;
}
}
return FALSE;
}
Updated 10/23/2023