MX Foundation 4
hdlc_aperiodic.c
/*******************************************************************************
//
// File:
// hdlc_aperiodic.c
//
// Copyright (c) MAX Technologies Inc. 1988-2015, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This demo shows how to use the different types of aperiodic transmissions
// with HDLC channels.
//
// Hardware Requirements:
// - MAXT FlexMulti or 500 series carrier with IPM-MULTI
// - loopback between TX0 and RX0 and TX4 and RX7 Multi channels.
//
*******************************************************************************/
#include "example.h"
#define LOOPBACK
//#define LOCAL
#define BUFFER_SIZE 15*1024 // 15KB
#define MAX_TX_RECORDS_TO_TRANSMIT 8
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
void DisplayDataArray(uint64 recNum, MXF_HDLC_DATAREC* rec);
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_HDLC_DATAREC* rxHostBuffer);
uint32 StartAperiodicTransmissionDefault(HMXF_BUFFER txBuffer, MXF_HDLC_DATAREC* txHostBuffer);
uint32 StartAperiodicTransmissionAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_HDLC_DATAREC* txHostBuffer);
uint32 StartAperiodicTransmissionRecordAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_HDLC_DATAREC* txHostBuffer);
uint32 StartAperiodicTransmissionRecordRelative(HMXF_BUFFER txBuffer, MXF_HDLC_DATAREC* txHostBuffer);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannel=0;
HMXF_CHANNEL txChannel=0;
HMXF_BUFFER rxBuffer=0;
HMXF_BUFFER txBuffer=0;
size_t txBufferSize=0;
size_t rxBufferSize=0;
MXF_HDLC_DATAREC* rxHostBuffer=0;
MXF_HDLC_DATAREC* txHostBuffer=0;
uint64 dev, mod, port;
// Connect to services local or remote
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
if (rc)
{
printf("Failed to connect; rc=0x%08x", rc);
printf("\nPress a key to terminate\n");
getchar();
return 0;
}
//Configuration of the Multi port in HDLC
if(!rc)
// Initialize the server
printf("\nStarting\n");
if(!rc)
rc = mxfSystemInit(server);
// Gets handle of first HDLC RX channel
if(!rc)
rc = mxfChannelGet(server, MXF_CLASS_HDLC, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, 0, &rxChannel);
// Gets handle of first HDLC TX channel
if (!rc)
rc = mxfChannelGet(server, MXF_CLASS_HDLC, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 0, &txChannel);
if(!rc)
rc = mxfChannelInfoGet(txChannel, &device, &module);
//Set tx interframe time fill
if (!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_TX_INTERFRAME_TIME_FILL, VMXF_HDLC_TX_INTERFRAME_TIME_FILL_NONE);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_INTERNAL_CLOCK_FREQ, 1000000);
//Set tx module FIFO threshold
if (!rc)
{
rc = mxfAttributeUint64Set(module, KMXF_HDLC_MODULE_TX_FIFO_AF, 200);
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_HDLC_MODULE_TX_FIFO_AE, 100);
}
//Enable HDLC
if (!rc)
rc = mxfHDLCChannelEnable(txChannel, VMXF_HDLC_CLOCK_SOURCE_INTERNAL);
//Enable HDLC
if (!rc)
rc = mxfHDLCChannelEnable(rxChannel, VMXF_HDLC_CLOCK_SOURCE_EXTERNAL);
// Enables loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocates buffer for tx data
if(!rc)
{
txBufferSize = BUFFER_SIZE;
// Allocates 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_HDLC_DATAREC*) calloc(1, txBufferSize);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocates buffer for RX data
if(!rc)
{
rxBufferSize = BUFFER_SIZE;
// Allocates RX acquisition static buffer
rc = mxfRxAcqBufferAlloc(rxChannel, rxBufferSize, &rxBuffer,NULL);
// Host buffer allocation
if(!rc)
{
rxHostBuffer = (MXF_HDLC_DATAREC*) calloc(1, rxBufferSize);
if(!rxHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Sets timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Gets the physical port location
if (!rc)
{
rc = mxfChannelLocationGet(rxChannel, &dev, &mod, &port);
if (!rc)
printf("Acquisition Channel (RX) location=%"PRIu64".%"PRIu64".%"PRIu64"\n", dev, mod, port);
}
if (!rc)
{
rc = mxfChannelLocationGet(txChannel, &dev, &mod, &port);
if (!rc)
printf("Transmitter Channel (TX) location=%"PRIu64".%"PRIu64".%"PRIu64"\n", dev, mod, port);
}
// Starts the acquisition process
if (!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
if (!rc)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if(!rc)
mxfSleep(100);
if (!rc)
{
// Starts HDLC data transmission
rc = StartAperiodicTransmissionDefault(txBuffer, txHostBuffer);
if (!rc)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (!rc)
rc = StartAperiodicTransmissionAbsolute(device, txBuffer, txHostBuffer);
if (!rc)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (!rc)
rc = StartAperiodicTransmissionRecordAbsolute(device, txBuffer, txHostBuffer);
if (!rc)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (!rc)
rc = StartAperiodicTransmissionRecordRelative(txBuffer, rxHostBuffer);
if (!rc)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
}
// Stops acquisition
if(!rc)
rc = mxfRxAcqStop(rxBuffer);
//Disable HDLC
if (txChannel)
if (rxChannel)
// Frees device and host buffers
if(txBuffer)
if(rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
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");
// Unloads MX Foundation library
// Disconnects from MX Foundation library
printf("\nPress a key to terminate\n");
getchar();
return rc;
}
/***************************************************************************************************************/
// ReadAcquisitionData
/***************************************************************************************************************/
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_HDLC_DATAREC* rxHostBuffer)
{
uint64 status, msgsCount, bytesCount;
uint32 rc;
// Reads and displays records
printf("Receiving ...\n");
rc = mxfHDLCRxAcqRead(rxBuffer, 0, BUFFER_SIZE, &status, &msgsCount, &bytesCount, rxHostBuffer);
if(!rc)
{
printf("String received count = %"PRIu64" \n", msgsCount);
// Displays received strings
DisplayDataArray(msgsCount, rxHostBuffer);
}
else
printf("Acquisition read failed; rc=0x%08x\n\r", rc);
return rc;
}
/***************************************************************************************************************/
// StartAperiodicTransmissionDefault
/***************************************************************************************************************/
uint32 StartAperiodicTransmissionDefault(HMXF_BUFFER txBuffer, MXF_HDLC_DATAREC* txHostBuffer)
{
uint64 data, word;
uint32 rc=0;
uint16 ramp=0;
// In the example below the record are sent back-to-back 100 ms in the future.
printf("\nAperiodic transmission (Relative Start Time-Default)\n");
rec = txHostBuffer;
//Prepares records to send for basic transmission/reception test
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = 0;
if(data == 0)
rec->control = MXF_HDLC_TX_REC_CTRL_CLOSING_FLAG_NOT_SEND | MXF_HDLC_TX_REC_CTRL_FCS_NOT_SEND | MXF_HDLC_TX_REC_CTRL_FRAMESIZE_NOT_SEND;
else if(data == 1)
rec->control = MXF_HDLC_TX_REC_CTRL_OPENING_FLAG_NOT_SEND;
else
rec->control = 0;
rec->repeatCount = 1;
rec->dataSize = (data)?64:4096;
rec->reserved = 0;
for(word=0; word < rec->dataSize/2; word++, ramp++)
{
rec->data[word] = ramp;
}
rc = mxfHDLCNextDataRecordPtrGet(rec, &rec);
}
if(!rc)
{
printf("\nTransmitting ...\n");
// Transmits strings on relative record time
rc = mxfHDLCTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 100000000, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if(!rc)
mxfSleep(1000);
return rc;
}
/***************************************************************************************************************/
// StartAperiodicTransmissionAbsolute
/***************************************************************************************************************/
uint32 StartAperiodicTransmissionAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_HDLC_DATAREC* txHostBuffer)
{
uint32 rc;
uint32 delay100ms = 100000000;
uint64 currentTime;
uint64 data, word;
// Get the current time
rc = mxfDeviceTimerGet(device, &currentTime);
if (rc)
return rc;
// In the example below the option MXF_TXAPERIODIC_FLAG_ABSOLUTE_START_TIME
// is used. In this case the records are sent freely (no time clock control)
// with a start transmission time based on the device clock + 100ms in the future.
printf("\nAperiodic transmission (Absolute)\n");
if(!rc)
{
rec = txHostBuffer;
//Prepares records to send for basic transmission/reception test
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = 0;
rec->control = 0;
rec->repeatCount = 1;
rec->dataSize = (uint32)(12+data);
rec->reserved = 0;
for(word=0; word < rec->dataSize/2; word++)
{
rec->data[word] = (uint16)(0x1111*data);
}
rc = mxfHDLCNextDataRecordPtrGet(rec, &rec);
}
}
// Transmit the array of records
if(!rc)
{
printf("\nTransmitting ...\n");
rc = mxfHDLCTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_ABSOLUTE_START_TIME, currentTime+delay100ms, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if(!rc)
mxfSleep(1000);
return rc;
}
/***************************************************************************************************************/
// StartAperiodicTransmissionRecordAbsolute
/***************************************************************************************************************/
uint32 StartAperiodicTransmissionRecordAbsolute(HMXF_DEVICE device, HMXF_BUFFER txBuffer, MXF_HDLC_DATAREC* txHostBuffer)
{
uint64 currentTime;
uint32 rc;
uint64 data, word;
// In the example below the timetag in the record is set
// and the option MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME
// In this case the records are sent based on the absolute clock
// timing specified in the timetag field for each records.
printf("\nAperiodic transmission (Absolute with timetag)\n");
// Get the current time
rc = mxfDeviceTimerGet(device, &currentTime);
if (rc)
return rc;
currentTime+=100000000;
// Set the record
// The scheduling timetag is as follow:
// clock current time + 100 ms
// Each record are sent with a timetag of 10 ms interval.
if(!rc)
{
rec = txHostBuffer;
//Prepares recordw to send for basic transmission/reception test
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = currentTime;
rec->control = 0;
rec->repeatCount = 1;
rec->dataSize = 16;
rec->reserved = 0;
for(word=0; word < rec->dataSize/2; word++)
{
rec->data[word] = (uint16)(0x1111*data);
}
currentTime += 10000000;
rc = mxfHDLCNextDataRecordPtrGet(rec, &rec);
}
}
// Transmit the array of records
if(!rc)
{
printf("\nTransmitting ...\n");
rc = mxfHDLCTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if(!rc)
mxfSleep(1000);
return rc;
}
/***************************************************************************************************************/
// StartAperiodicTransmissionRecordRelative
/***************************************************************************************************************/
uint32 StartAperiodicTransmissionRecordRelative(HMXF_BUFFER txBuffer, MXF_HDLC_DATAREC* txHostBuffer)
{
uint64 currentTime;
uint32 rc=0;
uint64 data, word;
// In the example below the timetag in the record is set
// and the option MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME.
// The relative time between each records is set in the
// timetag field of the record array.
printf("\nAperiodic transmission (Record Relative)\n");
// Set the record
// The scheduling timetag is as follow:
// clock current time + 100 ms
// Each record are sent with a timetag a 10000 uS (10 ms) interval.
if(!rc)
{
rec = txHostBuffer;
//Prepares records to send for basic transmission/reception test
for(data=0, currentTime=100000000; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = currentTime;
rec->control = 0;
rec->repeatCount = 1;
rec->dataSize = 32;
rec->reserved = 0;
for(word=0; word < rec->dataSize/2; word++)
{
rec->data[word] = (uint16)(0x1010*data);
}
currentTime += 10000000;
rc = mxfHDLCNextDataRecordPtrGet(rec, &rec);
}
}
// Transmit the array of records
if(!rc)
{
printf("\nTransmitting ...\n");
rc = mxfHDLCTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if(!rc)
mxfSleep(1000);
return rc;
}
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_MULTI_EH) || (deviceInfo.modules[moduleIndex].type == MXF_MODULE_MULTI)))
{
// Sets MULTI first TX and RX channel to HDLC
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
*value = MXF_CLASS_HDLC;
return TRUE;
}
else if ((channelIndex == 4) || (channelIndex == deviceInfo.modules[moduleIndex].txCount+4))
{
*value = MXF_CLASS_CLOCK;
return TRUE;
}
}
}
return FALSE;
}
void DisplayDataArray(uint64 recNum, MXF_HDLC_DATAREC* rec)
{
uint64 iRec,
jRec,
iData,
jData=0,
iMsg=0,
timeTag;
uint32 control,
dataSize=0;
uint8 *pByte;
printf("\n");
for(iRec=0; iRec < recNum; iRec++)
{
if(p->control & MXF_HDLC_RX_REC_CTRL_OPENING_FLAG)
{
timeTag = p->timeTag;
control = p->control;
dataSize = p->dataSize;
if(!(p->control & MXF_HDLC_RX_REC_CTRL_CLOSING_FLAG))
{
p2 = p;
for(jRec=iRec; jRec < recNum; jRec++)
{
control |= p2->control;
dataSize += p2->dataSize;
if(control & MXF_HDLC_RX_REC_CTRL_CLOSING_FLAG)
break;
}
}
printf("%03"PRIu64" %010"PRIu64" 0x%08x %04u ", iMsg++, timeTag, control, dataSize);
}
if((dataSize % 2) == 0)
{
for(iData=0; iData < p->dataSize/2; iData++, jData++)
{
printf("%04x ", p->data[iData]);
if(!((jData+1)%8) && (jData+1 < dataSize/2))
printf("\n ");
}
}
else
{
pByte = (uint8*)p->data;
for(iData=0; iData < p->dataSize-4; iData++, jData++, pByte++)
{
printf("%02x ", *pByte);
if(!((jData+1)%14) && (jData+1 < dataSize))
printf("\n ");
}
printf("%04x ", *((uint16 *)pByte)); // Framesize is 16-bit
pByte += 2;
printf("%04x ", *((uint16 *)pByte)); // FCS is 16-bit
}
if(p->control & MXF_HDLC_RX_REC_CTRL_CLOSING_FLAG)
{
jData = 0;
printf("\n");
}
}
}
Updated 10/23/2023