MX Foundation 4
mil1553_cond_branch_1.c
/*****************************************************************************
//
## File:
## mil1553_cond_branch_1.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 message conditional branch option
// with MIL-STD-1553 BC channel.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti.
//
*****************************************************************************/
#include "example.h"
#define BUF_SA3_RX 0
#define BUF_SA3_TX 1
#define BUF_SA5_RX 2
#define BUF_SA5_TX 3
#define RT_ADRS 5
#define LOCAL
//#define LOOPBACK
int main(void)
{
uint32 rc;
uint64 deviceCount=0;
uint64 moduleCount=0;
uint64 channelCount=0;
HMXF_SERVER server=0;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL bc=0;
HMXF_CHANNEL rt=0;
HMXF_BUFFER bcBufferTx[4];
HMXF_BUFFER bcBufferRx=0;
HMXF_BUFFER rtBuffer[4];
MXF_MSGID_MIL1553 bcMsgID[4]={{MXF_MIL1553_MSGTYPE_RX,RT_ADRS,3,0},{MXF_MIL1553_MSGTYPE_TX,RT_ADRS,3,0},{MXF_MIL1553_MSGTYPE_RX,RT_ADRS,5,0},{MXF_MIL1553_MSGTYPE_TX,RT_ADRS,5,0}};
MXF_MSGID_MIL1553 rtMsgID[4]={{MXF_MIL1553_MSGTYPE_RX,0,3,0},{MXF_MIL1553_MSGTYPE_TX,0,3,0},{MXF_MIL1553_MSGTYPE_RX,0,5,0},{MXF_MIL1553_MSGTYPE_TX,0,5,0}};
uint32 txBufferSize=0;
MXF_MIL1553_DATAREC* txBuffer=NULL;
uint32 rxBufferSize=0;
MXF_MIL1553_DATAREC* rxBuffer=NULL;
uint64 bus=MXF_MIL1553_BUS_A|MXF_MIL1553_BUS_B;
uint64 rxAcqStatus=0;
uint64 msgCount=0;
uint64 byteCount=0;
uint64 rxRec;
uint32 loop=0;
uint64 address, subAddress, dir, wordCount, indexBuffer;
uint32 data;
uint32 msg;
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);
}
// Get handle of first Flex 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 not found, return an error
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
// Get handle of the 6th Flex MIL-STD-1553 remote terminal channel
if(!rc)
rc = mxfModuleChannelGet(module, 2+RT_ADRS, &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;
// Flex allocation
for(msg=0; msg<4 && !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, rtMsgID[msg].type, rtMsgID[msg].subAddress, 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;
// Flex allocation
rc = mxfRxAcqBufferAlloc(bc, rxBufferSize, &bcBufferRx, NULL);
// 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
if(!rc)
{
memset(&err, 0, sizeof(err));
err.errors[33] = (RT_ADRS<<11)+1;
rc = mxfMIL1553ErrorInjectionCompose(MXF_MIL1553_ERRORID_STATUS_OV, 0, &err.errors[0]);
if(!rc)
rc = mxfMIL1553ErrorInjectionSet(bc, 0, &err);
}
// Set the minor frame #0 using 2 Commands
if(!rc)
{
memset(minorFrame, 0, sizeof(minorFrame));
memset(&mfProp, 0, sizeof(mfProp));
mfProp.modulo = 1;
mfProp.repeatCount = 1;
mfProp.options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MINOR_PROPERTIES_OPT_BRANCH_GOTO;
mfProp.branchIndex = 0;
// Command #0 : Address 5, Subaddress 3, RX, 2 words, Modulo 2
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA3_RX].address, bcMsgID[BUF_SA3_RX].subAddress, bcMsgID[BUF_SA3_RX].type, 2, &minorFrame[0].command);
minorFrame[0].modulo = 2;
minorFrame[0].buffer = bcBufferTx[BUF_SA3_RX];
// Command #1 : Address 5, Subaddress 3, TX, 2 words, delay of 1 msec from preceding command or start of minor frame, conditional branch to minor frame 1 if status is not equal to 0x2800
if(!rc)
{
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA3_TX].address, bcMsgID[BUF_SA3_TX].subAddress, bcMsgID[BUF_SA3_TX].type, 2, &minorFrame[1].command);
if(!rc)
{
minorFrame[1].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_DELAY;
minorFrame[1].modulo = 1;
minorFrame[1].buffer = bcBufferTx[BUF_SA3_TX];
minorFrame[1].delay = 8000; // 1 msec in 1/8 of bit time
minorFrame[1].branchMinorIndex = 1;
minorFrame[1].branchMsgIndex = 0xFFFF;
minorFrame[1].condMask = 0xFFFF;
minorFrame[1].condData = (uint16)(bcMsgID[BUF_SA3_TX].address<<11);
rc = mxfMIL1553ConditionalBranchCompose(MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_CONDBRANCH_OPT_NOTEQUAL, 0, &minorFrame[1].condBranch);
}
if(!rc)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc, 0, 0, 2, minorFrame, &mfProp);
}
}
// Set the minor frame #1 using 2 Commands
if(!rc)
{
memset(minorFrame, 0, sizeof(minorFrame));
// Command #0 : Address 5, Subaddress 5, RX, 3 words
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA5_RX].address, bcMsgID[BUF_SA5_RX].subAddress, bcMsgID[BUF_SA5_RX].type, 3, &minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[BUF_SA5_RX];
// Command #1 : Address 5, Subaddress 5, TX, 3 words, delay of 1 msec after end of preceding command, conditional branch to message 1 of minor frame 0 if status is equal to 0x2800
if(!rc)
{
rc = mxfMIL1553CommandCompose(bcMsgID[BUF_SA5_TX].address, bcMsgID[BUF_SA5_TX].subAddress, bcMsgID[BUF_SA5_TX].type, 3, &minorFrame[1].command);
if(!rc)
{
minorFrame[1].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_GAP;
minorFrame[1].modulo = 1;
minorFrame[1].buffer = bcBufferTx[BUF_SA5_TX];
minorFrame[1].delay = 8000; // 1 msec in 1/8 of bit time
minorFrame[1].branchMinorIndex = 0;
minorFrame[1].branchMsgIndex = 1;
minorFrame[1].condMask = 0xFFFF;
minorFrame[1].condData = (uint16)(bcMsgID[BUF_SA3_TX].address<<11);
rc = mxfMIL1553ConditionalBranchCompose(MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_CONDBRANCH_OPT_EQUAL, 0, &minorFrame[1].condBranch);
}
}
if(!rc)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc, 0, 1, 2, minorFrame, NULL);
}
// Set BC data for address 5, subaddress 3, RX
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 2; // repeat count of 2
txRec1553->dataSize = 6; //6 bytes (command + 2 words)
txRec1553->data[0] = 0x0000; //Not used
txRec1553->data[1] = 0x0000;
txRec1553->data[2] = 0x1111;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[BUF_SA3_RX], 1, txBuffer);
}
// Set BC data for address 5, subaddress 5, RX
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->dataSize = 8; //8 bytes (command + 3 words)
txRec1553->data[0] = 0x0000; //Not used
txRec1553->data[1] = 0xFFFF;
txRec1553->data[2] = 0xEEEE;
txRec1553->data[3] = 0xDDDD;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBufferTx[BUF_SA5_RX], 1, txBuffer);
}
// Set RT data for address 5, subaddress 3, TX, Send 5 times first one, then 5 times second one that has an override status error
// then send last one with no error
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 5;
txRec1553->dataSize = 6; //6 bytes (command + 2 words)
txRec1553->data[0] = 0x0000; //Not used
txRec1553->data[1] = 0x1234;
txRec1553->data[2] = 0x5678;
rc = mxfMIL1553NextDataRecordPtrGet(txRec1553, &txRec1553);
if(!rc)
{
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->dataSize = 6; //6 bytes (command + 2 words)
txRec1553->repeatCount = 5;
txRec1553->control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553->service.txPeriodicUpdateMsg.errorIndex = 0; // override status
txRec1553->data[0] = 0x0000; //Not used
txRec1553->data[1] = 0x9ABC;
txRec1553->data[2] = 0xDEF0;
}
if(!rc)
rc = mxfMIL1553NextDataRecordPtrGet(txRec1553, &txRec1553);
if(!rc)
{
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->dataSize = 6; //6 bytes (command + 2 words)
txRec1553->repeatCount = 1;
txRec1553->control = 0;
txRec1553->data[0] = 0x0000; //Not used
txRec1553->data[1] = 0x1234;
txRec1553->data[2] = 0x5678;
}
if(!rc)
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[BUF_SA3_TX], 3, txBuffer);
}
// Set RT data for address 5, subaddress 5, TX
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->dataSize = 8; //8 bytes (command + 3 words)
txRec1553->data[0] = 0x0000; //Not used
txRec1553->data[1] = 0x4444;
txRec1553->data[2] = 0x5555;
txRec1553->data[3] = 0x6666;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rtBuffer[BUF_SA5_TX], 1, txBuffer);
}
// Start messages queues
if(!rc)
{
printf("Starting BC and 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);
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 (0x%04x)\n\r", address, subAddress, wordCount, rxRec1553->data[0]);
printf("\t\tRT status: 0x%04x\n\r", rxRec1553->data[msgInfo.statusIndex[0]]);
break;
case MXF_MIL1553_MSGINFO_TYPE_RTBC:
printf("RT%llu SA%llu WC%llu to BC (0x%04x)\n\r", address, subAddress, wordCount, rxRec1553->data[0]);
printf("\t\tRT status: 0x%04x\n\r", rxRec1553->data[msgInfo.statusIndex[0]]);
printf("\t\tRT data:");
for(data=0; data<msgInfo.dataWordCount; data++)
{
if(data && !(data%4))
printf("\n\r\t\t\t");
printf(" 0x%04x", rxRec1553->data[msgInfo.dataIndex + data]);
}
printf("\n\r");
break;
case MXF_MIL1553_MSGINFO_TYPE_MODECODE_TXDATA:
printf("BC Mode Command %llu to RT%llu SA%llu (0x%04x)\n\r", wordCount, address, subAddress, rxRec1553->data[0]);
if(msgInfo.statusIndex[0] != 0xffff)
{
printf("\t\tRT status: 0x%04x\n\r", rxRec1553->data[msgInfo.statusIndex[0]]);
printf("\t\tRT data: 0x%04x\n\r", rxRec1553->data[msgInfo.dataIndex]);
}
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)
// Stop and Clear message queues
if(!rc)
{
printf("Stopping BC and RT\n\r");
rc = mxfMIL1553RtEnableSet(rt, FALSE);
for(msg=0; msg<4 && !rc; msg++)
{
rc = mxfTxPeriodicUpdateMsgClear(bcBufferTx[msg], MXF_TXPERIODIC_UPDATEMSG_CLEAR_OPT_DEFAULT);
if(!rc)
rc = mxfTxPeriodicUpdateMsgClear(rtBuffer[msg], MXF_TXPERIODIC_UPDATEMSG_CLEAR_OPT_DEFAULT);
}
}
printf("Terminating\n");
// 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);
}
// When all services are terminated, there is no pending Tx/Rx data, free.
for (indexBuffer = 0; indexBuffer < 4; indexBuffer++)
{
if (bcBufferTx[indexBuffer])
{
rc = mxfTxPeriodicUpdateMsgBufferFree(bcBufferTx[indexBuffer]);
if (rc)
printf("Free buffer failed !\n\r");
}
}
for (indexBuffer = 0; indexBuffer < 4; 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