MX Foundation 4
ar429_rx_event_handler.c
/****************************************************************************************************************
//
// File:
// arinc429_rx_event_handler.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 shows how to set a basic periodic transmission.
// The receiver handler get data on specific label/sdi.
//
// 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 4096 // 4KB
#define LABEL 5
#define SDI 0
uint32 RXmsgTotal = 0;
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param);
uint32 TX429PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER* txBuffer, MXF_A429_DATAREC* rec429);
/***************************************************************************************************************/
// 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[2];
MXF_ASYNCEVENT_CONDITION asyncEventInfo[2];
uint64 count=0, type=0, indexBuffer;
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 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 nanoseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// 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, NULL, &asyncEvent);
// Allocate RX sampling buffer
if (!rc)
rc = mxfRxSamplingBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Set the RX async event conditions
if (!rc)
{
asyncEventInfo[0].condID = MXF_ASYNCEVENT_COND_RX_MSG;
asyncEventInfo[0].condition.rxMsg.channel = rxChannel;
asyncEventInfo[1].condID = MXF_ASYNCEVENT_COND_RX_ERROR;
asyncEventInfo[1].condition.rxErr.channel = rxChannel;
rc = mxfAsyncEventConditionsSet(asyncEvent, TRUE, 2, &asyncEventInfo[0]);
}
// Monitor a specific label/sdi
if (!rc)
{
msgid.label = LABEL;
msgid.sdi = SDI;
msgid.reserved[0] = 0;
msgid.reserved[1] = 0;
rc = mxfA429AsyncEventRxMsgSelectSet(asyncEvent, rxChannel, MXF_MSG_SELECT_ADD, 1, &msgid);
}
// 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);
// Set the Periodic Scheduler
if (!rc)
TX429PeriodicScheduling(txChannel, txBuffer, hostBuffer);
// Stop sampling
if (!rc)
rc = mxfRxSamplingStop(rxBuffer);
// Disable conditions
if (!rc)
rc = mxfAsyncEventConditionsSet(asyncEvent, FALSE, 2, asyncEventInfo);
// 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)
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;
}
//****************************************************************************************************************
// RX asynchronous event Handler
//****************************************************************************************************************
uint32 asyncEventHandler(HMXF_ASYNCEVENT asyncEvent, void* param)
{
HMXF_CHANNEL channel;
uint64 pendingCount;
uint64 label, sdi, status;
uint64 i;
uint32 rc = MAXT_SUCCESS;
uint64 dev, mod, port;
HMXF_BUFFER sampBuffer;
(void)param;
// Build the list of pending events to process
rc = mxfAsyncEventPendingGet(asyncEvent, 64, &pendingCount, pendingList);
for (i = 0; !rc && i < pendingCount; i++)
{
switch (pendingList[i].condID)
{
// An event was generated when the data on the specific (label/sdi) was received.
case MXF_ASYNCEVENT_COND_RX_MSG:
{
channel = pendingList[i].condition.rxMsg.channel;
label = pendingList[i].condition.rxMsg.msg.a429.label;
sdi = pendingList[i].condition.rxMsg.msg.a429.sdi;
memset(&rec, 0, sizeof(rec));
// Get latest value
rc = mxfRxSamplingBufferGet(channel, &sampBuffer);
if (!rc)
rc = mxfA429RxSamplingSingleRead(sampBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, label, sdi, &rec);
if (!rc)
printf("Msg %02d received with label=%llu sdi=%llu (0x%08x)\n", RXmsgTotal++, label, sdi, rec.data);
if (rc)
printf("Error getting latest value rc = 0x%08x\n", rc);
break;
}
case MXF_ASYNCEVENT_COND_RX_ERROR:
channel = pendingList[i].condition.rxErr.channel;
status = pendingList[i].condition.rxErr.status;
mxfChannelLocationGet(channel, &dev, &mod, &port);
printf("Status 0x%08llx received on channel %llu.%llu.%llu\n", status, dev, mod, port);
break;
default:
printf("Unknown condID 0x%llx)", pendingList[i].condID);
break;
}
}
return rc;
}
/***************************************************************************************************************/
// TX429PeriodicScheduling
/***************************************************************************************************************/
uint32 TX429PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER* txBuffer, MXF_A429_DATAREC* rec429)
{
HMXF_SCHED schedule;
HMXF_SCHED_MSG msg = 0;
uint64 label, sdi, data, ssm, parity;
uint32 rc;
// Set the A429 records and create the schedule
// Create the periodic scheduler
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: ARINC 429 LABEL=05, SDI=0
if (!rc)
{
rec429->timeTag = 0;
rec429->control = 0;
rec429->repeatCount = 1;
rec429->reserved = 0;
label = LABEL;
sdi = SDI;
data = 0x7fe;
parity = VMXF_A429_PARITY_ODD;
ssm = 1;
rc = mxfA429ArwCompose(label, sdi, data, ssm, parity, &rec429->data);
if (!rc)
rc = mxfA429TxPeriodicUpdateMsgWrite(txBuffer[0], 1, rec429);
}
// Set a record that will be ignored later on by the event handler
if (!rc)
{
rec429->timeTag = 0;
rec429->control = 0;
rec429->repeatCount = 1;
rec429->reserved = 0;
label = LABEL + 1;
sdi = SDI;
data = 0x12;
parity = VMXF_A429_PARITY_ODD;
ssm = 1;
rc = mxfA429ArwCompose(label, sdi, data, ssm, parity, &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);
}
// Wait 2 seconds
if (!rc)
{
mxfSleep(2000);
rc = mxfTxPeriodicScheduleFree(schedule);
}
if (!rc)
printf("\n\rTransmission stopped\n\r");
return rc;
}
Updated 10/23/2023