M1553 RT BITWordSetByHost
Edit this on GitLab
M1553 RT BITWordSetByHost
Explanation
About This Sample Application Code
This sample application code from North Atlantic Industries (NAI) demonstrates the usage of their System Starter Kit (SSK) to interact with embedded function modules, specifically focusing on configuring and running the MIL-STD-1553 channel as a Remote Terminal (RT) and writing a Built-In Test (BIT) word from the host. Below is a detailed explanation of the various components and functionality in the code:
Header Inclusions
The code includes various system and library headers crucial for its functionality:
#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" /* 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"
These headers provide access to functionalities for board access, user queries, displaying information, and handling 1553 communication protocols.
Constants and Static Variables
static const int8_t *CONFIG_FILE = (int8_t *)"default_1553_RTBitWordSetByHost.txt"; static const int32_t DEF_RT_CHANNEL = 1; static const int16_t DEF_RT_DEV_NUM = 1; static const uint8_t DEF_RT_ADDRESS = 1; static const uint16_t wBitWord = 0x6789;
The constants are defined for configuration and default values used throughout the application. They include the configuration file, default channel, device number, address, and the BIT word to be set.
Function Declarations
static bool_t Run_M1553_RT_BITWordSetByHost(int32_t cardIndex, int32_t module, uint32_t modid); static int32_t ProcessMessages(uint16_t DevNum, int32_t duration);
These functions handle the main operational logic: - 'Run_M1553_RT_BITWordSetByHost': Configures and operates the MIL-STD-1553 channel. - 'ProcessMessages': Handles the processing of messages received during communication.
Main Function
#if defined (__VXWORKS__) int32_t M1553_RT_BITWordSetByHost(void) #else int32_t main(void) #endif { ... }
The 'main' function (or 'M1553_RT_BITWordSetByHost' on VxWorks) is the program’s starting point. It initializes and manages the application flow, handling user queries to configure the system and execute the operational routines.
Program Execution Flow:
-
Menu Initialization: Executes the board menu based on the configuration file.
-
User Input for Card and Module: Asks for card index and module number from the user.
-
BIT Word Setting: Calls 'Run_M1553_RT_BITWordSetByHost' to configure and write the BIT word.
-
Loop for Restart or Quit: Allows the user to restart the application or exit.
Run_M1553_RT_BITWordSetByHost Function
This function configures the 1553 channel and writes the BIT word from the host:
static bool_t Run_M1553_RT_BITWordSetByHost(int32_t cardIndex, int32_t module, uint32_t modid) { ... }
Key Steps: 1. Configuration: Retrieves card, module, and channel configurations. 2. Device Initialization: Opens and initializes the 1553 device. 3. RT Address Configuration: Sets RT address either in software or hardware. 4. BIT Word Operations: Sets up the BIT word configuration and writes the BIT word. 5. Message Processing: Starts the RT, processes messages for a user-defined duration, and then stops the RT. 6. Device Cleanup: Frees the 1553 device after operation completion.
ProcessMessages Function This function manages message processing:
static int32_t ProcessMessages(uint16_t DevNum, int32_t duration) { ... }
Core Functionality: 1. Time-based Processing: Runs for a specified duration. 2. Message Retrieval and Decoding: Retrieves raw messages and decodes them. 3. Display and Count Messages: Prints received messages and increments a count.
Summary This sample application provides a comprehensive guide to configuring and operating a MIL-STD-1553 RT channel using NAIs SSK. Detailed user queries, device configurations, and message handling are demonstrated to facilitate an understanding of managing embedded function modules effectively.
#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"
/* 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_RTBitWordSetByHost.txt";
/* Function prototypes */
static bool_t Run_M1553_RT_BITWordSetByHost(int32_t cardIndex, int32_t module, uint32_t modid);
static int32_t ProcessMessages(uint16_t DevNum, int32_t duration);
static const int32_t DEF_RT_CHANNEL = 1;
static const int16_t DEF_RT_DEV_NUM = 1;
static const uint8_t DEF_RT_ADDRESS = 1;
static const uint16_t wBitWord = 0x6789;
/**************************************************************************************************************/
/**
<summary>
The purpose of the M1553_RT_BITWordSetByHost is to illustrate the methods to call in the naibrd library to configure
and run the 1553 channel as a Remote Terminal, provide the host with write access to the Built-In Test (BIT) word
and write the value 0x6789 to the BIT word from the host. The FT[1-9] and FTJ/FTK modules possess the capability
for the BIT word to be written by the host. The default mode however does not allow the host to write the BIT word
and instead it is updated by the core. By calling naibrd_1553_RtBITWordConfigure() with the appropriate parameter,
the host can write the 16-bit BIT word and in turn, when the Bus Controller sends a "Transmit BIT Word" mode code,
the RT will respond with status and a data word that contains the BIT word written by the host.
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_RT_BITWordSetByHost(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_RT_BITWordSetByHost(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_RT_BITWordSetByHost(int32_t cardIndex, int32_t module, uint32_t modid)
{
/* Variables */
bool_t bQuit = FALSE;
int32_t rtchan;
uint8_t rtaddr;
int16_t DevNum = 0;
int32_t swResult;
bool_t bSoftwareRTAddr;
uint16_t wData;
bool_t bContinue = TRUE;
int32_t duration;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
/* Get Card, Module, Channel Numbers and Open a Handle */
bQuit = Get1553RTCfg(modid, DEF_RT_CHANNEL, DEF_RT_ADDRESS, &rtchan, &rtaddr);
if (bQuit)
{
return bQuit;
}
/* Get Logical Device # */
bQuit = Get1553LogicalDevNum(DEF_RT_DEV_NUM, &DevNum);
if (bQuit)
{
return bQuit;
}
/* Associate Card, Module and Channel Numbers with the Logical Device # */
swResult = naibrd_1553_Open(cardIndex, module, rtchan, DevNum);
if(swResult)
{
bQuit = TRUE;
printf("Error: naibrd_1553_Open %d", swResult);
return bQuit;
}
/* Initialize Device */
swResult = naibrd_1553_Initialize(DevNum,NAI_1553_ACCESS_CARD,NAI_1553_MODE_RT,0,0,0);
if(swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_Initialize %d", swResult);
return bQuit;
}
/* Get RT Address Source from user */
bQuit = Get1553RTAddressSource(TRUE, &bSoftwareRTAddr);
if (bQuit)
{
return bQuit;
}
if (bSoftwareRTAddr)
{
/* Set RTAD_SW_EN and RT_ADR_LAT in software */
swResult = naibrd_1553_WriteAuxReg(DevNum, 0x2, 0x0018);
/* Set RT address */
swResult = naibrd_1553_RtSetAddress(DevNum, rtaddr);
if(swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtSetAddress %d", swResult);
return bQuit;
}
}
else
{
/* Unset RTAD_SW_EN and set RT_ADR_LAT in software */
swResult = naibrd_1553_WriteAuxReg(DevNum, 0x2, 0x0008);
}
/* Set the location of the BIT word */
swResult = naibrd_1553_RtBITWordConfigure(DevNum, NAI_1553_RT_BIT_MEMORY, NAI_1553_RT_BIT_ENABLED);
if(swResult != 0)
{
printf("Error: naibrd_1553_RtBITWordConfigure status = %d", swResult);
return TRUE;
}
/* Write the BIT word */
swResult = naibrd_1553_RtBITWordWrite(DevNum, wBitWord);
if(swResult != 0)
{
printf("Error: naibrd_1553_RtBITWordWrite status = %d", swResult);
return TRUE;
}
/* Read the BIT word from the memory/register location updated by the core */
swResult = naibrd_1553_RtBITWordRead(DevNum, NAI_1553_RT_BIT_REGISTER, &wData);
if(swResult != 0)
{
printf("Error: naibrd_1553_RtBITWordRead status = %d", swResult);
return TRUE;
}
printf("\nCore BIT word: 0x%04X ", wData);
/* Read the BIT word from the memory location updated by the host */
swResult = naibrd_1553_RtBITWordRead(DevNum, NAI_1553_RT_BIT_MEMORY, &wData);
if(swResult != 0)
{
printf("Error: naibrd_1553_RtBITWordRead status = %d", swResult);
return TRUE;
}
printf("Host BIT word: 0x%04X\n", wData);
/* Legalize Tx Mode Codes */
swResult = naibrd_1553_RtMessageLegalityEnable(DevNum, NAI_1553_RT_ADDRESS_OWN, NAI_1553_MT_FILTER_TX, NAI_1553_MODE_CODE1, NAI_1553_WORDCOUNT_OR_MODECODE_MASK_ALL);
if(swResult != 0)
{
printf("Error: naibrd_1553_RtMessageLegalityEnable status = %d", swResult);
return TRUE;
}
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 RT */
swResult = naibrd_1553_RtStart(DevNum);
if(swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtStart %d", swResult);
return bQuit;
}
/* Process New Messages */
ProcessMessages(DevNum, duration);
/* Stop RT */
swResult = naibrd_1553_RtStop(DevNum);
if(swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtStop %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 int32_t ProcessMessages(uint16_t DevNum, int32_t duration)
{
time_t end;
uint32_t swResult;
int32_t msgcount;
uint16_t wsBuffer[72] = { 0x0000 };
naiDecodedMessageStructure DecodedMsgStruct;
int32_t i, j;
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_RtMessageGetFromStackRaw(DevNum, wsBuffer, NAI_1553_MAX_MESSAGE_SIZE_RT*2);
if (swResult < 0)
{
printf("Error: naibrd_1553_RtMessageGetFromStackRaw %d\n\n", swResult);
return 0;
}
else if (swResult > 0)
{
msgcount = swResult;
for (j = 0; j < msgcount; j++)
{
/* Decode Raw Message */
swResult = naibrd_1553_RtMessageDecodeRaw(DevNum, &wsBuffer[j*NAI_1553_MAX_MESSAGE_SIZE_RT], &DecodedMsgStruct);
if (swResult < 0)
{
printf("Error: naibrd_1553_RtMessageDecodeRaw %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;
}