MX Foundation 4
Updating Periodic Messages

While a schedule runs, the CAN data can be updated asynchronously by the main application using the mxfCanBusTxPeriodicUpdateMsgWrite() function.

The next example shows how to use some functions for updating a transmission buffer.

Scheduling a Single Message

The program below shows how to define a single record with mxfTxPeriodicScheduleSingleMsgAdd() to schedule what will be later updated while the schedule runs.

The example in the next section shows how to update the records dynamically.

#define BUFFER_SIZE 4096 // 4KB
uint32 rc;
HMXF_SCHED schedule=0;
HMXF_SCHED_MSG msgSched;
HMXF_CHANNEL channel;
HMXF_BUFFER txBuffer=0;
MXF_CANBUS_DATAREC *hostBuffer=NULL;
...
// Allocates host buffer
if(!rc)
{
hostBuffer = (MXF_CANBUS_DATAREC *)malloc(BUFFER_SIZE);
if(!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Sets the Periodic Scheduler
if(!rc)
rc = mxfTxPeriodicScheduleNew(channel, &schedule);
// Sets scheduling values: Rate=25 ms, Phase=0 us
if(!rc)
rc = mxfTxPeriodicScheduleSingleMsgAdd(schedule, 25000000ll, 0, txBuffer, &msgSched);
// Runs the schedule
if(!rc)
rc = mxfTxPeriodicScheduleRun(schedule);
// Sends messages for 2 seconds
if(!rc)
{
printf("Running periodic transmission...\n\r");
mxfSleep(2000);
rc = mxfTxPeriodicScheduleFree(schedule);
}
...

Updating the buffer

The value of the data sent (CAN data) can be updated while a background schedule runs.

The example below shows how the mxfCanBusTxPeriodicUpdateMsgWrite() function can be used to perform the update operation.

#define TXALMOSTFULL 7
uint32 rc=0;
uint32 i;
MXF_CANBUS_DATAREC *recPtr=hostBuffer;
static uint64 data=0;
...
// Refills the FIFO in order to produce a ramp
for(i=0; !rc && i<TXALMOSTFULL; i++)
{
recPtr->timeTag=0;
recPtr->control=0;
recPtr->repeatCount=1;
recPtr->reserved=0;
rec->id = 0x080A0000;
rec->info = MXF_CANBUS_REC_INFO_EXTENDED | 8;
for (i = 0; i<rec->dataSize; i++)
rec->data[i] = (uint8)data;
data++;
rc = mxfCanBusNextDataRecordPtrGet(recPtr, &recPtr);
}
// Adds more data to the buffer
if(!rc)
rc = mxfCanBusTxPeriodicUpdateMsgWrite(buffer, i, hostBuffer);
...
Updated 10/23/2023