MX Foundation 4
mil1553_embedded_update_embedded.c
/*****************************************************************************
//
// File:
// mil1553_embedded_update_embedded.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 how to transmit information from host to
// embedded application. The communication queues are used to communicate
// between host and embedded application part. Data from communication
// queue is used to update BC message. Asynchronous event is used for
// data communication queue threshold.
//
// This file is used to generate the embedded part of the application.
// mil1553_embedded_update_host.c file is used for the host part
// of the application.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti.
//
*****************************************************************************/
#include "example.h"
#define MAX_TX_RECORDS_TO_TRANSMIT 8
#define EX_USER_COMMAND_ID_START 0x80000001
#define EX_USER_COMMAND_ID_STOP 0x80000002
#define EX_USER_COMMAND_ID_READ 0x80000003
#define EX_USER_COMMAND_ID_TERMINATE 0x80000005
#define EX_USER_COMMAND_ACK 0xA5A5A5A5
#define RT_ADDRESS 1
#define RT_SUBADDRESS 2
typedef struct
{
HMXF_SERVER server;
HMXF_DEVICE device;
HMXF_MODULE module1553;
HMXF_CHANNEL bcChannel;
HMXF_CHANNEL bmChannel;
HMXF_BUFFER commTxBuffer;
HMXF_BUFFER commRxBuffer;
HMXF_BUFFER commTxUpdateDataBuffer;
}
TEST_INFO;
typedef struct
{
uint32 command;
uint32 paramNum;
uint32 param[32];
}
EX_COMMAND;
typedef struct
{
MXF_EMBEDDED_DATAREC* commTxHostUpdateDataBuffer;
MXF_MIL1553_DATAREC* bcHostBuffer;
uint64 txBufferSize;
HMXF_BUFFER bcBuffer;
}
EVENT_INFO;
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
uint32 commandSend(HMXF_BUFFER txBuffer, HMXF_BUFFER rxBuffer, EX_COMMAND* cmd);
uint32 commandRead(HMXF_BUFFER rxBuffer, uint64* readCnt, EX_COMMAND* cmd);
uint32 embeddedPart(TEST_INFO info);
uint32 eventHandler(HMXF_HANDLE asyncEvent, void* param);
int
main(void)
{
TEST_INFO info;
uint32 rc;
uint64 moduleCount=0;
uint64 channelCount=0;
// Connect to MX Foundation library
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &info.server);
// Set initialisation handler
if(!rc)
rc = mxfSystemInitAttributeUint64CallbackHandler(info.server, &initHandler);
// Initialize MX Foundation library
if(!rc)
{
printf("Starting ...\n\r");
rc = mxfSystemInit(info.server);
}
// Get the device handle
if (!rc)
rc = mxfSystemDeviceGet(info.server, 0, &info.device);
// Get handle of first 1553-EH module
if(!rc)
rc = mxfDeviceModuleAllGet(info.device, MXF_MODULE_MIL1553MRT_EH, 1, &moduleCount, &info.module1553);
// Get handle of first MIL1553 BC channel
if(!rc && moduleCount)
rc = mxfModuleChannelAllGet(info.module1553, MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, 1, &channelCount, &info.bcChannel);
// Get handle of first MIL1553 BM channel
if(!rc && channelCount)
rc = mxfModuleChannelAllGet(info.module1553, MXF_CLASS_MIL1553, MXF_SCLASS_BM_CHANNEL, 1, &channelCount, &info.bmChannel);
if(!rc && !channelCount)
rc = MAXT_ERROR_NOT_FOUND;
if(!rc)
rc = embeddedPart(info);
if(rc)
{
char errorString[200];
if(mxfSystemErrorStringGet(info.server, rc, sizeof(errorString), errorString))
sprintf (errorString,"ERROR # 0x%X", rc);
printf("%s\n\r", errorString);
}
printf("Terminating ...\n\r");
// Free device buffers
mxfDeviceBuffersFree(info.device);
// Unload MX Foundation library
mxfSystemTerminate(info.server);
// Disconnect from MX Foundation library
mxfServerDisconnect(info.server);
return rc;
}
uint32
initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value)
{
server=server;
deviceIndex=deviceIndex;
moduleIndex=moduleIndex;
channelIndex=channelIndex;
if(attrib == KMXF_DEVICE_COMM_QUEUE_NUM)
{
// Set the communication queue number attribute
*value = 3;
return TRUE;
}
return FALSE;
}
uint32
commandSend(HMXF_BUFFER txBuffer, HMXF_BUFFER rxBuffer, EX_COMMAND* cmd)
{
uint64 readCnt=0;
uint64 byteCnt;
uint32 rc=0;
memset(&msg, 0, sizeof(msg));
msg.dataSize = (2+cmd->paramNum)*4;
memcpy(msg.data, cmd, msg.dataSize);
// Write command in the communication queue
rc=mxfDeviceCommBufferWrite(txBuffer, 1, &msg);
if(!rc && rxBuffer)
{
while(!rc)
{
// Wait acknowledge of embedded application
rc=mxfDeviceCommBufferRead(rxBuffer, 1, sizeof(msg), &readCnt, &byteCnt, &msg);
if(!rc && readCnt)
{
if((msg.dataSize >= 8) && (msg.data[0]==EX_USER_COMMAND_ACK))
{
if(msg.dataSize >= 12)
rc = msg.data[2];
break;
}
}
}
}
return rc;
}
uint32
commandRead(HMXF_BUFFER rxBuffer, uint64* readCnt, EX_COMMAND* cmd)
{
uint64 byteCnt;
uint32 rc;
memset(cmd, 0, sizeof(*cmd));
// Read command from the communication queue
rc=mxfDeviceCommBufferRead(rxBuffer, 1, sizeof(*cmd), readCnt, &byteCnt, &msg);
if(!rc && *readCnt)
{
cmd->command = msg.data[0];
cmd->paramNum = msg.data[1];
memcpy(cmd->param, &msg.data[2], cmd->paramNum*4);
}
return rc;
}
uint32
embeddedPart(TEST_INFO info)
{
EX_COMMAND cmd;
HMXF_BUFFER bcBuffer=0;
MXF_EMBEDDED_DATAREC* commTxHostBuffer=0;
uint32 rc;
int run=TRUE;
size_t txBufferSize=0;
uint64 msgCount=0;
HMXF_ASYNCEVENT asyncEvent=0;
EVENT_INFO eventInfo;
HMXF_BUFFER bmBuffer=0;
size_t rxBufferSize=0;
uint64 status=0;
uint64 byteCount=0;
MXF_MIL1553_DATAREC* bmHostBuffer=0;
uint64 data;
uint64 word;
memset(&eventInfo, 0, sizeof(eventInfo));
// Set timebase to RTC nsec
rc=mxfSystemTimeBaseSet(info.server, MXF_TIMEBASE_DEVICE_NSEC);
// Get TX communication buffer
if(!rc)
rc=mxfDeviceCommBufferGet(info.device, 1, &info.commTxBuffer);
// Get RX communication buffer
if(!rc)
rc=mxfDeviceCommBufferGet(info.device, 0, &info.commRxBuffer);
// Get TX communication update data buffer
if(!rc)
rc=mxfDeviceCommBufferGet(info.device, 2, &info.commTxUpdateDataBuffer);
// Allocate 1 KB buffer for BC update
if(!rc)
{
txBufferSize = 1*1024;
// Allocate BC update buffer for RT1 SA2 RX
rc=mxfTxPeriodicUpdateMsgBufferAlloc(info.bcChannel, 0, txBufferSize, &bcBuffer, NULL);
// Host buffer
if(!rc)
{
eventInfo.bcHostBuffer = (MXF_MIL1553_DATAREC*)malloc(txBufferSize);
if(!eventInfo.bcHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Allocate 10 KB buffer for host comm tx transmission
if(!rc)
{
txBufferSize = 10*1024;
commTxHostBuffer = (MXF_EMBEDDED_DATAREC*)malloc(txBufferSize);
if(!commTxHostBuffer)
rc = MAXT_ERROR_MEM;
}
// Allocate 1 KB buffer for host comm update data transmission
if(!rc)
{
eventInfo.txBufferSize = 1*1024;
eventInfo.commTxHostUpdateDataBuffer = (MXF_EMBEDDED_DATAREC*)malloc((size_t)eventInfo.txBufferSize);
if(!eventInfo.commTxHostUpdateDataBuffer)
rc = MAXT_ERROR_MEM;
}
// Allocate 10 KB buffer for BM acquisition
if(!rc)
{
rxBufferSize = 10*1024;
// Allocate RX data acquisition buffer
rc=mxfRxAcqBufferAlloc(info.bmChannel, rxBufferSize, &bmBuffer, NULL);
// Host buffer allocation
if(!rc)
{
bmHostBuffer = (MXF_MIL1553_DATAREC*)calloc(1, rxBufferSize);
if(!bmHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Initialize condition event for update data communication queue threshold
memset(&condition, 0, sizeof(condition));
condition.condID = MXF_ASYNCEVENT_COND_COMM_RX_BUFFER_THRESHOLD;
condition.condition.commBufferThreshold.buffer = info.commTxUpdateDataBuffer;
while(!rc && run)
{
rc=commandRead(info.commRxBuffer, &msgCount, &cmd);
if(!rc && msgCount)
{
msgCount=0;
switch(cmd.command)
{
case EX_USER_COMMAND_ID_START:
// Set the minor frame #0 using 1 Command
memset(&minorFrame, 0, sizeof(minorFrame));
// Command #0 : Address 1, Subaddress 2, RX, 8 words
rc = mxfMIL1553CommandCompose(RT_ADDRESS, RT_SUBADDRESS, MXF_MIL1553_COMMAND_DIR_RX, 8, &minorFrame.command);
// Set Mil1553 frame
if(!rc)
{
minorFrame.modulo = 1;
minorFrame.buffer = bcBuffer;
rc=mxfMIL1553TxPeriodicMajorFrameSet(info.bcChannel, 0, 0, 1, &minorFrame, NULL);
}
// attach async event handler
if(!rc)
{
eventInfo.bcBuffer = bcBuffer;
rc=mxfAsyncEventHandlerInit(info.server, eventHandler, &eventInfo, &asyncEvent);
}
// set event condition on communication queue update data
if(!rc)
rc=mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &condition);
// Start acquisition on BM
if(!rc)
rc=mxfRxAcqStart(bmBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Start BC frame with 50 msec rate
if(!rc)
rc=mxfMIL1553TxPeriodicMajorFrameStart(info.bcChannel, 0, 50000000, NULL);
printf("Recorder started !\n\r");
break;
case EX_USER_COMMAND_ID_STOP:
// Stop Mil1553 frame
rc=mxfTxPeriodicMajorFrameStop(info.bcChannel, 0, 0);
// Stop acquisition on BM
if(!rc)
rc=mxfRxAcqStop(bmBuffer);
// Clear Mil1553 frame
if(!rc)
rc=mxfTxPeriodicMajorFrameClear(info.bcChannel, MXF_TXPERIODIC_UPDATEMSG_CLEAR_OPT_DEFAULT);
// Disable condition
if(!rc)
rc=mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &condition);
// Detach event handler
if(!rc)
printf("Recorder stopped !\n\r");
break;
case EX_USER_COMMAND_ID_READ:
if(!rc)
{
printf("Reading ...\n\r");
// Read rx data buffer
rc=mxfMIL1553RxAcqRead(bmBuffer, 0, rxBufferSize, &status, &msgCount, &byteCount, bmHostBuffer);
if(!rc)
{
printf("Message received count = %llu \n\r", msgCount);
rec = bmHostBuffer;
// print BM messages
for(data=0; data<msgCount && !rc; data++)
{
printf(" %02llu: Timetag=%012llu, Size=%u, Data=", data, rec->timeTag, rec->dataSize);
for(word=0; word<rec->dataSize/2; word++)
printf("%04X", rec->data[word]);
printf("\n\r");
}
}
}
break;
case EX_USER_COMMAND_ID_TERMINATE:
run=FALSE;
printf("App terminated !\n\r");
break;
}
// Send ACK to host application
cmd.command = EX_USER_COMMAND_ACK;
cmd.paramNum = 1;
cmd.param[0] = rc;
commandSend(info.commTxBuffer, 0, &cmd);
}
}
// Free buffers
if(eventInfo.commTxHostUpdateDataBuffer)
free(eventInfo.commTxHostUpdateDataBuffer);
if(commTxHostBuffer)
free(commTxHostBuffer);
if(bmHostBuffer)
free(bmHostBuffer);
return rc;
}
uint32 eventHandler(HMXF_HANDLE asyncEvent, void* param)
{
EVENT_INFO* eventInfo=(EVENT_INFO*)param;
uint32 rc=0;
uint64 eventCount;
uint64 msg, msgCount, byteCount;
MXF_MIL1553_DATAREC* mil1553Rec;
// Get pending event
rc=mxfAsyncEventPendingGet(asyncEvent, 1, &eventCount, &pendingList);
if(!rc && eventCount)
{
switch(pendingList.condID)
{
case MXF_ASYNCEVENT_COND_COMM_RX_BUFFER_THRESHOLD:
// Read update data from communication queue
rc=mxfDeviceCommBufferRead(pendingList.condition.commBufferThreshold.buffer, MAX_TX_RECORDS_TO_TRANSMIT, eventInfo->txBufferSize, &msgCount, &byteCount, eventInfo->commTxHostUpdateDataBuffer);
if(!rc)
{
// Copy update data to Mil1553 buffer
memset(eventInfo->bcHostBuffer, 0, (size_t)eventInfo->txBufferSize);
mil1553Rec = eventInfo->bcHostBuffer;
commRec = eventInfo->commTxHostUpdateDataBuffer;
for(msg=0; msg<msgCount; msg++)
{
mil1553Rec->control = commRec->control;
mil1553Rec->repeatCount = commRec->repeatCount;
mil1553Rec->dataSize = min(64, commRec->dataSize);
memcpy(mil1553Rec->data, commRec->data, mil1553Rec->dataSize);
mxfMIL1553NextDataRecordPtrGet(mil1553Rec, &mil1553Rec);
mxfNextRecordPtrGet(MXF_CLASS_EMBEDDED, MXF_RECTYPE_DATAREC, commRec, (void**)&commRec);
}
// Update Mil1553 message
rc=mxfMIL1553TxPeriodicUpdateMsgWrite(eventInfo->bcBuffer, msg, eventInfo->bcHostBuffer);
}
break;
}
}
if(rc)
printf("event handler rc=0x%08x\n\r", rc);
return rc;
}
Updated 10/23/2023