DSW PullUp
Edit this on GitLab
DSW PullUp Sample Application (SSK 1.x)
Overview
The DSW PullUp sample application demonstrates how to configure internal pull-up resistors and monitor channel status on discrete switch (DSW) channels using the NAI Software Support Kit (SSK 1.x). It covers the DSW operations most relevant to pull-up resistor management: enabling and disabling the internal pull-up source, setting switch output states, reading real-time voltage and current measurements, and monitoring channel status flags.
Pull-up resistors bias a floating input to a known voltage level. Without a pull-up, a disconnected or open input floats at an indeterminate voltage, which can cause erratic threshold crossings and false status readings. When you enable the internal pull-up on a DSW channel, the module connects an on-board resistor between the channel input and the internal pull-up voltage source. This serves two purposes: it holds disconnected inputs at a predictable level so your application can distinguish a genuinely open circuit from a low-voltage signal, and it enables reliable open-detect — if the channel voltage reads at the pull-up level with no external load, you know the sensor or wire is disconnected.
This sample supports the following DSW module types: K7 (Gen3), DT2, and DT5 (Gen5). Not all DSW modules support the pull-up feature — the sample checks for NAI_ERROR_NOT_SUPPORTED at runtime and reports accordingly. It serves as a practical API reference for pull-up configuration that you can lift into your own code.
For the full set of DSW configuration operations (threshold programming, all status flags, voltage/current measurements), see the DSW BasicOps sample application guide.
Prerequisites
Before running this sample, make sure you have:
-
An NAI board with a DSW module installed (K7, DT2, or DT5).
-
SSK 1.x installed on your development host.
-
The sample applications built. Refer to the SSK 1.x build instructions for your platform if you have not already compiled them.
How to Run
Launch the DSW_PullUp executable from your build output directory. On startup the application looks for a configuration file (default_DSW_BasicOp.txt). On the first run, this file will not exist — the application will present an interactive board menu where you configure a board connection, card index, and module slot. You can save this configuration so that subsequent runs skip the menu and connect automatically. Once connected, select a channel and the command menu lets you exercise pull-up and switch state operations.
Board Connection and Module Selection
|
Note
|
This startup sequence is common to all NAI sample applications. The board connection and module selection code shown here is not specific to DSW. For details on board connection configuration, see the First Time Setup Guide. |
The main() function follows a standard SSK 1.x startup flow:
-
Call
naiapp_RunBoardMenu()to load a saved configuration file (if one exists) or present the interactive board menu. The configuration file (default_DSW_BasicOp.txt) is not included with the SSK — it is created when the user saves their connection settings from the board menu. On the first run, the menu will always appear. -
Query the user for a card index with
naiapp_query_CardIndex(). -
Query for a module slot with
naiapp_query_ModuleNumber(). -
Retrieve the module ID with
naibrd_GetModuleID()so downstream code can adapt to the specific DSW variant installed.
#if defined (__VXWORKS__)
int32_t DSW_PullUp(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)
{
stop = naiapp_query_CardIndex(naiapp_GetBoardCnt(), 0, &cardIndex);
if (stop != TRUE)
{
check_status(naibrd_GetModuleCount(cardIndex, &moduleCnt));
stop = naiapp_query_ModuleNumber(moduleCnt, 1, &module);
if (stop != TRUE)
{
moduleID = naibrd_GetModuleID(cardIndex, module);
if ((moduleID != 0))
{
Run_DSW_PullUp(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);
}
}
naiapp_access_CloseAllOpenCards();
return 0;
}
|
Important
|
Common connection errors you may encounter at this stage:
|
Program Structure
Entry Point
On standard platforms the entry point is main(). On VxWorks the entry point is DSW_PullUp() — the SSK 1.x build system selects the correct variant via a preprocessor guard:
#if defined (__VXWORKS__)
int32_t DSW_PullUp(void)
#else
int32_t main(void)
#endif
The startup flow is the same in both cases:
-
Attempt to load the saved configuration file via
naiapp_RunBoardMenu(CONFIG_FILE). If the file does not yet exist, the interactive board menu is presented instead. -
Enter a loop that queries for card index and module slot.
-
Call
Run_DSW_PullUp()to validate the module and enter the interactive command loop. -
On exit, close all open board connections with
naiapp_access_CloseAllOpenCards().
Module Validation
Run_DSW_PullUp() validates the selected module by calling naibrd_DSW_GetChannelCount(). If the module is not a recognized DSW type, the channel count returns zero and the function prints an error. In your own application, use this same check to confirm the target slot contains a DSW module before issuing DSW API calls:
MaxChannel = naibrd_DSW_GetChannelCount(ModuleID);
if (MaxChannel == 0)
{
printf(" *** Module selection not recognized as DSW module. ***\n\n");
}
else
{
Cfg_DSW_Channel(cardIndex, module, ModuleID, MaxChannel);
}
Application Parameters
The Cfg_DSW_Channel() function populates an naiapp_AppParameters_t struct that is passed to every command handler. Your application will need to track these same values to identify which board, module, and channel you are targeting:
naiapp_AppParameters_t dsw_params;
p_naiapp_AppParameters_t dsw_pullup_params = &dsw_params;
dsw_pullup_params->cardIndex = cardIndex;
dsw_pullup_params->module = module;
dsw_pullup_params->modId = ModuleID;
dsw_pullup_params->maxChannels = MaxChannel;
-
cardIndex— identifies which board in a multi-board system. -
module— the slot number where the DSW module is installed. -
modId— the module identifier returned bynaibrd_GetModuleID(). API functions use this to apply module-specific behavior. -
maxChannels— the total number of channels on the module, used to validate channel selection.
Command Loop
Cfg_DSW_Channel() drives the interactive command loop. On each iteration it displays the current channel configuration (including pull-up state, voltage, and current), prints the command menu, and dispatches the user’s selection to the matching handler function.
The available commands are registered in the DSW_PullUpMenuCmds[] table:
| Command | Description |
|---|---|
Switch |
Set the switch output state (open or closed) |
PU |
Enable or disable the internal pull-up resistor |
STAT |
Display all channel status flags |
CLR |
Clear all latched channel status flags |
The menu-driven structure is a convenience of the sample application. In your own application, you would call the same underlying naibrd_DSW_*() API functions directly — for example, calling naibrd_DSW_SetPullUpSourceEnable() instead of navigating to the "PU" menu command.
Pull-Up Resistor Configuration
The pull-up feature is the central focus of this sample. Internal pull-up resistors connect between the channel input pin and an on-board voltage source. When enabled, the pull-up biases floating inputs to a known voltage level. This is essential for two common use cases:
-
Open-detect for disconnected sensors — if a sensor wire breaks or a connector comes loose, the pull-up holds the input at the pull-up voltage. Your application can detect this known voltage and flag the channel as disconnected, rather than interpreting a random floating voltage as a valid signal.
-
Biasing floating inputs — in systems where the external signal source may be temporarily disconnected or tri-stated, the pull-up ensures the input settles to a predictable level instead of drifting between thresholds and generating spurious transition events.
Check Pull-Up Support
Not all DSW modules implement internal pull-up resistors. Before attempting to enable the pull-up, your application should check whether the feature is supported on the installed module. The sample does this by calling naibrd_DSW_GetPullUpSourceEnable() and checking the return code:
if (check_status(naibrd_DSW_GetPullUpSourceEnable(cardIndex, module, chan, &pullupEn))
== NAI_ERROR_NOT_SUPPORTED)
{
printf("ERROR: Not Supported\n");
return bQuit;
}
If naibrd_DSW_GetPullUpSourceEnable() returns NAI_ERROR_NOT_SUPPORTED, the module does not have internal pull-up circuitry. Your application should handle this gracefully — skip the pull-up configuration and rely on external pull-up resistors if needed.
Enable or Disable Pull-Up
To enable or disable the internal pull-up resistor on a channel, call naibrd_DSW_SetPullUpSourceEnable(). Pass NAI_DSW_STATE_HI to enable the pull-up or NAI_DSW_STATE_LO to disable it:
nai_dsw_state_t pullupEn = NAI_DSW_STATE_HI; /* enable pull-up */
check_status(naibrd_DSW_SetPullUpSourceEnable(cardIndex, module, chan, pullupEn));
-
cardIndex— identifies the board. -
module— the slot containing the DSW module. -
chan— the channel to configure. -
pullupEn—NAI_DSW_STATE_HI(enable) orNAI_DSW_STATE_LO(disable).
To read back the current pull-up state, use naibrd_DSW_GetPullUpSourceEnable():
bool_t pullupEn = 0;
check_status(naibrd_DSW_GetPullUpSourceEnable(cardIndex, module, chan, &pullupEn));
The display function in the sample shows the pull-up state as "Enabled", "Disabled", or "Not Supported" depending on the read-back value.
|
Important
|
Common Errors
|
Switch State Configuration
The switch state controls the physical relay or solid-state switch on the DSW channel. Setting the state to closed establishes a current path (NO-COM contact closure), while open breaks the circuit.
Set Switch State
To control the switch output on a channel in your own application, call naibrd_DSW_SetSwitchState() with the desired state value. Pass NAI_DSW_STATE_LO (0) for open or NAI_DSW_STATE_HI (1) for closed:
nai_dsw_state_t switchstate = NAI_DSW_STATE_HI; /* closed */
check_status(naibrd_DSW_SetSwitchState(cardIndex, module, chan, switchstate));
-
cardIndex— identifies the board. -
module— the slot containing the DSW module. -
chan— the channel to control. -
switchstate—NAI_DSW_STATE_LO(open) orNAI_DSW_STATE_HI(closed).
|
Important
|
Common Errors
|
Channel Configuration Display
The sample’s Display_DSW_ChannelCfg() function reads and displays a comprehensive snapshot of the selected channel’s state, including the switch state, pull-up enable status, measured voltage and current, and RMS averages. This is a useful diagnostic pattern for your own application.
Read Voltage and Current
To read the real-time voltage and current measurements on a channel, use naibrd_DSW_GetVoltage() and naibrd_DSW_GetCurrent(). For averaged (RMS) readings, use naibrd_DSW_GetAvgVoltage() and naibrd_DSW_GetAvgCurrent():
float64_t voltage = 0.0, RMSvoltage = 0.0;
float64_t current = 0.0, RMScurrent = 0.0;
check_status(naibrd_DSW_GetVoltage(cardIndex, module, chan, &voltage));
check_status(naibrd_DSW_GetCurrent(cardIndex, module, chan, ¤t));
check_status(naibrd_DSW_GetAvgVoltage(cardIndex, module, chan, &RMSvoltage));
check_status(naibrd_DSW_GetAvgCurrent(cardIndex, module, chan, &RMScurrent));
The current values are returned in amps. The sample converts to milliamps for display by multiplying by 1000.
You can also read the voltage LSB resolution for the module with naibrd_DSW_GetVoltageLSB():
float64_t voltageLSB = 0.0;
check_status(naibrd_DSW_GetVoltageLSB(cardIndex, module, &voltageLSB));
This tells you the smallest voltage step the module’s ADC can resolve. Consult your module’s manual for full measurement specifications.
Read Switch and Input State
To read the current state of a channel, use two complementary calls. naibrd_DSW_GetSwitchState() returns the commanded output state (what you set), while naibrd_DSW_GetInputState() returns the actual sensed input state (what the hardware detects):
nai_dsw_state_t switchstate = 0;
nai_dsw_state_t inputstate = 0;
check_status(naibrd_DSW_GetSwitchState(cardIndex, module, chan, &switchstate));
check_status(naibrd_DSW_GetInputState(cardIndex, module, chan, &inputstate));
A mismatch between the commanded switch state and the sensed input state can indicate a wiring fault, overcurrent condition, or external load issue.
|
Important
|
Common Errors
|
Channel Status Monitoring
The sample provides commands to display and clear the latched status flags for a channel. These flags capture threshold crossings, transitions, BIT failures, and overcurrent events.
Display Status
To read a specific status flag for a channel, call naibrd_DSW_GetStatus() with the appropriate status type enum. The sample reads all seven latched status types:
nai_status_bit_t status;
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_MIN_LO_LATCHED, &status));
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_MID_RANGE_LATCHED, &status));
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_MAX_HI_LATCHED, &status));
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_LO_HI_TRANS_LATCHED, &status));
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_HI_LO_TRANS_LATCHED, &status));
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_BIT_LATCHED, &status));
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_OVERCURRENT_LATCHED, &status));
Each call returns a nai_status_bit_t value — nonzero means the condition has been detected since the last clear. The latched variants retain their state until explicitly cleared, so you will not miss transient events.
The status types are:
-
MIN_LO — voltage dropped below the minimum low threshold.
-
MID_RANGE — voltage is in the mid-range zone between the lower and upper thresholds.
-
MAX_HI — voltage exceeded the maximum high threshold.
-
LO_HI_TRANS — a low-to-high transition was detected.
-
HI_LO_TRANS — a high-to-low transition was detected.
-
BIT — built-in test failure detected on the channel.
-
OVERCURRENT — the channel drew more current than the overcurrent limit.
Clear Status
To clear all latched status flags for a channel, call naibrd_DSW_ClearStatus() for each status type. The sample clears all seven flags in sequence:
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_MIN_LO_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_MID_RANGE_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_MAX_HI_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_LO_HI_TRANS_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_HI_LO_TRANS_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_BIT_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_OVERCURRENT_LATCHED));
After clearing, the sample calls Display_DSW_Status() to confirm that all flags have been reset to zero. In your own application, clear status flags after processing them to ensure you catch the next occurrence.
|
Important
|
Common Errors
|
Troubleshooting Reference
The following table summarizes common errors and symptoms you may encounter when working with DSW pull-up configuration. This consolidates the error information from the preceding sections. Consult your module’s manual for hardware-specific diagnostics and specifications.
| Error / Symptom | Possible Causes | Suggested Resolution |
|---|---|---|
|
The installed DSW module does not have internal pull-up circuitry. |
Use external pull-up resistors if input biasing is required. Check your module’s manual for pull-up feature availability. |
Pull-up enabled but input voltage stays near zero |
External load pulling the line down; pull-up source not configured; short circuit on the channel. |
Disconnect external load and verify voltage rises to pull-up level. Check module manual for pull-up source voltage configuration. |
Switch state reads "UNK" |
Communication error or uninitialized module. |
Verify board connection, module slot, and that the module has been properly detected during startup. |
No voltage change after setting switch state |
External wiring disconnected; channel in overcurrent fault. |
Check physical wiring and read the overcurrent status flag. Clear the fault and retry. |
Status flags re-latch immediately after clearing |
The underlying fault condition is still active. |
Resolve the root cause (reduce load, fix wiring, correct threshold settings) before clearing flags. |
Voltage and current readings seem wrong |
Current is in amps, not milliamps; voltage LSB resolution may be coarser than expected. |
Multiply current by 1000 for milliamp display. Use |
|
The selected slot does not contain a DSW module (K7, DT2, or DT5). |
Use the board menu to verify which modules are installed and select the correct slot. |
BIT status set on a channel with no external signal |
Some BIT tests require specific input conditions or configurations to pass. |
Consult your module’s manual for BIT test requirements and expected operating conditions. |
Full Source
Full Source — DSW_PullUp.c (SSK 1.x)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.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 "nai.h"
#include "naibrd.h"
#include "functions/naibrd_dsw.h"
#include "advanced/nai_ether_adv.h"
static const int8_t *CONFIG_FILE = (int8_t *)"default_DSW_BasicOp.txt";
/* Function prototypes */
int32_t Run_DSW_PullUp(int32_t cardIndex, int32_t module, int32_t ModuleID);
static void Cfg_DSW_Channel(int32_t cardIndex, int32_t module, uint32_t ModuleID, int32_t MaxChannel);
static void Display_DSW_ChannelCfg(int32_t cardIndex, int32_t module, int32_t chan, uint32_t ModuleID);
static nai_status_t Display_DSW_Status(int32_t paramCount, int32_t* p_params);
static nai_status_t Clear_DSW_Status(int32_t paramCount, int32_t* p_params);
static nai_status_t Configure_DSW_SwitchState(int32_t paramCount, int32_t* p_params);
static nai_status_t Configure_DSW_PullUpEnable(int32_t paramCount, int32_t* p_params);
static const int32_t DEF_DSW_CHANNEL = 1;
/****** Command Table *******/
enum dsw_pullup_commands
{
DSW_PULLUP_CMD_SWITCHSTATE,
DSW_PULLUP_CMD_PULLUP_ENABLE,
DSW_PULLUP_CMD_STATUS,
DSW_BASICOP_CMD_CLEAR_STAT,
DSW_PULLUP_CMD_COUNT
};
/****** Command Tables *******/
naiapp_cmdtbl_params_t DSW_PullUpMenuCmds[] = {
{"Switch", "DSW Set Switch State", DSW_PULLUP_CMD_SWITCHSTATE, Configure_DSW_SwitchState},
{"PU", "DSW Set Pull Up enable", DSW_PULLUP_CMD_PULLUP_ENABLE, Configure_DSW_PullUpEnable},
{"STAT", "DSW Display Status", DSW_PULLUP_CMD_STATUS, Display_DSW_Status},
{"CLR", "DSW Clear Status", DSW_BASICOP_CMD_CLEAR_STAT, Clear_DSW_Status}
};
/**************************************************************************************************************/
/**
<summary>
The purpose of the DSW_PullUp is to illustrate the methods to call in the naibrd library to perform basic
operations with the discrete switch modules for configuration setup, controlling the switch closure state, and reading
the channels.
The following system configuration routines from the nai_sys_cfg.c file are called to assist with the configuration
setup for this program prior to calling the naibrd DT routines.
- ClearDeviceCfg
- QuerySystemCfg
- DisplayDeviceCfg
- GetBoardSNModCfg
- SaveDeviceCfg
</summary>
*/
/**************************************************************************************************************/
#if defined (__VXWORKS__)
int32_t DSW_PullUp(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_DSW_PullUp(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;
}
/**************************************************************************************************************/
/**
<summary>
Run_DSW_PullUp prompts the user for the card, module and channel to use for the application and calls
Cfg_DSW_Channel if the card, module, channel is valid for as a discrete switch module.
</summary>
*/
/**************************************************************************************************************/
int32_t Run_DSW_PullUp(int32_t cardIndex, int32_t module, int32_t ModuleID)
{
int32_t MaxChannel;
MaxChannel = naibrd_DSW_GetChannelCount(ModuleID);
if (MaxChannel == 0)
{
printf(" *** Module selection not recognized as DSW module. ***\n\n");
}
else
{
Cfg_DSW_Channel(cardIndex, module, ModuleID, MaxChannel);
}
return cardIndex;
}
/**************************************************************************************************************/
/**
<summary>
Cfg_DSW_Channel handles calling the Display_DSW_ChannelCfg routine to display the discrete channel configuration
and calling the routines associated with the user's menu commands.
</summary>
*/
/**************************************************************************************************************/
static void Cfg_DSW_Channel(int32_t cardIndex, int32_t module, uint32_t ModuleID, int32_t MaxChannel)
{
bool_t bQuit = FALSE;
bool_t bContinue = TRUE;
bool_t bCmdFound = FALSE;
int32_t chan, defaultchan = 1;
int32_t cmd;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
naiapp_AppParameters_t dsw_params;
p_naiapp_AppParameters_t dsw_pullup_params = &dsw_params;
dsw_pullup_params->cardIndex = cardIndex;
dsw_pullup_params->module = module;
dsw_pullup_params->modId = ModuleID;
dsw_pullup_params->maxChannels = MaxChannel;
while (bContinue)
{
printf(" \r\n\r\n");
printf("Channel selection \r\n");
printf("================= \r\n");
defaultchan = DEF_DSW_CHANNEL;
bQuit = naiapp_query_ChannelNumber(MaxChannel, defaultchan, &chan);
dsw_pullup_params->channel = chan;
naiapp_utils_LoadParamMenuCommands(DSW_PULLUP_CMD_COUNT, DSW_PullUpMenuCmds);
while (bContinue)
{
Display_DSW_ChannelCfg(cardIndex, module, chan, ModuleID);
naiapp_display_ParamMenuCommands((int8_t *)"DSW Pull Up Menu");
printf("\nType DSW command or %c to return : ", NAI_QUIT_CHAR);
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt > 0)
{
bCmdFound = naiapp_utils_GetParamMenuCmdNum(inputResponseCnt, inputBuffer, &cmd);
if (bCmdFound)
{
switch (cmd)
{
case DSW_PULLUP_CMD_SWITCHSTATE:
case DSW_PULLUP_CMD_STATUS:
case DSW_PULLUP_CMD_PULLUP_ENABLE:
case DSW_BASICOP_CMD_CLEAR_STAT:
DSW_PullUpMenuCmds[cmd].func(APP_PARAM_COUNT, (int32_t*)dsw_pullup_params);
break;
default:
printf("Invalid command entered\n");
break;
}
}
else
printf("Invalid command entered\n");
}
}
else
bContinue = FALSE;
}
}
}
/**************************************************************************************************************/
/**
<summary>
Display_DSW_ChannelCfg illustrate the methods to call in the naibrd library to retrieve the configuration states
for basic operation.
</summary>
*/
/**************************************************************************************************************/
static void Display_DSW_ChannelCfg(int32_t cardIndex, int32_t module, int32_t chan, uint32_t ModuleID)
{
nai_dsw_state_t switchstate = 0;
nai_dsw_state_t inputstate = 0;
bool_t pullupEn=0;
float64_t voltageLSB = 0.0;
float64_t voltage = 0.0, RMSvoltage = 0.0, current = 0.0, RMScurrent = 0.0;
uint32_t ModuleVer;
uint32_t ModuleRev;
uint32_t ModInfo_Special;
naibrd_GetModuleInfo(cardIndex, module, &ModuleID, &ModuleVer, &ModuleRev, &ModInfo_Special);
check_status(naibrd_DSW_GetSwitchState(cardIndex, module, chan, &switchstate));
check_status(naibrd_DSW_GetInputState(cardIndex, module, chan, &inputstate));
check_status(naibrd_DSW_GetVoltageLSB(cardIndex, module, &voltageLSB));
if(check_status(naibrd_DSW_GetPullUpSourceEnable(cardIndex, module,chan, &pullupEn))==NAI_ERROR_NOT_SUPPORTED)
{
pullupEn=NAI_DSW_STATE_NOT_SUPPORTED;
}
/*read channel voltage and current*/
check_status(naibrd_DSW_GetVoltage(cardIndex, module, chan, &voltage));
check_status(naibrd_DSW_GetCurrent(cardIndex, module, chan, ¤t));
check_status(naibrd_DSW_GetAvgVoltage(cardIndex, module, chan, &RMSvoltage));
check_status(naibrd_DSW_GetAvgCurrent(cardIndex, module, chan, &RMScurrent));
printf("\n === Channel %d ===\n\n", chan);
printf(" Switch Pull-Up =======Meas.====== ====RMS=====\n");
printf(" State Enable V mA V mA\n");
printf(" ------ --------- ------ ------- --- -----\n");
switch (switchstate)
{
case NAI_DSW_STATE_LO:
printf(" Open ");
break;
case NAI_DSW_STATE_HI:
printf(" Closed");
break;
/* undefined value read back */
default:
printf(" UNK ");
break;
}
switch(pullupEn)
{
case NAI_DSW_STATE_LO:
printf(" Disabled ");
break;
case NAI_DSW_STATE_HI:
printf(" Enabled ");
break;
case NAI_DSW_STATE_NOT_SUPPORTED:
printf(" Not Supported ");
break;
/* undefined value read back */
default:
printf(" UNK ");
break;
}
printf("%+8.3f ", voltage);
printf("%+8.3f ", current*1000); /*display in mA units*/
printf("%+8.3f ", RMSvoltage);
printf("%+8.3f ", RMScurrent*1000); /*display in mA units*/
}
/**************************************************************************************************************/
/**
<summary>
Display_DSW_Status illustrate the methods to call in the naibrd library to retrieve the status states.
</summary>
*/
/**************************************************************************************************************/
static nai_status_t Display_DSW_Status(int32_t paramCount, int32_t* p_params)
{
nai_status_bit_t status;
p_naiapp_AppParameters_t p_dsw_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dsw_params->cardIndex;
int32_t module = p_dsw_params->module;
int32_t chan = p_dsw_params->channel;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
/* Available status:
NAI_DSW_STATUS_BIT,
NAI_DSW_STATUS_OVERCURRENT,
NAI_DSW_STATUS_MAX_HI,
NAI_DSW_STATUS_MIN_LO,
NAI_DSW_STATUS_MID_RANGE,
NAI_DSW_STATUS_LO_HI_TRANS,
NAI_DSW_STATUS_HI_LO_TRANS,
*/
printf("\n");
printf(" ----------------- Status ----------------------------\n");
printf(" MinLo MidRng MaxHi Low-Hi Hi-Lo BIT OC\n");
printf(" ------- -------- ------ ------- -------- ------ ------\n");
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_MIN_LO_LATCHED, &status));
printf(" %3i ", status);
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_MID_RANGE_LATCHED, &status));
printf(" %3i ", status);
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_MAX_HI_LATCHED, &status));
printf(" %3i ", status);
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_LO_HI_TRANS_LATCHED, &status));
printf(" %3i ", status);
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_HI_LO_TRANS_LATCHED, &status));
printf(" %3i ", status);
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_BIT_LATCHED, &status));
printf(" %3i ", status);
check_status(naibrd_DSW_GetStatus(cardIndex, module, chan, NAI_DSW_STATUS_OVERCURRENT_LATCHED, &status));
printf(" %3i ", status);
printf("\n\n");
return NAI_ERROR_UNKNOWN;
}
/**************************************************************************************************************/
/**
<summary>
Clear_DSW_Status illustrate the methods to call in the naibrd library to clear the status states.
</summary>
*/
/**************************************************************************************************************/
static nai_status_t Clear_DSW_Status(int32_t paramCount, int32_t* p_params)
{
p_naiapp_AppParameters_t p_dsw_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dsw_params->cardIndex;
int32_t module = p_dsw_params->module;
int32_t chan = p_dsw_params->channel;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_MIN_LO_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_MID_RANGE_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_MAX_HI_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_LO_HI_TRANS_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_HI_LO_TRANS_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_BIT_LATCHED));
check_status(naibrd_DSW_ClearStatus(cardIndex, module, chan, NAI_DSW_STATUS_OVERCURRENT_LATCHED));
Display_DSW_Status(paramCount, p_params);
return NAI_ERROR_UNKNOWN;
}
/**************************************************************************************************************/
/**
<summary>
Configure_DSW_SwitchState handles the user request to change the switch state for the selected
channel and calls the method in the naibrd library to set the state.
</summary>
*/
/**************************************************************************************************************/
static nai_status_t Configure_DSW_SwitchState(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
bool_t bUpdateOutput = FALSE;
nai_dsw_state_t switchstate = 0;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
p_naiapp_AppParameters_t p_dsw_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dsw_params->cardIndex;
int32_t module = p_dsw_params->module;
int32_t chan = p_dsw_params->channel;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
/* Set the switch state (open or closed).
*/
printf("\n Type the desired switch state, Open or Closed (i.e. NO-COM Contact closure)\n ");
printf(" Enter Open or Closed: ");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt > 0)
{
switch (toupper(inputBuffer[0]))
{
case 'O':
switchstate = 0;
bUpdateOutput = TRUE;
break;
case 'C':
switchstate= 1;
bUpdateOutput = TRUE;
break;
default:
printf("ERROR: Invalid switch state selection\n");
break;
}
}
}
if (!bQuit)
{
if (bUpdateOutput)
check_status(naibrd_DSW_SetSwitchState(cardIndex, module, chan, switchstate));
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
/**************************************************************************************************************/
/**
<summary>
Configure_DSW_PullUpEnable handles the user request to change the PullUp Enable bit for the selected
channel and calls the method in the naibrd library to set the bit.
</summary>
*/
/**************************************************************************************************************/
static nai_status_t Configure_DSW_PullUpEnable(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = FALSE;
bool_t bUpdateOutput = FALSE;
nai_dsw_state_t pullupEn = 0;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
p_naiapp_AppParameters_t p_dsw_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dsw_params->cardIndex;
int32_t module = p_dsw_params->module;
int32_t chan = p_dsw_params->channel;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
/*
Check if Pull up enable is supported on current Module
*/
if(check_status(naibrd_DSW_GetPullUpSourceEnable(cardIndex, module,chan, &pullupEn))==NAI_ERROR_NOT_SUPPORTED)
{
printf("ERROR: Not Supported\n");
return bQuit;
}
/* Set the switch state (open or closed).
*/
printf("\n Type the desired Pull Up Enable Status, Enabled or Disabled\n ");
printf(" Enter Enabled or Disabled: ");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt > 0)
{
switch (toupper(inputBuffer[0]))
{
case 'E':
pullupEn = NAI_DSW_STATE_HI;
bUpdateOutput = TRUE;
break;
case 'D':
pullupEn = NAI_DSW_STATE_LO;
bUpdateOutput = TRUE;
break;
default:
printf("ERROR: Invalid selection\n");
break;
}
}
}
if (!bQuit)
{
if (bUpdateOutput)
check_status(naibrd_DSW_SetPullUpSourceEnable(cardIndex, module, chan, pullupEn));
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}