MX Foundation 4
async_rx_acquisition_trigger.c
/*******************************************************************************
//
// File:
// async_rx_acquisition_trigger.c
//
// Copyright (c) MAX Technologies Inc. 1988-2019, 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 the acquisition and waits for first byte of the
// message to have a value of 07. Aperiodic transmission is then done to fire
// the trigger.
//
// Note that this example uses RS-422 instead of the default RS-485.
//
// Hardware Requirements:
// - MAXT Flex Device with ASYNC support
//
*******************************************************************************/
#include "example.h"
#include <time.h>
//#define LOOPBACK
#define LOCAL
#define MAX_TX_RECORDS_TO_TRANSMIT 100
#define BUFFER_SIZE MAX_TX_RECORDS_TO_TRANSMIT*sizeof(MXF_ASYNCEH_DATAREC) // Space for at least 100 records
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_ASYNCEH_DATAREC *recAsync);
uint32 TransmitAperiodicData(HMXF_BUFFER txBuffer, MXF_ASYNCEH_DATAREC *recAsync);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
uint64 count=0;
HMXF_CHANNEL rxChannel=0;
HMXF_CHANNEL txChannel=0;
HMXF_BUFFER rxBuffer=0;
HMXF_BUFFER txBuffer=0;
MXF_ASYNCEH_DATAREC *recAsync=NULL;
uint64 dev, mod, port;
HMXF_COND_LIST condList=0;
// Connect to services local or remote
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
if (rc)
{
printf("Failed to connect; rc=0x%08x", rc);
printf("\nPress a key to terminate\n");
getchar();
return 0;
}
// Initialize the server
printf("\nStarting\n");
rc = mxfSystemInit(server);
// Obtain the first ASYNC Protocol RX channel (RX logical #0)
if (!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, 1, &count, &rxChannel);
// Obtain the first ASYNC Protocol TX channel (TX logical #0)
if (!rc && count)
rc = mxfChannelAllGet(server, MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 1, &count, &txChannel);
// If channel not found, return an error
if(!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Set timebase to computer time 64-bit microseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_USEC);
// Get the physical port location
if (!rc)
{
rc = mxfChannelLocationGet(rxChannel, &dev, &mod, &port);
if (!rc)
printf("Acquisition Channel (RX) location=%"PRIu64".%"PRIu64".%"PRIu64"\n", dev, mod, port);
}
if (!rc)
{
rc = mxfChannelLocationGet(txChannel, &dev, &mod, &port);
if (!rc)
printf("Transmitter Channel (TX) location=%"PRIu64".%"PRIu64".%"PRIu64"\n", dev, mod, port);
}
//Activate loopback before transmission and reception
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
//Set electrical interface to RS-422
if (!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_ELECTRICAL_INTERFACE, VMXF_ASYNCEH_ELECTRICAL_INTERFACE_RS422);
// Allocate RX acquisition buffer
if (!rc)
rc = mxfRxAcqBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Allocate TX Aperiodic buffer
if(!rc)
rc = mxfTxAperiodicBufferAlloc(txChannel, 0, BUFFER_SIZE, &txBuffer, NULL);
// Allocate host buffer
if (!rc)
{
recAsync = (MXF_ASYNCEH_DATAREC*)malloc(BUFFER_SIZE);
if (!recAsync)
rc = MAXT_ERROR_MEM;
}
// Configure trigger
// Create the condition list
if (!rc)
rc = mxfRxAcqTrigConditionListAlloc(server, &condList);
// The condition will be triggered on the 8th record
if(!rc)
{
condParam.mask = 0x000000ff;
condParam.data = 7;
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 3 which allows some records to be read before the condition was triggered.
if(!rc)
rc = mxfRxAcqTrigSet(rxBuffer, condList, 3);
// Start the acquisition process
if (!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
if (!rc)
{
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
printf("\nAcquisition started\n\r");
}
mxfSleep(1); //necessary to allow one RX string gap
// Start ASYNC data transmission
if (!rc)
{
rc = TransmitAperiodicData(txBuffer, recAsync);
if (!rc)
{
// Read ASYNC data in acquisition buffer
rc = ReadAcquisitionData(rxBuffer, recAsync);
}
}
// 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);
}
//Frees all buffers
if(rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
if(txBuffer)
printf("\nTerminating\n");
if (recAsync)
free(recAsync);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
/***************************************************************************************************************/
// TransmitAperiodicData
/***************************************************************************************************************/
uint32 TransmitAperiodicData(HMXF_BUFFER txBuffer, MXF_ASYNCEH_DATAREC *recAsync)
{
uint32 rc;
MXF_ASYNCEH_DATAREC* rec=recAsync;
uint64 data, byte;
// Prepare string array (8 bytes per string with minimum delay between each)
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = 0;
rec->control = 0;
rec->repeatCount = 1;
rec->dataSize = 8;
rec->reserved = 0;
for(byte=0; byte < rec->dataSize; byte++)
{
rec->data[byte] = (uint8)((byte)?byte:data);
}
}
printf("Transmitting ...\n");
rc = mxfASYNCEHTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_RECORDS_TO_TRANSMIT, recAsync);
// Wait a little for transmission to occur
if(!rc)
mxfSleep(1000);
return rc;
}
/***************************************************************************************************************/
// ReadAcquisitionData
/***************************************************************************************************************/
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_ASYNCEH_DATAREC *recAsync)
{
MXF_ASYNCEH_DATAREC* rec=recAsync;
uint64 status, msgsCount, bytesCount;
uint64 j, trigTime=0;
uint32 rc;
time_t timeTemp;
struct tm * ptm;
char szTime[1024];
uint64 msec, usec;
uint64 byte;
// 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 = mxfASYNCEHRxAcqRead(rxBuffer, 0, BUFFER_SIZE, &status, &msgsCount, &bytesCount, (MXF_ASYNCEH_DATAREC *)recAsync);
for (j=0; j<msgsCount && !rc; j++)
{
printf(" %02"PRIu64": Timetag=%012"PRIu64", Size=%u\n Data=", j, rec->timeTag, rec->dataSize);
for(byte=0; byte<rec->dataSize; byte++)
printf("%02X", rec->data[byte]);
printf("%s\n", trigTime==rec->timeTag?" *":"");
}
return rc;
}
Updated 10/23/2023