CANopen Master Documentation
Version 6.08.00
Loading...
Searching...
No Matches
com_demo_nmt.c

//====================================================================================================================//
// File: com_demo_nmt.c //
// Description: Examples for NMT service in CANopen Master stack //
// //
// Copyright (C) MicroControl GmbH & Co. KG //
// 53844 Troisdorf - Germany //
// www.microcontrol.net //
// //
//--------------------------------------------------------------------------------------------------------------------//
// The copyright to the computer program(s) herein is the property of MicroControl GmbH & Co. KG, Germany. The //
// program(s) may be used and/or copied only with the written permission of MicroControl GmbH & Co. KG or in //
// accordance with the terms and conditions stipulated in the agreement/contract under which the program(s) have //
// been supplied. //
// //
//====================================================================================================================//
/*--------------------------------------------------------------------------------------------------------------------*\
** Include files **
** **
\*--------------------------------------------------------------------------------------------------------------------*/
//------------------------------------------------------------------------------------------------------
// CANopen Master demo header
//
#include "com_demo.h"
//------------------------------------------------------------------------------------------------------
// Miscellaneous header
//
#include "mc_fifo.h"
/*--------------------------------------------------------------------------------------------------------------------*\
** Definitions **
** **
\*--------------------------------------------------------------------------------------------------------------------*/
#define DEMO_NMT_FIFO_SIZE 16
#define DEMO_NMT_STRING_SIZE 80
/*--------------------------------------------------------------------------------------------------------------------*\
** Variables of module **
** **
\*--------------------------------------------------------------------------------------------------------------------*/
static McFifoCtrl_ts atsNmtFifoS[COM_NET_MAX];
static uint16_t atsNmtDataS[COM_NET_MAX][DEMO_NMT_FIFO_SIZE];
static uint8_t atsNmtNodeS[COM_NET_MAX];
/*--------------------------------------------------------------------------------------------------------------------*\
** Functions **
** **
\*--------------------------------------------------------------------------------------------------------------------*/
//--------------------------------------------------------------------------------------------------------------------//
// ComDemoNmtConfigurationDone() //
// //
//--------------------------------------------------------------------------------------------------------------------//
void ComDemoNmtConfigurationDone(uint8_t ubNetV, uint8_t ubNodeIdV)
{
(void) ubNodeIdV;
atsNmtNodeS[ubNetV] = 0;
}
//--------------------------------------------------------------------------------------------------------------------//
// ComDemoNmtInit() //
// //
//--------------------------------------------------------------------------------------------------------------------//
{
McFifoInit(&atsNmtFifoS[ubNetV], &atsNmtDataS[ubNetV][0], sizeof(uint16_t), DEMO_NMT_FIFO_SIZE);
return (eCOM_ERR_NONE);
}
//--------------------------------------------------------------------------------------------------------------------//
// ComDemoNmtProcess() //
// //
//--------------------------------------------------------------------------------------------------------------------//
{
uint16_t uwFifoValueT = 0;
uint16_t *puwFifoEntryT;
if (atsNmtNodeS[ubNetV] == 0)
{
if (McFifoIsEmpty(&atsNmtFifoS[ubNetV]) == 0)
{
//-----------------------------------------------------------------------------------
// Read NMT state and node-id from FIFO
//
puwFifoEntryT = McFifoDataOutPtr(&atsNmtFifoS[ubNetV]);
uwFifoValueT = *puwFifoEntryT;
McFifoIncOut(&atsNmtFifoS[ubNetV]);
uint8_t ubNodeIdT = (uint8_t) uwFifoValueT;
uint8_t ubNmtStateT = (uint8_t) (uwFifoValueT >> 8);
switch (ubNmtStateT)
{
atsNmtNodeS[ubNetV] = ubNodeIdT;
ComDemoNodeIdentify(ubNetV, ubNodeIdT);
break;
break;
break;
default:
break;
}
}
}
return (eCOM_ERR_NONE);
}
//--------------------------------------------------------------------------------------------------------------------//
// ComDemoNmtQueueStateChange() //
// //
//--------------------------------------------------------------------------------------------------------------------//
void ComDemoNmtQueueStateChange(uint8_t ubNetV, uint8_t ubNodeIdV, uint8_t ubNmtStateV)
{
uint16_t uwFifoValueT = 0;
uint16_t *puwFifoEntryT;
uwFifoValueT = ubNmtStateV;
uwFifoValueT = uwFifoValueT << 8;
uwFifoValueT = uwFifoValueT + ubNodeIdV;
if (McFifoIsFull(&atsNmtFifoS[ubNetV]) == 0)
{
//------------------------------------------------------------------------------------------------
// Push NMT state and node-id to FIFO
//
puwFifoEntryT = McFifoDataInPtr(&atsNmtFifoS[ubNetV]);
*puwFifoEntryT = uwFifoValueT;
McFifoIncIn(&atsNmtFifoS[ubNetV]);
}
}
//--------------------------------------------------------------------------------------------------------------------//
// ComDemoNmtStartNodes() //
// Start all nodes in the network //
//--------------------------------------------------------------------------------------------------------------------//
ComStatus_tv ComDemoNmtStartNodes(uint8_t ubNetV)
{
ComStatus_tv tvStatusT;
//---------------------------------------------------------------------------------------------------
// set NMT inhibit time to 500 ms
//
tvStatusT = ComNmtSetInhibit(eCOM_NET_1, 500000);
if(tvStatusT != eCOM_ERR_NONE)
{
return(tvStatusT);
}
//---------------------------------------------------------------------------------------------------
// send "Reset all Nodes" command
//
//---------------------------------------------------------------------------------------------------
// now send "Operational" command, wait until inhibit time is over
//
do {
} while (tvStatusT == eCOM_ERR_SERVICE_MODE);
return (tvStatusT);
}
//--------------------------------------------------------------------------------------------------------------------//
// ComDemoNmtStateString() //
// Print node state in text buffer //
//--------------------------------------------------------------------------------------------------------------------//
char * ComDemoNmtStateString(uint8_t ubNetV, uint8_t ubNodeIdV)
{
static char szNmtStateStringS[DEMO_NMT_STRING_SIZE];
uint8_t ubNodeStateT;
ComStatus_tv tvStatusT;
tvStatusT = ComNmtGetNodeState(ubNetV, ubNodeIdV, &ubNodeStateT);
if (tvStatusT != eCOM_ERR_NONE)
{
snprintf(szNmtStateStringS, DEMO_NMT_STRING_SIZE, "COM-ERR: %d", (int) tvStatusT);
return (szNmtStateStringS);
}
switch(ubNodeStateT)
{
snprintf(szNmtStateStringS, DEMO_NMT_STRING_SIZE, "can%d: NID %03d - %s", ubNetV, ubNodeIdV, "Stopped ");
break;
snprintf(szNmtStateStringS, DEMO_NMT_STRING_SIZE, "can%d: NID %03d - %s", ubNetV, ubNodeIdV, "Pre-Operational");
break;
snprintf(szNmtStateStringS, DEMO_NMT_STRING_SIZE, "can%d: NID %03d - %s", ubNetV, ubNodeIdV, "Operational ");
break;
snprintf(szNmtStateStringS, DEMO_NMT_STRING_SIZE, "can%d: NID %03d - %s", ubNetV, ubNodeIdV, "Reset ");
break;
snprintf(szNmtStateStringS, DEMO_NMT_STRING_SIZE, "can%d: NID %03d - %s", ubNetV, ubNodeIdV, "Boot-up ");
break;
default:
snprintf(szNmtStateStringS, DEMO_NMT_STRING_SIZE, "can%d: NID %03d - %s", ubNetV, ubNodeIdV, "Unknown state ");
break;
}
return (szNmtStateStringS);
}
#define COM_NET_MAX
Definition com_conf.h:51
int32_t ComStatus_tv
Definition com_defs.h:116
@ eCOM_NET_1
Definition com_defs.h:728
@ eCOM_ERR_SERVICE_MODE
Definition com_defs.h:245
@ eCOM_ERR_NONE
Definition com_defs.h:155
Demo functions for CANopen Master protocol stack services .
ComStatus_tv ComDemoNodeIdentify(uint8_t ubNetV, uint8_t ubNodeIdV)
ComStatus_tv ComDemoNmtProcess(uint8_t ubNetV)
void ComDemoNmtQueueStateChange(uint8_t ubNetV, uint8_t ubNodeIdV, uint8_t ubNmtStateV)
void ComDemoNmtConfigurationDone(uint8_t ubNetV, uint8_t ubNodeIdV)
ComStatus_tv ComDemoNmtInit(uint8_t ubNetV)
char * ComDemoNmtStateString(uint8_t ubNetV, uint8_t ubNodeIdV)
ComStatus_tv ComNmtGetNodeState(uint8_t ubNetV, uint8_t ubNodeIdV, uint8_t *pubStateV)
@ eCOM_NMT_STATE_RESET_NODE
Definition com_nmt.h:92
@ eCOM_NMT_STATE_OPERATIONAL
Definition com_nmt.h:89
@ eCOM_NMT_STATE_BOOTUP
Definition com_nmt.h:98
@ eCOM_NMT_STATE_PREOPERATIONAL
Definition com_nmt.h:86
@ eCOM_NMT_STATE_RESET_COM
Definition com_nmt.h:95
@ eCOM_NMT_STATE_STOPPED
Definition com_nmt.h:83
ComStatus_tv ComNmtSetNodeState(uint8_t ubNetV, uint8_t ubNodeIdV, uint8_t ubStateV)
Set NMT state of CANopen device.
ComStatus_tv ComNmtSetInhibit(uint8_t ubNetV, uint32_t ulTimeV)
Set NMT message inhibit time.
Definitions and prototypes for FIFO implementation.
void * McFifoDataInPtr(McFifoCtrl_ts *ptsFifoV)
void * McFifoDataOutPtr(McFifoCtrl_ts *ptsFifoV)
void McFifoIncIn(McFifoCtrl_ts *ptsFifoV)
bool_t McFifoIsFull(McFifoCtrl_ts *ptsFifoV)
bool_t McFifoIsEmpty(McFifoCtrl_ts *ptsFifoV)
void McFifoInit(McFifoCtrl_ts *ptsFifoV, void *pvdDataV, uint32_t ulDataSizeV, uint32_t ulFifoSizeV)
void McFifoIncOut(McFifoCtrl_ts *ptsFifoV)
Definition mc_fifo.h:66




*/