MX Foundation 4
mil1553_bm_acquisition_trigger.c
/*******************************************************************************
//
// File:
// mil1553_bm_acquisition_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 demo illustrates how to use the trigger functionality
// to start acquisition on a receive channel.
//
// The program first starts the acquisition and waits for command BC-RT 10 SA 1 WC 4.
// Aperiodic transmission is then done to fire the trigger.
//
// Hardware Requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*******************************************************************************/
#include "example.h"
#include <time.h>
//#define LOOPBACK
#define LOCAL
#define MAX_TX_RECORDS_TO_TRANSMIT 100
#define BUFFER_SIZE MAX_TX_RECORDS_TO_TRANSMIT*sizeof(MXF_MIL1553_DATAREC) // Space for at least 100 records
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_MIL1553_DATAREC *rec1553);
uint32 TransmitAperiodicData(HMXF_BUFFER txBuffer, MXF_MIL1553_DATAREC *rec1553);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
uint64 count=0;
HMXF_CHANNEL bm=0;
HMXF_CHANNEL bc=0;
HMXF_BUFFER bmBuffer=0;
HMXF_BUFFER bcBuffer=0;
MXF_MIL1553_DATAREC *rec1553=NULL;
uint64 dev, mod, port;
HMXF_COND_LIST condList=0;
uint16 triggerData;
// Connect to services local or remote
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
if (rc)
{
printf("Failed to connect; rc=0x%08x", rc);
printf("\nPress a key to terminate\n");
getchar();
return 0;
}
// Initialize the server
printf("\nStarting\n");
rc = mxfSystemInit(server);
// Obtain the first MIL1553 Protocol BM channel (BM logical #0)
if (!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_MIL1553, MXF_SCLASS_BM_CHANNEL, MXF_MODULE_ALL, 1, &count, &bm);
// Obtain the first MIL1553 Protocol BC channel (BC logical #0)
if (!rc && count)
rc = mxfChannelAllGet(server, MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, MXF_MODULE_ALL, 1, &count, &bc);
// If channel not found, return an error
if(!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Set timebase to computer time 64-bit microseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_USEC);
// Get the physical port location
if (!rc)
{
rc = mxfChannelLocationGet(bm, &dev, &mod, &port);
if (!rc)
printf("Acquisition Channel (BM) location=%llu.%llu.%llu\n", dev, mod, port);
}
if (!rc)
{
rc = mxfChannelLocationGet(bc, &dev, &mod, &port);
if (!rc)
printf("Transmitter Channel (BC) location=%llu.%llu.%llu\n", dev, mod, port);
}
//Activate loopback before transmission and reception
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate RX acquisition buffer
if (!rc)
rc = mxfRxAcqBufferAlloc(bm, BUFFER_SIZE, &bmBuffer, NULL);
// Allocate TX Aperiodic buffer (1MiB)
if(!rc)
rc = mxfTxAperiodicBufferAlloc(bc, 0, BUFFER_SIZE, &bcBuffer, NULL);
// Allocate host buffer
if (!rc)
{
rec1553 = (MXF_MIL1553_DATAREC*)malloc(BUFFER_SIZE);
if (!rec1553)
rc = MAXT_ERROR_MEM;
}
// Configure trigger
// Create the condition list
if (!rc)
rc = mxfRxAcqTrigConditionListAlloc(server, &condList);
// The condition will be triggered on BC-RT 10 SA 1 WC 4 command
if(!rc)
{
mxfMIL1553CommandCompose(10, 1, MXF_MIL1553_COMMAND_DIR_RX, 4, &triggerData);
condParam.mask = 0x0000ffff;
condParam.data = triggerData;
condParam.offset = 0;
condParam.options = MXF_RXACQ_TRIG_COND_RDATA_OPTIONS_EQUAL;
rc = mxfRxAcqTrigConditionAdd(condList, MXF_RXACQ_TRIG_COND_ID_RDATA_DW , &condParam);
}
// Set the trigger with a pretrig count of 3 which allows some records to be read before the condition was triggered.
if(!rc)
rc = mxfRxAcqTrigSet(bmBuffer, condList, 3);
// Start the acquisition process
if (!rc)
rc = mxfRxAcqModeSet(bmBuffer, MXF_RXACQ_MODE_LINEAR);
if (!rc)
{
rc = mxfRxAcqStart(bmBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
printf("\nAcquisition started\n\r");
}
// Start ASYNC data transmission
if (!rc)
{
rc = TransmitAperiodicData(bcBuffer, rec1553);
if (!rc)
{
// Read ASYNC data in acquisition buffer
rc = ReadAcquisitionData(bmBuffer, rec1553);
}
}
// Stop and flush unread data
if (!rc)
rc = mxfRxAcqStop(bmBuffer);
if (!rc)
{
rc = mxfRxAcqClear(bmBuffer);
if (!rc)
printf("\nAcquisition stopped\n\r");
}
// Free trigger condition list
if(!rc)
// Catch any previous error
if (rc)
{
char buffer[256];
rc = mxfSystemErrorStringGet(server, rc, sizeof(buffer), buffer);
printf("%s\n", buffer);
}
if(bmBuffer)
{
rc = mxfRxAcqBufferFree(bmBuffer);
if (rc)
printf("Free buffer failed !\n\r");
}
if(bcBuffer)
{
rc = mxfTxAperiodicBufferFree(bcBuffer);
if (rc)
printf("Free buffer failed !\n\r");
}
printf("\nTerminating\n");
if (rec1553)
free(rec1553);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
/***************************************************************************************************************/
// TransmitAperiodicData
/***************************************************************************************************************/
uint32 TransmitAperiodicData(HMXF_BUFFER bcBuffer, MXF_MIL1553_DATAREC *rec1553)
{
uint32 rc;
MXF_MIL1553_DATAREC* rec=rec1553;
uint64 data;
// Prepare data
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = 0;
rec->control = 0;
rec->repeatCount = 1;
rec->dataSize = 10;
rec->reserved = 0;
mxfMIL1553CommandCompose(data%32, 1, MXF_MIL1553_COMMAND_DIR_RX, 4, &rec->data[0]);
rec->data[1] = 0x1111;
rec->data[2] = 0x2222;
rec->data[3] = 0x3333;
rec->data[4] = 0x4444;
mxfNextRecordPtrGet(MXF_CLASS_MIL1553, MXF_RECTYPE_DATAREC, rec, (void **)&rec);
}
printf("Transmitting ...\n");
rc = mxfTxAperiodicWrite(bcBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_RECORDS_TO_TRANSMIT, rec1553);
// Wait a little for transmission to occur
if(!rc)
mxfSleep(1000);
return rc;
}
/***************************************************************************************************************/
// ReadAcquisitionData
/***************************************************************************************************************/
uint32 ReadAcquisitionData(HMXF_BUFFER bmBuffer, MXF_MIL1553_DATAREC *rec1553)
{
MXF_MIL1553_DATAREC* rec=rec1553;
uint64 status, msgsCount, bytesCount;
uint64 j, trigTime=0;
uint32 rc;
time_t timeTemp;
struct tm * ptm;
char szTime[1024];
uint64 msec, usec;
uint64 word;
// Check trigger happened
rc = mxfRxAcqBufferStatusGet(bmBuffer, &status, NULL, NULL, NULL);
if(!rc)
{
if(status & MXF_RXACQ_STATUS_TRIG_OCCURRED)
{
// Display the acquisition trigger time
rc = mxfRxAcqTrigTimeGet(bmBuffer, &trigTime);
if(!rc)
{
timeTemp = trigTime/1000000;
ptm = localtime(&timeTemp); //fill a tm structure with the values that represent the corresponding local time
strftime(szTime, sizeof(szTime), "%Y-%m-%d %H:%M:%S", ptm);
usec = trigTime%1000;
timeTemp = trigTime/1000;
msec = timeTemp%1000;
printf("Event triggered at %s:%03llu:%03llu\n", szTime, msec, usec);
}
}
else
printf("Trigger not fired\n");
}
// Read and display records
rc = mxfRxAcqRead(bmBuffer, 0, BUFFER_SIZE, &status, &msgsCount, &bytesCount, rec1553);
for (j=0; j<msgsCount && !rc; j++)
{
printf(" %02llu: Timetag=%012llu, Size=%u\n Data=", j, rec->timeTag, rec->dataSize);
for(word=0; word<rec->dataSize/2; word++)
printf("%04X ", rec->data[word]);
printf("%s\n", trigTime==rec->timeTag?"*":"");
mxfNextRecordPtrGet(MXF_CLASS_MIL1553, MXF_RECTYPE_DATAREC, rec, (void **)&rec);
}
return rc;
}
Updated 10/23/2023