Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
508 kaklik 1
/* 
2
 * Copyright (C) 2004 Darren Hutchinson (dbh@gbdt.com.au)
3
 * 
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU Library General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or (at your
7
 * option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful, but
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
12
 * License for more details.
13
 * 
14
 * You should have received a copy of the GNU Library General Public License
15
 * along with this software; see the file COPYING.  If not, write to
16
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17
 * MA 02111-1307, USA. 
18
 *
19
 * $Id: driver.c,v 1.4 2004/04/04 06:54:05 dbh Exp $
20
 */
21
 
22
/* This file contains the code that takes the excitation values for each
23
 * coil (determined by the stepper module) and performs a crude PWM
24
 * and generates the output values for the stepper drivers.
25
 *
26
 * This module has it's own timer at a high frequency timer, so efficiency
27
 * is an important issue.
28
 */
29
 
30
#include <avr/io.h>
31
#include <avr/interrupt.h>
32
#include <inttypes.h>
33
 
34
#include "eq6.h"
35
#include "stepper.h"
36
#include "driver.h"
37
 
38
/* Like the stepper.h module this one is heavily table-driven. Basically
39
 * each excitation value corresponds to 4, 8 bit lists of values are used
40
 * for 8 clock cycles. This gives a crude PWM system.
41
 *
42
 * The hardware is orginized such that both "ends" of a coil are in
43
 * adjacent bits, so each pair of 8 bits is interleaved into a single
44
 * 16 bit word where the bits are used two at a time.
45
 *
46
 * Note: The high side and low side tables here were different - basically if
47
 * a high side was driven for one coil, the low side should be driven for the
48
 * other.
49
 *
50
 * However the low side bits are reversed because of the bit numbers running
51
 * in the opposite direction for Port A (low side) vs Port C (high side), so
52
 * it turns out that the same table can be used for both!
53
 *
54
 * Of course the bits for the coils are composed in the opposite direction
55
 * too, but that happens below
56
 */
57
uint8_t driveTbl[] =
58
{
59
    [EX_M_1] = 0xaa,
60
    [EX_M_0_67] = 0x2a,
61
    [EX_M_0_4] = 0x22,
62
    [EX_M_0_2] = 0x02,
63
    [EX_0] = 0x00,
64
    [EX_P_0_2] = 0x01,
65
    [EX_P_0_4] = 0x11,
66
    [EX_P_0_67] = 0x15,
67
    [EX_P_1] = 0x55
68
};
69
 
70
#define PWM_RATE        48000           /* PWM update speed */
71
#define PWM_STEPS       4               /* Steps per PWM cycle */
72
 
73
/* driverInit() initializes the port used by the stepper motors and the timer
74
 * used to update the stepper driver state
75
 *
76
 * Passed:
77
 *      Nothing
78
 *
79
 * Returns:
80
 *      Nothing
81
 *
82
 */
83
void
84
driverInit(void)
85
{
86
    /* Set up port A and C as outputs and set them to a safe default state
87
     * i.e. no outputs driven
88
     */
89
    PORTA = 0x00;               // Disable high-side drivers
90
    DDRA = 0xff;                // Set as outputs
91
 
92
    PORTC = 0xff;               // Disable low-side drivers
93
    DDRC = 0xff;                // Set as outputs
94
 
95
    /* Setup the "magic" relay bit as an output */
96
 
97
    MAGIC_PORT &= ~_BV(MAGIC_BIT);
98
    MAGIC_DDR |= _BV(MAGIC_BIT);
99
 
100
    /* Setup timer 0 to generate interrupts for PWM */
101
    OCR0 = (CLK_RATE / PWM_RATE / 8);
102
    TCCR0 = _BV(WGM01) | _BV(CS01);     // Divied by 8, CTC mode
103
 
104
    /* Enable interrupt generation from timer 0 */
105
    TIMSK |= _BV(OCIE0);
106
}
107
 
108
/* driverInt() is called whenever a timer 0 overflow interrupt occurs
109
 *
110
 * Passed:
111
 *      Nothing
112
 *
113
 * Returns:
114
 *      Nothing
115
 */
116
SIGNAL(SIG_OUTPUT_COMPARE0)
117
{
118
    static uint8_t      raCoil1States;
119
    static uint8_t      raCoil2States;
120
 
121
    static uint8_t      decCoil1States;
122
    static uint8_t      decCoil2States;
123
 
124
    static uint8_t      ctr = PWM_STEPS - 1;
125
 
126
    uint8_t             highSidePort;
127
    uint8_t             lowSidePort;
128
 
129
// PORTA = ~excitation.ra;
130
// PORTC = ~excitation.dec;
131
 
132
    /* Increment the step count. Reinitialize the states entries
133
     * if the counter wraps around
134
     */
135
    if (++ctr == PWM_STEPS)
136
    {
137
        uint8_t         tmp;
138
 
139
        ctr = 0;
140
 
141
        /* Update states */
142
        tmp = _GET_C1(raExcitation.excitation);
143
        raCoil1States = driveTbl[tmp];
144
 
145
        tmp = _GET_C2(raExcitation.excitation);
146
        raCoil2States = driveTbl[tmp];
147
 
148
        tmp = _GET_C1(decExcitation.excitation);
149
        decCoil1States = driveTbl[tmp];
150
 
151
        tmp = _GET_C2(decExcitation.excitation);
152
        decCoil2States = driveTbl[tmp];
153
 
154
        /* Update magic relay state */
155
        if (raExcitation.useRelay)
156
            MAGIC_PORT &= ~_BV(MAGIC_BIT);
157
        else
158
            MAGIC_PORT |= _BV(MAGIC_BIT);
159
    }
160
 
161
    /* Build the high_side driver output value */
162
    highSidePort = decCoil2States & 0x3;
163
    highSidePort <<= 2;
164
 
165
    highSidePort |= decCoil1States & 0x3;
166
    highSidePort <<= 2;
167
 
168
    highSidePort |= raCoil2States & 0x3;
169
    highSidePort <<= 2;
170
 
171
    highSidePort |= raCoil1States & 0x3;
172
 
173
    /* Build the low-side driver states. Note that, due to the
174
     * reverse pin pordering for Port A vs Port C that these are built in
175
     * the opposite direction
176
     */
177
    lowSidePort = raCoil1States & 0x3;
178
    lowSidePort <<= 2;
179
    raCoil1States >>= 2;
180
 
181
    lowSidePort |= raCoil2States & 0x3;
182
    lowSidePort <<= 2;
183
    raCoil2States >>= 2;
184
 
185
    lowSidePort |= decCoil1States & 0x3;
186
    lowSidePort <<= 2;
187
    decCoil1States >>= 2;
188
 
189
    lowSidePort |= decCoil2States & 0x3;
190
    decCoil2States >>= 2;
191
 
192
    /* Potential safety check: rev (PortA) & PortC == 0 */
193
 
194
    /* Write the values to the output ports */
195
    PORTC = highSidePort;
196
    PORTA = ~lowSidePort;
197
}