MX Foundation 4
mil1553_bc_buserror_trigger.c
/*****************************************************************************
//
// File:
// mil1553_bc_buserror_trigger.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 basic usage of Bus Error trigger of
// MIL-STD-1553 bus controller. The discrete input is used to monitor
// the trigger pins. The error injection feature is used on BC data
// to simulate all bus errors.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe (FX1553-4) or FlexMulti 1553 (FM1553-4)
// - FLEX discrete output #0 must be connected to discrete input #0
// - FLEX discrete output #1 must be connected to discrete input #1
//
*****************************************************************************/
#include "example.h"
#define M_ADDRESS 5
#define M_SUBADDR 3
#define LOCAL
//#define LOOPBACK
#define ACQUISITION
int main(void)
{
HMXF_SERVER server;
HMXF_DEVICE device = 0;
HMXF_MODULE mil1553Module = 0;
HMXF_MODULE discreteModule = 0;
HMXF_MODULE module[2];
HMXF_CHANNEL bc = 0;
HMXF_CHANNEL bm = 0;
HMXF_CHANNEL discrete = 0;
HMXF_BUFFER rxDiscreteBuffer = 0;
HMXF_BUFFER txMsg1553Buffer = 0;
MXF_MIL1553_DATAREC* txRec1553;
#ifdef ACQUISITION
HMXF_BUFFER acq1553Buffer = 0;
MXF_MIL1553_DATAREC* rxRec1553;
uint64 rate;
uint64 last = 0;
#else
HMXF_BUFFER samp1553Buffer = 0;
MXF_MIL1553_SAMPREC* sampRec1553;
#endif
MXF_DISCRETE_DATAREC* rxRecDiscrete;
uint32 rc;
uint64 discreteCount = 0;
uint32 txDataSize;
MXF_MIL1553_DATAREC* txData = 0;
uint32 rxDataSize = 0;
void* rxData = 0;
uint64 rxAcqStatus;
uint64 msgCount;
uint64 byteCount;
uint64 rxRec;
uint64 loop = 0, deviceCount = 0, moduleCount = 0;
uint32 data;
char errorString[200];
#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\r");
rc = mxfSystemInit(server);
}
// Initialize MX Foundation library
if (!rc)
rc = mxfSystemInit(server);
//Get a FM1553-4 or FX1553-4 device handle
if (!rc)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_FLEXMULTI_1553, 1, &deviceCount, &device);
if (!rc && !deviceCount)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_FLEX1553_PCIE, 1, &deviceCount, &device);
// Get a MIL1553 module handle
if (!rc && deviceCount)
{
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MIL1553MRT_EH, 2, &moduleCount, module);
//If not a FM1553-4 or FX1553-4 , return an error
if (moduleCount != 2)
{
printf("This example only works for FM1553-4 or FX1553-4\n");
rc = MAXT_ERROR_MODEL;
}
}
//If no device found, return an error
else if (!rc && !deviceCount)
rc = MAXT_ERROR_MODEL;
// Get handle of first module
mil1553Module = module[0];
//Get handle of Discrete module
if (!rc)
{
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_DIOFIFO_EH, 1, &discreteCount, &discreteModule);
if (!rc && !discreteCount)
rc = MAXT_ERROR_NOT_FOUND;
}
// Get handle of first MIL1553 Bus controller channel
if (!rc)
rc = mxfModuleChannelGet(mil1553Module, 1, &bc);
// Get handle of first MIL1553 Bus monitor channel
if (!rc)
rc = mxfModuleChannelGet(mil1553Module, 0, &bm);
// Get handle of discrete input channel
if (!rc)
rc = mxfModuleChannelGet(discreteModule, 1, &discrete);
// Set timebase to RTC nsec
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Configure to EBR
if (!rc)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_INTERFACE, VMXF_MIL1553_INTERFACE_RS485);
#ifdef LOOPBACK
// Enable internal loopback
if (!rc)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
if (!rc)
rc = mxfAttributeUint64Set(discrete, KMXF_DISCRETE_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Configure discrete
if (!rc)
rc = mxfAttributeUint64Set(discrete, KMXF_DISCRETE_RX_PULSE_WIDTH_FILTER, 10);
if (!rc)
rc = mxfAttributeUint64Set(discrete, KMXF_DISCRETE_RX_FIFO_SAMP_PERIOD, 10);
// Enable channel statistics
if (!rc)
if (!rc)
{
// Enable discrete pin #0/1 rising edge detection
rc = mxfAttributeUint64Set(discrete, KMXF_DISCRETE_RX_EDGE_FALLING, 0x0000);
if (!rc)
rc = mxfAttributeUint64Set(discrete, KMXF_DISCRETE_RX_EDGE_RISING, 0x0003);
}
if (!rc)
{
// Enable discrete trigger on bus error of BUS A & B on discrete #0
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSA_ERROR, VMXF_ENABLE, 0);
if (!rc)
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSB_ERROR, VMXF_ENABLE, 0);
}
if (!rc)
{
// Enable discrete trigger on protocol error of BUS A & B on discrete #1
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSA_PROTOCOL_ERROR, VMXF_ENABLE, 1);
if (!rc)
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSB_PROTOCOL_ERROR, VMXF_ENABLE, 1);
}
// Allocate 1KB buffer for tx data
if (!rc)
{
txDataSize = 1024;
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, 0, txDataSize, &txMsg1553Buffer, NULL);
// Host allocation
if (!rc)
{
txData = (MXF_MIL1553_DATAREC*)malloc(txDataSize);
if (!txData)
rc = MAXT_ERROR_MEM;
}
}
rxDataSize = 10 * 1024;
#ifdef ACQUISITION
// Allocate 10KB buffer for 1553 acquisition data
if (!rc)
rc = mxfRxAcqBufferAlloc(bm, rxDataSize, &acq1553Buffer, NULL);
#else
// Allocate 10KB buffer for 1553 sampling data
if (!rc)
rc = mxfRxSamplingBufferAlloc(bm, rxDataSize, &samp1553Buffer, NULL);
if (!rc)
rc = mxfRxSamplingKilltimeSet(samp1553Buffer, 2000000000);
#endif
// Allocate 10KB buffer for discrete rx data
if (!rc)
rc = mxfRxAcqBufferAlloc(discrete, rxDataSize, &rxDiscreteBuffer, NULL);
// Host allocation
if (!rc)
{
rxData = malloc(rxDataSize);
if (!rxData)
rc = MAXT_ERROR_MEM;
}
// Clear the major frame
if (!rc)
// Set the minor frame #0 using 3 Commands
if (!rc)
{
memset(minorFrame, 0, sizeof(minorFrame));
// Command #0 : Address 5, Subaddress 3, RX, 16 words
rc = mxfMIL1553CommandCompose(M_ADDRESS, M_SUBADDR, MXF_MIL1553_COMMAND_DIR_RX, 4, &minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = txMsg1553Buffer;
if (!rc)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc, 0, 0, 1, minorFrame, NULL);
}
txRec1553 = (MXF_MIL1553_DATAREC*)txData;
// Set default data for address 5, subaddress 3
if (!rc)
{
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->dataSize = 10; //10 bytes (command + 4 words)
txRec1553->data[0] = 0x0000; //Not used
txRec1553->data[1] = 0x0000;
txRec1553->data[2] = 0x1111;
txRec1553->data[3] = 0x2222;
txRec1553->data[4] = 0x3333;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(txMsg1553Buffer, 1, txData);
}
// Inject bus error (parity, bit count, manchester, data gap, sync) after 1st transmission
if (!rc)
{
uint32 errorIndex = 0;
memset(&Error, 0, sizeof(MXF_MIL1553_ERROR_INJ));
mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_BITCNT, 17, &Error.errors[0]);//Bit count = 17 bit
mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_PAR, 0, &Error.errors[1]);//Parity error
mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_MANCHBIPHASEDATA, 8, &Error.errors[2]);//Manchester error on bit 8
mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_WORDGAP, 3, &Error.errors[3]);//Data gap of 3 us
mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_MANCHSYNCLEVEL, 0x4, &Error.errors[4]);//SYNC zero crossing + 1/2 bit
rc = mxfMIL1553ErrorInjectionSet(bc, errorIndex, &Error);
if (!rc)
{
txRec1553->control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553->service.txPeriodicUpdateMsg.errorIndex = (uint16)errorIndex;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(txMsg1553Buffer, 1, txData);
}
}
#ifdef ACQUISITION
// Start BM acquisition
if (!rc)
rc = mxfRxAcqStart(acq1553Buffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
#else
// Start BM sampling
if (!rc)
rc = mxfRxSamplingStart(samp1553Buffer);
#endif
// Start Discrete acquisition
if (!rc)
rc = mxfRxAcqStart(rxDiscreteBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Start the major frame with the clock mode (on rising edge)
if (!rc)
rc = mxfMIL1553TxPeriodicMajorFrameStart(bc, 0, 500 * 1000 * 1000, NULL); //500 msec
if (!rc)
mxfSleep(1000);
// Read and display received messages
if (!rc)
{
loop = 0;
do
{
#ifdef ACQUISITION
rc = mxfMIL1553RxAcqRead(acq1553Buffer, 0, rxDataSize, &rxAcqStatus, &msgCount, &byteCount, (MXF_MIL1553_DATAREC*)rxData);
rxRec1553 = (MXF_MIL1553_DATAREC*)rxData;
for (rxRec = 0; !rc && rxRec < msgCount; rxRec++)
{
rate = last ? ((rxRec1553->timeTag - last) / 1000000) : 0;//msec
last = rxRec1553->timeTag;
printf("%012llu ", rxRec1553->timeTag);
printf("%03llu ", rate);
printf("%08X ", rxRec1553->control);
printf("%04u ", (rxRec1553->dataSize / 2) - 1);
for (data = 0; data < min(8, rxRec1553->dataSize / 2); data++)
{
printf("%04X ", rxRec1553->data[data]);
}
printf("\n\r");
// Get next msg
mxfMIL1553NextDataRecordPtrGet(rxRec1553, &rxRec1553);
}
#else
rc = mxfMIL1553RxSamplingRead(samp1553Buffer, MXF_RXSAMPLING_FLAG_DEFAULT, 0, rxDataSize, &msgCount, &byteCount, (MXF_MIL1553_SAMPREC*)rxData);
sampRec1553 = (MXF_MIL1553_SAMPREC*)rxData;
for (rxRec = 0; !rc && rxRec < msgCount; rxRec++)
{
printf("%012llu ", sampRec1553->timeTag);
printf("%03u ", sampRec1553->rate);
printf("%08X ", sampRec1553->control);
printf("%04u ", (sampRec1553->dataSize / 2) - 1);
for (data = 0; data < min(5, sampRec1553->dataSize / 2); data++)
{
printf("%04X ", sampRec1553->data[data]);
}
printf("\n\r");
// Get next msg
mxfMIL1553NextSamplingRecordPtrGet(sampRec1553, &sampRec1553);
}
#endif
// Read DISCRETE acquisition queue
if (!rc)
rc = mxfDiscreteRxAcqRead(rxDiscreteBuffer, 0, rxDataSize, &rxAcqStatus, &msgCount, &byteCount, (MXF_DISCRETE_DATAREC*)rxData);
rxRecDiscrete = (MXF_DISCRETE_DATAREC*)rxData;
for (rxRec = 0; !rc && rxRec < msgCount; rxRec++)
{
if (rxRecDiscrete->edge) //skip initial value
{
printf("DISCRETE -> %012llu ", rxRecDiscrete->timeTag);
printf("%04X ", rxRecDiscrete->edge);
printf("%04X ", rxRecDiscrete->data);
printf("\n\r");
}
mxfDiscreteNextDataRecordPtrGet(rxRecDiscrete, &rxRecDiscrete);
}
mxfSleep(500);
loop++;
} while (loop < 5);
}
if (!rc)
{
//Disable discrete trigger on bus error
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSA_ERROR, VMXF_DISABLE, 0);
if (!rc)
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSB_ERROR, VMXF_DISABLE, 0);
}
if (!rc)
{
//Disable discrete trigger on protocol error
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSA_PROTOCOL_ERROR, VMXF_DISABLE, 0);
if (!rc)
rc = mxfChannelDiscreteOutputTriggerEnableSet(bm, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSB_PROTOCOL_ERROR, VMXF_DISABLE, 0);
}
printf("Stopping\n\r");
// Stop the major frame
#ifdef ACQUISITION
// Stop acquisition
mxfRxAcqStop(acq1553Buffer);
mxfRxAcqClear(acq1553Buffer);
#else
// Stop sampling
mxfRxSamplingStop(samp1553Buffer);
#endif
mxfRxAcqStop(rxDiscreteBuffer);
mxfRxAcqClear(rxDiscreteBuffer);
if (txData)
free(txData);
if (rxData)
free(rxData);
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 (txMsg1553Buffer)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(txMsg1553Buffer);
if (rc)
printf("Free buffer failed !\n\r");
}
#ifdef ACQUISITION
if (acq1553Buffer)
{
rc = mxfRxAcqBufferFree(acq1553Buffer);
if (rc)
printf("Free buffer failed !\n\r");
}
#else
if (samp1553Buffer)
{
rc = mxfRxSamplingBufferFree(samp1553Buffer);
if (rc)
printf("Free buffer failed !\n\r");
}
#endif
if (rxDiscreteBuffer)
{
rc = mxfRxAcqBufferFree(rxDiscreteBuffer);
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