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