-
Notifications
You must be signed in to change notification settings - Fork 27
/
eeprom.c
320 lines (264 loc) · 9.5 KB
/
eeprom.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
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
/*
* Copyright 1996, 1997, 1998, 1999 by Daniel B. Suthers,
* Pleasanton Ca. 94588 USA
* E-MAIL [email protected]
*
*/
/*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "eeprom.h"
#include "process.h"
extern int tty;
extern int sptty;
extern int display();
extern int send_buffer ( unsigned char *, int, int, int, int );
/*-----------------------------------------------------------------+
| This function erases the eeprom. |
| 0 = 0 |
| 1 = 3 (start of macros |
| 2 = 0xff (end of timer initiators) |
| 3 = 0xff (end of macro initiators) |
| |
| It does it by writing the value 0xff to all memory positions |
| past the 3rd. We start by writing bytes 3 through 18 to 0xff, |
| and then byte 0 to 0 and 1 to 3. |
| |
| We want to avoid having a timer trigger to an invalid position. |
| We want all memory to be initialized just in case. |
+-----------------------------------------------------------------*/
int eraseall ( void )
{
unsigned char block[20];
int blockno;
int x;
extern void error();
extern void remove_record_file();
extern int c_setclock();
block[0] = 0;
block[1] = 3;
for ( x = 2; x < 16; x++ )
block[x] = (unsigned char) 0xff;
/* Write the first block */
if ( sendpacket(0, block) < 0 ) {
error("Erase failed to write the first block\n");
}
fputc( '.', stdout);
fflush(stdout);
/* fill the block */
for(x=0; x < 16; x++)
block[x] = (unsigned char) 0xff;
/* For each 16 byte block... */
for ( blockno = 1; blockno < (PROMSIZE / 16); blockno++ ) {
if ( sendpacket((blockno * 16) , block) < 0 ) {
char tmpstr[100];
sprintf( tmpstr, "Erase failed to write block %d\n", blockno);
error(tmpstr);
}
fputc( '.', stdout);
fflush(stdout);
}
/*
* Delete the X10 Record File and reset the cm11a clock.
* (In the absence of record file, the clock is set to
* system standard time.)
*
*/
remove_record_file();
(void) c_setclock(1, NULL);
return 0;
}
/*-----------------------------------------------------------------+
| sendpacket sends the packet from a 19 byte string and handles |
| locking and handshake. |
| loc = eeprom address (0 relative ) |
| dat = 16 btyes of data. |
| Return 0 on success, -1 on error. |
+-----------------------------------------------------------------*/
int sendpacket ( int loc, unsigned char *dat)
{
unsigned char buf[23];
extern int xwrite();
int rtn;
unsigned char sum;
int timeout;
extern int xwrite(), exread(), check4poll();
extern unsigned char checksum();
/* timeout = 10; */
timeout = 3;
buf[0] = (char) 0xfb; /* write to eeprom command */
buf[1] = loc / 256; /* hi byte of eeprom address */
buf[2] = loc % 256; /* low byte of eeprom address */
memcpy(&buf[3], dat, 16);
rtn = xwrite(tty, buf, 19);
if ( rtn < 0 )
return rtn;
/* the checksum covers the data... Not the leading 0xfb */
sum = checksum(buf + 1, 18) ;
/* read back the check sum */
rtn = exread(sptty, buf, 1, timeout);
if ( rtn < 0 )
return rtn;
if ( sum != buf[0] ) {
fprintf(stderr, "Checksum failure sending eeprom command\n");
fprintf( stderr, "Expected %0x, got %0x\n", sum, buf[0]);
return -2;
}
/* check sums match */
rtn= xwrite(tty, "\00" , 1); /* WRMI (we really mean it) */
if ( rtn < 0 )
return(rtn);
buf[0] = 0;
rtn = exread(sptty, buf, 1, timeout);
if ( rtn == 1 ) {
if ( buf[0] != 0x55 ) {
fprintf(stderr,
"Ack after execution = 0x%02x, It should be 0x55)\n", buf[0]);
rtn = 0;
}
}
if ( rtn != 1 ) {
if ( rtn == 0 && sum == 0x5a ) /* Workaround for 0x5a checksum problem */
return 0;
else {
fprintf(stderr,
"Interface not ready after sending data for location %0x)\n", loc);
fprintf(stderr, "rtn = %0x\n", rtn);
return -1;
}
}
return 0;
}
/*-----------------------------------------------------------------+
| Upload the 1024 byte memory image 'prommap' to the CM11A EEPROM.|
| Start by nulling out the header block. Then upload backwards |
| so everything will be in place when the new header block is |
| uploaded. |
+-----------------------------------------------------------------*/
void upload_eeprom_image_old ( unsigned char *prommap )
{
unsigned char emptyprom[32];
extern int verbose;
extern void error();
int x;
/* Zero out the initiators to null the header block */
emptyprom[0] = (unsigned char)0;
emptyprom[1] = (unsigned char)3;
for ( x = 2; x < 16; x++)
emptyprom[x] = (unsigned char)0xff;
/* erase the old eeprom header information */
if ( sendpacket(0 , emptyprom) < 0 ) {
(void)error("load_macro() failed to erase initiator block");
/* error() exits */
}
/* Copy the data from the array 'prommap' to the CM11A */
/* Load backwards
* This is so the timers at the end of the eeprom are there when
* the initiators are loaded at the start of the eeprom
*/
fprintf(stdout, "Uploading %d block memory image to interface.\n",
PROMSIZE / 16 );
for ( x = 0; x < (PROMSIZE/16); x++ )
fputc( '.', stdout);
fputc( '\r', stdout);
fflush(stdout);
for ( x = (PROMSIZE / 16) - 1; x >= 0; x-- ) {
if (verbose)
fprintf( stderr, "Loading %d\n", x * 16 );
if ( sendpacket((x * 16), prommap + (x*16)) < 0 ) {
char tmpstr[100];
sprintf( tmpstr, "Upload failed to write block %d\n", x);
error(tmpstr);
}
fputc( '#', stdout);
fflush(stdout);
}
fputc('\n', stdout);
return;
}
/*-----------------------------------------------------------------+
| Upload the 1024 byte memory image 'prommap' to the CM11A EEPROM.|
| Start by nulling out the header block. Then upload backwards |
| so everything will be in place when the new header block is |
| uploaded. |
+-----------------------------------------------------------------*/
void upload_eeprom_image ( unsigned char *prommap )
{
extern int verbose;
unsigned char buffer[32];
unsigned int loc;
int j, k, block, chkoff, result, timeout = 3;
/* Offset checksum from beginning of buffer - the initial */
/* 0xfb is not included in the checksum. */
chkoff = 1;
/* Zero out the initiators to null the header block */
buffer[0] = 0xfb;
buffer[1] = 0x00;
buffer[2] = 0x03;
for ( j = 3; j < 19; j++)
buffer[j] = 0xff;
/* erase the old eeprom header information */
if ( send_buffer(buffer, 19, chkoff, timeout, NO_SYNC) != 0 ) {
fprintf(stderr, "upload_eeprom_image() failed to erase initiator block.\n");
exit(1);
}
/* Copy the data from the array 'prommap' to the CM11A */
/* Load backwards
* This is so the timers at the end of the eeprom are there when
* the initiators are loaded at the start of the eeprom
*/
fprintf(stdout, "Uploading %d block memory image to interface.\n",
PROMSIZE / 16 );
for ( j = 0; j < (PROMSIZE/16); j++ )
fputc( '.', stdout);
fputc( '\r', stdout);
fflush(stdout);
for ( block = (PROMSIZE / 16) - 1; block >= 0; block-- ) {
loc = block * 16;
buffer[0] = 0xfb;
buffer[1] = loc / 256;
buffer[2] = loc % 256;
memcpy(&buffer[3], prommap + loc, 16);
result = 0; /* Keep compiler happy */
for ( k = 0; k < 3; k++ ) {
if (verbose)
fprintf(stderr, "Uploading block %d to address 0x%03x\n", block, loc );
if ( (result = send_buffer(buffer, 19, chkoff, timeout, NO_SYNC)) == 0 )
break;
sleep(2);
}
if ( result != 0 ) {
fprintf(stderr, "Upload failed to write block %d to address 0x%03x\n", block, loc);
exit(1);
}
fputc( '#', stdout);
fflush(stdout);
}
fputc('\n', stdout);
return;
}