252
|
1 |
/*
|
|
2 |
This file is part of CanFestival, a library implementing CanOpen Stack.
|
|
3 |
|
|
4 |
|
|
5 |
Copyright (C): Edouard TISSERANT and Francis DUPIN
|
|
6 |
|
|
7 |
|
|
8 |
See COPYING file for copyrights details.
|
|
9 |
|
|
10 |
|
|
11 |
This library is free software; you can redistribute it and/or
|
|
12 |
modify it under the terms of the GNU Lesser General Public
|
|
13 |
License as published by the Free Software Foundation; either
|
|
14 |
version 2.1 of the License, or (at your option) any later version.
|
|
15 |
|
|
16 |
|
|
17 |
This library is distributed in the hope that it will be useful,
|
|
18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20 |
Lesser General Public License for more details.
|
|
21 |
|
|
22 |
|
|
23 |
You should have received a copy of the GNU Lesser General Public
|
|
24 |
License along with this library; if not, write to the Free Software
|
|
25 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
26 |
*/
|
|
27 |
|
|
28 |
|
|
29 |
/*!
|
|
30 |
** @file sync.c
|
|
31 |
** @author Edouard TISSERANT and Francis DUPIN
|
|
32 |
** @date Tue Jun 5 09:32:32 2007
|
|
33 |
**
|
|
34 |
** @brief
|
|
35 |
**
|
|
36 |
**
|
|
37 |
*/
|
|
38 |
|
|
39 |
#include "data.h"
|
|
40 |
#include "sync.h"
|
|
41 |
#include "canfestival.h"
|
|
42 |
|
|
43 |
/* Prototypes for internals functions */
|
|
44 |
|
|
45 |
/*!
|
|
46 |
**
|
|
47 |
**
|
|
48 |
** @param d
|
|
49 |
** @param id
|
|
50 |
**/
|
|
51 |
void SyncAlarm(CO_Data* d, UNS32 id);
|
|
52 |
UNS32 OnCOB_ID_SyncUpdate(CO_Data* d, const indextable * unsused_indextable,
|
|
53 |
UNS8 unsused_bSubindex);
|
|
54 |
|
|
55 |
/*!
|
|
56 |
**
|
|
57 |
**
|
|
58 |
** @param d
|
|
59 |
** @param id
|
|
60 |
**/
|
|
61 |
void SyncAlarm(CO_Data* d, UNS32 id)
|
|
62 |
{
|
|
63 |
sendSYNC(d, *d->COB_ID_Sync & 0x1FFFFFFF) ;
|
|
64 |
}
|
|
65 |
|
|
66 |
/*!
|
|
67 |
** This is called when Index 0x1005 is updated.
|
|
68 |
**
|
|
69 |
** @param d
|
|
70 |
** @param unsused_indextable
|
|
71 |
** @param unsused_bSubindex
|
|
72 |
**
|
|
73 |
** @return
|
|
74 |
**/
|
|
75 |
UNS32 OnCOB_ID_SyncUpdate(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
|
|
76 |
{
|
|
77 |
startSYNC(d);
|
|
78 |
return 0;
|
|
79 |
}
|
|
80 |
|
|
81 |
/*!
|
|
82 |
**
|
|
83 |
**
|
|
84 |
** @param d
|
|
85 |
**/
|
|
86 |
void startSYNC(CO_Data* d)
|
|
87 |
{
|
|
88 |
RegisterSetODentryCallBack(d, 0x1005, 0, &OnCOB_ID_SyncUpdate);
|
|
89 |
RegisterSetODentryCallBack(d, 0x1006, 0, &OnCOB_ID_SyncUpdate);
|
|
90 |
|
|
91 |
if(d->syncTimer != TIMER_NONE){
|
|
92 |
stopSYNC(d);
|
|
93 |
}
|
|
94 |
|
|
95 |
if(*d->COB_ID_Sync & 0x40000000 && *d->Sync_Cycle_Period)
|
|
96 |
{
|
|
97 |
d->syncTimer = SetAlarm(
|
|
98 |
d,
|
|
99 |
0 /*No id needed*/,
|
|
100 |
&SyncAlarm,
|
|
101 |
US_TO_TIMEVAL(*d->Sync_Cycle_Period),
|
|
102 |
US_TO_TIMEVAL(*d->Sync_Cycle_Period));
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
/*!
|
|
107 |
**
|
|
108 |
**
|
|
109 |
** @param d
|
|
110 |
**/
|
|
111 |
void stopSYNC(CO_Data* d)
|
|
112 |
{
|
|
113 |
d->syncTimer = DelAlarm(d->syncTimer);
|
|
114 |
}
|
|
115 |
|
|
116 |
/*!
|
|
117 |
**
|
|
118 |
**
|
|
119 |
** @param d
|
|
120 |
** @param cob_id
|
|
121 |
**
|
|
122 |
** @return
|
|
123 |
**/
|
|
124 |
UNS8 sendSYNC(CO_Data* d, UNS32 cob_id)
|
|
125 |
{
|
|
126 |
Message m;
|
|
127 |
UNS8 resultat ;
|
|
128 |
|
|
129 |
MSG_WAR(0x3001, "sendSYNC ", 0);
|
|
130 |
|
|
131 |
m.cob_id.w = cob_id ;
|
|
132 |
m.rtr = NOT_A_REQUEST;
|
|
133 |
m.len = 0;
|
|
134 |
resultat = canSend(d->canHandle,&m) ;
|
|
135 |
proceedSYNC(d, &m) ;
|
|
136 |
return resultat ;
|
|
137 |
}
|
|
138 |
|
|
139 |
/*!
|
|
140 |
**
|
|
141 |
**
|
|
142 |
** @param d
|
|
143 |
** @param m
|
|
144 |
**
|
|
145 |
** @return
|
|
146 |
**/
|
|
147 |
UNS8 proceedSYNC(CO_Data* d, Message *m)
|
|
148 |
{
|
|
149 |
|
|
150 |
UNS8 res;
|
|
151 |
|
|
152 |
MSG_WAR(0x3002, "SYNC received. Proceed. ", 0);
|
|
153 |
|
|
154 |
(*d->post_sync)();
|
|
155 |
|
|
156 |
/* only operational state allows PDO transmission */
|
|
157 |
if(! d->CurrentCommunicationState.csPDO)
|
|
158 |
return 0;
|
|
159 |
|
|
160 |
res = _sendPDOevent(d, 1 /*isSyncEvent*/ );
|
|
161 |
|
|
162 |
/*Call user app callback*/
|
|
163 |
(*d->post_TPDO)();
|
|
164 |
|
|
165 |
return res;
|
|
166 |
|
|
167 |
}
|
|
168 |
|
|
169 |
|
|
170 |
void _post_sync(){}
|
|
171 |
void _post_TPDO(){}
|