MX Foundation 4
hfce_buffer_threshold.c
/************************************************************************************************
//
// File:
// hfce_buffer_threshold.c
//
// Copyright (c) MAX Technologies Inc. 1988-2020, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example illustrates how to manage buffer thresholds with asynchronous events.
// The demo will transmit and record the messages using asynchronous
// events.
//
// Hardware Requirements:
// - 500 series carrier with IPM-HFCE
// - loopback between TX0 and RX0
//
**************************************************************************************************/
#include "example.h"
#define LOOPBACK
#define DURATION_ms 5000
#define TXALMOSTFULL 7
#define TXALMOSTEMPTY 3
#define RXALMOSTFULL 5
#define RXALMOSTEMPTY 2
#define LABEL 0xCD10
#define PAYLOAD_SIZE 6
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void *param);
uint32 writeMsgs(HMXF_BUFFER buffer, MXF_HFCE_DATAREC *txHostBuffer);
uint32 readAcquisition(HMXF_BUFFER rxBuffer, MXF_HFCE_DATAREC *rxHostBuffer);
void DisplayDataArray(uint64 recNum, MXF_HFCE_DATAREC* rec);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_CHANNEL rxChannel=0, txChannel=0;
HMXF_ASYNCEVENT asyncEvent=0;
HMXF_BUFFER rxBuffer=0, txBuffer=0;
MXF_HFCE_DATAREC* rxHostBuffer=0;
MXF_HFCE_DATAREC* txHostBuffer=0;
MXF_ASYNCEVENT_CONDITION RXasyncEventInfo, TXasyncEventInfo;
size_t txBufferSize, rxBufferSize;
// 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(txChannel, KMXF_HFCE_SPEED, 400000);
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, 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 = TXALMOSTFULL*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 = RXALMOSTFULL*sizeof(MXF_HFCE_DATAREC);
// 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 nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Sets the event handler
if(!rc)
rc = mxfAsyncEventHandlerInit(server, asyncEventHandler, txHostBuffer, &asyncEvent);
// Sets the RX async event condition
if(!rc)
{
memset(&RXasyncEventInfo, 0, sizeof(RXasyncEventInfo));
RXasyncEventInfo.condID = MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD;
RXasyncEventInfo.condition.rxAcqBufferThreshold.buffer = rxBuffer;
RXasyncEventInfo.condition.rxAcqBufferThreshold.almostFull = RXALMOSTFULL;
RXasyncEventInfo.condition.rxAcqBufferThreshold.almostEmpty = RXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &RXasyncEventInfo);
}
// Sets acquisition mode
if(!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Starts acquisition
if(!rc)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if(!rc)
printf("Acquisition started\n\r");
// Sets the TX async event condition
if(!rc)
{
memset(&TXasyncEventInfo, 0, sizeof(TXasyncEventInfo));
TXasyncEventInfo.condID = MXF_ASYNCEVENT_COND_TXAPERIODIC_BUFFER_THRESHOLD;
TXasyncEventInfo.condition.txAperiodicBufferThreshold.buffer = txBuffer;
TXasyncEventInfo.condition.txAperiodicBufferThreshold.almostFull = TXALMOSTFULL;
TXasyncEventInfo.condition.txAperiodicBufferThreshold.almostEmpty = TXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &TXasyncEventInfo);
}
// Waits for transmission and reading to occur
if(!rc)
mxfSleep(DURATION_ms);
// Stops acquisition
if(!rc)
rc = mxfRxAcqStop(rxBuffer);
// Disable conditions
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &RXasyncEventInfo);
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &TXasyncEventInfo);
// Terminates async event handler
if(!rc)
// Catches any previous failing function
if(rc)
{
char buffer[256];
if(mxfSystemErrorStringGet(server, rc, sizeof(buffer), buffer))
sprintf (buffer,"ERROR # 0x%08X", rc);
printf("%s\n\r", buffer);
}
// Frees device and host buffers
if (txBuffer)
if (rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
// Terminates
if(txHostBuffer)
free(txHostBuffer);
if(rxHostBuffer)
free(rxHostBuffer);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
//****************************************************************************************************************
// Asynchronous Event Handler
//****************************************************************************************************************
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void *param)
{
uint64 maxCount=64, pendingCount;
uint64 i;
uint32 rc;
// Gets the list of pending events to process
rc = mxfAsyncEventPendingGet(asyncEvent, maxCount, &pendingCount, pendingList);
for (i=0; !rc && i<pendingCount; i++)
{
switch(pendingList[i].condID)
{
case MXF_ASYNCEVENT_COND_TXAPERIODIC_BUFFER_THRESHOLD:
// An almost empty condition was detected...
writeMsgs(pendingList[i].condition.txAperiodicBufferThreshold.buffer, (MXF_HFCE_DATAREC *)param);
break;
case MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
readAcquisition(pendingList[i].condition.rxAcqBufferThreshold.buffer, (MXF_HFCE_DATAREC *)param);
break;
default:
printf("Unknown condID 0x%"PRIx64")", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Aperiodic Transmission
//****************************************************************************************************************
uint32 writeMsgs(HMXF_BUFFER buffer, MXF_HFCE_DATAREC *txHostBuffer)
{
uint32 rc=0;
uint32 i;
MXF_HFCE_DATAREC *rec=txHostBuffer;
uint64 word;
static uint32 TXAsyncEvents=0;
static uint64 time=0;
// Gets initial timer value
if(TXAsyncEvents == 0)
{
HMXF_CHANNEL channel;
HMXF_DEVICE device=0;
rc = mxfTxAperiodicBufferInfoGet(buffer, &channel, NULL);
if(!rc)
rc = mxfChannelInfoGet(channel, &device, NULL);
if(!rc)
rc = mxfDeviceTimerGet(device, &time);
}
// Refills the FIFO
for(i=0; !rc && i<TXALMOSTFULL; i++)
{
time += 100000000;
rec->timeTag = time;
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)(0x0101*word);
}
rc = mxfHFCENextDataRecordPtrGet(rec, &rec);
}
if(!rc)
{
printf("Transmitting ...\n");
// Transmits strings on absolute record time
rc = mxfHFCETxAperiodicWrite(buffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, TXALMOSTFULL, txHostBuffer);
}
if(rc)
printf("Periodic Update failed; rc=0x%08x\n", rc);
else
printf("\nAsync Event %d - Writing %d records\n", ++TXAsyncEvents, i);
return rc;
}
/***************************************************************************************************************/
// readAcquisition
/***************************************************************************************************************/
uint32 readAcquisition(HMXF_BUFFER rxBuffer, MXF_HFCE_DATAREC *rxHostBuffer)
{
uint64 status, msgsCount, bytesCount;
uint32 rc;
size_t bufferSize = (RXALMOSTFULL*sizeof(MXF_HFCE_DATAREC));
// Reads and display records
rc = mxfHFCERxAcqRead(rxBuffer, 0, bufferSize, &status, &msgsCount, &bytesCount, rxHostBuffer);
if(!rc)
printf("String received count = %"PRIu64" \n", msgsCount);
if(!rc)
{
// Displays received strings
DisplayDataArray(msgsCount, rxHostBuffer);
}
if(rc)
printf("Acquisition read failed; rc=0x%08x\n", rc);
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 %03u ", 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