summaryrefslogtreecommitdiff
path: root/src/hal/drivers/hal_evoreg.c
blob: b83f5d7757661e96421851f357c95e39030aab29 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/********************************************************************
* Description:  hal_evoreg.c
*               This file, 'hal_evoreg.c', is a HAL component that 
*               provides a driver for the Siemens EVOREG motion 
*               control board.
*
* Author: Martin Kuhnle, John Kasunich
* License: GPL Version 2
*    
* Copyright (c) 2003 All rights reserved.
*
* Last change: 
********************************************************************/

/** This file, 'hal_evoreg.c', is a HAL component that provides a
    driver for the Siemens EVOREG motion control board.
    This board privides three 16bit DAC's, three encoder inputs,
    46 digital inputs and 21 digital outputs

    Fixme: error messages are not proper up to now
    ToDo: better error messages
          make inverted bits available
          posibility to read back outputs
          check dac values for limits
          scale for every dacs
          scale for every encoder
          check if the dacs are updated all at the same time
          enable Interrupts
          check for wire break
          watchdog



 Copyright (C) 2003 Martin Kuhnle
                       <mkuhnle AT users DOT sourceforge DOT net>
                John Kasunich
                       <jmkasunich AT users DOT sourceforge DOT net>
                       
*/

/** This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General
    Public License as published by the Free Software Foundation.
    This library 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 library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111 USA

    THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR
    ANY HARM OR LOSS RESULTING FROM ITS USE.  IT IS _EXTREMELY_ UNWISE
    TO RELY ON SOFTWARE ALONE FOR SAFETY.  Any machinery capable of
    harming persons must have provisions for completely removing power
    from all motors, etc, before persons enter any danger area.  All
    machinery must be designed to comply with local and national safety
    codes, and the authors of this software can not, and do not, take
    any responsibility for such compliance.

    This code was written as part of the EMC HAL project.  For more
    information, go to www.linuxcnc.org.
*/

#include "rtapi_ctype.h"	/* isspace() */
#include "rtapi.h"		/* RTAPI realtime OS API */
#include "rtapi_app.h"		/* RTAPI realtime module decls */
#include "hal.h"		/* HAL public API decls */

/* If FASTIO is defined, uses outb() and inb() from <asm.io>,
   instead of rtapi_outb() and rtapi_inb() - the <asm.io> ones
   are inlined, and save a microsecond or two (on my 233MHz box)
*/
#define FASTIO

#ifdef FASTIO
#define rtapi_inb inb
#define rtapi_outb outb
#include <asm/io.h>
#endif

/* module information */
MODULE_AUTHOR("Martin Kuhnle");
MODULE_DESCRIPTION("SIEMENS-EVOREG Driver for EMC HAL");
MODULE_LICENSE("GPL");
/* static char *cfg = 0; */
/* config string
RTAPI_MP_STRING(cfg, "config string"); */

/***********************************************************************
*                STRUCTURES AND GLOBAL VARIABLES                       *
************************************************************************/

/* this structure contains the runtime data needed by the
   driver for a single port/channel
*/

typedef struct {
	void *io_base;
        hal_float_t *dac_out[3];  /* ptrs for dac output */
	hal_float_t *position[3];	   /* ptrs for encoder input */
	hal_bit_t *digital_in[47];    /* ptrs for digital input pins 0 - 45 */
        hal_bit_t *digital_out[25];    /* ptrs for digital output pins 0 - 20 */
        __u16 raw_counts_old[3];
        __s32 counts[3];
        hal_float_t pos_scale;         /*! \todo scale for position command FIXME schould be one per axis */
} evoreg_t;

/* pointer to array of evoreg_t structs in shared memory, 1 per port */
static evoreg_t *port_data_array;

/* other globals */
static int comp_id;		/* component ID */
static int num_ports;		/* number of ports configured */

/***********************************************************************
*                  LOCAL FUNCTION DECLARATIONS                         *
************************************************************************/
/* These is the functions that actually do the I/O
   everything else is just init code
*/
static void update_port(void *arg, long period);

/***********************************************************************
*                       INIT AND EXIT CODE                             *
************************************************************************/

#define MAX_PORTS 8
#define MAX_DAC 3               /* number of DACs per card */
#define MAX_ENC 3               /* number of Encoders per card */

#define MAX_TOK ((MAX_PORTS*2)+3)

int rtapi_app_main(void)
{
    char name[HAL_NAME_LEN + 1];
    int n,i , retval, num_dac, num_enc;

    unsigned int base=0x300;

    /* only one port at the moment */
    num_ports = 1;
    n = 0;

    #define ISA_BASE    0xC9000
    #define ISA_MAX    0x100000  /* allgemeiner Speicherzugriff */

    
    /* STEP 1: initialise the driver */
    comp_id = hal_init("hal_evoreg");
    if (comp_id < 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
	    "EVOREG: ERROR: hal_init() failed\n");
	return -1;
    }

    /* STEP 2: allocate shared memory for EVOREG data */
    port_data_array = hal_malloc(num_ports * sizeof(evoreg_t));
    if (port_data_array == 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
	    "EVOREG: ERROR: hal_malloc() failed\n");
	hal_exit(comp_id);
	return -1;
    }

    /*! \todo FIXME: Test memory area and setup the card */
    port_data_array->io_base = ioremap(ISA_BASE, ISA_MAX - ISA_BASE);
    rtapi_print_msg(RTAPI_MSG_ERR,"EVOREG: io_base: %p \n", port_data_array->io_base);
    outw(0x82c9,base); /* set indexregister */

    /* Set all outputs to zero */
    writew(0, port_data_array->io_base + 0x20); /* digital out 0-15  */
    writew(0, port_data_array->io_base + 0x40); /* digital out 16-23 */
    writew(0, port_data_array->io_base + 0x60); /* DAC 1 */
    writew(0, port_data_array->io_base + 0x80); /* DAC 2 */
    writew(0, port_data_array->io_base + 0xa0); /* DAC 3 */
    /* Reset Encoder's */
    writew(0, port_data_array->io_base + 0x02); /* ENCODER 1 */
    writew(0, port_data_array->io_base + 0x0a); /* ENCODER 2 */
    writew(0, port_data_array->io_base + 0x12); /* ENCODER 3 */
    
    /* STEP 3: export the pin(s) */

    /* Export DAC pin's */
    for ( num_dac=1; num_dac<=MAX_DAC; num_dac++) {
      retval = hal_pin_float_newf(HAL_IN, &(port_data_array->dac_out[num_dac-1]),
				  comp_id, "evoreg.%d.dac-%02d-out", 1, num_dac);
      if (retval < 0) {
	  rtapi_print_msg(RTAPI_MSG_ERR,
	    "EVOREG: ERROR: port %d var export failed with err=%i\n", n + 1,
	    retval);
	hal_exit(comp_id);
	return -1;
      }
    }

    /* Export Encoder pin's */
    for ( num_enc=1; num_enc<=MAX_ENC; num_enc++) {
      retval = hal_pin_float_newf(HAL_OUT, &(port_data_array->position[num_enc - 1]),
				  comp_id, "evoreg.%d.position-%02d-in", 1, num_enc);
      if (retval < 0) {
	  rtapi_print_msg(RTAPI_MSG_ERR,
	      "EVOREG: ERROR: port %d var export failed with err=%i\n", n + 1,
	      retval);
  	  hal_exit(comp_id);
  	  return -1;
      }
    }

    /* Export IO pin's */

    /* export write only HAL pin's for the input bit */
    for ( i=0; i<=45;i++) {
      retval += hal_pin_bit_newf(HAL_OUT, &(port_data_array->digital_in[i]),
				 comp_id, "evoreg.%d.pin-%02d-in", 1, i);

      /* export another write only HAL pin for the same bit inverted */
      /*
      retval += hal_pin_bit_newf(HAL_OUT, &(port_data_array->digital_in[(2*i)+1]),
				 comp_id, "evoreg.%d.pin-%02d-in-not", 1, i); */
      if (retval < 0) {
	  rtapi_print_msg(RTAPI_MSG_ERR,
	      "EVOREG: ERROR: port %d var export failed with err=%i\n", n + 1,
	      retval);
  	  hal_exit(comp_id);
  	  return -1;
      }
    }

    /* export read only HAL pin's for the output bit */
    for ( i=0; i<=23;i++) {
      retval += hal_pin_bit_newf(HAL_IN, &(port_data_array->digital_out[i]),
				 comp_id, "evoreg.%d.pin-%02d-out", 1, i);

      /* export another read only HAL pin for the same bit inverted */
      /*
      retval += hal_pin_bit_newf(HAL_IN, &(port_data_array->digital_out[(2*i)+1]),
				 comp_id, "evoreg.%d.pin-%02d-out-not", 1, i));  */
      if (retval < 0) {
	  rtapi_print_msg(RTAPI_MSG_ERR,
	      "EVOREG: ERROR: port %d var export failed with err=%i\n", n + 1,
	      retval);
  	  hal_exit(comp_id);
  	  return -1;
      }
    }

    /* export parameter for scaling */
    retval = hal_param_float_newf(HAL_RW, &(port_data_array->pos_scale),
				  comp_id, "evoreg.%d.position-scale", 1);
    if (retval != 0) {
	return retval;
    }


    /* STEP 4: export function */
    rtapi_snprintf(name, sizeof(name), "evoreg.%d.update", n + 1);
    retval = hal_export_funct(name, update_port, &(port_data_array[n]), 1, 0,
	comp_id);
    if (retval < 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
	    "EVOREG: ERROR: port %d write funct export failed\n", n + 1);
	hal_exit(comp_id);
	return -1;
    }

    rtapi_print_msg(RTAPI_MSG_INFO,
	"EVOREG: installed driver for %d card(s)\n", num_ports);
    hal_ready(comp_id);
    return 0;
}

void rtapi_app_exit(void)
{
    outw(0x0,0x300);
    hal_exit(comp_id);
}

/**************************************************************
* REALTIME PORT WRITE FUNCTION                                *
**************************************************************/

static void update_port(void *arg, long period)
{
    evoreg_t *port;
    int pin;
    unsigned char tmp, mask;
    __u16 raw_counts[3];

    port = arg;

/* write DAC's */
    writew((*(port->dac_out[0])/10 * 0x7fff), port->io_base + 0x60);
    writew((*(port->dac_out[1])/10 * 0x7fff), port->io_base + 0x80);
    writew((*(port->dac_out[2])/10 * 0x7fff), port->io_base + 0xa0);

/* Read Encoders, improve the 16bit hardware counters to 32bit and scale the values */
    raw_counts[0] = (__u16) readw(port->io_base);
    raw_counts[1] = (__u16) readw(port->io_base + 0x08 );
    raw_counts[2] = (__u16) readw(port->io_base + 0x10 );

    port->counts[0] += (__s16) (raw_counts[0] - port->raw_counts_old[0]);
    port->raw_counts_old[0] = raw_counts[0];

    port->counts[1] += (__s16) (raw_counts[1] - port->raw_counts_old[1]);
    port->raw_counts_old[1] = raw_counts[1];

    port->counts[2] += (__s16) (raw_counts[2] - port->raw_counts_old[2]);
    port->raw_counts_old[2] = raw_counts[2];

    *port->position[0] = port->counts[0] * port->pos_scale;
    *port->position[1] = port->counts[1] * port->pos_scale;
    *port->position[2] = port->counts[2] * port->pos_scale;


/* read digital inputs */
     tmp = readw(port->io_base + 0x20);       /* digital input 0-15 */
      mask = 0x01;
	for (pin=0 ; pin < 16 ; pin++) {
	*port->digital_in[pin] = (tmp & mask) ? 1:0 ;
	mask <<= 1;
	}
     tmp = readw(port->io_base + 0x40);       /* digital input 16-31 */
      mask = 0x01;
	for (pin=16 ; pin < 32 ; pin++) {
	*port->digital_in[pin] = (tmp & mask) ? 1:0 ;
	mask <<= 1;
	}

     tmp = readw(port->io_base + 0x60);       /* digital input 32-45 */
      mask = 0x01;
	for (pin=32 ; pin < 46 ; pin++) {
	*port->digital_in[pin] = (tmp & mask) ? 1:0 ;
	mask <<= 1;
	}


/* write digital outputs */
     tmp = 0x0;
     mask = 0x01;
     for (pin=0; pin < 16; pin++) {
        if (port->digital_out[pin]) {
        tmp |= mask;
        mask <<= 1;
        }
     }
     writew( tmp, port->io_base + 0x20);  /* digital output 0-15 */


     tmp = 0x0;
     mask = 0x01;
     for (pin=16; pin < 24; pin++) {
        if (port->digital_out[pin]) {
        tmp |= mask;
        mask <<= 1;
        }
     }
     writew( tmp, port->io_base + 0x40);  /* digital output 16-23 */

}