MX Foundation 4
hdlc_buffer_threshold.c
/************************************************************************************************
//
// File:
// hdlc_buffer_threshold.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 illustrates how to manage buffer thresholds with asynchronous events.
// The demo will transmit and record the messages using asynchronous
// events.
//
// 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"
//#define LOOPBACK
//#define LOCAL
#define DURATION_ms 5000
#define TXALMOSTFULL 7
#define TXALMOSTEMPTY 3
#define RXALMOSTFULL 5
#define RXALMOSTEMPTY 2
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void *param);
uint32 writeMsgs(HMXF_BUFFER buffer, MXF_HDLC_DATAREC *txHostBuffer);
uint32 readAcquisition(HMXF_BUFFER rxBuffer, MXF_HDLC_DATAREC *rxHostBuffer);
void DisplayDataArray(uint64 recNum, MXF_HDLC_DATAREC* rec);
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
/***************************************************************************************************************/
// 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_HDLC_DATAREC* rxHostBuffer=0;
MXF_HDLC_DATAREC* txHostBuffer=0;
MXF_ASYNCEVENT_CONDITION RXasyncEventInfo, TXasyncEventInfo;
size_t txBufferSize, rxBufferSize;
// 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
if(!rc)
// 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
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_FRAME_SIZE_ENABLE, VMXF_ENABLE);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_FRAME_SIZE_ENABLE, VMXF_ENABLE);
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 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);
}
//Disable HDLC
if (txChannel)
if (rxChannel)
// 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_HDLC_DATAREC *)param);
break;
case MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
readAcquisition(pendingList[i].condition.rxAcqBufferThreshold.buffer, (MXF_HDLC_DATAREC *)param);
break;
default:
printf("Unknown condID 0x%"PRIx64")", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Aperiodic Transmission
//****************************************************************************************************************
uint32 writeMsgs(HMXF_BUFFER buffer, MXF_HDLC_DATAREC *txHostBuffer)
{
uint32 rc=0;
uint32 i;
MXF_HDLC_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 = 64;
rec->reserved = 0;
for(word=0; word < rec->dataSize/2; word++)
{
rec->data[word] = (uint16)(0x0101*word);
}
rc = mxfHDLCNextDataRecordPtrGet(rec, &rec);
}
if(!rc)
{
printf("Transmitting ...\n");
// Transmits strings on absolute record time
rc = mxfHDLCTxAperiodicWrite(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_HDLC_DATAREC *rxHostBuffer)
{
uint64 status, msgsCount, bytesCount;
uint32 rc;
size_t bufferSize = (RXALMOSTFULL*sizeof(MXF_HDLC_DATAREC));
// Reads and display records
rc = mxfHDLCRxAcqRead(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_HDLC_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");
}
}
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;
}
Updated 10/23/2023