IgH EtherCAT Master  1.6.2
sync.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH
4  *
5  * This file is part of the IgH EtherCAT Master.
6  *
7  * The IgH EtherCAT Master is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version 2, as
9  * published by the Free Software Foundation.
10  *
11  * The IgH EtherCAT Master is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with the IgH EtherCAT Master; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  ****************************************************************************/
21 
26 /****************************************************************************/
27 
28 #include "globals.h"
29 #include "slave.h"
30 #include "master.h"
31 #include "pdo.h"
32 #include "sync.h"
33 
34 /****************************************************************************/
35 
39  ec_sync_t *sync,
40  ec_slave_t *slave
41  )
42 {
43  sync->slave = slave;
44  sync->physical_start_address = 0x0000;
45  sync->default_length = 0x0000;
46  sync->control_register = 0x00;
47  sync->enable = 0x00;
48  ec_pdo_list_init(&sync->pdos);
49 }
50 
51 /****************************************************************************/
52 
56  ec_sync_t *sync,
57  const ec_sync_t *other
58  )
59 {
60  sync->slave = other->slave;
62  sync->default_length = other->default_length;
63  sync->control_register = other->control_register;
64  sync->enable = other->enable;
65  ec_pdo_list_init(&sync->pdos);
66  ec_pdo_list_copy(&sync->pdos, &other->pdos);
67 }
68 
69 /****************************************************************************/
70 
74  ec_sync_t *sync
75  )
76 {
77  ec_pdo_list_clear(&sync->pdos);
78 }
79 
80 /****************************************************************************/
81 
87  const ec_sync_t *sync,
88  uint8_t sync_index,
89  uint16_t data_size,
90  const ec_sync_config_t *sync_config,
91  uint8_t pdo_xfer,
93  uint8_t *data
94  )
95 {
96  // enable only if (SII enable is set or PDO xfer)
97  // and size is > 0 and SM is not virtual
98  uint16_t enable = ((sync->enable & 0x01) || pdo_xfer)
99  && data_size
100  && ((sync->enable & 0x04) == 0);
101  uint8_t control = sync->control_register;
102 
103  if (sync_config) {
104 
105  switch (sync_config->dir) {
106  case EC_DIR_OUTPUT:
107  case EC_DIR_INPUT:
108  EC_WRITE_BIT(&control, 2,
109  sync_config->dir == EC_DIR_OUTPUT ? 1 : 0);
110  EC_WRITE_BIT(&control, 3, 0);
111  break;
112  default:
113  break;
114  }
115 
116  switch (sync_config->watchdog_mode) {
117  case EC_WD_ENABLE:
118  case EC_WD_DISABLE:
119  EC_WRITE_BIT(&control, 6,
120  sync_config->watchdog_mode == EC_WD_ENABLE);
121  break;
122  default:
123  break;
124  }
125  }
126 
127  EC_SLAVE_DBG(sync->slave, 1, "SM%u: Addr 0x%04X, Size %3u,"
128  " Ctrl 0x%02X, En %u\n",
129  sync_index, sync->physical_start_address,
130  data_size, control, enable);
131 
133  EC_WRITE_U16(data + 2, data_size);
134  EC_WRITE_U8 (data + 4, control);
135  EC_WRITE_U8 (data + 5, 0x00); // status byte (read only)
136  EC_WRITE_U16(data + 6, enable);
137 }
138 
139 /****************************************************************************/
140 
146  ec_sync_t *sync,
147  const ec_pdo_t *pdo
148  )
149 {
150  return ec_pdo_list_add_pdo_copy(&sync->pdos, pdo);
151 }
152 
153 /****************************************************************************/
154 
160  const ec_sync_t *sync
161  )
162 {
163  switch ((sync->control_register & 0x0C) >> 2) {
164  case 0x0: return EC_DIR_INPUT;
165  case 0x1: return EC_DIR_OUTPUT;
166  default: return EC_DIR_INVALID;
167  }
168 }
169 
170 /****************************************************************************/
#define EC_SLAVE_DBG(slave, level, fmt, args...)
Convenience macro for printing slave-specific debug messages to syslog.
Definition: slave.h:98
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:169
EtherCAT slave structure.
void ec_pdo_list_clear(ec_pdo_list_t *pl)
PDO list destructor.
Definition: pdo_list.c:53
#define EC_WRITE_U8(DATA, VAL)
Write an 8-bit unsigned value to EtherCAT data.
Definition: ecrt.h:3137
Disable the watchdog.
Definition: ecrt.h:520
Enable the watchdog.
Definition: ecrt.h:519
ec_direction_t ec_sync_default_direction(const ec_sync_t *sync)
Determines the default direction from the control register.
Definition: sync.c:159
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:86
Global definitions and macros.
EtherCAT master structure.
EtherCAT slave.
Definition: slave.h:168
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:145
PDO description.
Definition: pdo.h:41
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:133
Sync manager.
Definition: sync.h:39
#define EC_WRITE_U16(DATA, VAL)
Write a 16-bit unsigned value to EtherCAT data.
Definition: ecrt.h:3154
ec_direction_t
Direction type for PDO assignment functions.
Definition: ecrt.h:504
ec_slave_t * slave
Slave, the sync manager belongs to.
Definition: sync.h:40
uint8_t control_register
Control register value.
Definition: sync.h:43
Values read by the master.
Definition: ecrt.h:507
ec_direction_t dir
Sync manager direction.
Definition: sync_config.h:39
void ec_sync_clear(ec_sync_t *sync)
Destructor.
Definition: sync.c:73
ec_watchdog_mode_t watchdog_mode
Watchdog mode.
Definition: sync_config.h:40
uint8_t enable
Enable bit.
Definition: sync.h:44
Invalid direction.
Definition: ecrt.h:505
Sync manager configuration.
Definition: sync_config.h:38
#define EC_WRITE_BIT(DATA, POS, VAL)
Write a certain bit of an EtherCAT data byte.
Definition: ecrt.h:2960
EtherCAT sync manager.
ec_pdo_list_t pdos
Current PDO assignment.
Definition: sync.h:45
void ec_pdo_list_init(ec_pdo_list_t *pl)
PDO list constructor.
Definition: pdo_list.c:42
uint16_t physical_start_address
Physical start address.
Definition: sync.h:41
void ec_sync_init_copy(ec_sync_t *sync, const ec_sync_t *other)
Copy constructor.
Definition: sync.c:55
uint16_t default_length
Data length in bytes.
Definition: sync.h:42
Values written by the master.
Definition: ecrt.h:506
void ec_sync_init(ec_sync_t *sync, ec_slave_t *slave)
Constructor.
Definition: sync.c:38
EtherCAT Process data object structure.