M1553 BC SendMessage FIFO
Edit this on GitLab
M1553 BC SendMessage FIFO Sample Application (SSK 1.x)
Overview
The M1553 BC SendMessage FIFO sample application demonstrates how to use FIFO-based message decoding with a MIL-STD-1553 Bus Controller (BC) using the NAI Software Support Kit (SSK 1.x). Instead of decoding messages from the standard stack-based message storage, this sample initializes the BC in FIFO mode (NAI_1553_MESSAGE_FIFO_MODE) and reads completed messages from a hardware FIFO buffer. This approach is useful when you need to process messages in strict chronological order as they complete on the bus.
Like the basic M1553_BC_SendMessage sample, this sample supports all four MIL-STD-1553 message types (BC-to-RT, RT-to-BC, RT-to-RT, and mode code). The key difference is the message retrieval path: instead of calling naibrd_1553_BcMessageGetByIdDecoded(), this sample uses naibrd_1553_GetMsgFIFOCount(), naibrd_1553_ReadMsgFIFO(), and naibrd_1553_DecodeFIFOMsg() to read and decode messages from the FIFO.
|
Note
|
FIFO mode is only available on the FTA, FTB, FTC, FTD, and FTF module variants. Other FTx variants (FT0-FT9, FTE) do not support FIFO mode. Check your installed module type before using this sample. |
This sample supports the following 1553 module types:
-
FT0 through FTF — 4-channel MIL-STD-1553 modules (FIFO mode limited to FTA, FTB, FTC, FTD, FTF)
-
Combination modules CM1, CM5, and CM8
The application detects the installed module variant at runtime using IsFTx1553().
For detailed 1553 protocol specifications, message formats, and hardware register descriptions, see the FTA-FTF Manual.
Prerequisites
Before running this sample, make sure you have:
-
An NAI board with a 1553 module installed — specifically an FTA, FTB, FTC, FTD, or FTF module for FIFO mode support.
-
SSK 1.x installed on your development host.
-
The sample applications built. Refer to the SSK 1.x build instructions for your platform if you have not already compiled them.
How to Run
Launch the M1553_BC_SendMessage_FIFO executable from your build output directory. On startup the application looks for a configuration file (default_1553BC_SendMessage_FIFO.txt). On the first run, this file will not exist — the application will present an interactive board menu where you configure a board connection, card index, and module slot. You can save this configuration so that subsequent runs skip the menu and connect automatically. Once connected, the application prompts for channel, message type, bus selection, and software override settings, then sends messages in a loop, reading and decoding responses from the FIFO.
Board Connection and Module Selection
|
Note
|
This startup sequence is common to all NAI sample applications. The board connection and module selection code shown here is not specific to 1553. |
The main() function follows a standard SSK 1.x startup flow:
-
Call
naiapp_RunBoardMenu()to load a saved configuration file (if one exists) or present the interactive board menu. The configuration file (default_1553BC_SendMessage_FIFO.txt) is not included with the SSK — it is created when the user saves their connection settings from the board menu. On the first run, the menu will always appear. -
Query the user for a card index with
naiapp_query_CardIndex(). -
Query for a module slot with
naiapp_query_ModuleNumber(). -
Retrieve the module ID with
naibrd_GetModuleID()so downstream code can adapt to the specific 1553 variant installed. -
Query for a channel number within the selected module.
#if defined (__VXWORKS__)
int32_t M1553_BC_SendMessage_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)
{
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_BC_SendMessage_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);
}
}
naiapp_access_CloseAllOpenCards();
return 0;
}
|
Important
|
Common Connection Errors
|
Program Structure
The application is organized into three functions:
-
main()— standard SSK 1.x startup: board menu, card/module selection, then delegates to the run function. -
Run_M1553_BC_SendMessage_FIFO()— validates the module supports 1553 viaIsFTx1553(), queries for a channel number, and callsRunSendMessage(). -
RunSendMessage()— the core BC workflow: opens the device, initializes as BC in FIFO mode, enables the gap timer, creates a message of the selected type, builds the frame hierarchy, sends messages in a loop, and reads/decodes responses from the FIFO.
The sample defines these constants for its frame hierarchy:
#define MSG1 1 /* Message ID */
#define OP1 1 /* Execute message opcode */
#define OP2 2 /* Call subroutine opcode */
#define MNR1 1 /* Minor frame ID */
#define MJR 2 /* Major frame ID */
#define DBLK1 1 /* Data block for standard messages */
#define DBLK2 2 /* Data block for mode code messages */
#define RT_ADDRESS 1 /* Target RT address */
#define RT_SUBADDRESS 2 /* Target subaddress */
#define RT_ADDRESS_2 2 /* Second RT address (for RT-to-RT) */
#define WORDCOUNT 32 /* Data words per message */
Opening and Initializing the 1553 Channel in FIFO Mode
To configure a channel as a Bus Controller with FIFO-based message decoding, call naibrd_1553_Open() to associate the hardware channel with a logical device number, then naibrd_1553_Initialize() with the NAI_1553_MESSAGE_FIFO_MODE flag OR’d into the mode parameter:
/* Open 1553 Device */
status = naibrd_1553_Open(cardIndex, module, channel, devnum);
/* Software override for BC_DISABLE and M1760 pins */
if (bSoftware)
{
naibrd_1553_WriteAuxReg(devnum, 0x2, 0xA000);
}
/* Reset Device */
naibrd_1553_WriteAuxReg(devnum, 0x1, 0x1);
Sleep(100);
naibrd_1553_WriteAuxReg(devnum, 0x1, 0x0);
/* Initialize as BC with FIFO mode */
status = naibrd_1553_Initialize(devnum, NAI_1553_ACCESS_CARD, NAI_1553_MODE_BC | NAI_1553_MESSAGE_FIFO_MODE, 0, 0, 0);
The NAI_1553_MESSAGE_FIFO_MODE flag tells the hardware to store completed messages in a FIFO buffer rather than the standard stack-based message storage. When FIFO mode is enabled, completed messages are written to the FIFO in the order they finish on the bus. This is the only initialization difference from the standard BC setup.
The software override (0xA000 written to auxiliary register 0x2) allows the BC to operate even when the external BC_DISABLE and M1760 hardware pins are not driven high. In production, these pins should be properly wired, and the software override should be disabled.
Enabling the Message Gap Timer
After initializing in FIFO mode, enable the message gap timer by calling naibrd_1553_BcConfigureMessageGapTimerEnable():
status = naibrd_1553_BcConfigureMessageGapTimerEnable(devnum, TRUE);
The gap timer inserts a configurable delay between consecutive messages on the bus. When using FIFO mode, the gap timer ensures there is sufficient time for the hardware to write each completed message into the FIFO before the next message begins. Without the gap timer, messages sent back-to-back at high rates may overflow the FIFO write path.
|
Important
|
Common Errors
|
Creating Messages
This sample supports all four MIL-STD-1553 message types. The user selects the type interactively via GetMsgTypeAndCheckForQuit(), and the application creates the appropriate message structure:
BC-to-RT Message
To send data from the BC to an RT, call naibrd_1553_BcMessageCreateBcToRt():
status = naibrd_1553_BcDataBlockCreate(devnum, DBLK1, WORDCOUNT, NULL, 0);
status = naibrd_1553_BcMessageCreateBcToRt(devnum, MSG1, DBLK1, RT_ADDRESS, RT_SUBADDRESS, WORDCOUNT, 1, usBus);
RT-to-BC Message
To request data from an RT, call naibrd_1553_BcMessageCreateRtToBc():
status = naibrd_1553_BcMessageCreateRtToBc(devnum, MSG1, DBLK1, RT_ADDRESS, RT_SUBADDRESS, WORDCOUNT, 1, usBus);
RT-to-RT Message
To command a transfer between two RTs, call naibrd_1553_BcMessageCreateRtToRt():
status = naibrd_1553_BcMessageCreateRtToRt(devnum, MSG1, DBLK1, RT_ADDRESS_2, RT_SUBADDRESS, WORDCOUNT, RT_ADDRESS, RT_SUBADDRESS, 0, usBus);
Mode Code Message
To send a mode code command (transmit or receive), call naibrd_1553_BcMessageCreateMode():
status = naibrd_1553_BcMessageCreateMode(devnum, MSG1, DBLK1, RT_ADDRESS,
(msgType == M1553_MSGTYPE_MODECODE_TX) ? NAI_1553_CMD_TX : NAI_1553_CMD_RX,
modeCommand, 0, usBus);
The message creation API is identical to the standard (non-FIFO) BC sample. The FIFO mode only changes how completed messages are retrieved — the message creation and frame hierarchy setup are the same regardless of the decoding mode.
|
Important
|
Common Errors
|
Building and Executing the Frame Hierarchy
The frame hierarchy is the same as the standard BC sample: one message, one opcode, one minor frame, one major frame.
/* Create Execute Message Command */
status = naibrd_1553_BcCommandCreate(devnum, OP1, NAI_1553_OPCODE_EXECUTE_MESSAGE, NAI_1553_OPCODE_COND_ALWAYS, MSG1, 0, 0);
/* Create Call Subroutine Command */
status = naibrd_1553_BcCommandCreate(devnum, OP2, NAI_1553_OPCODE_CALL_SUBROUTINE, NAI_1553_OPCODE_COND_ALWAYS, MNR1, 0, 0);
/* Create Minor Frame containing one message */
aOpCodes[0] = OP1;
status = naibrd_1553_BcFrameCreate(devnum, MNR1, NAI_1553_BC_FRAME_MINOR, aOpCodes, 1, 0, 0);
/* Create Major Frame containing one minor frame */
aOpCodes[0] = OP2;
status = naibrd_1553_BcFrameCreate(devnum, MJR, NAI_1553_BC_FRAME_MAJOR, aOpCodes, 1, 1000, 0);
The major frame time of 1000 (100 ms in 0.1 ms units) sets the cycle period.
Sending Messages and Reading from the FIFO
The main loop writes data to the data block, clears the FIFO, runs one major frame, stops the BC, and then reads completed messages from the FIFO. This is where the FIFO sample diverges from the standard BC sample.
Clearing the FIFO and Running the BC
Before each transmission cycle, clear the FIFO to discard any stale messages from previous iterations:
status = naibrd_1553_ClearMsgFIFO(devnum);
/* Start BC for 1 major frame */
status = naibrd_1553_BcStart(devnum, MJR, 1);
/* Wait for execution */
nai_msDelay(200);
/* Stop BC */
status = naibrd_1553_BcStop(devnum);
Calling naibrd_1553_ClearMsgFIFO() before each run ensures you only read messages from the current cycle. Without this step, the FIFO may contain leftover messages from a previous BcStart/BcStop cycle, and your decode loop would process stale data.
Reading the FIFO
After stopping the BC, query the FIFO for completed messages and read them into a local buffer:
status = naibrd_1553_GetMsgFIFOCount(devnum, &fifoCount);
if (fifoCount > 0)
{
/* Zero the buffer to prevent decoding stale data */
memset(fifoData, 0, sizeof(fifoData));
/* Read all messages from the FIFO */
status = naibrd_1553_ReadMsgFIFO(devnum, fifoCount, fifoData);
}
naibrd_1553_GetMsgFIFOCount() returns the number of 32-bit words currently in the FIFO. naibrd_1553_ReadMsgFIFO() reads that many words into a local uint32_t array. The memset to zero is important — if you reuse the same buffer across loop iterations, old data that was not overwritten could be misinterpreted by the decode function.
Decoding FIFO Messages
To decode messages from the FIFO buffer, call naibrd_1553_DecodeFIFOMsg() in a loop, advancing the block index after each decoded message:
currBlockIndex = 0;
do
{
status = naibrd_1553_DecodeFIFOMsg(fifoData, currBlockIndex, &nextBlockIndex, &msgStruct);
if (status == 0)
{
printf("Command Word: 0x%04X\n", msgStruct.wCommandWord1);
if (msgStruct.bIsCommandWord2Relevant)
printf("Command Word 2: 0x%04X\n", msgStruct.wCommandWord2);
printf("Block Status: 0x%04X\n", msgStruct.wBlockStatus);
printf("Time Tag: 0x%04X\n", msgStruct.wTimeTag);
printf("Word Count: 0x%04X\n", msgStruct.wDataWordCount);
printf("RT Status Word: 0x%04X\n", msgStruct.wStatus1);
if (msgStruct.bIsStatus2Relevant)
printf("RT Status Word 2: 0x%04X\n", msgStruct.wStatus2);
}
/* Advance to next message in the FIFO buffer */
currBlockIndex = nextBlockIndex;
} while (status == NAI_SUCCESS);
The decode function reads one message at a time from the fifoData buffer starting at currBlockIndex. After decoding, it writes the starting index of the next message into nextBlockIndex. You must update currBlockIndex = nextBlockIndex before the next iteration — failing to advance the index will cause the same message to be decoded repeatedly in an infinite loop.
The decoded structure (naiDecodedMessageStructureFIFO) contains the same fields as the standard decoded message structure: command word(s), block status, time tag, data word count, RT status word(s), and the data payload. For RT-to-BC and mode code Tx messages, the data words contain the RT’s response data.
When no more messages remain in the buffer, the function returns NAI_1553_CMD_NO_MSG_TO_DECODE, which terminates the loop.
Checking FIFO Full Status
After decoding all messages, you can check whether the FIFO overflowed during the BC run:
status = naibrd_1553_GetMsgFIFOFullStatus(devnum, NAI_1553_STATUS_REALTIME, &FIFIOfullStatusBit);
if (status == NAI_SUCCESS)
{
printf("FIFO status: %d", FIFIOfullStatusBit);
printf((FIFIOfullStatusBit == TRUE) ? " Full\r\n" : " Not full\r\n");
}
If the FIFO full status bit is set, the FIFO reached capacity during the BC run and some messages may have been lost. To prevent this, reduce the number of messages per major frame, increase the major frame time, or read the FIFO more frequently. The NAI_1553_STATUS_REALTIME parameter reads the live hardware status rather than a latched value.
|
Important
|
Common Errors
|
Cleanup
When finished, free the logical device to release hardware resources:
status = naibrd_1553_Free(devnum);
Always call naibrd_1553_Free() before exiting, even if errors occurred during operation. The main function also calls naiapp_access_CloseAllOpenCards() to close all board connections.
Troubleshooting Reference
|
Note
|
This table summarizes errors covered in preceding sections. Consult the FTA-FTF Manual for hardware-specific diagnostics. |
| Error / Symptom | Possible Causes | Suggested Resolution |
|---|---|---|
|
Channel already open, invalid card/module/channel index |
Verify indices, ensure no other application holds the channel |
|
Device not reset, invalid mode flags, FIFO mode not supported on this module |
Ensure device reset sequence completed, verify module is FTA/FTB/FTC/FTD/FTF |
|
Device not initialized as BC |
Call after |
Message creation returns non-zero |
Invalid RT address/subaddress, data block not created |
Check parameter ranges, create data block before message |
|
Incomplete frame hierarchy, device not initialized as BC |
Verify all opcodes, minor frames, and major frame are created |
FIFO count is 0 |
BC did not complete message before stop, FIFO was not read in time |
Increase delay between |
Decode loop infinite |
|
Ensure |
Stale or corrupted data decoded |
|
Call |
FIFO full status set |
Too many messages per frame, FIFO overflow |
Reduce messages per frame or increase frame time |
No response from RT |
RT not on bus, bus wiring issue, wrong bus (A/B) selected |
Verify RT is active, check cabling, try other bus |
Software override needed |
BC_DISABLE or M1760 pins not externally driven high |
Enable software override (aux reg 0x2 = 0xA000) for bench testing |
Full Source
Full Source — M1553_BC_SendMessage_FIFO.c (SSK 1.x)
#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"
/* 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"
#include "functions/naibrd_1553_assisted.h"
static const int8_t *CONFIG_FILE = (int8_t *)"default_1553BC_SendMessage_FIFO.txt";
/* Function prototypes */
static bool_t Run_M1553_BC_SendMessage_FIFO(int32_t cardIndex, int32_t module, uint32_t modid);
static bool_t RunSendMessage(int32_t cardIndex, int32_t module, int32_t channel, uint32_t modid);
/* define message constants */
#define MSG1 1
/* define opcodes */
#define OP1 1
#define OP2 2
/* define frame constants */
#define MNR1 1
#define MJR 2
/* define data block numbers */
#define DBLK1 1
#define DBLK2 2
#define DEF_M1553_CARD_INDEX 0
#define DEF_M1553_MODULE 1
#define DEF_M1553_CHANNEL 1
#define DEF_M1553_DEVNUM 1
#define RT_ADDRESS 1
#define RT_SUBADDRESS 2
#define RT_ADDRESS_2 2
#define WORDCOUNT 32
/**************************************************************************************************************/
/**
<summary>
The purpose of the M1553_BC_SendMessage_FIFO is to illustrate the methods to call in the naibrd library to configure
the 1553 channel as a Bus Controller and to send a 1553 message. The message will be decoded using FIFO mode.
Note: FIFO mode is available for the FTA, FTB, FTC, FTD, and FTF only.
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_BC_SendMessage_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_BC_SendMessage_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;
}
/**************************************************************************************************************/
/**
<summary>
Run_M1553_BC_SendMessage_FIFO initializes the 1553 device as a bus controller (BC),
FIFO mode NAI_1553_MESSAGE_FIFO_MODE, and sends out a message.
This routine demonstrates the following API functions in the 1553 naibrd library:
naibrd_1553_Open
naibrd_1553_Initialize
naibrd_1553_BcConfigureMessageGapTimerEnable
naibrd_1553_BcDataBlockCreate
naibrd_1553_BcMessageCreateBcToRt
naibrd_1553_BcMessageCreateRtToBc
naibrd_1553_BcMessageCreateMode
naibrd_1553_BcMessageCreateRtToRt
naibrd_1553_BcCommandCreate
naibrd_1553_BcFrameCreate
naibrd_1553_BcDataBlockWrite
naibrd_1553_ClearMsgFIFO
naibrd_1553_BcStart
naibrd_1553_BcStop
naibrd_1553_GetMsgFIFOCount
naibrd_1553_ReadMsgFIFO
naibrd_1553_DecodeFIFOMsg
naibrd_1553_GetMsgFIFOFullStatus
naibrd_1553_Free
</summary>
*/
/**************************************************************************************************************/
static bool_t Run_M1553_BC_SendMessage_FIFO(int32_t cardIndex, int32_t module, uint32_t modid)
{
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 = RunSendMessage(cardIndex, module, channel, modid);
}
}
else
printf("\nThis module does not support 1553 functionality.\n");
return bQuit;
}
static bool_t RunSendMessage(int32_t cardIndex, int32_t module, int32_t channel, uint32_t modid)
{
int32_t i;
bool_t bQuit = FALSE;
uint32_t usBus;
nai_1553_t status;
uint16_t increment = 0;
uint16_t aData[32] = {0};
int16_t aOpCodes[20] = { 0 };
bool_t bContinue = TRUE;
int16_t devnum;
bool_t isValidModeCode;
M1553MsgType_t msgType;
naiDecodedMessageStructureFIFO msgStruct;
bool_t bSoftware;
M1553ModeCodes_t modeCommand = (M1553ModeCodes_t)0;
uint32_t fifoCount = 0;
uint32_t fifoData[1024];
int32_t currBlockIndex = 0, nextBlockIndex = 0;
bool_t FIFIOfullStatusBit;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
/* Get the Logical Device Number */
bQuit = Get1553LogicalDevNum(DEF_M1553_DEVNUM, &devnum);
if (!bQuit)
{
/* Get Msg Direction */
bQuit = GetMsgTypeAndCheckForQuit(&msgType);
if (!bQuit)
{
/* Which bus are we firing on? */
bQuit = GetBus(&usBus);
if (!bQuit)
{
bQuit = Get1553BCSoftwareOverride(TRUE, &bSoftware);
if (!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 ((msgType == M1553_MSGTYPE_MODECODE_TX) || (msgType == M1553_MSGTYPE_MODECODE_RX))
{
do
{
bQuit = AskAndCheckForValidModeCodeAndCheckForQuit((msgType == M1553_MSGTYPE_MODECODE_TX) ?
M1553_MSGTYPE_MODECODE_DIRECTION_TX : M1553_MSGTYPE_MODECODE_DIRECTION_RX, &modeCommand, &isValidModeCode);
} while ((!bQuit) && (!isValidModeCode));
}
if (!bQuit)
{
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 | NAI_1553_MESSAGE_FIFO_MODE, 0, 0, 0);
if (status != 0)
{
printf("Error: naibrd_1553_Initialize Ch %d, status = %d", channel, status);
return TRUE;
}
/* Enable Gap Timer */
status = naibrd_1553_BcConfigureMessageGapTimerEnable(devnum, TRUE);
if (status != 0)
{
printf("Error: naibrd_1553_BcConfigureMessageGapTimerEnable Ch %d, status = %d", channel, status);
return TRUE;
}
if ((msgType != M1553_MSGTYPE_MODECODE_TX) || (msgType != M1553_MSGTYPE_MODECODE_RX))
{
/* Create BC Data Block */
status = naibrd_1553_BcDataBlockCreate(devnum, DBLK1, WORDCOUNT, NULL, 0);
if (status != 0)
{
printf("Error: naibrd_1553_BcDataBlockCreate Ch %d, status = %d", channel, status);
return TRUE;
}
}
else if ((msgType == M1553_MSGTYPE_MODECODE_TX) || (msgType == M1553_MSGTYPE_MODECODE_RX))
{
status = naibrd_1553_BcDataBlockCreate(devnum, DBLK2, modeCommand, NULL, 0);
if (status != 0)
{
printf("Error: naibrd_1553_BcDataBlockCreate Ch %d, status = %d", channel, status);
return TRUE;
}
}
if (msgType == M1553_MSGTYPE_RTTOBC)
{
/* Create RT to BC Message */
status = naibrd_1553_BcMessageCreateRtToBc(devnum, MSG1, DBLK1, RT_ADDRESS, RT_SUBADDRESS, WORDCOUNT, 1, usBus);
if (status != 0)
{
printf("Error: naibrd_1553_BcMessageCreateRtToBc status = %d", status);
return TRUE;
}
}
else if (msgType == M1553_MSGTYPE_BCTORT)
{
/* Create BC to RT Message */
status = naibrd_1553_BcMessageCreateBcToRt(devnum, MSG1, DBLK1, RT_ADDRESS, RT_SUBADDRESS, WORDCOUNT, 1, usBus);
if (status != 0)
{
printf("Error: naibrd_1553_BcMessageCreateBcToRt status = %d", status);
return TRUE;
}
}
else if ((msgType == M1553_MSGTYPE_MODECODE_TX) || (msgType == M1553_MSGTYPE_MODECODE_RX))
{
/* Create Mode Code Tx Message */
status = naibrd_1553_BcMessageCreateMode(devnum, MSG1, DBLK1, RT_ADDRESS, (msgType == M1553_MSGTYPE_MODECODE_TX) ?
NAI_1553_CMD_TX : NAI_1553_CMD_RX, modeCommand, 0, usBus);
if (status != 0)
{
printf("Error: naibrd_1553_BcMessageCreateMode status = %d", status);
return TRUE;
}
}
else if (msgType == M1553_MSGTYPE_RTTORT)
{
/* Create RT to RT Message */
status = naibrd_1553_BcMessageCreateRtToRt(devnum, MSG1, DBLK1, RT_ADDRESS_2, RT_SUBADDRESS, WORDCOUNT, RT_ADDRESS, RT_SUBADDRESS, 0, usBus);
if (status != 0)
{
printf("Error: naibrd_1553_BcMessageCreateRtToRt status = %d", status);
return TRUE;
}
}
/* Create Execute Message Command */
status = naibrd_1553_BcCommandCreate(devnum, OP1, NAI_1553_OPCODE_EXECUTE_MESSAGE, NAI_1553_OPCODE_COND_ALWAYS, MSG1, 0, 0);
if (status != 0)
{
printf("Error: naibrd_1553_BcCommandCreate status = %d", status);
return TRUE;
}
/* Create Call Subroutine Command */
status = naibrd_1553_BcCommandCreate(devnum, OP2, NAI_1553_OPCODE_CALL_SUBROUTINE, NAI_1553_OPCODE_COND_ALWAYS, MNR1, 0, 0);
if (status != 0)
{
printf("Error: naibrd_1553_BcCommandCreate status = %d", status);
return TRUE;
}
/* Create Minor Frame */
aOpCodes[0] = OP1;
status = naibrd_1553_BcFrameCreate(devnum, MNR1, 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] = OP2;
status = naibrd_1553_BcFrameCreate(devnum, MJR, NAI_1553_BC_FRAME_MAJOR, aOpCodes, 1, 1000, 0);
if (status != 0)
{
printf("Error: naibrd_1553_BcFrameCreate status = %d", status);
return TRUE;
}
while (bContinue)
{
/* Load BC data block with incremental data */
for (i = 0; i < WORDCOUNT; i++)
{
aData[i] = increment++;
}
status = naibrd_1553_BcDataBlockWrite(devnum, DBLK1, aData, WORDCOUNT, 0);
if (status != 0)
{
printf("Error: naibrd_1553_BcDataBlockWrite status = %d", status);
return TRUE;
}
status = naibrd_1553_ClearMsgFIFO(devnum);
if (status != 0)
{
printf("Error: naibrd_1553_ClearMsgFIFO status = %d", status);
return TRUE;
}
/* Start BC */
status = naibrd_1553_BcStart(devnum, MJR, 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;
}
status = naibrd_1553_GetMsgFIFOCount(devnum, &fifoCount);
if (status != 0)
{
printf("Error: naibrd_1553_GetMsgFIFOCount %d", status);
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 */
status = naibrd_1553_ReadMsgFIFO(devnum, fifoCount, fifoData);
if (status != 0)
{
printf("Error: naibrd_1553_ReadMsgFIFO %d", status);
return TRUE;
}
else
{
/* Read Messages */
do
{
status = naibrd_1553_DecodeFIFOMsg(fifoData, currBlockIndex, &nextBlockIndex, &msgStruct);
if (status == 0)
{
printf("Command Word: 0x%04X\n", msgStruct.wCommandWord1);
if(msgStruct.bIsCommandWord2Relevant)
printf("Command Word 2: 0x%04X\n", msgStruct.wCommandWord2);
printf("Block Status: 0x%04X\n", msgStruct.wBlockStatus);
printf("Time Tag: 0x%04X\n", msgStruct.wTimeTag);
printf("Word Count: 0x%04X\n", msgStruct.wDataWordCount);
printf("RT Status Word: 0x%04X\n", msgStruct.wStatus1);
if (msgStruct.bIsStatus2Relevant)
printf("RT Status Word 2: 0x%04X\n", msgStruct.wStatus2);
if ((msgType == M1553_MSGTYPE_RTTOBC) || (msgType == M1553_MSGTYPE_MODECODE_TX))
{
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("\n\n");
}
else if(status != NAI_1553_CMD_NO_MSG_TO_DECODE)
{
printf("Error: naibrd_1553_DecodeFIFOMsg %d", status);
return TRUE;
}
/*** IMPORTANT !!! ***/
currBlockIndex = nextBlockIndex;
nai_msDelay(1);
} while ((status == NAI_SUCCESS) && (bContinue));
}
status = naibrd_1553_GetMsgFIFOFullStatus(devnum, NAI_1553_STATUS_REALTIME, &FIFIOfullStatusBit);
if (status == NAI_SUCCESS)
{
printf("FIFO status: %d", FIFIOfullStatusBit);
printf((FIFIOfullStatusBit == TRUE) ? " Full\r\n" : " Not full\r\n");
}
else
{
printf("Error: naibrd_1553_GetMsgFIFOFullStatus %d", status);
return TRUE;
}
}
printf("\nPress any key to send another message or Q to quit.");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (bQuit)
{
bContinue = FALSE;
}
}
}
}
}
}
}
}
/* Free 1553 Device */
status = naibrd_1553_Free(devnum);
if (status != 0)
{
printf("Error: naibrd_1553_Free status = %d", status);
return TRUE;
}
return bQuit;
}