MX Foundation 4
csdb_rx_acquisition_trigger.c
/*******************************************************************************
//
// File:
// csdb_rx_acquisition_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 demo illustrates how to use the trigger functionality
// to start acquisition on a receive channel.
//
// The program first starts a basic periodic transmission using the periodic
// scheduling service. The acquisition process is then started and waits for
// bit 7 value of byte 1 of label 07 to be 0.
//
// Hardware Requirements:
// - MAXT Flex with loopback between first TX and RX CSDB Enhanced channels.
//
*******************************************************************************/
#include "example.h"
#include <time.h>
#define LOOPBACK
#define LOCAL
#define BUFFER_SIZE 1*1024*1024 // 1 MiB
#define LABEL 0x7
#define SI 0
#define BLOCKCOUNT 6
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_CSDB_DATAREC *recCsdb);
uint32 PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER *txBuffer, MXF_CSDB_DATAREC *recCsdb);
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_BUFFER rxBuffer=0, txBuffer[2]={0,0};
uint64 count=0;
MXF_CSDB_DATAREC *hostBuffer=NULL;
HMXF_COND_LIST condList=0;
// 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 = mxfChannelAllGet(server, MXF_CLASS_CSDB, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ASYNC_EH, 1, &count, &rxChannel);
// Get the first CSDB Protocol TX channel (TX logical #0)
if (!rc && count)
rc = mxfChannelAllGet(server, MXF_CLASS_CSDB, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ASYNC_EH, 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, BLOCKCOUNT);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
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 microseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_USEC);
// Alloc host buffer
if(!rc)
{
hostBuffer = (MXF_CSDB_DATAREC *)malloc(BUFFER_SIZE);
if(!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Allocate RX acquisition buffer
if(!rc)
rc = mxfRxAcqBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Allocate TX sync block buffer
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, 0xA5, 4096, &txBuffer[0], NULL);
// Allocate TX periodic update buffer
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, LABEL, BUFFER_SIZE, &txBuffer[1], NULL);
// Configure trigger
// Create the condition list
if (!rc)
rc = mxfRxAcqTrigConditionListAlloc(server, &condList);
// The condition will be triggered on bit 7 value of byte 1 of LABEL/SI
if(!rc)
{
condParam.mask = 0x000083ff;
condParam.data = (SI << 8) | LABEL;
condParam.offset = 0;
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 30 which allows some records to be read before the condition was triggered.
// For the CSDB protocol this number is rounded to the closest internal acquisition buffer size.
if(!rc)
rc = mxfRxAcqTrigSet(rxBuffer, condList, 30);
// Start the acquisition process
if (!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_CIRCULAR);
if (!rc)
{
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
printf("\nAcquisition started\n\r");
}
// Start data transmission
if (!rc)
{
rc = PeriodicScheduling(txChannel, txBuffer, hostBuffer);
if (!rc)
{
// Read data in acquisition buffer
rc = ReadAcquisitionData(rxBuffer, hostBuffer);
}
}
// Stop and flush unread data
if (!rc)
rc = mxfRxAcqStop(rxBuffer);
if (!rc)
{
rc = mxfRxAcqClear(rxBuffer);
if (!rc)
printf("\nAcquisition stopped\n\r");
}
// Free trigger condition list
if(!rc)
// Catch any previous error
if (rc)
{
char buffer[256];
rc = mxfSystemErrorStringGet(server, rc, sizeof(buffer), buffer);
printf("%s\n", buffer);
}
printf("\nTerminating\n");
// Free all buffers and terminate
if (device)
if (hostBuffer)
free(hostBuffer);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
/***************************************************************************************************************/
// PeriodicScheduling
/***************************************************************************************************************/
uint32 PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER *txBuffer, MXF_CSDB_DATAREC *recCsdb)
{
HMXF_SCHED schedule;
HMXF_SCHED_MSG msg=0;
uint32 rc;
uint64 i;
// Create the periodic scheduler
rc = mxfTxPeriodicScheduleNew(txChannel, &schedule);
// Set scheduling values: Rate=100 ms (.1sec), Phase=0 us
if(!rc)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 100000LL, 0, &msg);
// Define the number of buffer for the list and link to it
if(!rc)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 2, 0, txBuffer);
// Set records for the buffers
if(!rc)
{
// Set the sync block
recCsdb->timeTag = 0;
recCsdb->control = 0;
recCsdb->repeatCount = 1;
recCsdb->reserved = 0;
for (i=0; i<BLOCKCOUNT; i++)
recCsdb->data[i] = 0xa5;
rc = mxfTxPeriodicUpdateMsgWrite(txBuffer[0], 1, recCsdb);
// Set the message block
recCsdb->timeTag = 0;
recCsdb->control = 0;
recCsdb->repeatCount = 1;
recCsdb->data[0] = LABEL;
recCsdb->data[1] = 0x80 | SI;
for (i=2; i<BLOCKCOUNT; i++)
recCsdb->data[i]= (uint8)i;
recCsdb->reserved=0;
rc = mxfTxPeriodicUpdateMsgWrite(txBuffer[1], 1, recCsdb);
}
// Run the scheduler
if(!rc)
{
printf("Running periodic transmission, please wait...\n\r");
// Run the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Wait 0.5 second
if(!rc)
{
mxfSleep(500);
// fire trigger
recCsdb->control = 0;
recCsdb->data[1] = SI;
rc = mxfTxPeriodicUpdateMsgWrite(txBuffer[1], 1, recCsdb);
}
// Wait 0.5 second
if(!rc)
{
mxfSleep(500);
rc = mxfTxPeriodicScheduleFree(schedule);
}
if(!rc)
printf("\n\rTransmission stopped\n\r");
return rc;
}
/***************************************************************************************************************/
// ReadAcquisitionData
/***************************************************************************************************************/
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_CSDB_DATAREC *recCsdb)
{
MXF_CSDB_DATAREC* rec=recCsdb;
uint64 status, msgsCount, bytesCount;
uint64 i, j, trigTime=0;
uint32 rc;
time_t timeTemp;
struct tm * ptm;
char szTime[1024];
uint64 msec, usec;
// Check trigger happened
rc = mxfRxAcqBufferStatusGet(rxBuffer, &status, NULL, NULL, NULL);
if(!rc)
{
if(status & MXF_RXACQ_STATUS_TRIG_OCCURRED)
{
// Display 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");
}
// Read and display records
rc = mxfRxAcqRead(rxBuffer, 0, BUFFER_SIZE, &status, &msgsCount, &bytesCount, recCsdb);
for (j=0; j<msgsCount && !rc; j++)
{
printf("%02"PRIu64": Timetag=%016"PRIu64": CSDB data=[ ", j, rec->timeTag);
for (i=0; i<BLOCKCOUNT; i++)
printf("0x%02X ", rec->data[i]);
printf("] %s\n\r", trigTime==rec->timeTag?"*":"");
mxfNextRecordPtrGet(MXF_CLASS_CSDB, MXF_RECTYPE_DATAREC, rec, (void**)&rec);
}
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