MX Foundation 4
flexadc_buffer_threshold.c
/************************************************************************************************
//
// File:
// flexadc_buffer_threshold.c
//
// Copyright (c) MAX Technologies Inc. 1988-2022, 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 record the messages using asynchronous events.
//
// Hardware Requirements:
// - MAXT FlexMAX with FlexADC.
//
**************************************************************************************************/
#include "example.h"
#define RXALMOSTFULL 5
#define RXALMOSTEMPTY 2
#define MAX_ANALOG_CHN_NUM 64
#define MAX_REC 1000
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param);
uint32 readAcquisition(HMXF_BUFFER buffer, void* param);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device = 0;
HMXF_MODULE module = 0;
HMXF_CHANNEL rxChannel[MAX_ANALOG_CHN_NUM]={0};
HMXF_ASYNCEVENT asyncEvent = 0;
HMXF_BUFFER rxBuffer=0;
MXF_ASYNCEVENT_CONDITION RXasyncEventInfo={0};
int iPort;
uint64 count=0;
// Connect to services and initialize environment
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
if (rc != MAXT_SUCCESS)
{
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);
// Get the device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Get the first 64 FlexADC channels
if (!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_FLEXANALOG, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, MAX_ANALOG_CHN_NUM, &count, rxChannel);
// If channel not found, return an error
if (!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
if (!rc)
rc = mxfChannelInfoGet(rxChannel[0], NULL, &module);
// Set timebase to 64-bit nanoseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Set the input range to 20V differential for all
for(iPort=0; iPort<MAX_ANALOG_CHN_NUM && !rc; iPort++)
{
// For differential, only positive pin is used to set the attribute
if(iPort & 0x8)
continue;
rc = mxfAttributeUint64Set(rxChannel[iPort], KMXF_FLEXADC_CHN_INPUT_RANGE, VMXF_FLEXADC_CHN_INPUT_RANGE_DIFF_BIPOLAR_20V);
}
// Set the conversion rate to 10 millisecond (100Hz)
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_FLEXADC_MODULE_CONVERSION_PERIOD, 10*1000*1000);
// Set the channel selection to all. For differential, only the positive pin is used to select
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_FLEXADC_MODULE_CHN_SELECTION, 0x00FF00FF00FF00FFULL);
// Set the event handler
if (!rc)
rc = mxfAsyncEventHandlerInit(server, &asyncEventHandler, rxChannel, &asyncEvent);
// Allocate RX acquisition buffer
if (!rc)
rc = mxfRxAcqBufferAlloc(module, MAX_REC*sizeof(MXF_FLEXANALOG_DATAREC), &rxBuffer, NULL);
// Set the RX async event condition
if (!rc)
{
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");
// Run for 2 seconds
if (!rc)
{
mxfSleep(2000);
}
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(rxBuffer);
// Disable conditions
if (!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &RXasyncEventInfo);
//Frees all buffers
if (!rc)
rc = mxfRxAcqClear(rxBuffer);
if(!rc)
rc = mxfRxAcqBufferFree(rxBuffer);
// 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
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;
param=param;
// 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_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
readAcquisition(pendingList[i].condition.rxAcqBufferThreshold.buffer, param);
break;
default:
printf("Unknown condID 0x%llx)", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Acquisition Reception
//****************************************************************************************************************
uint32 readAcquisition(HMXF_BUFFER buffer, void* param)
{
MXF_FLEXANALOG_DATAREC recANALOG[MAX_REC];
MXF_FLEXANALOG_DATAREC* recPtr = recANALOG;
uint64 status, msgsCount, bytesCount;
uint64 j;
uint32 rc;
HMXF_CHANNEL* rxChn=(HMXF_CHANNEL*)param;
float value;
uint64 index;
int iPort;
// Read and display records
rc = mxfFlexAnalogRxAcqRead(buffer, 0, sizeof(recANALOG), &status, &msgsCount, &bytesCount, recANALOG);
if(!rc)
{
printf("Read %llu messages\n\r", msgsCount);
for (j = 0; !rc && j < msgsCount; j++)
{
iPort = 0;
index = 0;
printf("%02llu: Timetag %llu\n", j, recPtr->timeTag);
do
{
// Convert ADC code to voltage
if(recPtr->dataMask & 0x1)
{
rc = mxfFlexAdcDataConvert(rxChn[iPort], recPtr->data[index], &value);
if(!rc)
printf("%d: %.3f V\n", iPort, value);
index++;
}
recPtr->dataMask >>= 1;
iPort++;
}while(!rc && recPtr->dataMask);
rc = mxfFlexAnalogNextDataRecordPtrGet(recPtr, &recPtr);
}
}
if (rc)
printf("Acquisition read failed; rc=0x%08x\n", rc);
return rc;
}
Updated 10/23/2023