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: sr.c,v 1.4 2004/04/04 10:32:50 dbh Exp $
20
 */
21
 
22
/* This file contains the code to save and restore the "configurable
23
 * parameters" of the program. These are held in the internal EEPROM
24
 *
25
 * These functions are expected to be executed with interrupts enabled and
26
 * will not return until complete.
27
 */
28
 
29
#include <avr/io.h>
30
#include <inttypes.h>
31
#include <avr/interrupt.h>
32
 
33
#include "stepper.h"
34
#include "paddle.h"
35
 
36
/* Define the staging structure used to save configurable parameters */
37
struct sr_s
38
{
39
    uint8_t     raFinPos;
40
    uint8_t     raFinNeg;
41
    uint16_t    raBacklash;
42
 
43
    uint8_t     decFinPos;
44
    uint8_t     decFinNeg;
45
    uint16_t    decBacklash;
46
 
47
    uint8_t     paddleGuideRate;
48
 
49
    int8_t      trackingRate;
50
    uint8_t     transRatio;
51
 
52
    uint8_t     csum;   // Two's complement of other fields
53
};
54
 
55
volatile uint8_t        doSave;
56
 
57
/* srSaveState() collects the configurable parameters from all over the
58
 * system and saves them into the EEPROM
59
 *
60
 * Passed
61
 *      Nothing
62
 *
63
 * Returns
64
 *      Nothing
65
 *
66
 * Notes:
67
 *      This function assumes that the interrupts are enabled on
68
 *      entry.
69
 */
70
void
71
srSaveState(void)
72
{
73
    struct sr_s         state;
74
    uint8_t             i;
75
    uint8_t             *p;
76
 
77
    /* Turn off the interrupts while we "snapshot" the current
78
     * state
79
     */
80
    cli();
81
 
82
    /* Copy the current state */
83
    state.raFinPos = raState.finPos;
84
    state.raFinNeg = raState.finNeg;
85
    state.raBacklash = raState.backlash;
86
 
87
    state.decFinPos = decState.finPos;
88
    state.decFinNeg = decState.finNeg;
89
    state.decBacklash = decState.backlash;
90
 
91
    state.paddleGuideRate = paddleGuideRate;
92
    state.trackingRate = trackingRate;
93
    state.transRatio = transRatio;
94
 
95
    /* Reenable interrupts */
96
    sei();
97
 
98
    /* Calculate the checksum of the structure */
99
    for (p = (uint8_t *)&state, i = 0 ; p != (uint8_t *)&state.csum ; )
100
         i += *p++;
101
    state.csum = i;
102
 
103
    /* Write the complete structure into the EEPROM */
104
    for (i = 0, p = (uint8_t *)&state ; i < sizeof(state) ; ++i, ++p)
105
    {
106
        /* Wait for EEWE to become zero (ie. EEPROM not writting) */
107
        while (EECR & _BV(EEWE))
108
            ;
109
 
110
        /* Setup the address */
111
        EEARH = 0;
112
        EEARL = i;
113
 
114
        /* Put the data in place */
115
        EEDR = *p;
116
 
117
        /* Perform the write */
118
        cli();
119
        EECR = _BV(EEMWE);
120
        EECR = _BV(EEMWE) | _BV(EEWE);
121
        sei();
122
    }
123
 
124
    /* All done */
125
}
126
 
127
/* srLoadState() reads the configurable parameters from EEPROM and copies
128
 * them to where they need to do.
129
 *
130
 * The data is protected by a checksum calculated when the data was
131
 * saved.
132
 *
133
 * Passed
134
 *      Nothing
135
 *
136
 * Returns
137
 *      Nothing
138
 *
139
 * Notes:
140
 *      This interrupt does not change the interrupt state and does
141
 *      not return until all the data is read
142
 */
143
void
144
srLoadState(void)
145
{
146
    struct sr_s         state;
147
    uint8_t             i;
148
    uint8_t             *p;
149
 
150
    /* Read the state data from the EEPROM */
151
    for (i = 0, p = (uint8_t *)&state ; i < sizeof(state) ; ++i, ++p)
152
    {
153
        /* Wait for EEWE to become zero (ie. EEPROM not writting) */
154
        while (EECR & _BV(EEWE))
155
            ;
156
 
157
        /* Setup the address */
158
        EEARH = 0;
159
        EEARL = i;
160
 
161
        /* Read the data. The device will stall until the data is
162
         * ready
163
         */
164
        EECR = _BV(EERE);
165
 
166
        /* Get the data */
167
        *p = EEDR;
168
    }
169
 
170
    /* Calculate the checksum of the structure. If it doesn't
171
     * match then the data is either corrupt or not initialized
172
     *
173
     * Either way keep the current data
174
     */
175
    for (p = (uint8_t *)&state, i = 0 ; p != (uint8_t *)&state.csum ; )
176
         i += *p++;
177
    if (state.csum != i)
178
        return;
179
 
180
    /* Copy the restored data to the current state */
181
    raState.finPos = state.raFinPos;
182
    raState.finNeg = state.raFinNeg;
183
    raState.backlash = state.raBacklash;
184
 
185
    decState.finPos = state.decFinPos;
186
    decState.finNeg = state.decFinNeg;
187
    decState.backlash = state.decBacklash;
188
 
189
    paddleGuideRate = state.paddleGuideRate;
190
    trackingRate = state.trackingRate;
191
    transRatio = state.transRatio;
192
 
193
    /* All done */
194
}