SUM1553 RT PingPong
Edit this on GitLab
SUM1553 RT PingPong
Explanation
About the Sample Application Code
This sample application code is written in C and is designed to work with North Atlantic Industries (NAI) embedded function modules. Specifically, it demonstrates how to interact with a SUM1553 interface configured as a Remote Terminal (RT) on a 1553 communication bus. Below is an explanation of the code and its functionalities.
Included Libraries and Headers
The code starts by including various standard and NAI-specific libraries:
-
Standard libraries:
-
<stdio.h>
: Standard input and output library. -
<stdlib.h>
: General utility functions. -
<string.h>
: String handling functions. -
<time.h>
: Time-related functions. -
<ctype.h>
: Character handling functions. -
NAI-specific libraries:
-
naiapp_boardaccess_menu.h
,naiapp_boardaccess_query.h
,naiapp_boardaccess_access.h
,naiapp_boardaccess_display.h
,naiapp_boardaccess_utils.h
: These headers contain routines for configuring, accessing, and displaying information from NAI boards. -
nai_1553_utils.h
: Specific to handling 1553 communication. -
nai.h
,naibrd.h
,naibrd_sum1553.h
: Include functions related to the naibrd library and specific modules such as SUM1553.
Constants and Global Variables
-
CONFIG_FILE
: Defines the path to the configuration file. -
Default values for remote terminal (RT):
-
DEF_RT_CHANNEL
: Default channel number. -
DEF_RT_ADDRESS
: Default RT address. -
DEF_RT_SUBADDR
: Default subaddress. -
Global data block:
datablock
array to store transmit and receive data.
Main Function
The main()
function (or SUM1553_RT_PingPong()
in a VXWORKS environment) is the entry point. It controls the main loop to repeatedly configure the hardware, and if successful, invokes the Run_SUM1553_RT_PingPong()
function.
-
Initialization: The application initializes and offers a menu to select configuration from
CONFIG_FILE
. -
Card and Module Selection: It queries users to select a card index and module number.
-
Module Interaction: If a valid module ID is found,
Run_SUM1553_RT_PingPong()
is called for further operations. -
Exit Control: Provides options to quit or restart the application.
Function Run_SUM1553_RT_PingPong
This function configures the selected module to act as a Remote Terminal (RT) and manages the Ping Pong buffer mode for communication:
-
Configuration: It sets up the channel, configures it as an RT, enables Ping Pong buffer, and legalizes subaddresses for RX and TX.
-
Execution Loop: Runs an interactive loop where users can:
-
View BC-RT messages using
Retrieve_SUM1553_BCRTMessages
. -
Update RT-BC message data using
Update_SUM1553_RTSAXmitData
. -
Quit the routine.
-
-
Disable RT: Disables the RT execution when done.
Function Retrieve_SUM1553_BCRTMessages
This function reads BC-RT messages from the RT and displays the data:
-
For each subaddress: It checks if new data is present.
-
Data Retrieval: Reads the data block if new data is found.
-
Display: Prints the timestamp and data content of the received messages.
Function Update_SUM1553_RTSAXmitData
This function updates the RT-BC data in the specified subaddress:
-
Disable Ping Pong: Temporarily disables Ping Pong mode.
-
Update Data: Modifies the data block.
-
Re-enable Ping Pong: Restores Ping Pong mode.
This sample code demonstrates a typical workflow to configure and interact with a 1553 RT module in an embedded system, providing mechanisms for real-time communication and data handling.
#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"
/* Common 1553 Sample Program include files */
#include "nai_1553_utils.h"
/* naibrd include files */
#include "nai.h"
#include "naibrd.h"
#include "functions/naibrd_sum1553.h"
static const int8_t *CONFIG_FILE = (int8_t *)"default_SUM1553_RTPP.txt";
/* Function prototypes */
static bool_t Run_SUM1553_RT_PingPong(int32_t cardIndex, int32_t module, uint32_t modid);
static void Retrieve_SUM1553_BCRTMessages(int32_t cardIndex, int32_t module, int32_t rtchan);
static void Update_SUM1553_RTSAXmitData(int32_t cardIndex, int32_t module, int32_t rtchan, uint8_t increment, bool_t PingPongEnabled);
static const int32_t DEF_RT_CHANNEL = 1;
static const uint8_t DEF_RT_ADDRESS = 1;
static const uint8_t DEF_RT_SUBADDR = 2;
static uint16_t datablock[32][32];
/**************************************************************************************************************/
/**
<summary>
The purpose of the SUM1553_RT_PingPong is to illustrate the methods to call in the naibrd library to configure
the 1553 channel as a Remote Terminal and receiving BC-RT messages and sending responses to RT-BC messages.
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
Note, the SUM1553_RT_PingPong can run in conjunction with the SUM1553_BC_Scheduler applications to illustrate
BC and RT operations together with the NAI 1553 module.
</summary>
*/
/**************************************************************************************************************/
#if defined (__VXWORKS__)
int32_t SUM1553_RT_PingPong(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_SUM1553_RT_PingPong(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_SUM1553_RT_PingPong queries the user for the module and channel to configure as the Remote Terminal (RT).
After getting the module/channel selection, methods in the naibrd library are invoked to setup this channel
as a RT configured to run with Buffer Ping Pong.
Once the RT is configured and running, the user can:
1) Type the Enter key to view the BC-RT messages that are received from all subaddresses.
2) Type 'U' to change the data in the RT-BC message.
3) Type 'Q' to quit stop the RT execution and exit the routine.
</summary>
*/
/**************************************************************************************************************/
static bool_t Run_SUM1553_RT_PingPong(int32_t cardIndex, int32_t module, uint32_t modid)
{
bool_t bQuit;
bool_t bContinue = TRUE;
bool_t resetting = TRUE;
int32_t rtchan = 1;
uint8_t rtaddr = 1;
uint8_t increment = 0;
int32_t i, j;
int8_t inputBuffer[80];
int32_t inputResponseCnt;
bQuit = Get1553RTCfg(modid, DEF_RT_CHANNEL, DEF_RT_ADDRESS, &rtchan, &rtaddr);
if (!bQuit)
{
/* Reset RT channel */
check_status(naibrd_SUM1553_Reset(cardIndex,module,rtchan));
/* Wait for channels to reset */
while(check_status(naibrd_SUM1553_IsResetting(cardIndex,module,rtchan,&resetting)) == NAI_SUCCESS && resetting);
/* Configure RT */
check_status(naibrd_SUM1553_SetMode(cardIndex,module,rtchan,NAI_SUM1553_OPSTATUS_RT_MODE)); /* Set to RT mode */
check_status(naibrd_SUM1553_RT_SetBusEnable(cardIndex,module,rtchan,1,1)); /* Enable bus A and bus B */
check_status(naibrd_SUM1553_RT_SetAddress(cardIndex,module,rtchan,rtaddr)); /* Set RT address */
check_status(naibrd_SUM1553_RT_ConfigureForPingPong(cardIndex,module,rtchan)); /* Set up descriptor table with ping pong buffers */
check_status(naibrd_SUM1553_RT_SetPingPongEnable(cardIndex,module,rtchan,1)); /* Enable ping pong */
check_status(naibrd_SUM1553_RT_Legalize(cardIndex,module,rtchan,NAI_SUM1553_SAMCTYPE_SA|NAI_SUM1553_SAMCTYPE_RX,0xFFFFFFFF,1)); /* Legalize the subaddresses we want for rx */
check_status(naibrd_SUM1553_RT_Legalize(cardIndex,module,rtchan,NAI_SUM1553_SAMCTYPE_SA|NAI_SUM1553_SAMCTYPE_TX,0xFFFFFFFF,1)); /* Legalize the subaddresses we want for tx*/
/* Disable processor control */
check_status(naibrd_SUM1553_Proc_SetCtrl(cardIndex,module,rtchan,0));
/* Load the subaddress transmit data */
Update_SUM1553_RTSAXmitData(cardIndex,module,rtchan,0, FALSE);
for (i = 0; i < 32; i++)
for (j = 0; j < 32; j++)
datablock[i][j] = 0;
/* Run the RT */
check_status(naibrd_SUM1553_EnableExecution(cardIndex,module,rtchan,1));
}
if (bQuit)
bContinue = FALSE;
while (bContinue)
{
printf("\nType Enter key BC-RT to view BC-RT message or U to update the subaddress transmit data or %c to quit: ", NAI_QUIT_CHAR);
bQuit = naiapp_query_ForQuitResponse(sizeof(inputBuffer), NAI_QUIT_CHAR, inputBuffer, &inputResponseCnt);
if (!bQuit)
{
if (toupper(inputBuffer[0]) == 'U')
{
increment++;
Update_SUM1553_RTSAXmitData(cardIndex,module,rtchan,increment, TRUE);
}
else
{
Retrieve_SUM1553_BCRTMessages(cardIndex, module, rtchan);
}
}
else
bContinue = FALSE;
}
/* Disable the RT */
check_status(naibrd_SUM1553_EnableExecution(cardIndex,module,rtchan,0));
return bQuit;
}
/**************************************************************************************************************/
/**
<summary>
Retrieve_SUM1553_BCRTMessages reads the Control Word for the BC-RT (i.e. Receive) Subaddress. If the Block
Accessed (BAC) bit is set, indicating a new message, the Data block associated with the BC-RT message is
read and displayed.
</summary>
*/
/**************************************************************************************************************/
static void Retrieve_SUM1553_BCRTMessages(int32_t cardIndex, int32_t module, int32_t rtchan)
{
uint16_t ctrl;
uint16_t rxdatablock[32], rxwordcnt;
uint16_t timestamp;
uint8_t sa;
int32_t i;
for (sa = 0; sa < 32; sa++)
{
check_status(naibrd_SUM1553_RT_GetControlWord(cardIndex,module,rtchan,NAI_SUM1553_SAMCTYPE_SA|NAI_SUM1553_SAMCTYPE_RX,sa,&ctrl));
if (ctrl & NAI_SUM1553_RT_CTL_BAC) /* new data */
{
check_status(naibrd_SUM1553_RT_ReadRxDataBlock(cardIndex,module,rtchan,TRUE,sa,&rxwordcnt,×tamp,rxdatablock));
check_status(naibrd_SUM1553_RT_ClearBlockAccess(cardIndex,module,rtchan,NAI_SUM1553_SAMCTYPE_SA|NAI_SUM1553_SAMCTYPE_RX,sa));
if (rxwordcnt == 0)
rxwordcnt = 32;
printf("SA %d ", sa);
printf("TimeTag: %04X ", timestamp);
printf("Data: ");
for (i = 0; i < rxwordcnt; i++)
{
if (i != 0)
printf(",");
printf ("%04X", rxdatablock[i]);
}
printf("\n");
}
}
}
/**************************************************************************************************************/
/**
<summary>
Update_SUM1553_RTSAXmitData changes the data that is sent in response to the RT-BC messages sent to the
"DEF_RT_SUBADDR" Subaddress.
Note: To properly implement buffer servicing while the core is using the ping pong buffering scheme, the
host or subsystem needs to do the following:
- Disable the ping pong mode by setting PPEN (Ping Pong Enable) (Core Register 0, bit 2) to 0.
- Verify that ping pong mode has been disabled by querying bit MSGTO (Message Timeout) (Core Register 0, bit 9)
is set to 0.
- Service the secondary buffer
- Re-enable ping pong mode (setting PPEN) to 1.
- Verify that ping pong mode has been enabled by querying MSGTO to be set to 1.
</summary>
*/
/**************************************************************************************************************/
static void Update_SUM1553_RTSAXmitData(int32_t cardIndex, int32_t module, int32_t rtchan, uint8_t increment, bool_t bPingPongEnabled)
{
uint16_t sa = DEF_RT_SUBADDR;
uint16_t txdatablock[32];
uint8_t msb, lsb;
int32_t i;
bool_t enabled;
msb = (uint8_t)(sa << 4) + (increment & 0x0F); /* upper byte (subaddress=upper 4 bits | increment=lower 4 bits)) */
lsb = 0;
for (i = 0; i < 32; i++)
{
txdatablock[i] = (msb << 8) | (uint8_t)(lsb + i); /* incremental data */
}
/* Update the data */
if (bPingPongEnabled)
{
naibrd_SUM1553_RT_SetPingPongEnable(cardIndex, module, rtchan, 0);
/* Wait for ping pong to be disabled */
while(check_status(naibrd_SUM1553_RT_GetPingPongEnabled(cardIndex,module,rtchan,&enabled)) == NAI_SUCCESS && (enabled != 0));
}
check_status(naibrd_SUM1553_RT_LoadTxDataBlock(cardIndex,module,rtchan,TRUE, sa,txdatablock));
if (bPingPongEnabled)
{
naibrd_SUM1553_RT_SetPingPongEnable(cardIndex, module, rtchan, 1);
}
}