MX Foundation 4
async_flow_control_sw.c
/*****************************************************************************
//
// File:
// async_flow_control_sw.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 the basic usage of ASYNC enhanced channel class
// for transmission and reception with software flow control (XON/XOFF). Note
// that this examples uses RS-422 instead of the default RS-485.
//
// Hardware requirements:
// - MAXT Flex with ASYNC_EH module
//
*****************************************************************************/
#include "example.h"
#define MAX_TX_RECORDS_TO_TRANSMIT 8
#define LOCAL
#define LOOPBACK
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value)
{
HMXF_DEVICE device;
MXF_DEVICE_INFO deviceInfo;
uint32 rc;
server = server;
deviceIndex = deviceIndex;
if(attrib==KMXF_CHANNEL_CLASS)
{
rc = mxfSystemDeviceGet(server, deviceIndex, &device);
if (!rc)
rc = mxfDeviceInfoGet(device, &deviceInfo);
if (!rc && (deviceInfo.modules[moduleIndex].type == MXF_MODULE_ASYNC_EH))
{
if((channelIndex==1) || (channelIndex==1+deviceInfo.modules[moduleIndex].txCount))
{
*value = MXF_CLASS_HANDSHAKE;
return TRUE;
}
}
}
return FALSE;
}
int main(void)
{
uint32 rc;
HMXF_SERVER server=0;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannels[4];
HMXF_CHANNEL txChannels[4];
HMXF_CHANNEL rxChannel=0;
HMXF_CHANNEL txChannel=0;
HMXF_CHANNEL rxFlowCtrlChannel=0;
HMXF_CHANNEL txFlowCtrlChannel=0;
HMXF_BUFFER rxFlowCtrlBuffer=0;
HMXF_BUFFER rxBuffer=0;
HMXF_BUFFER txBuffer=0;
uint64 moduleCount=0;
uint64 channelCount=0;
size_t txBufferSize=0;
size_t rxBufferSize=0;
MXF_ASYNCEH_DATAREC* rxHostBuffer=0;
MXF_ASYNCEH_DATAREC* txHostBuffer=0;
uint64 rxAcqStatus=0;
uint64 msgCount=0;
uint64 byteCount=0;
uint64 data;
uint64 byte;
// 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
// Set initialisation handler
if(!rc)
// Initialize MX Foundation library
if(!rc)
{
printf("Starting ...\n");
rc = mxfSystemInit(server);
}
// Get the device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Get handle of first ASYNC-EH module
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_ASYNC_EH, 1, &moduleCount, &module);
// If module not found, return an error
if (!rc && !moduleCount)
rc = MAXT_ERROR_NOT_FOUND;
// Get handle of ASYNC-EH RX channels
if (!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_RX_CHANNEL, 4, &channelCount, rxChannels);
// Get handle of ASYNC-EH TX channels
if (!rc && channelCount)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_TX_CHANNEL, 4, &channelCount, txChannels);
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
rxChannel = rxChannels[0];
rxFlowCtrlChannel = rxChannels[1];
txChannel = txChannels[0];
txFlowCtrlChannel = txChannels[1];
// Enable loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_TX_RX_TEST_LB , VMXF_ENABLE);
#endif
// Allocate 10 KB buffer for tx data
if(!rc)
{
txBufferSize = 10*1024;
// Allocate TX Aperiodic static buffer for HIGH priority queue
rc=mxfTxAperiodicBufferAlloc(txChannel, MXF_TXAPERIODIC_PRIORITY_HIGH, txBufferSize, &txBuffer, NULL);
// Host buffer allocation
if(!rc)
{
txHostBuffer = (MXF_ASYNCEH_DATAREC*)calloc(1, txBufferSize);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocate 10 KB buffer for rx data
if(!rc)
{
rxBufferSize = 10*1024;
// Allocate RX data acquisition static buffer
rc=mxfRxAcqBufferAlloc(rxChannel, rxBufferSize, &rxBuffer, NULL);
// Allocate RX control acquisition static buffer
if(!rc)
rc=mxfRxAcqBufferAlloc(rxFlowCtrlChannel, rxBufferSize, &rxFlowCtrlBuffer, NULL);
// Host buffer allocation
if(!rc)
{
rxHostBuffer = (MXF_ASYNCEH_DATAREC*) calloc(1, rxBufferSize);
if(!rxHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Set timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
//Set electrical interface to RS-422
if (!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_ELECTRICAL_INTERFACE, VMXF_ASYNCEH_ELECTRICAL_INTERFACE_RS422);
// Set RX & TX channels speed to 1 mbps
if(!rc)
rc=mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_SPEED, 1000000);
if(!rc)
rc=mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_SPEED, 1000000);
if(!rc)
rc=mxfAttributeUint64Set(rxFlowCtrlChannel, KMXF_ASYNCEH_SPEED, 1000000);
if(!rc)
rc=mxfAttributeUint64Set(txFlowCtrlChannel, KMXF_ASYNCEH_SPEED, 1000000);
// Set TX string gap to 1 bit
if(!rc)
rc=mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_TX_STRING_GAP, 4);
// Set RX string gap to 1/2 bit
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_RX_STRING_GAP, 2);
// Set the flow control rx channel to word mode
if(!rc)
rc=mxfAttributeUint64Set(rxFlowCtrlChannel, KMXF_ASYNCEH_RX_WORD_MODE_ENABLE, VMXF_ENABLE);
// Set RX threshold to 32 words and aging timeout to ~16 msec
if(!rc)
rc=mxfAttributeUint64Set(module, KMXF_ASYNCEH_MODULE_RX_FIFO_AF, 32);
if(!rc)
rc=mxfAttributeUint64Set(module, KMXF_ASYNCEH_MODULE_RX_FIFO_AGING, 16000000);
// Set flow control association for transmission (RX0 transmit XOFF/XON to TX1)
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_RX_FLOWCTRL_TX_INDEX, 1);
// Set flow control association for detection (TX0 detect XOFF/XON from RX1)
if (!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_TX_FLOWCTRL_RX_INDEX, 0);
// Set module XON/XOFF values
if(!rc)
rc=mxfAttributeUint64Set(module, KMXF_ASYNCEH_MODULE_XON_VAL, 0x11);
if(!rc)
rc=mxfAttributeUint64Set(module, KMXF_ASYNCEH_MODULE_XOFF_VAL, 0x13);
// Set module XON/XOFF threshold
if(!rc)
rc=mxfAttributeUint64Set(module, KMXF_ASYNCEH_MODULE_RX_FLOWCTRL_FIFO_AE, 1);
if(!rc)
rc=mxfAttributeUint64Set(module, KMXF_ASYNCEH_MODULE_RX_FLOWCTRL_FIFO_AF, 64);
// Enable flow control detection (TX 0)
if(!rc)
rc=mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_XONXOFF_DETECTION_ENABLE, VMXF_ENABLE);
// Enable flow control transmission (TX 1)
if(!rc)
rc=mxfAttributeUint64Set(txFlowCtrlChannel, KMXF_ASYNCEH_XONXOFF_TRANSMIT_ENABLE, VMXF_ENABLE);
// Enable flow control RX fifo (XON/XOFF will be saved in FIFO)
if(!rc)
rc=mxfAttributeUint64Set(rxFlowCtrlChannel, KMXF_ASYNCEH_XONXOFF_RX_FIFO_ENABLE, VMXF_ENABLE);
// Start acquisition on data & flow control channels
if(!rc)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if(!rc)
rc = mxfRxAcqStart(rxFlowCtrlBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if(!rc)
{
uint64 startTime = 100*1000*1000; // 100 msec
uint64 delay = 1*1000*1000; // 1 msec
// Prepare string array (8 bytes per string with delay of 1 msec between each)
rec = txHostBuffer;
for(data=0; !rc && data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = startTime+(delay*data);
rec->control = 0;
rec->repeatCount = 1;
rec->dataSize = 8;
rec->reserved = 0;
for (byte=0; byte < rec->dataSize; byte++)
rec->data[byte] = (uint8)(0x11*byte);
}
}
if(!rc)
{
printf("Transmitting ...\n");
// Transmit strings on relative record time
rc = mxfASYNCEHTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if(!rc)
mxfSleep(1000);
if(!rc)
{
printf("Receiving ...\n");
// Read rx data buffer
rc = mxfASYNCEHRxAcqRead(rxBuffer, 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxHostBuffer);
if(!rc)
printf("String received count = %"PRIu64" \n", msgCount);
if(!rc)
{
// Display received strings
rec = rxHostBuffer;
for(data=0; data<msgCount && !rc; data++)
{
printf(" %02"PRIu64": Timetag=%012"PRIu64", Size=%u, Data=", data, rec->timeTag, rec->dataSize);
for (byte=0; byte<rec->dataSize; byte++)
printf("%02X", rec->data[byte]);
printf("\n");
}
}
}
if(!rc)
{
// Read rx ctrl buffer
rc = mxfASYNCEHRxAcqRead(rxFlowCtrlBuffer, 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxHostBuffer);
if(!rc)
printf("Flow control messages received count = %"PRIu64" \n", msgCount);
if(!rc)
{
// Display received strings
rec = rxHostBuffer;
for(data=0; data<msgCount && !rc; data++)
{
printf(" %02"PRIu64": Timetag=%012"PRIu64", Size=%u, Data=", data, rec->timeTag, rec->dataSize);
for(byte=0; byte<rec->dataSize; byte++)
printf("%02X", rec->data[byte]);
printf("\n");
}
}
}
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(rxBuffer);
if(!rc)
rc = mxfRxAcqStop(rxFlowCtrlBuffer);
// Free device and host buffers
if(txHostBuffer)
free(txHostBuffer);
if(rxHostBuffer)
free(rxHostBuffer);
if(rc)
{
char errorString[200];
if(mxfSystemErrorStringGet(server, rc, sizeof(errorString), errorString))
sprintf (errorString,"ERROR # 0x%X", rc);
printf("%s\n\r", errorString);
}
printf("Terminating ...\n");
// Unload MX Foundation library
// Disconnect from MX Foundation library
printf("\nPress a key to terminate\n");
getchar();
return rc;
}
Updated 10/23/2023