MX Foundation 4
hfce_trigger.c
/*****************************************************************************
//
// File:
// hfce_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 HFCE channel class
// for transmission and reception using a trigger.
//
// Hardware requirements:
// - 500 series carrier with IPM-HFCE
// - loopback between TX0 and RX0
//
*****************************************************************************/
#include "example.h"
#include <time.h>
#define MAX_TX_RECORDS_TO_TRANSMIT 8
#define LOOPBACK
#define LABEL 0xCD10
#define PAYLOAD_SIZE 6
void DisplayDataArray(uint64 rececNum, MXF_HFCE_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_HFCE_DATAREC* rxHostBuffer=0;
MXF_HFCE_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
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
// Initializes MX Foundation library
if (!rc)
{
printf("Starting ...\n");
rc = mxfSystemInit(server);
}
// Gets handle of first HFCE RX channel
if(!rc)
rc = mxfChannelGet(server, MXF_CLASS_HFCE, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, 0, &rxChannel);
// Gets handle of first HFCE TX channel
if (!rc)
rc = mxfChannelGet(server, MXF_CLASS_HFCE, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 0, &txChannel);
// Sets speed
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HFCE_SPEED, 400000);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_HFCE_SPEED, 400000);
// Sets interframe idle pattern
if (!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_HFCE_TX_INTERFRAME_TIME_FILL, VMXF_HFCE_TX_INTERFRAME_TIME_FILL_IDLE_PATTERN);
// Enables loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HFCE_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocates buffer for tx data
if(!rc)
{
txBufferSize = MAX_TX_RECORDS_TO_TRANSMIT * sizeof(MXF_HFCE_DATAREC);
// 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_HFCE_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_HFCE_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 = 2; // first payload word
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)
{
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 = (PAYLOAD_SIZE+2)*2;
rec->reserved = 0;
rec->data[0] = LABEL;
rec->data[1] = PAYLOAD_SIZE;
for(word=0; word < PAYLOAD_SIZE; word++)
{
rec->data[word+2] = (uint16)(0x0001*data);
}
rc = mxfHFCENextDataRecordPtrGet(rec, &rec);
}
}
//if (!rc)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
if(!rc)
{
printf("Transmitting ...\n");
// Transmits strings on relative record time
rc = mxfHFCETxAperiodicWrite(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 = mxfHFCERxAcqRead(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)
// 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;
}
void DisplayDataArray(uint64 recNum, MXF_HFCE_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