MX Foundation 4
ar429_buffer_threshold.c
/************************************************************************************************
//
// File:
// ar429_buffer_threshold.c
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example illustrates how to manage buffer thresholds with asynchronous events.
// The demo will transmit a ramp on one label and record the messages using asynchronous
// events. The first record of each update will have a parity error.
//
// Hardware Requirements:
// - MAXT Flex or PCI-500 carrier
// - Loopback between first TX and RX ARINC 429 channels.
//
**************************************************************************************************/
#include "example.h"
#define LOOPBACK
#define LOCAL
#define BUFFER_SIZE 1*1024*1024 // 1 MiB
#define TXALMOSTFULL 7
#define TXALMOSTEMPTY 3
#define RXALMOSTFULL 5
#define RXALMOSTEMPTY 2
#define TX_MSG_LABEL 5
#define TX_MSG_SDI 0
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param);
uint32 updateMsgs(HMXF_BUFFER buffer, MXF_A429_DATAREC* hostBuffer);
uint32 readAcquisition(HMXF_BUFFER buffer, MXF_A429_DATAREC* hostBuffer);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device = 0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannel = 0, txChannel = 0;
HMXF_ASYNCEVENT asyncEvent = 0;
HMXF_BUFFER rxBuffer = 0, txBuffer = 0;
MXF_ASYNCEVENT_CONDITION RXasyncEventInfo, TXasyncEventInfo;
HMXF_SCHED schedule = 0;
HMXF_SCHED_MSG msgSched;
uint64 count=0, type=0, options=0;
MXF_A429_DATAREC* hostBuffer = NULL;
// Connect to services and initialize environment
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
if (rc != MAXT_SUCCESS)
{
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);
// Get the first ARINC 429 Protocol RX channel (RX logical #0)
if (!rc)
rc = mxfChannelAllGet(server, MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, 1, &count, &rxChannel);
// Obtain the first ARINC 429 Protocol TX channel (TX logical #0)
if (!rc && count)
rc = mxfChannelAllGet(server, MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 1, &count, &txChannel);
// If channel not found, return an error
if (!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
//Get module type
if (!rc)
rc = mxfChannelInfoGet(rxChannel, &device, &module);
if (!rc)
rc = mxfAttributeUint64Get(module, KMXF_MODULE_TYPE, &type);
// Set the channels to low speed
if(!rc)
{
// If IPM-429
if(type == MXF_MODULE_A429E)
{
rc = mxfAttributeUint64Set(rxChannel, KMXF_A429_SPEED_SELECT, VMXF_A429_SPEED_SELECT_LOW);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_A429_SPEED_SELECT, VMXF_A429_SPEED_SELECT_LOW);
}
else
{
rc = mxfAttributeUint64Set(rxChannel, KMXF_A429_SPEED, 12500);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_A429_SPEED, 12500);
}
}
// If Flex429 bidirectionnal, enable Tx port
if(!rc && type == MXF_MODULE_FLEX429)
{
rc = mxfAttributeUint64Get(module, KMXF_MODULE_OPTIONS, &options);
if(!rc && (options & VMXF_A429_MODULE_OPTIONS_BIDIRECTIONAL))
rc = mxfAttributeUint64Set(txChannel, KMXF_A429_TX_PORT_ENABLE, VMXF_ENABLE);
}
// Enable loopback
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Set timebase to 64-bit nanoseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Alloc host buffer
if (!rc)
{
hostBuffer = (MXF_A429_DATAREC*)malloc(BUFFER_SIZE);
if (!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Set the event handler
if (!rc)
rc = mxfAsyncEventHandlerInit(server, &asyncEventHandler, hostBuffer, &asyncEvent);
// Allocate RX acquisition buffer (1 MiB)
if (!rc)
rc = mxfRxAcqBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Set the RX async event condition
if (!rc)
{
memset(&RXasyncEventInfo, 0, sizeof(RXasyncEventInfo));
RXasyncEventInfo.condID = MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD;
RXasyncEventInfo.condition.rxAcqBufferThreshold.buffer = rxBuffer;
RXasyncEventInfo.condition.rxAcqBufferThreshold.almostFull = RXALMOSTFULL;
RXasyncEventInfo.condition.rxAcqBufferThreshold.almostEmpty = RXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &RXasyncEventInfo);
}
// Set acquisition mode
if (!rc)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Select all labels to be received
if (!rc)
rc = mxfA429RxAcqMsgSelectSet(rxBuffer, MXF_MSG_SELECT_ADD, 0, NULL);
// Start acquisition
if (!rc)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
printf("Acquisition started\n\r");
// Allocate TX Periodic Update buffer (1MiB)
if (!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, TX_MSG_LABEL, BUFFER_SIZE, &txBuffer, NULL);
// Set the TX async event condition
if (!rc)
{
memset(&TXasyncEventInfo, 0, sizeof(TXasyncEventInfo));
TXasyncEventInfo.condID = MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD;
TXasyncEventInfo.condition.txPeriodicUpdateMsgBufferThreshold.almostFull = TXALMOSTFULL;
TXasyncEventInfo.condition.txPeriodicUpdateMsgBufferThreshold.almostEmpty = TXALMOSTEMPTY;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 1, &TXasyncEventInfo);
}
// Select message for async event condition
if (!rc)
rc = mxfAsyncEventTxPeriodicUpdateMsgSelectSet(asyncEvent, txChannel, MXF_MSG_SELECT_ONLY, 1, &txBuffer);
// Set the Periodic Scheduler
if (!rc)
rc = mxfTxPeriodicScheduleNew(txChannel, &schedule);
// Set scheduling values: Rate=25 ms, Phase=0 us
if (!rc)
rc = mxfTxPeriodicScheduleSingleMsgAdd(schedule, 25000000ll, 0, txBuffer, &msgSched);
// Run the schedule
if (!rc)
rc = mxfTxPeriodicScheduleRun(schedule);
// Send messages for 2 seconds
if (!rc)
{
printf("Running periodic transmission...\n\r");
mxfSleep(2000);
rc = mxfTxPeriodicScheduleFree(schedule);
}
// Stop acquisition
if (!rc)
rc = mxfRxAcqStop(rxBuffer);
// Disable conditions
if (!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &RXasyncEventInfo);
if (!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 1, &TXasyncEventInfo);
// Terminate async event handler
if (!rc)
// Catch 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 (rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer)
// Terminate
if (hostBuffer)
free(hostBuffer);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
//****************************************************************************************************************
// Asynchronous Event Handler
//****************************************************************************************************************
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param)
{
uint64 maxCount = 64, pendingCount;
uint64 i;
uint32 rc;
// Get the list of pending events to process
rc = mxfAsyncEventPendingGet(asyncEvent, maxCount, &pendingCount, pendingList);
for (i = 0; !rc && i < pendingCount; i++)
{
switch (pendingList[i].condID)
{
case MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD:
// An almost empty condition was detected...
updateMsgs(pendingList[i].condition.txPeriodicUpdateMsgBufferThreshold.buffer, (MXF_A429_DATAREC*)param);
break;
case MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD:
// An almost full condition was detected...
readAcquisition(pendingList[i].condition.rxAcqBufferThreshold.buffer, (MXF_A429_DATAREC*)param);
break;
default:
printf("Unknown condID 0x%llx)", pendingList[i].condID);
break;
}
}
return rc;
}
//****************************************************************************************************************
// Periodic Transmission
//****************************************************************************************************************
uint32 updateMsgs(HMXF_BUFFER buffer, MXF_A429_DATAREC* hostBuffer)
{
uint32 rc = 0;
uint32 i;
MXF_A429_DATAREC* recPtr = hostBuffer;
uint64 label, sdi, ssm, parity;
static uint64 data = 0;
static uint32 TXAsyncEvents = 0;
// Refill the FIFO in order to produce a ramp
for (i = 0; !rc && i < TXALMOSTFULL; i++)
{
// Send a parity error on first record
recPtr->timeTag = 0;
recPtr->control = (i != 0) ? 0 : MXF_A429_TX_REC_CTRL_PARITY_ERROR;
recPtr->repeatCount = 1;
recPtr->reserved = 0;
label = TX_MSG_LABEL;
sdi = TX_MSG_SDI;
data++;
ssm = 0;
parity = VMXF_A429_PARITY_ODD;
rc = mxfA429ArwCompose(label, sdi, data, ssm, parity, &recPtr->data);
if (!rc)
rc = mxfA429NextDataRecordPtrGet(recPtr, &recPtr);
}
// Add more data to the buffer
if (!rc)
rc = mxfA429TxPeriodicUpdateMsgWrite(buffer, i, hostBuffer);
if (rc)
printf("Periodic Update failed; rc=0x%08x\n", rc);
else
printf("\nAsync Event %d - Writing %d records\n", ++TXAsyncEvents, i);
return rc;
}
//****************************************************************************************************************
// Acquisition Reception
//****************************************************************************************************************
uint32 readAcquisition(HMXF_BUFFER buffer, MXF_A429_DATAREC* hostBuffer)
{
MXF_A429_DATAREC* recPtr = hostBuffer;
uint64 status, msgsCount, bytesCount;
uint64 label, sdi, data, ssm, parity;
uint64 j;
uint32 rc;
// Read and display records
rc = mxfA429RxAcqRead(buffer, 0, BUFFER_SIZE, &status, &msgsCount, &bytesCount, hostBuffer);
if (!rc)
{
printf("Read %llu messages\n\r", msgsCount);
for (j = 0; !rc && j < msgsCount; j++)
{
rc = mxfA429ArwDecompose(recPtr->data, &label, &sdi, &data, &ssm, &parity);
if (!rc)
{
printf("%02llu: Timetag %llu", j, recPtr->timeTag);
printf(" - Control : %s ", (recPtr->control == MXF_A429_RX_REC_CTRL_PARITY_ERROR) ? "PARITY ERROR" : "NO ERROR");
printf("- ARINC word=[%03llo,%lld,%05llX,%lld,%s]\n", label, sdi, data, ssm, (parity == VMXF_A429_PARITY_ODD) ? "ODD" : "EVEN");
if (!rc)
rc = mxfA429NextDataRecordPtrGet(recPtr, &recPtr);
}
}
}
if (rc)
printf("Acquisition read failed; rc=0x%08x\n", rc);
return rc;
}
Updated 10/23/2023