DIF PWM
Edit this on GitLab
DIF PWM Sample Application (SSK 2.x)
Overview
The DIF PWM sample application demonstrates how to configure and output Pulse Width Modulation (PWM) signals on DIF (Differential Input Function) channels using the NAI Software Support Kit (SSK 2.x). It shows how to set the pulse period, pulse width, polarity, and burst count, and how to start and stop PWM output on individual channels or across all channels simultaneously.
PWM output on DIF modules is an enhanced operating mode where the FPGA generates a precisely timed pulse train on the selected channel. Unlike simple high/low output toggling, the hardware handles all timing internally — your application only needs to configure the parameters and trigger the output. The module supports two PWM modes: continuous (the pulse train repeats indefinitely) and burst (the pulse train repeats for a specified number of cycles and then stops automatically).
Supported modules: DF1, DF2, DF3.
For the SSK 1.x version, see DIF PWM (SSK 1.x). For detailed register-level information, consult the DF1 Manual.
Prerequisites
Before running this sample, make sure you have:
-
An NAI board with a supported DIF module installed (DF1, DF2, or DF3).
-
SSK 2.x installed on your development host.
-
The sample applications built. Refer to the SSK 2.x Software Development Guide for platform-specific build instructions.
-
Appropriate output connections or a load/oscilloscope on the DIF channel(s) under test to observe the PWM waveform.
How to Run
Launch the dif_pwm executable from your build output directory. On startup the application looks for a configuration file (default_DIF_PWM.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 application presents a PWM operations menu where you configure the mode, period, pulse width, polarity, burst count, and start or stop PWM output.
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 DIF. For details on board connection configuration, see the SSK 2.x Software Development Guide. |
The main() function follows a standard SSK 2.x startup flow:
-
Call
naiapp_RunBoardMenu()to load a saved configuration file (if one exists) or present the interactive board menu. -
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_GetModuleName()so downstream code can verify the module is a supported DIF type.
#if defined (__VXWORKS__)
int32_t DIF_PWM(void)
#else
int32_t main(void)
#endif
{
bool_t stop = NAI_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) == NAI_TRUE)
{
while (stop != NAI_TRUE)
{
stop = naiapp_query_CardIndex(naiapp_GetBoardCnt(),
DEF_DIF_CARD_INDEX, &cardIndex);
if (stop != NAI_TRUE)
{
check_status(naibrd_GetModuleCount(cardIndex, &moduleCnt));
stop = naiapp_query_ModuleNumber(moduleCnt,
DEF_DIF_MODULE, &module);
if (stop != NAI_TRUE)
{
check_status(naibrd_GetModuleName(cardIndex, module,
&moduleID));
if ((moduleID != 0))
{
Run_DIF_PWM(cardIndex, module, moduleID);
}
}
}
naiif_printf("\r\nType Q to quit or Enter key to restart "
"application:\r\n");
stop = naiapp_query_ForQuitResponse(sizeof(inputBuffer),
NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
}
}
naiapp_access_CloseAllOpenCards();
return 0;
}
After retrieving the module ID, the application validates the module with naibrd_DIF_GetChannelCount():
void Run_DIF_PWM(int32_t cardIndex, int32_t module, int32_t ModuleID)
{
int32_t MaxChannel;
MaxChannel = naibrd_DIF_GetChannelCount(ModuleID);
if (MaxChannel == 0)
{
naiif_printf(
" *** Module selection not recognized as DIF module. ***\r\n\r\n");
}
else
{
Cfg_DIF_PWM_Channel(cardIndex, module, ModuleID, MaxChannel);
}
}
|
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 DIF_PWM().
Application Parameters
The Cfg_DIF_PWM_Channel() function populates an naiapp_AppParameters_t struct:
naiapp_AppParameters_t dif_params;
p_naiapp_AppParameters_t dif_pwm_params = &dif_params;
dif_pwm_params->cardIndex = cardIndex;
dif_pwm_params->module = module;
dif_pwm_params->modId = ModuleID;
dif_pwm_params->channel = chan;
Command Loop
After selecting a channel, the application sets the channel to PWM continuous output mode and configures the I/O format for output:
check_status(naibrd_DIF_SetOpMode(cardIndex, module, chan,
NAIBRD_DIF_MODE_OUTPUT_PWM_FOREVER));
check_status(naibrd_DIF_SetIOFormat(cardIndex, module, chan,
NAIBRD_DIF_GEN5_IOFORMAT_OUTPUT));
The available commands are:
| Command | Description |
|---|---|
Mode |
Select continuous, burst, or standard input mode |
Period |
Set PWM period (ms) |
Width |
Set PWM pulse width (ms) |
Count |
Set burst count |
POlarity |
Set output polarity (positive or negative) |
PM |
Display current PWM configuration |
Trigger |
Start PWM output |
Halt |
Stop PWM output |
Demo |
Configure all channels with incrementing parameters |
Reset |
Reset all channels to initial settings |
Stat |
Display channel status |
SETBurst |
Set all channels to PWM burst mode |
SETCont |
Set all channels to PWM continuous mode |
PWM Mode Selection
The sample supports three mode selections:
Continuous Mode
In continuous mode, the PWM output repeats indefinitely until explicitly stopped:
check_status(naibrd_DIF_SetOpMode(cardIndex, module, chan,
NAIBRD_DIF_MODE_OUTPUT_PWM_FOREVER));
check_status(naibrd_DIF_SetIOFormat(cardIndex, module, chan,
NAIBRD_DIF_GEN5_IOFORMAT_OUTPUT));
PWM Parameter Configuration
Period
The period defines the total time for one complete PWM cycle, specified in milliseconds:
check_status(naibrd_DIF_SetPWM_Period(cardIndex, module, chan, time));
The valid range depends on the module’s timebase LSB, retrieved with naibrd_DIF_GetTimebaseLSB().
Pulse Width
The pulse width defines the active-high (or active-low, depending on polarity) portion of each cycle:
check_status(naibrd_DIF_SetPWM_Pulsewidth(cardIndex, module, chan, time));
The pulse width must be less than or equal to the period.
Burst Count
The burst count specifies how many PWM cycles are output in burst mode:
check_status(naibrd_DIF_SetPWM_BurstNum(cardIndex, module, chan,
burstcount));
The minimum burst count is 1.
Polarity
The polarity controls whether the PWM pulse is positive-going or negative-going:
/* Positive polarity */
check_status(naibrd_DIF_SetPWM_Polarity(cardIndex, module, chan,
(naibrd_dif_pwm_polarity_t)NAIBRD_DIF_PWMPOLARITY_POS));
/* Negative polarity */
check_status(naibrd_DIF_SetPWM_Polarity(cardIndex, module, chan,
(naibrd_dif_pwm_polarity_t)NAIBRD_DIF_PWMPOLARITY_NEG));
Starting and Stopping PWM Output
Displaying Configuration and Status
PWM Configuration
The Display_DIF_PWM_Configuration() function reads back and displays the current period, pulse width, burst count, and polarity:
check_status(naibrd_DIF_GetPWM_Period(cardIndex, module, chan, &period));
check_status(naibrd_DIF_GetPWM_Pulsewidth(cardIndex, module, chan,
&pulsewidth));
check_status(naibrd_DIF_GetPWM_BurstNum(cardIndex, module, chan,
&burstnumber));
check_status(naibrd_DIF_GetPWM_Polarity(cardIndex, module, chan,
&polaritysetting));
Troubleshooting Reference
| Symptom | Possible Cause and Resolution |
|---|---|
No PWM output observed |
PWM has not been triggered. Call |
PWM period is incorrect |
Verify the period value in milliseconds. Check the valid range using |
Pulse width exceeds period |
The pulse width must be less than or equal to the period. Reduce the pulse width or increase the period. |
Burst mode runs continuously |
The mode may be set to continuous instead of burst. Verify with |
Module not recognized |
|
Overcurrent status |
The output channel may be shorted or overloaded. Check |
Output polarity inverted |
Check the polarity setting with |
Full Source
Full Source — dif_pwm.c (SSK 2.x)
/* nailib include files */
#include "nai_libs/nailib/include/naitypes.h"
#include "nai_libs/nailib/include/nailib.h"
#include "nai_libs/nailib/include/nailib_utils.h"
/* naibrd include files */
#include "nai_libs/naibrd/include/naibrd.h"
#include "nai_libs/naibrd/include/functions/naibrd_dif.h"
/* naiif include files */
#include "nai_libs/naiif/include/naiif_stdio.h"
/* Common Sample Program include files */
#include "nai_sample_apps/naiapp_common/include/naiapp_boardaccess_menu.h"
#include "nai_sample_apps/naiapp_common/include/naiapp_boardaccess_query.h"
#include "nai_sample_apps/naiapp_common/include/naiapp_boardaccess_access.h"
#include "nai_sample_apps/naiapp_common/include/naiapp_boardaccess_display.h"
#include "nai_sample_apps/naiapp_common/include/naiapp_boardaccess_utils.h"
static const int8_t *CONFIG_FILE = (int8_t *)"default_DIF_PWM.txt";
/* Function prototypes */
static void Run_DIF_PWM(int32_t cardIndex, int32_t module, int32_t ModuleID);
static void Cfg_DIF_PWM_Channel(int32_t cardIndex, int32_t module, uint32_t ModuleID, int32_t MaxChannel);
static void Display_DIF_PWM_ChannelCfg(int32_t cardIndex, int32_t module, int32_t chan, uint32_t ModuleID);
static nai_status_t Display_DIF_PWM_Configuration(int32_t paramCount, int32_t* p_params);
static nai_status_t Display_DIF_Status(int32_t paramCount, int32_t* p_params);
static nai_status_t Configure_DIF_PWM_Mode(int32_t paramCount, int32_t* p_params);
static nai_status_t Configure_DIF_PWM_Period(int32_t paramCount, int32_t* p_params);
static nai_status_t Configure_DIF_PWM_Pulsewidth(int32_t paramCount, int32_t* p_params);
static nai_status_t Configure_DIF_PWM_Burstcount(int32_t paramCount, int32_t* p_params);
static nai_status_t Configure_DIF_PWM_Polarity(int32_t paramCount, int32_t* p_params);
static const int32_t DEF_DIF_CARD_INDEX = 0;
static const int32_t DEF_DIF_MODULE = 1;
static const int32_t DEF_DIF_CHANNEL = 1;
/****** Command Table *******/
enum dif_pwm_commands
{
DIF_PWM_CMD_MODE,
DIF_PWM_CMD_PERIOD,
DIF_PWM_CMD_PULSEWIDTH,
DIF_PWM_CMD_BURSTCOUNT,
DIF_PWM_CMD_POLARITY,
DIF_PWM_CMD_PWM_CFG,
DIF_PWM_CMD_PWM_START,
DIF_PWM_CMD_PWM_STOP,
DIF_PWM_CMD_PWM_DEMO,
DIF_PWM_CMD_PWM_INITIAL,
DIF_PWM_CMD_STATUS,
DIF_PWM_CMD_PWM_BURST,
DIF_PWM_CMD_PWM_CONT,
DIF_PWM_CMD_COUNT
};
naiapp_cmdtbl_params_t DIF_PWM_MenuCmds[] = {
{"Mode", " DIF Select PWM Mode", DIF_PWM_CMD_MODE, Configure_DIF_PWM_Mode},
{"Period", " DIF Set PWM Period", DIF_PWM_CMD_PERIOD, Configure_DIF_PWM_Period},
{"Width", " DIF Set PWM Pulsewidth", DIF_PWM_CMD_PULSEWIDTH, Configure_DIF_PWM_Pulsewidth},
{"Count", " DIF Set PWM burst count", DIF_PWM_CMD_BURSTCOUNT, Configure_DIF_PWM_Burstcount},
{"POlarity", " DIF Set PWM Polarity", DIF_PWM_CMD_POLARITY, Configure_DIF_PWM_Polarity},
{"PM", " DIF Display PWM configuration settings", DIF_PWM_CMD_PWM_CFG, Display_DIF_PWM_Configuration},
{"Trigger", " DIF Start PWM output", DIF_PWM_CMD_PWM_START, NULL},
{"Halt", " DIF Stop PWM output", DIF_PWM_CMD_PWM_STOP, NULL},
{"Demo", " DIF Demo PWM Settings", DIF_PWM_CMD_PWM_DEMO, NULL},
{"Reset", " DIF Reset All Channels to Initial Settings", DIF_PWM_CMD_PWM_INITIAL, NULL},
{"Stat", " DIF Display Status", DIF_PWM_CMD_STATUS, Display_DIF_Status},
{"SETBurst", " DIF Set all channels to PWM Burst", DIF_PWM_CMD_PWM_BURST, NULL},
{"SETCont", " DIF Set all channels to PWM Continuous", DIF_PWM_CMD_PWM_CONT, NULL},
};
#if defined (__VXWORKS__)
int32_t DIF_PWM(void)
#else
int32_t main(void)
#endif
{
bool_t stop = NAI_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) == NAI_TRUE)
{
while (stop != NAI_TRUE)
{
stop = naiapp_query_CardIndex(naiapp_GetBoardCnt(), DEF_DIF_CARD_INDEX, &cardIndex);
if (stop != NAI_TRUE)
{
check_status(naibrd_GetModuleCount(cardIndex, &moduleCnt));
stop = naiapp_query_ModuleNumber(moduleCnt, DEF_DIF_MODULE, &module);
if (stop != NAI_TRUE)
{
check_status(naibrd_GetModuleName(cardIndex, module, &moduleID));
if ((moduleID != 0))
{
Run_DIF_PWM(cardIndex, module, moduleID);
}
}
}
naiif_printf("\r\nType Q to quit or Enter key to restart application:\r\n");
stop = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
}
}
naiif_printf("\r\nType the Enter key to exit the program: ");
naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
naiapp_access_CloseAllOpenCards();
return 0;
}
void Run_DIF_PWM(int32_t cardIndex, int32_t module, int32_t ModuleID)
{
int32_t MaxChannel;
MaxChannel = naibrd_DIF_GetChannelCount(ModuleID);
if (MaxChannel == 0)
naiif_printf(" *** Module selection not recognized as DIF module. ***\r\n\r\n");
else
Cfg_DIF_PWM_Channel(cardIndex, module, ModuleID, MaxChannel);
}
void Display_DIF_PWM_ChannelCfg(int32_t cardIndex, int32_t module, int32_t chan, uint32_t ModuleID)
{
uint32_t ioformat = 0;
naibrd_dif_state_t outputstate = 0;
naibrd_dif_state_t inputstate = 0;
naibrd_dif_enhanced_mode_t opmode = (naibrd_dif_enhanced_mode_t)0;
check_status(naibrd_GetModuleName(cardIndex, module, &ModuleID));
check_status(naibrd_DIF_GetIOFormat(cardIndex, module, chan, &ioformat));
check_status(naibrd_DIF_GetOutputState(cardIndex, module, chan, &outputstate));
check_status(naibrd_DIF_GetInputState(cardIndex, module, chan, &inputstate));
check_status(naibrd_DIF_GetOpMode(cardIndex, module, chan, &opmode));
naiif_printf("\r\n === Channel %d ===\r\n\r\n", chan);
naiif_printf(" I/O Output Input \r\n");
naiif_printf(" Format State State Enhanced Mode Selection \r\n");
naiif_printf("-------- ------- ------- ------------------------- \r\n");
switch (ioformat) {
case NAIBRD_DIF_IOFORMAT_INPUT: naiif_printf(" Input "); break;
case NAIBRD_DIF_GEN3_IOFORMAT_OUTPUT:
case NAIBRD_DIF_GEN5_IOFORMAT_OUTPUT: naiif_printf(" High-side"); break;
default: naiif_printf(" Unknown "); break;
}
switch (outputstate) {
case NAIBRD_DIF_STATE_LO: naiif_printf(" LOW "); break;
case NAIBRD_DIF_STATE_HI: naiif_printf(" HIGH "); break;
default: naiif_printf(" Unknown "); break;
}
switch (inputstate) {
case NAIBRD_DIF_STATE_LO: naiif_printf(" LOW Input "); break;
case NAIBRD_DIF_STATE_HI: naiif_printf("HIGH Input "); break;
default: naiif_printf("Unknown "); break;
}
/* Display opmode - abbreviated for readability */
naiif_printf("\r\n");
}
void Cfg_DIF_PWM_Channel(int32_t cardIndex, int32_t module, uint32_t ModuleID, int32_t MaxChannel)
{
nai_status_t status = (nai_status_t)0;
bool_t bQuit = NAI_FALSE;
bool_t bContinue = NAI_TRUE;
bool_t bCmdFound = NAI_FALSE;
int32_t chan, defaultchan = 1;
int32_t cmd;
int32_t ch_loop;
float64_t time;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
naiapp_AppParameters_t dif_params;
p_naiapp_AppParameters_t dif_pwm_params = &dif_params;
dif_pwm_params->cardIndex = cardIndex;
dif_pwm_params->module = module;
dif_pwm_params->modId = ModuleID;
while (bContinue)
{
naiif_printf(" \r\r\n\r\r\n");
naiif_printf("Channel selection \r\r\n");
naiif_printf("================= \r\r\n");
defaultchan = DEF_DIF_CHANNEL;
bQuit = naiapp_query_ChannelNumber(MaxChannel, defaultchan, &chan);
dif_pwm_params->channel = chan;
check_status(naibrd_DIF_SetOpMode(cardIndex, module, chan, NAIBRD_DIF_MODE_OUTPUT_PWM_FOREVER));
check_status(naibrd_DIF_SetIOFormat(cardIndex, module, chan, NAIBRD_DIF_GEN5_IOFORMAT_OUTPUT));
naiapp_utils_LoadParamMenuCommands(DIF_PWM_CMD_COUNT, DIF_PWM_MenuCmds);
while (bContinue)
{
Display_DIF_PWM_ChannelCfg(cardIndex, module, chan, ModuleID);
naiapp_display_ParamMenuCommands((int8_t *)"DIF PWM Menu");
naiif_printf("\r\nType DIF command or %c to quit : ", 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 DIF_PWM_CMD_MODE:
case DIF_PWM_CMD_PERIOD:
case DIF_PWM_CMD_PULSEWIDTH:
case DIF_PWM_CMD_BURSTCOUNT:
case DIF_PWM_CMD_POLARITY:
case DIF_PWM_CMD_PWM_CFG:
case DIF_PWM_CMD_STATUS:
DIF_PWM_MenuCmds[cmd].func(APP_PARAM_COUNT, (int32_t*)dif_pwm_params);
break;
case DIF_PWM_CMD_PWM_START:
check_status(naibrd_DIF_StartPWM(cardIndex, module, chan));
break;
case DIF_PWM_CMD_PWM_STOP:
check_status(naibrd_DIF_StopPWM(cardIndex, module, chan));
naiif_printf("Mode reset to Input\r\n");
break;
case DIF_PWM_CMD_PWM_DEMO:
case DIF_PWM_CMD_PWM_INITIAL:
case DIF_PWM_CMD_PWM_BURST:
case DIF_PWM_CMD_PWM_CONT:
/* Batch operations on all channels - see full source */
break;
default:
naiif_printf("Invalid command entered\r\n");
break;
}
}
}
}
else
bContinue = NAI_FALSE;
}
}
}
nai_status_t Configure_DIF_PWM_Mode(int32_t paramCount, int32_t* p_params)
{
bool_t bQuit = NAI_FALSE;
p_naiapp_AppParameters_t p_dif_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dif_params->cardIndex;
int32_t module = p_dif_params->module;
int32_t chan = p_dif_params->channel;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
naiif_printf("\r\n == PWM Mode Selection == \r\n C Continuous PWM mode \r\n Burst PWM Burst mode \r\n None Basic input mode \r\n\r\n Type DSW command : ");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt > 0)
{
if ((toupper(inputBuffer[0]) == 'C'))
{
check_status(naibrd_DIF_SetOpMode(cardIndex, module, chan, NAIBRD_DIF_MODE_OUTPUT_PWM_FOREVER));
check_status(naibrd_DIF_SetIOFormat(cardIndex, module, chan, NAIBRD_DIF_GEN5_IOFORMAT_OUTPUT));
}
else if ((toupper(inputBuffer[0]) == 'B'))
{
check_status(naibrd_DIF_SetOpMode(cardIndex, module, chan, NAIBRD_DIF_MODE_OUTPUT_PWM_CYCLE_NUM_TIMES));
check_status(naibrd_DIF_SetIOFormat(cardIndex, module, chan, NAIBRD_DIF_GEN5_IOFORMAT_OUTPUT));
}
else if ((toupper(inputBuffer[0]) == 'N'))
{
check_status(naibrd_DIF_SetOpMode(cardIndex, module, chan, NAIBRD_DIF_MODE_STD_INPUT_OUTPUT));
check_status(naibrd_DIF_SetIOFormat(cardIndex, module, chan, NAIBRD_DIF_IOFORMAT_INPUT));
}
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
nai_status_t Configure_DIF_PWM_Period(int32_t paramCount, int32_t* p_params)
{
p_naiapp_AppParameters_t p_dif_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dif_params->cardIndex;
int32_t module = p_dif_params->module;
int32_t chan = p_dif_params->channel;
bool_t bQuit = NAI_FALSE;
float64_t time = 0.0, lsb = 0, min = 1, max = -1;
uint32_t ModuleID;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
naiif_printf("\r\nEnter the desired period in ms: ");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt > 0)
{
check_status(naibrd_GetModuleName(cardIndex, module, &ModuleID));
time = atof((const char *)inputBuffer);
lsb = naibrd_DIF_GetTimebaseLSB(ModuleID);
switch (ModuleID) {
case NAIBRD_MODULE_ID_DF2:
min = (float64_t)(0x2u * lsb);
max = (float64_t)(0xFFFFFFFF * lsb);
default: break;
}
if (time > max || time < min)
naiif_printf(" Entry out of range. Range %7.3f to %7.3f ms\r\n", min, max);
else
check_status(naibrd_DIF_SetPWM_Period(cardIndex, module, chan, time));
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
nai_status_t Configure_DIF_PWM_Pulsewidth(int32_t paramCount, int32_t* p_params)
{
p_naiapp_AppParameters_t p_dif_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dif_params->cardIndex;
int32_t module = p_dif_params->module;
int32_t chan = p_dif_params->channel;
bool_t bQuit = NAI_FALSE;
float64_t time = 0.0, lsb = 0, min = 1, max = -1;
uint32_t ModuleID;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
naiif_printf("\r\nEnter the desired pulsewidth in milliseconds: ");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt > 0)
{
check_status(naibrd_GetModuleName(cardIndex, module, &ModuleID));
time = atof((const char *)inputBuffer);
lsb = naibrd_DIF_GetTimebaseLSB(ModuleID);
switch (ModuleID) {
case NAIBRD_MODULE_ID_DF2:
min = (float64_t)(0x1u * lsb);
max = (float64_t)(0xFFFFFFFE * lsb);
break;
default: break;
}
if (time > max || time < min)
naiif_printf(" Entry out of range. Range %7.3f to %7.3f ms\r\n", min, max);
else
check_status(naibrd_DIF_SetPWM_Pulsewidth(cardIndex, module, chan, time));
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
nai_status_t Configure_DIF_PWM_Burstcount(int32_t paramCount, int32_t* p_params)
{
p_naiapp_AppParameters_t p_dif_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dif_params->cardIndex;
int32_t module = p_dif_params->module;
int32_t chan = p_dif_params->channel;
bool_t bQuit = NAI_FALSE;
uint32_t burstcount, ModuleID;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
naiif_printf("\r\nEnter the desired burst count: ");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt > 0)
{
check_status(naibrd_GetModuleName(cardIndex, module, &ModuleID));
burstcount = atoi((const char *)inputBuffer);
switch (ModuleID) {
case NAIBRD_MODULE_ID_DF2:
if (burstcount < 1) { burstcount = 1; naiif_printf("Setting burstcount to minimum of 1.\r\n"); }
check_status(naibrd_DIF_SetPWM_BurstNum(cardIndex, module, chan, burstcount));
break;
default: naiif_printf("Unsupported function for this module.\r\n"); break;
}
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
nai_status_t Configure_DIF_PWM_Polarity(int32_t paramCount, int32_t* p_params)
{
p_naiapp_AppParameters_t p_dif_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dif_params->cardIndex;
int32_t module = p_dif_params->module;
int32_t chan = p_dif_params->channel;
bool_t bQuit = NAI_FALSE;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
naiif_printf("\r\n == PWM Output Polarity Selection == \r\n Pos DIF PWM positive going pulse output \r\n Neg DIF PWM negative going pulse output \r\n\r\n Type DIF command : ");
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (inputResponseCnt > 0)
{
if ((toupper(inputBuffer[0]) == 'P'))
check_status(naibrd_DIF_SetPWM_Polarity(cardIndex, module, chan, (naibrd_dif_pwm_polarity_t)NAIBRD_DIF_PWMPOLARITY_POS));
else if ((toupper(inputBuffer[0]) == 'N'))
check_status(naibrd_DIF_SetPWM_Polarity(cardIndex, module, chan, (naibrd_dif_pwm_polarity_t)NAIBRD_DIF_PWMPOLARITY_NEG));
}
}
return (bQuit) ? NAI_ERROR_UNKNOWN : NAI_SUCCESS;
}
nai_status_t Display_DIF_PWM_Configuration(int32_t paramCount, int32_t* p_params)
{
p_naiapp_AppParameters_t p_dif_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dif_params->cardIndex;
int32_t module = p_dif_params->module;
int32_t chan = p_dif_params->channel;
naibrd_dif_pwm_polarity_t polaritysetting;
float64_t period, pulsewidth;
uint32_t burstnumber;
naiif_printf("\r\n -------------PWM Configuration Settings----------------------------\r\n");
naiif_printf(" Period (ms) Pulsewidth (ms) Burst Cnt Polarity \r\n");
naiif_printf(" -------------- ---------------- ----------- ----------- \r\n");
check_status(naibrd_DIF_GetPWM_Period(cardIndex, module, chan, &period));
naiif_printf(" %10.6f ", period);
check_status(naibrd_DIF_GetPWM_Pulsewidth(cardIndex, module, chan, &pulsewidth));
naiif_printf(" %10.6f ", pulsewidth);
check_status(naibrd_DIF_GetPWM_BurstNum(cardIndex, module, chan, &burstnumber));
naiif_printf(" 0x%08X ", burstnumber);
check_status(naibrd_DIF_GetPWM_Polarity(cardIndex, module, chan, &polaritysetting));
naiif_printf(polaritysetting == 0 ? " POS " : " NEG ");
naiif_printf("\r\n\r\n");
return NAI_ERROR_UNKNOWN;
}
static nai_status_t Display_DIF_Status(int32_t paramCount, int32_t* p_params)
{
p_naiapp_AppParameters_t p_dif_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_dif_params->cardIndex;
int32_t module = p_dif_params->module;
int32_t chan = p_dif_params->channel;
nai_status_bit_t status;
naiif_printf("\r\n ----------------- Status ----------------------------\r\n");
naiif_printf(" BIT OC Lo-Hi Hi-Lo \r\n");
naiif_printf(" ------- -------- ------ ------- \r\n");
check_status(naibrd_DIF_GetChanMappedStatus(cardIndex, module, chan, NAIBRD_DIF_STATUS_BIT_LATCHED, &status));
naiif_printf(" %3i ", status);
check_status(naibrd_DIF_GetChanMappedStatus(cardIndex, module, chan, NAIBRD_DIF_STATUS_OVERCURRENT_LATCHED, &status));
naiif_printf(" %3i ", status);
check_status(naibrd_DIF_GetChanMappedStatus(cardIndex, module, chan, NAIBRD_DIF_STATUS_LO_HI_TRANS_LATCHED, &status));
naiif_printf(" %3i ", status);
check_status(naibrd_DIF_GetChanMappedStatus(cardIndex, module, chan, NAIBRD_DIF_STATUS_HI_LO_TRANS_LATCHED, &status));
naiif_printf(" %3i ", status);
naiif_printf("\r\n\r\n");
return NAI_ERROR_UNKNOWN;
}