IgH EtherCAT Master  1.5.3
sync.c
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * $Id$
4  *
5  * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH
6  *
7  * This file is part of the IgH EtherCAT Master.
8  *
9  * The IgH EtherCAT Master is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version 2, as
11  * published by the Free Software Foundation.
12  *
13  * The IgH EtherCAT Master 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 General
16  * Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with the IgH EtherCAT Master; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * ---
23  *
24  * The license mentioned above concerns the source code only. Using the
25  * EtherCAT technology and brand is only permitted in compliance with the
26  * industrial property and similar rights of Beckhoff Automation GmbH.
27  *
28  *****************************************************************************/
29 
34 /*****************************************************************************/
35 
36 #include "globals.h"
37 #include "slave.h"
38 #include "master.h"
39 #include "pdo.h"
40 #include "sync.h"
41 
42 /*****************************************************************************/
43 
47  ec_sync_t *sync,
48  ec_slave_t *slave
49  )
50 {
51  sync->slave = slave;
52  sync->physical_start_address = 0x0000;
53  sync->default_length = 0x0000;
54  sync->control_register = 0x00;
55  sync->enable = 0x00;
56  ec_pdo_list_init(&sync->pdos);
57 }
58 
59 /*****************************************************************************/
60 
64  ec_sync_t *sync,
65  const ec_sync_t *other
66  )
67 {
68  sync->slave = other->slave;
70  sync->default_length = other->default_length;
71  sync->control_register = other->control_register;
72  sync->enable = other->enable;
73  ec_pdo_list_init(&sync->pdos);
74  ec_pdo_list_copy(&sync->pdos, &other->pdos);
75 }
76 
77 /*****************************************************************************/
78 
82  ec_sync_t *sync
83  )
84 {
85  ec_pdo_list_clear(&sync->pdos);
86 }
87 
88 /*****************************************************************************/
89 
95  const ec_sync_t *sync,
96  uint8_t sync_index,
97  uint16_t data_size,
98  const ec_sync_config_t *sync_config,
99  uint8_t pdo_xfer,
101  uint8_t *data
102  )
103 {
104  // enable only if (SII enable is set or PDO xfer)
105  // and size is > 0 and SM is not virtual
106  uint16_t enable = ((sync->enable & 0x01) || pdo_xfer)
107  && data_size
108  && ((sync->enable & 0x04) == 0);
109  uint8_t control = sync->control_register;
110 
111  if (sync_config) {
112 
113  switch (sync_config->dir) {
114  case EC_DIR_OUTPUT:
115  case EC_DIR_INPUT:
116  EC_WRITE_BIT(&control, 2,
117  sync_config->dir == EC_DIR_OUTPUT ? 1 : 0);
118  EC_WRITE_BIT(&control, 3, 0);
119  break;
120  default:
121  break;
122  }
123 
124  switch (sync_config->watchdog_mode) {
125  case EC_WD_ENABLE:
126  case EC_WD_DISABLE:
127  EC_WRITE_BIT(&control, 6,
128  sync_config->watchdog_mode == EC_WD_ENABLE);
129  break;
130  default:
131  break;
132  }
133  }
134 
135  EC_SLAVE_DBG(sync->slave, 1, "SM%u: Addr 0x%04X, Size %3u,"
136  " Ctrl 0x%02X, En %u\n",
137  sync_index, sync->physical_start_address,
138  data_size, control, enable);
139 
141  EC_WRITE_U16(data + 2, data_size);
142  EC_WRITE_U8 (data + 4, control);
143  EC_WRITE_U8 (data + 5, 0x00); // status byte (read only)
144  EC_WRITE_U16(data + 6, enable);
145 }
146 
147 /*****************************************************************************/
148 
154  ec_sync_t *sync,
155  const ec_pdo_t *pdo
156  )
157 {
158  return ec_pdo_list_add_pdo_copy(&sync->pdos, pdo);
159 }
160 
161 /*****************************************************************************/
162 
168  const ec_sync_t *sync
169  )
170 {
171  switch ((sync->control_register & 0x0C) >> 2) {
172  case 0x0: return EC_DIR_INPUT;
173  case 0x1: return EC_DIR_OUTPUT;
174  default: return EC_DIR_INVALID;
175  }
176 }
177 
178 /*****************************************************************************/
#define EC_SLAVE_DBG(slave, level, fmt, args...)
Convenience macro for printing slave-specific debug messages to syslog.
Definition: slave.h:106
int ec_pdo_list_copy(ec_pdo_list_t *pl, const ec_pdo_list_t *other)
Makes a deep copy of another PDO list.
Definition: pdo_list.c:177
EtherCAT slave structure.
void ec_pdo_list_clear(ec_pdo_list_t *pl)
PDO list destructor.
Definition: pdo_list.c:61
#define EC_WRITE_U8(DATA, VAL)
Write an 8-bit unsigned value to EtherCAT data.
Definition: ecrt.h:2401
Disable the watchdog.
Definition: ecrt.h:462
Enable the watchdog.
Definition: ecrt.h:461
ec_direction_t ec_sync_default_direction(const ec_sync_t *sync)
Determines the default direction from the control register.
Definition: sync.c:167
void ec_sync_page(const ec_sync_t *sync, uint8_t sync_index, uint16_t data_size, const ec_sync_config_t *sync_config, uint8_t pdo_xfer, uint8_t *data)
Initializes a sync manager configuration page.
Definition: sync.c:94
Global definitions and macros.
EtherCAT master structure.
EtherCAT slave.
Definition: slave.h:176
int ec_sync_add_pdo(ec_sync_t *sync, const ec_pdo_t *pdo)
Adds a PDO to the list of known mapped PDOs.
Definition: sync.c:153
PDO description.
Definition: pdo.h:49
int ec_pdo_list_add_pdo_copy(ec_pdo_list_t *pl, const ec_pdo_t *pdo)
Add the copy of an existing PDO to the list.
Definition: pdo_list.c:141
Sync manager.
Definition: sync.h:47
#define EC_WRITE_U16(DATA, VAL)
Write a 16-bit unsigned value to EtherCAT data.
Definition: ecrt.h:2418
ec_direction_t
Direction type for PDO assignment functions.
Definition: ecrt.h:446
ec_slave_t * slave
Slave, the sync manager belongs to.
Definition: sync.h:48
uint8_t control_register
Control register value.
Definition: sync.h:51
Values read by the master.
Definition: ecrt.h:449
ec_direction_t dir
Sync manager direction.
Definition: sync_config.h:47
void ec_sync_clear(ec_sync_t *sync)
Destructor.
Definition: sync.c:81
ec_watchdog_mode_t watchdog_mode
Watchdog mode.
Definition: sync_config.h:48
uint8_t enable
Enable bit.
Definition: sync.h:52
Invalid direction.
Definition: ecrt.h:447
Sync manager configuration.
Definition: sync_config.h:46
#define EC_WRITE_BIT(DATA, POS, VAL)
Write a certain bit of an EtherCAT data byte.
Definition: ecrt.h:2228
EtherCAT sync manager.
ec_pdo_list_t pdos
Current PDO assignment.
Definition: sync.h:53
void ec_pdo_list_init(ec_pdo_list_t *pl)
PDO list constructor.
Definition: pdo_list.c:50
uint16_t physical_start_address
Physical start address.
Definition: sync.h:49
void ec_sync_init_copy(ec_sync_t *sync, const ec_sync_t *other)
Copy constructor.
Definition: sync.c:63
uint16_t default_length
Data length in bytes.
Definition: sync.h:50
Values written by the master.
Definition: ecrt.h:448
void ec_sync_init(ec_sync_t *sync, ec_slave_t *slave)
Constructor.
Definition: sync.c:46
EtherCAT Process data object structure.