MX Foundation 4
hdlc_trigger.c
/*****************************************************************************
//
// File:
// hdlc_trigger.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 usage of HDLC channel class
// for transmission and reception using a trigger.
//
// Hardware requirements:
// - MAXT FlexMulti or 500 series carrier with IPM-MULTI
// - loopback between TX0 and RX0 and TX4 and RX7 Multi channels.
//
*****************************************************************************/
#include "example.h"
#include <time.h>
#define MAX_TX_RECORDS_TO_TRANSMIT 8
//#define LOCAL
//#define LOOPBACK
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
void DisplayDataArray(uint64 rececNum, MXF_HDLC_DATAREC* rec);
int main(void)
{
uint32 rc;
HMXF_SERVER server=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_HDLC_DATAREC* rxHostBuffer=0;
MXF_HDLC_DATAREC* txHostBuffer=0;
uint64 rxAcqStatus=0;
uint64 msgCount=0;
uint64 byteCount=0;
uint64 data,word=0;
HMXF_COND_LIST condList=0;
time_t timeTemp;
struct tm * ptm;
char szTime[1024];
uint64 msec, usec;
uint64 trigTime;
// 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
//Configuration of the Multi port in HDLC
// Initializes MX Foundation library
if (!rc)
{
printf("Starting ...\n");
rc = mxfSystemInit(server);
}
// Gets handle of first HDLC RX channel
if(!rc)
rc = mxfChannelGet(server, MXF_CLASS_HDLC, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, 0, &rxChannel);
// Gets handle of first HDLC TX channel
if (!rc)
rc = mxfChannelGet(server, MXF_CLASS_HDLC, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 0, &txChannel);
// Sets frame size and internal clock frequency to be sure of the used values
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_FRAME_SIZE_ENABLE, VMXF_DISABLE);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_FRAME_SIZE_ENABLE, VMXF_DISABLE);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_INTERNAL_CLOCK_FREQ, 100000);
//Enable HDLC
if (!rc)
rc = mxfHDLCChannelEnable(txChannel, VMXF_HDLC_CLOCK_SOURCE_INTERNAL);
//Enable HDLC
if (!rc)
rc = mxfHDLCChannelEnable(rxChannel, VMXF_HDLC_CLOCK_SOURCE_EXTERNAL);
// Enables loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocates 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_HDLC_DATAREC*) calloc(1, txBufferSize);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocates 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_HDLC_DATAREC*) calloc(1, rxBufferSize);
if(!rxHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Sets timebase to RTC usec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_USEC);
// Configures trigger
// Creates the condition list
if (!rc)
rc = mxfRxAcqTrigConditionListAlloc(server, &condList);
// The condition will be triggered on the sixth record
if(!rc)
{
condParam.mask = 0x0000000F;
condParam.data = 0x00000005;
condParam.offset = 0;
condParam.options = MXF_RXACQ_TRIG_COND_RDATA_OPTIONS_EQUAL;
rc = mxfRxAcqTrigConditionAdd(condList, MXF_RXACQ_TRIG_COND_ID_RDATA_DW , &condParam);
}
// Set the trigger with a pretrig count of 3 which allows some records to be read before the condition was triggered.
if(!rc)
rc = mxfRxAcqTrigSet(rxBuffer, condList, 3);
if (!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_CIRCULAR);
// Starts acquisition
if(!rc)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if(!rc)
{
// Prepares string array (for later when the basic test works)
rec = txHostBuffer;
//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->dataSize = 8;
rec->reserved = 0;
for(word=0; word < rec->dataSize/2; word++)
{
rec->data[word] = (uint16)(0x0001*data);
}
rc = mxfHDLCNextDataRecordPtrGet(rec, &rec);
}
}
//if (!rc)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
if(!rc)
{
printf("Transmitting ...\n");
// Transmits strings on relative record time
rc = mxfHDLCTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if(!rc)
mxfSleep(1000);
// Check trigger happened
rc = mxfRxAcqBufferStatusGet(rxBuffer, &rxAcqStatus, NULL, NULL, NULL);
if(!rc)
{
if(rxAcqStatus & MXF_RXACQ_STATUS_TRIG_OCCURRED)
{
// Displays the acquisition trigger time
rc = mxfRxAcqTrigTimeGet(rxBuffer, &trigTime);
if(!rc)
{
timeTemp = trigTime/1000000;
ptm = localtime(&timeTemp);
strftime(szTime, sizeof(szTime), "%Y-%m-%d %H:%M:%S", ptm);
usec = trigTime%1000;
timeTemp = trigTime/1000;
msec = timeTemp%1000;
printf("Event triggered at %s:%03"PRIu64":%03"PRIu64"\n", szTime, msec, usec);
}
}
else
printf("Trigger not fired\n");
}
if(!rc)
{
printf("Receiving ...\n");
// Reads rx buffer
rc = mxfHDLCRxAcqRead(rxBuffer, 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxHostBuffer);
}
if(!rc)
{
printf("String received count = %"PRIu64" \n", msgCount);
// Displays received strings
DisplayDataArray(msgCount, rxHostBuffer);
}
// Stops acquisition
if(!rc)
rc = mxfRxAcqStop(rxBuffer);
// Free trigger condition list
if(!rc)
//Disable HDLC
if (txChannel)
if (rxChannel)
// Frees device and host buffers
if(txBuffer)
if(rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
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;
server=server;
deviceIndex=deviceIndex;
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) || (deviceInfo.modules[moduleIndex].type == MXF_MODULE_MULTI)))
{
// Sets IPM-MULTI-EH first TX and RX channel to HDLC
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
*value = MXF_CLASS_HDLC;
return TRUE;
}
else if ((channelIndex == 4) || (channelIndex == deviceInfo.modules[moduleIndex].txCount+4))
{
*value = MXF_CLASS_CLOCK;
return TRUE;
}
}
}
return FALSE;
}
void DisplayDataArray(uint64 recNum, MXF_HDLC_DATAREC* rec)
{
uint64 iRec,
iData;
printf("\n");
for(iRec=0; iRec < recNum; iRec++)
{
printf("%03"PRIu64" %010"PRIu64" 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