-
Notifications
You must be signed in to change notification settings - Fork 0
/
eep.c
204 lines (177 loc) · 6.72 KB
/
eep.c
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
/*********************************************************************
*
* Data EEPROM memory access
*
*********************************************************************
* FileName: eep.c
* Dependencies:
* Processor: PIC18F2525
* Complier: XC8 v1.45
*
* Software License Agreement
*
* Copyright © 2011-2019 Sylvain Girard
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Sylvain Girard 15/05/2011 Creation
********************************************************************/
#define _EEP_C
#include <p18cxxx.h>
#include "system.h"
#include "eep.h"
#include "stepper.h"
#include "temperature.h"
#include "adc.h"
unsigned char eep_adr_config_byte;
unsigned char eep_adr_stepper_delay_manual;
unsigned char eep_adr_stepper_delay_auto;
unsigned char eep_adr_stepper_pos_max;
unsigned char eep_adr_stepper_pos_min;
unsigned char eep_adr_stepper_position;
unsigned char eep_adr_stepper_backlash;
unsigned char eep_adr_temperature_coef;
unsigned char eep_adr_p_step_sequencer;
unsigned char eep_adr_pwr_threshold;
/**********************************************************************
* Function: void eep_read(unsigned int adr, unsigned char *buf, unsigned int lg)
* PreCondition: None
* Input: EEPROM address
* Output: byte
* Side Effects:
* Overview: Read the data EEPROM memory
***********************************************************************/
void eep_read(unsigned char adr, unsigned char *buf, unsigned int lg)
{
//adr += 0x20;
while (lg--)
{
// EEADRH = adr >> 8;
EEADR = adr;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.RD = 1;
*buf++ = EEDATA;
adr++;
}
}
/**********************************************************************
* Function: void eep_write(unsigned int adr, unsigned char *buf, unsigned int lg)
* PreCondition: None
* Input: EEPROM address
* Output: byte
* Side Effects:
* Overview: Write in the data EEPROM memory
***********************************************************************/
void eep_write(unsigned char adr, unsigned char *buf, unsigned int lg)
{
//adr += 0x20;
while (lg--)
{
// EEADRH = adr >> 8;
EEADR = adr;
EEDATA = *buf;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1;
//INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
EECON1bits.WR = 1;
//INTCONbits.GIE = 1;
while (PIR2bits.EEIF == 0)
;
PIR2bits.EEIF = 0;
EECON1bits.WREN = 0;
buf++;
adr++;
}
}
/**********************************************************************
* Function: void load_param(void)
* PreCondition: None
* Input: None
* Output: None
* Side Effects:
* Overview: Load parameters from EEPROM
***********************************************************************/
void load_param(void)
{
unsigned char adr;
unsigned int signature, nb_essai = 3;
adr = 0;
while (--nb_essai && signature != EEP_SIGNATURE)
{
eep_read(adr, (unsigned char *)&signature, sizeof(signature));
}
adr += sizeof(signature);
if (signature == EEP_SIGNATURE)
{
eep_adr_config_byte = adr;
eep_read(adr, (unsigned char *)&config_byte.byte, sizeof(config_byte.byte));
adr += sizeof(config_byte.byte);
eep_adr_stepper_delay_manual = adr;
eep_read(adr, (unsigned char *)&stepper_delay_manual, sizeof(stepper_delay_manual));
adr += sizeof(stepper_delay_manual);
eep_adr_stepper_delay_auto = adr;
eep_read(adr, (unsigned char *)&stepper_delay_auto, sizeof(stepper_delay_auto));
adr += sizeof(stepper_delay_auto);
eep_adr_stepper_pos_max = adr;
eep_read(adr, (unsigned char *)&stepper_pos_max, sizeof(stepper_pos_max));
adr += sizeof(stepper_pos_max);
eep_adr_stepper_pos_min = adr;
eep_read(adr, (unsigned char *)&stepper_pos_min, sizeof(stepper_pos_min));
adr += sizeof(stepper_pos_min);
eep_adr_stepper_position = adr;
eep_read(adr, (unsigned char *)&stepper_position, sizeof(stepper_position));
adr += sizeof(stepper_position);
eep_adr_stepper_backlash = adr;
eep_read(adr, (unsigned char *)&stepper_backlash, sizeof(stepper_backlash));
adr += sizeof(stepper_backlash);
eep_adr_temperature_coef = adr;
eep_read(adr, (unsigned char *)&temperature_coef, sizeof(temperature_coef));
adr += sizeof(temperature_coef);
eep_adr_p_step_sequencer = adr;
eep_read(adr, (unsigned char *)&p_step_sequencer, sizeof(p_step_sequencer));
adr += sizeof(p_step_sequencer);
eep_adr_pwr_threshold = adr;
eep_read(adr, (unsigned char *)&pwr_threshold, sizeof(pwr_threshold));
adr += sizeof(pwr_threshold);
}
else
{
// bad signature, writing default values
eep_adr_config_byte = adr;
eep_write(adr, (unsigned char *)&config_byte.byte, sizeof(config_byte.byte));
adr += sizeof(config_byte.byte);
eep_adr_stepper_delay_manual = adr;
eep_write(adr, (unsigned char *)&stepper_delay_manual, sizeof(stepper_delay_manual));
adr += sizeof(stepper_delay_manual);
eep_adr_stepper_delay_auto = adr;
eep_write(adr, (unsigned char *)&stepper_delay_auto, sizeof(stepper_delay_auto));
adr += sizeof(stepper_delay_auto);
eep_adr_stepper_pos_max = adr;
eep_write(adr, (unsigned char *)&stepper_pos_max, sizeof(stepper_pos_max));
adr += sizeof(stepper_pos_max);
eep_adr_stepper_pos_min = adr;
eep_write(adr, (unsigned char *)&stepper_pos_min, sizeof(stepper_pos_min));
adr += sizeof(stepper_pos_min);
eep_adr_stepper_position = adr;
eep_write(adr, (unsigned char *)&stepper_position, sizeof(stepper_position));
adr += sizeof(stepper_position);
eep_adr_stepper_backlash = adr;
eep_write(adr, (unsigned char *)&stepper_backlash, sizeof(stepper_backlash));
adr += sizeof(stepper_backlash);
eep_adr_temperature_coef = adr;
eep_write(adr, (unsigned char *)&temperature_coef, sizeof(temperature_coef));
adr += sizeof(temperature_coef);
eep_adr_p_step_sequencer = adr;
eep_write(adr, (unsigned char *)&p_step_sequencer, sizeof(p_step_sequencer));
adr += sizeof(p_step_sequencer);
eep_adr_pwr_threshold = adr;
eep_write(adr, (unsigned char *)&pwr_threshold, sizeof(pwr_threshold));
adr += sizeof(pwr_threshold);
signature = EEP_SIGNATURE;
eep_write(0, (unsigned char *)&signature, sizeof(signature));
}
}