]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - src/sync.c
Size verification of the mapped variables in PDOs (both reception and transmission)
[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
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) ;
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     RegisterSetODentryCallBack(d, 0x1005, 0, NULL);
114     RegisterSetODentryCallBack(d, 0x1006, 0, NULL);
115         d->syncTimer = DelAlarm(d->syncTimer);
116 }
117
118
119 /*!                                                                                                
120 **                                                                                                 
121 **                                                                                                 
122 ** @param d                                                                                        
123 ** @param cob_id                                                                                   
124 **                                                                                                 
125 ** @return                                                                                         
126 **/  
127 UNS8 sendSYNCMessage(CO_Data* d)
128 {
129   Message m;
130   
131   MSG_WAR(0x3001, "sendSYNC ", 0);
132   
133   m.cob_id.w = *d->COB_ID_Sync & 0x1FFFFFFF;
134   m.rtr = NOT_A_REQUEST;
135   m.len = 0;
136   
137   return canSend(d->canHandle,&m);
138 }
139
140
141 /*!                                                                                                
142 **                                                                                                 
143 **                                                                                                 
144 ** @param d                                                                                        
145 ** @param cob_id                                                                                   
146 **                                                                                                 
147 ** @return                                                                                         
148 **/  
149 UNS8 sendSYNC(CO_Data* d)
150 {
151   UNS8 res;
152   res = sendSYNCMessage(d);
153   proceedSYNC(d) ; 
154   return res ;
155 }
156
157 /*!                                                                                                
158 **                                                                                                 
159 **                                                                                                 
160 ** @param d                                                                                        
161 ** @param m                                                                                        
162 **                                                                                                 
163 ** @return                                                                                         
164 **/ 
165 UNS8 proceedSYNC(CO_Data* d)
166 {
167
168   UNS8 res;
169   
170   MSG_WAR(0x3002, "SYNC received. Proceed. ", 0);
171   
172   (*d->post_sync)();
173
174   /* only operational state allows PDO transmission */
175   if(! d->CurrentCommunicationState.csPDO) 
176     return 0;
177
178   res = _sendPDOevent(d, 1 /*isSyncEvent*/ );
179   
180   /*Call user app callback*/
181   (*d->post_TPDO)();
182   
183   return res;
184   
185 }
186
187
188 void _post_sync(){}
189 void _post_TPDO(){}