]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - src/sync.c
Removed some warnings.
[CanFestival-3.git] / src / sync.c
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 #include "sysdep.h"
43
44 /* Prototypes for internals functions */
45
46 /*!                                                                                                
47 **                                                                                                 
48 **                                                                                                 
49 ** @param d                                                                                        
50 ** @param id                                                                                       
51 **/  
52 void SyncAlarm(CO_Data* d, UNS32 id);
53 UNS32 OnCOB_ID_SyncUpdate(CO_Data* d, const indextable * unsused_indextable, 
54         UNS8 unsused_bSubindex);
55
56 /*!                                                                                                
57 **                                                                                                 
58 **                                                                                                 
59 ** @param d                                                                                        
60 ** @param id                                                                                       
61 **/   
62 void SyncAlarm(CO_Data* d, UNS32 id)
63 {
64         sendSYNC(d) ;
65 }
66
67 /*!                                                                                                
68 ** This is called when Index 0x1005 is updated.                                                                                                
69 **                                                                                                 
70 ** @param d                                                                                        
71 ** @param unsused_indextable                                                                       
72 ** @param unsused_bSubindex                                                                        
73 **                                                                                                 
74 ** @return                                                                                         
75 **/  
76 UNS32 OnCOB_ID_SyncUpdate(CO_Data* d, const indextable * unsused_indextable, UNS8 unsused_bSubindex)
77 {
78         startSYNC(d);
79         return 0;
80 }
81
82 /*!                                                                                                
83 **                                                                                                 
84 **                                                                                                 
85 ** @param d                                                                                        
86 **/ 
87 void startSYNC(CO_Data* d)
88 {
89         if(d->syncTimer != TIMER_NONE){
90                 stopSYNC(d);
91         }
92
93         RegisterSetODentryCallBack(d, 0x1005, 0, &OnCOB_ID_SyncUpdate);
94         RegisterSetODentryCallBack(d, 0x1006, 0, &OnCOB_ID_SyncUpdate);
95
96         if(*d->COB_ID_Sync & 0x40000000ul && *d->Sync_Cycle_Period)
97         {
98                 d->syncTimer = SetAlarm(
99                                 d,
100                                 0 /*No id needed*/,
101                                 &SyncAlarm,
102                                 US_TO_TIMEVAL(*d->Sync_Cycle_Period), 
103                                 US_TO_TIMEVAL(*d->Sync_Cycle_Period));
104         }
105 }
106
107 /*!                                                                                                
108 **                                                                                                 
109 **                                                                                                 
110 ** @param d                                                                                        
111 **/   
112 void stopSYNC(CO_Data* d)
113 {
114     RegisterSetODentryCallBack(d, 0x1005, 0, NULL);
115     RegisterSetODentryCallBack(d, 0x1006, 0, NULL);
116         d->syncTimer = DelAlarm(d->syncTimer);
117 }
118
119
120 /*!                                                                                                
121 **                                                                                                 
122 **                                                                                                 
123 ** @param d                                                                                        
124 ** @param cob_id                                                                                   
125 **                                                                                                 
126 ** @return                                                                                         
127 **/  
128 UNS8 sendSYNCMessage(CO_Data* d)
129 {
130   Message m;
131   
132   MSG_WAR(0x3001, "sendSYNC ", 0);
133   
134   m.cob_id = UNS16_LE(*d->COB_ID_Sync);
135   m.rtr = NOT_A_REQUEST;
136   m.len = 0;
137   
138   return canSend(d->canHandle,&m);
139 }
140
141
142 /*!                                                                                                
143 **                                                                                                 
144 **                                                                                                 
145 ** @param d                                                                                        
146 ** @param cob_id                                                                                   
147 **                                                                                                 
148 ** @return                                                                                         
149 **/  
150 UNS8 sendSYNC(CO_Data* d)
151 {
152   UNS8 res;
153   res = sendSYNCMessage(d);
154   proceedSYNC(d) ; 
155   return res ;
156 }
157
158 /*!                                                                                                
159 **                                                                                                 
160 **                                                                                                 
161 ** @param d                                                                                        
162 ** @param m                                                                                        
163 **                                                                                                 
164 ** @return                                                                                         
165 **/ 
166 UNS8 proceedSYNC(CO_Data* d)
167 {
168
169   UNS8 res;
170   
171   MSG_WAR(0x3002, "SYNC received. Proceed. ", 0);
172   
173   (*d->post_sync)(d);
174
175   /* only operational state allows PDO transmission */
176   if(! d->CurrentCommunicationState.csPDO) 
177     return 0;
178
179   res = _sendPDOevent(d, 1 /*isSyncEvent*/ );
180   
181   /*Call user app callback*/
182   (*d->post_TPDO)(d);
183   
184   return res;
185   
186 }
187
188
189 void _post_sync(CO_Data* d){}
190 void _post_TPDO(CO_Data* d){}