MX Foundation 4
mil1553_manyErrorInjections.c
/*****************************************************************************
//
## File:
## mil1553_manyErrorInjections.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 :
// - bit count error
// - override error
// -superseding error
//
// The minor frame for this example is built as follows :
// BC -> RT20 SA3 bit count error injection
// RT20 SA3 -> BC override error injection
// RT10 SA3 -> BC superseding error injection
// BC -> RT10 SA3
//
// The main thing in this example is that we followed the SAE specification for simulating a bus switching
// with an override command followed by a superseding command.
// As you will notice when receiving the data (console display), the 3rd command is then received on bus B.
//
// Important to notice : the override error is on the bc side and we applied it on a RT-> BC command, so the
// BM will receive the 32 words because the data are sent by the RT and not the BC. If you setup an override
// error on a BC-> RT command, you will receive only the number of words decided by your override (if you
// override after 8 words, you will only read 8 words of course).
//
// We check the control to see if the errors are well simulated and display the information on the console
// after each command.
//
// We setup the superseding command to be sent with a GAP of 4 bits after the override command. In 1553, 4
// bits take 4 usec to be transmitted. That is why we check the timetags at the end of the program between
// the 2 commands to see if there is actually a 4 usec difference.
//
// When injecting an error on a RT-> BC command like we do in this example, you have to be careful with the
// buffer handling. The RT data will be set with mxfMIL1553TxPeriodicUpdateMsgWrite() using a rt buffer
// allocated before and the RT error injection will be set with mxfMIL1553TxPeriodicUpdateMsgWrite() using a
// bc buffer also allocated at the beginning.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#include "../../inc/example.h"
#define BITCNT_SHIFT 18 //bit count error of +2 bits
#define RT_ADRS 20
#define RT_SUBADRS 3
#define NB_BC_BUFFERS 4
#define NB_RT_BUFFERS 2
//#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]={0};
HMXF_BUFFER bmBufferRx=0;
HMXF_BUFFER rtBuffer[NB_RT_BUFFERS]={0};
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 timetagArray[NB_BC_BUFFERS], index=0;
memset(timetagArray, 0, sizeof(timetagArray));
// 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]);
#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 error
if(!rc)
{
memset(&err, 0, sizeof(err));
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_OVERRIDE, 0x00, &err.errors[0]);
if (!rc)
rc = mxfMIL1553ErrorInjectionSet(bc, 0, &err);
}
// Configure injection #1, bit count error
if(!rc)
{
memset(&err, 0, sizeof(err));
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_BITCNT, BITCNT_SHIFT, &err.errors[0]);
if (!rc)
rc = mxfMIL1553ErrorInjectionSet(bc, 1, &err);
}
// Configure injection #2, superseding error
if(!rc)
{
memset(&err, 0, sizeof(err));
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_SUPERSEDING, 0, &err.errors[0]);
if (!rc)
{
err.errors[33] = 32; // 1/8 bits so it is equivalent to a 4 bits delay
rc = mxfMIL1553ErrorInjectionSet(bc, 2, &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 20, Subaddress 3, TX, 32 words.
if (!rc)
rc = mxfMIL1553CommandCompose(RT_ADRS, RT_SUBADRS, MXF_MIL1553_MSGTYPE_TX, 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 10, Subaddress 3, TX, 32 words.
if (!rc)
rc = mxfMIL1553CommandCompose(RT_ADRS-10, RT_SUBADRS, MXF_MIL1553_MSGTYPE_TX, 0, &minorFrame[2].command); // specification : 0 means 32 words
minorFrame[2].modulo = 1;
minorFrame[2].buffer = bcBufferTx[3];
minorFrame[2].retryHaltOptions = 0;
minorFrame[2].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_BUS_B;
// Command #3 : Address 10, Subaddress 3, RX, 32 words.
if (!rc)
rc = mxfMIL1553CommandCompose(RT_ADRS-10, RT_SUBADRS, MXF_MIL1553_MSGTYPE_RX, 0, &minorFrame[3].command); // specification : 0 means 32 words
minorFrame[3].modulo = 1;
minorFrame[3].buffer = bcBufferTx[2];
minorFrame[3].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, 4, 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 = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553->service.txPeriodicUpdateMsg.errorIndex = 1; // bit 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[0], 1, txBuffer);
}
// Set RT data for address 20, subaddress 3, TX
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(rtBuffer[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 error
txRec1553->dataSize = 0;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[1], 1, txBuffer);
}
// Set RT data for address 10, subaddress 3, TX
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(rtBuffer[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 = 2; // superseding error
txRec1553->dataSize = 0;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[3], 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[2], 1, txBuffer);
}
// Start messages queues
if(!rc)
{
printf("Starting BC - RT\n\r");
rc = mxfMIL1553RtEnableSet(rt[0], TRUE);
if(!rc)
rc = mxfMIL1553RtEnableSet(rt[1], 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);
timetagArray[rxRec] = 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);
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_RTBC:
printf("RT%llu SA%llu to BC 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);
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");
}
if(!rc)
rc = mxfMIL1553RtEnableSet(rt[0], FALSE);
if(!rc)
rc = mxfMIL1553RtEnableSet(rt[1], FALSE);
//Displays if we have the wanted gap of 4 microseconds between the command with the override and the command with the superseding error
printf("Timetag Diff between RT20 SA3 -> BC and RT10 SA3 -> BC : %llu usec\n", (timetagArray[2]-timetagArray[1])/1000);
// 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 < 1; indexBuffer++)
{
if (bcBufferTx[indexBuffer])
{
rc = mxfTxPeriodicUpdateMsgBufferFree(bcBufferTx[indexBuffer]);
if (rc)
printf("Free buffer failed !\n\r");
}
}
for(indexBuffer = 0; indexBuffer < 1; 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 03/30/2022