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 |
|
71
|
23 |
|
0
|
24 |
#include "data.h"
|
|
25 |
#include "sync.h"
|
|
26 |
|
71
|
27 |
/* Prototypes for internals functions */
|
53
|
28 |
void SyncAlarm(CO_Data* d, UNS32 id);
|
|
29 |
UNS32 OnCOB_ID_SyncUpdate(CO_Data* d, const indextable * unsused_indextable,
|
|
30 |
UNS8 unsused_bSubindex);
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
/*****************************************************************************/
|
0
|
35 |
void SyncAlarm(CO_Data* d, UNS32 id)
|
|
36 |
{
|
|
37 |
sendSYNC(d, *d->COB_ID_Sync & 0x1FFFFFFF) ;
|
|
38 |
}
|
|
39 |
|
53
|
40 |
/*****************************************************************************/
|
71
|
41 |
/* This is called when Index 0x1005 is updated.*/
|
0
|
42 |
UNS32 OnCOB_ID_SyncUpdate(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
|
|
43 |
{
|
|
44 |
startSYNC(d);
|
31
|
45 |
return 0;
|
0
|
46 |
}
|
|
47 |
|
53
|
48 |
/*****************************************************************************/
|
0
|
49 |
void startSYNC(CO_Data* d)
|
|
50 |
{
|
|
51 |
RegisterSetODentryCallBack(d, 0x1005, 0, &OnCOB_ID_SyncUpdate);
|
|
52 |
RegisterSetODentryCallBack(d, 0x1006, 0, &OnCOB_ID_SyncUpdate);
|
|
53 |
|
|
54 |
if(d->syncTimer != TIMER_NONE){
|
|
55 |
stopSYNC(d);
|
|
56 |
}
|
|
57 |
|
|
58 |
if(*d->COB_ID_Sync & 0x40000000 && *d->Sync_Cycle_Period)
|
|
59 |
{
|
|
60 |
d->syncTimer = SetAlarm(
|
|
61 |
d,
|
71
|
62 |
0 /*No id needed*/,
|
0
|
63 |
&SyncAlarm,
|
|
64 |
US_TO_TIMEVAL(*d->Sync_Cycle_Period),
|
|
65 |
US_TO_TIMEVAL(*d->Sync_Cycle_Period));
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
53
|
69 |
/*****************************************************************************/
|
0
|
70 |
void stopSYNC(CO_Data* d)
|
|
71 |
{
|
|
72 |
d->syncTimer = DelAlarm(d->syncTimer);
|
|
73 |
}
|
|
74 |
|
|
75 |
/*********************************************************************/
|
|
76 |
UNS8 sendSYNC(CO_Data* d, UNS32 cob_id)
|
|
77 |
{
|
|
78 |
Message m;
|
|
79 |
UNS8 resultat ;
|
|
80 |
|
|
81 |
MSG_WAR(0x3001, "sendSYNC ", 0);
|
|
82 |
|
|
83 |
m.cob_id.w = cob_id ;
|
|
84 |
m.rtr = NOT_A_REQUEST;
|
|
85 |
m.len = 0;
|
|
86 |
resultat = (*d->canSend)(&m) ;
|
|
87 |
proceedSYNC(d, &m) ;
|
|
88 |
return resultat ;
|
|
89 |
}
|
|
90 |
|
|
91 |
/*****************************************************************************/
|
|
92 |
UNS8 proceedSYNC(CO_Data* d, Message *m)
|
|
93 |
{
|
|
94 |
|
71
|
95 |
UNS8 pdoNum, /* number of the actual processed pdo-nr. */
|
|
96 |
prp_j;
|
|
97 |
|
|
98 |
const UNS8 * pMappingCount = NULL; /* count of mapped objects...*/
|
|
99 |
/* pointer to the var which is mapped to a pdo */
|
|
100 |
/* void * pMappedAppObject = NULL; */
|
|
101 |
/* pointer fo the var which holds the mapping parameter of an mapping entry */
|
0
|
102 |
UNS32 * pMappingParameter = NULL;
|
71
|
103 |
/* pointer to the transmissiontype...*/
|
0
|
104 |
UNS8 * pTransmissionType = NULL;
|
|
105 |
UNS32 * pwCobId = NULL;
|
|
106 |
|
|
107 |
UNS8 * pSize;
|
|
108 |
UNS8 size;
|
|
109 |
UNS8 dataType;
|
|
110 |
UNS16 index;
|
|
111 |
UNS8 subIndex;
|
|
112 |
UNS8 offset;
|
|
113 |
UNS8 status;
|
|
114 |
UNS8 sizeData;
|
|
115 |
UNS32 objDict;
|
|
116 |
UNS16 offsetObjdict;
|
|
117 |
UNS16 offsetObjdictMap;
|
|
118 |
UNS16 lastIndex;
|
71
|
119 |
|
|
120 |
pSize = &size;
|
|
121 |
status = state3;
|
|
122 |
pdoNum = 0x00;
|
|
123 |
prp_j = 0x00;
|
|
124 |
offset = 0x00;
|
|
125 |
|
|
126 |
MSG_WAR(0x3002, "SYNC received. Proceed. ", 0);
|
|
127 |
|
0
|
128 |
/* only operational state allows PDO transmission */
|
|
129 |
if( d->nodeState != Operational )
|
|
130 |
return 0;
|
|
131 |
|
|
132 |
(*d->post_sync)();
|
|
133 |
|
|
134 |
/* So, the node is in operational state */
|
|
135 |
/* study all PDO stored in the objects dictionary */
|
|
136 |
|
|
137 |
offsetObjdict = d->firstIndex->PDO_TRS;
|
|
138 |
lastIndex = d->lastIndex->PDO_TRS;
|
|
139 |
offsetObjdictMap = d->firstIndex->PDO_TRS_MAP;
|
|
140 |
|
|
141 |
if(offsetObjdict) while( offsetObjdict <= lastIndex) {
|
|
142 |
switch( status ) {
|
|
143 |
|
|
144 |
case state3: /* get the PDO transmission type */
|
|
145 |
if (d->objdict[offsetObjdict].bSubCount <= 2) {
|
|
146 |
MSG_ERR(0x1004, "Subindex 2 not found at index ", 0x1800 + pdoNum);
|
|
147 |
return 0xFF;
|
|
148 |
}
|
|
149 |
pTransmissionType = d->objdict[offsetObjdict].pSubindex[2].pObject;
|
|
150 |
MSG_WAR(0x3005, "Reading PDO at index : ", 0x1800 + pdoNum);
|
|
151 |
status = state4;
|
|
152 |
break;
|
|
153 |
case state4: /* check if transmission type is after (this) SYNC */
|
|
154 |
/* The message may not be transmited every SYNC but every n SYNC */
|
|
155 |
if( (*pTransmissionType >= TRANS_SYNC_MIN) && (*pTransmissionType <= TRANS_SYNC_MAX) &&
|
|
156 |
(++d->count_sync[pdoNum] == *pTransmissionType) ) {
|
|
157 |
d->count_sync[pdoNum] = 0;
|
|
158 |
MSG_WAR(0x3007, " PDO is on SYNCHRO. Trans type : ", *pTransmissionType);
|
|
159 |
status = state5;
|
|
160 |
break;
|
|
161 |
}
|
|
162 |
else {
|
|
163 |
MSG_WAR(0x3008, " Not on synchro or not at this SYNC. Trans type : ",
|
|
164 |
*pTransmissionType);
|
|
165 |
pdoNum++;
|
|
166 |
offsetObjdict++;
|
|
167 |
offsetObjdictMap++;
|
|
168 |
status = state11;
|
|
169 |
break;
|
|
170 |
}
|
|
171 |
case state5: /* get PDO CobId */
|
|
172 |
pwCobId = d->objdict[offsetObjdict].pSubindex[1].pObject;
|
|
173 |
MSG_WAR(0x3009, " PDO CobId is : ", *pwCobId);
|
|
174 |
status = state7;
|
|
175 |
break;
|
|
176 |
case state7: /* get mapped objects number to transmit with this PDO */
|
|
177 |
pMappingCount = d->objdict[offsetObjdictMap].pSubindex[0].pObject;
|
|
178 |
MSG_WAR(0x300D, " Number of objects mapped : ",*pMappingCount );
|
|
179 |
status = state8;
|
|
180 |
case state8: /* get mapping parameters */
|
|
181 |
pMappingParameter = d->objdict[offsetObjdictMap].pSubindex[prp_j + 1].pObject;
|
|
182 |
MSG_WAR(0x300F, " got mapping parameter : ", *pMappingParameter);
|
|
183 |
MSG_WAR(0x3050, " at index : ", 0x1A00 + pdoNum);
|
|
184 |
MSG_WAR(0x3051, " sub-index : ", prp_j + 1);
|
|
185 |
status = state9;
|
|
186 |
|
|
187 |
case state9: /* get data to transmit */
|
|
188 |
index = (UNS16)((*pMappingParameter) >> 16);
|
|
189 |
subIndex = (UNS8)(( (*pMappingParameter) >> (UNS8)8 ) & (UNS32)0x000000FF);
|
71
|
190 |
/* <<3 because in *pMappingParameter the size is in bits */
|
0
|
191 |
sizeData = (UNS8) ((*pMappingParameter & (UNS32)0x000000FF) >> 3) ;
|
|
192 |
|
|
193 |
objDict = getODentry(d, index, subIndex, (void *)&d->process_var.data[offset], &sizeData, &dataType, 0 );
|
|
194 |
|
|
195 |
if( objDict != OD_SUCCESSFUL ){
|
|
196 |
MSG_ERR(0x1013, " Couldn't find mapped variable at index-subindex-size : ", (UNS16)(*pMappingParameter));
|
|
197 |
return 0xFF;
|
|
198 |
}
|
|
199 |
|
|
200 |
offset += sizeData ;
|
|
201 |
d->process_var.count = offset;
|
|
202 |
prp_j++;
|
|
203 |
status = state10;
|
|
204 |
break;
|
|
205 |
|
|
206 |
case state10: /* loop to get all the data to transmit */
|
|
207 |
if( prp_j < *pMappingCount ){
|
|
208 |
MSG_WAR(0x3014, " next variable mapped : ", prp_j);
|
|
209 |
status = state8;
|
|
210 |
break;
|
|
211 |
}
|
|
212 |
else {
|
|
213 |
MSG_WAR(0x3015, " End scan mapped variable", 0);
|
|
214 |
PDOmGR( d, *pwCobId );
|
|
215 |
MSG_WAR(0x3016, " End of this pdo. Should have been sent", 0);
|
|
216 |
pdoNum++;
|
|
217 |
offsetObjdict++;
|
|
218 |
offsetObjdictMap++;
|
|
219 |
offset = 0x00;
|
|
220 |
prp_j = 0x00;
|
|
221 |
status = state11;
|
|
222 |
break;
|
|
223 |
}
|
|
224 |
|
|
225 |
case state11:
|
|
226 |
MSG_WAR(0x3017, "next pdo index : ", pdoNum);
|
|
227 |
status = state3;
|
|
228 |
break;
|
|
229 |
|
|
230 |
default:
|
|
231 |
MSG_ERR(0x1019,"Unknown state has been reached : %d",status);
|
|
232 |
return 0xFF;
|
71
|
233 |
}/* end switch case */
|
0
|
234 |
|
71
|
235 |
}/* end while( prp_i<dict_cstes.max_count_of_PDO_transmit ) */
|
0
|
236 |
|
|
237 |
(*d->post_TPDO)();
|
|
238 |
|
|
239 |
return 0;
|
|
240 |
}
|
|
241 |
|
|
242 |
|