MX Foundation 4
flexdio_event_handler.c
/******************************************************************************
//
// File:
// flexdio_event_handler.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 how to read discrete input from event handler
// and to write to discrete output.
//
// Hardware requirements:
// - MAXT FlexMAX with FlexMAX DIO module.
// - loopback between first 32 pins and last 32 pins
//
*******************************************************************************/
#include "example.h"
//#define LOCAL
typedef struct
{
uint64 rxBufferSize;
MXF_FLEXDIO_DATAREC* rxHostBuffer;
FILE* fp;
}
EVENT_INFO;
uint32 eventHandler(HMXF_HANDLE asyncEvent, void* param);
uint32 ReadDiscreteInput(HMXF_BUFFER bufferRx, EVENT_INFO* eventInfo);
int main(void)
{
uint32 rc;
uint64 channelCount=0;
uint64 msgCount;
HMXF_SERVER server;
HMXF_DEVICE device;
HMXF_CHANNEL io=0;
HMXF_BUFFER bufferRx=0;
HMXF_BUFFER bufferTx=0;
HMXF_ASYNCEVENT asyncEvent=0;
EVENT_INFO eventInfo;
char errorString[200];
uint32 edge=0xffffffff;
uint64 pin;
// 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 channel
if(!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_FLEXDIO, MXF_SCLASS_ALL, MXF_MODULE_FLEXDIO, 1, &channelCount, &io);
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
if(!rc)
rc = mxfChannelInfoGet(io, &device, NULL);
// Configure first 32 pins as output low-side and next 32 pins as input with pull-up
for (pin = 0; pin<64 && !rc; pin++)
{
if (pin>=32)
rc = mxfFlexDIOModeSet(io, pin, MXF_FLEXDIO_MODE_IN_PULLUP);
else
rc = mxfFlexDIOModeSet(io, pin, MXF_FLEXDIO_MODE_OUT_LS);
}
// configure discrete input
if(!rc)
rc = mxfAttributeUint64Set(io, KMXF_FLEXDIO_RX_EDGE_FALLING, 0xffffffff00000000ULL);
if(!rc)
rc = mxfAttributeUint64Set(io, KMXF_FLEXDIO_RX_EDGE_RISING, 0xffffffff00000000ULL);
if(!rc)
rc = mxfAttributeUint64Set(io, KMXF_FLEXDIO_RX_FIFO_MODE, VMXF_FLEXDIO_RX_FIFO_MODE_EDGE);
memset(&eventInfo, 0, sizeof(eventInfo));
// Allocate 10KB buffer for rx data
if(!rc)
{
eventInfo.rxBufferSize = 10*1024;
// Device allocation
rc = mxfRxAcqBufferAlloc(io, eventInfo.rxBufferSize, &bufferRx, NULL);
// Host allocation
if(!rc)
{
eventInfo.rxHostBuffer = (MXF_FLEXDIO_DATAREC*)malloc((size_t)eventInfo.rxBufferSize);
if(!eventInfo.rxHostBuffer)
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.fp = fopen("discrete_log.txt", "w+t");
rc = mxfAsyncEventHandlerInit(server, &eventHandler, &eventInfo, &asyncEvent);
}
if(!rc)
{
condition.reserved = 0;
condition.condID = MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD;
condition.condition.rxAcqBufferThreshold.buffer = bufferRx;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &condition);
}
// Start acquisition
if(!rc)
rc = mxfRxAcqStart(bufferRx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if(!rc)
printf("Acquisition started\n");
if(!rc)
{
uint32 pulseCount;
uint64 timer=0;
memset(dioRec, 0, sizeof(dioRec));
// Allocate 1KB buffer for discrete tx data
rc = mxfTxAperiodicBufferAlloc(io, MXF_TXAPERIODIC_PRIORITY_NORMAL, 1024, &bufferTx, NULL);
if(!rc)
rc = mxfDeviceTimerGet(device, &timer);
if(!rc)
{
// Fill the queue with; (2*100ms requests) => 10 records/secs => 300 records for the next 30 secs (max)
for(pulseCount=0; !rc && pulseCount<149-1; pulseCount++)
{
timer += (uint64)100*1000*1000; // 100 msec
dioRec[0].repeatCount= 1;
dioRec[0].control= 0;
dioRec[0].timeTag = timer;
dioRec[0].edge = 0x00000000ffffffffULL;
dioRec[0].data = 0x00000000ffffffffULL;
timer += 100*1000*1000; // +100 msec
dioRec[1].repeatCount= 1;
dioRec[1].control= 0;
dioRec[1].timeTag = timer;
dioRec[1].edge = 0x00000000ffffffffULL;
dioRec[1].data = 0;
rc = mxfFlexDIOTxAperiodicWrite(bufferTx, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, 2, dioRec);
if (rc)
{
printf("mxfFlexDIOTxAperiodicWrite() error; rc=0x%08x, pulse count=%u\n", rc, pulseCount);
break;
}
do
{
rc = mxfTxAperiodicBufferStatusGet(bufferTx, &msgCount, NULL, NULL);
} while (!rc && (msgCount != 0));
}
}
}
// Wait 3 seconds
if(!rc)
mxfSleep(3000);
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(bufferRx);
// Disable asynchronous event condition
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &condition);
// Clear the queue and terminate
if (!rc)
rc = mxfAsyncEventPendingClear(asyncEvent);
if(!rc)
// Clear what not sent yet (do not wait that queue is empty).
if(!rc)
mxfTxAperiodicClear(bufferTx, MXF_TXAPERIODIC_CLEAR_OPT_HWFIFO);
// Read last messages
if(!rc)
rc = ReadDiscreteInput(bufferRx, &eventInfo);
printf("\n");
// Close log file
if(eventInfo.fp)
fclose(eventInfo.fp);
// free buffers
if(eventInfo.rxHostBuffer)
free(eventInfo.rxHostBuffer);
if(rc)
{
if(mxfSystemErrorStringGet(server, rc, sizeof(errorString), errorString))
sprintf (errorString,"ERROR # 0x%08X", rc);
printf("%s\n\r", errorString);
}
// Free all buffers and terminate
if (bufferRx)
{
rc = mxfRxAcqBufferFree(bufferRx);
if (rc)
printf("Free buffer failed !\n\r");
}
if (bufferTx)
{
rc = mxfTxAperiodicBufferFree(bufferTx);
if (rc)
printf("Free buffer failed !\n\r");
}
// Unload MX Foundation library
printf("\nPress a key to terminate\n");
getchar();
return rc;
}
uint32 eventHandler(HMXF_HANDLE asyncEvent, void* param)
{
uint32 rc;
uint64 count;
// get asynchronous event information
rc = mxfAsyncEventPendingGet(asyncEvent, 1, &count, &pendingInfo);
if(!rc && count)
{
if(pendingInfo.condID == MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD)
{
// Read discrete input
rc = ReadDiscreteInput(pendingInfo.condition.rxAcqBufferThreshold.buffer, (EVENT_INFO*)param);
printf("+");
}
}
if(rc)
printf("event handler rc=0x%08x\n", rc);
return rc;
}
uint32 ReadDiscreteInput(HMXF_BUFFER bufferRx, EVENT_INFO* eventInfo)
{
uint32 rc;
uint64 status, msgCount, byteCount, msg;
rc = mxfFlexDIORxAcqRead(bufferRx, 0, eventInfo->rxBufferSize, &status, &msgCount, &byteCount, eventInfo->rxHostBuffer);
rxRec = eventInfo->rxHostBuffer;
for (msg=0; msg<msgCount && !rc; msg++)
{
fprintf(eventInfo->fp, "%"PRIu64": 0x%016"PRIx64"\n\r", rxRec->timeTag, rxRec->data);
fflush(eventInfo->fp);
rc = mxfFlexDIONextDataRecordPtrGet(rxRec, &rxRec);
}
return rc;
}
Updated 10/23/2023