MX Foundation 4
csdb_buffer_threshold.c
/************************************************************************************************
//
// File:
// csdb_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 a ramp on one label and record the messages using asynchronous
// events.
//
// Hardware Requirements:
// - MAXT Flex with loopback between first TX and RX CSDB Enhanced channels.
//
**************************************************************************************************/
#include "example.h"
#define LOOPBACK
#define LOCAL
#define BUFFER_SIZE 1*1024*1024 // 1 MiB
#define TXALMOSTFULL 7
#define TXALMOSTEMPTY 3
#define RXALMOSTFULL 5
#define RXALMOSTEMPTY 2
#define TX_MSG_LABEL 0xA0
#define TX_MSG_SI 0x01
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void *param);
uint32 updateMsgs(HMXF_BUFFER buffer, MXF_CSDB_DATAREC *hostBuffer);
uint32 readAcquisition(HMXF_BUFFER buffer, MXF_CSDB_DATAREC *hostBuffer);
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[2]={0,0};
MXF_ASYNCEVENT_CONDITION rxAsyncEventInfo, txAsyncEventInfo;
HMXF_SCHED schedule=0;
HMXF_SCHED_MSG msgSched=0;
uint64 count=0;
MXF_CSDB_DATAREC *hostBuffer=NULL;
MXF_CSDB_DATAREC recTXCsdb;
// Connect to services and initialize environment
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", TRUE, &server);
#endif
if(rc!=MAXT_SUCCESS)
{
printf("Failed to connect; rc=0x%08x", rc);
getchar();
return 0;
}
printf("\n\rStarting\n\r");
// initialize init callback handler to set IPM-ASYNC-EH first TX and RX channel to CSDB
rc = mxfSystemInitAttributeUint64CallbackHandler(server, &initHandler);
if(!rc)
rc = mxfSystemInit(server);
// Get the device handle
if(!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Get the module handle
if(!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_ASYNC_EH, 1, &count, &module);
// Get the first CSDB Protocol RX channel (RX logical #0)
if(!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_CSDB, MXF_SCLASS_RX_CHANNEL, 1, &count, &rxChannel);
// Get the first CSDB Protocol TX channel (TX logical #0)
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_CSDB, MXF_SCLASS_TX_CHANNEL, 1, &count, &txChannel);
// If channel not found, return an error
if(!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Configure RX and TX channel to 12.5Kbps, blockcount 6, parity odd, one stop bit
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_CSDB_SPEED, 12500);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_CSDB_SPEED, 12500);
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_CSDB_BLOCKCOUNT, 6);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_CSDB_BLOCKCOUNT, 6);
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_CSDB_PARITY, VMXF_CSDB_PARITY_ODD);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_CSDB_PARITY, VMXF_CSDB_PARITY_ODD);
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_CSDB_STOPBITSIZE, 1);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_CSDB_STOPBITSIZE, 1);
// Enable loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_CSDB_TX_RX_TEST_LB , VMXF_ENABLE);
#endif
// Set timebase to 64-bit nanoseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Alloc host buffer (1 MiB)
if(!rc)
{
hostBuffer = (MXF_CSDB_DATAREC *)malloc(BUFFER_SIZE);
if(!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Set the event handler
if(!rc)
rc = mxfAsyncEventHandlerInit(server, &asyncEventHandler, hostBuffer, &asyncEvent);
// Allocate RX acquisition buffer (1 MiB)
if(!rc)
rc = mxfRxAcqBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Set 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);
}
// Set acquisition mode
if(!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Start acquisition
if(!rc)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if(!rc)
printf("Acquisition started\n\r");
// Allocate TX sync block buffer
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, 0xA5, 4096, &txBuffer[0], NULL);
// Allocate TX periodic update buffer (1MiB)
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, TX_MSG_LABEL, BUFFER_SIZE, &txBuffer[1], NULL);
// Set the TX async event condition
if(!rc)
{
memset(&txAsyncEventInfo, 0, sizeof(txAsyncEventInfo));
txAsyncEventInfo.condID = MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD;
txAsyncEventInfo.condition.txPeriodicUpdateMsgBufferThreshold.almostFull = TXALMOSTFULL;
txAsyncEventInfo.condition.txPeriodicUpdateMsgBufferThreshold.almostEmpty = TXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &txAsyncEventInfo);
}
// Select message for async event condition
if(!rc)
rc = mxfAsyncEventTxPeriodicUpdateMsgSelectSet(asyncEvent, txChannel, MXF_MSG_SELECT_ONLY, 1, &txBuffer[1]);
// Set the Periodic Scheduler
if(!rc)
rc = mxfTxPeriodicScheduleNew(txChannel, &schedule);
// Add one message to scheduler, Rate=50 ms, Phase=0 us
if(!rc)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 50000000, 0, &msgSched);
// Add two buffers to the message
if(!rc)
rc = mxfTxPeriodicScheduleBufferListAdd(msgSched, 2, 0, txBuffer);
// Set sync block, will not be updated later on
if(!rc)
{
// SYNC block
recTXCsdb.timeTag = 0;
recTXCsdb.control = 0;
recTXCsdb.repeatCount = 1;
recTXCsdb.reserved = 0;
recTXCsdb.data[0] = 0xA5;
recTXCsdb.data[1] = 0xA5;
recTXCsdb.data[2] = 0xA5;
recTXCsdb.data[3] = 0xA5;
recTXCsdb.data[4] = 0xA5;
recTXCsdb.data[5] = 0xA5;
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[0], 1, &recTXCsdb);
}
// Run the schedule
if(!rc)
rc = mxfTxPeriodicScheduleRun(schedule);
// Send messages for 2 seconds
if(!rc)
{
printf("Running periodic transmission...\n\r");
mxfSleep(2000);
rc = mxfTxPeriodicScheduleFree(schedule);
}
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(rxBuffer);
// Disable conditions
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &rxAsyncEventInfo);
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &txAsyncEventInfo);
// Terminate async event handler
if(!rc)
// Catch 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);
}
// Terminate
if(hostBuffer)
free(hostBuffer);
printf("\n\rPress enter to terminate\n\r");
getchar();
return rc;
}
//****************************************************************************************************************
// Asynchronous Event Handler
//****************************************************************************************************************
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void *param)
{
uint64 maxCount=64, pendingCount;
uint64 i;
uint32 rc;
// Get 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_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD:
// An almost empty condition was detected...
updateMsgs(pendingList[i].condition.txPeriodicUpdateMsgBufferThreshold.buffer, (MXF_CSDB_DATAREC *)param);
break;
case MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
readAcquisition(pendingList[i].condition.rxAcqBufferThreshold.buffer, (MXF_CSDB_DATAREC *)param);
break;
default:
printf("Unknown condID 0x%"PRIx64")", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Periodic Transmission
//****************************************************************************************************************
uint32 updateMsgs(HMXF_BUFFER buffer, MXF_CSDB_DATAREC *hostBuffer)
{
uint32 rc=0;
uint32 i;
MXF_CSDB_DATAREC *recPtr=hostBuffer;
static uint16 data=0;
static uint32 TXAsyncEvents=0;
// Refill the FIFO in order to produce a ramp
for(i=0; !rc && i<TXALMOSTFULL; i++)
{
// Send a parity error on first record
recPtr->timeTag=0;
recPtr->control=(i!=0)?0:MXF_CSDB_TX_REC_CTRL_PARITY_ERROR;
recPtr->repeatCount=1;
recPtr->reserved=0;
recPtr->data[0] = TX_MSG_LABEL;
recPtr->data[1] = 0x40 | TX_MSG_SI;
recPtr->data[2] = (uint8)data;
recPtr->data[3] = (uint8)(data >> 8);
recPtr->data[4] = (uint8)data;
recPtr->data[5] = (uint8)(data >> 8);
data++;
rc = mxfCSDBNextDataRecordPtrGet(recPtr, &recPtr);
}
// Add more data to the buffer
if(!rc)
rc = mxfCSDBTxPeriodicUpdateMsgWrite(buffer, i, hostBuffer);
if(rc)
printf("Periodic Update failed; rc=0x%08x\n\r", rc);
else
printf("\nAsync Event %d - Writing %d records\n\r", ++TXAsyncEvents, i);
return rc;
}
//****************************************************************************************************************
// Acquisition Reception
//****************************************************************************************************************
uint32 readAcquisition(HMXF_BUFFER buffer, MXF_CSDB_DATAREC *hostBuffer)
{
MXF_CSDB_DATAREC *recPtr=hostBuffer;
uint64 status, msgsCount, bytesCount;
uint64 j;
uint32 rc, data;
// Read and display records
rc = mxfCSDBRxAcqRead(buffer, 0, BUFFER_SIZE, &status, &msgsCount, &bytesCount, hostBuffer);
if(!rc)
{
printf("Read %"PRIu64" messages\n\r", msgsCount);
for (j=0; !rc && j<msgsCount; j++)
{
printf("%02"PRIu64": Timetag %"PRIu64" - ", j, recPtr->timeTag);
for(data=0; data<6; data++)
printf("%02x", recPtr->data[data]);
if(recPtr->control & MXF_CSDB_RX_REC_CTRL_PARITY_ERROR)
printf(" Parity error");
if(recPtr->control & MXF_CSDB_RX_REC_CTRL_STOPBIT_ERROR)
printf(" Stop bit error");
printf("\n\r");
rc = mxfCSDBNextDataRecordPtrGet(recPtr, &recPtr);
}
}
if(rc)
printf("Acquisition read failed; rc=0x%08x\n\r", 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_ASYNC_EH))
{
// Sets IPM-ASYNC-EH first TX and RX channel to CSDB
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
*value = MXF_CLASS_CSDB;
return TRUE;
}
}
}
return FALSE;
}
Updated 10/23/2023