NNP STM Generic Remote Module git-main
Loading...
Searching...
No Matches
emcy.c
Go to the documentation of this file.
1
13#include <data.h>
14#include "emcy.h"
15#include "canfestival.h"
16#include "sysdep.h"
17
18
19
20UNS32 OnNumberOfErrorsUpdate(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex);
21UNS8 sendEMCY(CO_Data* d, UNS16 errCode, UNS8 errRegister);
22
23
32UNS32 OnNumberOfErrorsUpdate(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
33{
34 UNS8 index;
35 // if 0, reset Pre-defined Error Field
36 // else, don't change and give an abort message (eeror code: 0609 0030h)
37 if (*d->error_number == 0)
38 for (index = 0; index < d->error_history_size; ++index)
39 *(d->error_first_element + index) = 0; /* clear all the fields in Pre-defined Error Field (1003h) */
40 else
41 ;// abort message
42 return 0;
43}
44
51{
53
54 *d->error_number = 0;
55}
56
63{
64
65}
66
75UNS8 sendEMCY(CO_Data* d, UNS16 errCode, UNS8 errRegister)
76{
77 Message m;
78
79 MSG_WAR(0x3051, "sendEMCY", 0);
80
81 m.cob_id = UNS16_LE(*(UNS32*)d->error_cobid);
82 m.rtr = NOT_A_REQUEST;
83 m.len = 8;
84 m.data[0] = errCode & 0xFF; /* LSB */
85 m.data[1] = (errCode >> 8) & 0xFF; /* MSB */
86 m.data[2] = errRegister;
87 m.data[3] = 0; /* Manufacturer specific Error Field still not implemented */
88 m.data[4] = 0;
89 m.data[5] = 0;
90 m.data[6] = 0;
91 m.data[7] = 0;
92
93 return canSend(d->canHandle, &m);
94}
95
105UNS8 EMCY_setError(CO_Data* d, UNS16 errCode, UNS8 errRegMask, UNS16 addInfo)
106{
107 UNS8 index;
108 UNS8 errRegister_tmp;
109
110 for (index = 0; index < EMCY_MAX_ERRORS; ++index)
111 {
112 if (d->error_data[index].errCode == errCode) /* error already registered */
113 {
114 if (d->error_data[index].active)
115 {
116 MSG_WAR(0x3052, "EMCY message already sent", 0);
117 return 0;
118 } else d->error_data[index].active = 1; /* set as active error */
119 break;
120 }
121 }
122
123 if (index == EMCY_MAX_ERRORS) /* if errCode not already registered */
124 for (index = 0; index < EMCY_MAX_ERRORS; ++index) if (d->error_data[index].active == 0) break; /* find first inactive error */
125
126 if (index == EMCY_MAX_ERRORS) /* error_data full */
127 {
128 MSG_ERR(0x3053, "error_data full", 0);
129 return 1;
130 }
131
132 d->error_data[index].errCode = errCode;
133 d->error_data[index].errRegMask = errRegMask;
134 d->error_data[index].active = 1;
135
136 /* set the new state in the error state machine */
137 d->error_state = Error_occurred;
138
139 /* set Error Register (1001h) */
140 for (index = 0, errRegister_tmp = 0; index < EMCY_MAX_ERRORS; ++index)
141 if (d->error_data[index].active == 1) errRegister_tmp |= d->error_data[index].errRegMask;
142 *d->error_register = errRegister_tmp;
143
144 /* set Pre-defined Error Field (1003h) */
145 for (index = d->error_history_size - 1; index > 0; --index)
146 *(d->error_first_element + index) = *(d->error_first_element + index - 1);
147 *(d->error_first_element) = errCode | ((UNS32)addInfo << 16);
148 if(*d->error_number < d->error_history_size) ++(*d->error_number);
149
150 /* send EMCY message */
151 if (d->CurrentCommunicationState.csEmergency)
152 return sendEMCY(d, errCode, *d->error_register);
153 else return 1;
154}
155
163{
164 UNS8 index;
165 UNS8 errRegister_tmp;
166 UNS8 anyActiveError = 0;
167
168 for (index = 0; index < EMCY_MAX_ERRORS; ++index)
169 if (d->error_data[index].errCode == errCode) break; /* find the position of the error */
170
171
172 if ((index != EMCY_MAX_ERRORS) && (d->error_data[index].active == 1))
173 {
174 d->error_data[index].active = 0;
175
176 /* set Error Register (1001h) and check error state machine */
177 for (index = 0, errRegister_tmp = 0; index < EMCY_MAX_ERRORS; ++index)
178 if (d->error_data[index].active == 1)
179 {
180 anyActiveError = 1;
181 errRegister_tmp |= d->error_data[index].errRegMask;
182 }
183 if(anyActiveError == 0)
184 {
185 d->error_state = Error_free;
186 /* send a EMCY message with code "Error Reset or No Error" */
187 if (d->CurrentCommunicationState.csEmergency)
188 sendEMCY(d, 0x0000, 0x00);
189 }
190 *d->error_register = errRegister_tmp;
191 }
192 else
193 MSG_WAR(0x3054, "recovered error was not active", 0);
194}
195
203{
204 UNS8 nodeID;
205 UNS16 errCode;
206 UNS8 errReg;
207
208 MSG_WAR(0x3055, "EMCY received. Proceed. ", 0);
209
210 /* Test if the size of the EMCY is ok */
211 if ( m->len != 8) {
212 MSG_ERR(0x1056, "Error size EMCY. CobId : ", m->cob_id);
213 return;
214 }
215
216 /* post the received EMCY */
217 nodeID = UNS16_LE(m->cob_id) & 0x7F;
218 errCode = m->data[0] | ((UNS16)m->data[1] << 8);
219 errReg = m->data[2];
220 (*d->post_emcy)(d, nodeID, errCode, errReg);
221}
222
231void _post_emcy(CO_Data* d, UNS8 nodeID, UNS16 errCode, UNS8 errReg){}
#define UNS8
Unsigned int8 representation in CANFest.
Definition applicfg.h:25
#define UNS16
Unsigned int16 representation in CANFest.
Definition applicfg.h:26
#define MSG_WAR(num, str, val)
Definition of MSG_WAR.
Definition applicfg.h:64
#define UNS32
Unsigned int32 representation in CANFest.
Definition applicfg.h:27
#define MSG_ERR(num, str, val)
Definition of MSG_ERR.
Definition applicfg.h:52
UNS8 EMCY_setError(CO_Data *d, UNS16 errCode, UNS8 errRegMask, UNS16 addInfo)
Sets a new error with code errCode. Also sets corresponding bits in Error register (1001h)
Definition emcy.c:105
UNS32 OnNumberOfErrorsUpdate(CO_Data *d, const indextable *unsused_indextable, UNS8 unsused_bSubindex)
This is called when Index 0x1003 is updated.
Definition emcy.c:32
void proceedEMCY(CO_Data *d, Message *m)
This function is responsible to process an EMCY canopen-message.
Definition emcy.c:202
void EMCY_errorRecovered(CO_Data *d, UNS16 errCode)
Indicates it has recovered from error errCode. Also clears corresponding bits in Error register (1001...
Definition emcy.c:162
UNS8 sendEMCY(CO_Data *d, UNS16 errCode, UNS8 errRegister)
<BRIEF> sendEMCY
Definition emcy.c:75
void _post_emcy(CO_Data *d, UNS8 nodeID, UNS16 errCode, UNS8 errReg)
<BRIEF> _post_emcy
Definition emcy.c:231
void emergencyStop(CO_Data *d)
Stop EMCY producer and consumer.
Definition emcy.c:62
void emergencyInit(CO_Data *d)
Start EMCY consumer and producer.
Definition emcy.c:50
UNS32 RegisterSetODentryCallBack(CO_Data *d, UNS16 wIndex, UNS8 bSubindex, ODCallback_t Callback)
<BRIEF>
Definition objacces.c:357
Definition can.h:13
UNS16 cob_id
Definition can.h:14
UNS8 data[8]
Definition can.h:17
UNS8 rtr
Definition can.h:15
UNS8 len
Definition can.h:16
This structure contains all necessary informations to define a CANOpen node.
Definition data.h:44