MX Foundation 4
ar708_sampling.c
/*****************************************************************************
//
// File:
// ar708_sampling.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 708 channel class
// for transmission and reception with sampling.
//
// Hardware requirements:
// - MAXT Flex with A708 option.
// - Loopback between TX and RX channels if internal loopback is not used.
//
*****************************************************************************/
#include "example.h"
#define MAX_TX_RECORDS_TO_TRANSMIT 10
#define BUFFER_SIZE MAX_TX_RECORDS_TO_TRANSMIT*sizeof(MXF_A708_DATAREC)
// #define LOCAL
//#define LOOPBACK
void DisplayDataArray(uint64 recNum, MXF_A708_DATAREC* rec);
void DisplaySampDataArray(uint64 recNum, MXF_A708_SAMPREC* rec);
int main(void)
{
uint32 rc;
HMXF_SERVER server=0;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannel=0;
HMXF_CHANNEL txChannel=0;
HMXF_BUFFER rxBuffer=0;
HMXF_BUFFER txBuffer=0;
uint64 moduleCount=0;
uint64 channelCount=0;
MXF_A708_DATAREC* txHostBuffer=0;
MXF_A708_SAMPREC* hostBuffer=NULL;
HMXF_SCHED schedule=0;
uint64 i, msgsCount, bytesCount;
HMXF_SCHED_MSG msg=0;
uint64 data, word;
// 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", FALSE, &server);
#endif
// 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 && channelCount)
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
// Alloc Rx host buffer
if(!rc)
{
hostBuffer = (MXF_A708_SAMPREC *)malloc(BUFFER_SIZE);
if(!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Tx host buffer allocation
if(!rc)
{
txHostBuffer = (MXF_A708_DATAREC*)malloc(BUFFER_SIZE);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
// Sets timebase to RTC usec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Allocate RX sampling buffer
if(!rc)
rc = mxfRxSamplingBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Sets sampling kill time to 5 seconds.
if (!rc)
rc = mxfRxSamplingKilltimeSet(rxBuffer, 5000000);
// Start sampling
if(!rc)
rc = mxfRxSamplingStart(rxBuffer);
if(!rc)
printf("Sampling started\n\r");
// Allocate TX Periodic Update buffer
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, 0, BUFFER_SIZE, &txBuffer, NULL);
// Create the periodic scheduler
if(!rc)
rc = mxfTxPeriodicScheduleNew(txChannel, &schedule);
// Set scheduling values: Rate=5 ms, Phase=0 us
if(!rc)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 5000, 0, &msg);
// Define the number of buffer for the list and link to it
if(!rc)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 1, 0, &txBuffer);
// Set records for the buffers
if(!rc)
{
rec =txHostBuffer;
//Prepares a buffer with a record that will be read by the sampling
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = 0;
rec->control = 0;
rec->repeatCount = 100;
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);
}
}
// Transmits strings on relative record time
if(!rc)
rc = mxfA708TxPeriodicUpdateMsgWrite(txBuffer, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
// Run the schedule now
if(!rc)
rc = mxfTxPeriodicScheduleRun(schedule);
// Read and display records for 5 seconds
for(i=0; i<10 && !rc; i++)
{
rc = mxfA708RxSamplingRead(rxBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, 0, BUFFER_SIZE, &msgsCount, &bytesCount, hostBuffer);
if(!rc)
{
printf("Read %llu messages\n\r", msgsCount);
DisplaySampDataArray(msgsCount, hostBuffer);
}
if(rc)
printf("Sampling read failed; rc=0x%08x\n", rc);
else
mxfSleep(500);
}
// Stop transmission
if(!rc)
rc = mxfTxPeriodicScheduleFree(schedule);
// Stop sampling
if(!rc)
rc = mxfRxSamplingStop(rxBuffer);
if (!rc)
{
printf("\nAcquisition stopped\n\r");
}
// Frees device and host buffers
if (rxBuffer)
if (txBuffer)
if(txHostBuffer)
free(txHostBuffer);
if(hostBuffer)
free(hostBuffer);
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;
}
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");
}
}
void DisplaySampDataArray(uint64 recNum, MXF_A708_SAMPREC* 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