TTL Output
Edit this on GitLab
TTL Output
Explanation
About: North Atlantic Industries Sample Application Code
This code is a sample application from North Atlantic Industries (NAI) that demonstrates how to interact with their embedded function modules, specifically for TTL output. The provided code leverages various libraries and APIs included with NAI’s Software Support Kit (SSK) and functions specifically designed to handle TTL (Transistor-Transistor Logic) operations. Below is a detailed walkthrough of the provided code.
Definitions and Constants - CONFIG_FILE: A constant pointer to an 8-bit signed integer representing the configuration file name. - DEF_TTL_FIRST_CHAN: Default TTL channel number set to 1.
Function Prototypes - Run_TTL_Output: Prompts the user to select a card, module, and channel, then configures and runs the TTL output if valid. - Run_TTL_Output_Start: Executes the inner menu that allows users to select a TTL channel and output state. - Print_TTL_Channel_Header: Formats and prints the header containing the channel number, mode, and output status. - Set_TTL_High: Sets the specified TTL channel state to logic 1 (high). - Set_TTL_Low: Sets the specified TTL channel state to logic 0 (low). - Set_TTL_Toggle: Toggles the TTL channel state between high and low. - Cfg_TTL_Setup: Resets and configures the specified channel for output mode.
Enumerations - enum ttl_SynchronousPreload_commands: Contains commands for TTL output states: - TTL_OUTPUT_CMD_HIGH: Command to set the TTL output to high. - TTL_OUTPUT_CMD_LOW: Command to set the TTL output to low. - TTL_OUTPUT_CMD_TOGGLE: Command to toggle the TTL output. - TTL_OUTPUT_CMD_LAST: Placeholder indicating the last command.
Main Function
The main
function initiates and executes the program:
-
Configuration Loading: Calls
naiapp_RunBoardMenu
with the configuration file to load device configuration. -
User Input Loop: Repeatedly queries the user for card and module selection:
-
Card Index Query: Prompts user to input card index and verifies it.
-
Module Count & Module Number Query: Fetches module count and prompts user for the module number selection.
-
Module ID Validation: Validates the selected module ID and initiates
Run_TTL_Output
.
-
-
Termination Query: At the end of each loop, the user is prompted to type 'Q' to quit or press Enter to restart.
Run_TTL_Output Function
This function manages the TTL output operation:
- Channel Count Validation: Retrieves the channel count and validates if the selected module is a TTL module.
- Run TTL Output Start: If the module is valid, Run_TTL_Output_Start
is called to handle the TTL operations.
Run_TTL_Output_Start Function This function manages the detailed operations for TTL output: - Channel Selection: Queries user to select a TTL channel. - Channel Configuration: Configures the selected TTL channel for first use. - Internal Power Supply: Ensures the internal power supply is used. - Command Execution Loop: Displays a command menu, takes user input, and executes the corresponding functions to set TTL states.
TTL Channel Header Printing
The Print_TTL_Channel_Header
function formats and prints the channel number, mode, and state (High or Low).
Configuration Setup
The Cfg_TTL_Setup
function resets and configures the specified channel for output mode based on the module ID.
Functions for Setting TTL States - Set_TTL_High: Sets the TTL channel state to high (logic 1). - Set_TTL_Low: Sets the TTL channel state to low (logic 0). - Set_TTL_Toggle: Toggles the TTL channel state (high to low or low to high).
Overall, the provided code demonstrates how to interactively handle TTL outputs using NAI�s embedded function modules through user inputs and predefined functions. Each part of the code is carefully orchestrated to ensure smooth read and write operations on designated TTL channels within the defined card and module constraints.
#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_ttl.h"
#include "advanced/nai_ether_adv.h"
static const int8_t *CONFIG_FILE = (const int8_t *)"default_TTL_Output.txt";
static const int32_t DEF_TTL_FIRST_CHAN = 1;
/* Function Prototypes */
void Run_TTL_Output(int32_t cardIndex, int32_t module, int32_t ModuleID);
void Run_TTL_Output_Start(int32_t cardIndex, int32_t module, uint32_t ModuleID, int32_t MaxChannel);
static void Print_TTL_Channel_Header(int32_t cardIndex, int32_t module, int32_t channelCount);
nai_status_t Set_TTL_High(int32_t paramCount, int32_t* p_params);
nai_status_t Set_TTL_Low(int32_t paramCount, int32_t* p_params);
nai_status_t Set_TTL_Toggle(int32_t paramCount, int32_t* p_params);
static int32_t Cfg_TTL_Setup(int32_t cardIndex, int32_t module, int32_t modid, int32_t channel);
enum ttl_SynchronousPreload_commands
{
TTL_OUTPUT_CMD_HIGH,
TTL_OUTPUT_CMD_LOW,
TTL_OUTPUT_CMD_TOGGLE,
TTL_OUTPUT_CMD_LAST
};
naiapp_cmdtbl_params_t TTL_Output_MainMenuCmds[] =
{
{"High", "Output a logic 1 (high)", TTL_OUTPUT_CMD_HIGH, Set_TTL_High },
{"Low", "Output a logic 0 (low)", TTL_OUTPUT_CMD_LOW, Set_TTL_Low },
{"Toggle", "Toggle the output state", TTL_OUTPUT_CMD_TOGGLE, Set_TTL_Toggle }
};
/**************************************************************************************************************/
/**
<summary>
main obtains and/or loads device configuration.
</summary>
*/
/**************************************************************************************************************/
#if defined (__VXWORKS__)
int32_t TTL_Output(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_TTL_Output(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_TTL_Output prompts the user for the card, module and channel to use for the application and calls
Run_TTL_Output_Start if the card, module, channel is valid for as a TLL module.
</summary>
*/
/**************************************************************************************************************/
void Run_TTL_Output(int32_t cardIndex, int32_t module, int32_t ModuleID)
{
int32_t MaxChannel;
MaxChannel = naibrd_TTL_GetChannelCount(ModuleID);
if (MaxChannel == 0)
{
printf(" *** Module selection not recognized as TTL module. ***\n\n");
}
else
{
Run_TTL_Output_Start(cardIndex, module, ModuleID, MaxChannel);
}
}
/**************************************************************************************************************/
/**
<summary>
Run_TTL_Output_Start runs the inner menu which allows the user to select a channel and an output state.
</summary>
*/
/**************************************************************************************************************/
void Run_TTL_Output_Start(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 = 0;
int32_t defaultchan = 1;
int32_t cmd;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
naiapp_AppParameters_t ttl_params;
p_naiapp_AppParameters_t ttl_output_params = &ttl_params;
ttl_output_params->cardIndex = cardIndex;
ttl_output_params->module = module;
while (bContinue)
{
printf(" \r\n\r\n");
printf("Channel selection \r\n");
printf("================= \r\n");
defaultchan = DEF_TTL_FIRST_CHAN;
bQuit = naiapp_query_ChannelNumber(MaxChannel, defaultchan, &ttl_output_params->channel);
check_status(naibrd_TTL_SetBankVCCSource(cardIndex, module, 1, (nai_ttl_vcc_t)NAI_TTL_VCC_INTERNAL));
/* Configure the channel for first use */
Cfg_TTL_Setup(cardIndex, module, ModuleID, chan);
/* Load commands from the state table and get a response*/
naiapp_utils_LoadParamMenuCommands(TTL_OUTPUT_CMD_LAST, TTL_Output_MainMenuCmds);
/* Ensure that we're using the internal power supply */
check_status(naibrd_TTL_SetBankVCCSource(cardIndex, module, 1, (nai_ttl_vcc_t)NAI_TTL_VCC_INTERNAL));
while (bContinue)
{
/* Display_TTL_SynchronousPreload_Channel(cardIndex, module, chan, ModuleID); */
naiapp_display_ParamMenuCommands((int8_t *)"TTL Synchronous Preload Operation Menu");
/* Print the state of the channel */
Print_TTL_Channel_Header(cardIndex, module, chan);
printf("\nType TTL 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 TTL_OUTPUT_CMD_HIGH:
case TTL_OUTPUT_CMD_LOW:
case TTL_OUTPUT_CMD_TOGGLE:
if (NULL != TTL_Output_MainMenuCmds[cmd].func) /* Ensure that the function pointer is valid */
{
TTL_Output_MainMenuCmds[cmd].func(APP_PARAM_COUNT, (int32_t*)ttl_output_params);
Print_TTL_Channel_Header(cardIndex, module, chan);
}
else
printf("No function is associated with the command %s.\r\n", TTL_Output_MainMenuCmds[cmd].cmdstr);
break;
default:
printf("Invalid command entered\n");
break;
}
}
else
printf("Invalid command entered\n");
}
}
else
bContinue = FALSE;
}
}
}
/**************************************************************************************************************/
/**
<summary>
Print_TTL_Channel_Header Formats and prints a header which indicates the channel number, mode, and output status.
</summary>
*/
/**************************************************************************************************************/
static void Print_TTL_Channel_Header(int32_t cardIndex, int32_t module, int32_t channel)
{
uint32_t status;
nai_ttl_state_t state;
nai_ttl_enhanced_mode_t mode;
printf(" \r\n\r\n");
printf("=======================\r\n");
printf(" Channel Status \r\n");
printf("=======================\r\n");
printf(" Ch Mode State \r\n");
printf("-----------------------\r\n");
status = check_status(naibrd_TTL_GetOpMode(cardIndex, module, channel, &mode));
status |= check_status(naibrd_TTL_GetOutputState(cardIndex, module, channel, &state));
printf(" %2d %3d %c \r\n", channel, (uint32_t)mode, (state == NAI_TTL_STATE_LO) ? 'L' : 'H');
}
/*============================================================================================================= */
/* Sample Code Starts Here */
/*============================================================================================================= */
/**************************************************************************************************************/
/**
<summary>
Cfg_TTL_Setup resets and configures the specified channel to NAI_TTL_MODE_OUTPUT mode.
</summary>
*/
/**************************************************************************************************************/
static int32_t Cfg_TTL_Setup(int32_t cardIndex, int32_t module, int32_t modid, int32_t channel)
{
int32_t status;
status = (int32_t)check_status(naibrd_TTL_Reset(cardIndex, module, channel, NAI_TTL_RESET_TIMER_ONLY));
switch (modid)
{
case NAI_MODULE_ID_TL1:
case NAI_MODULE_ID_TL2:
status |= (int32_t)check_status(naibrd_TTL_SetOpMode(cardIndex, module, channel, NAI_TTL_MODE_STD_INPUT_OUTPUT));
break;
case NAI_MODULE_ID_D7:
status |= (int32_t)check_status(naibrd_TTL_SetIOFormat(cardIndex, module, channel, NAI_TTL_GEN5_IOFORMAT_OUTPUT));
}
return status;
}
/**************************************************************************************************************/
/**
<summary>
Set_TTL_High will set the output state of the specified channel to logic 1 (High).
</summary>
*/
/**************************************************************************************************************/
nai_status_t Set_TTL_High(int32_t paramCount, int32_t* p_params)
{
int32_t status;
p_naiapp_AppParameters_t p_ttl_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_ttl_params->cardIndex;
int32_t module = p_ttl_params->module;
int32_t channel = p_ttl_params->channel;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
status = check_status(naibrd_TTL_SetOutputState(cardIndex, module, channel, NAI_TTL_STATE_HI));
if (status == NAI_SUCCESS)
return NAI_SUCCESS;
else
return NAI_ERROR_UNKNOWN;
}
/**************************************************************************************************************/
/**
<summary>
Set_TTL_Low will set the output state of the specified channel to logic 0 (Low).
</summary>
*/
/**************************************************************************************************************/
nai_status_t Set_TTL_Low(int32_t paramCount, int32_t* p_params)
{
int32_t status;
p_naiapp_AppParameters_t p_ttl_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_ttl_params->cardIndex;
int32_t module = p_ttl_params->module;
int32_t channel = p_ttl_params->channel;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
status = check_status(naibrd_TTL_SetOutputState(cardIndex, module, channel, NAI_TTL_STATE_LO));
if (status == NAI_SUCCESS)
return NAI_SUCCESS;
else
return NAI_ERROR_UNKNOWN;
}
/**************************************************************************************************************/
/**
<summary>
Set_TTL_Toggle will check the output state of the specified channel and will toggle it accordingly
(i.e. High -> Low, Low -> High).
</summary>
*/
/**************************************************************************************************************/
nai_status_t Set_TTL_Toggle(int32_t paramCount, int32_t* p_params)
{
int32_t status;
nai_ttl_state_t state;
p_naiapp_AppParameters_t p_ttl_params = (p_naiapp_AppParameters_t)p_params;
int32_t cardIndex = p_ttl_params->cardIndex;
int32_t module = p_ttl_params->module;
int32_t channel = p_ttl_params->channel;
#if defined (WIN32)
UNREFERENCED_PARAMETER(paramCount);
#endif
status = check_status(naibrd_TTL_GetOutputState(cardIndex, module, channel, &state));
if (NAI_TTL_STATE_HI == state)
status |= check_status(naibrd_TTL_SetOutputState(cardIndex, module, channel, NAI_TTL_STATE_LO));
else
status |= check_status(naibrd_TTL_SetOutputState(cardIndex, module, channel, NAI_TTL_STATE_HI));
if (status == NAI_SUCCESS)
return NAI_SUCCESS;
else
return NAI_ERROR_UNKNOWN;
}