M1553 MT Monitor FIFO
Edit this on GitLab
M1553 MT Monitor FIFO
Explanation
About the Sample Application Code
This C application code from North Atlantic Industries (NAI) provides an example of how to use the Software Support Kit (SSK) to interact with their embedded function modules, specifically focusing on the 1553 channel for message monitoring. Below is a detailed explanation of the application, its components, and functionality to guide you through the code.
Files and Libraries Included
-
<stdio.h>
,<stdlib.h>
,<string.h>
,<time.h>
: Standard C libraries for basic input-output operations, memory manipulation, and time tracking. -
"include/naiapp_boardaccess_menu.h"
,"include/naiapp_boardaccess_query.h"
,"include/naiapp_boardaccess_access.h"
,"include/naiapp_boardaccess_display.h"
,"include/naiapp_boardaccess_utils.h"
: NAI-provided header files for board access utilities and menus. -
"nai_1553_utils.h"
: Utility functions for the 1553 module. -
"nai.h"
,"naibrd.h"
,"functions/naibrd_1553.h"
,"functions/naibrd_1553_assisted.h"
: Core header files for interacting with NAI boards and 1553-specific functions.
Constants and Macros
-
CONFIG_FILE
: Default configuration file for the 1553 message monitor. -
DEF_MT_CHANNEL
,DEF_MT_DEV_NUM
: Default settings for the 1553 channel and device number. -
RT_DISABLE
,SA_DISABLE
: Constants used to disable monitoring for specific message types.
Function Prototypes
-
Run_M1553_MT_Monitor_FIFO
: Main function to configure and run the 1553 message monitor. -
MonitorMessages
: Function to monitor and display incoming 1553 messages.
Main Function
Initialization and Configuration
int32_t main(void) {
...
if (naiapp_RunBoardMenu(CONFIG_FILE) == TRUE) {
while (stop != TRUE) {
stop = naiapp_query_CardIndex(naiapp_GetBoardCnt(), 0, &cardIndex);
if (stop != TRUE) {
check_status(naibrd_GetModuleCount(cardIndex, &moduleCnt));
stop = naiapp_query_ModuleNumber(moduleCnt, 1, &module);
if (stop != TRUE) {
moduleID = naibrd_GetModuleID(cardIndex, module);
if ((moduleID != 0)) {
Run_M1553_MT_Monitor_FIFO(cardIndex, module, moduleID);
}
}
}
printf("\nType Q to quit or Enter key to restart application:\n");
stop = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
}
}
printf("\nType the Enter key to exit the program: ");
naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
naiapp_access_CloseAllOpenCards();
return 0;
}
Functionality
-
Configurations and Menu Setup: The function
naiapp_RunBoardMenu
initiates a configuration menu based on the provided CONFIG_FILE. -
User Query: Queries user for the card index, module number, and retrieves the module ID.
-
1553 Monitor Routine: Calls
Run_M1553_MT_Monitor_FIFO
with the user-selected parameters to start the monitoring process.
Run_M1553_MT_Monitor_FIFO Function
This function configures the 1553 message monitor and starts message monitoring.
static bool_t Run_M1553_MT_Monitor_FIFO(int32_t cardIndex, int32_t module, uint32_t modid) {
...
/* Get Configuration */
bQuit = Get1553MTCfg(modid, DEF_MT_CHANNEL, &mtchan);
bQuit = Get1553LogicalDevNum(DEF_MT_DEV_NUM, &DevNum);
swResult = naibrd_1553_Open(cardIndex, module, mtchan, DevNum);
swResult = naibrd_1553_Initialize(DevNum, NAI_1553_ACCESS_CARD, NAI_1553_MODE_MT | NAI_1553_MESSAGE_FIFO_MODE, 0, 0, 0);
swResult = naibrd_1553_MtMessageMonitoringDisable(DevNum, RT_DISABLE, NAI_1553_MT_FILTER_ALL, 1 << SA_DISABLE);
...
while (bContinue) {
...
swResult = naibrd_1553_ClearMsgFIFO(DevNum);
swResult = naibrd_1553_MtStart(DevNum);
MonitorMessages(DevNum, duration);
swResult = naibrd_1553_MtStop(DevNum);
...
}
swResult = naibrd_1553_Free(DevNum);
return bQuit;
}
Functionality
-
Configuration Setup:
-
Get1553MTCfg
andGet1553LogicalDevNum
: Retrieve configurations for the 1553 channel and logical device number. -
naibrd_1553_Open
: Open the 1553 device with the specified parameters. -
naibrd_1553_Initialize
: Initialize the 1553 device in monitor mode with FIFO enabled. -
naibrd_1553_MtMessageMonitoringDisable
: Disable monitoring for specific message types (RT 1, SA 3).
-
-
Monitoring Process:
-
Queries user for monitoring duration.
-
Clears the message FIFO.
-
Starts the message monitor (
naibrd_1553_MtStart
). -
Calls
MonitorMessages
to process and display 1553 messages. -
Stops the message monitor (
naibrd_1553_MtStop
).
-
-
Resource Cleanup:
-
Frees the 1553 device (
naibrd_1553_Free
).
-
MonitorMessages Function
This function monitors and displays received 1553 messages.
static int MonitorMessages(uint16_t DevNum, int32_t duration) {
...
while (time(NULL) < end) {
swResult = naibrd_1553_GetMsgFIFOCount(DevNum, &fifoCount);
if (fifoCount > 0) {
memset(fifoData, 0, sizeof(fifoData));
swResult = naibrd_1553_ReadMsgFIFO(DevNum, fifoCount, fifoData);
do {
swResult = naibrd_1553_DecodeFIFOMsg(fifoData, currBlockIndex, &nextBlockIndex, &msgStruct);
if ((msgStruct.wCommandWord1 & 0x0400) != 0x0400) {
printf("Rx Msg Received\n");
...
} else {
printf("Tx Msg Received\n");
...
}
currBlockIndex = nextBlockIndex;
nai_msDelay(1);
} while ((swResult == NAI_SUCCESS) && (time(NULL) < end));
swResult = naibrd_1553_GetMsgFIFOFullStatus(DevNum, NAI_1553_STATUS_REALTIME, &FIFOfullStatusBit);
...
}
}
return 1;
}
Functionality
-
FIFO Message Count:
-
Retrieves the count of messages in the FIFO (
naibrd_1553_GetMsgFIFOCount
).
-
-
Message Processing:
-
Reads messages from the FIFO and decodes them (
naibrd_1553_ReadMsgFIFO
,naibrd_1553_DecodeFIFOMsg
). -
Distinguishes between Rx and Tx messages and prints the relevant data.
-
-
FIFO Status Check:
-
Checks and prints whether the FIFO is full (
naibrd_1553_GetMsgFIFOFullStatus
).
-
Summary
The provided code offers a comprehensive example of how to configure and use NAI’s 1553 message monitor functionality. It includes initialization and setup procedures, user interaction for configuration, and continuous monitoring of 1553 messages, both receiving (Rx) and transmitting (Tx). The code is well-structured, making it adaptable for specific requirements and further customization.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Common Sample Program include files */
#include "include/naiapp_boardaccess_menu.h"
#include "include/naiapp_boardaccess_query.h"
#include "include/naiapp_boardaccess_access.h"
#include "include/naiapp_boardaccess_display.h"
#include "include/naiapp_boardaccess_utils.h"
#include "nai_1553_utils.h"
/* Common 1553 Sample Program include files */
#include "nai_1553_utils.h"
/* naibrd include files */
#include "nai.h"
#include "naibrd.h"
#include "functions/naibrd_1553.h"
#include "functions/naibrd_1553_assisted.h"
static const int8_t *CONFIG_FILE = (int8_t *)"default_1553_MTMonitor_FIFO.txt";
/* Function prototypes */
static bool_t Run_M1553_MT_Monitor_FIFO(int32_t cardIndex, int32_t module, uint32_t modid);
static int32_t MonitorMessages(uint16_t DevNum, int32_t duration);
static const int32_t DEF_MT_CHANNEL = 1;
static const int16_t DEF_MT_DEV_NUM = 1;
#define RT_DISABLE 1
#define SA_DISABLE 3
/**************************************************************************************************************/
/**
<summary>
The purpose of the M1553_MT_Monitor_FIFO is to illustrate the methods to call in the naibrd library to configure
and run the 1553 channel as a Message Monitor. If an Rx or TX message is detected on the bus, the received
data and message information will be displayed via message FIFO. This application also demonstrates message
filtering using the naibrd_1553_MtMessageMonitoringDisable() function to disable monitoring for certain
message types.
The following system configuration routines from the nai_sys_cfg.c file are called to assist with the configuration
setup for this program prior to calling the naibrd 1553 routines.
- ConfigDevice
- DisplayDeviceCfg
- GetBoardSNModCfg
- CheckModule
</summary>
*/
/**************************************************************************************************************/
#if defined (__VXWORKS__)
int32_t M1553_MT_Monitor_FIFO(void)
#else
int32_t main(void)
#endif
{
bool_t stop = FALSE;
int32_t cardIndex;
int32_t moduleCnt;
int32_t module;
uint32_t moduleID = 0;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
if (naiapp_RunBoardMenu(CONFIG_FILE) == TRUE)
{
while (stop != TRUE)
{
/* Query the user for the card index */
stop = naiapp_query_CardIndex(naiapp_GetBoardCnt(), 0, &cardIndex);
if (stop != TRUE)
{
check_status(naibrd_GetModuleCount(cardIndex, &moduleCnt));
/* Query the user for the module number */
stop = naiapp_query_ModuleNumber(moduleCnt, 1, &module);
if (stop != TRUE)
{
moduleID = naibrd_GetModuleID(cardIndex, module);
if ((moduleID != 0))
{
Run_M1553_MT_Monitor_FIFO(cardIndex, module, moduleID);
}
}
}
printf("\nType Q to quit or Enter key to restart application:\n");
stop = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
}
}
printf("\nType the Enter key to exit the program: ");
naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
naiapp_access_CloseAllOpenCards();
return 0;
}
static bool_t Run_M1553_MT_Monitor_FIFO(int32_t cardIndex, int32_t module, uint32_t modid)
{
/* Variables */
bool_t bQuit = FALSE;
int32_t mtchan;
int16_t DevNum = 0;
int32_t swResult;
bool_t bContinue = TRUE;
int32_t duration;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
/* Get Card, Module, Channel Numbers and Open a Handle */
bQuit = Get1553MTCfg(modid, DEF_MT_CHANNEL, &mtchan);
if (bQuit)
{
return bQuit;
}
/* Get Logical Device # */
bQuit = Get1553LogicalDevNum(DEF_MT_DEV_NUM, &DevNum);
if (bQuit)
{
return bQuit;
}
/* Associate Card, Module and Channel Numbers with the Logical Device # */
swResult = naibrd_1553_Open(cardIndex, module, mtchan, DevNum);
if(swResult)
{
bQuit = TRUE;
printf("Error: naibrd_1553_Open %d", swResult);
return bQuit;
}
/* Simplex enable */
/* swResult = naibrd_1553_WriteAuxReg(DevNum, 0xF, 0x0); */
/* Initialize Device */
swResult = naibrd_1553_Initialize(DevNum,NAI_1553_ACCESS_CARD,NAI_1553_MODE_MT | NAI_1553_MESSAGE_FIFO_MODE,0,0,0);
if(swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_Initialize %d", swResult);
return bQuit;
}
/* Disable Monitoring for RT 1, SA 3 Message Types (In default power on state, all messages are monitored) */
swResult = naibrd_1553_MtMessageMonitoringDisable(DevNum, RT_DISABLE, NAI_1553_MT_FILTER_ALL, 1 << SA_DISABLE);
if(swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_Initialize %d", swResult);
return bQuit;
}
while (bContinue)
{
printf("\nType duration (in seconds) to run RT or %c to quit (default: 5) : ", NAI_QUIT_CHAR);
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
duration = 5;
if (inputResponseCnt > 0)
{
duration = (int)atol((const char*)inputBuffer);
}
swResult = naibrd_1553_ClearMsgFIFO(DevNum);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_ClearMsgFIFO %d", swResult);
return bQuit;
}
/* Start MT */
swResult = naibrd_1553_MtStart(DevNum);
if(swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_MtStart %d", swResult);
return bQuit;
}
/* Start the monitor and display any new messages */
MonitorMessages(DevNum, duration);
/* Stop MT */
swResult = naibrd_1553_MtStop(DevNum);
if(swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_MtStop %d", swResult);
return bQuit;
}
}
else
bContinue = FALSE;
}
/* Free 1553 Device */
swResult = naibrd_1553_Free(DevNum);
if(swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_Free %d", swResult);
return bQuit;
}
return bQuit;
}
static int MonitorMessages(uint16_t DevNum, int32_t duration)
{
time_t end;
uint32_t swResult;
int32_t i;
naiDecodedMessageStructureFIFO msgStruct;
uint32_t fifoData[1024];
int32_t count = 0;
int32_t currBlockIndex = 0, nextBlockIndex = 0;
uint32_t fifoCount;
bool_t FIFOfullStatusBit;
end = time(NULL) + duration;
while (time(NULL) < end)
{
swResult = naibrd_1553_GetMsgFIFOCount(DevNum, &fifoCount);
if (swResult != 0)
{
printf("Error: naibrd_1553_GetMsgFIFOCount %d", swResult);
return TRUE;
}
else
{
if (fifoCount > 0)
{
currBlockIndex = 0;
/* This is necessary because otherwise, fifoData array gets used over and over without getting zero'ed out. */
/* When the array gets decoded, old data may get decoded unintentionally */
memset(fifoData, 0, sizeof(fifoData));
/* Read Message FIFO */
swResult = naibrd_1553_ReadMsgFIFO(DevNum, fifoCount, fifoData);
if (swResult != 0)
{
printf("Error: naibrd_1553_ReadMsgFIFO %d", swResult);
return TRUE;
}
else
{
/* Read Messages */
do
{
swResult = naibrd_1553_DecodeFIFOMsg(fifoData, currBlockIndex, &nextBlockIndex, &msgStruct);
if (swResult == 0)
{
if ((msgStruct.wCommandWord1 & 0x0400) != 0x0400) /* If this is a Rx message */
{
printf("Rx Msg Received\n");
printf("\n\nDecoded Message:\n\n");
printf("Block Status - 0x%04X\n", msgStruct.wBlockStatus);
printf("Time Tag - 0x%04X\n", msgStruct.wTimeTag);
printf("Command Word - 0x%04X\n", msgStruct.wCommandWord1);
if(msgStruct.bIsCommandWord2Relevant)
printf("Command Word 2 - 0x%04X\n", msgStruct.wCommandWord2);
printf("RT Status Word: 0x%04X\n", msgStruct.wStatus1);
if (msgStruct.bIsStatus2Relevant)
printf("RT Status Word 2: 0x%04X\n", msgStruct.wStatus2);
printf("Data Word Count - 0x%04X\n", msgStruct.wDataWordCount);
printf((msgStruct.wDataWordCount > 0) ? ("Data:") : (""));
for (i = 0; i < msgStruct.wDataWordCount; i++)
{
if (i % 8 == 0)
{
printf("\n");
}
printf("0x%04X ", msgStruct.waData[i]);
}
printf("count: %d\n", count++);
printf("\n\n");
}
else
{
printf("Tx Msg Received\n");
printf("\n\nDecoded Message:\n\n");
printf("Block Status - 0x%04X\n", msgStruct.wBlockStatus);
printf("Time Tag - 0x%04X\n", msgStruct.wTimeTag);
printf("Command Word - 0x%04X\n", msgStruct.wCommandWord1);
if (msgStruct.bIsCommandWord2Relevant)
printf("Command Word 2 - 0x%04X\n", msgStruct.wCommandWord2);
printf("RT Status Word: 0x%04X\n", msgStruct.wStatus1);
if (msgStruct.bIsStatus2Relevant)
printf("RT Status Word 2: 0x%04X\n", msgStruct.wStatus2);
printf("Data Word Count - 0x%04X\n", msgStruct.wDataWordCount);
printf((msgStruct.wDataWordCount > 0) ? ("Data:") : (""));
for (i = 0; i < msgStruct.wDataWordCount; i++)
{
if (i % 8 == 0)
{
printf("\n");
}
printf("0x%04X ", msgStruct.waData[i]);
}
printf("count: %d\n", count++);
printf("\n\n");
}
}
else if (swResult != (uint32_t)NAI_1553_CMD_NO_MSG_TO_DECODE)
{
printf("Error: naibrd_1553_DecodeFIFOMsg %d", swResult);
return TRUE;
}
/*** IMPORTANT !!! ***/
currBlockIndex = nextBlockIndex;
nai_msDelay(1);
} while ((swResult == NAI_SUCCESS) && (time(NULL) < end));
}
swResult = naibrd_1553_GetMsgFIFOFullStatus(DevNum, NAI_1553_STATUS_REALTIME, &FIFOfullStatusBit);
if (swResult == NAI_SUCCESS)
{
printf("FIFO status: %d", FIFOfullStatusBit);
printf((FIFOfullStatusBit == TRUE) ? " Full\r\n" : " Not full\r\n");
}
else
{
printf("Error: naibrd_1553_GetMsgFIFOFullStatus %d", swResult);
return TRUE;
}
}
}
}
return 1;
}