M1553 MT Monitor
Edit this on GitLab
M1553 MT Monitor
Explanation
== About This Code
This C application is designed to interact with North Atlantic Industries' (NAI) embedded function modules, specifically targeting the 1553 bus communication standard. The primary purpose of this code is to configure and run a 1553 channel as a Message Monitor (MT), which can detect and display incoming (Rx) and outgoing (Tx) messages on the bus. It also demonstrates message filtering to selectively disable certain message types.
Here is a detailed breakdown of the code and its functionality:
=== Included Headers
-
Standard Libraries
-
'stdio.h', 'stdlib.h', 'string.h', 'time.h': Standard C libraries for input/output, memory allocation, string handling, and time management.
-
-
Common Sample Program Include Files
-
Various 'naiapp_boardaccess_*' headers: These headers provide functions to access, query and display board and module information.
-
'nai_1553_utils.h': Provides utility functions specific to the 1553 communication standard.
-
-
NAI Board and Function Solution Include Files
-
'nai.h', 'naibrd.h', 'naibrd_1553.h': Include functions and macros for interacting with NAI boards and specifically the 1553 board functionality.
-
=== Static Constants and Definitions
-
Config File
-
'CONFIG_FILE': Points to a default configuration file for the 1553 MT monitor.
-
-
Default Configuration
-
'DEF_MT_CHANNEL': Default channel number for the MT.
-
'DEF_MT_DEV_NUM': Default device number for the MT.
-
-
Disable Macros
-
'RT_DISABLE', 'SA_DISABLE': Define constants for disabling specific message types.
-
=== Main Function and Workflow
-
Entry Point
-
'int32_t main(void)': The application’s entry point.
-
-
Configuration and Querying
-
'naiapp_RunBoardMenu': Runs the configuration menu based on the provided config file.
-
'naiapp_query_CardIndex': Queries the user for the card index.
-
'naibrd_GetModuleCount': Retrieves the number of modules on the specified card.
-
'naiapp_query_ModuleNumber': Queries the user for the module number.
-
'naibrd_GetModuleID': Gets the module ID based on the card and module number.
-
-
Monitoring Messages
-
'Run_M1553_MT_Monitor': Configures and monitors the 1553 channel as an MT. It initializes the device, applies message filters, starts the MT, and displays messages received/transmitted on the bus.
-
-
Exit Handling
-
'naiapp_access_CloseAllOpenCards': Closes all open NAI cards safely before exiting the program.
-
=== Monitoring Functionality
-
Monitoring Loop
-
The 'MonitorMessages' function continually checks for new messages for a specified duration. It reads messages from the stack, decodes them, and prints the decoded information based on whether it s an Rx or Tx message.
-
-
Decoded Message Structure
-
'naiDecodedMessageStructure': A structure that holds the decoded message details including block status, time tag, command word, data word count, and the data itself.
-
=== Functions and Prototypes
-
Function Prototypes
-
'Run_M1553_MT_Monitor': Sets up and runs the monitor for a given card, module, and module ID.
-
'MonitorMessages': Monitors and displays messages based on the logical device number and duration specified.
-
=== Key Enumerations and Macros
-
NAI-specific Macros
-
The code utilizes several NAI-specific macros and constants for configuring and interacting with the 1553 bus.
-
=== Usage Summary This application is a crucial tool for developers and engineers who are working with NAI’s 1553 function modules. It not only demonstrates the setup and monitoring of 1553 communication but also showcases message filtering, decoding, and display, making it an essential reference for working with NAI’s 1553 modules.
By understanding and using this code, users can effectively monitor and troubleshoot 1553 bus messages, facilitating smoother and more reliable communication in their embedded systems and applications.
#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"
static const int8_t *CONFIG_FILE = (int8_t *)"default_1553_MTMonitor.txt";
/* Function prototypes */
static bool_t Run_M1553_MT_Monitor(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 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 message is detected on the bus, the Rx message data will
be displayed. If a Tx message is detected, the Tx message will be displayed. 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(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(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(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,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);
}
/* 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;
naiDecodedMessageStructure DecodedMsgStruct;
uint16_t wsBuffer[80] = { 0x0000 };
int32_t count = 0;
end = time(NULL) + duration;
while (time(NULL) < end)
{
/* If the stack pointer has updated (new data arrived), read one message at a time */
swResult = naibrd_1553_MtMessageGetFromStackRaw(DevNum, wsBuffer, NAI_1553_MAX_MESSAGE_SIZE_MT, NAI_1553_MT_STACK_ACTIVE);
if (swResult < 0)
{
printf("Error: naibrd_1553_MtMessageGetFromStackRaw %d\n\n", swResult);
return 0;
}
else if (swResult > 0)
{
/* Decode Raw Message */
swResult = naibrd_1553_MtMessageDecodeRaw(DevNum, wsBuffer, &DecodedMsgStruct);
if (swResult < 0)
{
printf("Error: naibrd_1553_MtMessageDecodeRaw %d\n\n", swResult);
return 0;
}
if ((DecodedMsgStruct.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", DecodedMsgStruct.wBlockStatus);
printf("Time Tag - 0x%04X\n", DecodedMsgStruct.wTimeTag);
printf("Command Word - 0x%04X\n", DecodedMsgStruct.wCommandWord1);
printf("Data Word Count - 0x%04X\n", DecodedMsgStruct.wDataWordCount);
printf("Data:");
for (i = 0; i < DecodedMsgStruct.wDataWordCount; i++)
{
if (i % 8 == 0)
{
printf("\n");
}
printf("0x%04X ", DecodedMsgStruct.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", DecodedMsgStruct.wBlockStatus);
printf("Time Tag - 0x%04X\n", DecodedMsgStruct.wTimeTag);
printf("Command Word - 0x%04X\n", DecodedMsgStruct.wCommandWord1);
printf("Data Word Count - 0x%04X\n", DecodedMsgStruct.wDataWordCount);
printf("count: %d\n", count++);
printf("\n\n");
}
}
nai_msDelay(10);
}
return 1;
}