MX Foundation 4
ar629_alternate.c
/*****************************************************************************
//
// File:
// ar629_alternate.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 use of alternate and synchronized schedules.
//
// Hardware requirements:
// - MAXT FlexMulti-629.
// - Pin IO #0 must be connected to MAFS Pin.
//
*****************************************************************************/
#include "example.h"
//#define TTL
//#define LOCAL
#define MAX_RX_RECORDS_TO_RECEIVE 1000
#define RX_BUFFER_SIZE MAX_RX_RECORDS_TO_RECEIVE*sizeof(MXF_A629_DATAREC)
#define MAX_TX_RECORDS_TO_TRANSMIT 1
#define TX_BUFFER_SIZE MAX_TX_RECORDS_TO_TRANSMIT*sizeof(MXF_A629_DATAREC)
typedef struct
{
uint16 label;
uint16 lenght;
uint32 bufferIndex;
}
LABEL_INFO;
#define NUM_LABEL 5
#define ALT_LABEL 4
#define NUM_Y 4
#define NUM_X 2
LABEL_INFO LABEL[NUM_LABEL] = {{0xEE1,20,0}, {0xEF5,24,1}, {0xEF6,20,2}, {0xEF7,20,3}, {0xEF6,20,4}};
//***************************************************************************
//
// Main()
//
//***************************************************************************
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
uint64 count=0;
HMXF_CHANNEL rxChannel=0;
HMXF_CHANNEL txChannel=0;
HMXF_CHANNEL discreteChannel=0;
HMXF_BUFFER rxBuffer=0;
HMXF_BUFFER txBuffer[NUM_LABEL]={0,0,0,0,0};
MXF_A629_DATAREC *hostBuffer=NULL;
MXF_A629_DATAREC *rec629=NULL;
uint64 index, word;
uint64 status, msgsCount, bytesCount;
uint32 minor, label;
char szTemp[32];
LABEL_INFO *majorFrame[NUM_Y][NUM_X] = { {&LABEL[1], &LABEL[3]},
{&LABEL[1], &LABEL[0]},
{&LABEL[1], &LABEL[3]},
{&LABEL[1], &LABEL[2]} };
// 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);
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A629MRT_EH, 1, &count, &module);
// Obtain the first ARINC 629 Protocol RX channel (RX logical #0)
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A629, MXF_SCLASS_RX_CHANNEL, 1, &count, &rxChannel);
// Obtain the first ARINC 629 Protocol TX channel (TX logical #0)
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A629, MXF_SCLASS_TX_CHANNEL, 1, &count, &txChannel);
// If module or channel not found, return an error
if(!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Get discrete output channel
if(!rc)
rc = mxfChannelGet(server, MXF_CLASS_DISCRETE, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 0, &discreteChannel);
// Set timebase to 64-bit nanoseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Set interface to SIM or TTL
#ifdef TTL
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_A629_MODULE_INTERFACE, VMXF_A629_MODULE_INTERFACE_TTL);
#else
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_A629_MODULE_INTERFACE, VMXF_A629_MODULE_INTERFACE_SIM);
#endif
// Allocate RX acquisition buffer
if (!rc)
rc = mxfRxAcqBufferAlloc(rxChannel, RX_BUFFER_SIZE, &rxBuffer, NULL);
// Allocate TX Periodic Update Message buffer
for(index=0; index<NUM_LABEL && !rc; index++)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, index, TX_BUFFER_SIZE, &txBuffer[index], NULL);
// Allocate host buffer
if (!rc)
{
hostBuffer = (MXF_A629_DATAREC*)malloc(max(RX_BUFFER_SIZE, TX_BUFFER_SIZE));
if (!hostBuffer)
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");
}
//Clear the major frame
if(!rc)
rc = mxfTxPeriodicMajorFrameClear(txChannel, 0);
// set 8 minor frames with 2 labels each
for(minor=0;minor<NUM_Y && !rc;minor++)
{
memset(minorFrame, 0, sizeof(minorFrame));
for(label=0;label<NUM_X && !rc;label++)
{
minorFrame[label].buffer = txBuffer[majorFrame[minor][label]->bufferIndex];
minorFrame[label].label = majorFrame[minor][label]->label;
minorFrame[label].cid = 0;
minorFrame[label].length = majorFrame[minor][label]->lenght;
minorFrame[label].options = MXF_A629_TXPERIODIC_MJRFRAME_MSG_OPT_SYNC;
}
rc = mxfA629TxPeriodicMajorFrameSet(txChannel, 0, minor, NUM_X, minorFrame);
}
// Add Alternate frame
if(!rc)
{
minorFrame[0].buffer = txBuffer[LABEL[ALT_LABEL].bufferIndex];
minorFrame[0].label = LABEL[ALT_LABEL].label;
minorFrame[0].cid = 0;
minorFrame[0].length = LABEL[ALT_LABEL].lenght;
minorFrame[0].options = MXF_A629_TXPERIODIC_MJRFRAME_MSG_OPT_SYNC;
rc = mxfA629TxPeriodicMajorFrameSet(txChannel, 0, 30, 1, minorFrame);
}
// set SYNC Frame
if(!rc)
{
memset(&sync, 0, sizeof(sync));
sync.altIndex = 30;
sync.syncIndex = 0;
rc = mxfA629TxPeriodicMajorFrameSyncSet(txChannel, 0, 0, &sync);
if(!rc)
{
printf("*** Sync cell #0 ***\n\r");
printf("Alternate index : %i\n\r", sync.altIndex);
printf("Sync index : %i\n\r", sync.syncIndex);
}
}
//--- Set default data ---//
memset(&txRec629, 0, sizeof(txRec629));
for(index=0; index<NUM_LABEL && !rc; index++)
{
txRec629.control = 0;
txRec629.repeatCount = 1;
txRec629.dataSize = 2+(LABEL[index].lenght*2);
txRec629.data[0] = 0;
for(word=1; word<=LABEL[index].lenght; word++)
txRec629.data[word] = (uint16)(0x1111*index);
rc = mxfA629TxPeriodicUpdateMsgWrite(txBuffer[LABEL[index].bufferIndex], 1, &txRec629);
}
//Start the major frame in the Independent mode
if(!rc)
{
majorProperties.mode = MXF_A629_TXPERIODIC_MJRFRAME_PROPERTIES_MODE_INDEPENDENT;
majorProperties.reserved = 0;
rc = mxfA629TxPeriodicMajorFrameStart(txChannel, 0, &majorProperties);
}
// PIN discrete ouput #0 must be connected to MAFS Pin
if(!rc)
{
printf("Press enter key to switch to Alternate schedule\n\r");
fgets(szTemp, sizeof(szTemp), stdin);
rc = mxfAttributeUint64Set(txChannel, KMXF_A629_RT_ALTERNATE_SCHED, VMXF_ENABLE);
if(!rc)
rc = mxfDiscreteChannelWrite(discreteChannel, 0x01, 0x00);
}
if(!rc)
{
printf("Press enter key to switch to Synchronized schedule\n\r");
fgets(szTemp, sizeof(szTemp), stdin);
rc = mxfAttributeUint64Set(txChannel, KMXF_A629_RT_ALTERNATE_SCHED, VMXF_DISABLE);
}
if(!rc)
{
printf("Press a key to switch back to primary schedule\n\r");
fgets(szTemp, sizeof(szTemp), stdin);
rc = mxfDiscreteChannelWrite(discreteChannel, 0x01, 0x01);
}
if(!rc)
{
printf("Press a key to stop\n\r");
fgets(szTemp, sizeof(szTemp), stdin);
rc = mxfTxPeriodicMajorFrameStop(txChannel, 0, 0);
}
// Stop acquisition and read data
if (!rc)
rc = mxfRxAcqStop(rxBuffer);
if (!rc)
{
rc = mxfA629RxAcqRead(rxBuffer, 0, RX_BUFFER_SIZE, &status, &msgsCount, &bytesCount, hostBuffer);
if (!rc)
{
// Display received messages
rec629 = hostBuffer;
for(index=0; index<msgsCount && !rc; index++)
{
printf(" %03llu: Timetag=%012llu, Size=%u", index, rec629->timeTag, rec629->dataSize);
if(rec629->control & MXF_A629_RX_REC_CTRL_STRING_CRC_ERROR)
printf(" CRC error");
if(rec629->control & MXF_A629_RX_REC_CTRL_STRING_DATA_SYNC_ERROR)
printf(" Data sync error");
if(rec629->control & (MXF_A629_RX_REC_CTRL_STRING_LABEL_MANCHESTER_ERROR|MXF_A629_RX_REC_CTRL_STRING_DATA_MANCHESTER_ERROR))
printf(" Manchester error");
if(rec629->control & (MXF_A629_RX_REC_CTRL_STRING_LABEL_PARITY_ERROR|MXF_A629_RX_REC_CTRL_STRING_DATA_PARITY_ERROR))
printf(" Parity error");
if(rec629->control & MXF_A629_RX_REC_CTRL_STRING_EOS_ERROR)
printf(" End of String error");
printf("\n Data=");
for(word=0; word<rec629->dataSize/2; word++)
{
printf("%04X ", rec629->data[word]);
if(!((word+1)%8) && (word+1 < rec629->dataSize/2))
printf("\n ");
}
printf("\n");
mxfA629NextDataRecordPtrGet(rec629, &rec629);
}
}
}
// 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 (device)
if (hostBuffer)
free(hostBuffer);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
Updated 10/23/2023