M1553 ModeCode TxBITWord
Edit this on GitLab
M1553 ModeCode TxBITWord
Explanation
About the Sample Application Code
This sample application code is designed to interact with North Atlantic Industries (NAI) embedded function modules using their Single Software Kernel (SSK). The primary purpose of the code is to configure a 1553 channel as a Bus Controller and send out a "Transmit BIT Word" mode code message.
Key Components of the Code
Include Directives
-
Standard Libraries: The code includes standard I/O, memory handling, and time libraries.
c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
-
Conditional OS Inclusion: If the code is being compiled for VxWorks, the
taskLib
is included.c #if defined (VXWORKS) #include "taskLib.h" #endif
-
NAI Specific Libraries: Includes various NAI specific headers that provide necessary functions and definitions to interact with NAI boards.
c #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" #include "BC/nai_1553_bc_utils.h" #include "nai.h" #include "naibrd.h" #include "functions/naibrd_1553.h"
Constants and Macros
-
Configuration File: A predefined configuration file for the 1553 mode code transmission.
c static const int8_t *CONFIG_FILE = (int8_t *)"default_1553_ModeCode_TransmitBITWord.txt";
-
Message, Opcode, Frame, and Data Block Definitions: Constants that define message IDs, opcodes, frame types, and data block numbers.
c #define MSG2 1 #define OP3 1 #define OP4 2 #define MNR2 1 #define MJR1 2 #define DBLK2 1
-
Default Values and Addresses: Default indices for cards, modules, channels, and device numbers, along with RT (Remote Terminal) addresses.
c #define DEF_M1553_CARD_INDEX 0 #define DEF_M1553_MODULE 1 #define DEF_M1553_CHANNEL 2 #define DEF_M1553_DEVNUM 0 #define RT_ADDRESS 1 #define RT_SUBADDRESS 2 #define WORDCOUNT 11
Function Prototypes
Two static functions are declared with their prototypes:
static bool_t Run_M1553_ModeCode_TxBITWord(int32_t cardIndex, int32_t module, uint32_t modid, uint16_t wModeCommand);
static bool_t RunSendModeCodeMessage(int32_t cardIndex, int32_t module, int32_t channel, uint32_t modid, uint16_t wModeCommand);
Main Application Logic
Depending on the operating system (VxWorks or others), the entry point of the application is defined:
#if defined (__VXWORKS__)
int32_t M1553_ModeCode_TxBITWord(void)
#else
int32_t main(void)
#endif
The main function walks through the following steps:
-
Run Board Menu: Uses
naiapp_RunBoardMenu()
to initialize configuration from a file. -
Query Card Index and Module Number: Queries from the user.
-
Check Module ID and Run Transmission: Runs the transmission function
Run_M1553_ModeCode_TxBITWord()
if the module ID is valid. -
Terminate Loop on User Input: Allows exiting or restarting the application based on user input.
Transmission Logic
The Run_M1553_ModeCode_TxBITWord()
function prepares the system and delegates to RunSendModeCodeMessage()
which performs the actual 1553 mode code message transmission using the following steps:
-
Initialization and Device Configuration: Initializes the 1553 device, sets up auxiliary registers, and prepares the device for BC (Bus Controller) mode.
-
Message and Command Creation: Creates data blocks, mode code messages, and commands using APIs like
naibrd_1553_BcDataBlockCreate()
andnaibrd_1553_BcMessageCreateMode()
. -
Frame Setup: Configures minor and major frames.
-
Start and Stop BC: Starts the Bus Controller to send the message and subsequently stops it.
-
Result Retrieval: Retrieves and prints the decoded message structure containing status and data of the transmitted message.
Enumerations and API Functions
The code makes extensive use of NAI’s API functions and enumerations to control the 1553 hardware. Key APIs are:
- naibrd_1553_Open
- naibrd_1553_Initialize
- naibrd_1553_BcDataBlockCreate
- naibrd_1553_BcMessageCreateMode
- naibrd_1553_BcCommandCreate
- naibrd_1553_BcFrameCreate
- naibrd_1553_BcStart
- naibrd_1553_BcStop
- naibrd_1553_BcMessageGetByIdDecoded
These functions collectively configure, run, and assess the performance of the 1553 Bus Controller module, ensuring the successful transmission of mode code messages.
This detailed explanation provides a comprehensive walk-through of the application, key components, and the utilized API functions, ensuring a clear understanding of the provided sample code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if defined (__VXWORKS__)
#include "taskLib.h"
#endif
/* 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"
#include "BC/nai_1553_bc_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_ModeCode_TransmitBITWord.txt";
/* Function prototypes */
static bool_t Run_M1553_ModeCode_TxBITWord(int32_t cardIndex, int32_t module, uint32_t modid, uint16_t wModeCommand);
static bool_t RunSendModeCodeMessage(int32_t cardIndex, int32_t module, int32_t channel, uint32_t modid, uint16_t wModeCommand);
/* define message constants */
#define MSG2 1
/* define opcodes */
#define OP3 1
#define OP4 2
/* define frame constants */
#define MNR2 1
#define MJR1 2
/* define data block numbers */
#define DBLK2 1
#define DEF_M1553_CARD_INDEX 0
#define DEF_M1553_MODULE 1
#define DEF_M1553_CHANNEL 2
#define DEF_M1553_DEVNUM 0
#define RT_ADDRESS 1
#define RT_SUBADDRESS 2
#define WORDCOUNT 11
/**************************************************************************************************************/
/**
<summary>
The purpose of the M1553_ModeCode_TxBITWord is to illustrate the methods to call in the naibrd library to
configure the 1553 channel as a Bus Controller and send out a "Transmit BIT Word" mode code message.
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
API function in the 1553 naibrd library:
- naibrd_1553_Free
API function in the naibrd library:
- naibrd_Close
</summary>
*/
/**************************************************************************************************************/
#if defined (__VXWORKS__)
int32_t M1553_ModeCode_TxBITWord(void)
#else
int32_t main(void)
#endif
{
bool_t stop = FALSE;
int32_t cardIndex;
int32_t moduleCnt;
int32_t module;
uint32_t moduleID = 0;
uint16_t modeTransmitBITWord = 0x13;
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_ModeCode_TxBITWord(cardIndex, module, moduleID, modeTransmitBITWord);
}
}
}
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_ModeCode_TxBITWord(int32_t cardIndex, int32_t module, uint32_t modid, uint16_t wModeCommand)
{
bool_t bQuit = FALSE;
int32_t channel;
int32_t MaxChannel = 4;
if (IsFTx1553(modid))
{
MaxChannel = 4;
bQuit = naiapp_query_ChannelNumber(MaxChannel, DEF_M1553_CHANNEL, &channel);
if (!bQuit)
{
bQuit = RunSendModeCodeMessage(cardIndex, module, channel, modid, wModeCommand);
}
}
else if (IsFTx1760(modid))
{
MaxChannel = 2;
bQuit = naiapp_query_ChannelNumber(MaxChannel, DEF_M1553_CHANNEL, &channel);
if (!bQuit)
{
bQuit = RunSendModeCodeMessage(cardIndex, module, channel, modid, wModeCommand);
}
}
else
printf("\nThis module does not support 1553 functionality.\n");
return bQuit;
}
/**************************************************************************************************************/
/**
<summary>
RunSendModeCodeMessage initializes the 1553 device as a bus controller (BC) and sends out a mode code message.
This routine demonstrates the following API functions in the 1553 naibrd library:
naibrd_1553_Open
naibrd_1553_Initialize
naibrd_1553_BcDataBlockCreate
naibrd_1553_BcMessageCreateMode
naibrd_1553_BcCommandCreate
naibrd_1553_BcFrameCreate
naibrd_1553_BcStart
naibrd_1553_BcStop
naibrd_1553_BcMessageGetByIdDecoded
</summary>
*/
/**************************************************************************************************************/
static bool_t RunSendModeCodeMessage(int32_t cardIndex, int32_t module, int32_t channel, uint32_t modid, uint16_t wModeCommand)
{
int32_t i;
naiDecodedMessageStructure DecodedMsgStruct;
nai_1553_t status = 0;
int16_t aOpCodes[20] = { 0 };
bool_t bQuit = FALSE;
uint32_t usBus;
int16_t devnum;
bool_t bSoftware;
/* Get the Logical Device Number */
bQuit = Get1553LogicalDevNum(DEF_M1553_DEVNUM, &devnum);
if (bQuit)
{
return bQuit;
}
/* Which bus are we firing on? */
bQuit = GetBus(&usBus);
if (bQuit)
{
return bQuit;
}
bQuit = Get1553BCSoftwareOverride(TRUE, &bSoftware);
if (bQuit)
{
return bQuit;
}
/* Open 1553 Device(s) */
status = naibrd_1553_Open(cardIndex, module, channel, devnum);
if(status != 0)
{
printf("Error: naibrd_1553_Open Ch %d, status = %d", channel, status);
return TRUE;
}
if (bSoftware)
{
/* Override external BC_DISABLE and M1760 (In order to configure as BC, this needs to */
/* be set if BC_DISABLE and M1760 pins are not driven high) */
naibrd_1553_WriteAuxReg(devnum, 0x2, 0xA000);
}
else
{
/* Do not override external BC_DISABLE and M1760 Inputs */
naibrd_1553_WriteAuxReg(devnum, 0x2, 0x0000);
}
if (modid == NAI_MODULE_ID_FT8)
{
/* Simplex Enable (for NAI internal testing only, do not enable) */
/*naibrd_1553_WriteAuxReg(devnum, 0x3, 0x4000);
naibrd_1553_WriteAuxReg(devnum, 0xF, 0x0001);*/
}
/* Reset Device */
naibrd_1553_WriteAuxReg(devnum, 0x1, 0x1);
#if defined (__VXWORKS__)
taskDelay(1);
#elif defined (LINUX)
usleep(100000);
#else
Sleep(100);
#endif
naibrd_1553_WriteAuxReg(devnum, 0x1, 0x0);
/* Initialize 1553 Device(s) */
status = naibrd_1553_Initialize(devnum, NAI_1553_ACCESS_CARD,NAI_1553_MODE_BC,0,0,0);
if(status != 0)
{
printf("Error: naibrd_1553_Initialize Ch %d, status = %d", channel, status);
return TRUE;
}
status = naibrd_1553_BcDataBlockCreate(devnum, DBLK2, WORDCOUNT, NULL, 0);
if(status != 0)
{
printf("Error: naibrd_1553_BcDataBlockCreate Ch %d, status = %d", channel, status);
return TRUE;
}
/* Set up Tx Mode code message */
status = naibrd_1553_BcMessageCreateMode(devnum, MSG2, DBLK2, RT_ADDRESS, NAI_1553_CMD_TX, wModeCommand, 100, usBus);
if (status != 0)
{
printf("Error: naibrd_1553_BcMessageCreateMode status = %d", status);
return TRUE;
}
/* Create Execute Message Command */
status = naibrd_1553_BcCommandCreate(devnum, OP3, NAI_1553_OPCODE_EXECUTE_MESSAGE, NAI_1553_OPCODE_COND_ALWAYS, MSG2, 0, 0);
if (status != 0)
{
printf("Error: naibrd_1553_BcCommandCreate status = %d", status);
return TRUE;
}
/* Create Call Subroutine Command */
status = naibrd_1553_BcCommandCreate(devnum, OP4, NAI_1553_OPCODE_CALL_SUBROUTINE, NAI_1553_OPCODE_COND_ALWAYS, MNR2, 0, 0);
if (status != 0)
{
printf("Error: naibrd_1553_BcCommandCreate status = %d", status);
return TRUE;
}
/* Create Minor Frame */
aOpCodes[0] = OP3;
status = naibrd_1553_BcFrameCreate(devnum, MNR2, NAI_1553_BC_FRAME_MINOR, aOpCodes, 1, 0, 0);
if (status != 0)
{
printf("Error: naibrd_1553_BcFrameCreate status = %d", status);
return TRUE;
}
/* Create Major Frame */
aOpCodes[0] = OP4;
status = naibrd_1553_BcFrameCreate(devnum,MJR1,NAI_1553_BC_FRAME_MAJOR,aOpCodes,1,1000,0);
if (status != 0)
{
printf("Error: naibrd_1553_BcFrameCreate status = %d", status);
return TRUE;
}
/* Start BC */
status = naibrd_1553_BcStart(devnum,MJR1,1);
if (status != 0)
{
printf("Error: naibrd_1553_BcStart status = %d", status);
return TRUE;
}
/* This delay is necessary to allow the BC to run */
nai_msDelay(200);
/* Stop BC */
status = naibrd_1553_BcStop(devnum);
if (status != 0)
{
printf("Error: naibrd_1553_BcStop status = %d", status);
return TRUE;
}
/* Get Decoded Msg Structure */
status = naibrd_1553_BcMessageGetByIdDecoded(devnum, MSG2, &DecodedMsgStruct, 1);
if (status < 0)
{
printf("Error: naibrd_1553_BcMessageGetByIdDecoded status = %d", status);
return TRUE;
}
else if (status > 0)
{
printf("Control Word: 0x%04X\n", DecodedMsgStruct.wBcControlWord);
printf("Command Word: 0x%04X\n", DecodedMsgStruct.wCommandWord1);
printf("Block Status: 0x%04X\n", DecodedMsgStruct.wBlockStatus);
printf("Time Tag: 0x%04X\n", DecodedMsgStruct.wTimeTag);
printf("Word Count: 0x%04X\n", DecodedMsgStruct.wDataWordCount);
printf("RT Status Word: 0x%04X\n", DecodedMsgStruct.wStatus1);
printf("Data:");
for (i = 0; i < DecodedMsgStruct.wDataWordCount; i++)
{
if (i % 8 == 0)
{
printf("\n");
}
printf("0x%04X ", DecodedMsgStruct.waData[i]);
}
printf("\n\n");
}
return TRUE;
}