M1553 RT Status Bits
Edit this on GitLab
M1553 RT Status Bits Sample Application (SSK 2.x)
Overview
The M1553 RT Status Bits sample application demonstrates how to configure a MIL-STD-1553 Remote Terminal (RT) and set the RT response status bits using the NAI Software Support Kit (SSK 2.x). This sample extends the basic RT Recv sample by adding the ability to configure which status bits the RT includes in its response status word when replying to BC commands.
The MIL-STD-1553 status word contains several programmable bits that the RT uses to communicate its operational state to the Bus Controller. These include the Busy bit, Service Request, Subsystem Flag, RT Flag, and Dynamic Bus Control Acceptance. For MIL-STD-1760 modules, the Instrumentation bit is also available. The sample also supports an alternate status mode that provides a different set of configurable status bits.
For detailed 1553 protocol specifications, message formats, and hardware register descriptions, see the FTA-FTF Manual.
For the SSK 1.x version, see M1553 RT Receive StatusBits (SSK 1.x).
Prerequisites
Before running this sample, make sure you have:
-
An NAI board with a 1553 module installed (FTA-FTF, FTJ-FTK).
-
SSK 2.x installed on your development host.
-
The sample applications built. Refer to the SSK 2.x Software Development Guide for platform-specific build instructions.
-
A Bus Controller on the same 1553 bus to send commands to this RT.
How to Run
Launch the m1553_rt_status_bits executable from your build output directory. On startup the application looks for a configuration file (default_1553_RTReceive_StatusBits.txt). Once connected, the application prompts for RT configuration, Rx buffer type (single, double, or circular), alternate status mode selection, and status bit mask configuration. The RT then runs for a user-specified duration while displaying received messages and the active status bit mask.
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. For details on board connection configuration, see the First Time Setup Guide. |
The main() function follows the standard SSK 2.x startup flow: board menu, card index query, module selection, and module ID retrieval. After confirming a valid module is present, it calls Run_M1553_RT_Receive().
#if defined (NAIBSP_CONFIG_SOFTWARE_OS_VXWORKS)
int32_t m1553_rt_receive_status_bits(void)
#else
int32_t main(void)
#endif
{
bool_t stop = NAIBSP_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) == NAIBSP_TRUE)
{
while (stop != NAIBSP_TRUE)
{
stop = naiapp_query_CardIndex(naiapp_GetBoardCnt(), 0, &cardIndex);
if (stop != NAIBSP_TRUE)
{
check_status(naibrd_GetModuleCount(cardIndex, &moduleCnt));
stop = naiapp_query_ModuleNumber(moduleCnt, 1, &module);
if (stop != NAIBSP_TRUE)
{
check_status(naibrd_GetModuleName(cardIndex, module, &moduleID));
if ((moduleID != 0))
{
Run_M1553_RT_Receive(cardIndex, module, moduleID, NAIBSP_FALSE);
}
}
}
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;
}
|
Important
|
Common Connection Errors
|
Program Structure
Entry Point
The program entry point is main() on most platforms or m1553_rt_receive_status_bits() on VxWorks. After the board connection and module selection, the application calls Run_M1553_RT_Receive(), which handles all RT-specific configuration and message processing.
User Input Flow
Once inside Run_M1553_RT_Receive(), the application collects RT operating parameters through a series of utility functions:
-
Get1553RTCfg()— prompts for the 1553 channel number and RT address. -
Get1553Address()— prompts for the subaddress to legalize for Tx and Rx traffic. Valid subaddresses are 1 through 31. The default is subaddress 2. -
Get1553LogicalDevNum()— prompts for a logical device number used as a software handle for subsequent API calls. The default is device 1. -
Get1553RTAddressSource()— asks whether to set the RT address in software or use the hardware address pins. -
Get1553RxBufferType()— selects the Rx buffer type (single, double, or circular). The default is double-buffered.
Application Flow
-
Configure RT channel and address via
Get1553RTCfg(). -
Open and initialize the 1553 channel in RT mode.
-
Configure RT address source (software or hardware).
-
Select Rx buffer type (single, double, or circular) via
Get1553RxBufferType(). -
Optionally enable alternate status mode via
naibrd_1553_RtCfg(). -
Create Rx and Tx data blocks and map them to the target subaddress.
-
Configure status bit mask via
SetStatusBits(). -
Start RT and process messages for the specified duration.
RT Initialization
The application opens the 1553 channel and initializes it in RT mode:
/* Associate Card, Module and Channel Numbers with the Logical Device # */
swResult = naibrd_1553_Open(cardIndex, module, rtchan, DevNum);
if(swResult)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_Open %d", swResult);
return bQuit;
}
/* Initialize Device */
swResult = naibrd_1553_Init(DevNum, NAIBRD_1553_ACCESS_CARD, NAIBRD_1553_MODE_RT, 0, 0, 0);
-
naibrd_1553_Open()associates the card, module, and channel with a logical device number. All subsequent API calls use this device number. -
naibrd_1553_Init()initializes the device in RT mode.NAIBRD_1553_ACCESS_CARDindicates direct hardware access. The three trailing zeros are reserved parameters unused for RT initialization.
RT Address Configuration
The RT address can be set from two sources: software control or hardware address pins. The application calls Get1553RTAddressSource() to let the user choose.
Software Addressing
When software addressing is selected, the application writes to the auxiliary registers to enable software RT address control, then programs the desired RT address:
if (bSoftwareRTAddr)
{
/* Set RTAD_SW_EN and RT_ADR_LAT in software */
swResult = naibrd_1553_WriteAuxRegister(DevNum, (naibrd_1553_general_auxRegAddress_t)0x2, 0x0018);
/* Set RT address */
swResult = naibrd_1553_RtSetAddr(DevNum, rtaddr);
}
The value 0x0018 sets both the RTAD_SW_EN and RT_ADR_LAT bits in auxiliary register 0x2. This enables software-controlled RT address assignment. naibrd_1553_RtSetAddr() then programs the desired address.
Hardware Pin Addressing
When hardware addressing is selected, the application configures the auxiliary register to read the RT address from the module’s external address pins:
else
{
/* Unset RTAD_SW_EN and set RT_ADR_LAT in software */
swResult = naibrd_1553_WriteAuxRegister(DevNum, (naibrd_1553_general_auxRegAddress_t)0x2, 0x0008);
}
The value 0x0008 sets only the RT_ADR_LAT bit (without RTAD_SW_EN), so the RT address is read from the hardware pins.
Hardware pin addressing is required in some MIL-STD-1760 installations where the platform wiring determines the RT address.
|
Important
|
|
Data Block Creation and Subaddress Mapping
The sample creates three data blocks to handle Rx and Tx traffic on the configured subaddress.
Data Block IDs
| Constant | ID | Description |
|---|---|---|
|
1 |
First Tx data block (single, 32 words). Used for ping-pong buffering. |
|
2 |
Second Tx data block (single, 32 words). Used for ping-pong buffering. |
|
3 |
Rx data block (user-selected type: single, double, or circular). Receives BC-to-RT messages. |
Rx Data Block Creation and Mapping
The application creates an Rx data block using the user-selected buffer type and maps it to the target subaddress:
/* Create a Rx Buffer data block and map to the desired subaddress */
swResult = naibrd_1553_RtDataBlkCreate(DevNum, DATA_BLOCK_ID_RX, nDataBlockType, NULL, 0);
swResult = naibrd_1553_RtDataBlkMapToSA(DevNum, DATA_BLOCK_ID_RX, sa, NAIBRD_1553_RT_MESSAGE_TYPE_RX,
(naibrd_1553_rt_irqOptions_t)0, 1);
naibrd_1553_RtDataBlkCreate() allocates the data block on the module. When NAIBRD_1553_RT_DATABLOCK_DOUBLE is selected, the hardware maintains two buffers (A and B) and automatically swaps between them on each message, preventing data tearing. The NULL and 0 arguments are for an optional user-supplied buffer pointer and size (not used here).
naibrd_1553_RtDataBlkMapToSA() binds the block to a subaddress. The final argument (1) is the legalization flag — setting it to 1 legalizes the subaddress so the RT will respond to BC commands targeting it.
Tx Double-Buffering Pattern
For transmit, the sample creates two single-type data blocks and maps the first one as the active Tx block:
/* Create two Tx Buffer data blocks and map the first to the desired subaddress */
swResult = naibrd_1553_RtDataBlkCreate(DevNum, DATA_BLOCK_ID_TX1, NAIBRD_1553_RT_DATABLOCK_SINGLE_32, NULL, 0);
swResult = naibrd_1553_RtDataBlkCreate(DevNum, DATA_BLOCK_ID_TX2, NAIBRD_1553_RT_DATABLOCK_SINGLE_32, NULL, 0);
swResult = naibrd_1553_RtDataBlkMapToSA(DevNum, DATA_BLOCK_ID_TX1, sa, NAIBRD_1553_RT_MESSAGE_TYPE_TX,
(naibrd_1553_rt_irqOptions_t)0, 1);
currDataBlock = DATA_BLOCK_ID_TX1;
TX1 is mapped as the active block and currDataBlock tracks which block the BC is currently reading from. The application writes new data into the inactive block, then swaps. This ensures the BC always reads a complete, consistent data set.
|
Note
|
Rx double-buffering is handled automatically by the hardware when you create a single DOUBLE-type block. Tx double-buffering is done explicitly in software using two SINGLE-type blocks and the naibrd_1553_RtTxDataBlkSwap() API.
|
|
Important
|
|
Setting Status Bits (SetStatusBits)
The SetStatusBits() function is the core of this sample. It demonstrates how to configure the RT response status word — the 16-bit word the RT sends back to the BC after every command. The MIL-STD-1553 specification defines several programmable bits in this word that the RT uses to communicate its operational state.
The function supports three operating modes depending on the module type and user selection:
-
Standard mode (default) — available on all modules.
-
Alternate status mode — available on non-1760 modules, provides a different set of configurable bits.
-
1760 mode — standard mode with an additional Instrumentation bit.
Standard Mode Status Bits
In standard mode (the default), the following bits are available:
| Constant | Value | Description |
|---|---|---|
|
|
Dynamic Bus Control Acceptance — indicates the RT can accept bus control. |
|
|
Busy — tells the BC the RT cannot process the command. |
|
|
Service Request — the RT needs servicing by the BC. |
|
|
Subsystem Flag — indicates a fault in the subsystem attached to the RT. |
|
|
RT Flag — a general-purpose flag set by RT application logic. |
For MIL-STD-1760 modules, an additional bit is available in standard mode:
| Constant | Value | Description |
|---|---|---|
|
|
Instrumentation — 1760-specific status indicator. |
Alternate Mode Status Bits (non-1760 modules)
When alternate status mode is enabled, a different set of bits is available:
| Constant | Value | Description |
|---|---|---|
|
|
Terminal Flag |
|
|
Subsystem Flag |
|
|
Busy |
|
|
Service Request |
|
|
Instrumentation |
|
|
Message Error (read-only on 1760) |
Building the Status Bit Mask
The function presents a numbered menu and the user selects one or more options (space-separated). The selections are OR’ed together to form the mask. In standard mode the mapping looks like:
if (maskInputArr[i] == '2')
{
*statusBitMask |= NAIBRD_1553_RT_STATUS_BIT_DBCA;
}
else if (maskInputArr[i] == '3')
{
*statusBitMask |= NAIBRD_1553_RT_STATUS_BIT_BUSY;
}
else if (maskInputArr[i] == '4')
{
*statusBitMask |= NAIBRD_1553_RT_STATUS_BIT_SERVICE_REQUEST;
}
else if (maskInputArr[i] == '5')
{
*statusBitMask |= NAIBRD_1553_RT_STATUS_BIT_SUBSYSTEM_FLAG;
}
else if (maskInputArr[i] == '6')
{
*statusBitMask |= NAIBRD_1553_RT_STATUS_BIT_RT_FLAG;
}
In alternate mode, the same menu numbers map to the alternate bit constants instead (e.g., selection 2 maps to NAIBRD_1553_ALT_RT_STATUS_BIT_TERMINALFLAG).
Applying the Status Bits
Once the mask is constructed, the function applies it using three API calls:
/* Clear all existing status bits */
swResult = naibrd_1553_RtRespStatusBitUnset(DevNum, 0xFFFF);
/* Set the selected status bits */
swResult = naibrd_1553_RtRespStatusBitSet(DevNum, *statusBitMask);
/* Read back the applied mask to confirm */
swResult = naibrd_1553_RtRespStatusBitGet(DevNum, statusBitMask);
-
naibrd_1553_RtRespStatusBitUnset()clears all status bits first by passing0xFFFFas the mask. This ensures a clean starting state. -
naibrd_1553_RtRespStatusBitSet()sets the selected bits. The RT will include these bits in every status word it transmits back to the BC. -
naibrd_1553_RtRespStatusBitGet()reads back the actual mask from the hardware for confirmation.
The status bits must be set before calling naibrd_1553_RtStart(). Once the RT is running, the configured status bits are included in every response.
Alternate Status Mode Configuration
For non-1760 modules, the sample can enable alternate status mode. This mode changes the meaning of several bits in the RT status word and is configured before data block creation:
if (!IsFTx1760(modid))
{
/* ... prompt user for alternate mode ... */
if ((inputBuffer[0] == 'y') || (inputBuffer[0] == 'Y'))
{
isAlternateMode = NAIBSP_TRUE;
swResult = naibrd_1553_RtDataBlkGetSize(nDataBlockType, &wordSize, &dataBlockSize);
wRtCommandStackSize = ((65536 <= wordSize * 1024) ? NAIBRD_1553_RT_CMDSTK_SIZE_2048 :
((32768 <= wordSize) ? NAIBRD_1553_RT_CMDSTK_SIZE_1024 :
((16384 <= wordSize) ? NAIBRD_1553_RT_CMDSTK_SIZE_512 :
NAIBRD_1553_RT_CMDSTK_SIZE_256)));
swResult = naibrd_1553_RtCfg(DevNum, wRtCommandStackSize, NAIBRD_1553_RT_OPT_ALTERNATE_STATUS);
}
}
The command stack size is calculated based on the data block type to ensure the RT has sufficient memory for its command stack. naibrd_1553_RtCfg() with NAIBRD_1553_RT_OPT_ALTERNATE_STATUS enables alternate status mode on the device. This call must happen before SetStatusBits() so that the correct set of status bit constants are used.
|
Note
|
Alternate status mode is not available on FTJ/FTK (1760) modules. The IsFTx1760() check prevents the option from being offered on those modules.
|
Processing Messages
The ProcessMessages() function is the core runtime loop. It runs for a user-specified duration (default 5 seconds), polling the RT command stack every 10 ms for new messages. On each iteration it reads raw messages from the stack, decodes them, displays the message details along with the active status bit mask, and periodically updates the Tx data block contents.
Stack-Based Message Retrieval
Each iteration calls naibrd_1553_RtMsgGetFromStackRaw() to read one raw message from the RT command stack:
swResult = naibrd_1553_RtMsgGetFromStackRaw(DevNum, wsBuffer, NAIBRD_1553_MAX_MESSAGE_SIZE_RT);
if (swResult < 0)
{
printf("Error: naibrd_1553_RtMessageGetFromStackRaw %d\r\n\r\n", swResult);
return 0;
}
else if (swResult > 0)
{
/* Decode Raw Message */
swResult = naibrd_1553_RtMsgDecodeRaw(DevNum, wsBuffer, &DecodedMsgStruct);
}
The return value conventions are:
-
Less than 0 — an error occurred.
-
0 — no new messages are available.
-
Greater than 0 — a message was retrieved (value is the message size).
The stack is a hardware-managed FIFO. If it fills before the application reads messages out, older messages are lost. Call the retrieval function frequently enough to keep up with the incoming message rate.
Message Type Discrimination
The command word’s TR (transmit/receive) bit — bit 10 (mask 0x0400) — determines the direction of the transfer:
if ((DecodedMsgStruct.commandWord1 & 0x0400) != 0x0400) /* If this is a Rx message */
{
printf("Rx Msg Received\n");
printf("Block Status - 0x%04X\n", DecodedMsgStruct.blockStatus);
printf("Time Tag - 0x%04X\n", DecodedMsgStruct.timeTag);
printf("Command Word - 0x%04X\n", DecodedMsgStruct.commandWord1);
printf("Data Word Count - 0x%04X\n", DecodedMsgStruct.dataWordCount);
printf("Data:");
for (i = 0; i < DecodedMsgStruct.dataWordCount; i++)
{
if (i % 8 == 0)
printf("\n");
printf("0x%04X ", DecodedMsgStruct.data[i]);
}
printf("count: %d\n", count++);
printf("Status Bit mask = %X\n", statusMask);
}
else
{
printf("Tx Msg Received\n");
printf("Block Status - 0x%04X\n", DecodedMsgStruct.blockStatus);
printf("Time Tag - 0x%04X\n", DecodedMsgStruct.timeTag);
printf("Command Word - 0x%04X\n", DecodedMsgStruct.commandWord1);
printf("Data Word Count - 0x%04X\n", DecodedMsgStruct.dataWordCount);
printf("count: %d\n", count++);
printf("Status Bit mask = %X\n", statusMask);
}
-
When the TR bit is clear (
0), this is an Rx message — the BC sent data to the RT. The application prints the full decoded message including the data payload. -
When the TR bit is set (
1), this is a Tx message — the BC requested data from the RT. The application prints the header fields but omits the data words since the data was written by the RT itself.
In both cases the active statusMask is displayed so the user can verify which status bits are being included in the RT’s responses.
Decoded Message Fields
Each decoded message (naibrd_1553_msgstruct_t) contains:
| Field | Description |
|---|---|
|
Hardware-generated status flags for the message transfer. Indicates error conditions, bus selection (A or B), and message type. |
|
Hardware timestamp marking when the transaction was received. Resolution depends on module configuration. |
|
The 1553 command word containing the RT address (bits 15-11), TR bit (bit 10), subaddress (bits 9-5), and word count (bits 4-0). |
|
Number of data words in the message payload (0 to 32). |
|
The actual data words. Displayed for Rx messages only. Up to 32 16-bit words. |
Tx Buffer Ping-Pong (UpdateTxDataBlock)
Every 1 second during the monitoring loop, UpdateTxDataBlock() refreshes the Tx data by writing to the inactive buffer and swapping it with the active buffer. This ping-pong pattern ensures the BC always reads a complete, consistent data set — it never sees a partially-updated buffer.
UpdateTxDataBlock()
This function determines which data block is currently inactive, writes new data to it, and then atomically swaps it to become the active block:
static int32_t UpdateTxDataBlock(uint16_t DevNum, uint8_t Subaddress)
{
if (currDataBlock == DATA_BLOCK_ID_TX1)
{
UpdateTxBuffer(DevNum, DATA_BLOCK_ID_TX2);
naibrd_1553_RtTxDataBlkSwap(DevNum, DATA_BLOCK_ID_TX2, Subaddress);
currDataBlock = DATA_BLOCK_ID_TX2;
}
else
{
UpdateTxBuffer(DevNum, DATA_BLOCK_ID_TX1);
naibrd_1553_RtTxDataBlkSwap(DevNum, DATA_BLOCK_ID_TX1, Subaddress);
currDataBlock = DATA_BLOCK_ID_TX1;
}
return 0;
}
naibrd_1553_RtTxDataBlkSwap() atomically changes the subaddress mapping to point to the newly-written block. The swap is atomic from the BC’s perspective.
UpdateTxBuffer() and naibrd_1553_RtDataBlkWrite()
UpdateTxBuffer() increments a static 32-word array and writes it to the specified data block:
static int32_t UpdateTxBuffer(uint16_t DevNum, uint16_t nDataBlkID)
{
static uint16_t wBuffer[32] = { 0x0000 };
uint16_t i = 0x0000;
uint32_t swResult;
/* Increment Tx buffer data */
for (i = 0; i < 32; i++)
{
wBuffer[i] += 1;
}
/* Write new data to Tx buffer */
swResult = naibrd_1553_RtDataBlkWrite(DevNum, nDataBlkID, wBuffer, RTBC_WORDCNT, 0);
if (swResult < 0)
{
printf("Error: naibrd_1553_RtDataBlockWrite %d\n\n", swResult);
return 0;
}
else
{
printf("New data written to Tx Buffer\n\n");
printf("Status Bit mask = %X\n", statusMask);
}
return 1;
}
The naibrd_1553_RtDataBlkWrite() parameters are the device number, block ID, data buffer pointer, word count (RTBC_WORDCNT = 32), and offset (0 = start of the block). The static wBuffer array persists across calls so the data increments each time, allowing the BC to verify it is receiving updated data.
Troubleshooting Reference
| Error / Symptom | Possible Causes | Suggested Resolution |
|---|---|---|
|
Channel already open, invalid indices |
Verify indices, ensure no other application holds the channel |
Status bits not appearing in RT response |
Status bits not set before RT start, wrong mode |
Call |
Alternate status mode not available |
Module is FTJ/FTK (1760) |
Alternate status mode is only available on FTA-FTF modules |
|
Invalid command stack size, device not initialized |
Ensure |
RT does not respond to BC |
Wrong RT address, subaddress not legalized |
Verify RT address and subaddress mapping |
Full Source
The complete source for this sample is provided below for reference. The sections above explain each part in detail.
Full Source — m1553_rt_status_bits.c (SSK 2.x)
/* nailib include files */
#include "nai_libs/nailib/include/naitypes.h"
#include "nai_libs/nailib/include/nailib.h"
#include "nai_libs/nailib/include/nailib_utils.h"
/* naibrd include files */
#include "nai_libs/naibrd/include/naibrd.h"
#include "nai_libs/naibrd/include/functions/naibrd_1553.h"
/* naiif include files */
#include "nai_libs/naiif/include/naiif.h"
#include "nai_libs/naiif/include/naiif_stdio.h"
/* Common Sample Program include files */
#include "nai_sample_apps/naiapp_common/include/naiapp_boardaccess_menu.h"
#include "nai_sample_apps/naiapp_common/include/naiapp_boardaccess_query.h"
#include "nai_sample_apps/naiapp_common/include/naiapp_boardaccess_access.h"
#include "nai_sample_apps/naiapp_common/include/naiapp_boardaccess_display.h"
#include "nai_sample_apps/naiapp_common/include/naiapp_boardaccess_utils.h"
/* Common 1553 Sample Program include files */
#include "nai_sample_apps/naiapp_src/board_modules/1553/m1553_common_utils/m1553_common_utils.h"
#include "nai_sample_apps/naiapp_src/board_modules/1553/m1553_common_utils/BC/m1553_bc_common_utils.h"
static const int8_t *CONFIG_FILE = (int8_t *)"default_1553_RTReceive_StatusBits.txt";
/* Function prototypes */
static bool_t Run_M1553_RT_Receive(int32_t cardIndex, int32_t module, uint32_t modid, bool_t isAlternateMode);
static int32_t UpdateTxDataBlock(uint16_t DevNum, uint8_t Subaddress);
static int32_t UpdateTxBuffer(uint16_t DevNum, uint16_t nDataBlkID);
static int32_t ProcessMessages(uint16_t DevNum, uint8_t Subaddress, int32_t duration);
static int32_t SetStatusBits(uint16_t DevNum, uint32_t modid, bool_t isAlternateMode, uint16_t *statusBitMask);
static const uint16_t DATA_BLOCK_ID_TX1 = 1;
static const uint16_t DATA_BLOCK_ID_TX2 = 2;
static const uint16_t DATA_BLOCK_ID_RX = 3;
static const int16_t DEF_RT_CARD_INDEX = 0;
static const int32_t DEF_RT_MODULE = 1;
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 uint8_t DEF_RT_SUBADDR = 2;
static const uint16_t DEF_RT_RX_BUF_TYPE = NAIBRD_1553_RT_DATABLOCK_DOUBLE;
static const uint16_t RTBC_WORDCNT = 32;
/* Global Variables */
static int32_t currDataBlock;
#define NO_MASK 0x0000
static uint16_t statusMask;
/**************************************************************************************************************/
/** \defgroup M1553_RT_StatusBits
\brief This sample application demonstrates how to configure a channel as a Remote Terminal and set the RT status bits.
The purpose of the m1553_rt_receive_status_bits is to illustrate the methods to call in the naibrd library to configure
the 1553 channel as a Remote Terminal, legalize Subaddress 2 for both Tx and Rx, set the RT stautus bits, and set it up to receive data
using an Rx double buffer and transmit data from two alternating Tx buffers (active and inactive). If a Rx message
is received, the received data will be displayed. If a Tx message is received, the data in the active Tx buffer will be sent to the bus and
display the status word. While the RT is running, Tx buffer data will be updated periodically by incrementing all 32 data words of Tx data.
The main steps include:
- Querying the user for the card index and module number.
- Configuring the channel for Remote Terminal functionality.
- Starting the RT to start receiving messages.
- Setting the RT response status bits to those selected by the user.
- Displaying any messages received by the RT, while periodically updating the TX buffer.
*/
/**************************************************************************************************************/
#if defined (NAIBSP_CONFIG_SOFTWARE_OS_VXWORKS)
int32_t m1553_rt_receive_status_bits(void)
#else
int32_t main(void)
#endif
{
bool_t stop = NAIBSP_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) == NAIBSP_TRUE)
{
while (stop != NAIBSP_TRUE)
{
/* Query the user for the card index */
stop = naiapp_query_CardIndex(naiapp_GetBoardCnt(), 0, &cardIndex);
if (stop != NAIBSP_TRUE)
{
check_status(naibrd_GetModuleCount(cardIndex, &moduleCnt));
/* Query the user for the module number */
stop = naiapp_query_ModuleNumber(moduleCnt, 1, &module);
if (stop != NAIBSP_TRUE)
{
check_status(naibrd_GetModuleName(cardIndex, module, &moduleID));
if ((moduleID != 0))
{
Run_M1553_RT_Receive(cardIndex, module, moduleID, NAIBSP_FALSE);
}
}
}
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_Receive(int32_t cardIndex, int32_t module, uint32_t modid, bool_t isAlternateMode)
{
/* Variables */
bool_t bQuit = NAIBSP_FALSE;
int32_t rtchan;
naibrd_1553_rt_address_t rtaddr;
naibrd_1553_rt_subaddress_t sa = DEF_RT_SUBADDR;
int16_t DevNum = 0;
int32_t swResult;
naibrd_1553_rt_dataBlockType_t nDataBlockType = NAIBRD_1553_RT_DATABLOCK_DOUBLE;
bool_t bContinue = NAIBSP_TRUE;
int32_t duration;
bool_t bSoftwareRTAddr;
uint16_t wordSize;
uint16_t dataBlockSize;
naibrd_1553_rt_commandStackSize_t wRtCommandStackSize;
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;
}
printf("Enter Subaddress\n");
bQuit = Get1553Address(31, DEF_RT_SUBADDR, &sa);
/* 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 = NAIBSP_TRUE;
printf("Error: naibrd_1553_Open %d", swResult);
return bQuit;
}
/* Initialize Device */
swResult = naibrd_1553_Init(DevNum,NAIBRD_1553_ACCESS_CARD, NAIBRD_1553_MODE_RT, 0, 0, 0);
if(swResult != 0)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_Initialize %d", swResult);
return bQuit;
}
/* Get RT Address Source from user */
bQuit = Get1553RTAddressSource(NAIBSP_TRUE, &bSoftwareRTAddr);
if (bQuit)
{
return bQuit;
}
if (bSoftwareRTAddr)
{
/* Set RTAD_SW_EN and RT_ADR_LAT in software */
swResult = naibrd_1553_WriteAuxRegister(DevNum, (naibrd_1553_general_auxRegAddress_t)0x2, 0x0018);
/* Set RT address */
swResult = naibrd_1553_RtSetAddr(DevNum, rtaddr);
if(swResult != 0)
{
bQuit = NAIBSP_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_WriteAuxRegister(DevNum, (naibrd_1553_general_auxRegAddress_t)0x2, 0x0008);
}
/* Will be enabled in future */
#if 0
if (modid == NAIBRD_MODULE_ID_FT8)
{
/* Enable(for internal NAI testing only, do not enable) */
naibrd_1553_WriteAuxRegister(DevNum, NAIBRD_1553_AUX_ADDRESS_SIMPLEX, 0x4000);
naibrd_1553_WriteAuxRegister(DevNum, 0xF, 0x1);
}
#endif
/* Select Single Buffer, Double Buffer or Circular Buffer for Rx Messages */
bQuit = Get1553RxBufferType(NAIBRD_1553_RT_DATABLOCK_DOUBLE, &nDataBlockType);
if (bQuit)
{
return bQuit;
}
if (!IsFTx1760(modid))
{
do
{
inputBuffer[0] = 'N';
printf("Do you want to use Alternate Status Mode? y or n default = No\n");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (inputBuffer[0] == '\0')
{
inputBuffer[0] = 'n';
}
} while (inputBuffer[0] != 'y' && inputBuffer[0] != 'Y' && inputBuffer[0] != 'n' && inputBuffer[0] !='N');
if ((inputBuffer[0] == 'y') || (inputBuffer[0] == 'Y'))
{
isAlternateMode = NAIBSP_TRUE;
swResult = naibrd_1553_RtDataBlkGetSize(nDataBlockType, &wordSize, &dataBlockSize);
if (swResult != 0)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_RtDataBlockGetSize %d", swResult);
return bQuit;
}
wRtCommandStackSize = ((65536 <= wordSize * 1024) ? NAIBRD_1553_RT_CMDSTK_SIZE_2048 : ((32768 <= wordSize) ? NAIBRD_1553_RT_CMDSTK_SIZE_1024 :
((16384 <= wordSize) ? NAIBRD_1553_RT_CMDSTK_SIZE_512 : NAIBRD_1553_RT_CMDSTK_SIZE_256)));
swResult = naibrd_1553_RtCfg(DevNum, wRtCommandStackSize, NAIBRD_1553_RT_OPT_ALTERNATE_STATUS);
if (swResult != 0)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_RtInitialize %d", swResult);
return bQuit;
}
}
}
/* Create a Rx Buffer data block and map to the desired subaddress */
swResult = naibrd_1553_RtDataBlkCreate(DevNum, DATA_BLOCK_ID_RX, nDataBlockType, NULL, 0);
if(swResult != 0)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_RtDataBlockCreate %d", swResult);
return bQuit;
}
swResult = naibrd_1553_RtDataBlkMapToSA(DevNum, DATA_BLOCK_ID_RX, sa, NAIBRD_1553_RT_MESSAGE_TYPE_RX, (naibrd_1553_rt_irqOptions_t)0, 1);
if(swResult != 0)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_RtDataBlockMapToSubaddress %d", swResult);
return bQuit;
}
/* Create two Tx Buffer data blocks and map the first to the desired subaddress */
swResult = naibrd_1553_RtDataBlkCreate(DevNum, DATA_BLOCK_ID_TX1, NAIBRD_1553_RT_DATABLOCK_SINGLE_32, NULL, 0);
if(swResult != 0)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_RtDataBlockCreate %d", swResult);
return bQuit;
}
swResult = naibrd_1553_RtDataBlkCreate(DevNum, DATA_BLOCK_ID_TX2, NAIBRD_1553_RT_DATABLOCK_SINGLE_32, NULL, 0);
if(swResult != 0)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_RtDataBlockCreate %d", swResult);
return bQuit;
}
swResult = naibrd_1553_RtDataBlkMapToSA(DevNum, DATA_BLOCK_ID_TX1, sa, NAIBRD_1553_RT_MESSAGE_TYPE_TX, (naibrd_1553_rt_irqOptions_t)0, 1);
if(swResult < 0)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_RtDataBlockMapToSubaddress %d", swResult);
return bQuit;
}
currDataBlock = DATA_BLOCK_ID_TX1;
statusMask = 0;
/* Set status bits */
swResult = SetStatusBits(DevNum, modid, isAlternateMode, &statusMask);
if (swResult != 0)
{
bQuit = NAIBSP_TRUE;
return bQuit;
}
while (bContinue)
{
printf("\nType duration (in seconds) to run RT or %c to quit (default: 5) : ", NAI_QUIT_CHAR);
naibsp_fflush(0);
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 = NAIBSP_TRUE;
printf("Error: naibrd_1553_RtStart %d", swResult);
return bQuit;
}
/* Process New Messages */
ProcessMessages(DevNum, sa, duration);
/* Stop RT */
swResult = naibrd_1553_RtStop(DevNum);
if(swResult != 0)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_RtStop %d", swResult);
return bQuit;
}
}
else
bContinue = NAIBSP_FALSE;
}
/* Free 1553 Device */
swResult = naibrd_1553_Free(DevNum);
if(swResult != 0)
{
bQuit = NAIBSP_TRUE;
printf("Error: naibrd_1553_Free %d", swResult);
return bQuit;
}
return bQuit;
}
static int32_t UpdateTxDataBlock(uint16_t DevNum, uint8_t Subaddress)
{
/* Update Data in TX Data Block */
if (currDataBlock == DATA_BLOCK_ID_TX1)
{
UpdateTxBuffer(DevNum, DATA_BLOCK_ID_TX2);
/* Change data pointer to block 2 */
naibrd_1553_RtTxDataBlkSwap(DevNum, DATA_BLOCK_ID_TX2, Subaddress);
currDataBlock = DATA_BLOCK_ID_TX2;
}
else
{
UpdateTxBuffer(DevNum, DATA_BLOCK_ID_TX1);
/* Change data pointer to block 1 */
naibrd_1553_RtTxDataBlkSwap(DevNum, DATA_BLOCK_ID_TX1, Subaddress);
currDataBlock = DATA_BLOCK_ID_TX1;
}
return 0;
}
static int ProcessMessages(uint16_t DevNum, uint8_t Subaddress, int32_t duration)
{
time_t end, period;
uint32_t swResult;
int32_t i;
naibrd_1553_msgstruct_t DecodedMsgStruct;
uint16_t wsBuffer[72] = { 0x0000 };
int32_t count = 0;
end = time(NULL) + duration;
period = time(NULL) + 1;
while (time(NULL) < end)
{
/* Update Tx Data Block periodically */
if (time(NULL) > period)
{
UpdateTxDataBlock(DevNum, Subaddress);
period = time(NULL) + 1;
}
/* If the stack pointer has updated (new data arrived), read one message at a time */
swResult = naibrd_1553_RtMsgGetFromStackRaw(DevNum, wsBuffer, NAIBRD_1553_MAX_MESSAGE_SIZE_RT);
if (swResult < 0)
{
printf("Error: naibrd_1553_RtMessageGetFromStackRaw %d\r\n\r\n", swResult);
return 0;
}
else if (swResult > 0)
{
/* Decode Raw Message */
swResult = naibrd_1553_RtMsgDecodeRaw(DevNum, wsBuffer, &DecodedMsgStruct);
if (swResult < 0)
{
printf("Error: naibrd_1553_RtMessageDecodeRaw %d\r\n\r\n", swResult);
return 0;
}
if ((DecodedMsgStruct.commandWord1 & 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.blockStatus);
printf("Time Tag - 0x%04X\n", DecodedMsgStruct.timeTag);
printf("Command Word - 0x%04X\n", DecodedMsgStruct.commandWord1);
printf("Data Word Count - 0x%04X\n", DecodedMsgStruct.dataWordCount);
printf("Data:");
for (i = 0; i < DecodedMsgStruct.dataWordCount; i++)
{
if (i % 8 == 0)
{
printf("\n");
}
printf("0x%04X ", DecodedMsgStruct.data[i]);
}
printf("count: %d\n", count++);
/* print out RT status word */
printf("Status Bit mask = %X\n", statusMask);
printf("\n\n");
}
else
{
printf("Tx Msg Received\n");
printf("\n\nDecoded Message:\n\n");
printf("Block Status - 0x%04X\n", DecodedMsgStruct.blockStatus);
printf("Time Tag - 0x%04X\n", DecodedMsgStruct.timeTag);
printf("Command Word - 0x%04X\n", DecodedMsgStruct.commandWord1);
printf("Data Word Count - 0x%04X\n", DecodedMsgStruct.dataWordCount);
printf("count: %d\n", count++);
/* print out RT status word */
printf("Status Bit mask = %X\n", statusMask);
printf("\n\n");
}
}
naibrd_msDelay(10);
}
return 1;
}
static int32_t UpdateTxBuffer(uint16_t DevNum, uint16_t nDataBlkID)
{
static uint16_t wBuffer[32] = { 0x0000 };
uint16_t i = 0x0000;
uint32_t swResult;
/* Increment Tx buffer data */
for (i = 0; i < 32; i++)
{
wBuffer[i] += 1;
}
/* Write new data to Tx buffer */
swResult = naibrd_1553_RtDataBlkWrite(DevNum, nDataBlkID, wBuffer, RTBC_WORDCNT, 0);
if (swResult < 0)
{
printf("Error: naibrd_1553_RtDataBlockWrite %d\n\n", swResult);
return 0;
}
else
{
printf("\n");
printf("New data written to Tx Buffer\n\n");
/* print out RT status word */
printf("Status Bit mask = %X\n", statusMask);
}
return 1;
}
int32_t SetStatusBits(uint16_t DevNum, uint32_t modid, bool_t isAlternateMode, uint16_t *statusBitMask)
{
uint32_t i = 0;
int16_t swResult;
bool_t quit = NAI_FALSE;
char maskInputArr[100] = { 0 };
char inputBuffer[100] = { 0 };
int32_t inputResponseCnt = 0;
bool_t validSelection = NAIBSP_TRUE;
printf("\n\nChoose the desired OR'ed combinations to set the status bit mask:\n");
printf("Press:\n");
printf("1 - NO MASK = 0x0000\n");
/* module is not in 1553 alternate mode */
if (!isAlternateMode)
{
printf("2 - NAI_1553_RT_STATUS_BIT_DBCA = 0x0800\n");
printf("3 - NAI_1553_RT_STATUS_BIT_BUSY = 0x0400\n");
printf("4 - NAI_1553_RT_STATUS_BIT_SERVICE_REQUEST = 0x0200\n");
printf("5 - NAI_1553_RT_STATUS_BIT_SUBSYSTEM_FLAG = 0x0100\n");
printf("6 - NAI_1553_RT_STATUS_BIT_RT_FLAG = 0x0080\n\n");
}
/* module is not a 1760 module, and is in 1553 alternate status mode */
if (!IsFTx1760(modid) && (isAlternateMode))
{
printf("2 - NAI_1553_ALT_RT_STATUS_BIT_TERMINALFLAG = 0x0002,\n");
printf(" for 1553 alternate only\n");
printf("3 - NAI_1553_ALT_RT_STATUS_BIT_SUBSYSTEMFLAG = 0x0008,\n");
printf(" for 1553 alternate only\n");
printf("4 - NAI_1553_ALT_RT_STATUS_BIT_BUSY = 0x0010,\n");
printf(" for 1553 alternate only\n");
printf("5 - NAI_1553_ALT_RT_STATUS_BIT_SERVICE_REQUEST = 0x0200,\n");
printf(" for 1553 alternate only\n");
printf("6 - NAI_1553_ALT_RT_STATUS_BIT_INSTRUMENTATION = 0x0400,\n");
printf(" for 1553 alternate only\n");
printf("7 - NAI_1553_ALT_RT_STATUS_BIT_MESSAGE_ERROR = 0x0800,\n");
printf(" for 1553 alternate mode 1553 and 1760(is read only bit 1760)\n\n");
}
/* module is a 1760 module, and is not in 1553 alternate status mode */
if (IsFTx1760(modid) && (!isAlternateMode))
{
printf("7 - NAI_1760_RT_STATUS_BIT_INSTRUMENTATION = 0x0002,\n");
printf(" for 1760 only\n\n");
}
printf("If desired, use spaces as seperators, in between your choice(s) for multiple OR'ed combinations\n");
quit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
do
{
if (!quit)
{
sscanf(inputBuffer, " %[^\n]c", maskInputArr);
for (i = 0; i < strlen(maskInputArr); i++)
{
if ((maskInputArr[i] != '1') && (maskInputArr[i] != '2') && (maskInputArr[i] != '3') && (maskInputArr[i] != '4') &&
(maskInputArr[i] != '5') && (maskInputArr[i] != '6') && (maskInputArr[i] != '7') && (maskInputArr[i] != ' '))
{
validSelection = NAIBSP_FALSE;
if (!isAlternateMode)
{
if (IsFTx1760(modid))
{
printf("\nPlease enter a valid selection from combinations between 1 and 7.\n");
}
else
{
printf("\nPlease enter a valid selection from combinations between 1 and 6.\n");
}
}
else if(!IsFTx1760(modid) && (isAlternateMode))
{
printf("\nPlease enter a valid selection from combinations between 1 and 7.\n");
}
printf("Use spaces as sepreators, including spaces.\n");
quit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!quit)
{
sscanf(inputBuffer, " %[^\n]c", maskInputArr);
}
}
else
{
validSelection = NAIBSP_TRUE;
}
}
}
} while (!quit && !validSelection);
if (!quit && maskInputArr != NULL)
{
/* parse string and set statusBitMask */
for (i = 0; i < strlen(maskInputArr); i++)
{
if ((maskInputArr[i] == '1') || (maskInputArr[i] == '2') || (maskInputArr[i] == '3') || (maskInputArr[i] == '4') ||
(maskInputArr[i] == '5') || (maskInputArr[i] == '6') || (maskInputArr[i] == '7'))
{
if (maskInputArr[i] == '1')
{
*statusBitMask |= NO_MASK;
}
else if (!isAlternateMode)
{
if (maskInputArr[i] == '2')
{
*statusBitMask |= NAIBRD_1553_RT_STATUS_BIT_DBCA;
}
else if (maskInputArr[i] == '3')
{
*statusBitMask |= NAIBRD_1553_RT_STATUS_BIT_BUSY;
}
else if (maskInputArr[i] == '4')
{
*statusBitMask |= NAIBRD_1553_RT_STATUS_BIT_SERVICE_REQUEST;
}
else if (maskInputArr[i] == '5')
{
*statusBitMask |= NAIBRD_1553_RT_STATUS_BIT_SUBSYSTEM_FLAG;
}
else if (maskInputArr[i] == '6')
{
*statusBitMask |= NAIBRD_1553_RT_STATUS_BIT_RT_FLAG;
}
else if (maskInputArr[i] == '7')
{
if (IsFTx1760(modid))
{
*statusBitMask |= NAIBRD_1760_RT_STATUS_BIT_INSTRUMENTATION;
}
}
}
else if ((!IsFTx1760(modid) && (isAlternateMode)))
{
if (maskInputArr[i] == '2')
{
*statusBitMask |= NAIBRD_1553_ALT_RT_STATUS_BIT_TERMINALFLAG;
}
else if (maskInputArr[i] == '3')
{
*statusBitMask |= NAIBRD_1553_ALT_RT_STATUS_BIT_SUBSYSTEMFLAG;
}
else if (maskInputArr[i] == '4')
{
*statusBitMask |= NAIBRD_1553_ALT_RT_STATUS_BIT_BUSY;
}
else if (maskInputArr[i] == '5')
{
*statusBitMask |= NAIBRD_1553_ALT_RT_STATUS_BIT_SERVICE_REQUEST;
}
else if (maskInputArr[i] == '6')
{
*statusBitMask |= NAIBRD_1553_ALT_RT_STATUS_BIT_INSTRUMENTATION;
}
else if (maskInputArr[i] == '7')
{
*statusBitMask |= NAIBRD_1553_ALT_RT_STATUS_BIT_MESSAGE_ERROR;
}
}
}
}
}
/* Clear masks */
swResult = naibrd_1553_RtRespStatusBitUnset(DevNum, 0xFFFF);
if (swResult != 0)
{
printf("Error: naibrd_1553_RtResponseStatusBitsUnset %d\n\n", swResult);
return swResult;
}
/* set status bits */
swResult = naibrd_1553_RtRespStatusBitSet(DevNum, *statusBitMask);
if (swResult != 0)
{
printf("Error: naibrd_1553_RtResponseStatusBitsSet %d\n\n", swResult);
return swResult;
}
/* Get status mask */
swResult = naibrd_1553_RtRespStatusBitGet(DevNum, statusBitMask);
if (swResult != 0)
{
printf("Error: naibrd_1553_RtResponseStatusBitsGet %d\n\n", swResult);
return swResult;
}
return swResult;
}