MX Foundation 4
mil1553_aperiodic_event.c
/*****************************************************************************
//
## File:
## mil1553_aperiodic_event.c
//
// Copyright (c) MAX Technologies Inc. 1988-2016, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example demonstrates the usage of aperiodic transmission asynchronous
// event to transmit a major frame with MIL-STD-1553 BC channel and record using
// BM string mode.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#include "example.h"
#define FRAME_RATE 20000000 //20 msec
#define ALMOST_FULL 20
#define ALMOST_EMPTY 10
#define LOCAL
//#define LOOPBACK
uint64 startTime=0;
uint32 txHandler(HMXF_ASYNCEVENT asyncEvent, void* param);
int main(void)
{
uint32 rc;
uint64 deviceCount;
uint64 moduleCount=0;
uint64 channelCount=0;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL bc=0;
HMXF_CHANNEL bm=0;
HMXF_BUFFER bcBufferTx=0;
HMXF_BUFFER bmBufferRx=0;
HMXF_ASYNCEVENT asyncEvent=0;
uint32 txBufferSize;
MXF_MIL1553_DATAREC* txBuffer=NULL;
uint32 rxBufferSize=0;
MXF_MIL1553_DATAREC* rxBuffer=NULL;
uint64 card, mod, port;
uint64 rxAcqStatus;
uint64 msgCount;
uint64 byteCount;
uint64 rxRec;
uint32 loop=0;
uint32 data;
char errorString[200];
// 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 first Flex MIL-STD-1553 Bus controller channel
if(!rc)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_ALL, 1, &deviceCount, &device);
// Try with MXF_MODULE_MIL1553MRT_EH and if not found, try with MXF_MODULE_MIL1553MRT
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MIL1553MRT_EH, 1, &moduleCount, &module);
if (!rc && !moduleCount)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MIL1553MRT, 1, &moduleCount, &module);
if(!rc && moduleCount)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, 1, &channelCount, &bc);
// If module or channel not found, return an error
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
// Get handle of first Flex MIL-STD-1553 bus monitor channel
if(!rc)
rc = mxfModuleChannelGet(module, 0, &bm);
// Show channel locations for BM/BC
rc = mxfChannelLocationGet(bm, &card, &mod, &port);
if (!rc)
printf("BM location is Device:%llu Module:%llu Port:%llu\n", card, mod, port);
rc = mxfChannelLocationGet(bc, &card, &mod, &port);
if (!rc)
printf("BC location is Device:%llu Module:%llu Port:%llu\n", card, mod, port);
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate enough buffer for at least ALMOST_FULL tx data
if(!rc)
{
txBufferSize = ALMOST_FULL * sizeof(MXF_MIL1553_DATAREC);
// Device allocation
rc = mxfTxAperiodicBufferAlloc(bc, MXF_TXAPERIODIC_PRIORITY_LOW, txBufferSize, &bcBufferTx, NULL);
// Host allocation
if(!rc)
{
txBuffer = (MXF_MIL1553_DATAREC*)malloc(txBufferSize);
if(!txBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocate 10KB buffer for rx data
if(!rc)
{
rxBufferSize = 10*1024;
// Device allocation
rc = mxfRxAcqBufferAlloc(bm, rxBufferSize, &bmBufferRx, NULL);
// Host allocation
if(!rc)
{
rxBuffer = (MXF_MIL1553_DATAREC*)malloc(rxBufferSize);
if(!rxBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Configure to BM to string mode
if(!rc)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_BM_MODE, VMXF_MIL1553_BM_MODE_STRING);
// Set timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Get current time and add 100 msec to it
if(!rc)
rc = mxfDeviceTimerGet(device, &startTime);
startTime += 100000000;
// Configure aperiodic asynchronous event condition
if(!rc)
rc = mxfAsyncEventHandlerInit(server, &txHandler, txBuffer, &asyncEvent);
if(!rc)
{
condition.reserved = 0;
condition.condID = MXF_ASYNCEVENT_COND_TXAPERIODIC_BUFFER_THRESHOLD;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &condition);
}
// Start BM acquisition
if(!rc)
rc = mxfRxAcqStart(bmBufferRx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Read and display received messages for 5 seconds
if(!rc)
{
do
{
rc = mxfMIL1553RxAcqRead(bmBufferRx, 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxBuffer);
rxRec1553 = (MXF_MIL1553_DATAREC*)rxBuffer;
for(rxRec=0; rxRec<msgCount && !rc; rxRec++)
{
printf("\n\r%llu:\t", rxRec1553->timeTag);
printf("Control: 0x%08x", rxRec1553->control);
for(data=0; data<rxRec1553->dataSize/2; data++)
{
if(data && !(data%5))
printf("\n\r\t\t\t");
printf(" 0x%04x", rxRec1553->data[data]);
}
printf("\n\r");
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(rxRec1553, &rxRec1553);
}
mxfSleep(500);
loop++;
} while(loop < 10);
}
// disable asynchronous event condition
if(!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &condition);
if(!rc)
// Clear aperiodic buffer
if(!rc)
rc = mxfTxAperiodicClear(bcBufferTx, 0);
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(bmBufferRx);
// Clear acquisition
if(!rc)
rc = mxfRxAcqClear(bmBufferRx);
// 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);
}
// Free all buffers and terminate
if(bcBufferTx)
{
rc = mxfTxAperiodicBufferFree(bcBufferTx);
if (rc)
printf("Free buffer failed !\n\r");
}
if(bmBufferRx)
{
rc = mxfRxAcqBufferFree(bmBufferRx);
if (rc)
printf("Free buffer failed !\n\r");
}
// Unload MX Foundation library
printf("\n\rPress enter to terminate\n\r");
getchar();
return 0;
}
uint32 txHandler(HMXF_ASYNCEVENT 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_TXAPERIODIC_BUFFER_THRESHOLD)
{
uint64 cmd;
// Send commands until it reaches almost full threshold
txRec1553 = (MXF_MIL1553_DATAREC *)param;
for(cmd=0; cmd<pendingInfo.condition.txAperiodicBufferThreshold.almostFull-1; cmd++)
{
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
// send two commands in simulated major frame
if(cmd % 2)
{
txRec1553->timeTag = startTime + 1000000; // 1 msec after other command
txRec1553->repeatCount = 1;
txRec1553->dataSize = 10; //10 bytes (command + 4 words)
mxfMIL1553CommandCompose(0, 3, MXF_MIL1553_MSGTYPE_RX, 4, &txRec1553->data[0]);
txRec1553->data[1] = 0x0000;
txRec1553->data[2] = 0x1111;
txRec1553->data[3] = 0x2222;
txRec1553->data[4] = 0x3333;
}
else
{
startTime += FRAME_RATE; // one FRAME_RATE after the first command
txRec1553->timeTag = startTime;
txRec1553->repeatCount = 1;
txRec1553->dataSize = 10; //10 bytes (command + 4 words)
mxfMIL1553CommandCompose(0, 5, MXF_MIL1553_MSGTYPE_RX, 4, &txRec1553->data[0]);
txRec1553->data[1] = 0x3333;
txRec1553->data[2] = 0x2222;
txRec1553->data[3] = 0x1111;
txRec1553->data[4] = 0x0000;
}
mxfMIL1553NextDataRecordPtrGet(txRec1553, &txRec1553);
}
// Send commands
rc = mxfMIL1553TxAperiodicWrite(pendingInfo.condition.txAperiodicBufferThreshold.buffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, cmd, (MXF_MIL1553_DATAREC *)param);
}
}
return rc;
}
Updated 10/23/2023