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

SER EtherInterrupts Tx

SER EtherInterrupts Tx

Explanation

North Atlantic Industries (NAI) Sample Application Code Explained

This C sample application demonstrates how to interface with NAI’s embedded function modules using their Software Support Kit (SSK). The application configures and handles Ethernet interrupts for a serial module.

Code Breakdown

Header Files - Standard Libraries: Standard C libraries for input/output operations ('stdio.h'), memory management ('stdlib.h'), string manipulation ('string.h'), and time functions ('time.h'). - NAI App Libraries: These are custom libraries provided by NAI for board access, including menu operations, query functions, access utilities, display functions, and general utilities. - 'naiapp_boardaccess_menu.h' - 'naiapp_boardaccess_query.h' - 'naiapp_boardaccess_access.h' - 'naiapp_boardaccess_display.h' - 'naiapp_boardaccess_utils.h' - NAI Board Libraries: These include specific functions for interacting with the NAI board and its modules. - 'nai.h' - 'naibrd.h' - 'naibrd_ser.h' - 'naibrd_ether.h' - 'nai_ether_adv.h' - 'nai_map_ser.h' - Platform-Specific Headers: Platform-specific network headers for Windows, Linux, and VxWorks are included to manage socket operations.

Conditional Compilation - The code contains preprocessor directives to handle different platforms (Windows, Linux, VxWorks), including custom type definitions and function redefinitions to maintain compatibility.

Constants and Definitions - Configuration File: Default configuration file: 'default_SerEthInterruptsTx.txt'. - Interrupt Vectors: Defines interrupt vectors for different serial channels. - Default Values: Various default values for Ethernet commands, ports, and IP addresses used in the application.

Function Prototypes The prototypes for various static functions that manage serial transmission, initialization, and handling of Ethernet interrupts.

Main Function For VxWorks, the entry point is 'SER_EtherInterrupts_Tx()', while for other platforms, it�s 'main()'. The application prompts the user for a card index and module number, then proceeds to initialize and handle the serial Ethernet interrupts.

Static Functions

'Run_SER_EthInterrupt_Tx' - Purpose: Handles the configuration and execution of serial transmit operations with Ethernet interrupts. - Parameters: Takes a card index, module number, and module ID. - Initializes the serial transmission and Ethernet interrupt handling by configuring the necessary settings and creating a thread for handling unprompted Ethernet replies.

'QueryEthInterruptTxIPAddr' - Purpose: Queries the user for the IP address for receiving serial interrupt commands. - Returns: A boolean value indicating if the user wishes to quit.

'Cfg_Tx_Serial' - Purpose: Configures the serial channel for transmission. - Parameters: Takes a card index, module number, and channel number. - Configuration: Sets various serial channel parameters such as protocol, interface level, clock mode, parity, data bits, stop bits, and baud rate.

'InitSERTxIDRCommands' - Purpose: Initializes interrupt driven response (IDR) commands for the serial Tx interrupts. - Parameters: Takes a card index, module, channel, commands buffer, command count, and command length. - Returns: NAI status code.

'MakeSerialTxReadRegsCommand' - Purpose: Formats the buffer for an Ethernet read registers command. - Parameters: Takes relevant parameters like whether the module supports Gen4 Ethernet, start index, module offset, channel, commands buffer, and command length.

'MakeSerialTxWriteRegsCommand' - Purpose: Formats the buffer for an Ethernet write registers command. - Parameters: Similar to the read command but configures for a write operation.

'UPR_SerialTxEthInt_Handler' - Purpose: Thread function to handle unprompted reply Ethernet messages. - Depending on the protocol (TCP/UDP), it creates the relevant server to receive messages.

'CreateSerialTxIDRTCPServer' - Purpose: Creates a TCP server to listen for and handle incoming Ethernet messages. - Operations: Socket creation, binding, listening, and handling incoming messages.

'CreateSerialTxIDRUDPServer' - Purpose: Creates a UDP server to listen for and handle incoming Ethernet messages. - Operations: Similar to the TCP server but uses UDP protocol.

'DecodeUPRSerialTxIDRMessages' - Purpose: Decodes received IDR messages and calls relevant functions to process and display them.

'ParseSerialTxIDRResponse' - Purpose: Parses the IDR response to break it down into individual Ethernet messages.

'DisplayDecodedSerialTxIDRResponse' - Purpose: Displays the parsed data from the Ethernet message.

Conclusion This sample code demonstrates configuring and handling serial transmission with Ethernet interrupts using NAI’s hardware and libraries. It covers initialization, user interaction, and message handling. Each static function plays a crucial role in managing specific tasks related to the operation, ensuring a modular and organized approach to handling the embedded function module’s features.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.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 "nai.h"
#include "naibrd.h"
#include "functions/naibrd_ser.h"
#include "naibrd_ether.h"
#include "advanced/nai_ether_adv.h"
#include "maps/nai_map_ser.h"

#if defined (WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
/* Need to link with Ws2_32.lib */
#pragma comment (lib, "Ws2_32.lib")
/* The following eliminates the FD_SET warning C4127 associated with the do...while(0) construct in the Microsoft winsock2.h file */
#pragma warning (disable:4127)
#elif (LINUX)
#include <sys/errno.h>
#include <pthread.h>
typedef int32_t SOCKET;
#define ZeroMemory(S, N)         memset((S), 0, (N))
#define closesocket(SD)          close(SD)
#define INVALID_SOCKET -1
#define SOCKET_ERROR   -1
#define SD_RECEIVE        0
#define SD_SEND           1
#define SD_BOTH           2
#elif (__VXWORKS__)
#include <netinet/ip.h>
#define ZeroMemory(S, N)         memset((S), 0, (N))
#define closesocket(SD)          close(SD)
#define INVALID_SOCKET -1
#define SOCKET_ERROR   -1
#define SD_RECEIVE        0
#define SD_SEND           1
#define SD_BOTH           2
#endif

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

/* Function prototypes */
static bool_t Run_SER_EthInterrupt_Tx(int32_t cardIndex, int32_t module, uint32_t modid);
static bool_t QueryEthInterruptTxIPAddr(void);
static void Cfg_Tx_Serial(int32_t cardIndex, int32_t module, int32_t channel);
static nai_status_t InitSERTxIDRCommands(int32_t cardIndex, int32_t module, int32_t channel, uint8_t commands[], uint16_t *cmdcount, uint16_t *cmdlen);
static void MakeSerialTxReadRegsCommand(bool_t bGen4Ether, uint16_t startIndex, uint32_t moduleOffset, int32_t channel, uint8_t commands[], uint16_t *cmdlen);
static void MakeSerialTxWriteRegsCommand(bool_t bGen4Ether, uint16_t startIndex, uint32_t moduleOffset, int32_t channel, uint8_t commands[], uint16_t *cmdlen);
static void CreateSerialTxIDRTCPServer(void);
static void CreateSerialTxIDRUDPServer(void);
static void DecodeUPRSerialTxIDRMessages(const uint8_t msg[], int32_t msgsize);
static bool_t ParseSerialTxIDRResponse(int32_t startIndex, int32_t rsparraysize, uint8_t response[], uint16_t *idrrsplen, int32_t idrrsparraysize, uint8_t idr_response[]);
static void DisplayDecodedSerialTxIDRResponse(uint16_t responselen, uint8_t response[]);

#define NAI_SER_CHANNEL1_INTERRUPT_VECTOR             0x000000A1u
#define NAI_SER_CHANNEL2_INTERRUPT_VECTOR             0x000000A2u
#define NAI_SER_CHANNEL3_INTERRUPT_VECTOR             0x000000A3u
#define NAI_SER_CHANNEL4_INTERRUPT_VECTOR             0x000000A4u

static bool_t bGen4SerialIDRCommands = FALSE;
/* Default Ethernet Command Count specified for the IDR Commands.
   Change this to match the Command Count specified in InitSERTxIDRCommands() routine
*/
#define DEF_SER_TX_IDR_ID                           1
#define DEF_SER_TX_ETHER_CMD_COUNT                  2   /* Number of Ethernet Commands in IDR Configuration */
#define DEF_SER_TX_RESPONSE_PROTOCOL                ETHER_GEN4_TCP_PROTOCOL
#define DEF_SER_TX_RESPONSE_IP_LEN                  ETHER_GEN4_IPv4_ADDR_LEN

static uint16_t DEF_SER_TX_RESPONSE_PORT = 52801;
static uint8_t  DEF_SER_TX_RESPONSE_IP_ADDR[] = { 192,168,1,100 };

#define CLEAR_FIFO_TIMEOUT 1000  /* 1 second */
#define TX_DATA_COUNT      8
static int32_t xmitDataValue = 0;

static const uint32_t NAI_SER_GEN5_ChanStatusLatchedAddr_4Ch[] = NAI_SER_GEN5_CHAN_STATUS_LATCHED_ADD;

#if defined (__VXWORKS__)
int UPR_SerialTxEthInt_Handler(int Param);
#elif LINUX
pthread_t interruptSerialTxThread;
static void* UPR_SerialTxEthInt_Handler(void* lpParam);
#else /* Default Windows */
DWORD WINAPI UPR_SerialTxEthInt_Handler(LPVOID lpParam);
#endif
static int terminateSerialTxThread;

/**************************************************************************************************************/
/** \defgroup SEREthIntTx Serial Ethernets Interrupts - Transmit
The purpose of the SER_EtherInterrupts_Tx is to demonstrate the methods to call in the naibrd library to
perform Ethernet interrupt operations using NAI's ethernet protocol with a given serial module.
*/
/**************************************************************************************************************/
#if defined (__VXWORKS__)
int32_t SER_EtherInterrupts_Tx(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_SER_EthInterrupt_Tx(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;
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
Queries the user for which channel they would like to configure interrupts on the following two Serial module
statuses: Transmit Buffer Almost Empty and Transmit Complete. This routine will create a thread to wait and
receive the unprompted interrupt ethernet messages.
\param cardIndex (Input) Logical Card Index assigned to connection with the NAI_BOARD (0 - NAI_MAX_CARDS-1).
\param module    (Input) Module Number of the module to access (1 - [max modules for board]).
\param modid     (Input) The ID of the module.
*/
/**************************************************************************************************************/
static bool_t Run_SER_EthInterrupt_Tx(int32_t cardIndex, int32_t module, uint32_t modid)
{
   bool_t bQuit = FALSE;
   nai_status_t status;
   int32_t MAX_CHANNELS = naibrd_SER_GetChannelCount(modid);
   int32_t channel;
   nai_ser_status_type_t type;
   int32_t vector;
   uint8_t commands[MAX_ETHER_IDR_CMD_CNT*MAX_ETHER_BLOCK_REG_CNT];
   uint16_t cmdcount = 0;
   uint16_t cmdlength = 0;
   uint16_t protocol = DEF_SER_TX_RESPONSE_PROTOCOL;
   uint16_t iplen = DEF_SER_TX_RESPONSE_IP_LEN;
   uint16_t port;
   uint8_t ip[DEF_SER_TX_RESPONSE_IP_LEN];
   int32_t i;
   uint32_t xmitData[TX_DATA_COUNT];
   uint32_t numWordsSend = 0;
   int8_t inputBuffer[80];
   int32_t inputResponseCnt;

   /* Determine if the board selected supports the Generation 4 Ethernet Commands */
   bGen4SerialIDRCommands = SupportsGen4Ether(cardIndex);

   /* Get the Serial channel to configure for Tx Ethernet interrupts */
   printf("Serial Tx Channel Selection:");
   bQuit = naiapp_query_ChannelNumber(MAX_CHANNELS, 1, &channel);

   if (!bQuit)
      bQuit = QueryEthInterruptTxIPAddr();

   if (!bQuit)
   {
      if (bGen4SerialIDRCommands)
      {
         /* Make sure there are no pending interrupts before interrupts are enabled */
         switch (channel)
         {
            case 1:
               type = NAI_SER_STATUS_CHANNEL1_LATCHED;
               vector = NAI_SER_CHANNEL1_INTERRUPT_VECTOR;
               break;
            case 2:
               type = NAI_SER_STATUS_CHANNEL2_LATCHED;
               vector = NAI_SER_CHANNEL2_INTERRUPT_VECTOR;
               break;
            case 3:
               type = NAI_SER_STATUS_CHANNEL3_LATCHED;
               vector = NAI_SER_CHANNEL3_INTERRUPT_VECTOR;
               break;
            case 4:
               type = NAI_SER_STATUS_CHANNEL4_LATCHED;
               vector = NAI_SER_CHANNEL4_INTERRUPT_VECTOR;
               break;
            default:
               type = NAI_SER_STATUS_CHANNEL1_LATCHED;
               vector = NAI_SER_CHANNEL1_INTERRUPT_VECTOR;
               break;
         }

         /* Clear NAI_SER_GEN5_INT_TXBUF_ALMOST_EMPTY and NAI_SER_GEN5_INT_TXCOMPLETE channel status */
         check_status(naibrd_SER_ClearChannelStatus(cardIndex, module, type, NAI_SER_GEN5_INT_TXBUF_ALMOST_EMPTY | NAI_SER_GEN5_INT_TXCOMPLETE));

         /* Configure the Serial channel for transmission */
         Cfg_Tx_Serial(cardIndex, module, channel);

         /* Configure the Interrupt Driven Response Ethernet Command */
         status = check_status(naibrd_Ether_ClearIDRConfig(cardIndex, (uint16_t)DEF_SER_TX_IDR_ID));

         InitSERTxIDRCommands(cardIndex, module, channel, commands, &cmdcount, &cmdlength);
         for (i = 0; i < DEF_SER_TX_RESPONSE_IP_LEN; i++)
            ip[i] = DEF_SER_TX_RESPONSE_IP_ADDR[i];
         port = DEF_SER_TX_RESPONSE_PORT;
         status = check_status(naibrd_Ether_SetIDRConfig(cardIndex, (uint16_t)DEF_SER_TX_IDR_ID, protocol, iplen, ip, port, vector, cmdcount, cmdlength, commands));
         if (status == NAI_SUCCESS)
         {
            printf("IDR ID = %d configured.\n", DEF_SER_TX_IDR_ID);
         }

         /* Configure to generate interrupts on NAI_SER_GEN5_INT_TXBUF_ALMOST_EMPTY and NAI_SER_GEN5_INT_TXCOMPLETE */
         check_status(naibrd_SER_SetInterruptEdgeLevel(cardIndex, module, type, 0));  /* Edge triggered */
         check_status(naibrd_SER_SetInterruptVector(cardIndex, module, type, vector));
         check_status(naibrd_SER_SetInterruptSteering(cardIndex, module, type, NAIBRD_INT_STEERING_ON_BOARD_1));
         check_status(naibrd_SER_SetInterruptEnable(cardIndex, module, type, NAI_SER_GEN5_INT_TXBUF_ALMOST_EMPTY | NAI_SER_GEN5_INT_TXCOMPLETE));

         /* Start the IDR */
         status = check_status(naibrd_Ether_StartIDR(cardIndex, (uint16_t)DEF_SER_TX_IDR_ID));
         if (status == NAI_SUCCESS)
         {
            printf("IDR ID = %d Started\n", DEF_SER_TX_IDR_ID);
         }

         /* Create a thread that will receive Unprompted Reply interrupt messages */
         terminateSerialTxThread = FALSE;

#if defined (__VXWORKS__)
         taskSpawn("uprSerialTxHandler", 100, 0, 10000, (FUNCPTR)UPR_SerialTxEthInt_Handler, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0);
#elif defined (LINUX)
         pthread_create(&interruptSerialTxThread, NULL, (void*)UPR_SerialTxEthInt_Handler, NULL);
#else /* Default Windows */
         CreateThread(NULL, 0, UPR_SerialTxEthInt_Handler, NULL, 0, NULL);
#endif

         while (!bQuit)
         {
            printf("Type Enter Key to transmit data or %c to exit program : ", NAI_QUIT_CHAR);
            bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
            if (!bQuit)
            {
               for (i = 0; i < TX_DATA_COUNT; i++)
               {
                  xmitData[i] = (uint16_t)(xmitDataValue & 0xFF); /* Only change the lower byte of data */
                  xmitDataValue++;
               }
               check_status(naibrd_SER_TransmitBuffer(cardIndex, module, channel, sizeof(xmitData[0]), xmitData, TX_DATA_COUNT, &numWordsSend));
               printf("***Serial Transmit: %d words sent\n", numWordsSend);
            }
            else
            {
               terminateSerialTxThread = TRUE;

               /* Stop the IDR */
               status = check_status(naibrd_Ether_StopIDR(cardIndex, (uint16_t)DEF_SER_TX_IDR_ID));
               if (status == NAI_SUCCESS)
               {
                  printf("IDR ID = %d Stopped\n", DEF_SER_TX_IDR_ID);
               }
            }
         }
      }
      else
      {
         printf("Serial Ethernet Interrupt Support Prior to Generation 4 Ethernet commands currently not supported\n");
         bQuit = TRUE;
      }
   }
   return bQuit;
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This function queries the user to enter the IP address associated with receiving Serial Interrupt commands.
*/
/**************************************************************************************************************/
static bool_t QueryEthInterruptTxIPAddr(void)
{
   bool_t bQuit = FALSE;
   bool_t bContinue = TRUE;
   int8_t inputBuffer[80];
   int32_t inputResponseCnt;

   /* Get the IP Address */
   if (!bQuit)
   {
      bContinue = TRUE;
      while (bContinue)
      {
         /* Get the Response IP Address */
         printf("Please Enter IDR Response IP Address: [default=%d.%d.%d.%d]): ",
            DEF_SER_TX_RESPONSE_IP_ADDR[0], DEF_SER_TX_RESPONSE_IP_ADDR[1], DEF_SER_TX_RESPONSE_IP_ADDR[2], DEF_SER_TX_RESPONSE_IP_ADDR[3]);
         bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
         if (bQuit)
            bContinue = FALSE;
         else
         {
            if (inputResponseCnt > NAI_MAX_IP_LEN)
               printf("ERROR: Invalid IP Address.\n");
            else
            {
               if (inputResponseCnt > 0)
                  ParseIPv4Address((char *)inputBuffer, DEF_SER_TX_RESPONSE_IP_ADDR);
            }
         }
         if (!bQuit)
         {
            /* Get the Response Port */
            printf("Please Enter IDR Response Port: [default=%d]): ", DEF_SER_TX_RESPONSE_PORT);
            bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
            if (bQuit)
               bContinue = FALSE;
            else
            {
               if (inputResponseCnt > 0)
                  DEF_SER_TX_RESPONSE_PORT = (uint16_t)atol((const char *)inputBuffer);
               bContinue = FALSE;
            }

         }
      }
   }
   return bQuit;
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This function configures the Serial channel. The Serial channel is configured to run in async mode, loopback
(so that external cabling is not needed to generate Tx interrupts), internal clock, no parity, 8 data bits,
1 stop bit, and 9600 baud.
\param cardIndex (Input) Logical Card Index assigned to connection with the NAI_BOARD (0 - NAI_MAX_CARDS-1).
\param module    (Input) Module Number of the module to access (1 - [max modules for board]).
\param channel   (Input) Channel Number of the channel to access (1 - [max channels for module]).
*/
/**************************************************************************************************************/
static void Cfg_Tx_Serial(int32_t cardIndex, int32_t module, int32_t channel)
{
   int32_t nCntlValueLo;
   int32_t i;

   check_status(naibrd_SER_ChannelReset(cardIndex, module, channel));
   check_status(naibrd_SER_ClearTxFifo(cardIndex, module, channel));
   nCntlValueLo = NAI_SER_CTRLLO_CLEAR_TX_FIFO;
   for (i = 0; i < CLEAR_FIFO_TIMEOUT && (nCntlValueLo & NAI_SER_CTRLLO_CLEAR_TX_FIFO); i++)
   {
      nai_ser_chanctrl chanCtrlRaw;
      check_status(naibrd_SER_GetChannelControlRaw(cardIndex, module, channel, &chanCtrlRaw));
      nCntlValueLo = chanCtrlRaw & 0x0000FFFF;
      nai_msDelay(1);
   }
   if (i == CLEAR_FIFO_TIMEOUT)
   {
      printf("Failed to clear FIFO for channel:%d\n", channel);
   }

   check_status(naibrd_SER_SetProtocol(cardIndex, module, channel, NAI_SER_PROTOCOL_ASYNC));       /* async mode */
   check_status(naibrd_SER_SetInterfaceLevel(cardIndex, module, channel, NAI_SER_INTF_LOOPBACK));  /* LoopBack */
   check_status(naibrd_SER_SetClockMode(cardIndex, module, channel, TXINT_RXINT));                 /* Tx and Rx internal */
   check_status(naibrd_SER_SetParity(cardIndex, module, channel, NAI_SER_PARITY_NONE));            /* No Parity */
   check_status(naibrd_SER_SetDataBits(cardIndex, module, channel, 8));                            /* 8 Data Bits */
   check_status(naibrd_SER_SetStopBits(cardIndex, module, channel, 1));                            /* 1 Stop Bit */
   check_status(naibrd_SER_SetBaudrate(cardIndex, module, channel, 9600));                         /* 9600, async mode */
   check_status(naibrd_SER_AsyncTransmitControl(cardIndex, module, channel, 1));                   /* Enable Transmit */
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This function configures the IDR (Interrupt Driven Response) commands when a Serial Tx interrupt occurs.
There are two Ethernet commands that will be processed by the board when a Serial Tx interrupt occurs. This
routine calls the MakeSerialTxReadRegsCommand() and MakeSerialTxWriteRegsCommand() to configure the two
Ethernet commands to include when setting the IDR.
\param cardIndex (Input) Logical Card Index assigned to connection with the NAI_BOARD (0 - NAI_MAX_CARDS-1).
\param module    (Input) Module Number of the module to access (1 - [max modules for board]).
\param channel   (Input) Channel Number of the channel to access (1 - [max channels for module]).
\param commands  (Input) Message buffer to fill with the Ethernet Read Registers elements.
\param cmdcount  (Output) Number of ethernet commands.
\param cmdlen    (Output) Message size.
*/
/**************************************************************************************************************/
static nai_status_t InitSERTxIDRCommands(int32_t cardIndex, int32_t module, int32_t channel, uint8_t commands[], uint16_t *cmdcount, uint16_t *cmdlen)
{
   nai_status_t status = NAI_SUCCESS;
   uint16_t msgIndex = 0;
   uint16_t idrcmdcnt = 0;
   uint16_t ethcmdlen = 0;
   uint16_t idrcmdlen = 0;
   uint32_t moduleOffset;

   status = check_status(naibrd_GetModuleOffset(cardIndex, module, &moduleOffset));
   if (status == NAI_SUCCESS)
   {
      if (bGen4SerialIDRCommands)
      {
         /* First command */
         MakeSerialTxReadRegsCommand(bGen4SerialIDRCommands, msgIndex, moduleOffset, channel, commands, &ethcmdlen);
         msgIndex += ethcmdlen;
         idrcmdlen += ethcmdlen;
         idrcmdcnt++;

         /* Next command */
         MakeSerialTxWriteRegsCommand(bGen4SerialIDRCommands, msgIndex, moduleOffset, channel, commands, &ethcmdlen);
         msgIndex += ethcmdlen;
         idrcmdlen += ethcmdlen;
         idrcmdcnt++;

         *cmdcount = idrcmdcnt;
         *cmdlen = msgIndex;
      }
      else
      {
         printf("Serial Ethernet Interrupt Support Prior to Generation 4 Ethernet commands currently not supported\n");
         status = NAI_ERROR_NOT_SUPPORTED;
      }
   }
   return status;
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This function formats the buffer for the Ethernet Read Registers command.
\param bGen4Ether   (Input) Boolean to represent if the module supports NAI's Gen4 ethernet protocol.
\param startIndex   (Input) Offset to index the commands[] array.
\param moduleOffset (Input) Address offset of the module.
\param channel      (Input) Serial channel.
\param commands     (Input) Message buffer to fill with the Ethernet Read Registers elements.
\param cmdlen       (Output) Message size.
*/
/**************************************************************************************************************/
static void MakeSerialTxReadRegsCommand(bool_t bGen4Ether, uint16_t startIndex, uint32_t moduleOffset, int32_t channel, uint8_t commands[], uint16_t *cmdlen)
{
   uint16_t msgIndex = startIndex;
   uint16_t seqno;
   uint32_t count, stride;
   uint32_t regaddr;

   if (bGen4Ether)
   {
      /* Create a ReadRegs command with the following attributes:
            Onboard Access, 32-bit Register Size,
            Reg Address: NAI_SER_GEN5_ChanStatusLatchedAddr_4Ch[channel-1] (to read the channel status)
            Count: 1, Stride 4
      */
      seqno = 0;
      regaddr = moduleOffset + NAI_SER_GEN5_ChanStatusLatchedAddr_4Ch[channel - 1];
      count = 1;
      stride = 4;
      msgIndex = (uint16_t)nai_ether_MakeReadMessage(&commands[startIndex], seqno, NAI_ETHER_GEN4, NAI_INTF_ONBOARD, regaddr, stride, count, NAI_REG32);

      *cmdlen = msgIndex;
   }
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This function formats the buffer for the Ethernet Write Registers command.
\param bGen4Ether   (Input) Boolean to represent if the module supports NAI's Gen4 ethernet protocol.
\param startIndex   (Input) Offset to index the commands[] array.
\param moduleOffset (Input) Address offset of the module.
\param channel      (Input) Serial channel.
\param commands     (Input) Message buffer to fill with the Ethernet Write Registers elements.
\param cmdlen       (Output) Message size.
*/
/**************************************************************************************************************/
static void MakeSerialTxWriteRegsCommand(bool_t bGen4Ether, uint16_t startIndex, uint32_t moduleOffset, int32_t channel, uint8_t commands[], uint16_t *cmdlen)
{
   uint16_t msgIndex = startIndex;
   uint16_t seqno;
   uint32_t count, stride;
   uint32_t regaddr;
   uint32_t data = NAI_SER_GEN5_INT_TXBUF_ALMOST_EMPTY | NAI_SER_GEN5_INT_TXCOMPLETE;  /* Clear interrupt bits */

   if (bGen4Ether)
   {
      /* Create a WriteRegs command with the following attributes:
            Onboard Access, 32-bit Register Size,
            Reg Address: NAI_SER_GEN5_ChanStatusLatchedAddr_4Ch[channel-1], Count: 1, Stride 4,
            Data: NAI_SER_GEN5_INT_TXBUF_ALMOST_EMPTY | NAI_SER_GEN5_INT_TXCOMPLETE (to clear the interrupts)
      */
      seqno = 0;
      regaddr = moduleOffset + NAI_SER_GEN5_ChanStatusLatchedAddr_4Ch[channel - 1];
      count = 1;
      stride = 4;

      msgIndex = (uint16_t)nai_ether_BeginWriteMessage(&commands[startIndex], seqno, NAI_ETHER_GEN4, NAI_INTF_ONBOARD, regaddr, stride, count, NAI_REG32);
      if (msgIndex >= 0)
      {
         msgIndex = (uint16_t)nai_ether_WriteMessageData(&commands[startIndex], msgIndex, NAI_REG32, &data, NAI_REG32, count);

         if (msgIndex >= 0)
         {
            msgIndex = (uint16_t)nai_ether_FinishMessage(&commands[startIndex], msgIndex, NAI_ETHER_GEN4);
         }
      }
      *cmdlen = msgIndex;
   }
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This thread calls CreateSerialTxIDRTCPServer or CreateSerialTxIDRUDPServer depending on the
DEF_SER_RESPONSE_PROTOCOL to receive the Interrupt Driven Response (IDR) Unprompted Ethernet Replies.
*/
/**************************************************************************************************************/
#if defined (__VXWORKS__)
int UPR_SerialTxEthInt_Handler(int32_t nParam)
{
#elif LINUX
static void* UPR_SerialTxEthInt_Handler(void *lpParam)
{
#else /* Default Windows */
DWORD WINAPI UPR_SerialTxEthInt_Handler(LPVOID lpParam)
{
#endif

   uint16_t protocol = DEF_SER_TX_RESPONSE_PROTOCOL;

#if defined (WIN32)
   UNREFERENCED_PARAMETER(lpParam);
#endif

   printf("\nUnprompted Reply Thread Started....\n");
   if (protocol == ETHER_GEN4_TCP_PROTOCOL)
      CreateSerialTxIDRTCPServer();
   else
      CreateSerialTxIDRUDPServer();
   printf("\nUnprompted Reply Thread Terminated\n");
   return 0;
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This function creates a TCP Listener socket to receive the Interrupt Driven Response (IDR) Unprompted Ethernet
Replies and waits for the Ethernet messages.
*/
/**************************************************************************************************************/
static void CreateSerialTxIDRTCPServer(void)
{
   nai_socket_t listensock = INVALID_SOCKET, clientsock = INVALID_SOCKET;
   struct addrinfo hints;
   struct addrinfo *info;
   uint8_t portbuf[10];
   uint8_t recvbuf[NAI_ETHER_MAX_PACKET_SIZE_BYTES];
   int32_t recvbuflen = NAI_ETHER_MAX_PACKET_SIZE_BYTES;
   int32_t result;

#if defined (WIN32)
   WSADATA wsaData;
   result = WSAStartup(MAKEWORD(2, 2), &wsaData);
   if (result != 0)
   {
      printf("WSAStartup failed with error: %d\n", result);
      return;
   }
#endif

   ZeroMemory(&hints, sizeof(hints));
   hints.ai_family = AF_INET;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_protocol = IPPROTO_TCP;
   hints.ai_flags = AI_PASSIVE;

   /* Resolve the server address and port */
   sprintf((char *)portbuf, "%d", DEF_SER_TX_RESPONSE_PORT);
#if defined (WIN32)
   result = getaddrinfo(NULL, (PCSTR)portbuf, &hints, &info);
#elif defined(LINUX) || defined(__VXWORKS__)
   result = getaddrinfo(NULL, (const char *)portbuf, &hints, &info);
#endif
   if (result != 0)
   {
      printf("getaddrinfo failed with error: %d\n", result);
#if defined (WIN32)
      WSACleanup();
#endif
      return;
   }

   /* Create a SOCKET for connecting to server */
   listensock = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
   if (listensock == INVALID_SOCKET)
   {
#if defined (WIN32)
      printf("socket failed with error: %ld\n", WSAGetLastError());
#elif defined(LINUX) || defined(__VXWORKS__)
      printf("socket failed with error: %s\n", strerror(errno));
#endif
      freeaddrinfo(info);
#if defined (WIN32)
      WSACleanup();
#endif
      return;
   }

   /* Setup the TCP listening socket */
   result = bind(listensock, info->ai_addr, (int)info->ai_addrlen);
   if (result == SOCKET_ERROR)
   {
#if defined (WIN32)
      printf("bind failed with error: %d\n", WSAGetLastError());
#elif defined(LINUX) || defined(__VXWORKS__)
      printf("bind failed with error: %s\n", strerror(errno));
#endif
      freeaddrinfo(info);
      closesocket(listensock);
#if defined (WIN32)
      WSACleanup();
#endif
      return;
   }

   freeaddrinfo(info);

   result = listen(listensock, SOMAXCONN);
   if (result == SOCKET_ERROR)
   {
#if defined (WIN32)
      printf("listen failed with error: %d\n", WSAGetLastError());
#elif defined(LINUX) || defined(__VXWORKS__)
      printf("listen failed with error: %s\n", strerror(errno));
#endif
      closesocket(listensock);
#if defined (WIN32)
      WSACleanup();
#endif
      return;
   }

   /* Accept a client socket */
   clientsock = accept(listensock, NULL, NULL);
   if (clientsock == INVALID_SOCKET)
   {
#if defined (WIN32)
      printf("accept failed with error: %d\n", WSAGetLastError());
#elif defined(LINUX) || defined(__VXWORKS__)
      printf("accept failed with error: %s\n", strerror(errno));
#endif
      closesocket(listensock);
#if defined (WIN32)
      WSACleanup();
#endif
      return;
   }

   /* No longer need server socket */
   closesocket(listensock);

   printf("Ready to receive a TCP messages...\n");

   /* Receive until the peer shuts down the connection */
   do
   {
      result = recv(clientsock, (char *)recvbuf, recvbuflen, 0);
      if (result > 0)
      {
         printf("\n\nTCP Server: Bytes received: %d\n", result);
         DecodeUPRSerialTxIDRMessages(recvbuf, result);
      }
      else if (result == 0)
         printf("Connection closing...\n");
      else
      {
#if defined (WIN32)
         printf("recv failed with error: %d\n", WSAGetLastError());
#elif defined(LINUX) || defined(__VXWORKS__)
         printf("recv failed with error: %s\n", strerror(errno));
#endif
         closesocket(clientsock);
#if defined (WIN32)
         WSACleanup();
#endif
         return;
      }
   } while ((result > 0) && (terminateSerialTxThread == FALSE));

   /* shutdown the connection when done */
   result = shutdown(clientsock, SD_SEND);
   if (result == SOCKET_ERROR)
   {
#if defined (WIN32)
      printf("shutdown failed with error: %d\n", WSAGetLastError());
#elif defined(LINUX) || defined(__VXWORKS__)
      printf("shutdown failed with error: %s\n", strerror(errno));
#endif
      closesocket(clientsock);
#if defined (WIN32)
      WSACleanup();
#endif
      return;
   }

   /*cleanup */
   closesocket(clientsock);
#if defined (WIN32)
   WSACleanup();
#endif
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This function creates a UDP Listener socket to receive the Interrupt Driven Response (IDR) Unprompted Ethernet
Replies and waits for the Ethernet messages.
*/
/**************************************************************************************************************/
static void CreateSerialTxIDRUDPServer()
{
   nai_socket_t recvsock;
   uint8_t recvbuf[NAI_ETHER_MAX_PACKET_SIZE_BYTES];
   int32_t recvbuflen = NAI_ETHER_MAX_PACKET_SIZE_BYTES;
#if defined (WIN32)
   SOCKADDR_IN recvaddr;
   SOCKADDR_IN sendaddr;
#elif defined(LINUX) || defined(__VXWORKS__)
   struct sockaddr_in recvaddr;
   struct sockaddr_in sendaddr;
#endif
   int32_t sendaddrsize = sizeof(sendaddr);
   int32_t result;

#if defined (WIN32)
   WSADATA wsaData;
   result = WSAStartup(MAKEWORD(2, 2), &wsaData);
   if (result != 0)
   {
      printf("WSAStartup failed with error: %d\n", result);
      return;
   }
#endif

   /* Create a new socket to receive datagrams on. */
   recvsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
   if (recvsock == INVALID_SOCKET)
   {
#if defined (WIN32)
      printf("socket failed with error: %ld\n", WSAGetLastError());
#elif defined(LINUX) || defined(__VXWORKS__)
      printf("socket failed with error: %s\n", strerror(errno));
#endif
#if defined (WIN32)
      WSACleanup();
#endif
      return;
   }

   /* Set up a SOCKADDR_IN structure that will tell bind that we
      want to receive datagrams from all interfaces using port DEF_RESPONSE_PORT.
   */
   /* The IPv4 family */
   recvaddr.sin_family = AF_INET;
   recvaddr.sin_port = htons(DEF_SER_TX_RESPONSE_PORT);
   /* From all interface (0.0.0.0) */
   recvaddr.sin_addr.s_addr = htonl(INADDR_ANY);

   /* Associate the address information with the socket using bind.
      At this point you can receive datagrams on your bound socket.
   */
#if defined (WIN32)
   result = bind(recvsock, (SOCKADDR *)&recvaddr, sizeof(recvaddr));
#elif defined(LINUX) || defined(__VXWORKS__)
   result = bind(recvsock, (struct sockaddr *)&recvaddr, sizeof(recvaddr));
#endif
   if (result == SOCKET_ERROR)
   {
#if defined (WIN32)
      printf("bind failed with error: %d\n", WSAGetLastError());
#elif defined(LINUX) || defined(__VXWORKS__)
      printf("bind failed with error: %s\n", strerror(errno));
#endif
      closesocket(recvsock);
#if defined (WIN32)
      WSACleanup();
#endif
      return;
   }

   printf("Ready to receive UDP messages...\n");

   while (terminateSerialTxThread == FALSE)
   {
      /* Call recvfrom() to get it then display the received data. */
#if defined (WIN32)
      result = recvfrom(recvsock, (char *)recvbuf, recvbuflen,
         0, (SOCKADDR *)&sendaddr, &sendaddrsize);
#elif defined(LINUX) || defined(__VXWORKS__)
      result = recvfrom(recvsock, (char *)recvbuf, recvbuflen, 0, (struct sockaddr *)&sendaddr, (socklen_t *)&sendaddrsize);
#endif
      if (result > 0)
      {
         printf("\n\nUDP Server: Bytes received: %d\n", result);
         DecodeUPRSerialTxIDRMessages(recvbuf, result);
      }
      else if (result <= 0)
#if defined (WIN32)
         printf("Connection closed with error code: %ld\n", WSAGetLastError());
#elif defined(LINUX) || defined(__VXWORKS__)
         printf("Connection closed with error code: %s\n", strerror(errno));
#endif
      else
#if defined (WIN32)
         printf("recvfrom() failed with error code: %d\n", WSAGetLastError());
#elif defined(LINUX) || defined(__VXWORKS__)
         printf("recvfrom() failed with error code: %s\n", strerror(errno));
#endif
   }

   /* When application is finished receiving datagrams close the socket. */
   printf("Server: Finished receiving. Closing the listening socket...\n");
   closesocket(recvsock);
#if defined (WIN32)
   WSACleanup();
#endif
   return;
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This function calls ParseSerialTxIDRResponse() to break down the IDR message received to individual Ethernet
messages and calls DisplayDecodedSerialTxIDRResponse() to display the data in the Ethernet message.
\param msg     (Input) Input message buffer.
\param msgsize (Input) Size of input message buffer.
*/
/**************************************************************************************************************/
static void DecodeUPRSerialTxIDRMessages(const uint8_t msg[], int32_t msgsize)
{
   uint32_t arraysize = MAX_ETHER_IDR_CMD_CNT * MAX_ETHER_BLOCK_REG_CNT;
   uint16_t responselen;
   uint8_t response[MAX_ETHER_BLOCK_REG_CNT];
   int32_t startIndex;

   startIndex = 0;
   while (startIndex < msgsize)
   {
      if (ParseSerialTxIDRResponse(startIndex, arraysize, (uint8_t *)msg, &responselen, MAX_ETHER_BLOCK_REG_CNT, response))
      {
         startIndex += responselen;
         DisplayDecodedSerialTxIDRResponse(responselen, response);
      }
   }
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This function break downs the IDR message received to individual Ethernet messages.
\param startIndex      (Input) Offset to index the commands[] array.
\param rsparraysize    (Input) Max array size.
\param response        (Input) Input message array.
\param idrrsplen       (Output) Length of idr_response array.
\param idrrsparraysize (Input)  Max Array size.
\param idr_response    (Output) Output ethernet messages array.
\returns True if function succeeded in parsing the data.
*/
/**************************************************************************************************************/
static bool_t ParseSerialTxIDRResponse(int32_t startIndex, int32_t rsparraysize, uint8_t response[], uint16_t *idrrsplen, int32_t idrrsparraysize, uint8_t idr_response[])
{
   bool_t bParsed = FALSE;
   bool_t bContinueCopy = TRUE;
   uint16_t preamble, postamble;
   int32_t rspIndex = startIndex;
   int32_t idrIndex = 0;

   preamble = (response[startIndex] << 8) | response[startIndex + 1];
   if (bGen4SerialIDRCommands)
   {
      if (preamble == ETHER_GEN4_PREAMBLE)
      {
         while ((bContinueCopy) && (idrIndex < idrrsparraysize) && (rspIndex < rsparraysize - 1))
         {
            postamble = (response[rspIndex] << 8) | response[rspIndex + 1];
            if (postamble == ETHER_GEN4_POSTAMBLE)
            {
               idr_response[idrIndex++] = response[rspIndex++];
               idr_response[idrIndex++] = response[rspIndex++];
               bContinueCopy = FALSE;
            }
            else
            {
               idr_response[idrIndex++] = response[rspIndex++];
            }
         }
         bParsed = TRUE;
         *idrrsplen = (uint16_t)idrIndex;
      }
   }
   else
   {
      printf("Serial Ethernet Interrupt Support Prior to Generation 4 Ethernet commands currently not supported\n");
   }
   return bParsed;
}

/**************************************************************************************************************/
/** \ingroup SEREthIntTx
This function displays the data in the Ethernet message.
\param responselen (Input) Length of array of ethernet messages.
\param response    (Input) Array holding ethernet messages.
*/
/**************************************************************************************************************/
static void DisplayDecodedSerialTxIDRResponse(uint16_t responselen, uint8_t response[])
{
   uint16_t seq;
   nai_ether_typecode_t tc;
   nai_ether_gen_t gen;
   int32_t size;
   int32_t offset;
   uint16_t seqrsptype;
   uint16_t seqrspidindex;
   uint16_t seqcmdindex;
   uint16_t datacnt;
   uint32_t data;
   int32_t i;

   if (bGen4SerialIDRCommands)
      gen = NAI_ETHER_GEN4;
   else
      gen = NAI_ETHER_GEN3;

   offset = nai_ether_DecodeMessageHeader(response, responselen, &seq, &tc, gen, &size);

   printf("Size=%d   ", size);

   /* Parse IDR Response Sequence Value */
   seqrsptype = seq & ETHER_GEN4_SEQ_UPR_MASK;                                     /* Bit 15: Unprompted Response  Bit 14: TDR(0)/IDR(1) Response */
   seqrspidindex = (seq & (ETHER_GEN4_SEQ_ID_MASK)) >> ETHER_GEN4_SEQ_ID_SHIFT;    /* Bits 13-10: TDR/IDR ID Index (Note, value is ID-1) */
   seqcmdindex = (seq & (ETHER_GEN4_SEQ_CMD_MASK)) >> ETHER_GEN4_SEQ_CMD_SHIFT;    /* Bits 9-6: Command Index */
   printf("seq:0x%04X (", seq);
   if (seqrsptype == ETHER_GEN4_SEQ_UPR_TDR)
      printf("Type=Unprompted TDR ");
   else if (seqrsptype == ETHER_GEN4_SEQ_UPR_IDR)
      printf("Type=Unprompted IDR ");
   else
      printf("Type=UNKNOWN(0x%X) ", seqrsptype);
   printf("ID=%d Cmd=%d)   ", seqrspidindex + 1, seqcmdindex + 1);

   switch (tc)
   {
      case NAI_ETHER_TYPECODE_RSP_COMMAND_COMPLETE_READ_4:
         printf("Typecode: Read Regs (0x%4X)\n", tc);
         datacnt = (responselen - 10) / NAI_REG32;
         printf("   Data (Count=%d): ", datacnt);
         for (i = 0; i < datacnt; i++)
         {
            if (i != 0)
               printf(",");
            data = 0;
            data = response[offset++] << 24;
            data |= response[offset++] << 16;
            data |= response[offset++] << 8;
            data |= response[offset++];
            printf("0x%08X", data);
         }
         printf("\n");
         break;
      case NAI_ETHER_TYPECODE_RSP_COMMAND_COMPLETE_WRITE_4:
         printf("Typecode: Write Regs (0x%4X)\n", tc);
         break;
      case NAI_ETHER_TYPECODE_RSP_COMMAND_COMPLETE_READ_BLOCK_4:
         printf("Typecode: Read Block (0x%4X)\n", tc);
         datacnt = (responselen - 10) / NAI_REG32;
         printf("   Data (Count=%d): ", datacnt);
         for (i = 0; i < datacnt; i++)
         {
            if (i != 0)
               printf(",");
            data = 0;
            data = response[offset++] << 24;
            data |= response[offset++] << 16;
            data |= response[offset++] << 8;
            data |= response[offset++];
            printf("0x%08X", data);
         }
         printf("\n");
         break;
      case NAI_ETHER_TYPECODE_RSP_COMMAND_COMPLETE_WRITE_BLOCK_4:
         printf("Typecode: Write Block (0x%4X)\n", tc);
         break;
      case NAI_ETHER_TYPECODE_RSP_ERROR_BLOCK_NOT_CONFIG_4:
         printf("Typecode: Block Error (Not Configured) (0x%4X)\n", tc);
         break;
      default:
         printf("Typecode: Unknown (0x%4X)\n", tc);
         break;
   }
}

Help Bot

X