Integrator Resources

The official home for NAI Support

Not sure where to start? Try Quick Start Guide or ask a question below!

Toggle Components with Visual Button
JavaScript Form Processing

M1553 BC SendMessage

M1553 BC SendMessage

Explanation

About the Sample Application Code

This sample application code provides an example of how to interact with North Atlantic Industries (NAI)'s embedded function modules, specifically using their SSK to send messages via a 1553 Bus Controller (BC). The code is divided into multiple segments involving configuration, initialization, and operation of the communication channel. Here is a detailed walkthrough:

Includes and Preprocessor Directives - Standard Tool Includes: The code includes standard headers for input/output (stdio.h), general utilities (stdlib.h), string manipulation (string.h), and time handling (time.h). - Conditional OS-specific Inclusions: For VxWorks, the taskLib.h header is included. - NAI Specific Includes: Several headers related to NAI’s application and 1553 operations are included for board access, display, and utility functions.

Constants and Definitions - CONFIG_FILE: This constant defines the name of the configuration file used for sending messages ("default_1553BC_SendMessage.txt"). - Message Constants, Opcodes, Frame Constants, Data Block Numbers: Several constants (MSG1, OP1, OP2, MNR1, MJR, DBLK1, DBLK2) are defined to be used later in message and operation sequences. - Default Settings: Default settings for card index, module, channel, and device number are provided. - Routing Table (RT) Settings: Constants for routing table addresses and word counts are defined.

Function Prototypes - Run_M1553_BC_SendMessage: Initializes the 1553 device as a Bus Controller (BC) and sends a message. - RunSendMessage: Executes the process of sending a message on a specified channel.

Main Function The main function (conditionally named M1553_BC_SendMessage for VxWorks) serves as the entry point: 1. RunBoardMenu: Calls a function to run the board menu based on the configuration file. 2. User Queries: Interacts with users to get card index and module number, verifies the module ID, and can restart or quit the application based on user input. 3. Close All Open Cards: Ensures that all open cards are properly closed before exiting the program.

Detailed Operation Functions Run_M1553_BC_SendMessage This function is responsible for: - Channel Verification: Ensures the card supports 1553 functionality and queries the user for the channel number. - Message Execution: Calls RunSendMessage to handle the actual message sending on the specified channel.

RunSendMessage This function handles the core logic for sending a message on the 1553 BC: 1. Device Initialization: Gets the logical device number, message type, bus information, and software override settings, then opens the 1553 device. 2. Mode Commands and Overrides: Handles specific settings for mode codes and BC_DISABLE and M1760 pin configurations. 3. Device Reset and Initialization: Resets and initializes the 1553 device to operate as a BC. 4. Data Block and Message Creation: - Creates data blocks and appropriate messages (BC to RT, RT to BC, mode commands, RT to RT). - Creates execution and subroutine commands and frames (minor and major). 5. Message Sending Loop: - Continuously loads data blocks, starts the BC, sends messages, stops the BC, and displays the decoded message data until the user decides to quit. 6. Device Cleanup: Frees the 1553 device resources before exiting.

Key Functions from the NAI Library - naibrd_1553_Open - naibrd_1553_Initialize - 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_BcStart - naibrd_1553_BcStop - naibrd_1553_Free

These functions are critical to managing and operating the 1553 communication channel effectively.

Conclusion The code demonstrates how to interact with NAI’s 1553 hardware, predominately focused on setting up the channel as a Bus Controller, sending various types of messages, and handling user input and module verification. Users can refer to the function and constant definitions for clarity about their specific roles in the communication process.

#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"

static const int8_t *CONFIG_FILE = (int8_t *)"default_1553BC_SendMessage.txt";

/* Function prototypes */
static bool_t Run_M1553_BC_SendMessage(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 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 out.

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(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(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 initializes the 1553 device as a bus controller (BC) 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_BcDataBlockCreate
naibrd_1553_BcMessageCreateBcToRt
naibrd_1553_BcMessageCreateRtToBc
naibrd_1553_BcMessageCreateMode
naibrd_1553_BcMessageCreateRtToRt
naibrd_1553_BcCommandCreate
naibrd_1553_BcFrameCreate
naibrd_1553_BcDataBlockWrite
naibrd_1553_BcStart
naibrd_1553_BcStop
naibrd_1553_Free
</summary>
*/
/**************************************************************************************************************/
static bool_t Run_M1553_BC_SendMessage(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;
   naiDecodedMessageStructure DecodedMsgStruct;
   bool_t bSoftware;
   M1553ModeCodes_t modeCommand = (M1553ModeCodes_t)0;
   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 (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;
               }

               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;
                  }

                  /* 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;
                  }

                  /* Get Decoded Msg Structure */
                  status = naibrd_1553_BcMessageGetByIdDecoded(devnum, MSG1, &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);

                     if (DecodedMsgStruct.bIsCommandWord2Relevant)
                        printf("Command Word 2: 0x%04X\n", DecodedMsgStruct.wCommandWord2);

                     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);

                     if (DecodedMsgStruct.bIsStatus2Relevant)
                        printf("RT Status Word 2: 0x%04X\n", DecodedMsgStruct.wStatus2);

                     if ((msgType == M1553_MSGTYPE_RTTOBC) || (msgType == M1553_MSGTYPE_MODECODE_TX))
                     {

                        printf((DecodedMsgStruct.wDataWordCount > 0) ? ("Data:") : (""));
                        for (i = 0; i < DecodedMsgStruct.wDataWordCount; i++)
                        {
                           if (i % 8 == 0)
                           {
                              printf("\n");
                           }
                           printf("0x%04X ", DecodedMsgStruct.waData[i]);
                        }
                     }
                     printf("\n\n");
                  }

                  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;
}

Help Bot

X