MX Foundation 4
ar708_buffer_threshold.c
/************************************************************************************************
//
// File:
// ar708_buffer_threshold.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 illustrates how to manage buffer thresholds with asynchronous events.
// The demo will transmit and record the messages using asynchronous
// events.
//
// Hardware Requirements:
// - MAXT FlexMulti-1553 or FlexMulti-629 with A708 option.
// - Loopback between TX and RX channels if internal loopback is not used.
//
**************************************************************************************************/
#include "example.h"
//#define LOOPBACK
//#define LOCAL
#define DURATION_ms 6000
#define TXALMOSTFULL 100
#define TXALMOSTEMPTY 30
#define RXALMOSTFULL 10
#define RXALMOSTEMPTY 5
#define SCANFIELD 0X7FF8
#define LABELFIELD 0X00FF
#define CTRLACCEPFIELD 0X0300
#define MODEFIELD 0X1C00
#define TILTFIELD1 0XE000
#define TILTFIELD2 0X000F
#define RANGEFIELD 0XFC00
#define GAINFIELD 0X03F0
#define DATAACCEPTFIELD 0X00006
#define BINFIELD 0X00007
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void *param);
uint32 updateMsgs(HMXF_BUFFER buffer, MXF_A708_DATAREC *txHostBuffer);
uint32 readAcquisition(HMXF_BUFFER rxBuffer, MXF_A708_DATAREC *rxHostBuffer);
void DisplayDataArray(uint64 recNum, MXF_A708_DATAREC* rec);
void setScanAngle(MXF_A708_DATAREC *rec, double angle);
void setLabel(MXF_A708_DATAREC *rec, uint8 label);
void setCtrlAccept(MXF_A708_DATAREC *rec, uint16 ctrlAccept);
void setMode(MXF_A708_DATAREC *rec, uint16 mode);
void setRange(MXF_A708_DATAREC *rec, uint16 range);
void setGain(MXF_A708_DATAREC *rec, int16 gain);
void setDataAccept(MXF_A708_DATAREC *rec, uint16 dataAccept);
void setBin(MXF_A708_DATAREC *rec, uint16 bin, uint16 val);
void setTilt(MXF_A708_DATAREC *rec, double tilt);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannel=0, txChannel=0;
HMXF_ASYNCEVENT asyncEvent=0;
HMXF_BUFFER rxBuffer=0, txBuffer=0;
MXF_A708_DATAREC* rxHostBuffer=0;
MXF_A708_DATAREC* txHostBuffer=0;
MXF_ASYNCEVENT_CONDITION RXasyncEventInfo, TXasyncEventInfo;
uint64 moduleCount=0, channelCount;
size_t txBufferSize, rxBufferSize;
HMXF_SCHED schedule=0;
HMXF_SCHED_MSG msgSched;
// Connects 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
// Initializes MX Foundation library
if (!rc)
{
printf("Starting ...\n");
rc = mxfSystemInit(server);
}
// Gets the device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Gets handle of first ARINC 708 module
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A708_EH, 1, &moduleCount, &module);
// If module not found, return an error
if(!rc && !moduleCount)
rc = MAXT_ERROR_NOT_FOUND;
// Activates the ARINC 708 module (because the MIL-STD-1553 is by default activated, except on FM1553-2)
if (!rc)
{
rc = mxfAttributeUint64Set(module, KMXF_A708_MODULE_ACTIVE, TRUE);
if (rc == MAXT_ERROR_NOT_SUPPORTED) // Attribute not supported on FM1553-2, simply ignore
rc = MAXT_SUCCESS;
}
// Gets handle of first ARINC 708 RX channel
if(!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A708, MXF_SCLASS_RX_CHANNEL, 1, &channelCount, &rxChannel);
// Gets handle of first ARINC 708 TX channel
if (!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A708, MXF_SCLASS_TX_CHANNEL, 1, &channelCount, &txChannel);
// Enables loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_A708_TX_RX_TEST_LB , VMXF_ENABLE);
#endif
// Allocates buffer for tx data
if(!rc)
{
txBufferSize = 720*sizeof(MXF_A708_DATAREC);
// Allocate TX Periodic Update buffer
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, 0, txBufferSize, &txBuffer, NULL);
// Host buffer allocation
if(!rc)
{
txHostBuffer = (MXF_A708_DATAREC*)calloc(1, txBufferSize);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocates buffer for RX data
if(!rc)
{
rxBufferSize = 720*sizeof(MXF_A708_DATAREC);
// Allocates RX acquisition static buffer
rc=mxfRxAcqBufferAlloc(rxChannel, rxBufferSize, &rxBuffer,NULL);
// Host buffer allocation
if(!rc)
{
rxHostBuffer = (MXF_A708_DATAREC*)calloc(1, rxBufferSize);
if(!rxHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Sets timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Sets the event handler
if(!rc)
rc = mxfAsyncEventHandlerInit(server, asyncEventHandler, txHostBuffer, &asyncEvent);
// Sets the RX async event condition
if(!rc)
{
memset(&RXasyncEventInfo, 0, sizeof(RXasyncEventInfo));
RXasyncEventInfo.condID = MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD;
RXasyncEventInfo.condition.rxAcqBufferThreshold.buffer = rxBuffer;
RXasyncEventInfo.condition.rxAcqBufferThreshold.almostFull = RXALMOSTFULL;
RXasyncEventInfo.condition.rxAcqBufferThreshold.almostEmpty = RXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &RXasyncEventInfo);
}
// Sets acquisition mode
if(!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Starts acquisition
if(!rc)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if(!rc)
printf("Acquisition started\n\r");
// Sets the TX async event condition
if(!rc)
{
memset(&TXasyncEventInfo, 0, sizeof(TXasyncEventInfo));
TXasyncEventInfo.condID = MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD;
TXasyncEventInfo.condition.txPeriodicUpdateMsgBufferThreshold.almostFull = TXALMOSTFULL;
TXasyncEventInfo.condition.txPeriodicUpdateMsgBufferThreshold.almostEmpty = TXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &TXasyncEventInfo);
}
// Select message for async event condition
if(!rc)
rc = mxfAsyncEventTxPeriodicUpdateMsgSelectSet(asyncEvent, txChannel, MXF_MSG_SELECT_ONLY, 1, &txBuffer);
// Set the Periodic Scheduler
if(!rc)
rc = mxfTxPeriodicScheduleNew(txChannel, &schedule);
// Set scheduling values: Rate=5 ms, Phase=0 us
if(!rc)
rc = mxfTxPeriodicScheduleSingleMsgAdd(schedule, 5000000, 0, txBuffer, &msgSched);
// Run the schedule
if(!rc)
rc = mxfTxPeriodicScheduleRun(schedule);
// Waits for transmission and reading to occur
if(!rc)
{
mxfSleep(DURATION_ms);
rc = mxfTxPeriodicScheduleFree(schedule);
}
// Stops acquisition
if(!rc)
rc = mxfRxAcqStop(rxBuffer);
// Disable conditions
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &RXasyncEventInfo);
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &TXasyncEventInfo);
// Terminates async event handler
if(!rc)
// Catches 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);
}
// Terminates
// Frees device and host buffers
if (rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer)
if(txHostBuffer)
free(txHostBuffer);
if(rxHostBuffer)
free(rxHostBuffer);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
//****************************************************************************************************************
// Asynchronous Event Handler
//****************************************************************************************************************
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, pvoid param)
{
uint64 maxCount=64, pendingCount;
uint64 i;
uint32 rc;
// Gets 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_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD:
// An almost empty condition was detected...
updateMsgs(pendingList[i].condition.txPeriodicUpdateMsgBufferThreshold.buffer, (MXF_A708_DATAREC *)param);
break;
case MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
readAcquisition(pendingList[i].condition.rxAcqBufferThreshold.buffer, (MXF_A708_DATAREC *)param);
break;
default:
printf("Unknown condID 0x%llx)", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Aperiodic Transmission
//****************************************************************************************************************
uint32 updateMsgs(HMXF_BUFFER buffer, MXF_A708_DATAREC *txHostBuffer)
{
uint32 rc=0;
uint16 i, bin;
MXF_A708_DATAREC *recPtr=txHostBuffer;
static uint32 TXAsyncEvents=0;
static uint16 scanAngle=0;
static uint16 binValue=0;
// Refills the FIFO
for(i=0; !rc && i<TXALMOSTFULL; i++, scanAngle++)
{
if(scanAngle > 720)
{
scanAngle = 0;
binValue++;
}
recPtr->timeTag=0;
recPtr->control=0;
recPtr->dataSize = 200; //200 bytes corresponds to 1600 bits (see ARINC 708 specification)
recPtr->repeatCount=1;
recPtr->manchesterBitErr = 0;
recPtr->data[0] = 0;
recPtr->data[1] = 0;
recPtr->data[2] = 0;
recPtr->data[3] = 0;
setLabel(recPtr, 055); // Bits 0-7 : Label
setCtrlAccept(recPtr, 0x3); // Bits 8-9 : Cntrol Accept
setMode(recPtr, 1); // Mode Set 1
setTilt(recPtr, 0);
setGain(recPtr, -16);
setRange(recPtr, 80);
setDataAccept(recPtr, 3);
setScanAngle(recPtr, ((double)scanAngle*0.25)+270);
for(bin=1; bin<=512; bin++)
{
setBin(recPtr, bin, binValue);
}
if(!rc)
rc = mxfA708NextDataRecordPtrGet(recPtr, &recPtr);
}
// Add more data to the buffer
if(!rc)
rc = mxfA708TxPeriodicUpdateMsgWrite(buffer, TXALMOSTFULL, txHostBuffer);
//if(!rc)
//DisplayDataArray(TXALMOSTFULL, txHostBuffer);
if(rc)
printf("Periodic Update failed; rc=0x%08x\n", rc);
else
printf("\nAsync Event %d - Writing %d records\n", ++TXAsyncEvents, i);
return rc;
}
/***************************************************************************************************************/
// RX708ReadAcquisitionData
/***************************************************************************************************************/
uint32 readAcquisition(HMXF_BUFFER rxBuffer, MXF_A708_DATAREC *rxHostBuffer)
{
uint64 status, msgsCount, bytesCount;
uint32 rc;
size_t bufferSize = (RXALMOSTFULL*sizeof(MXF_A708_DATAREC));
// Reads and display records
rc = mxfA708RxAcqRead(rxBuffer, 0, bufferSize, &status, &msgsCount, &bytesCount, rxHostBuffer);
if(!rc)
printf("String received count = %llu \n", msgsCount);
// Displays received strings
//if(!rc)
//DisplayDataArray(msgsCount, rxHostBuffer);
if(rc)
printf("Acquisition read failed; rc=0x%08x\n", rc);
return rc;
}
void DisplayDataArray(uint64 recNum, MXF_A708_DATAREC* rec)
{
uint64 iRec,
iData;
printf("\n");
for(iRec=0; iRec < recNum; iRec++)
{
printf("%03llu %010llu 0x%08x %02u ", iRec, p->timeTag, p->control, p->dataSize);
for(iData=0; iData < p->dataSize/2; iData++)
{
printf("%04x ", p->data[iData]);
if(!((iData+1)%8) && (iData+1 < p->dataSize/2))
printf("\n ");
}
printf("\n");
}
}
void setLabel(MXF_A708_DATAREC *rec, uint8 label)
{
rec->data[0] = (rec->data[0] & ~LABELFIELD) | label;
}
void setCtrlAccept(MXF_A708_DATAREC *rec, uint16 ctrlAccept)
{
rec->data[0] = ((rec->data[0] & ~CTRLACCEPFIELD) | (CTRLACCEPFIELD & (ctrlAccept<<8)));
}
void setMode(MXF_A708_DATAREC *rec, uint16 mode)
{
rec->data[1] = ((rec->data[1] & ~MODEFIELD) | (MODEFIELD & (mode<<10)));
}
void setTilt(MXF_A708_DATAREC *rec, double tilt)
{
rec->data[1] = ((rec->data[1] & ~TILTFIELD1) | (TILTFIELD1 & ((uint16)(tilt/0.25)<<13)));
rec->data[2] = ((rec->data[2] & ~TILTFIELD2) | (TILTFIELD2 & ((uint16)(tilt/0.25)>>3)));
}
void setGain(MXF_A708_DATAREC *rec, int16 gain)
{
rec->data[2] = (( rec->data[2] & ~GAINFIELD) | (GAINFIELD & ((gain/-1)<<4)));
}
void setRange(MXF_A708_DATAREC *rec, uint16 range)
{
rec->data[2] = ((rec->data[2] & ~RANGEFIELD) | (RANGEFIELD & ((range/5)<<10)));
}
void setDataAccept(MXF_A708_DATAREC *rec, uint16 dataAccept)
{
rec->data[3] = ((rec->data[3] & ~DATAACCEPTFIELD) | (DATAACCEPTFIELD & (dataAccept<<1)));
}
void setScanAngle(MXF_A708_DATAREC *rec, double angle)
{
rec->data[3] = (( rec->data[3] & ~SCANFIELD) | (SCANFIELD & ((uint16)(angle/0.0879)<<3)));
}
void setBin(MXF_A708_DATAREC *rec, uint16 bin, uint16 val)
{
uint16 word_index = ((bin - 1)*3 + 64) / 16;
uint16 startbit = ((bin - 1)*3) % 16;
uint16 bin_field;
if (startbit < 14)
{
bin_field = BINFIELD << (startbit);
rec->data[word_index] = (uint16)((rec->data[word_index] & ~bin_field) | (bin_field & (val << startbit)));
}
if (startbit >= 14)
{
bin_field = BINFIELD << startbit;
rec->data[word_index] = ((rec->data[word_index] & ~bin_field) | (bin_field & (val << startbit)));
bin_field = BINFIELD >> (16-startbit);
rec->data[word_index + 1] = ((rec->data[word_index + 1] & ~bin_field) | (bin_field & (val >> (16-startbit))));
}
}
Updated 10/23/2023