MX Foundation 4
ar429_rx_sampling.c
/****************************************************************************************************************
//
// File:
// ar429_rx_sampling.c
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This demo is a basic program for using the MX API sampling services
// for receiving data. In this example, message selection is used to
// discriminate data received.
//
// Hardware Requirements:
// - MAXT Flex or PCI-500 carrier
// - Loopback between first TX and RX ARINC 429 Enhanced channels.
//
*****************************************************************************************************************/
#include "../../inc/example.h"
//#define LOOPBACK
//#define LOCAL
#define BUFFER_SIZE 4096 // 4KB
#define LABEL 3
#define SDI 0
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device = 0;
HMXF_MODULE module = 0;
HMXF_CHANNEL rxChannel = 0, txChannel = 0;
HMXF_BUFFER rxBuffer = 0, txBuffer[2];
uint64 count = 0, type = 0;
MXF_A429_SAMPREC* hostBuffer = NULL;
HMXF_SCHED schedule = 0;
HMXF_SCHED_MSG msg = 0;
uint64 i, j;
uint64 msgsCount, bytesCount, indexBuffer;
uint64 label = LABEL, sdi, data, ssm, parity;
// 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 device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// 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, NULL, &module);
if (!rc)
rc = mxfAttributeUint64Get(module, KMXF_MODULE_TYPE, &type);
// If Ipm-429
if (!rc && type == MXF_MODULE_A429E)
{
// Set the channels to low speed
if (!rc)
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
{
// Set the channels to low speed
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_A429_SPEED, 12500);
if (!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_A429_SPEED, 12500);
}
// Enable loopback
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Set timebase to 64-bit microseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Alloc host buffer
if (!rc)
{
hostBuffer = (MXF_A429_SAMPREC*)malloc(BUFFER_SIZE);
if (!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Allocate RX sampling buffer
if (!rc)
rc = mxfRxSamplingBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Filter out a specific label
if (!rc)
rc = mxfA429RxSamplingMsgSelectSet(rxBuffer, MXF_MSG_DESELECT, 1, &label);
// Start sampling
if (!rc)
rc = mxfRxSamplingStart(rxBuffer);
if (!rc)
printf("Sampling started\n\r");
// Allocate TX Periodic Update buffer
if (!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, LABEL, BUFFER_SIZE, &txBuffer[0], NULL);
if (!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, LABEL + 1, BUFFER_SIZE, &txBuffer[1], NULL);
// Create the periodic scheduler
if (!rc)
rc = mxfTxPeriodicScheduleNew(txChannel, &schedule);
// Set scheduling values: Rate=100 ms (.1sec), Phase=0 us
if (!rc)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 100000LL, 0, &msg);
// Define the number of buffer for the list and link to it
if (!rc)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 2, 0, txBuffer);
// Set record for the buffer, will be ignored by sampling
if (!rc)
{
rec429.timeTag = 0;
rec429.control = 0;
rec429.repeatCount = 1;
rec429.reserved = 0;
rc = mxfA429ArwCompose(LABEL, SDI, 0x7fe, 1, VMXF_A429_PARITY_ODD, &rec429.data);
if (!rc)
rc = mxfA429TxPeriodicUpdateMsgWrite(txBuffer[0], 1, &rec429);
}
// Set a record that will be received by sampling
if (!rc)
{
rec429.timeTag = 0;
rec429.control = 0;
rec429.repeatCount = 1;
rec429.reserved = 0;
rc = mxfA429ArwCompose(LABEL + 1, SDI, 0x12, 0, VMXF_A429_PARITY_ODD, &rec429.data);
if (!rc)
rc = mxfA429TxPeriodicUpdateMsgWrite(txBuffer[1], 1, &rec429);
}
// Run the scheduler, update records
if (!rc)
{
printf("Running periodic transmission, please wait...\n\r");
// Run the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Read and display records for 5 seconds
for (i = 0; i < 10 && !rc; i++)
{
rc = mxfA429RxSamplingRead(rxBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, 0, BUFFER_SIZE, &msgsCount, &bytesCount, hostBuffer);
if (!rc)
{
printf("Read %llu messages\n\r", msgsCount);
recSamp = (MXF_A429_SAMPREC*)hostBuffer;
for (j = 0; !rc && j < msgsCount; j++)
{
rc = mxfA429ArwDecompose(recSamp->data, &label, &sdi, &data, &ssm, &parity);
if (!rc)
{
printf("%02llu: Timetag %llu - ARINC word=[%03llo,%lld,%05llX,%lld,%s]\n",
j, recSamp->timeTag, label, sdi, data, ssm, (parity == VMXF_A429_PARITY_ODD) ? "ODD" : "EVEN");
if (!rc)
rc = mxfA429NextSamplingRecordPtrGet(recSamp, &recSamp);
}
}
}
if (rc)
printf("Sampling read failed; rc=0x%08x\n", rc);
else
mxfSleep(500);
}
// Stop transmission
if (!rc)
rc = mxfTxPeriodicScheduleFree(schedule);
// Stop sampling
if (!rc)
rc = mxfRxSamplingStop(rxBuffer);
// 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)
for (indexBuffer = 0; indexBuffer < 2; indexBuffer++)
{
if (txBuffer[indexBuffer])
mxfTxPeriodicUpdateMsgBufferFree(txBuffer[indexBuffer]);
}
// Terminate
if (hostBuffer)
free(hostBuffer);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
Updated 03/30/2022