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

ETC BasicOps

ETC BasicOps

Explanation

About This Code

This C application code is a sample provided by North Atlantic Industries (NAI) for interacting with their embedded function modules using a Software Support Kit (SSK). This specific application is designed to interact with an Elapsed Time Counter (ETC) feature embedded in the NIU SIU Chassis.

Dependencies and Includes

The code includes several standard and custom headers:

  • Standard Headers:

  • stdio.h: Used for input/output functions such as printf().

  • string.h: Contains functions like memcpy().

  • stdlib.h: Provides general utilities including atoi().

  • ctype.h: Contains functions like toupper().

  • Custom Headers:

  • "include/naiapp_boardaccess_menu.h"

  • "include/naiapp_boardaccess_query.h"

  • "include/naiapp_boardaccess_access.h"

  • "include/naiapp_boardaccess_display.h"

  • "include/naiapp_boardaccess_utils.h"

  • "naibrd.h"

  • "advanced/nai_ether_adv.h"

  • "devices/naibrd_DevEtc.h"

These custom headers provide necessary definitions and function declarations for interacting with NAI’s embedded modules.

Function Definitions

DisplayETCApplicationDescription()

This static function outputs a description of the ETC application, informing the user about its purpose and usage.

int32_t main(int argc, char* argv)*

This function serves as the entry point of the application.

Key Variables: - nai_status_t status: Holds the status of various operations. - int32_t g_etc_cardIndex: Logical reference to the ETC device. - bool_t bQuit: Flag to terminate the application. - bool_t bEthernet: Indicates whether communication is via Ethernet. - bool_t maximillionBrd: Flag to identify the board type. - int32_t Protocol, Port: Defines communication protocol and port. - int8_t szPort[NAI_MAX_PORT_LEN]: Stores the port as a string. - int8_t szIPAddress[NAI_MAX_IP_LEN]: Stores the IP address. - int8_t inputBuffer[80]: Buffer to take user input. - int32_t inputResponseCnt: Stores the count of responses. - int i, tst_cnt: Loop variables, tst_cnt determines the number of test iterations.

Execution Flow:

  1. Argument Parsing:

    • Parses command-line arguments to determine the number of iterations (tst_cnt).

  2. Display Description:

    • Calls DisplayETCApplicationDescription() to print the application purpose and instructions.

  3. Ethernet Configuration:

    • Asks the user if the application communicates via Ethernet. Defaults to 'Y' if no response is given.

    • Configures the Ethernet settings by calling naiapp_query_EthernetCfg() if Ethernet communication is selected.

  4. Communication Initialization:

    • Based on the communication protocol (Ethernet or I2C), opens the ETC device using naibrd_ETC_Open().

  5. Operational Loop:

    • If initialization is successful, an operational loop runs tst_cnt times.

    • In each iteration:

    • Fetches total seconds using naibrd_ETC_GetTotalSeconds().

    • Fetches elapsed time details using naibrd_ETC_GetElapsedTime().

    • Outputs the fetched data to the console.

    • Introduces a delay of 1 second between iterations using nai_msDelay().

  6. Cleanup:

    • Waits for the user to press Enter to exit.

    • Closes the ETC device using naibrd_Close().

    • Returns 0 indicating successful execution, or -1 if the application was terminated prematurely.

Enumerations and Custom Types

  • nai_status_t: Enum likely defines various status codes.

  • bool_t: Custom boolean type.

  • Various Constants (NAI_MAX_CARDS, NAI_MAX_PORT_LEN, NAI_MAX_IP_LEN): Define specific limits and constraints in the application.

Conclusion

This sample application demonstrates how to interact with NAI’s Elapsed Time Counter feature in an embedded environment, supporting both Ethernet and I2C communication protocols. The code handles user input, device initialization, data fetching, and cleanup using NAI-provided functions and utilities.

#include <stdio.h>  /* for printf(),getchar() */
#include <string.h> /* for void *memcpy(void *dest, const void *src, size_t n); */
#include <stdlib.h>
#include <ctype.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"

/* naibrd include files */
#include "naibrd.h"
#include "advanced/nai_ether_adv.h"
#include "devices/naibrd_DevEtc.h"

static void DisplayETCApplicationDescription()
{
   printf("                        ETC Application Description\n");
   printf("---------------------------------------------------------------------------------\n");
   printf("This application is intended to run with the SIU Chassis that supports\n");
   printf("the Elapsed Time Counter (ETC) feature.\n\n");

   printf("When this application is running externally from the SIU Chassis,\n");
   printf("the NAI Ethernet Listener Server must be running in the main processor\n");
   printf("in the NIU SIU Chassis (ex. 75G5, 75INT2, 75PPC1, 75ARM1, etc.),\n");
   printf("and the communication method must be Ethernet.\n\n");

   printf("When this application is running on the main processor in the NIU SIU Chassis\n");
   printf("(ex. 75INT2, 75PPC1, 75ARM1, etc.), the communication method must be I2C.\n\n");

   printf("\n");
}

/**************************************************************************************************************/
/**
<summary>
The purpose of the PS_BasicOps is to illustrate the methods to call in the naibrd library to perform basic
 monitor operations with the PS4 Power Supply.

</summary>
*/
/**************************************************************************************************************/
#if defined (__VXWORKS__)
int32_t ETC_BasicOps(void)
#else
int32_t main(int argc,char** argv)
#endif
{
   nai_status_t status = NAI_SUCCESS;
   int32_t g_etc_cardIndex = 1; /* Note, the g_etc_cardIndex is a logical reference to the Elasped Time Counter device - can be any value between (0 - NAI_MAX_CARDS-1) */
   bool_t bQuit = FALSE;

   uint32_t year;
   uint32_t day;
   uint32_t hour;
   uint32_t min;
   uint32_t sec;

   bool_t bEthernet = FALSE;
   bool_t maximillionBrd = TRUE;
   int32_t Protocol = 0;
   int8_t Port = 0;
   int8_t szPort[NAI_MAX_PORT_LEN];
   int8_t szIPAddress[NAI_MAX_IP_LEN];
   int8_t inputBuffer[80];
   int32_t inputResponseCnt;

   int i,tst_cnt=10;

#if defined (LINUX)
   if ( argc == 2 )
   {
      tst_cnt=atoi(argv[1]);
   }
#elif WIN32
   UNREFERENCED_PARAMETER(argc);
   UNREFERENCED_PARAMETER(argv);
#endif

   DisplayETCApplicationDescription();

   /* Check where this application is running */
   printf("Is this application running externally and communicating to the SIU\n");
   printf("via Ethernet? (default: Y) : ");
   bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
   if (!bQuit)
   {
      if ((inputResponseCnt == 0) || (toupper(inputBuffer[0]) == 'Y'))
      {
         bEthernet = TRUE;
         /* Get the IP Address for the NAI board */
         bQuit = naiapp_query_EthernetCfg(maximillionBrd, &Protocol, &Port, &szIPAddress[0]);
         if (!bQuit)
         {
            sprintf((char*)&szPort[0], "%d", Port);
         }
      }
   }

   if (!bQuit)
   {
      if (bEthernet)
      {
         check_status(naibrd_SetIPAddress(g_etc_cardIndex, (const char *)&szIPAddress[0], (const char *)&szPort[0]));
         if (Protocol == NAI_TCP_PROTOCOL)
         {
            status = check_status(naibrd_ETC_Open(g_etc_cardIndex, NAIBRD_COMM_ETHER_TCP));
         }
         else
         {
            status = check_status(naibrd_ETC_Open(g_etc_cardIndex, NAIBRD_COMM_ETHER_UDP));
         }
      }
      else
      {
         status = check_status(naibrd_ETC_Open( g_etc_cardIndex, NAIBRD_COMM_I2C));
      }

      if (status == NAI_SUCCESS)
      {
         for ( i = 0; i < tst_cnt; i++ )
         {
            if ( check_status(naibrd_ETC_GetTotalSeconds(g_etc_cardIndex, &sec)) == NAI_SUCCESS  )
            {
                printf("<%d/%d>total seconds: 0x%08X\n", i+1, tst_cnt, sec);
            }

            if ( check_status(naibrd_ETC_GetElapsedTime(g_etc_cardIndex, &year, &day, &hour, &min, &sec) ) == NAI_SUCCESS  )
            {
               printf("<%d/%d>year=%u,day=%u,hour=%u,min=%u,sec=%u\n\n", i+1, tst_cnt, year, day, hour, min, sec);
            }
            nai_msDelay(1000);
         }
      }

      printf("Type Enter key to exit program : ");
      naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);

      /* Close the board */
      naibrd_Close(g_etc_cardIndex);

      return 0;
   }
   else
   {
      return -1;
   }
}

Help Bot

X