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