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

nai 1553 rt utils

nai 1553 rt utils

Explanation

About the Sample Application Code

This C code sample is designed to interact with North Atlantic Industries' (NAI) embedded function modules, specifically for managing and configuring 1553 Remote Terminals (RT). Below is a detailed walk-through and explanation of the provided functions and key components of the sample application:

Included Headers

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "include/naiapp_boardaccess_menu.h"
#include "include/naiapp_boardaccess_query.h"
#include "include/naiapp_boardaccess_access.h"
#include "include/naiapp_boardaccess_display.h"
#include "include/naiapp_boardaccess_utils.h"
#include "nai_1553_utils.h"
#include "nai_1553_rt_utils.h"
#include "nai.h"
#include "naibrd.h"
#include "functions/naibrd_1553.h"

These headers include standard C libraries and NAI-specific headers required for board access, display utilities, and interaction with the 1553 RT components.

Function Definitions bool_t RT_SetAddress(int16_t devnum) Sets the RT address for the specified device. - Parameters: int16_t devnum - Device number. - Functionality: Displays the current RT address, prompts the user to enter a new RT address within the range 0-31, and sets the RT address for the device.

bool_t RT_SubaddressLegal(int16_t devnum) Legalizes or illegalizes subaddresses for the specified device. - Parameters: int16_t devnum - Device number. - Functionality: Interacts with the user to legalize or illegalize specific subaddresses, considering broadcast, direction (TX/RX), and legalization value.

bool_t RT_UpdateDataBlocks(int16_t devnum) Updates data blocks for the specified device. - Parameters: int16_t devnum - Device number. - Functionality: Prompts the user to select and update specific data blocks. The user enters the range and new values to be written to the data blocks.

bool_t RT_SetStatusBits(int16_t devnum) Sets or unsets status bits for the specified device. - Parameters: int16_t devnum - Device number. - Functionality: Reads and displays current status register bits and allows the user to set or unset specific status bits such as Service Request, Busy, Subsystem Flag, and Terminal Flag.

bool_t SetOrUnset(int16_t devnum, uint16_t readvalue, int32_t statusbit) Sets or unsets a specific status bit. - Parameters: int16_t devnum, uint16_t readvalue, int32_t statusbit - Functionality: Prompts the user to set or unset a specific bit in the provided register value and updates the device register accordingly.

bool_t RT_RunStop(int16_t devnum) Starts or stops the RT operation for the specified device. - Parameters: int16_t devnum - Device number. - Functionality: Displays the current RT state (running or stopped) and allows the user to change the state.

bool_t File_IOTest(int16_t devnum) Performs a simple file I/O test. - Parameters: int16_t devnum - Device number. - Functionality: Creates, writes to, reads from, and deletes a test file to verify file I/O operations.

void DisplaySubaddressTable(int16_t devnum) Displays the current subaddress legalization table for the specified device. - Parameters: int16_t devnum - Device number. - Functionality: Displays the legalization status of all subaddresses for both broadcast and non-broadcast operations.

Utility Functions and Definitions - NAI_QUIT_CHAR: Defines a character used to quit input operations. - check_status: Macro or function to check status codes, assumed to handle error or success responses. - naiapp_query_ForQuitResponse: Function to handle input queries from users, checking for a quit command. - naiapp_utils_HexStrToDecUInt32: Utility function to convert hexadecimal strings to unsigned 32-bit integers. - naiapp_utils_HexStrToDecUInt16: Utility function to convert hexadecimal strings to unsigned 16-bit integers. - Naibrd (naibrd_1553_…​): Functions associated with the 1553 board operations to set addresses, read/write registers, and manage data blocks.

This code provides a comprehensive and interactive approach to configuring and managing an NAI 1553 Remote Terminal, allowing users to adjust settings, update data, and verify system functionality.

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

/* Common Sample Program include files */
#include "include/naiapp_boardaccess_menu.h"
#include "include/naiapp_boardaccess_query.h"
#include "include/naiapp_boardaccess_access.h"
#include "include/naiapp_boardaccess_display.h"
#include "include/naiapp_boardaccess_utils.h"
#include "nai_1553_utils.h"
#include "nai_1553_rt_utils.h"

/* naibrd include files */
#include "nai.h"
#include "naibrd.h"
#include "functions/naibrd_1553.h"

bool_t RT_SetAddress(int16_t devnum)
{
   bool_t bQuit = FALSE;
   bool_t bContinue = TRUE;
   uint16_t rtaddr;
   int8_t inputBuffer[80];
   int32_t inputResponseCnt;

   /* Display Current RT Address */
   check_status(naibrd_1553_RtGetAddress(devnum, &rtaddr));

   printf("\n\nCurrent RT Address: %d\n\n", rtaddr);

   /* Set New RT Address */
   while (bContinue)
   {
      printf("\nEnter a new RT address (0-31) or %c to quit: ", NAI_QUIT_CHAR);
      bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
      if (!bQuit)
      {
         if (inputResponseCnt > 0)
         {
            rtaddr = (uint16_t)atol((const char*)inputBuffer);

            if (rtaddr < 32 && rtaddr >= 0)
            {
               /* Set RT address */
               check_status(naibrd_1553_RtSetAddress(devnum, rtaddr));
               bContinue = FALSE;
               printf("\nRT Address set to %d. \n", rtaddr);
            }
            else
            {
               printf("\nInvalid value. \n");
            }
         }
      }
      else
         bContinue = FALSE;
   }

   return FALSE;
}
bool_t RT_SubaddressLegal(int16_t devnum)
{
   bool_t bQuit = FALSE;
   bool_t bContinue = TRUE;
   bool_t bValChanged = FALSE;
   uint16_t sa, broadcast, direction;
   uint32_t legalization;
   int32_t rtchan;
   int8_t inputBuffer[80];
   int32_t inputResponseCnt;

   while (bContinue)
   {
      /* Display Table of Subaddresses Both Broadcast and Non-Broadcast */
      DisplaySubaddressTable(devnum);

      printf("\nEnter a Value for the Subaddress to Legalize/Illegalize (or Q to quit): ", NAI_QUIT_CHAR);
      bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
      if (!bQuit)
      {
         if (inputResponseCnt > 0)
         {
            sa = (uint16_t)atol((const char*)inputBuffer);

            if (sa >= 0 && sa < 32)
            {
               while (bContinue)
               {
                  printf("\nBroadcast (B) or Non-Broadcast (N)? ", NAI_QUIT_CHAR);
                  bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
                  if (!bQuit)
                  {
                     if (inputResponseCnt > 0)
                     {
                        if (toupper(inputBuffer[0]) == 'B')
                           broadcast = NAI_1553_RT_ADDRESS_BROADCAST;
                        else if (toupper(inputBuffer[0]) == 'N')
                           broadcast = NAI_1553_RT_ADDRESS_OWN;
                        else
                           broadcast = 999;

                        if (broadcast != 999)
                        {
                           while (bContinue)
                           {
                              printf("\nTX (T) or RX (R)? ", NAI_QUIT_CHAR);
                              bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
                              if (!bQuit)
                              {
                                 if (inputResponseCnt > 0)
                                 {
                                    if (toupper(inputBuffer[0]) == 'T')
                                       direction = NAI_1553_MT_FILTER_TX;
                                    else if (toupper(inputBuffer[0]) == 'R')
                                       direction = NAI_1553_MT_FILTER_RX;
                                    else
                                       direction = 999;

                                    if (direction != 999)
                                    {
                                       while (bContinue)
                                       {
                                          printf("\nEnter Legalization value (0x00000000-0xFFFFFFFF): ", NAI_QUIT_CHAR);
                                          bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
                                          if (!bQuit)
                                          {
                                             if (inputResponseCnt > 0)
                                             {
                                                legalization = naiapp_utils_HexStrToDecUInt32((int8_t*)inputBuffer);
                                                /* First Disable All Word Counts */
                                                check_status(naibrd_1553_RtMessageLegalityDisable(devnum, broadcast, direction, sa, 0xFFFFFFFF));
                                                /* Then Legalize only what the user wants */
                                                check_status(naibrd_1553_RtMessageLegalityEnable(devnum, broadcast, direction, sa, legalization));
                                                bContinue = FALSE;
                                                bValChanged = TRUE;
                                             }
                                             else
                                                printf("\nInvalid Value.\n");
                                          }
                                          else
                                             bContinue = FALSE;
                                       }
                                    }
                                    else
                                       printf("\nInvalid Value.\n");
                                 }
                              }
                              else
                                 bContinue = FALSE;
                           }
                        }
                        else
                           printf("\nInvalid Value.\n");
                     }
                  }
                  else
                     bContinue = FALSE;
               }
            }
            else
               printf("\nInvalid Value.\n");
         }
      }
      else
         bContinue = FALSE;
   }

   if (bValChanged)
   {
      bContinue = TRUE;

      while (bContinue)
      {
         printf("\nSave Illegalization Changes (Y or N)? ");
         bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
         if (!bQuit)
         {
            if (toupper(inputBuffer[0]) == 'Y')
            {
               while (bContinue)
               {
                  printf("\nWhich Channel (1 to 4)? ");
                  bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
                  {
                     if (!bQuit)
                     {
                        rtchan = atoi((const char*)inputBuffer);

                        if (rtchan > 0 && rtchan < 5)
                        {
                           SaveIllegalization(devnum, rtchan);
                           bContinue = FALSE;
                        }
                        else
                           printf("\nInvalid value entered.\n");
                     }
                  }
               }
            }
            else if (toupper(inputBuffer[0]) == 'N')
            {
               bContinue = FALSE;
            }
            else
            {
               printf("Invalid Value Entered.\n");
            }
         }
      }
   }

   return FALSE;
}

bool_t RT_UpdateDataBlocks(int16_t devnum)
{
   bool_t bQuit = FALSE;
   bool_t bContinue = TRUE;
   uint16_t datablockvalue;
   int32_t i;
   uint16_t wapBuffer[64] = {0x0000};
   uint16_t minvalue, maxvalue, newvalue;
   int8_t inputBuffer[80];
   int32_t inputResponseCnt;

   while (bContinue)
   {
      /* Display Menu Options */
      printf("\n\n************************* DATA BLOCKS *************************\n");
      for (i = 0; i < 32; i++)
      {
         printf("RX%d\t-\t%d\tTX%d\t-\t%d\tBCST%d\t-\t%d\n", i, i, i, i+32, i, i+64);
      }
      printf("\n\nSelect a data block to update (0-95) (or enter Q to quit): ");
      bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
      if (!bQuit)
      {
         datablockvalue = (uint16_t)atol((const char*)inputBuffer);

         if (datablockvalue >= 0 || datablockvalue <= 95)
         {
            while (!bQuit)
            {
               /* Display the current data in the data block */
               printf("\n\nDataBlock %d:", datablockvalue);
               naibrd_1553_RtDataBlockRead(devnum, datablockvalue, wapBuffer, 64, 0);
               for (i = 0; i < 64; i++)
               {
                  if ((i % 8) == 0)
                  {
                     printf("\n");
                     printf("0x%02X-0x%02X ", i, i+7);
                  }
                  printf("0x%04X ", wapBuffer[i]);
               }

               /* Select the range of values to update and update the data block */
               printf("\n\nSelect min value of range (0x00 to 0x3F) to update (or Q to quit): ");
               bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
               if (!bQuit)
               {
                  minvalue = naiapp_utils_HexStrToDecUInt16(inputBuffer);

                  if (minvalue < 0x40)
                  {
                     printf("\n\nSelect max value of range (0x%02X to 0x3F) to update (or Q to quit): ", minvalue);
                     bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
                     if (!bQuit)
                     {
                        maxvalue = naiapp_utils_HexStrToDecUInt16(inputBuffer);

                        if ((maxvalue < 0x40) && (maxvalue >= minvalue))
                        {
                           printf("\n\nEnter new value in Hex (or Q to quit): ");
                           bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
                           {
                              if (!bQuit)
                              {
                                 newvalue = naiapp_utils_HexStrToDecUInt16(inputBuffer);

                                 for (i = 0; i < 64; i++)
                                 {
                                    wapBuffer[i] = newvalue;
                                 }

                                 naibrd_1553_RtDataBlockWrite(devnum, datablockvalue, wapBuffer, maxvalue-minvalue+1, minvalue);
                              }
                           }
                        }
                        else
                        {
                           printf("\nInvalid Value.");
                        }
                     }
                  }
                  else
                  {
                     printf("\nInvalid Value.");
                  }
               }
            }
         }
         else
         {
            printf("\nInvalid value.\n\n");
         }
      }
      else
      {
         bContinue = FALSE;
      }
   }

   return FALSE;
}

bool_t RT_SetStatusBits(int16_t devnum)
{
   bool_t bQuit = FALSE, bContinue = TRUE;
   uint16_t registervalue;
   int8_t inputBuffer[80];
   int32_t inputResponseCnt;

   /* Read Config Register #1 */
   registervalue = naibrd_1553_ReadReg(devnum, 1);

   if (registervalue & NAI_1553_CONFIG_REG1_MASK_RT_SERVICE_REQUEST)
   {
      printf("\n[R]Service Request: 1\n");
   }
   else
   {
      printf("\n[R]Service Request: 0\n");
   }

   if (registervalue & NAI_1553_CONFIG_REG1_MASK_RT_BUSY)
   {
      printf("[B]Busy           : 1\n");
   }
   else
   {
      printf("[B]Busy           : 0\n");
   }

   if (registervalue & NAI_1553_CONFIG_REG1_MASK_RT_SUBSYSTEM_FLAG)
   {
      printf("[S]Subsystem Flag : 1\n");
   }
   else
   {
      printf("[S]Subsystem Flag : 0\n");
   }

   if (registervalue & NAI_1553_CONFIG_REG1_MASK_RT_FLAG)
   {
      printf("[T]Terminal Flag  : 1\n");
   }
   else
   {
      printf("[T]Terminal Flag  : 0\n");
   }

   while (bContinue)
   {
      printf("\nSelect Bit to Set or Unset (R, B, S, T) or Q to Quit: ", NAI_QUIT_CHAR);
      bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
      if (!bQuit)
      {
         if (toupper(inputBuffer[0]) == 'R')
         {
            bContinue = SetOrUnset(devnum, registervalue, 9);
         }
         else if (toupper(inputBuffer[0]) == 'B')
         {
            bContinue = SetOrUnset(devnum, registervalue, 10);
         }
         else if (toupper(inputBuffer[0]) == 'S')
         {
            bContinue = SetOrUnset(devnum, registervalue, 8);
         }
         else if (toupper(inputBuffer[0]) == 'T')
         {
            bContinue = SetOrUnset(devnum, registervalue, 7);
         }
         else
            printf("\nInvalid Response.\n\n");
      }
      else
         bContinue = FALSE;
   }

   return bQuit;
}

bool_t SetOrUnset(int16_t devnum, uint16_t readvalue, int32_t statusbit)
{
   bool_t bQuit = FALSE, bContinue = TRUE;
   uint16_t writevalue;
   int8_t inputBuffer[80];
   int32_t inputResponseCnt;

   printf("\n0 to set or 1 to unset or Q to Quit: ", NAI_QUIT_CHAR);
   bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
   if (!bQuit)
   {
      if (atoi((char *)inputBuffer) == 1)
      {
         writevalue = readvalue | (1 << statusbit);
         naibrd_1553_WriteReg(devnum, 1, writevalue);
         bContinue = FALSE;
      }
      else if (atoi((char *)inputBuffer) == 0)
      {
         writevalue = readvalue & ~(1 << statusbit);
         naibrd_1553_WriteReg(devnum, 1, writevalue);
         bContinue = FALSE;
      }
      else if (toupper(inputBuffer[0]) == 'Q')
      {
         bContinue = FALSE;
      }
      else
      {
         printf("\nInvalid Response. Nothing Set.\n\n");
      }
   }

   return bContinue;
}

bool_t RT_RunStop(int16_t devnum)
{
   bool_t bQuit = FALSE;
   uint16_t registervalue;
   nai_1553_t status;
   int8_t inputBuffer[80];
   int32_t inputResponseCnt;

   /* Get the current run/stop state */
   printf("\nCurrent RT State: ");
   registervalue = naibrd_1553_ReadReg(devnum, 1);
   if (registervalue & NAI_1553_CONFIG_REG1_MASK_FUNCTION_RT)
   {
      printf("RUNNING\n\n");
   }
   else
   {
      printf("STOPPED\n\n");
   }

   printf("\nRun (R) or Stop (S) RT: ", NAI_QUIT_CHAR);
   bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
   if (!bQuit)
   {
      if (toupper(inputBuffer[0]) == 'R')
      {
         status = naibrd_1553_RtStart(devnum);
         if (status == 0)
         {
            printf("\n\nRT RUNNING\n\n");
         }
      }
      else if (toupper(inputBuffer[0]) == 'S')
      {
         status = naibrd_1553_RtStop(devnum);
         if (status == 0)
         {
            printf("\n\nRT STOPPED\n\n");
         }
      }
      else
         printf("\nInvalid Response.\n\n");
   }

   return FALSE;
}

bool_t File_IOTest(int16_t devnum)
{
   uint8_t filename[128];
   FILE* deffile = NULL;
   int32_t i;
   int8_t buffer[128];
   bool_t passed = TRUE;

   sprintf((char *)filename, "test%d.txt", devnum);
   deffile = fopen((const char *)filename,"w");
   if (deffile != NULL)
   {
      for (i = 0; i <= 5; i++)
      {
         fprintf(deffile, "%d\n", i);
      }
      fclose(deffile);
   }

   deffile = fopen((const char *)filename,"r");
   if (deffile != NULL)
   {
      for (i = 0; i <= 5; i++)
      {
         fgets((char *)buffer, sizeof(buffer), deffile);
         if (i != (int32_t)atol((const char *)buffer))
         {
            printf("\n\nTest Failed.\n");
            passed = FALSE;
            break;
         }
      }

      if (passed)
         printf("\n\nTest Passed.\n");

      fclose(deffile);
      remove((char *)filename);
   }

   return FALSE;
}

void DisplaySubaddressTable(int16_t devnum)
{
   uint32_t uiLegality;
   int32_t i;

   printf("\n\n");
   printf("*********** SUBADDRESS LEGALIZATION TABLE ***********\n");
   printf("SUBADDRESS  NON-BROADCAST             BROADCAST       \n");
   printf("           RX          TX          RX          TX     \n");

   for (i = 0; i < 32; i++)
   {
      printf(" %02d    ", i);
      naibrd_1553_RtMessageLegalityGetStatus(devnum, NAI_1553_RT_ADDRESS_OWN, NAI_1553_MT_FILTER_RX, (uint16_t)i, &uiLegality);
      printf("0x%08X  ", uiLegality);
      naibrd_1553_RtMessageLegalityGetStatus(devnum, NAI_1553_RT_ADDRESS_OWN, NAI_1553_MT_FILTER_TX, (uint16_t)i, &uiLegality);
      printf("0x%08X  ", uiLegality);
      naibrd_1553_RtMessageLegalityGetStatus(devnum, NAI_1553_RT_ADDRESS_BROADCAST, NAI_1553_MT_FILTER_RX, (uint16_t)i, &uiLegality);
      printf("0x%08X  ", uiLegality);
      naibrd_1553_RtMessageLegalityGetStatus(devnum, NAI_1553_RT_ADDRESS_BROADCAST, NAI_1553_MT_FILTER_TX, (uint16_t)i, &uiLegality);
      printf("0x%08X\n", uiLegality);
   }
   printf("\n\n");
}

Help Bot

X