block_beckhoff_el2xxx.c 7.33 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
/*
2
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2009, 2011, 2013
Jeroen Vreeken's avatar
Jeroen Vreeken committed
3
	Copyright Stichting C.A. Muller Radioastronomiestation, 2009, 2011
4
	Copyright Daan Vreeken <Daan@Vitsch.nl> - Vitsch Electronics, 2010
Jeroen Vreeken's avatar
Jeroen Vreeken committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <stdlib.h>
22
#include <string.h> 
Jeroen Vreeken's avatar
Jeroen Vreeken committed
23
24
25
26
27
28

#include <controller/controller_block.h>

#include "block_beckhoff_el2xxx.h"
#include "ec.h"
#include "esc.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
29
30
#include "esc_esi.h"
#include "esc_id.h"
31
#include "log.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
32
33
34
35
36
37
38
39
40
41
42
43

struct controller_block_private {
	bool	*output1;
	bool	*output2;
	bool	*output3;
	bool	*output4;
	bool	*output5;
	bool	*output6;
	bool	*output7;
	bool	*output8;
	
	unsigned char *tx_buffer;
44
45

	struct esc_device *dev;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
};

static void calculate_8(struct controller_block *block)
{
	struct controller_block_private *priv = block->private;
	
	priv->tx_buffer[0] =
		*priv->output1 |
		(*priv->output2 << 1) |
		(*priv->output3 << 2) |
		(*priv->output4 << 3) |
		(*priv->output5 << 4) |
		(*priv->output6 << 5) |
		(*priv->output7 << 6) |
		(*priv->output8 << 7);

	ec_tx_pdo(controller_samplenr);
}

static void calculate_4(struct controller_block *block)
{
	struct controller_block_private *priv = block->private;
	
	priv->tx_buffer[0] =
		*priv->output1 |
		(*priv->output2 << 1) |
		(*priv->output3 << 2) |
		(*priv->output4 << 3);

	ec_tx_pdo(controller_samplenr);
}

static void calculate_2(struct controller_block *block)
{
	struct controller_block_private *priv = block->private;
	
	priv->tx_buffer[0] =
		*priv->output1 |
		(*priv->output2 << 1);

	ec_tx_pdo(controller_samplenr);
}

struct beckhoff_el2xxx_type {
	char *name;
	uint32_t productcode;
	int outputs;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
	struct controller_block_interm_list *interms;
};

static struct controller_block_interm_list interms_2[] = {
	{ "output1",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output1)  },
	{ "output2",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output2)  },
	{ NULL },
};

static struct controller_block_interm_list interms_4[] = {
	{ "output1",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output1)  },
	{ "output2",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output2)  },
	{ "output3",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output3)  },
	{ "output4",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output4)  },
	{ NULL },
};

static struct controller_block_interm_list interms_8[] = {
	{ "output1",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output1)  },
	{ "output2",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output2)  },
	{ "output3",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output3)  },
	{ "output4",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output4)  },
	{ "output5",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output5)  },
	{ "output6",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output6)  },
	{ "output7",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output7)  },
	{ "output8",  CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, output8)  },
	{ NULL },
Jeroen Vreeken's avatar
Jeroen Vreeken committed
120
121
122
};

static struct beckhoff_el2xxx_type devlist[] = {
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	{ "EL2002", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2002, 2, interms_2 },
	{ "EL2004", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2004, 4, interms_4 },
	{ "EL2008", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2008, 8, interms_8 },
	{ "EL2022", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2022, 2, interms_2 },
	{ "EL2024", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2024, 4, interms_4 },
	{ "EL2032", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2032, 2, interms_2 },
	{ "EL2034", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2034, 4, interms_4 },
	{ "EL2042", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2042, 2, interms_2 },
	{ "EL2084", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2084, 4, interms_4 },
	{ "EL2088", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2088, 8, interms_8 },
	{ "EL2124", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2124, 4, interms_4 },
	{ "EL2202", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2202, 2, interms_2 },
	{ "EL2252", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2252, 2, interms_2 },
	{ "EL2262", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2262, 2, interms_2 },
	{ "EL2602", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2602, 2, interms_2 },
	{ "EL2612", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2612, 2, interms_2 },
	{ "EL2622", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2622, 2, interms_2 },
	{ "EL2624", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2624, 4, interms_4 },
	{ "EL2712", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2712, 2, interms_2 },
	{ "EL2722", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2722, 2, interms_2 },
	{ "EL2732", ESC_ESI_PRODUCTCODE_BECKHOFF_EL2732, 2, interms_2 },
Jeroen Vreeken's avatar
Jeroen Vreeken committed
144
145
146
147
148
149
150
151
152
153
154
};

struct controller_block * block_beckhoff_el2xxx_create(char *name, va_list ap)
{
	struct controller_block *block;
	struct ec_dgram_addr addr;
	int i;
	int dev = -1;
	char *eltype;
	int devno;
	bool sim = !ec_initialized();
155
	struct timespec timeout = { 1, 0 };
Jeroen Vreeken's avatar
Jeroen Vreeken committed
156
157
158
159
160
161
162
163
164
165
166
		
	eltype = va_arg(ap, char *);
	devno = va_arg(ap, int);
	
	for (i = 0; i < sizeof(devlist)/sizeof(struct beckhoff_el2xxx_type); i++) {
		if (!strcmp(eltype, devlist[i].name)) {
			dev = i;
			break;
		}
	}
	if (dev < 0)
167
		goto err_ethercat;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
168
169
	
	if (!sim) {
170
171
172
173
174
175
176
177
		uint32_t vendor, product;
		
		ec_addr_set_auto_inc_nr(&addr, devno);
		vendor = esc_esi_vendorid_get(&addr);
		product = esc_esi_productcode_get(&addr);

		if (vendor != ESC_ESI_VENDORID_BECKHOFF ||
		    product != devlist[dev].productcode) {
178
179
			log_send(LOG_T_ERROR, "%s: Device not found\n", name);
			goto err_ethercat;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
180
181
182
		}
	}
	
183
184
185
	block = controller_block_alloc(
	    sim ? "beckhoff_el2xxx_sim" : "beckhoff_el2xxx",
	    name, sizeof(struct controller_block_private));
Jeroen Vreeken's avatar
Jeroen Vreeken committed
186
187

	if (!sim) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
188
189
190
		block->private->dev = esc_device_create(&addr, name);
		esc_esi_device_fill(block->private->dev);
		esc_device_initialize_pre_operational(block->private->dev);
191
192
	
		block->private->tx_buffer = block->private->dev->tx_data;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
193

194
195
196
197
		if (esc_al_state_set(&addr, ESC_AL_STATE_SAFE_OPERATIONAL, &timeout) < 0)
			log_send(LOG_T_ERROR, "Could not go to state safe operational\n");
		if (esc_al_state_set(&addr, ESC_AL_STATE_OPERATIONAL, &timeout) < 0)
			log_send(LOG_T_ERROR, "Could not go to state safe operational\n");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
198
199
200
201
		log_send(LOG_T_DEBUG, "%s: state: %d", 
		    name, esc_al_state_get(&addr));
		log_send(LOG_T_DEBUG, "%s: statuscode: %d", 
		    name, esc_al_status_code_get(&addr));
Jeroen Vreeken's avatar
Jeroen Vreeken committed
202
203
204
	}
	
	/* Outputs on the hardware are inputs on this software block */
205
206
	if (controller_block_interm_list_init(block, devlist[dev].interms))
		goto err_inputs;	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
207
208
209
210
211
212
213
214
215
216
	
	switch (devlist[dev].outputs) {
		case 2:
			block->calculate = calculate_2;
			break;
		case 4:
			block->calculate = calculate_4;
			break;
		case 8:
			block->calculate = calculate_8;
217
			break;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
218
219
220
221
222
223
	}
	if (sim)
		block->calculate = NULL;

	controller_block_add(block);
	return block;
224
225
226
227
228

err_inputs:
	controller_block_free(block);
err_ethercat:
	return NULL;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
229
}