nai rtd int ether
Edit this on GitLab
nai rtd int ether
Explanation
About the Sample Application Code
This sample application code integrates with North Atlantic Industries (NAI) embedded function modules via Ethernet. The focus of the code is on configuring and handling Interrupt Driven Response (IDR) commands for Real-Time Data (RTD) modules. Below is a detailed walkthrough and explanation of the provided code, including definitions, types of functions, and their purposes.
Included Headers The code begins by including various header files which can be categorized into common sample program includes, RTD sample program includes, and naibrd specific files.
-
naiapp_interrupt.h
-
naiapp_interrupt_ether.h
-
naiapp_boardaccess_menu.h
-
naiapp_boardaccess_query.h
-
naiapp_boardaccess_access.h
-
naiapp_boardaccess_display.h
-
naiapp_boardaccess_utils.h
-
nai_rtd_int_ether.h
-
nai_rtd_cfg.h
-
naibrd_rtd.h
-
nai_map_rtd.h
-
nai_ether_adv.h
These headers provide necessary functions, structures, and constants used for interrupt handling, Ethernet communication, and accessing board functionalities.
Function Descriptions
-
setupRTDIDRConfiguration
-
Purpose: Configures the IDR commands and sets up the board’s IDR configuration.
-
Parameters:
-
RtdConfig inputRTDConfig
: Configuration specifics for the RTD. -
IDRConfig* inputIDRConfig
: Configuration specifics for IDR. -
bool_t bGen4RtdIDRCommands
: Indicates if Gen4 RTD IDR commands should be generated. -
Actions:
-
Initializes local variables using data from
inputIDRConfig
. -
Clears existing IDR configuration on the specified card.
-
Calls
InitRTDRxIDRCommands
to initialize RTD commands. -
Sets the new IDR configuration.
-
-
InitRTDRxIDRCommands
-
Purpose: Initializes Ethernet commands that respond to RTD open interrupts.
-
Parameters:
-
RtdConfig inputRTDConfig
: Configuration details for RTD. -
IDRConfig* inputIDRConfig
: IDR configuration details. -
bool_t bGen4RtdIDRCommands
: Flag to generate Gen4 RTD IDR commands. -
Actions:
-
Retrieves module offset and address.
-
If successful, it further initializes RTD read commands if Gen4 commands are required.
-
-
MakeRTDRxReadRegsCommand
-
Purpose: Constructs Ethernet commands to read module’s open latched status register.
-
Parameters:
-
IDRConfig* inputIDRConfig
: IDR configuration details. -
uint32_t boardAddress
: Base address of the board. -
int32_t moduleOffset
: Offset for the module. -
bool_t bGen4Ether
: Indicates whether Gen4 Ethernet commands should be used. -
uint16_t startIndex
: Start index for commands array. -
Actions:
-
Constructs read register commands and updates the command count and length in
inputIDRConfig
.
-
-
MakeRTDRxWriteRegsCommand
-
Purpose: Constructs Ethernet write commands to the module’s open latched status register.
-
Parameters:
-
RtdConfig inputRTDConfig
: Configuration details for RTD. -
IDRConfig* inputIDRConfig
: IDR configuration details. -
uint32_t boardAddress
: Base address of the board. -
int32_t moduleOffset
: Offset for the module. -
bool_t bGen4Ether
: Indicates whether Gen4 Ethernet commands should be used. -
uint16_t startIndex
: Start index for commands array. -
Actions:
-
Creates and stores write commands in the IDR configuration.
-
Clears the open latched status register to re-arm interrupts.
-
-
HandleRTDEtherInterrupt
-
Purpose: Handles responses to IDR Ethernet interrupts.
-
Parameters:
-
uint16_t msglen
: Length of the message received. -
uint8_t msg[]
: The message data. -
uint16_t tdr_idr_id
: IDR ID for the TDR. -
Actions:
-
Decodes the message header to identify the type and sequence of the command.
-
Processes the message based on its type.
-
Prints interrupt occurrence information if all commands have been executed.
-
Enumerations and Definitions
- nai_status_t
: Return status type for NAI functions (e.g., NAI_SUCCESS
).
- NAI_RTD_INTERRUPT_VECTOR
: Constant representing the RTD interrupt vector.
- DEF_ETHERNET_RTD_IDR_ID
: Default Ethernet RTD IDR identifier.
- NAI_RTD_GEN5_REG_OPEN_STATUS_LATCHED_ADD
: Register address for RTD Gen5 open status latch.
Summary This sample code is responsible for setting up and handling Ethernet-based interrupt responses for RTD modules on NAI boards. By configuring IDR commands, it ensures that the board can respond to interrupts by reading and clearing the open latched status registers, thereby maintaining real-time data integrity and system responsiveness.
/* Common Sample Program include files */
#include "include/naiapp_interrupt.h"
#include "include/naiapp_interrupt_ether.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"
/* Common RTD Sample Program include files */
#include "nai_rtd_int_ether.h"
#include "nai_rtd_cfg.h"
/* naibrd include files */
#include "functions/naibrd_rtd.h"
#include "maps/nai_map_rtd.h"
#include "advanced/nai_ether_adv.h"
/**************************************************************************************************************/
/**
<summary>
Constructs the ethernet commands that are part of the IDR. Configures the IDR on the board.
</summary>
*/
/**************************************************************************************************************/
void setupRTDIDRConfiguration( RtdConfig inputRTDConfig, IDRConfig* inputIDRConfig, bool_t bGen4RtdIDRCommands )
{
int32_t cardIndex = inputIDRConfig->cardIndex;
uint16_t protocol = inputIDRConfig->protocol;
uint16_t port = inputIDRConfig->port;
uint8_t* ipAddress = inputIDRConfig->ipAddress;
uint8_t ipLength = inputIDRConfig->ipLength;
int32_t vector = NAI_RTD_INTERRUPT_VECTOR;
uint8_t *commands = inputIDRConfig->commands; /* Stores the ethernet commands that are going to be executed as part of the IDR */
uint16_t *cmdcount = &inputIDRConfig->cmdcount;
uint16_t *cmdlength = &inputIDRConfig->cmdlength;
check_status( naibrd_Ether_ClearIDRConfig( cardIndex, (uint16_t)DEF_ETHERNET_RTD_IDR_ID ) ); /* clear IDR config */
InitRTDRxIDRCommands( inputRTDConfig, inputIDRConfig, bGen4RtdIDRCommands );
check_status( naibrd_Ether_SetIDRConfig( cardIndex, (uint16_t)DEF_ETHERNET_RTD_IDR_ID, protocol, ipLength, ipAddress, port, vector, *cmdcount, *cmdlength, commands ) );
}
/**************************************************************************************************************/
/**
<summary>
This function configures the IDR (Interrupt Driven Response) commands when an RTD open interrupt occurs.
There are two Ethernet commands that will be processed by the board when an RTD open interrupt occurs.
</summary>
*/
/**************************************************************************************************************/
void InitRTDRxIDRCommands( RtdConfig inputRTDConfig, IDRConfig* inputIDRConfig, bool_t bGen4RtdIDRCommands )
{
nai_status_t status = NAI_SUCCESS;
uint16_t msgIndex = 0;
uint32_t boardAddress;
uint32_t moduleOffset;
boardAddress = 0;
status = check_status( naibrd_GetModuleOffset( inputRTDConfig.cardIndex, inputRTDConfig.module, &moduleOffset ) );
if ( status == NAI_SUCCESS )
status = check_status( naibrd_GetAddress( inputRTDConfig.cardIndex, &boardAddress ) );
if ( status == NAI_SUCCESS )
{
if (bGen4RtdIDRCommands)
{
MakeRTDRxReadRegsCommand( inputIDRConfig, boardAddress, moduleOffset, bGen4RtdIDRCommands, msgIndex );
msgIndex = inputIDRConfig->cmdlength;
}
}
}
/**************************************************************************************************************/
/**
<summary>
This function constructs an ethernet read reg command and stores it in the IDR Configuration. The read will be performed
on the module's open latched status register.
</summary>
*/
/**************************************************************************************************************/
void MakeRTDRxReadRegsCommand( IDRConfig* inputIDRConfig, uint32_t boardAddress, int32_t moduleOffset, bool_t bGen4Ether, uint16_t startIndex )
{
uint16_t msgIndexOpen = startIndex;
uint16_t seqno;
uint32_t count, stride;
uint32_t regaddrOpen;
if (bGen4Ether)
{
seqno = 0;
regaddrOpen = boardAddress + moduleOffset + NAI_RTD_GEN5_REG_OPEN_STATUS_LATCHED_ADD;
count = 1;
stride = 4;
msgIndexOpen = (uint16_t)nai_ether_MakeReadMessage( &inputIDRConfig->commands[startIndex], seqno, NAI_ETHER_GEN4, (nai_intf_t)inputIDRConfig->boardInterface, regaddrOpen, stride, count, NAI_REG32 );
inputIDRConfig->cmdlength = inputIDRConfig->cmdlength + msgIndexOpen;
command_index_interrupt_status = inputIDRConfig->cmdcount;
inputIDRConfig->cmdcount++;
}
}
/**************************************************************************************************************/
/**
<summary>
This function constructs an ethernet write reg command and stores it in the IDR Configuration. The write will be performed
on the module's open latched status register. The purpose of this write is to clear the open latched status register and
re-arm the interrupts after one has occurred.
</summary>
*/
/**************************************************************************************************************/
void MakeRTDRxWriteRegsCommand( RtdConfig inputRTDConfig, IDRConfig* inputIDRConfig, uint32_t boardAddress, int32_t moduleOffset, bool_t bGen4Ether, uint16_t startIndex )
{
uint16_t msgIndexOpen = startIndex;
uint16_t seqno;
uint32_t count, stride;
uint32_t regaddrOpen;
uint32_t dataOpen = 0x1 << ( inputRTDConfig.channel-1 );
if (bGen4Ether)
{
seqno = 0;
regaddrOpen = moduleOffset + boardAddress + NAI_RTD_GEN5_REG_OPEN_STATUS_LATCHED_ADD;
count = 1;
stride = 4;
msgIndexOpen = (uint16_t)nai_ether_BeginWriteMessage( &inputIDRConfig->commands[startIndex], seqno, NAI_ETHER_GEN4, (nai_intf_t)inputIDRConfig->boardInterface, regaddrOpen, stride, count, NAI_REG32 );
if ( msgIndexOpen >= 0 )
{
msgIndexOpen = (uint16_t)nai_ether_WriteMessageData( &inputIDRConfig->commands[startIndex], msgIndexOpen, NAI_REG32, &dataOpen, NAI_REG32, count );
if ( msgIndexOpen >= 0 )
{
msgIndexOpen = (uint16_t)nai_ether_FinishMessage( &inputIDRConfig->commands[startIndex], msgIndexOpen, NAI_ETHER_GEN4 );
}
}
inputIDRConfig->cmdlength = inputIDRConfig->cmdlength + msgIndexOpen;
inputIDRConfig->cmdcount++;
}
}
/**************************************************************************************************************/
/**
<summary>
Handles interpreting the idr response msgs received for rtd data. displays the IDR ID and status. Expects
to be parsing a message for a SINGLE channel interrupt only.
</summary>
*/
/**************************************************************************************************************/
void HandleRTDEtherInterrupt( uint16_t msglen, uint8_t msg[], uint16_t tdr_idr_id )
{
static uint8_t status;
uint16_t seq;
nai_ether_typecode_t typeCode;
nai_ether_gen_t gen = NAI_ETHER_GEN4;
int32_t size;
int32_t commandIndex;
nai_ether_DecodeMessageHeader(msg, msglen, &seq, &typeCode, gen, &size);
commandIndex = ( seq & ( 0x0F << 6 ) ) >> 6;
switch (typeCode)
{
/* Write REG */
case NAI_ETHER_TYPECODE_RSP_COMMAND_COMPLETE_WRITE_4:
break;
/* READ REG */
case NAI_ETHER_TYPECODE_RSP_COMMAND_COMPLETE_READ_4:
if ( command_index_interrupt_status == commandIndex )
status = msg[11];
break;
}
if ( commandIndex == inputIDRConfig.cmdcount - 1 ) /* Print once all commands have been executed */
{
printf( "\nInterrupt Occurred\n" );
printInterruptInformation_RTD( tdr_idr_id, status, TRUE );
printInterruptInformationToFile( tdr_idr_id, status, TRUE, fifoDataFile );
}
}