M1760 RT Program EEPROM
Edit this on GitLab
M1760 RT Program EEPROM
Explanation
About the Sample Code
The provided C code demonstrates how to use North Atlantic Industries' (NAI) System Software Kit (SSK) to interact with embedded function modules, specifically to configure a MIL-STD-1760 Remote Terminal (RT) on the 1553 channel. This application illustrates the process to configure the RT, set up buffers, legalize subaddresses, and store the configuration in EEPROM for auto-initialization upon reboot. Below is a detailed walkthrough and explanation of the code.
Overview of Key Components
-
Header Files:
-
Standard C libraries:
<stdio.h>
,<ctype.h>
,<stdlib.h>
,<string.h>
,<time.h>
-
NAI Application Specific Libraries: Provides routines for board access, display, and utilities.
-
1553 Utilities: Specific for handling the 1553 bus standard.
-
NAI Board Libraries: Defines functions for interacting with the board modules.
-
-
Constants and Macros:
-
CONFIG_FILE
: Default configuration file for EEPROM. -
Data block IDs:
DBLK_SA2
,DBLK_SA15
,DBLK_SA23_1
,DBLK_SA23_2
-
Default RT parameters:
DEF_RT_CHANNEL
,DEF_RT_DEV_NUM
,DEF_RT_ADDRESS
-
-
Function Prototypes:
-
Run_M1760_RT_Program_EEPROM
: Core function to perform the EEPROM programming.
-
Main Program Flow
The main entry point varies depending on the operating system:
- For VxWorks, M1760_RT_Program_EEPROM
- Otherwise, main
-
Initialization:
-
Set flags, card index, module, module count, and module ID.
-
Load and run board menu using
naiapp_RunBoardMenu
.
-
-
Query for Card Index and Module:
-
Continuously query for card index and module number using menu functions until the user decides to quit.
-
-
Invoke Configuration Function:
-
Call
Run_M1760_RT_Program_EEPROM
with the selected card index, module, and module ID.
-
-
Exit Strategy:
-
Prompt user to quit or restart the application.
-
Close all open cards using
naiapp_access_CloseAllOpenCards
.
-
Core Function: Run_M1760_RT_Program_EEPROM
-
Get 1553 RT Configuration:
-
Get Remote Terminal channel and address using
Get1553RTCfg
. -
Get Logical Device Number using
Get1553LogicalDevNum
.
-
-
Open and Initialize Device:
-
naibrd_1553_Open
: Open the 1553 RT channel. -
naibrd_1553_Initialize
: Initialize the RT channel.
-
-
Read and Configure RT Address:
-
naibrd_1553_RtGetAddress
: Read current RT address. -
naibrd_1553_RtDataBlockCreate
andnaibrd_1553_RtDataBlockMapToSubaddress
: Create and map data blocks for subaddresses 2 and 15 for Rx, and subaddress 23 for Tx.
-
-
Buffer Handling:
-
Load data into Tx Buffer.
-
-
Start and Stop RT:
-
naibrd_1553_RtStart
andnaibrd_1553_RtStop
: Manage RT state.
-
-
EEPROM Configuration:
-
Unlock EEPROM using
naibrd_1760_SetEEPROMUnlock
. -
Copy configuration to EEPROM using
naibrd_1760_SetEECOPY
. -
Wait for EEPROM completion using
naibrd_1760_GetDeviceReady
.
-
-
Free Device:
-
Free resources allocated for the device using
naibrd_1553_Free
.
-
Definitions and Enumerations
-
Boolean Type:
-
bool_t
: A custom Boolean type. -
Module Functions:
-
Get1553RTCfg
,Get1553LogicalDevNum
: Utility functions to get 1553 configuration parameters. -
naiapp_RunBoardMenu
,naiapp_query_CardIndex
,naiapp_query_ModuleNumber
: Functions for board and module querying. -
naibrd_GetModuleCount
,naibrd_GetModuleID
: Functions to get the number of modules and module ID.
Usage of the Code
This code is aimed at users familiar with embedded systems, MIL-STD-1553 communication, and NAI’s SSK. It provides a structured method to configure RT parameters and store these configurations in EEPROM for persistent use. The code ensures proper initialization, buffer handling, and cleanup to maintain system stability and reliability.
This summary should give users a comprehensive insight into how the provided code works and how they can leverage it to configure their own NAI embedded function modules.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <time.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"
/* Common 1553 Sample Program include files */
#include "nai_1553_utils.h"
/* naibrd include files */
#include "nai.h"
#include "naibrd.h"
#include "functions/naibrd_1553.h"
#include "functions/naibrd_1760.h"
static const int8_t *CONFIG_FILE = (int8_t *)"default_1760_RTProgramEEPROM.txt";
/* Function prototypes */
static bool_t Run_M1760_RT_Program_EEPROM(int32_t cardIndex, int32_t module, uint32_t modid);
#define DBLK_SA2 1
#define DBLK_SA15 2
#define DBLK_SA23_1 3
#define DBLK_SA23_2 4
#define DEF_RT_CHANNEL 1
#define DEF_RT_DEV_NUM 1
#define DEF_RT_ADDRESS 1
/**************************************************************************************************************/
/**
<summary>
The purpose of the M1760_RT_Program_EEPROM is to illustrate the methods to call in the naibrd library to configure
the FTJ or FTK channel as a Remote Terminal, create buffers and legalize Subaddresses 2 and 15 for Rx messages,
create buffers and legalize Subaddress 23 for Tx messages and copy the current configuration to the module EEPROM
so that when the module is rebooted, the channel will auto-initialize with this configuration. With this configuration,
the channel will respond with clear status (within 100 ms from power-up) to Rx messages on subaddresses 2 and 15.
Additionally, the channel will respond with clear status and data to Tx messages on subaddress 23.
This application demonstrates the usage of the following naibrd 1553 routines.
- naibrd_1553_GetChannelCount
- naibrd_1553_Open
- naibrd_1553_Initialize
- naibrd_1553_RtGetAddress
- naibrd_1553_RtDataBlockCreate
- naibrd_1553_RtDataBlockMapToSubaddress
- naibrd_1553_RtDataBlockWrite
- naibrd_1553_RtStart
- naibrd_1553_RtStop
- naibrd_1760_SetEEPROMUnlock
- naibrd_1760_SetEECOPY
- naibrd_1760_GetDeviceReady
- naibrd_1553_Free
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 1553 routines.
- ConfigDevice
- DisplayDeviceCfg
- GetBoardSNModCfg
- CheckModule
</summary>
*/
/**************************************************************************************************************/
#if defined (__VXWORKS__)
int32_t M1760_RT_Program_EEPROM(void)
#else
int32_t main(void)
#endif
{
bool_t bQuit = FALSE;
int32_t cardIndex = -1;
int32_t module = 0;
int32_t moduleCount = 0;
uint32_t modid = 0u;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
if (naiapp_RunBoardMenu(CONFIG_FILE) == TRUE)
{
while (!bQuit)
{
bQuit = naiapp_query_CardIndex(naiapp_GetBoardCnt(), 0, &cardIndex);
bQuit = naibrd_GetModuleCount(cardIndex, &moduleCount);
bQuit = naiapp_query_ModuleNumber(moduleCount, 1, &module);
modid = naibrd_GetModuleID(cardIndex, module);
}
bQuit = Run_M1760_RT_Program_EEPROM(cardIndex, module, modid);
printf("Type %c to exit program or Enter key to restart application: ", NAI_QUIT_CHAR);
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
}
naiapp_access_CloseAllOpenCards();
return 0;
}
static bool_t Run_M1760_RT_Program_EEPROM(int32_t cardIndex, int32_t module, uint32_t modid)
{
/* Variables */
bool_t bQuit = FALSE;
int32_t rtchan;
uint8_t rtaddr;
int16_t DevNum = 0;
int32_t swResult;
uint16_t usData;
uint16_t txData[32];
int32_t i;
bool_t ready;
/* Get Card, Module, Channel Numbers and Open a Handle */
bQuit = Get1553RTCfg(modid, DEF_RT_CHANNEL, DEF_RT_ADDRESS, &rtchan, &rtaddr);
if (bQuit)
{
return bQuit;
}
/* Get Logical Device # */
bQuit = Get1553LogicalDevNum(DEF_RT_DEV_NUM, &DevNum);
if (bQuit)
{
return bQuit;
}
/* Associate Card, Module and Channel Numbers with the Logical Device # */
swResult = naibrd_1553_Open(cardIndex, module, rtchan, DevNum);
if (swResult)
{
bQuit = TRUE;
printf("Error: naibrd_1553_Open %d", swResult);
return bQuit;
}
/* Initialize Device */
swResult = naibrd_1553_Initialize(DevNum, NAI_1553_ACCESS_CARD, NAI_1553_MODE_RT, 0, 0, 0);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_Initialize %d", swResult);
return bQuit;
}
/* Read RT Address */
swResult = naibrd_1553_RtGetAddress(DevNum, &usData);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtGetAddress %d", swResult);
return bQuit;
}
printf("RT Address set to %d\n\n", usData);
/* Create a Rx Double Buffer and map to subaddress 2 */
swResult = naibrd_1553_RtDataBlockCreate(DevNum, DBLK_SA2, NAI_1553_RT_DATABLOCK_DOUBLE, NULL, 0);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtDataBlockCreate %d", swResult);
return bQuit;
}
swResult = naibrd_1553_RtDataBlockMapToSubaddress(DevNum, DBLK_SA2, 2, NAI_1553_RT_MESSAGE_TYPE_RX, 0, 1);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtDataBlockMapToSubaddress %d", swResult);
return bQuit;
}
/* Create a Rx Double Buffer and map to subaddress 15 */
swResult = naibrd_1553_RtDataBlockCreate(DevNum, DBLK_SA15, NAI_1553_RT_DATABLOCK_DOUBLE, NULL, 0);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtDataBlockCreate %d", swResult);
return bQuit;
}
swResult = naibrd_1553_RtDataBlockMapToSubaddress(DevNum, DBLK_SA15, 15, NAI_1553_RT_MESSAGE_TYPE_RX, 0, 1);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtDataBlockMapToSubaddress %d", swResult);
return bQuit;
}
/* Create two Tx Single Buffers and map the first buffer to subaddress 23. The second buffer may be used during operation */
/* for software-controlled ping pong buffering between the first and second buffer */
swResult = naibrd_1553_RtDataBlockCreate(DevNum, DBLK_SA23_1, NAI_1553_RT_DATABLOCK_SINGLE_32, NULL, 0);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtDataBlockCreate %d", swResult);
return bQuit;
}
swResult = naibrd_1553_RtDataBlockCreate(DevNum, DBLK_SA23_2, NAI_1553_RT_DATABLOCK_SINGLE_32, NULL, 0);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtDataBlockCreate %d", swResult);
return bQuit;
}
swResult = naibrd_1553_RtDataBlockMapToSubaddress(DevNum, DBLK_SA23_1, 23, NAI_1553_RT_MESSAGE_TYPE_TX, 0, 1);
if (swResult < 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtDataBlockMapToSubaddress %d", swResult);
return bQuit;
}
/* Load the Tx Buffer with data that will be requested from the Bus Controller */
for (i = 0; i < 32; i++)
{
txData[i] = (uint16_t)(0xBEEF + i);
}
swResult = naibrd_1553_RtDataBlockWrite(DevNum, DBLK_SA23_1, txData, 32, 0);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtDataBlockWrite %d", swResult);
return bQuit;
}
/* Start RT */
swResult = naibrd_1553_RtStart(DevNum);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtStart %d", swResult);
return bQuit;
}
/* Stop RT - For EEPROM copy to work, the RT must not be running. */
swResult = naibrd_1553_RtStop(DevNum);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_RtStop %d", swResult);
return bQuit;
}
/* Copy the RT configuration to EEPROM for auto-initialization */
swResult = naibrd_1760_SetEEPROMUnlock(DevNum);
if (swResult != NAI_SUCCESS)
{
printf("Error: naibrd_1760_SetEEPROMUnlock %d", swResult);
return TRUE;
}
printf("\nSerial EEPROM Unlock Successful.");
swResult = naibrd_1760_SetEECOPY(DevNum, TRUE);
if (swResult != NAI_SUCCESS)
{
printf("Error: naibrd_1760_SetEECOPY %d", swResult);
}
printf("\nDriving EECOPY input high.");
nai_msDelay(10);
swResult = naibrd_1760_SetEECOPY(DevNum, FALSE);
if (swResult != NAI_SUCCESS)
{
printf("Error: naibrd_1760_SetEECOPY %d", swResult);
return TRUE;
}
printf("\nDriving EECOPY input low.");
printf("\nWait until READY output goes high for EEPROM copy completion...");
do
{
nai_msDelay(1000);
swResult = naibrd_1760_GetDeviceReady(DevNum, &ready);
} while (!ready);
printf("Copy Complete\n");
/* Free 1553 Device */
swResult = naibrd_1553_Free(DevNum);
if (swResult != 0)
{
bQuit = TRUE;
printf("Error: naibrd_1553_Free %d", swResult);
return bQuit;
}
return bQuit;
}