MX Foundation 4
flexadc_sampling.c
/*****************************************************************************
//
// File:
// flexadc_sampling.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 demonstrates the configuration of an FlexADC module and perform
// sampling on 16 differential channels and 16 single-ended channels.
//
// Hardware requirements:
// - MAXT FlexMAX with FlexADC
//
*****************************************************************************/
#include "example.h"
#define MAX_ANALOG_CHN_NUM 40
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChn[MAX_ANALOG_CHN_NUM];
HMXF_BUFFER rxBuffer;
int iPort;
uint64 count=0;
uint64 byteCount;
uint64 timerStart=0, timer=0;
uint64 index;
float value;
// 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 a handle to 40 Analog FlexADC channel
if (!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_FLEXANALOG, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, MAX_ANALOG_CHN_NUM, &count, rxChn);
// If channel not found, return an error
if (!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
if (!rc)
rc = mxfChannelInfoGet(rxChn[0], &device, &module);
// Set time base
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Set the input range of the first 16 to +/-10V single-ended and the following 16 to 10V differential
for(iPort=0; iPort<MAX_ANALOG_CHN_NUM && !rc; iPort++)
{
if(iPort < 16) // single-ended
{
rc = mxfAttributeUint64Set(rxChn[iPort], KMXF_FLEXADC_CHN_INPUT_RANGE, VMXF_FLEXADC_CHN_INPUT_RANGE_SE_BIPOLAR_10V);
}
else // differential
{
// In differential, only positive pin is used to set attribute
if(!(iPort & 0x8))
rc = mxfAttributeUint64Set(rxChn[iPort], KMXF_FLEXADC_CHN_INPUT_RANGE, VMXF_FLEXADC_CHN_INPUT_RANGE_DIFF_BIPOLAR_10V);
}
}
// Set the conversion rate to 1 millisecond
if(!rc)
rc = mxfAttributeUint64Set(module, KMXF_FLEXADC_MODULE_CONVERSION_PERIOD, 1000);
// Set the channel selection to 16 single-ended and 16 differential
if(!rc)
rc = mxfAttributeUint64Set(module, KMXF_FLEXADC_MODULE_CHN_SELECTION, 0x000000FF00FFFFFFULL);
// Allocate RX sampling buffer
if(!rc)
rc = mxfRxSamplingBufferAlloc(module, sizeof(recANALOG), &rxBuffer, NULL);
//Start sampling
if(!rc)
rc = mxfRxSamplingStart(rxBuffer);
if(!rc)
rc = mxfDeviceTimerGet(device, &timerStart);
// Receive loop
if(!rc)
{
do
{
iPort = 0;
index = 0;
mxfSleep(1000);
rc = mxfFlexAnalogRxSamplingRead(rxBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, sizeof(recANALOG), &count, &byteCount, &recANALOG);
if(!rc)
{
do
{
if(recANALOG.dataMask & 0x1) // if channel is enabled converts ADC code to voltage
{
rc = mxfFlexAdcDataConvert(rxChn[iPort], recANALOG.data[index], &value);
if(!rc)
printf("Value of ADC %d is %.3f. V\n\r", iPort, value);
index++;
}
recANALOG.dataMask >>= 1;
iPort++;
}while(!rc && recANALOG.dataMask);
}
if (!rc)
rc = mxfDeviceTimerGet(device, &timer);
}while(!rc && ((timer-timerStart) < 5000000));
}
//Stop acquisition
if(!rc)
rc = mxfRxSamplingStop(rxBuffer);
if (!rc)
rc = mxfRxSamplingBufferFree(rxBuffer);
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;
}
Updated 10/23/2023