MX Foundation 4
discrete_fifo.c
/******************************************************************************
//
// File:
// discrete_fifo.c
//
// Copyright (c) MAX Technologies Inc. 1988-2015, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example demonstrates how to read discrete input and to write to
// discrete output. It also demonstrates how to set user led.
//
// Hardware requirements:
// - MAXT Flex or MAXT 500 series carrier with IPM with discrete channel.
//
*******************************************************************************/
#include "example.h"
#define LOCAL
typedef struct
{
MXF_DISCRETE_DATAREC* rxHostBuffer;
MXF_DISCRETE_DATAREC* txHostBuffer;
HMXF_BUFFER bufferTx;
uint64 moduleType;
}
EVENT_INFO;
uint32 eventHandler(HMXF_HANDLE hAsyncEvent, void* pParam);
int main(void)
{
uint32 rc;
uint64 channelCount=0;
HMXF_SERVER server;
HMXF_MODULE module=0;
HMXF_CHANNEL rx=0;
HMXF_CHANNEL tx=0;
HMXF_BUFFER bufferTx=0;
HMXF_BUFFER bufferRx=0;
HMXF_ASYNCEVENT asyncEvent=0;
uint32 txBufferSize;
MXF_DISCRETE_DATAREC* txBuffer=NULL;
uint32 rxBufferSize=0;
MXF_DISCRETE_DATAREC* rxBuffer=NULL;
EVENT_INFO eventInfo;
char errorString[200];
uint64 moduleType=0;
// Connect to services and initialize environment
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
// Initialize MX Foundation library
if(!rc)
{
printf("Starting ...\n");
rc = mxfSystemInit(server);
}
// Get handle of discrete input channel
if(!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_DISCRETE, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, 1, &channelCount, &rx);
// Get handle of discrete output channel
if(!rc && channelCount)
rc = mxfChannelAllGet(server, MXF_CLASS_DISCRETE, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 1, &channelCount, &tx);
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
if(!rc)
rc = mxfChannelInfoGet(rx, NULL, &module);
// configure discrete input
if(!rc)
rc = mxfAttributeUint64Set(module, KMXF_DISCRETE_MODULE_RX_FIFO_AF, 1);
if(!rc)
rc = mxfAttributeUint64Set(rx, KMXF_DISCRETE_RX_EDGE_FALLING, 0xff);
if(!rc)
rc = mxfAttributeUint64Set(rx, KMXF_DISCRETE_RX_EDGE_RISING, 0xff);
if (!rc)
rc = mxfAttributeUint64Get(module, KMXF_MODULE_TYPE, &moduleType);
if (!rc)
{
if (moduleType != MXF_MODULE_DIOFIFO_EH)
{
rc = mxfAttributeUint64Set(rx, KMXF_DISCRETE_DIRECTION, 0x00ff);
}
}
// Allocate buffer for tx data
if(!rc)
{
txBufferSize = sizeof(MXF_DISCRETE_DATAREC);
// Device allocation
rc = mxfTxAperiodicBufferAlloc(tx, MXF_TXAPERIODIC_PRIORITY_LOW, txBufferSize, &bufferTx, NULL);
// Host allocation
if(!rc)
{
txBuffer = (MXF_DISCRETE_DATAREC*)malloc(txBufferSize);
if(!txBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocate 10KB buffer for rx data
if(!rc)
{
rxBufferSize = 10*1024;
// Device allocation
rc = mxfRxAcqBufferAlloc(rx, rxBufferSize, &bufferRx, NULL);
// Host allocation
if(!rc)
{
rxBuffer = (MXF_DISCRETE_DATAREC*)malloc(rxBufferSize);
if(!rxBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Set timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Configure acquisition asynchronous event condition
if(!rc)
{
eventInfo.rxHostBuffer = rxBuffer;
eventInfo.txHostBuffer = txBuffer;
eventInfo.bufferTx = bufferTx;
eventInfo.moduleType = moduleType;
rc = mxfAsyncEventHandlerInit(server, &eventHandler, &eventInfo, &asyncEvent);
}
if(!rc)
{
condition.reserved = 0;
condition.condID = MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &condition);
}
// Start acquisition
if(!rc)
rc = mxfRxAcqStart(bufferRx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// wait 3 seconds
if(!rc)
mxfSleep(3000);
// disable asynchronous event condition
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &condition);
if(!rc)
// Clear aperiodic buffer
if(!rc)
rc = mxfTxAperiodicClear(bufferTx, 0);
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(bufferRx);
// Clear acquisition
if(!rc)
rc = mxfRxAcqClear(bufferRx);
// free buffers
if(txBuffer)
free(txBuffer);
if(rxBuffer)
free(rxBuffer);
if(rc)
{
if(mxfSystemErrorStringGet(server, rc, sizeof(errorString), errorString))
sprintf (errorString,"ERROR # 0x%08X", rc);
printf("%s\n\r", errorString);
}
//Frees all buffers
if(bufferRx)
mxfRxAcqBufferFree(bufferRx);
if(bufferTx)
// Unload MX Foundation library
printf("\nPress a key to terminate\n");
getchar();
return rc;
}
uint32 eventHandler(HMXF_HANDLE hAsyncEvent, void* pParam)
{
EVENT_INFO* eventInfo=(EVENT_INFO*)pParam;
uint32 rc;
uint64 count;
uint64 status, msgCount, byteCount;
HMXF_CHANNEL channel;
HMXF_DEVICE device=0;
// get asynchronous event information
rc = mxfAsyncEventPendingGet(hAsyncEvent, 1, &count, &pendingInfo);
if(!rc && count)
{
if(pendingInfo.condID == MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD)
{
// Read discrete input
rc = mxfDiscreteRxAcqRead(pendingInfo.condition.rxAcqBufferThreshold.buffer, 1, sizeof(MXF_DISCRETE_DATAREC), &status, &msgCount, &byteCount, eventInfo->rxHostBuffer);
if(!rc && msgCount)
{
rxRec = eventInfo->rxHostBuffer;
txRec = eventInfo->txHostBuffer;
memset(txRec, 0, sizeof(MXF_DISCRETE_DATAREC));
txRec->repeatCount = 1;
txRec->data = rxRec->data;
if(eventInfo->moduleType == MXF_MODULE_DIOFIFO_EH)
{
txRec->edge = (rxRec->edge)?rxRec->edge:0xffff;
}
else
{
txRec->edge = 0;
}
// Update discrete output with value of discrete input
rc = mxfDiscreteTxAperiodicWrite(eventInfo->bufferTx, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, 1, txRec);
if(!rc)
{
if (eventInfo->moduleType == MXF_MODULE_DIOFIFO_EH)
{
// ligth first user led when first discrete input pin is low
rc = mxfTxAperiodicBufferInfoGet(eventInfo->bufferTx, &channel, NULL);
if(!rc)
rc = mxfChannelInfoGet(channel, &device, NULL);
if(!rc)
{
printf("Set led #0 state to %s\n", (rxRec->data & 0x01)?"OFF":"ON");
rc = mxfDeviceLedSet(device, 0x01, (rxRec->data & 0x01)?0:1);
}
}
}
}
}
}
if(rc)
printf("event handler rc=0x%08x\n", rc);
return rc;
}
Updated 10/23/2023