MX Foundation 4
usb.c
/*******************************************************************************
//
// File:
// usb.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 to set a basic periodic transmission with the
// scheduler and update the ARINC 429 data over USB connection.
//
// Hardware Requirements:
// - MAXT FlexMulti with loopback between first TX and RX ARINC 429 Enhanced channels.
//
*******************************************************************************/
#include "example.h"
#define BUFFER_SIZE 4096 // 4KB
#define TX_MSG_LABEL 5
#define TX_MSG_SDI 0
uint32 RX429ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A429_DATAREC *rec429);
uint32 TX429PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER txBuffer, MXF_A429_DATAREC *rec429);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server=0;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
uint64 count=0;
HMXF_CHANNEL rxChannel=0;
HMXF_CHANNEL txChannel=0;
HMXF_BUFFER rxBuffer=0;
HMXF_BUFFER txBuffer=0;
MXF_A429_DATAREC *rec429=NULL;
uint64 dev, mod, port;
uint64 usbDevCount;
// Detect MAXT USB device connected
rc = mxfSystemUSBDetect(&usbDevCount);
if(!rc && !usbDevCount)
{
printf("No MAXT USB device detected\n");
rc = MAXT_ERROR_DEVICE_NOT_DETECTED;
}
// Connect to the first USB device
if(!rc)
rc = mxfSystemUSBServerConnect(0, &server);
if(rc)
{
printf("Failed to connect; rc=0x%08x", rc);
printf("\nPress a key to terminate\n");
getchar();
return 0;
}
// Initialize the server
printf("\nStarting\n");
if(!rc)
rc = mxfSystemInit(server);
// Get the first device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A429_EH, 1, &count, &module);
// Obtain the first ARINC 429 Protocol RX channel (RX logical #0)
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, 1, &count, &rxChannel);
// Obtain the first ARINC 429 Protocol TX channel (TX logical #0)
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, 1, &count, &txChannel);
// If module or 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_USEC);
// Get the physical port location
if (!rc)
{
rc = mxfChannelLocationGet(rxChannel, &dev, &mod, &port);
if (!rc)
printf("Acquisition Channel (RX) location=%llu.%llu.%llu\n", dev, mod, port);
}
if (!rc)
{
rc = mxfChannelLocationGet(txChannel, &dev, &mod, &port);
if (!rc)
printf("Transmitter Channel (TX) location=%llu.%llu.%llu\n", dev, mod, port);
}
//Activate loopback before transmission and reception
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate RX acquisition buffer
if (!rc)
rc = mxfRxAcqBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Allocate TX Periodic Update buffer
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, TX_MSG_LABEL, BUFFER_SIZE, &txBuffer, 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(rxBuffer, MXF_RXACQ_MODE_LINEAR);
if (!rc)
{
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (!rc)
printf("\nAcquisition started\n\r");
}
// Start 429 data transmission
if (!rc)
{
rc = TX429PeriodicScheduling(txChannel, txBuffer, rec429);
if (!rc)
{
// Read 429 data in acquisition buffer
rc = RX429ReadAcquisitionData(rxBuffer, rec429);
}
}
// Stop and flush unread data
if (!rc)
rc = mxfRxAcqStop(rxBuffer);
if (!rc)
{
rc = mxfRxAcqClear(rxBuffer);
if (!rc)
printf("\nAcquisition stopped\n\r");
}
// Catch any previous error
if (rc)
{
char buffer[256];
rc = mxfSystemErrorStringGet(server, rc, sizeof(buffer), buffer);
printf("%s\n", buffer);
}
printf("\nTerminating\n");
// Free all buffers and terminate
if (device)
if (rec429)
free(rec429);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
/***************************************************************************************************************/
// RX429ReadAcquisitionData
/***************************************************************************************************************/
uint32 RX429ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_A429_DATAREC *rec429)
{
MXF_A429_DATAREC* recPtr=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++)
{
rc = mxfA429ArwDecompose(recPtr->data, &label, &sdi, &data, &ssm, &parity);
if(!rc)
{
printf("%02llu: Timetag %llu - ARINC word=[%03llo,%lld,%05llX,%lld,%s]\n",
j, recPtr->timeTag, label, sdi, data, ssm, (parity==VMXF_A429_PARITY_ODD)?"ODD":"EVEN");
if(!rc)
rc = mxfA429NextDataRecordPtrGet(recPtr, &recPtr);
}
}
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, 1, 0, &txBuffer);
// Set record for the buffer: ARINC 429 LABEL=005, SDI=0
if(!rc)
{
rec429->timeTag = 0;
rec429->control = 0;
rec429->repeatCount = 1;
rec429->reserved = 0;
label = TX_MSG_LABEL;
sdi = TX_MSG_SDI;
data = 0x7fe;
parity = VMXF_A429_PARITY_ODD;
ssm = 1;
rc = mxfA429ArwCompose(label, sdi, data, ssm, parity, &rec429->data);
if(!rc)
rc = mxfA429TxPeriodicUpdateMsgWrite(txBuffer, 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 10 seconds
if(!rc)
{
mxfSleep(10000);
rc = mxfTxPeriodicScheduleFree(schedule);
}
if(!rc)
printf("\n\rTransmission stopped\n\r");
return rc;
}
Updated 10/23/2023