MX Foundation 4
ar717_embedded_recorder.c
/*******************************************************************************
//
// File:
// ar717_embedded_recorder.c
//
// Copyright (c) MAX Technologies Inc. 1988-2015, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example shows how to use the flash storage onboard the FlexMulti.
//
// - Format the onboard flash file(s) if necessary.
// - Automatic recording of ARINC 717 incoming RX data and write it on the flash.
// - Transmit ARINC 717 records.
// - Read the data stored on the flash.
//
// Hardware Requirements:
// - MAXT FlexMulti
// - loopback between first TX and RX ARINC 717 Enhanced channel when internal loopback is not used
//
*******************************************************************************/
#include "example.h"
//#define LOOPBACK
//#define LOCAL
#define FILE_SIZE 256*1024*1024 // 256 MB
#define MAX_TX_SUBFRAMES_TO_TRANSMIT 6 //minimum 4 to have at least one full frame.
#define SUBFRAMESIZE 1024
uint32 writeMsgs(HMXF_BUFFER buffer, MXF_A717_DATAREC *hostBuffer);
uint32 read717RecordsFromEmbeddedFlash(HMXF_NVSTORAGE flash, uint64 fileIdx);
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
size_t BUFFER_SIZE = (MAX_TX_SUBFRAMES_TO_TRANSMIT*sizeof(MXF_A717_DATAREC));
uint64 deviceIdx = 0;
uint64 moduleIdx = 0;
/******************************************************************************/
// Main
/******************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL tx717=0, rx717=0;
HMXF_BUFFER txBuffer717=0;
HMXF_NVSTORAGE flash=0;
MXF_NVSTORAGE_INFO nvInfo={0};
MXF_A717_DATAREC* txHostBuffer=0;
uint64 count=0;
uint64 fileIdx=0, fileSize;
uint32 needUnmount=FALSE;
uint64 channelIdx=0;
// Connects to services and initialize environment
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", TRUE, &server);
#endif
if(rc!=MAXT_SUCCESS)
{
printf("Failed to connect; rc=0x%08x", rc);
printf("\nPress a key to terminate\n");
getchar();
return 0;
}
// Initializes the server
printf("Starting\n");
rc = mxfSystemInit(server);
// Gets handle of first channel of MULTI-EH module
if (!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_ALL, MXF_SCLASS_ALL, MXF_MODULE_MULTI_EH, 1, &count, &tx717);
// Error if no A717 channel found
if (!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Gets location of device and module for that channel
if (!rc)
rc = mxfChannelLocationGet(tx717, &deviceIdx, &moduleIdx, &channelIdx);
// Releases resources to configure A717 channel
if(!rc)
rc = mxfSystemResourcesRelease(server, 0);
// Initializes init callback handler to set first TX and RX channel to A717
if(!rc)
rc = mxfSystemInitAttributeUint64CallbackHandler(server, &initHandler);
// Initializes library
if(!rc)
rc = mxfSystemInit(server);
// Get the device handle
if (!rc)
rc = mxfSystemDeviceGet(server, deviceIdx, &device);
// Gets first module A717 Enhanced
if(!rc && count)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MULTI_EH, 1, &count, &module);
// Gets the first ARINC 717 TX channel
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A717, MXF_SCLASS_TX_CHANNEL, 1, &count, &tx717);
// Gets the first ARINC 717 RX channel
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A717, MXF_SCLASS_RX_CHANNEL, 1, &count, &rx717);
// Error if no A717 channel found
if(!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Allocates TX Aperiodic static buffer for HIGH priority queue
if (!rc)
rc=mxfTxAperiodicBufferAlloc(tx717, MXF_TXAPERIODIC_PRIORITY_HIGH, BUFFER_SIZE, &txBuffer717, NULL);
if(!rc)
{
txHostBuffer = (MXF_A717_DATAREC*) calloc(1, BUFFER_SIZE);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
// Sets timebase to 64-bit microseconds
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
//Activates loopback before transmission and reception
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(rx717, KMXF_A717_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
#if (MAX_TX_SUBFRAMES_TO_TRANSMIT < 4)
if(!rc)
printf("\nNumber of subframes to transmit less than 4: synchronization will not occur.\n\n");
#endif
// Sets RX & TX channels subframe size
if(!rc)
rc=mxfAttributeUint64Set(rx717, KMXF_A717_SUBFRAME_SIZE, SUBFRAMESIZE);
if(!rc)
rc=mxfAttributeUint64Set(tx717, KMXF_A717_SUBFRAME_SIZE, SUBFRAMESIZE);
// Sets RX & TX channels bit encoding to harvard
if(!rc)
rc=mxfAttributeUint64Set(rx717, KMXF_A717_BIT_ENCODING, VMXF_A717_BIT_ENCODING_HARVARDBIPHASE);
if(!rc)
rc=mxfAttributeUint64Set(tx717, KMXF_A717_BIT_ENCODING, VMXF_A717_BIT_ENCODING_HARVARDBIPHASE);
// Sets RX & TX channels electrical selection to default
if(!rc)
rc=mxfAttributeUint64Set(rx717, KMXF_A717_ELECTRICAL_SELECTION, VMXF_A717_ELECTRICAL_SELECT_DEFAULT);
if(!rc)
rc=mxfAttributeUint64Set(tx717, KMXF_A717_ELECTRICAL_SELECTION, VMXF_A717_ELECTRICAL_SELECT_DEFAULT);
/******************************************************************************/
// Initialize the flash
/******************************************************************************/
// Gets flash handle
if(!rc)
rc = mxfEmbeddedNVStorageHandleGet(device, &flash);
if (!rc)
rc = mxfEmbeddedNVStorageInfoGet(flash, &nvInfo);
if (!rc && (nvInfo.type == MXF_EMBEDDED_NVSTORAGE_TYPE_EMMC))
{
needUnmount = TRUE;
}
// Checks if a file is already create in flash
if(!rc)
{
rc = mxfEmbeddedNVStorageFileCountGet(flash, &count);
if (rc == MAXT_ERROR_NVSTORAGE_NOT_FORMATTED)
{
printf("Formatting...\n");
printf("Done\n");
count = 0;
}
}
if(!rc)
{
if(count)
{
// Verifies if first file is right size
rc = mxfEmbeddedNVStorageFileSizeGet(flash, 0, &fileSize);
if(!rc)
{
// This file is ok, resets it
if(fileSize == FILE_SIZE/nvInfo.sectorSize)
{
fileIdx = 0;
rc = mxfEmbeddedNVStorageFileReset(flash, fileIdx);
if(!rc)
printf("File #%llu reset\n", fileIdx);
}
// Formats and creates a new file
else
{
printf("Formatting...\n");
printf("Done\n");
if(!rc)
rc = mxfEmbeddedNVStorageFileCreate(flash, FILE_SIZE/nvInfo.sectorSize, &fileIdx);
if(!rc)
printf("File #%llu allocated\n", fileIdx);
}
}
}
else
{
// Allocates file
rc = mxfEmbeddedNVStorageInfoGet(flash, &nvInfo);
if(!rc)
rc = mxfEmbeddedNVStorageFileCreate(flash, FILE_SIZE/nvInfo.sectorSize, &fileIdx);
if(!rc)
printf("File #%llu allocated\n", fileIdx);
}
}
// Enables the recording of all messages
if (!rc)
rc = mxfEmbeddedNVStorageFileMsgSelectEnableSet(rx717, fileIdx, 0, TRUE);
// Transmits
if(!rc)
rc = writeMsgs(txBuffer717, txHostBuffer);
// Reads from memory
if (!rc)
rc = read717RecordsFromEmbeddedFlash(flash, fileIdx);
// Disables recording
if(!rc)
rc = mxfEmbeddedNVStorageFileMsgSelectEnableSet(rx717, fileIdx, 0, FALSE);
if (needUnmount)
// Catches any previous failing function
if(rc)
{
char buffer[256];
if(mxfSystemErrorStringGet(server, rc, sizeof(buffer), buffer))
sprintf (buffer,"ERROR # 0x%08X", rc);
printf("%s\n\r", buffer);
}
//Frees all buffers
if (txBuffer717)
// Terminates
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
//****************************************************************************************************************
// Aperiodic Transmission
//****************************************************************************************************************
uint32 writeMsgs(HMXF_BUFFER buffer, MXF_A717_DATAREC *hostBuffer)
{
uint32 rc=0;
uint32 i;
MXF_A717_DATAREC *recPtr=hostBuffer;
uint64 word;
for(i=0; !rc && i<MAX_TX_SUBFRAMES_TO_TRANSMIT; i++)
{
recPtr->timeTag=0;
recPtr->control=0;
recPtr->dataSize = 2 * (uint32)SUBFRAMESIZE; // 16 bits per word in subframe
recPtr->repeatCount=1;
recPtr->reserved=0;
for(word=0; word < SUBFRAMESIZE ; word++)
{
if (word == 0)
{
//1st word of each subframe has to be a sync word
// sync words are:
// - 0x247 for subframe #0
// - 0x5B8 for subframe #1
// - 0xA47 for subframe #2
// - 0xDB8 for subframe #3
switch (i%4)
{
case 0:
recPtr->data[word] = 0x247;
break;
case 1:
recPtr->data[word] = 0x5B8;
break;
case 2:
recPtr->data[word] = 0xA47;
break;
case 3:
recPtr->data[word] = 0xDB8;
break;
default:
break;
}
}
else
recPtr->data[word] = (uint16)(0x11*word);
}
if(!rc)
rc = mxfA717NextDataRecordPtrGet(recPtr, &recPtr);
}
if(!rc)
{
printf("Transmitting ...\n");
// Transmits strings on relative record time
rc = mxfA717TxAperiodicWrite(buffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_SUBFRAMES_TO_TRANSMIT, hostBuffer);
}
if (!rc)
mxfSleep((MAX_TX_SUBFRAMES_TO_TRANSMIT+2) * 1000); // 1 second per subframe + 2 seconds to be sure to exceed the minimum duration to wait.
if(rc)
printf("Transmit failed; rc=0x%08x\n", rc);
else
printf("\nWriting %d records\n", i);
return rc;
}
uint32 read717RecordsFromEmbeddedFlash(HMXF_NVSTORAGE flash, uint64 fileIdx)
{
MXF_A717_DATAREC* rec717, *p=0;
uint64 msgReadCnt, byteReadCnt;
uint64 word, data;
uint32 rc=0;
// Allocates host buffer
rec717 = (MXF_A717_DATAREC*)malloc(BUFFER_SIZE);
if (!rec717)
rc = MAXT_ERROR_MEM;
if(!rc)
{
p = (MXF_A717_DATAREC*) calloc(1, BUFFER_SIZE);
if(!p)
rc = MAXT_ERROR_MEM;
}
// Reads all the messages on flash in the buffer allocated previously
printf("Reading from flash\n\n");
if(!rc)
{
do
{
rc = mxfA717EmbeddedNVStorageFileMsgRead(flash, fileIdx, 0, BUFFER_SIZE, &msgReadCnt, &byteReadCnt, p);
// Displays all records written by the embedded application
if(!rc)
printf("String received count = %llu \n", msgReadCnt);
if(!rc)
{
// Displays received strings
rec717 = p;
for(data=0; data<msgReadCnt && !rc; data++)
{
printf("\n%02llu: Timetag=%012llu, Size=%u words\n", data, rec717->timeTag, (rec717->dataSize)/2);
for(word=0; word < SUBFRAMESIZE ; word++)
printf("%03X ", rec717->data[word]);
printf("\n");
mxfA717NextDataRecordPtrGet(rec717, &rec717);
}
}
if(rc)
printf("Acquisition read failed; rc=0x%08x\n", rc);
}while(!rc && msgReadCnt);
}
if (rec717)
free(rec717);
return rc;
}
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value)
{
HMXF_DEVICE device;
MXF_DEVICE_INFO deviceInfo;
uint32 rc;
server=server;
deviceIndex=deviceIndex;
if(attrib == KMXF_CHANNEL_CLASS)
{
rc = mxfSystemDeviceGet(server, deviceIndex, &device);
if (!rc)
rc = mxfDeviceInfoGet(device, &deviceInfo);
if(!rc && (deviceInfo.modules[moduleIndex].type == MXF_MODULE_MULTI_EH))
{
// Sets MXF_MODULE_MULTI_EH first TX and RX channels to A717
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
*value = MXF_CLASS_A717;
return TRUE;
}
}
}
return FALSE;
}
Updated 10/23/2023