MX Foundation 4
mil1553_bc.c
/*****************************************************************************
//
## File:
## mil1553_bc.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 a MIL-STD-1553 bus controller.
// It can be run with mil1553_rt and mil1553_bm at the same time.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#include "../../inc/example.h"
#define M_ADDRESS 5
#define M_SUBADDRESS 3
#define LOCAL
int main(void)
{
uint32 rc;
uint64 deviceCount=0;
uint64 moduleCount=0;
uint64 channelCount=0;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL bc=0;
HMXF_BUFFER tx=0;
HMXF_BUFFER tx2=0;
HMXF_BUFFER rx=0;
size_t txBufferSize=0;
MXF_MIL1553_DATAREC* txBuffer=NULL;
size_t rxBufferSize=0;
MXF_MIL1553_DATAREC* rxBuffer=NULL;
uint64 rxAcqStatus;
uint64 msgCount;
uint64 byteCount;
uint64 rxRec;
uint64 loop=0;
uint64 address, subAddress, dir, wordCount;
uint64 card=0, mod=0, port=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)
{
rc = mxfSystemInit(server);
if(rc == MAXT_ERROR_ANOTHER_PROCESS_RUNNING)
{
// if another MX Foundation application is running, only load MX-Foundation library, otherwise reset cards first
rc = mxfSystemResourcesInit(server, 0);
}
}
// 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 not found, return an error
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
// Display BC channel location
if(!rc)
rc = mxfChannelLocationGet(bc, &card, &mod, &port);
if (!rc)
printf("BC location is Device:%llu Module:%llu Port:%llu\n\r", card, mod, port);
// Allocate 1KB buffer for tx data
if(!rc)
{
txBufferSize = 1024;
// Device allocation
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, 0, txBufferSize, &tx, NULL);
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, 1, 0, &tx2, NULL);
// Host allocation
if(!rc)
{
txBuffer = (MXF_MIL1553_DATAREC*)malloc(txBufferSize);
if(!txBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocate 20KB buffer for rx data
if(!rc)
{
rxBufferSize = 20*1024;
// Device allocation
rc = mxfRxAcqBufferAlloc(bc, rxBufferSize, &rx, 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);
// 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_SUBADDRESS, MXF_MIL1553_COMMAND_DIR_RX, 16, &minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = tx;
// Command #1 : Address 5, Subaddress 3, TX, 16 words
if(!rc)
{
rc = mxfMIL1553CommandCompose(M_ADDRESS, M_SUBADDRESS, MXF_MIL1553_COMMAND_DIR_TX, 16, &minorFrame[1].command);
minorFrame[1].modulo = 1;
minorFrame[1].buffer = tx2;
}
// Command #2 : Address 5, Mode Command 16, TX
if(!rc)
{
rc = mxfMIL1553CommandCompose(M_ADDRESS, 0, MXF_MIL1553_COMMAND_DIR_TX, 16, &minorFrame[2].command);
minorFrame[2].modulo = 1;
minorFrame[2].buffer = tx2;
}
if(!rc)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc, 0, 0, 3, minorFrame, NULL);
}
// Set default data for address 5, subaddress 3
if(!rc)
{
txRec1553 = (MXF_MIL1553_DATAREC *)txBuffer;
memset(txRec1553, 0, sizeof(MXF_MIL1553_DATAREC));
txRec1553->repeatCount = 1;
txRec1553->dataSize = 34; //34 bytes (command + 16 words)
txRec1553->data[0] = 0x0000; //Not used
txRec1553->data[1] = 0x0000;
txRec1553->data[2] = 0x1111;
txRec1553->data[3] = 0x2222;
txRec1553->data[4] = 0x3333;
txRec1553->data[5] = 0x4444;
txRec1553->data[6] = 0x5555;
txRec1553->data[7] = 0x6666;
txRec1553->data[8] = 0x7777;
txRec1553->data[9] = 0x8888;
txRec1553->data[10] = 0x9999;
txRec1553->data[11] = 0xAAAA;
txRec1553->data[12] = 0xBBBB;
txRec1553->data[13] = 0xCCCC;
txRec1553->data[14] = 0xDDDD;
txRec1553->data[15] = 0xEEEE;
txRec1553->data[16] = 0xFFFF;
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(tx, 1, txBuffer);
}
// Start BC acquisition
if(!rc)
rc = mxfRxAcqStart(rx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Start the major frame with 250 msec rate
if(!rc)
rc = mxfMIL1553TxPeriodicMajorFrameStart(bc, 0, 250000000, NULL);
// Read and display received messages
if(!rc)
{
do
{
rc = mxfMIL1553RxAcqRead(rx, 0, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxBuffer);
if (rc)
{
printf("mxfMIL1553RxAcqRead() failed; rc=0x%08x\n\r", rc);
break;
}
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) && (msgInfo.dataIndex != 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 < 300);
}
// Stop the major frame
if(!rc)
if(!rc)
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(rx);
if(rc)
{
if(mxfSystemErrorStringGet(server, rc, sizeof(errorString), errorString))
sprintf (errorString,"ERROR # 0x%08X", rc);
printf("%s\n\r", errorString);
}
// Free buffers
if(txBuffer)
free(txBuffer);
if(rxBuffer)
free(rxBuffer);
// Unload MX Foundation library
rc = mxfSystemTerminate(server);
if (rc == MAXT_ERROR_ANOTHER_PROCESS_RUNNING)
{
// if another MX Foundation application is running, only release the resource, otherwise reset everything
rc = mxfSystemResourcesRelease(server, 0);
}
printf("\n\rPress enter to terminate\n\r");
getchar();
return 0;
}
Updated 03/30/2022