MX Foundation 4
adc_buffer_threshold.c
/************************************************************************************************
//
// File:
// adc_buffer_threshold.c
//
// Copyright (c) MAX Technologies Inc. 1988-2019, 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 500 series carrier with IPM-ADC (jumpers must be set for +/- 10V operation).
//
**************************************************************************************************/
#include "example.h"
#define RXALMOSTFULL 5
#define RXALMOSTEMPTY 2
#define MAX_ANALOG_CHN_NUM 2
#define MAX_REC 1000
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param);
uint32 readAcquisition(HMXF_BUFFER buffer);
/***************************************************************************************************************/
// 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[MAX_ANALOG_CHN_NUM] = {0};
MXF_ASYNCEVENT_CONDITION RXasyncEventInfo[MAX_ANALOG_CHN_NUM] = {0};
int iPort;
uint64 count=0;
// Connect to services and initialize environment
rc = mxfServerConnect("0.0.0.0", "", "", 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 two ADC channel (RX logical #0 and #1)
if (!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_ANALOG, 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 scan mode to burst continuous
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_ANALOG_MODULE_ADC_SCAN_MODE, VMXF_ANALOG_MODULE_ADC_SCAN_MODE_BURST_CONTINUOUS);
// Set the conversion rate to 10 millisecond (100Hz)
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_ANALOG_MODULE_ADC_CONVERSION_PERIOD, 10*1000*1000);
// Set calibration mode to disable
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_ANALOG_MODULE_ADC_CALIBRATION_MODE, VMXF_ANALOG_MODULE_ADC_CALIBRATION_MODE_DISABLE);
// Set the input range to +/-10v
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_ANALOG_MODULE_ADC_INPUT_RANGE, VMXF_ANALOG_MODULE_ADC_INPUT_RANGE_10v);
// Set the data format to float
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_ANALOG_MODULE_ADC_DATA_FORMAT, VMXF_ANALOG_MODULE_ADC_DATA_FORMAT_FLOAT);
// Set the channel #1 to differential
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_ANALOG_MODULE_ADC_CHN_DIFF_SELECTION, 0x0002);
// Set the channel selection to first two (channel #0 single-ended and channel #1 differential)
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_ANALOG_MODULE_ADC_CHN_SELECTION, 0x00000003);
// Set the event handler
if (!rc)
rc = mxfAsyncEventHandlerInit(server, &asyncEventHandler, NULL, &asyncEvent);
for (iPort = 0; !rc && iPort < MAX_ANALOG_CHN_NUM; iPort++)
{
// Allocate RX acquisition buffer
if (!rc)
rc = mxfRxAcqBufferAlloc(rxChannel[iPort], MAX_REC*sizeof(MXF_ANALOG_DATAREC), &rxBuffer[iPort], NULL);
// Set the RX async event condition
if (!rc)
{
RXasyncEventInfo[iPort].condID = MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD;
RXasyncEventInfo[iPort].condition.rxAcqBufferThreshold.buffer = rxBuffer[iPort];
RXasyncEventInfo[iPort].condition.rxAcqBufferThreshold.almostFull = RXALMOSTFULL;
RXasyncEventInfo[iPort].condition.rxAcqBufferThreshold.almostEmpty = RXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &RXasyncEventInfo[iPort]);
}
// Set acquisition mode
if (!rc)
rc = mxfRxAcqModeSet(rxBuffer[iPort], MXF_RXACQ_MODE_LINEAR);
// Start acquisition
if (!rc)
rc = mxfRxAcqStart(rxBuffer[iPort], MXF_RXACQ_FLAG_DEFAULT, 0, 0);
}
if (!rc)
printf("Acquisition started\n\r");
// Run for 2 seconds
if (!rc)
{
mxfSleep(2000);
}
// Stop acquisition
for (iPort = 0; !rc && iPort < MAX_ANALOG_CHN_NUM; iPort++)
{
rc = mxfRxAcqStop(rxBuffer[iPort]);
// Disable conditions
if (!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &RXasyncEventInfo[iPort]);
//Frees all buffers
if (!rc)
rc = mxfRxAcqClear(rxBuffer[iPort]);
if(!rc)
rc = mxfRxAcqBufferFree(rxBuffer[iPort]);
}
// 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);
break;
default:
printf("Unknown condID 0x%llx)", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Acquisition Reception
//****************************************************************************************************************
uint32 readAcquisition(HMXF_BUFFER buffer)
{
MXF_ANALOG_DATAREC recANALOG[MAX_REC];
MXF_ANALOG_DATAREC* recPtr = recANALOG;
uint64 status, msgsCount, bytesCount;
uint64 j;
uint32 rc;
HMXF_CHANNEL channel=0;
uint64 deviceIndex, moduleIndex, channelIndex=0;
// Read and display records
rc = mxfAnalogRxAcqRead(buffer, 0, sizeof(recANALOG), &status, &msgsCount, &bytesCount, recANALOG);
if (!rc)
rc = mxfRxAcqBufferInfoGet(buffer, &channel);
if(!rc)
rc = mxfChannelLocationGet(channel, &deviceIndex, &moduleIndex, &channelIndex);
if(!rc)
{
printf("Read %llu messages for channel %llu\n\r", msgsCount, channelIndex);
for (j = 0; !rc && j < msgsCount; j++)
{
printf("%02llu: Timetag %llu", j, recPtr->timeTag);
printf("\t %.3f V\n", recPtr->data.value);
rc = mxfAnalogNextDataRecordPtrGet(recPtr, &recPtr);
}
}
if (rc)
printf("Acquisition read failed; rc=0x%08x\n", rc);
return rc;
}
Updated 10/23/2023