MX Foundation 4
csdb_periodic.c
/******************************************************************************
//
// File:
// csdb_periodic.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
// based on the Collins Standard Digital Bus (CSDB) protocol.
//
// Hardware requirements:
// - MAXT Flex with loopback between first TX and RX CSDB Enhanced channels.
//
*******************************************************************************/
#include "example.h"
#define LOOPBACK
#define LOCAL
#define BUFFER_SIZE 4096 // 4KB
#define TX_MSG_LABEL 5
#define TX_MSG_SI 0
#define BLOCKCOUNT 6
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_CSDB_DATAREC *recCsdb);
uint32 PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER *txBuffer, MXF_CSDB_DATAREC *recCsdb);
uint32 InitCSDBProtocolHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
int main(void)
{
uint32 rc;
HMXF_SERVER server;
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[2]={0,0};
MXF_CSDB_DATAREC *rec=NULL;
uint64 dev, mod, port, indexBuffer;
// Connect to services local or remote
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
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");
// initialize init callback handler to set TX and RX channel to CSDB
rc = mxfSystemInitAttributeUint64CallbackHandler(server, &InitCSDBProtocolHandler);
if(!rc)
rc = mxfSystemInit(server);
// Get the first device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_ASYNC_EH, 1, &count, &module);
// Obtain the first CSDB Protocol RX channel (RX logical #0)
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_CSDB, MXF_SCLASS_RX_CHANNEL, 1, &count, &rxChannel);
// Obtain the first CSDB Protocol TX channel (TX logical #0)
if (!rc && count)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_CSDB, 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 CSDB block count
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
if(!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
// 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=%"PRIu64".%"PRIu64".%"PRIu64"\n", dev, mod, port);
}
if (!rc)
{
rc = mxfChannelLocationGet(txChannel, &dev, &mod, &port);
if (!rc)
printf("Transmitter Channel (TX) location=%"PRIu64".%"PRIu64".%"PRIu64"\n", dev, mod, port);
}
//Activate loopback before transmission and reception
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_CSDB_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 (1MiB)
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, 0xA5, BUFFER_SIZE, &txBuffer[0], NULL);
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, TX_MSG_LABEL, BUFFER_SIZE, &txBuffer[1], NULL);
// Allocate host buffer
if (!rc)
{
rec = (MXF_CSDB_DATAREC*)malloc(BUFFER_SIZE);
if (!rec)
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 data transmission
if (!rc)
{
rc = PeriodicScheduling(txChannel, txBuffer, rec);
if (!rc)
{
// Read data in acquisition buffer
rc = ReadAcquisitionData(rxBuffer, rec);
}
}
// 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 (rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
for (indexBuffer = 0; indexBuffer < 2; indexBuffer++)
{
if (txBuffer[indexBuffer])
{
mxfTxPeriodicUpdateMsgBufferFree(txBuffer[indexBuffer]);
}
}
if (rec)
free(rec);
printf("\nPress enter to terminate\n");
getchar();
return rc;
}
/***************************************************************************************************************/
// ReadAcquisitionData
/***************************************************************************************************************/
uint32 ReadAcquisitionData(HMXF_BUFFER rxBuffer, MXF_CSDB_DATAREC *recCsdb)
{
MXF_CSDB_DATAREC* rec=recCsdb;
uint64 status, msgsCount, bytesCount;
uint64 i, j;
uint32 rc;
// Read and display records
rc = mxfCSDBRxAcqRead(rxBuffer, 0, BUFFER_SIZE, &status, &msgsCount, &bytesCount, recCsdb);
for (j=0; j<msgsCount && !rc; j++)
{
printf("%02"PRIu64": Timetag=%016"PRIu64": CSDB data=[ ", j, rec->timeTag);
for (i=0; i<BLOCKCOUNT; i++)
printf("0x%02X ", rec->data[i]);
printf("]\n\r");
}
return rc;
}
/***************************************************************************************************************/
// PeriodicScheduling
/***************************************************************************************************************/
uint32 PeriodicScheduling(HMXF_CHANNEL txChannel, HMXF_BUFFER *txBuffer, MXF_CSDB_DATAREC *recCsdb)
{
HMXF_SCHED schedule;
HMXF_SCHED_MSG msg=0;
uint32 rc;
uint64 i;
// 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 records for the buffers
// Set the sync block
if(!rc)
{
recCsdb->timeTag = 0;
recCsdb->control = 0;
recCsdb->repeatCount = 1;
recCsdb->reserved = 0;
for (i=0; i<BLOCKCOUNT; i++)
recCsdb->data[i] = 0xa5;
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[0], 1, recCsdb);
}
// Set the message block
if(!rc)
{
recCsdb->timeTag = 0;
recCsdb->control = 0;
recCsdb->repeatCount = 1;
recCsdb->data[0] = TX_MSG_LABEL;
recCsdb->data[1] = TX_MSG_SI;
for (i=2; i<BLOCKCOUNT; i++)
recCsdb->data[i]= (uint8)i;
recCsdb->reserved=0;
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[1], 1, recCsdb);
}
// Run the scheduler, update records
if(!rc)
{
printf("Running periodic transmission, please wait...\n\r");
// Run the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Wait 5 seconds
if(!rc)
{
mxfSleep(5000);
rc = mxfTxPeriodicScheduleFree(schedule);
}
if(!rc)
printf("\n\rTransmission stopped\n\r");
return rc;
}
//******************************************************************************
// Initialize CSDB protocol
//******************************************************************************
uint32 InitCSDBProtocolHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value)
{
server=server;
deviceIndex=deviceIndex;
moduleIndex=moduleIndex;
channelIndex=channelIndex;
// Set the device in CSDB mode
if(attrib==KMXF_CHANNEL_CLASS)
{
*value=MXF_CLASS_CSDB;
return TRUE;
}
return FALSE;
}
Updated 10/23/2023