MX Foundation 4
mil1553_rtErrorInjections.c
/*****************************************************************************
//
## File:
## mil1553_rtErrorInjections.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 how to inject the following errors :
// - override status error
// - do not respond error
// - respond on error error
//
// The minor frame for this example is built as follows :
// BC -> RT20 SA3 override status error injection (RT side)
// BC -> RT10 SA3 do not respond error injection (RT side)
// BC -> RT30 SA3 word count error injection (BC side) + respond on error injection (RT side)
//
//
// As you can see in the minor frame described above, an override status error is injected on the first
// command. This will rise a MXF_MIL1553_STATUS_MSGERR flag in the status index of the MXF_MIL1553_MSGINFO
// structure obtained with the function mxfMIL1553DataRecordDecompose() in acquisition.
//
// Then, the "do not respond error" is injected in the second command. It is more simple to check this error:
// you just have to look at the record control to see if the MXF_MIL1553_RX_REC_CTRL_MSG_NO_RESPONSE flag is
// risen.
//
// On the third command, it is a bit more tricky. We inject a word count error on the BC side so the RT will detect
// an error and we also inject a "respond on error" error on the RT side. That is why we receive an unexpected message
// after the third command, also displayed on the console screen.
//
// We chose a word count error because this error will not affect the type of the command (will still be MXF_MIL1553_MSGINFO_TYPE_BCRT)
// when for example if you choose a parity error, your command will be seen as MXF_MIL1553_MSGINFO_TYPE_UNEXPECTED.
//
// When an error is injected in a command, in 1553, the RT will not respond and not transmit a status.
// For example in the third command, we inject a word count error, so the RT will not respond. That is
// why you should see on the console screen the message "RT response Error" for this command.
//
// It is normal that the datasize for the third command is 15 instead of 32 words because, as previously said,
// a word count error is injected on the BC side.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#include "example.h"
#define RT_ADRS 20
#define RT_SUBADRS 3
#define NB_BC_BUFFERS 3
#define NB_RT_BUFFERS 3
#define LOCAL
//#define LOOPBACK
int main(void)
{
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL bc=0;
HMXF_CHANNEL bm=0;
HMXF_CHANNEL rt[NB_RT_BUFFERS];
HMXF_BUFFER bcBufferTx[NB_BC_BUFFERS];
HMXF_BUFFER bmBufferRx=0;
HMXF_BUFFER rtBuffer[NB_RT_BUFFERS];
uint32 txBufferSize=0;
MXF_MIL1553_DATAREC* txBuffer=NULL;
uint32 rxBufferSize=0;
MXF_MIL1553_DATAREC* rxBuffer=NULL;
MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG minorFrame[NB_BC_BUFFERS];
char errorString[256];
uint64 bus=MXF_MIL1553_BUS_A|MXF_MIL1553_BUS_B;
uint64 rxAcqStatus;
uint64 msgCount;
uint64 byteCount, indexBuffer;
uint64 rxRec;
uint32 loop=0;
uint64 address, subAddress, dir, wordCount;
uint64 deviceCount=0;
uint64 moduleCount=0;
uint64 channelCount=0;
uint32 rc;
uint64 index=0;
// Connect to MX Foundation library
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", TRUE, &server);
#endif
// Initialize MX Foundation library
if(!rc)
{
printf("Starting ...\n\r");
rc = mxfSystemInit(server);
}
// Get handle of first MIL-STD-1553 Bus controller channel
if(!rc)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_ALL, 1, &deviceCount, &device);
if(!rc && deviceCount)
{
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(!rc && channelCount)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_MIL1553, MXF_SCLASS_BM_CHANNEL, 1, &channelCount, &bm);
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
// Get handle of MIL-STD-1553 remote terminal channels
if(!rc)
rc = mxfModuleChannelGet(module, 2+RT_ADRS, &rt[0]);
if(!rc)
rc = mxfModuleChannelGet(module, 2+(RT_ADRS-10), &rt[1]);
if(!rc)
rc = mxfModuleChannelGet(module, 2+(RT_ADRS+10), &rt[2]);
#ifdef LOOPBACK
// Enable internal loopback
if(!rc)
rc = mxfAttributeUint64Set(bc, KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate 1KB buffer for tx data
if(!rc)
{
txBufferSize = 1024;
// Allocate buffers and enable bus
for (index = 0; index < NB_BC_BUFFERS && !rc; index++)
{
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, index, txBufferSize, &bcBufferTx[index], NULL);
}
for (index = 0; index < NB_RT_BUFFERS && !rc; index++)
{
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt[index], index, txBufferSize, &rtBuffer[index], NULL);
if(!rc)
rc = mxfMIL1553RtSubsystemEnableSet(rt[index], MXF_MIL1553_MSGTYPE_RX, 3, bus, rtBuffer[index]);
if(!rc)
rc = mxfMIL1553RtSubsystemEnableSet(rt[index], MXF_MIL1553_MSGTYPE_TX, 3, bus, rtBuffer[index]);
}
// 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);
if(!rc)
rc = mxfRxAcqBufferStatusGet(bmBufferRx, NULL, NULL, NULL, &msgCount);
// Host allocation
if(!rc)
{
rxBuffer = (MXF_MIL1553_DATAREC*)malloc(rxBufferSize);
if(!rxBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Set timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Configure injection #0, override status error on RT20
if(!rc)
{
memset(&err, 0, sizeof(err));
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_STATUS_OV, 0, &err.errors[0]);
if (!rc)
{
err.errors[33] = MXF_MIL1553_STATUS_MSGERR; //rises the message error bit
rc = mxfMIL1553ErrorInjectionSet(rt[0], 0, &err);
}
}
// Configure injection #1, do not respond error on RT10
if(!rc)
{
memset(&err, 0, sizeof(err));
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_DONOTRESPOND, 0, &err.errors[0]);
if (!rc)
rc = mxfMIL1553ErrorInjectionSet(rt[1], 1, &err);
}
// Configure injection #2, respond on error error on RT30
if(!rc)
{
memset(&err, 0, sizeof(err));
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_RESPONDONERROR, 0, &err.errors[0]);
if (!rc)
rc = mxfMIL1553ErrorInjectionSet(rt[2], 2, &err);
}
// Configure injection #3, word count error
if(!rc)
{
memset(&err, 0, sizeof(err));
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_WORDCNT, 16, &err.errors[0]);
rc = mxfMIL1553ErrorInjectionSet(bc, 3, &err);
}
// Set the minor frame #0 using 2 Commands
if(!rc)
{
memset(minorFrame, 0, sizeof(minorFrame));
// Command #0 : Address 20, Subaddress 3, RX, 32 words.
rc = mxfMIL1553CommandCompose(RT_ADRS, RT_SUBADRS, MXF_MIL1553_MSGTYPE_RX, 0, &minorFrame[0].command); // specification : 0 means 32 words
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[0];
minorFrame[0].retryHaltOptions = 0;
// Command #1 : Address 10, Subaddress 3, RX, 32 words.
if (!rc)
rc = mxfMIL1553CommandCompose(RT_ADRS-10, RT_SUBADRS, MXF_MIL1553_MSGTYPE_RX, 0, &minorFrame[1].command); // specification : 0 means 32 words
minorFrame[1].modulo = 1;
minorFrame[1].buffer = bcBufferTx[1];
minorFrame[1].retryHaltOptions = 0;
// Command #2 : Address 30, Subaddress 3, RX, 32 words.
if (!rc)
rc = mxfMIL1553CommandCompose(RT_ADRS+10, RT_SUBADRS, MXF_MIL1553_MSGTYPE_RX, 0, &minorFrame[2].command); // specification : 0 means 32 words
minorFrame[2].modulo = 1;
minorFrame[2].buffer = bcBufferTx[2];
minorFrame[2].retryHaltOptions = 0;
if (!rc)
{
memset( &propertiesMinorFrame, 0, sizeof(propertiesMinorFrame));
propertiesMinorFrame.repeatCount = 1;
propertiesMinorFrame.modulo = 1;
propertiesMinorFrame.options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MINOR_PROPERTIES_OPT_BRANCH_END;
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc, 0, 0, 3, minorFrame, &propertiesMinorFrame);
}
}
// Set BC data for address 20, subaddress 3, RX
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->control = 0;
txRec1553->dataSize = 66; //66 bytes (command + 32 words)
txRec1553->data[0] = 0x0000; //Not used
for(index = 1; index < txRec1553->dataSize/2; index++)
txRec1553->data[index] = 0x0101*(uint16)index;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[0], 1, txBuffer);
}
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553->service.txPeriodicUpdateMsg.errorIndex = 0; // override status error
txRec1553->dataSize = 0;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[0], 1, txBuffer);
}
// Set BC data for address 10, subaddress 3, RX
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->control = 0;
txRec1553->dataSize = 66; //66 bytes (command + 32 words)
txRec1553->data[0] = 0x0000; //Not used
for(index = 1; index < txRec1553->dataSize/2; index++)
txRec1553->data[index] = 0x0101*(uint16)index;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[1], 1, txBuffer);
}
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553->service.txPeriodicUpdateMsg.errorIndex = 1; // do not respond error
txRec1553->dataSize = 0;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[1], 1, txBuffer);
}
// Set BC data for address 30, subaddress 3, RX
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553->service.txPeriodicUpdateMsg.errorIndex = 3; // word count error
txRec1553->dataSize = 66; //66 bytes (command + 32 words)
txRec1553->data[0] = 0x0000; //Not used
for(index = 1; index < txRec1553->dataSize/2; index++)
txRec1553->data[index] = 0x0101*(uint16)index;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[2], 1, txBuffer);
}
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553->service.txPeriodicUpdateMsg.errorIndex = 2; // respond on error error
txRec1553->dataSize = 0;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[2], 1, txBuffer);
}
// Start messages queues
printf("Starting BC - RT\n\r");
for (index = 0; index < NB_RT_BUFFERS && !rc; index++)
{
rc = mxfMIL1553RtEnableSet(rt[index], TRUE);
}
// Start BM acquisition
if(!rc)
rc = mxfRxAcqStart(bmBufferRx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Start the major frame with 250 msec rate
if(!rc)
rc = mxfTxPeriodicMajorFrameStart(bc, 0, 250000000, NULL);
// Read and display received messages
if(!rc)
{
do
{
rc = mxfMIL1553RxAcqRead(bmBufferRx, 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxBuffer);
if (rc)
printf("mxfMIL1553RxAcqRead() error; rc=0x%08x\n\r", rc);
rxRec1553 = (MXF_MIL1553_DATAREC*)rxBuffer;
for(rxRec=0; rxRec<msgCount && !rc; rxRec++)
{
rc = mxfMIL1553DataRecordDecompose(bm, 1, rxRec1553, &msgInfo);
if(!rc)
{
rc = mxfMIL1553CommandDecompose(rxRec1553->data[0], &address, &subAddress, &dir, &wordCount);
if(!rc)
{
printf("\n\r%llu:\t", rxRec1553->timeTag);
switch(msgInfo.msgType)
{
case MXF_MIL1553_MSGINFO_TYPE_BCRT:
printf("BC to RT%llu SA%llu WC%llu DataSize : %u Bus%s (0x%04x) Control : 0x%08x\n\r", address, subAddress, wordCount, (rxRec1553->dataSize/2)-2, rxRec1553->control & MXF_MIL1553_RX_REC_CTRL_MSG_BUS_B?"B":"A", rxRec1553->data[0], rxRec1553->control);
if (!(rxRec1553->control&MXF_MIL1553_RX_REC_CTRL_MSG_NO_RESPONSE))
{
printf("\t\tRT status: Override %s\n\r", (rxRec1553->data[msgInfo.statusIndex[0]]&MXF_MIL1553_STATUS_MSGERR)?"Injected":"Not injected");
printf("\t\tRT response OK\n\r");
}
else
printf("\t\tRT response Error\n\r");
printf("\t\tParity %s\n\r", rxRec1553->control&MXF_MIL1553_RX_REC_CTRL_MSG_PARITY_ERROR?"Error":"OK");
printf("\t\tData length %s\n\r", rxRec1553->control&MXF_MIL1553_RX_REC_CTRL_MSG_DATA_LENGTH_ERROR?"Error":"OK");
printf("\t\tBit Count %s\n\r", rxRec1553->control & MXF_MIL1553_RX_REC_CTRL_MSG_EXTRA_BIT_ERROR ? "Error" : "OK");
break;
case MXF_MIL1553_MSGINFO_TYPE_UNEXPECTED:
printf("BC to RT%llu SA%llu (unexpected command) Bus%s (0x%04x) Control : 0x%08x\n\r", address, subAddress, rxRec1553->control & MXF_MIL1553_RX_REC_CTRL_MSG_BUS_B?"B":"A", rxRec1553->data[0], rxRec1553->control);
if (rxRec1553->control&MXF_MIL1553_RX_REC_CTRL_MSG_NO_RESPONSE)
{
printf("\t\tRT response Error\n\r");
}
printf("\t\tParity %s\n\r", rxRec1553->control&MXF_MIL1553_RX_REC_CTRL_MSG_PARITY_ERROR?"Error":"OK");
printf("\t\tData length %s\n\r", rxRec1553->control&MXF_MIL1553_RX_REC_CTRL_MSG_DATA_LENGTH_ERROR?"Error":"OK");
printf("\t\tBit Count %s\n\r", rxRec1553->control & MXF_MIL1553_RX_REC_CTRL_MSG_EXTRA_BIT_ERROR ? "Error" : "OK");
break;
}
}
}
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(rxRec1553, &rxRec1553);
}
mxfSleep(500);
loop++;
} while(loop < 10);
}
// Stop acquisition
if(!rc)
{
rc = mxfRxAcqStop(bmBufferRx);
if (!rc)
rc = mxfRxAcqClear(bmBufferRx);
}
// Stop the major frame
if(!rc)
{
printf("\nStopping BC - RT\n\r");
}
for (index = 0; index < NB_RT_BUFFERS && !rc; index++)
{
rc = mxfMIL1553RtEnableSet(rt[index], FALSE);
}
// Free buffers
if(txBuffer)
free(txBuffer);
if(rxBuffer)
free(rxBuffer);
// Display any previous errors
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
for(indexBuffer = 0; indexBuffer < NB_BC_BUFFERS; indexBuffer++)
{
if (bcBufferTx[indexBuffer])
{
rc = mxfTxPeriodicUpdateMsgBufferFree(bcBufferTx[indexBuffer]);
if (rc)
printf("Free buffer failed !\n\r");
}
}
for(indexBuffer = 0; indexBuffer < NB_RT_BUFFERS; indexBuffer++)
{
if (rtBuffer[indexBuffer])
{
rc = mxfTxPeriodicUpdateMsgBufferFree(rtBuffer[indexBuffer]);
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;
}
Updated 10/23/2023