MX Foundation 4
ar429_embedded_discrete.c
/*******************************************************************************
//
// File:
// ar429_embedded_discrete.c
//
// Copyright (c) MAX Technologies Inc. 1988-2015, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This demo shows how an ARINC 429 onboard application can transmit
// data when a triggered edge discrete input is received.
//
// The embedded program transmit one ARINC 429 label 004 at each discrete input #0
// rising edge detected. The transmission is sent on timetag, 25 msec
// later of discrete edge timetag.
//
// The embedded program "ar429_embedded_side_discrete.c" is required by this program
// and compiled executable (ar429_embedded_side_discrete-flex.trap) must be located in
// the same folder as the host application executable.
//
// Hardware Requirements:
// - MAXT Flex with loopback between first TX and RX Discrete pin and one TX ARINC 429 Enhanced channel.
//
*******************************************************************************/
#include "../../inc/example.h"
//#define LOOPBACK
//#define LOCAL
#define BUFFER_SIZE 4096 // 4KB
// User Commands
#define USER_COMMAND_ID_START 0
#define USER_COMMAND_ID_STOP 1
#define C50ms 50ll*1000ll*1000ll
#define C10ms 10ll*1000ll*1000ll
uint32 EmbeddedMemoryAllocationHandler(HMXF_SERVER server, uint64 cardIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
uint32 TXDioSendAperiodicPulses(HMXF_DEVICE device, HMXF_BUFFER txDIOBuffer);
uint32 RX429ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A429_DATAREC *rec429);
/******************************************************************************/
// main
/******************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
HMXF_DEVICE device=0;
HMXF_MODULE moduleDIO=0;
HMXF_MODULE module429=0;
uint64 count=0;
HMXF_CHANNEL txDIO=0, rxDIO=0, tx429=0, rx429=0;
HMXF_BUFFER txDIOBuffer=0;
HMXF_BUFFER tx429Buffer=0;
HMXF_BUFFER rx429Buffer=0;
uint64 dev, mod=0, port=0;
uint32 params[3];
MXF_A429_DATAREC *rec429=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 library
printf("\nStarting\n");
rc = mxfSystemInitAttributeUint64CallbackHandler(server, EmbeddedMemoryAllocationHandler);
if(!rc)
rc = mxfSystemInit(server);
// Get the first device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
// Get first module discrete
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_DIOFIFO_EH, 1, &count, &moduleDIO);
// Get first discrete TX channel
if(!rc && count)
rc = mxfModuleChannelAllGet(moduleDIO, MXF_CLASS_DISCRETE, MXF_SCLASS_TX_CHANNEL, 1, &count, &txDIO);
// Get first discrete RX channel
if(!rc && count)
rc = mxfModuleChannelAllGet(moduleDIO, MXF_CLASS_DISCRETE, MXF_SCLASS_RX_CHANNEL, 1, &count, &rxDIO);
// If discrete channel not found, return an error
if(!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Get first module ARINC 429
if(!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A429_EH, 1, &count, &module429);
// Get the first ARINC 429 TX channel
if (!rc && count)
rc = mxfModuleChannelAllGet(module429, MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, 1, &count, &tx429);
// Get the first ARINC 429 RX channel
if (!rc && count)
rc = mxfModuleChannelAllGet(module429, MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, 1, &count, &rx429);
// If A429 channel not found, return an error
if(!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Set timebase to 64-bit microseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Enable discrete pin #0 rising edge detection
if (!rc)
rc = mxfAttributeUint64Set(rxDIO, KMXF_DISCRETE_RX_EDGE_FALLING, 0x0000);
if (!rc)
rc = mxfAttributeUint64Set(rxDIO, KMXF_DISCRETE_RX_EDGE_RISING, 0x0001);
//Activate loopback before transmission and reception
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(rxDIO, KMXF_DISCRETE_TX_RX_TEST_LB, VMXF_ENABLE);
if (!rc)
rc = mxfAttributeUint64Set(rx429, KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate Aperiodic TX static buffer for HIGH priority queue
if (!rc)
rc = mxfTxAperiodicBufferAlloc(txDIO, MXF_TXAPERIODIC_PRIORITY_HIGH, BUFFER_SIZE, &txDIOBuffer, NULL);
if (!rc)
rc = mxfTxAperiodicBufferAlloc(tx429, MXF_TXAPERIODIC_PRIORITY_HIGH, BUFFER_SIZE, &tx429Buffer, NULL);
// Allocate RX A429 acquisition buffer
if (!rc)
rc = mxfRxAcqBufferAlloc(rx429, BUFFER_SIZE, &rx429Buffer, NULL);
// Allocate host buffer
if (!rc)
{
rec429 = (MXF_A429_DATAREC*)malloc(BUFFER_SIZE);
if (!rec429)
rc = MAXT_ERROR_MEM;
}
// Start the acquisition process
if (!rc)
rc = mxfRxAcqModeSet(rx429Buffer, MXF_RXACQ_MODE_LINEAR);
if (!rc)
{
rc = mxfRxAcqStart(rx429Buffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
printf("\nAcquisition started\n\r");
}
// Download from the host (Client Side) the embedded application
if (!rc)
rc = mxfEmbeddedCodeDownload(device, "ar429_embedded_side_discrete-flex.trap");
// Enable Embedded Handler
if(!rc)
rc = mxfEmbeddedHandlerEnableSet(rxDIO, MXF_EMBEDDED_HANDLER_ID_RX, TRUE, 0);
if(!rc)
rc = mxfChannelLocationGet(tx429, &dev, &mod, &port);
// Set the parameters to pass
if (!rc)
{
params[0] = (uint32)mod;
params[1] = (uint32)port;
params[2] = MXF_TXAPERIODIC_PRIORITY_HIGH;
// Start the embedded code
rc = mxfEmbeddedCommandSend(device, USER_COMMAND_ID_START, sizeof(params), params);
}
if(!rc)
{
printf("Sending Periodic Pulses\n");
rc = TXDioSendAperiodicPulses(device, txDIOBuffer);
}
// Stop the embedded code
if (!rc)
rc = mxfEmbeddedCommandSend(device, USER_COMMAND_ID_STOP, 0, NULL);
// Stop and read A429 acquisition
if (!rc)
rc = mxfRxAcqStop(rx429Buffer);
if (!rc)
rc = RX429ReadAcquisitionData(rx429Buffer, rec429);
// 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);
}
printf("Stopping\n");
//Frees all buffers
if (tx429Buffer)
if (txDIOBuffer)
if (rx429Buffer)
mxfRxAcqBufferFree(rx429Buffer);
if (rec429)
free(rec429);
// Terminate
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
/******************************************************************************/
// EmbeddedMemoryHandler
/******************************************************************************/
uint32 EmbeddedMemoryAllocationHandler(HMXF_SERVER server, uint64 cardIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value)
{
// Allocate the maximum space available for the embedded application
uint64 embedded_memory_size=65536; // 64K
(void)server;
(void)cardIndex;
(void)moduleIndex;
(void)channelIndex;
if (attrib==KMXF_DEVICE_EMBEDDED_CODEANDDATA_SIZE)
{
printf("Reserving %lluK of memory for embedded application\n", embedded_memory_size/1024);
*value = embedded_memory_size;
return TRUE;
}
return FALSE;
}
/******************************************************************************/
// TXDioSendAperiodicPulses
/******************************************************************************/
uint32 TXDioSendAperiodicPulses(HMXF_DEVICE device, HMXF_BUFFER txDIOBuffer)
{
uint64 timer;
uint64 pulseCount;
uint32 rc;
// Set DIO record
rc=mxfDeviceTimerGet(device, &timer);
if (rc)
return rc;
timer += C50ms;
p = dioRec;
p->timeTag = timer;
p->control = 0;
p->repeatCount = 1;
p->highDuration = 0;
p->lowDuration = 0;
p->data = 0x0001;
p->edge = 0xFFFF;
timer+=C10ms;
p->timeTag = timer;
p->control = 0;
p->repeatCount = 1;
p->highDuration = 0;
p->lowDuration = 0;
p->data = 0x0000;
p->edge = 0xFFFF;
// Simulate 10 pulses on discrete output (10 msec high, 50 msec low)
for (pulseCount=0; !rc && pulseCount<10; pulseCount++)
{
rc=mxfDiscreteTxAperiodicWrite(txDIOBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, 2, dioRec);
//rc=mxfDiscreteTxAperiodicWrite(txDIOBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, 1, &dioRec[pulseCount%2]);
if (rc)
{
printf("mxfDiscreteTxAperiodicWrite failed ! rc=0x%08x\n (pulse=%llu)", rc, pulseCount);
break;
}
timer += C50ms;
p = dioRec;
p->timeTag = timer;
p->control = 0;
timer += C10ms;
p->timeTag = timer;
p->control = 0;
}
// Wait TX completion
mxfSleep(1000);
return rc;
}
/***************************************************************************************************************/
// RX429ReadAcquisitionData
/***************************************************************************************************************/
uint32 RX429ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A429_DATAREC *rec429)
{
MXF_A429_DATAREC* rec=rec429;
uint64 status, msgsCount, bytesCount;
uint64 label, sdi, data, ssm, parity;
uint64 j;
uint32 rc;
// Read and display records
rc = mxfA429RxAcqRead(rxBuffer, 0, BUFFER_SIZE, &status, &msgsCount, &bytesCount, rec429);
for (j=0; j<msgsCount && !rc; j++)
{
mxfA429ArwDecompose(rec->data, &label, &sdi, &data, &ssm, &parity);
printf("%02llu: Timetag %llu - ARINC word=[%lld,%lld,%05llX,%lld,%03llo]\n",
j, rec->timeTag, parity, ssm, data, sdi, label);
}
return rc;
}
Updated 03/30/2022