MX Foundation 4
dac_buffer_threshold.c
/************************************************************************************************
//
// File:
// dac_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 transmit a ramp on first DAC channel.
//
// Hardware Requirements:
// - MAXT 500 series carrier with IPM-DAC (jumpers must be set for +/- 10V operation).
//
**************************************************************************************************/
#include "example.h"
#define TXALMOSTFULL 81
#define TXALMOSTEMPTY 20
#define BUFFER_SIZE (TXALMOSTFULL*2*sizeof(MXF_ANALOG_DATAREC))
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param);
uint32 updateMsgs(HMXF_BUFFER buffer, MXF_ANALOG_DATAREC* hostBuffer);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device = 0;
HMXF_MODULE module=0;
HMXF_CHANNEL txChannel = 0;
HMXF_ASYNCEVENT asyncEvent = 0;
HMXF_BUFFER txBuffer = 0;
MXF_ASYNCEVENT_CONDITION TXasyncEventInfo;
uint64 count=0;
MXF_ANALOG_DATAREC* hostBuffer = NULL;
// 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 DAC channel (TX logical #0)
if (!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_ANALOG, 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;
if (!rc)
rc = mxfChannelInfoGet(txChannel, NULL, &module);
// Set time base
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Set the outut range to +/-10v
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_ANALOG_MODULE_DAC_OUTPUT_RANGE, VMXF_ANALOG_MODULE_DAC_OUTPUT_RANGE_10v);
// Alloc host buffer
if (!rc)
{
hostBuffer = (MXF_ANALOG_DATAREC*)malloc(BUFFER_SIZE);
if (!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Set the event handler
if (!rc)
rc = mxfAsyncEventHandlerInit(server, &asyncEventHandler, hostBuffer, &asyncEvent);
// Allocate TX Periodic Update buffer (1MiB)
if (!rc)
rc = mxfTxAperiodicBufferAlloc(txChannel, MXF_TXAPERIODIC_PRIORITY_NORMAL, BUFFER_SIZE, &txBuffer, NULL);
// Set the TX async event condition
if (!rc)
{
memset(&TXasyncEventInfo, 0, sizeof(TXasyncEventInfo));
TXasyncEventInfo.condID = MXF_ASYNCEVENT_COND_TXAPERIODIC_BUFFER_THRESHOLD;
TXasyncEventInfo.condition.txAperiodicBufferThreshold.buffer = txBuffer;
TXasyncEventInfo.condition.txAperiodicBufferThreshold.almostFull = TXALMOSTFULL;
TXasyncEventInfo.condition.txAperiodicBufferThreshold.almostEmpty = TXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &TXasyncEventInfo);
}
// Run for 20 seconds
if (!rc)
{
printf("Running transmission...\n\r");
mxfSleep(20000);
}
// Disable conditions
if (!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &TXasyncEventInfo);
// 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);
}
//Frees all buffers
if (txBuffer)
// Terminate
if (hostBuffer)
free(hostBuffer);
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;
// 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_TXAPERIODIC_BUFFER_THRESHOLD:
// An almost empty condition was detected...
updateMsgs(pendingList[i].condition.txAperiodicBufferThreshold.buffer, (MXF_ANALOG_DATAREC*)param);
break;
default:
printf("Unknown condID 0x%llx)", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Aperiodic Transmission
//****************************************************************************************************************
uint32 updateMsgs(HMXF_BUFFER buffer, MXF_ANALOG_DATAREC* hostBuffer)
{
uint32 rc = 0;
uint32 i;
MXF_ANALOG_DATAREC* recPtr = hostBuffer;
static uint32 TXAsyncEvents = 0;
static uint64 txTime = 0;
// first transmission in 250msec
if(txTime == 0)
{
HMXF_CHANNEL channel;
uint64 priority;
HMXF_DEVICE device=0;
rc = mxfTxAperiodicBufferInfoGet(buffer, &channel, &priority);
if(!rc)
rc = mxfChannelInfoGet(channel, &device, NULL);
if(!rc)
rc = mxfDeviceTimerGet(device, &txTime);
if(!rc)
txTime += 250000;
}
// Refill the FIFO in order to produce a ramp
for (i = 0; !rc && i < TXALMOSTFULL; i++)
{
recPtr->timeTag = txTime;
recPtr->control = 0;
recPtr->repeatCount = 1;
recPtr->reserved = 0;
recPtr->data.value = (float)(-10.0 + (i*0.25)); // -10v to 10v in 0.25v increments
txTime += 100000; // 100msec between each voltage change
rc = mxfAnalogNextDataRecordPtrGet(recPtr, &recPtr);
}
// Add more data to the buffer
if (!rc)
rc = mxfAnalogTxAperiodicWrite(buffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, TXALMOSTFULL, hostBuffer);
if (rc)
printf("Aperiodic Update failed; rc=0x%08x\n", rc);
else
printf("\nAsync Event %d - Writing %d records\n", ++TXAsyncEvents, i);
return rc;
}
Updated 10/23/2023