MX Foundation 4
ar717_buffer_threshold.c
/************************************************************************************************
//
// File:
// ar717_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
// - loopback between first TX and RX MULTI_EH channels if internal loopback is not used
//
**************************************************************************************************/
#include "example.h"
//#define LOOPBACK
//#define LOCAL
#define DURATION_ms 50000
#define TXALMOSTFULL 7
#define TXALMOSTEMPTY 3
#define RXALMOSTFULL 5
#define RXALMOSTEMPTY 2
#define SUBFRAMESIZE 64 //word per subframe (64, 128, 256 or 512)
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param);
uint32 writeMsgs(HMXF_BUFFER buffer, MXF_A717_DATAREC *hostBuffer);
uint32 readAcquisition(HMXF_BUFFER rxBuffer, MXF_A717_DATAREC *rxHostBuffer);
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_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannel=0, txChannel=0;
HMXF_ASYNCEVENT asyncEvent=0;
HMXF_BUFFER rxBuffer=0, txBuffer=0;
MXF_ASYNCEVENT_CONDITION RXasyncEventInfo, TXasyncEventInfo;
uint64 moduleCount=0, channelCount=0;
size_t bufferSize;
MXF_A717_DATAREC *hostBuffer=NULL;
// 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
if(rc!=MAXT_SUCCESS)
{
printf("Failed to connect; rc=0x%08x", rc);
printf("\nPress a key to terminate\n");
getchar();
return 0;
}
// Initializes init callback handler to set first TX and RX channel to A717
rc = mxfSystemInitAttributeUint64CallbackHandler(server, &initHandler);
// Initializes the server
printf("\nStarting\n");
rc = mxfSystemInit(server);
// Gets the device handle
if(!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Gets handle of first multi_eh module
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MULTI_EH, 1, &moduleCount, &module);
// If module not found, returns an error
if(!rc && !moduleCount)
rc = MAXT_ERROR_NOT_FOUND;
// Gets handle of first A717 RX channel
if(!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A717, MXF_SCLASS_RX_CHANNEL, 1, &channelCount, &rxChannel);
// Gets handle of first A717 TX channel
if (!rc && channelCount)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A717, MXF_SCLASS_TX_CHANNEL, 1, &channelCount, &txChannel);
// If channel not found, returns an error
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
// Enables loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_A717_TX_RX_TEST_LB , VMXF_ENABLE);
#endif
// Sets timebase to 64-bit nanoseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Allocates host buffer
bufferSize = (TXALMOSTFULL*sizeof(MXF_A717_DATAREC));
if(!rc)
{
hostBuffer = (MXF_A717_DATAREC *)malloc(bufferSize);
if(!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Sets RX & TX channels subframe size
if(!rc)
rc=mxfAttributeUint64Set(rxChannel, KMXF_A717_SUBFRAME_SIZE, SUBFRAMESIZE);
if(!rc)
rc=mxfAttributeUint64Set(txChannel, KMXF_A717_SUBFRAME_SIZE, SUBFRAMESIZE);
// Sets RX & TX channels bit encoding to harvard
if(!rc)
rc=mxfAttributeUint64Set(rxChannel, KMXF_A717_BIT_ENCODING, VMXF_A717_BIT_ENCODING_HARVARDBIPHASE);
if(!rc)
rc=mxfAttributeUint64Set(txChannel, KMXF_A717_BIT_ENCODING, VMXF_A717_BIT_ENCODING_HARVARDBIPHASE);
// Sets RX & TX channels electrical selection to default
if(!rc)
rc=mxfAttributeUint64Set(rxChannel, KMXF_A717_ELECTRICAL_SELECTION, VMXF_A717_ELECTRICAL_SELECT_DEFAULT);
if(!rc)
rc=mxfAttributeUint64Set(txChannel, KMXF_A717_ELECTRICAL_SELECTION, VMXF_A717_ELECTRICAL_SELECT_DEFAULT);
// Sets the event handler
if(!rc)
rc = mxfAsyncEventHandlerInit(server, &asyncEventHandler, hostBuffer, &asyncEvent);
// Allocates RX acquisition buffer
if(!rc)
rc = mxfRxAcqBufferAlloc(rxChannel, bufferSize, &rxBuffer, NULL);
// 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");
// Allocates TX Periodic Update buffer
if(!rc)
rc = mxfTxAperiodicBufferAlloc(txChannel, MXF_TXAPERIODIC_PRIORITY_HIGH, bufferSize, &txBuffer, NULL);
// 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 all buffers
if(rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer)
// Terminates
if(hostBuffer)
free(hostBuffer);
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_A717_DATAREC *)param);
break;
case MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
readAcquisition(pendingList[i].condition.rxAcqBufferThreshold.buffer, (MXF_A717_DATAREC *)param);
break;
default:
printf("Unknown condID 0x%llx)", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Aperiodic Transmission
//****************************************************************************************************************
uint32 writeMsgs(HMXF_BUFFER buffer, MXF_A717_DATAREC *hostBuffer)
{
uint32 rc=0;
uint32 i;
MXF_A717_DATAREC *recPtr=hostBuffer;
uint64 word;
static uint32 TXAsyncEvents=0;
// Refills the FIFO
for(i=0; !rc && i<TXALMOSTFULL; i++)
{
recPtr->timeTag=0;
recPtr->control=0;
recPtr->dataSize = 2 * (uint32)SUBFRAMESIZE; // 16 bits per word in subframe
recPtr->repeatCount=1;
recPtr->reserved=0;
for(word=0; word < SUBFRAMESIZE ; word++)
{
if (word == 0)
{
//1st word of each subframe has to be a sync word
// sync words are:
// - 0x247 for subframe #0
// - 0x5B8 for subframe #1
// - 0xA47 for subframe #2
// - 0xDB8 for subframe #3
switch (i%4)
{
case 0:
recPtr->data[word] = 0x247;
break;
case 1:
recPtr->data[word] = 0x5B8;
break;
case 2:
recPtr->data[word] = 0xA47;
break;
case 3:
recPtr->data[word] = 0xDB8;
break;
default:
break;
}
}
else
recPtr->data[word] = (uint16)(0x11*word);
}
if(!rc)
rc = mxfA717NextDataRecordPtrGet(recPtr, &recPtr);
}
if(!rc)
{
printf("Transmitting ...\n");
// Transmits strings on relative record time
rc = mxfA717TxAperiodicWrite(buffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, TXALMOSTFULL, hostBuffer);
}
if(rc)
printf("Periodic Update failed; rc=0x%08x\n", rc);
else
printf("\nAsync Event %d - Writing %d records\n", ++TXAsyncEvents, i);
return rc;
}
/***************************************************************************************************************/
// RX717ReadAcquisitionData
/***************************************************************************************************************/
uint32 readAcquisition(HMXF_BUFFER rxBuffer, MXF_A717_DATAREC *rxHostBuffer)
{
uint64 status, msgsCount, bytesCount;
uint64 word, data;
uint32 rc;
size_t bufferSize = (RXALMOSTFULL*sizeof(MXF_A717_DATAREC));
// Reads and display records
rc = mxfA717RxAcqRead(rxBuffer, 0, bufferSize, &status, &msgsCount, &bytesCount, rxHostBuffer);
if(!rc)
printf("String received count = %llu \n", msgsCount);
if(!rc)
{
// Displays received strings
recPtr = rxHostBuffer;
for(data=0; data<msgsCount && !rc; data++)
{
printf("\n%02llu: Timetag=%012llu, Size=%u words\n", data, recPtr->timeTag, (recPtr->dataSize)/2);
for(word=0; word < SUBFRAMESIZE ; word++)
printf("%03X ", recPtr->data[word]);
printf("\n");
mxfA717NextDataRecordPtrGet(recPtr, &recPtr);
}
}
if(rc)
printf("Acquisition read failed; rc=0x%08x\n", rc);
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))
{
// Sets MXF_MODULE_MULTI_EH first TX and RX channels to A717
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
*value = MXF_CLASS_A717;
return TRUE;
}
}
}
return FALSE;
}
Updated 10/23/2023