MX Foundation 4
mil1553_errors.c
/*****************************************************************************
//
## File:
## mil1553_errors.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 usage of error injection,
// retry functionality and statistics counter with MIL-STD-1553 channel.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti.
//
*****************************************************************************/
#include "example.h"
#define BUF_SA3_RX 0
#define RT_ADRS 20
#define RT_SUBADRS 3
#define RT_ADRS_ERROR 5
#define LOCAL
//#define LOOPBACK
int main(void)
{
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL bc=0;
HMXF_CHANNEL rt=0;
HMXF_BUFFER bcBufferTx[1];
HMXF_BUFFER bcBufferRx=0;
HMXF_BUFFER rtBuffer[1];
uint32 txBufferSize=0;
MXF_MIL1553_DATAREC* txBuffer=NULL;
uint32 rxBufferSize=0;
MXF_MIL1553_DATAREC* rxBuffer=NULL;
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;
uint32 msg;
uint64 deviceCount=0;
uint64 moduleCount=0;
uint64 channelCount=0;
uint32 rc;
// 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 = mxfModuleChannelAllGet(module, MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, 1, &channelCount, &bc);
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
// Get handle of MIL-STD-1553 remote terminal channel
if(!rc)
rc = mxfModuleChannelGet(module, 2+RT_ADRS_ERROR, &rt);
#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(msg=0; msg<1 && !rc; msg++)
{
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, msg, txBufferSize, &bcBufferTx[msg], NULL);
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt, msg, txBufferSize, &rtBuffer[msg], NULL);
if(!rc)
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_RX, 3, bus, rtBuffer[msg]);
}
// 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(bc, rxBufferSize, &bcBufferRx, NULL);
if(!rc)
rc = mxfRxAcqBufferStatusGet(bcBufferRx, NULL, NULL, NULL, &msgCount);
// Host allocation
if(!rc)
{
rxBuffer = (MXF_MIL1553_DATAREC*)malloc(rxBufferSize);
if(!rxBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Enable statistics counter
if(!rc)
// Enable discrete output pulse on bus error from bus A on discrete output pin #0
if(!rc)
rc = mxfChannelDiscreteOutputTriggerEnableSet(bc, MXF_MIL1553_DISCRETE_OUTPUT_TRIG_ON_BUSA_ERROR, TRUE, 0);
// Set timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Configure injection #0, override address
if(!rc)
{
memset(&err, 0, sizeof(err));
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_ADDRESS, RT_ADRS_ERROR, &err.errors[0]);
if (!rc)
rc = mxfMIL1553ErrorInjectionSet(bc, 0, &err);
}
// Configure injection #1, parity error
if(!rc)
{
err.errors[0] = MXF_MIL1553_ERRORID_PAR;
rc = mxfMIL1553ErrorInjectionSet(bc, 1, &err);
}
// Set the minor frame #0 using 1 Command
if(!rc)
{
memset(minorFrame, 0, sizeof(minorFrame));
// Command #0 : Address 20, Subaddress 3, RX, 3 words. Retry on alternate bus when bus error detected
rc = mxfMIL1553CommandCompose(RT_ADRS, RT_SUBADRS, MXF_MIL1553_MSGTYPE_RX, 3, &minorFrame[0].command);
if (!rc)
{
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[BUF_SA3_RX];
minorFrame[0].retryHaltOptions = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_RETRY_ERROR_ENABLE |MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_RETRY_ALT_BUS;
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc, 0, 0, 1, minorFrame, NULL);
}
}
// 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 = 0; // override address
txRec1553->dataSize = 8; //8 bytes (command + 3 words)
txRec1553->data[0] = 0x0000; //Not used
txRec1553->data[1] = 0x0000;
txRec1553->data[2] = 0x1111;
txRec1553->data[3] = 0x2222;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[BUF_SA3_RX], 1, txBuffer);
}
// Set RT data for address 5, 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; // parity error
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[BUF_SA3_RX], 1, txBuffer);
}
// Start messages queues
if(!rc)
{
printf("Starting BC - RT\n\r");
rc = mxfMIL1553RtEnableSet(rt, TRUE);
}
// Start BC acquisition
if(!rc)
rc = mxfRxAcqStart(bcBufferRx, 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(bcBufferRx, 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(bc, 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 Bus%s (0x%04x)\n\r", address, subAddress, wordCount, rxRec1553->control & MXF_MIL1553_RX_REC_CTRL_MSG_BUS_B?"B":"A", rxRec1553->data[0]);
printf("\t\tRT status: 0x%04x\n\r", rxRec1553->data[msgInfo.statusIndex[0]]);
printf("\t\tParity %s\n\r", rxRec1553->control&MXF_MIL1553_RX_REC_CTRL_MSG_PARITY_ERROR?"Error":"OK");
break;
}
}
}
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(rxRec1553, &rxRec1553);
}
mxfSleep(500);
loop++;
} while(loop < 10);
}
// Stop acquisition
if(!rc)
{
rc = mxfRxAcqStop(bcBufferRx);
if (!rc)
rc = mxfRxAcqClear(bcBufferRx);
}
// Stop the major frame
if(!rc)
{
printf("Stopping BC - RT\n\r");
}
if(!rc)
rc = mxfMIL1553RtEnableSet(rt, FALSE);
// Get total number of transactions
if(!rc)
{
memset(&transGlobal, 0, sizeof(transGlobal));
if(!rc)
printf("Total number of transactions: %u\n\r", transGlobal.totalCount);
}
// Get total number of errors
if(!rc)
{
memset(&errorGlobal, 0, sizeof(errorGlobal));
rc = mxfMIL1553StatisticErrorGlobalGet(bc, &errorGlobal);
if(!rc)
printf("Total number of errors: %u\n\r", errorGlobal.totalError);
}
// 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(bcBufferRx)
{
rc = mxfRxAcqBufferFree(bcBufferRx);
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