nai ds int
Edit this on GitLab
nai ds int
Explanation
About This Sample Application Code
This sample application, written in C, interacts with North Atlantic Industries' (NAI) embedded function modules using their Software Support Kit (SSK). The application primarily configures and handles interrupts related to Data Set (DS) modules. Let’s walk through each part of the code to understand its functionality:
Included Header Files
The code includes header files for various utilities, data structures, and functions provided by NAI:
- naiapp_interrupt.h
, naiapp_interrupt_ether.h
: Functions for configuring interrupts.
- naiapp_boardaccess_menu.h
, naiapp_boardaccess_query.h
, naiapp_boardaccess_access.h
, naiapp_boardaccess_display.h
, naiapp_boardaccess_utils.h
: Utilities for accessing and querying board information.
- nai_ds_int.h
, nai_ds_cfg.h
: Data structures and configurations specific to Data Set (DS) modules.
- nai.h
, naibrd.h
, naibrd_ether.h
: Core functions and structures for interacting with NAI boards.
- naibrd_ds.h
, nai_map_ds.h
: Functions and mappings specific to DS modules.
- stdlib.h
: Standard library functions.
Functions Overview
configureDSToInterrupt
This function configures an interrupt to occur when a message is received on any of the DS channels.
-
Clears the existing interrupt status.
-
Sets up the interrupt vectors.
-
Configures the interrupt triggers for each channel.
-
Enables the necessary steering for the interrupts.
enableDSInterrupts
This function enables or disables DS channel interrupts within a specified range.
-
Iterates over the defined channel range.
-
Enables/Disables the status, power, and interrupts for each channel.
-
Introduces a delay to ensure outputs are activated.
basic_ISR_DS
An Interrupt Service Routine (ISR) for handling interrupts.
-
Sets a flag indicating an interrupt has occurred.
-
Clears the interrupt on the board (platform specific).
checkForDSInterrupt
Prompts the user to check if an interrupt has occurred and processes the interrupt status.
-
Continuously checks for user input to either check the interrupt status or quit.
-
Displays interrupt information if an interrupt has occurred.
-
Offers to clear the status register based on user input.
GetDSLatchStatusTriggerMode
Prompts the user to input the trigger mode for the latched status register (Edge Triggered or Level Triggered).
-
Reads user input and validates it, setting the interrupt mode accordingly.
handleDSInterrupt
Handles the processing of an interrupt when it occurs.
-
Prints interrupt occurrence.
-
Clears the interrupt status and displays relevant information.
promptUserToClearInterrupt_DS
Prompts the user to clear the received interrupt.
-
Waits for the user to provide a clear interrupt command.
DisplayMessage_DSInterrupt
Displays messages related to DS interrupts based on a message ID.
-
Provides user guidance and information regarding interrupt processing.
printInterruptInformation_DS
Prints detailed information regarding the interrupt for each channel set in the status register.
-
Displays interrupt ID, status, and other relevant data.
Summary
This application demonstrates how to set up, enable/disable, and handle interrupts for NAI’s DS modules using their SSK. It includes utilities for user interaction and ensures the system properly processes and responds to interrupts. Each function targets specific tasks such as configuration, status checking, user prompting, and detailed status reporting, ensuring a comprehensive management of DS interrupts.
/* 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 DS Sample Program include files */
#include "nai_ds_int.h"
#include "nai_ds_cfg.h"
/* naibrd include files */
#include "nai.h"
#include "naibrd.h"
#include "naibrd_ether.h"
#include "functions/naibrd_ds.h"
#include "maps/nai_map_ds.h"
#include <stdlib.h>
/**************************************************************************************************************/
/**
<summary>
This function configures an interrupt to occur when a message is received on any of the Ds channels.
</summary>
*/
/**************************************************************************************************************/
void configureDSToInterrupt(InterruptConfig inputInterruptConfig,DsConfig inputDSConfig)
{
int32_t cardIndex = inputDSConfig.cardIndex;
int32_t module = inputDSConfig.module;
int32_t interrupt_Edge_Trigger = inputInterruptConfig.interrupt_Edge_Trigger;
int32_t steering = inputInterruptConfig.steering;
uint32_t rawstatus = 0;
int32_t chan;
enableDSInterrupts(inputDSConfig,FALSE);
/* Clear the Interrupt Status (Read the status and write back "1" to statuses which are set to clear the status) */
check_status(naibrd_DS_GetStatusRaw(cardIndex, module, NAI_DS_STATUS_LATCH_BIT_LOST, &rawstatus));
check_status(naibrd_DS_ClearStatusRaw(cardIndex,module, NAI_DS_STATUS_LATCH_BIT_LOST, rawstatus));
check_status(naibrd_DS_GetStatusRaw(cardIndex, module, NAI_DS_STATUS_LATCH_REFERENCE_LOST, &rawstatus));
check_status(naibrd_DS_ClearStatusRaw(cardIndex, module, NAI_DS_STATUS_LATCH_REFERENCE_LOST, rawstatus));
/* Setup the Interrupt Vector - map to the same vector */
check_status(naibrd_DS_SetInterruptVector(cardIndex, module, NAI_DS_INTERRUPT_BIT_LOST, NAI_DS_BITLOSS_INTERRUPT_VECTOR));
check_status(naibrd_DS_SetInterruptVector(cardIndex, module, NAI_DS_INTERRUPT_REFERENCE_LOST, NAI_DS_REFLOSS_INTERRUPT_VECTOR));
/* Setup the Latched Status Mode */
for (chan = 1; chan <= inputDSConfig.maxChannel; chan++)
{
check_status(naibrd_DS_SetEdgeLevelInterrupt(cardIndex, module, chan, NAI_DS_INTERRUPT_BIT_LOST,(nai_ds_interruptMode_type_t)interrupt_Edge_Trigger));
check_status(naibrd_DS_SetEdgeLevelInterrupt(cardIndex, module, chan, NAI_DS_INTERRUPT_REFERENCE_LOST,(nai_ds_interruptMode_type_t)interrupt_Edge_Trigger));
}
check_status(naibrd_DS_SetInterruptSteering(cardIndex, module, NAI_DS_STATUS_LATCH_BIT_LOST, steering));
check_status(naibrd_DS_SetInterruptSteering(cardIndex, module, NAI_DS_STATUS_LATCH_REFERENCE_LOST, steering));
}
/**************************************************************************************************************/
/**
<summary>
Enables the channels within the (minChannel,maxChannel) range to interrupt
if enable is true and disables them otherwise.
</summary>
*/
/**************************************************************************************************************/
void enableDSInterrupts(DsConfig inputDSConfig,bool_t enable){
int32_t channel;
for(channel = inputDSConfig.minChannel;channel <= inputDSConfig.maxChannel;channel++)
{
check_status(naibrd_DS_SetChanStatusEnable(inputDSConfig.cardIndex, inputDSConfig.module, channel, 0x1));
check_status(naibrd_DS_SetPowerEnable(inputDSConfig.cardIndex, inputDSConfig.module, channel, 0x1));
check_status(naibrd_DS_SetInterruptEnable(inputDSConfig.cardIndex, inputDSConfig.module, channel, NAI_DS_INTERRUPT_BIT_LOST, enable));
check_status(naibrd_DS_SetInterruptEnable(inputDSConfig.cardIndex, inputDSConfig.module, channel, NAI_DS_INTERRUPT_REFERENCE_LOST, enable));
}
naibrd_Wait(1000000); /*Delay to allow time for output to come on*/
}
/**************************************************************************************************************/
/**
<summary>
The routine is called to handle a interrupt. This function alerts checkForInterrupt that an interrupt has occurred.
In vxWorks you must clear the Interrupt on the board within the ISR.
</summary>
*/
/**************************************************************************************************************/
#if defined (__VXWORKS__)
void basic_ISR_DS(uint32_t param)
#else
void basic_ISR_DS(void *param, uint32_t vector)
#endif
{
#if defined (WIN32)
UNREFERENCED_PARAMETER(param);
#endif
interruptOccured = TRUE;
#if defined (__VXWORKS__)
interruptVector = nai_Onboard_GetInterruptVector();
nai_Onboard_ClearInterrupt();
#else
interruptVector = vector;
#endif
}
/**************************************************************************************************************/
/**
<summary>
Prompts user to check if an interrupt has occurred. MyIsr() should be installed if using this function.
</summary>
*/
/**************************************************************************************************************/
bool_t checkForDSInterrupt(DsConfig inputDSConfig){
bool_t bQuit;
int32_t cardIndex;
int32_t module;
uint32_t rawstatus = 0;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
cardIndex = inputDSConfig.cardIndex;
module = inputDSConfig.module;
bQuit = FALSE;
interruptOccured = FALSE;
while(!bQuit){
printf("\nPress enter to check if REF LOSS interrupt Occurred (press Q to quit):");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if(!bQuit){
if(interruptOccured){
check_status(naibrd_DS_GetStatusRaw(cardIndex, module, NAI_DS_STATUS_LATCH_REFERENCE_LOST, &rawstatus));
printf("\nVector = %#x \n",interruptVector);
printf("Status = %#x \n",rawstatus);
interruptOccured = FALSE;
}
else{
printf("\nNo Interrupt Occurred");
}
printf("\n\nWould you like to clear the status register? (default:N):");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if(inputBuffer[0] == 'y' || inputBuffer[0] =='Y')
{
check_status(naibrd_DS_ClearStatusRaw(cardIndex,module, NAI_DS_STATUS_LATCH_REFERENCE_LOST, rawstatus));
}
}
}
return bQuit;
}
/**************************************************************************************************************/
/**
<summary>
GetDSLatchStatusTriggerMode handles prompting the user for the trigger mode for the latched status register
(Edge Triggered or Level Triggered).
</summary>
*/
/**************************************************************************************************************/
bool_t GetDSLatchStatusTriggerMode(int32_t* interrupt_Edge_Trigger)
{
bool_t bQuit;
uint32_t temp;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
printf("\nEnter Latched Status Trigger Mode (Edge=0, Level=1) (Default=0): ");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if(!bQuit)
{
if(inputResponseCnt > 0)
{
temp = (int32_t)atol((const char*)inputBuffer);
if( temp == 0 || temp == 1)
{
*interrupt_Edge_Trigger = temp;
}
else
{
printf("ERROR: Invalid Interrupt Trigger Mode.\n");
}
}
else
*interrupt_Edge_Trigger = 0;
}
return(bQuit);
}
/**************************************************************************************************************/
/**
<summary>
This routine takes in the vector of the DS RX interrupt that has just occurred. And gets the status, fifo status,
and fifo data of all channels that interrupted, and prints it.
</summary>
*/
/**************************************************************************************************************/
void handleDSInterrupt(uint32_t nVector){
uint32_t rawstatus=0;
printf("\n\nInterrupt Occurred \n\n");
if (inputInterruptConfig.bPromptForInterruptClear)
{
promptUserToClearInterrupt_DS();
}
check_status(naibrd_DS_GetStatusRaw(inputDSConfig.cardIndex, inputDSConfig.module, NAI_DS_STATUS_LATCH_REFERENCE_LOST, &rawstatus));
check_status(naibrd_DS_ClearStatusRaw(inputDSConfig.cardIndex,inputDSConfig.module, NAI_DS_STATUS_LATCH_REFERENCE_LOST, rawstatus));
printInterruptInformation_DS(nVector,rawstatus,FALSE);
}
void promptUserToClearInterrupt_DS()
{
/* Prompt the user to clear the interrupt received */
SetUserRequestClearInt(FALSE);
printf("\n");
DisplayMessage_DSInterrupt(MSG_USER_CLEAR_DS_INT);
/* Wait for the user to respond */
while (!GetUserRequestClearInt())
{
nai_msDelay(10);
}
}
/**************************************************************************************************************/
/**
<summary>
DisplayMessage_DSInterrupt handles displaying the messages associated with the msgId passed in.
</summary>
*/
/**************************************************************************************************************/
void DisplayMessage_DSInterrupt(int32_t msgId)
{
switch (msgId)
{
case (int32_t)MSG_BANNER_DS_INT:
{
printf("\n********************************************************************************");
printf("\n****** DS INTERRUPT ******");
printf("\nAn interrupt will occur when the DS Module receives a REF Loss transition status ");
printf("\n********************************************************************************");
}
break;
case (int32_t)MSG_USER_TRIGGER_DS_INT:
{
printf("\nPress \"Q\" to quit the application.\nPlease trigger DS Reference Loss status interrupt (Recv Msg):");
}
break;
case (int32_t)MSG_USER_CLEAR_DS_INT:
{
printf("Press \"C\" to clear interrupts... ");
}
break;
}
}
/**************************************************************************************************************/
/**
<summary>
Function will print the Data provided for each channel that has been set in the status register.
It will also print the status and idr id or vector.
</summary>
*/
/**************************************************************************************************************/
void printInterruptInformation_DS(int32_t interruptID,uint32_t status,bool_t isEther)
{
printf("Interrupt Information\n");
printf("-----------------------------------\n");
if (isEther)
printf("\nIDR ID = %#x \n",interruptID);
else
printf("\nVector = %#x \n",interruptID);
printf("Status = %#x \n",status);
}