0
|
1 |
/*
|
|
2 |
This file is part of CanFestival, a library implementing CanOpen Stack.
|
|
3 |
|
|
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN
|
|
5 |
|
|
6 |
See COPYING file for copyrights details.
|
|
7 |
|
|
8 |
This library is free software; you can redistribute it and/or
|
|
9 |
modify it under the terms of the GNU Lesser General Public
|
|
10 |
License as published by the Free Software Foundation; either
|
|
11 |
version 2.1 of the License, or (at your option) any later version.
|
|
12 |
|
|
13 |
This library is distributed in the hope that it will be useful,
|
|
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 |
Lesser General Public License for more details.
|
|
17 |
|
|
18 |
You should have received a copy of the GNU Lesser General Public
|
|
19 |
License along with this library; if not, write to the Free Software
|
|
20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 |
*/
|
|
22 |
|
|
23 |
#include <data.h>
|
|
24 |
#include "lifegrd.h"
|
|
25 |
|
71
|
26 |
/* Prototypes for internals functions */
|
53
|
27 |
void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id);
|
|
28 |
void ProducerHearbeatAlarm(CO_Data* d, UNS32 id);
|
|
29 |
|
|
30 |
|
|
31 |
/*****************************************************************************/
|
0
|
32 |
e_nodeState getNodeState (CO_Data* d, UNS8 nodeId)
|
|
33 |
{
|
|
34 |
e_nodeState networkNodeState = d->NMTable[nodeId];
|
|
35 |
return networkNodeState;
|
|
36 |
}
|
|
37 |
|
53
|
38 |
/*****************************************************************************/
|
71
|
39 |
/* The Consumer Timer Callback */
|
0
|
40 |
void ConsumerHearbeatAlarm(CO_Data* d, UNS32 id)
|
|
41 |
{
|
71
|
42 |
/*MSG_WAR(0x00, "ConsumerHearbeatAlarm", 0x00);*/
|
0
|
43 |
|
71
|
44 |
/* call heartbeat error with NodeId */
|
0
|
45 |
(*d->heartbeatError)((UNS8)( ((d->ConsumerHeartbeatEntries[id]) & (UNS32)0x00FF0000) >> (UNS8)16 ));
|
|
46 |
}
|
|
47 |
|
53
|
48 |
/*****************************************************************************/
|
0
|
49 |
void proceedNODE_GUARD(CO_Data* d, Message* m )
|
|
50 |
{
|
|
51 |
UNS8 nodeId = (UNS8) GET_NODE_ID((*m));
|
|
52 |
|
|
53 |
if((m->rtr == 1) ) /* Notice that only the master can have sent this node guarding request */
|
71
|
54 |
{ /* Receiving a NMT NodeGuarding (request of the state by the master) */
|
|
55 |
/* only answer to the NMT NodeGuarding request, the master is not checked (not implemented) */
|
0
|
56 |
if (nodeId == *d->bDeviceNodeId )
|
|
57 |
{
|
|
58 |
Message msg;
|
|
59 |
msg.cob_id.w = *d->bDeviceNodeId + 0x700;
|
|
60 |
msg.len = (UNS8)0x01;
|
|
61 |
msg.rtr = 0;
|
|
62 |
msg.data[0] = d->nodeState;
|
|
63 |
if (d->toggle)
|
|
64 |
{
|
|
65 |
msg.data[0] |= 0x80 ;
|
|
66 |
d->toggle = 0 ;
|
|
67 |
}
|
|
68 |
else
|
|
69 |
d->toggle = 1 ;
|
71
|
70 |
/* send the nodeguard response. */
|
0
|
71 |
MSG_WAR(0x3130, "Sending NMT Nodeguard to master, state: ", d->nodeState);
|
|
72 |
(*d->canSend)(&msg );
|
|
73 |
}
|
|
74 |
|
71
|
75 |
}else{ /* Not a request CAN */
|
0
|
76 |
|
|
77 |
MSG_WAR(0x3110, "Received NMT nodeId : ", nodeId);
|
|
78 |
/* the slave's state receievd is stored in the NMTable */
|
71
|
79 |
/* The state is stored on 7 bit */
|
0
|
80 |
d->NMTable[nodeId] = (e_nodeState) ((*m).data[0] & 0x7F) ;
|
|
81 |
|
|
82 |
/* Boot-Up frame reception */
|
|
83 |
if ( d->NMTable[nodeId] == Initialisation)
|
|
84 |
{
|
71
|
85 |
/* The device send the boot-up message (Initialisation) */
|
|
86 |
/* to indicate the master that it is entered in pre_operational mode */
|
|
87 |
/* Because the device enter automaticaly in pre_operational mode, */
|
|
88 |
/* the pre_operational mode is stored */
|
|
89 |
/* NMTable[bus_id][nodeId] = Pre_operational; */
|
0
|
90 |
MSG_WAR(0x3100, "The NMT is a bootup from node : ", nodeId);
|
|
91 |
}
|
|
92 |
|
|
93 |
if( d->NMTable[nodeId] != Unknown_state ) {
|
|
94 |
UNS8 index, ConsummerHeartBeat_nodeId ;
|
|
95 |
for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
|
|
96 |
{
|
|
97 |
ConsummerHeartBeat_nodeId = (UNS8)( ((d->ConsumerHeartbeatEntries[index]) & (UNS32)0x00FF0000) >> (UNS8)16 );
|
|
98 |
if ( nodeId == ConsummerHeartBeat_nodeId )
|
|
99 |
{
|
|
100 |
TIMEVAL time = ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ;
|
71
|
101 |
/* Renew alarm for next heartbeat. */
|
0
|
102 |
DelAlarm(d->ConsumerHeartBeatTimers[index]);
|
|
103 |
d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0);
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
53
|
110 |
/*****************************************************************************/
|
71
|
111 |
/* The Consumer Timer Callback */
|
0
|
112 |
void ProducerHearbeatAlarm(CO_Data* d, UNS32 id)
|
|
113 |
{
|
|
114 |
if(*d->ProducerHeartBeatTime)
|
|
115 |
{
|
|
116 |
Message msg;
|
71
|
117 |
/* Time expired, the heartbeat must be sent immediately
|
|
118 |
* generate the correct node-id: this is done by the offset 1792
|
|
119 |
* (decimal) and additionaly
|
|
120 |
* the node-id of this device.
|
|
121 |
*/
|
0
|
122 |
msg.cob_id.w = *d->bDeviceNodeId + 0x700;
|
|
123 |
msg.len = (UNS8)0x01;
|
|
124 |
msg.rtr = 0;
|
71
|
125 |
msg.data[0] = d->nodeState; /* No toggle for heartbeat !*/
|
|
126 |
/* send the heartbeat */
|
0
|
127 |
MSG_WAR(0x3130, "Producing heartbeat: ", d->nodeState);
|
|
128 |
(*d->canSend)(&msg );
|
|
129 |
}else{
|
|
130 |
d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer);
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
53
|
134 |
/*****************************************************************************/
|
0
|
135 |
void heartbeatInit(CO_Data* d)
|
|
136 |
{
|
71
|
137 |
UNS8 index; /* Index to scan the table of heartbeat consumers */
|
0
|
138 |
|
|
139 |
d->toggle = 0;
|
|
140 |
|
|
141 |
for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
|
|
142 |
{
|
|
143 |
TIMEVAL time = (UNS16) ( (d->ConsumerHeartbeatEntries[index]) & (UNS32)0x0000FFFF ) ;
|
71
|
144 |
/* MSG_WAR(0x3121, "should_time : ", should_time ) ; */
|
0
|
145 |
if ( time )
|
|
146 |
{
|
|
147 |
d->ConsumerHeartBeatTimers[index] = SetAlarm(d, index, &ConsumerHearbeatAlarm, MS_TO_TIMEVAL(time), 0);
|
|
148 |
}
|
|
149 |
}
|
|
150 |
|
|
151 |
if ( *d->ProducerHeartBeatTime )
|
|
152 |
{
|
|
153 |
TIMEVAL time = *d->ProducerHeartBeatTime;
|
|
154 |
d->ProducerHeartBeatTimer = SetAlarm(d, 0, &ProducerHearbeatAlarm, MS_TO_TIMEVAL(time), MS_TO_TIMEVAL(time));
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
53
|
158 |
/*****************************************************************************/
|
0
|
159 |
void heartbeatStop(CO_Data* d)
|
|
160 |
{
|
|
161 |
UNS8 index;
|
|
162 |
for( index = (UNS8)0x00; index < *d->ConsumerHeartbeatCount; index++ )
|
|
163 |
{
|
|
164 |
d->ConsumerHeartBeatTimers[index + 1] = DelAlarm(d->ConsumerHeartBeatTimers[index + 1]);;
|
|
165 |
}
|
|
166 |
|
|
167 |
d->ProducerHeartBeatTimer = DelAlarm(d->ProducerHeartBeatTimer);;
|
|
168 |
}
|
|
169 |
|