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 Async

M1553 BC SendMessage Async

Explanation

About the Sample Application Code

This sample code is provided by North Atlantic Industries (NAI) for interacting with their embedded function modules using the System Solution Kit (SSK). The application’s purpose is to illustrate the configuration and operation needed to utilize the 1553 Bus Controller (BC) to send asynchronous messages. This document briefly explains the various components and flow of the application.

Definitions and Key Components

Header Files - #include <library>: Standard C libraries for input/output, memory, and time functions. - #include "include/naiapp_boardaccess_menu.h": Board access menu utilities. - #include "include/naiapp_boardaccess_query.h": Board access query utilities. - #include "include/naiapp_boardaccess_access.h": Board access functions. - #include "include/naiapp_boardaccess_display.h": Display utilities for board access. - #include "nai_1553_utils.h": General 1553 utilities. - #include "BC/nai_1553_bc_utils.h": Specific utilities for 1553 Bus Controller (BC) operations. - #include "nai.h": Main NAI library. - #include "naibrd.h": NAI board-level functions. - #include "functions/naibrd_1553.h": Specific 1553 functionality utilities.

Constants Predefined constants are used to manage message opcodes, frame constants, data blocks, device configuration, and timing: - #define MSGx, #define OPx, #define MNRx, #define DBLKx, #define DEF_M1553_xx - Timing constants like MNR_FRAME_TIME for minor frame time are defined in milliseconds.

Function Prototypes Prototypes for the main functional routines: - static bool_t Run_M1553_BC_SendMessage_Async(int32_t cardIndex, int32_t module, uint32_t modid) - static bool_t RunSendMessage(int32_t cardIndex, int32_t module, int32_t channel)

Code Explanation

Main Function

The main entry point is platform-dependent (VXWORKS or default). The purpose of this function is to set up the necessary conditions and configurations for running the 1553 Bus Controller (BC) to send asynchronous messages.

  1. Initialization and configuration loading: If the user’s response indicates running the board menu using naiapp_RunBoardMenu(CONFIG_FILE), the program continues.

  2. User Queries:

    • Query for the card index using naiapp_query_CardIndex.

    • Query for the module number with naiapp_query_ModuleNumber.

    • Fetch the module ID using naibrd_GetModuleID.

  3. Execution:

    • If valid, call Run_M1553_BC_SendMessage_Async with the card index, module, and module ID.

  4. Loop Termination:

    • Prompt user for quit/restart inputs with naiapp_query_ForQuitResponse.

  5. Cleanup:

    • Close all open cards using naiapp_access_CloseAllOpenCards.

Run_M1553_BC_SendMessage_Async

This function configures and initiates asynchronous 1553 messaging.

  1. Channel Configuration:

    • If the module supports FTx1553 (IsFTx1553(modid)), determine the channel number.

  2. Message Sending:

    • Calls the RunSendMessage function to handle the actual message transmission.

RunSendMessage

This function contains the primary operations for setting up and sending 1553 messages.

  1. Device Setup and Initialization:

    • Logical device number queried with Get1553LogicalDevNum.

    • Queries for bus selection and message configuration.

  2. Open and Configure 1553 Device:

    • Uses naibrd_1553_Open to open the device.

    • Initializes the device, sets BC configurations, and creates necessary data blocks and messages (naibrd_1553_* functions).

  3. Message Handling:

    • Sends high or low priority asynchronous messages as dictated by user input.

    • Checks for errors during message transmissions using the naibrd_1553_BcMessageGetByIdDecoded function.

  4. Async Message Cleanup:

    • Deletes asynchronous messages and data blocks after transmission.

  5. BC Stop:

    • Stops the BC with naibrd_1553_BcStop.

  6. Program Continuation Check:

    • Waits for user input to continue or quit.

Summary

This sample code sets up and operates an NAI 1553 Bus Controller to send both synchronous and asynchronous messages. It uses various library functions from NAI’s SSK to configure devices, create messages, and manage data blocks. This illustration is helpful for understanding the flow of operations needed to use NAI hardware effectively in practical embedded system applications.

#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_Async.txt";

/* Function prototypes */
static bool_t Run_M1553_BC_SendMessage_Async(int32_t cardIndex, int32_t module, uint32_t modid);
static bool_t RunSendMessage(int32_t cardIndex, int32_t module, int32_t channel);

/* define message constants */
#define MSG1         1
#define MSG2         2
#define MSG3         3
#define MSG4         4
#define MSG5         5
#define MSG6         6

/* define opcodes */
#define OP1          1
#define OP2          2
#define OP3          3
#define OP4          4

/* define frame constants */
#define MNR1         1
#define MNR2         3
#define MJR          2

/* define data block numbers */
#define DBLK1        1
#define DBLK2        2
#define DBLK3        3
#define DBLK4        4
#define DBLK5        5
#define DBLK6        6

#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 MNR_FRAME_TIME                       500         /* in ms */

/**************************************************************************************************************/
/**
<summary>
The purpose of the M1553_BC_SendMessage_Async is to illustrate the methods to call in the naibrd library to configure
the 1553 channel as a Bus Controller and send asynchronous messages during normal BC message transmissions.

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_Async(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_Async(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_Async initializes the 1553 device as a bus controller (BC) and sets up a BC-to-RT message,
and while the message is being sent continuously every 500 ms, an asynchronous message (either Low or High priority)
is sent out. This routine demonstrates the following API functions in the 1553 naibrd library:
naibrd_1553_Open
naibrd_1553_WriteAuxReg
naibrd_1553_Initialize
naibrd_1553_BcConfig
naibrd_1553_BcDataBlockCreate
naibrd_1553_BcMessageCreateBcToRt
naibrd_1553_BcCommandCreate
naibrd_1553_BcFrameCreate
naibrd_1553_BcDataBlockWrite
naibrd_1553_BcAsynchronousMessageCreateBcToRt
naibrd_1553_BcStart
naibrd_1553_BcAsynchronousMessageSendAtHighPriority_ExecuteOnce
naibrd_1553_BcAsynchronousMessageSendAtLowPriority_ExecuteOnce
naibrd_1553_BcMessageGetByIdDecoded
naibrd_1553_GetBlockStatusWordErrorString
naibrd_1553_BcMessageDelete
naibrd_1553_BcDataBlockDelete
naibrd_1553_BcStop
naibrd_1553_Free
</summary>
*/
/**************************************************************************************************************/
static bool_t Run_M1553_BC_SendMessage_Async(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);
      }
   }
   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)
{
   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;
   uint16_t dummycount;
   bool_t bSoftware, bHighPriority;
   naiDecodedMessageStructure DecodedMsgStruct;
   int8_t inputBuffer[80];
   int32_t inputResponseCnt;

   /* Get the Logical Device Number */
   bQuit = Get1553LogicalDevNum(DEF_M1553_DEVNUM, &devnum);
   if (!bQuit)
   {
      /* Which bus are we firing on? */
      bQuit = GetBus(&usBus);
      if (!bQuit)
      {
         bQuit = Get1553BCAsyncMsgType(TRUE, &bHighPriority);
         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);
               }

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

               /* Enable Asynchronous Messages */
               status = naibrd_1553_BcConfig(devnum, NAI_1553_BC_ASYNCHRONOUS_BOTH_PRIORITY_MODES | NAI_1553_BC_ASYNCHRONOUS_HIGH_PRIORITY_ONCE_ANYWAY);    /* Both are needed to use the *_ExecuteOnce functions */
               if(status != 0)
               {
                  printf("Error: naibrd_1553_BcConfig Ch %d, status = %d", channel, status);
                  return TRUE;
               }

               /* Create BC Data Block */
               status = naibrd_1553_BcDataBlockCreate(devnum, DBLK1, 32, NULL, 0);
               if(status != 0)
               {
                  printf("Error: naibrd_1553_BcDataBlockCreate Ch %d, status = %d", channel, status);
                  return TRUE;
               }

               /* Create BC to RT Message */
               status = naibrd_1553_BcMessageCreateBcToRt(devnum, MSG1, DBLK1, RT_ADDRESS, RT_SUBADDRESS, 32, 0, usBus);
               if (status != 0)
               {
                  printf("Error: naibrd_1553_BcMessageCreateBcToRt 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,MNR_FRAME_TIME*10,0);     /* wFrameTime: 1 = 0.1 ms */
               if (status != 0)
               {
                  printf("Error: naibrd_1553_BcFrameCreate status = %d", status);
                  return TRUE;
               }

               while (bContinue)
               {
                  /* Load Scheduled BC Message data block with incremental data */
                  for (i = 0; i < 32; i++)
                  {
                     aData[i] = increment++;
                  }
                  status = naibrd_1553_BcDataBlockWrite(devnum, DBLK1, aData, 32, 0);
                  if (status != 0)
                  {
                     printf("Error: naibrd_1553_BcDataBlockWrite status = %d", status);
                     return TRUE;
                  }

                  /**********************************************************************/
                  /*** ADD 2 MESSAGES TO THE ASYNC QUEUE (For Low priority messaging) ***/
                  /**********************************************************************/
                  /* Create BC to RT Async Message 1 */
                  for (i = 0; i < 32; i++)
                  {
                     aData[i] = 0xBCBC;
                  }
                  status = naibrd_1553_BcAsynchronousMessageCreateBcToRt(devnum, MSG3, DBLK3, RT_ADDRESS, RT_SUBADDRESS, 32, 0, usBus, aData);

                  /* Create BC to RT Async Message 2 */
                  for (i = 0; i < 32; i++)
                  {
                     aData[i] = 0xAAAA;
                  }
                  status = naibrd_1553_BcAsynchronousMessageCreateBcToRt(devnum, MSG4, DBLK4, RT_ADDRESS, RT_SUBADDRESS, 32, 0, usBus, aData);

                  /*********************************************************/
                  /* START BC TO TRANSMIT SYNCHRONOUS (SCHEDULED) MESSAGES */
                  /*********************************************************/
                  status = naibrd_1553_BcStart(devnum,MJR,NAI_1553_BC_FRAME_RUN_FOREVER);
                  if (status != 0)
                  {
                     printf("Error: naibrd_1553_BcStart status = %d", status);
                     return TRUE;
                  }

                  nai_msDelay(50);

                  /*******************************/
                  /* SEND MSG WITH HIGH PRIORITY */
                  /*******************************/
                  if (bHighPriority)
                  {
                     /* Send High Priority Async Message */
                     status = naibrd_1553_BcAsynchronousMessageSendAtHighPriority_ExecuteOnce(devnum, MSG3, 0);
                     if (status == NAI_1553_RC_ASYNCHRONOUS_MESSAGE_ERROR)
                     {
                        /* OPTIONAL check to determine the message error type */
                        printf("\nASYNC msg error:");
                        {
                           /* Get Decoded Msg Structure for Message 3 */
                           status = naibrd_1553_BcMessageGetByIdDecoded(devnum, MSG3, &DecodedMsgStruct, 1);
                           if (status < 0)
                           {
                              printf("\nError: naibrd_1553_BcMessageGetByIdDecoded status = %d\n\n", status);
                           }
                           else if (status > 0)
                           {
                              printf("\n%s", naibrd_1553_GetBlockStatusWordErrorString(NAI_1553_MODE_BC, DecodedMsgStruct.wBlockStatus));
                              printf("\n\n");
                           }
                        }
                     }
                     else if (status == 0)
                     {
                        printf("\nASYNC msg sent successfully\n\n");
                     }
                     else
                     {
                        printf("Error: naibrd_1553_BcAsynchronousMessageSendAtHighPriority status = %d\n\n", status);
                     }
                  }
                  /**********************************/
                  /* OR SEND MSGS WITH LOW PRIORITY */
                  /**********************************/
                  else
                  {
                     do
                     {
                        /* Each call to naibrd_1553_BcAsynchronousMessageSendAtLowPriority_ExecuteOnce() will re-update */
                        /* the BC async message queue so that already transmitted messages are not re-sent. */
                        /* A positive value returned in status is the count of async messages in the queue that are not yet sent. */
                        status = naibrd_1553_BcAsynchronousMessageSendAtLowPriority_ExecuteOnce(devnum, &dummycount, 0);
                        if (status > 0)
                        {
                           printf("\nremaining async count: %d", status);
                        }
                        if (status == NAI_1553_RC_ASYNCHRONOUS_LIST_IS_EMPTY)
                        {
                           /* Exit loop */
                           break;
                        }
                        /* Delay between calls to naibrd_1553_BcAsynchronousMessageSendAtLowPriority_ExecuteOnce should be */
                        /* set to the minor frame time, or the smallest minor frame time if multiple minor frames exist */
                        nai_msDelay(MNR_FRAME_TIME);
                     }
                     while (status > 0);

                     printf("\nasync transmission complete");

                     /* OPTIONAL check errors in async messages, if any */
                     /* Get Decoded Msg Structure for Message 3 */
                     status = naibrd_1553_BcMessageGetByIdDecoded(devnum, MSG3, &DecodedMsgStruct, 1);
                     if (status < 0)
                     {
                        printf("\nError: naibrd_1553_BcMessageGetByIdDecoded status = %d", status);
                     }
                     else if (status > 0)
                     {
                        printf("\nMSG3: %s", naibrd_1553_GetBlockStatusWordErrorString(NAI_1553_MODE_BC, DecodedMsgStruct.wBlockStatus));
                     }
                     /* Get Decoded Msg Structure for Message 4 */
                     status = naibrd_1553_BcMessageGetByIdDecoded(devnum, MSG4, &DecodedMsgStruct, 1);
                     if (status < 0)
                     {
                        printf("\nError: naibrd_1553_BcMessageGetByIdDecoded status = %d", status);
                     }
                     else if (status > 0)
                     {
                        printf("\nMSG4: %s", naibrd_1553_GetBlockStatusWordErrorString(NAI_1553_MODE_BC, DecodedMsgStruct.wBlockStatus));
                        printf("\n\n");
                     }

                     /* Delete async message */
                     status = naibrd_1553_BcMessageDelete(devnum, MSG3);
                     if (status != 0)
                     {
                        printf("Error: naibrd_1553_BcMessageDelete status = %d", status);
                        return TRUE;
                     }
                     /* Delete async message data block */
                     status = naibrd_1553_BcDataBlockDelete(devnum, DBLK3);
                     if (status != 0)
                     {
                        printf("Error: naibrd_1553_BcDataBlockDelete status = %d", status);
                        return TRUE;
                     }

                     /* Delete async message */
                     status = naibrd_1553_BcMessageDelete(devnum, MSG4);
                     if (status != 0)
                     {
                        printf("Error: naibrd_1553_BcMessageDelete status = %d", status);
                        return TRUE;
                     }
                     /* Delete async message data block */
                     status = naibrd_1553_BcDataBlockDelete(devnum, DBLK4);
                     if (status != 0)
                     {
                        printf("Error: naibrd_1553_BcDataBlockDelete status = %d", status);
                        return TRUE;
                     }
                  }

                  /* Allow 1 more second before stopping BC */
                  nai_msDelay(1000);

                  /* Stop BC */
                  status = naibrd_1553_BcStop(devnum);
                  if (status != 0)
                  {
                     printf("Error: naibrd_1553_BcStop status = %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;
}

Help Bot

X