summaryrefslogtreecommitdiff
path: root/src/hal/drivers/pluto_servo.comp
blob: 1987783d6fd20e1de329bf7c11f2ca45a19675e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//    Copyright (C) 2007 Jeff Epler
//
//    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 2 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, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

component pluto_servo """Hardware driver and firmware for the Pluto-P parallel-port FPGA, for use with servo machines.""";

description """
Pluto_servo is a LinuxCNC software driver and associated firmware that allow the Pluto-P board to be used to control a servo-based CNC machine.

The driver has 4 PWM channels, 4 quadrature channels with index pulse, 18
digital outputs (8 shared with PWM), and 20 digital inputs (12 shared with
quadrature).

.SS Encoders
The encoder pins and parameters conform to the `canonical encoder' interface
described in the HAL manual.  It operates in `x4 mode'.

The sample rate of the encoder is 40MHz.  The maximum number quadrature rate is
8191 counts per LinuxCNC servo cycle.  For correct handling of the index pulse, the
number of encoder counts per revolution must be less than 8191.

.SS PWM
The PWM pins and parameters conform to the `canonical analog output' interface
described in the HAL manual.  The output pins are `up/down' or `pwm/dir'
pins as described in the documentation of the `pwmgen' component.

Internally the PWM generator is based on a 12-bit, 40MHz counter, giving 4095
duty cycles from -100% to +100% and a frequency of approximately 19.5kHz.  In
PDM mode, the duty periods are approximately 100ns long.

.SS Digital I/O
The digital output pins conform to the `canonical digital output' interface
described in the HAL manual.

The digital input pins conform to the `canonical digital input' interface
described in the HAL manual.
""";
pin out s32 encoder.#.count[4];
pin out float encoder.#.position[4];
pin out float encoder.#.velocity[4];
pin in bit encoder.#.reset[4];
pin io bit encoder.#.index-enable[4] """encoder.\\fIM\\fR corresponds to the
pins labeled QA\\fIM\\fR, QB\\fIM\\fR, and QZ\\fIM\\fR on the pinout diagram""";
param rw float encoder.#.scale[4] =1;

param rw bit encoder.z-polarity "Set to TRUE if the index pulse is active low, FALSE if it is active high.  Affects all encoders.";

pin in float pwm.#.value[4];
pin in bit pwm.#.enable[4] "pwm.\\fIM\\fR corresponds to the pins labeled UP\\fIM\\fR and DN\\fIM\\fR on the pinout diagram";
param rw float pwm.#.offset[4];
param rw float pwm.#.scale[4]=1;
param rw float pwm.#.max-dc[4]=1;
param rw float pwm.#.min-dc[4]=0;
param rw bit pwm.#.pwmdir[4]=0
"Set to TRUE use PWM+direction mode.  Set to FALSE to use Up/Down mode.";

param rw bit pwm.is-pdm "Set to TRUE to use PDM (also called interleaved PWM) mode.  Set to FALSE to use traditional PWM mode.  Affects all PWM outputs.";


pin in bit dout.##[20]
"""dout.\\fI0M\\fR corresponds to the pin labeled
OUT\\fIM\\fR on the pinout diagram.  Other pins are shared with the PWM
function, as follows:
.RS 6
.ta T 10 6
.PP
\\fBPin\tShared\\fR
.PP
\\fBLabel\twith\\fR
.PP
dout.10\tUP0
.PP
dout.10\tUP0
.PP
dout.12\tUP1
.PP
dout.14\tUP2
.PP
dout.18\tUP3
.PP
dout.11\tDOWN0
.PP
dout.13\tDOWN1
.PP
dout.15\tDOWN2
.PP
dout.19\tDOWN3
.PP
.RE 
.DT""";
param rw bit dout.##-invert[20]
"If TRUE, the output on the corresponding \\fBdout.\\fIMM\\fR is inverted.";

pin out bit din.##[20];
pin out bit din.##_not[20]
"""For M=0 through 7, din.\\fI0M\\fR corresponds to the pin labeled
IN\\fIM\\fR on the pinout diagram.  Other pins are shared with the encoder
function, as follows:
.RS 6
.ta T 10 6
.PP
\\fBPin\tShared\\fR
.PP
\\fBLabel\twith\\fR
.PP
din.8\tQZ0
.PP
din.9\tQZ1
.PP
din.10\tQZ2
.PP
din.11\tQZ3
.PP
din.12\tQB0
.PP
din.13\tQB1
.PP
din.14\tQB2
.PP
din.15\tQB3
.PP
din.16\tQA0
.PP
din.17\tQA1
.PP
din.18\tQA2
.PP
din.19\tQA3
.PP
.RE
.DT""";

param rw u32 communication_error """Incremented each time 
pluto-servo.read detects an error code in the EPP status register.  While
this register is nonzero, new values are not being written to the Pluto-P
board, and the status of digital outputs and the PWM duty cycle of the PWM
outputs will remain unchanged.  If the watchdog is enabled, it will activate
soon after the communication error is detected.  To continue after a
communication error, set this parameter back to zero.""";

param rw s32 debug_0;
param rw s32 debug_1 """These parameters can display values which are useful to developers or for debugging the driver and firmware.  They are not useful for integrators or users.""";

option singleton;
option extra_setup;
option extra_cleanup;

option data internal;

function read "Read all the inputs from the pluto-servo board";
function write "Write all the outputs on the pluto-servo board";

see_also """The \\fIpluto_servo\\fR section in the HAL User Manual, which shows the location of each physical pin on the pluto board.""";

modparam dummy ioaddr = 0x378 "The base address of the parallel port.";
modparam dummy ioaddr_hi = 0
"""The secondary address of the parallel port, used to set EPP
mode.  0 means to use ioaddr + 0x400.  -1 means there is no
secondary address.  The secondary address is used to set the port to EPP
mode.""";

modparam dummy epp_wide = 1
    """Set to zero to disable the "wide EPP mode".  "Wide" mode allows a 16-
and 32-bit EPP transfers, which can reduce the time spent in the read and write
functions.  However, this may not work on all EPP parallel ports.""";

modparam dummy watchdog = 1
    """Set to zero to disable the "hardware watchdog".  "Watchdog" will
tristate all outputs approximately 6ms after the last execution of
\\fBpluto-servo.write\\fR, which adds some protection in the case of LinuxCNC
crashes.""";

modparam int test_encoder = 0 
    "Internally connect dout0..2 to QA0, QB0, QZ0 to test quadrature counting";

license "GPL";

include "hal/drivers/pluto_common.h";
;;


typedef struct {
    int64_t last_index[4];
    int64_t last_count[4];
    int64_t reset_count[4];
} internal;

#define W 14
#define MASK ((1<<W)-1)

int PWM(int enable, double value, double offset, double scale, double min_dc, double max_dc, int dio0, int dio1, int is_pdm, int is_pwmdir) {
    int result;
    if(enable == 0) return 0;

    value = value / scale + offset;
    if(value < -max_dc) value = -max_dc;
    else if(value > -min_dc && value < 0) value = -min_dc;
    else if(value > 0 && value < min_dc) value = min_dc;
    else if(value > max_dc) value = max_dc;

    value = 2047 * value;
    if(value < -2047) value = -2047;
    if(value > 2047) value = 2047;

    if(value < 0) {
	if(is_pwmdir) {
	     result = (1<<13) | (int)(-value);
	} else {
	     result = (1<<15) | (int)(-value);
	}
    } else result = value;
    if(is_pdm) result |= 1<<14;
    if(dio0) result ^= 1<<12;
    if(dio1) result ^= 1<<13;

    return result;
}

FUNCTION(write) {
    int r = 0;
    int i;
    if(communication_error) return;

    EPP_ADDR(0);

    for(i=0; i<4; i++) {
        if(pwm_max_dc(i) > 1) pwm_max_dc(i) = 1;
        else if(pwm_max_dc(i) < 0) pwm_max_dc(i) = 0;
        if(pwm_min_dc(i) < 0) pwm_min_dc(i) = 0;
        else if(pwm_min_dc(i) > pwm_max_dc(i)) pwm_min_dc(i) = pwm_max_dc(i);
    }

#define D(x) (!dout(x) ^ !dout_invert(x))
#define P(x,y) PWM(pwm_enable(x), pwm_value(x), pwm_offset(x), pwm_scale(x), \
        pwm_min_dc(x), pwm_max_dc(x), D(y), D(y+1), pwm_is_pdm, pwm_pwmdir(x))
    write32( P(0,10) | (P(1, 12) << 16));
    write32( P(2,14) | (P(3, 16) << 16));

    for(i=0; i<10; i++) {
        if(!dout(i) ^ !dout_invert(i)) r |= (1<<i);
    }
    if (encoder_z_polarity) r |= 0x8000;
    if (watchdog) r |= 0x4000;
    if (test_encoder) r |= 0x2000;
    write16(r);
}

FUNCTION(read) {
    int i;
    __u32 ppdata;

    EPP_ADDR(0);
    EPP_DIR_READ();

    for(i=0; i<4; i++) {
        int64_t count, index;
	int newlow, indlow, indexed;
        int reset;
        ppdata = read32();
        reset = encoder_reset(i);
        if(i == 0) {
            int status = inb(ioaddr+1) & 1;
            if(status) {
                communication_error ++;
		pluto_clear_error_register();
            }
            if(communication_error) { EPP_DIR_WRITE(); return; }
        }

	newlow = ppdata & MASK;
	indlow = (ppdata >> W) & MASK;
	indexed = ppdata & (1<<(2*W));

        count = extend(data.last_count[i], newlow, W);
	if(indexed)
	    index = extend(count, indlow, W);
	else
	    index = data.last_index[i];

        if(encoder_index_enable(i) && indexed) {
            encoder_index_enable(i) = 0;
            data.reset_count[i] = index;
        }
        if(reset) encoder_velocity(i) = 0;
        else encoder_velocity(i) = (count - data.last_count[i]) /
            encoder_scale(i) / fperiod;
        data.last_index[i] = index;
        data.last_count[i] = count;
        if(reset) data.reset_count[i] = count;
        encoder_count(i) = count - data.reset_count[i];
        encoder_position(i) = encoder_count(i) / encoder_scale(i);

        if(i == 0) {
            debug_0 = ppdata; debug_1 = count;
        }
    }

    ppdata = read32();

    for(i=0; i< 20; i++) {
        int b = ppdata & (1<<i);
        din(i) = !!b; din_not(i) = !b;
    }

    EPP_DIR_WRITE();
}

#include "hal/drivers/pluto_servo_rbf.h"

EXTRA_SETUP() {
    return pluto_setup(firmware);
}

EXTRA_CLEANUP() {
    pluto_cleanup();
}

// vim:sts=4:et:sw=4