summaryrefslogtreecommitdiff
path: root/src/flash.c
blob: f70dde1218470d9076bf236ad64bbc1f0a836a93 (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
/* SP12: A serial programmer for working with Atmel AVR uCs          */
/* Copyright (C) 1997-2008 Ken Huntington, Kevin Towers,             */
/*                         Artur Pundsack, Pitronics.                */

/* 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 2    */
/* 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, write to the Free Software       */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston,            */
/* MA  02111-1307, USA.                                              */

/* Pitronics can be reached by email: sbolt@xs4all.nl                */
/* Kevin Towers can be reached by email: ktowers@omnexcontrols.com   */
/* Ken Huntington can be reached by email: kenh@compmore.net         */
/* Artur Pundsack can be reached by email: ap@pa-tec.de          */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dos_cpt.h"
#include "sp12.h"

/* Accepts a 16-bit address. Reads the codeWord in two steps         */ 
/* from the device connected to the Centronics port.                 */
/* Power bits prior state assumed on; left on at return.             */
/* SCK prior state assumed lo; left lo at return.                    */
/* AP, 15.01.2008: - The adresswidth has been increased from         */
/*                   16 to 32 bits (int -> long),                    */
/*                 - Extended addressing support has been added      */

unsigned int readCodeWord(unsigned long address) {
    
   unsigned char readByte;
   unsigned int codeWord;
   unsigned long readCmd;
   unsigned long writeCmd;

   /* For large ATMegas like ATMega2561 an extended address has to  */
   /* be set when crossing the 64k boundary.                        */
   if (address > 0x0000FFFF) {
	  writeCmd = LOAD_EXT_ADDR | ((address & 0x00FF0000) >> 8);
	  clockOutCommand(writeCmd);
   }

   codeWord = 0;
   readCmd = READ_PROG_HI | ((address & 0x0000FFFF) << 8);
   readByte = (unsigned char) clockOutCommand(readCmd);
   codeWord = codeWord | readByte;
   readCmd = READ_PROG_LO | ((address & 0x0000FFFF) << 8);
   readByte = (unsigned char) clockOutCommand(readCmd);
   codeWord = (codeWord << 8) | readByte;
   return(codeWord);
}

/* Accepts a 16-bit codeWord and a 32-bit address. Writes the code   */ 
/* in two steps with no delays into the ATMega device connected to   */
/* the Centronics port.                                              */
/* Power bits prior state assumed on; left on at return.             */
/* SCK prior state assumed lo; left lo at return.                    */
/* AP, 15.01.2008: - The adresswidth has been increased from         */
/*                   16 to 32 bits (int -> long),                    */
/*                 - Extended addressing support has been added      */
void loadCodePage(unsigned long address, unsigned int codeWord) { 

   unsigned char loByte;
   unsigned char hiByte;
   unsigned long writeCmd;

   loByte = (unsigned char) codeWord & 0x00FF;
   hiByte = (unsigned char) ((codeWord & 0xFF00) >> 8);
   
   /* For large ATMegas like ATMega2561 an extended address has to  */
   /* be set once for the first page and when crossing the 64k      */
   /* boundary.                                                     */
   if ((ExtAddr != 1) && (address > 0x0000FFFF)) {
	  writeCmd = LOAD_EXT_ADDR | ((address & 0x00FF0000) >> 8);
	  clockOutCommand(writeCmd);
	  ExtAddr = 1; /* The 64k boundary has been detected            */
   }

   writeCmd = LOAD_PAGE_LO | ((address & 0x000000FF) << 8);
   writeCmd = writeCmd | loByte;
   clockOutCommand(writeCmd);
   writeCmd = LOAD_PAGE_HI | ((address & 0x000000FF) << 8);
   writeCmd = writeCmd | hiByte;  
   clockOutCommand(writeCmd);
}

/* Accepts a 16-bit page number and programs it into the ATMega      */
/* device connected to the Centronics parallel port.                 */
/* The page is read back and compared with the buffer; if all        */
/* is well, 0 is returned, else 1.                                   */
/* Power bits prior state assumed on; left on at return.             */
/* SCK prior state assumed lo; left lo at return.                    */

int writeCodePage(unsigned int flashBuf[], unsigned long address) {
   
   unsigned long writeCmd;
   unsigned int idx;
   unsigned char readByte;
   unsigned char testByte;

   writeCmd = WRITE_PAGE | ((address & 0x0000FFFF) << 8);
   clockOutCommand(writeCmd);
   if (POLL_RDY_BSY) {                              /* time-out 64ms */
      testByte = (unsigned char) ~POLL_RDY_BSY;
      for (idx = 0; idx < 64; idx++) {
         delay(1);
         readByte = (unsigned char) clockOutCommand(POLL_RDY_BSY);
         if ((readByte & testByte) == 0)
            break;
      }
   } else {
      loopDelay(timeConst.pageWrite);
   }
   for (idx = address + 1 - device.pageSize; idx <= address; idx++) {
      if (flashBuf[idx] != 0xffff) {
         if (readCodeWord(idx) != flashBuf[idx])
            return(1);
      }
   }
   return(0);
}

/* Accepts a 16-bit codeWord and a 16-bit address. Writes the code   */
/* in two steps into the device connected to the Centronics port and */
/* verifies arrival. Returns 1 if verify fails three times, else 0.  */
/* The time constant byteWrite is adjusted dynamically,              */
/* if the device is not a 1200(A) and optimizeFlag allows.           */
/* Power bits prior state assumed on; left on at return.             */
/* SCK prior state assumed lo; left lo at return.                    */
/* AP, 15.01.2008: This function is only available for non page mode!*/

int writeFlashVerified(unsigned int address, unsigned int codeWord,
                        int optimizeFlag) {
 
   unsigned char loByte;
   unsigned char hiByte;
   unsigned long readCmd, writeCmd;
   int failure;

   loByte = (unsigned char) codeWord & 0x00FF;
   hiByte = (unsigned char) ((codeWord & 0xFF00) >> 8);

   writeCmd = WRITE_PROG_HI | ((long) address << 8);
   writeCmd = writeCmd | hiByte;
   readCmd = READ_PROG_HI | ((long) address << 8);
   failure = writeByteVerified(writeCmd, readCmd, optimizeFlag);
   if (failure == 0) {
      writeCmd = WRITE_PROG_LO | ((long) address << 8);
      writeCmd = writeCmd | loByte;
      readCmd = READ_PROG_LO | ((long) address << 8);
      failure = writeByteVerified(writeCmd, readCmd, optimizeFlag);
   }
   return(failure);
}

/* Expects flashBuf[] to be filled with data.                        */
/* If a (page) write fails, 1 is returned, else 0.                   */
/* Power bits prior state assumed on; left on at return.             */
/* SCK prior state assumed lo; left lo at return.                    */
/* AP, 15.01.2008: - Added reset ext. address marker                 */
/*                 - Set the extended adress byte if necessary       */                 

int writeFlashArea(unsigned int flashBuf[], long bufLimit,
                                            int optimizeFlag) {

   long address, previous, writeCmd; 
   int writeFlg = 0;
   int idx = 1;

   previous = 0;
   printf("...............................................................\r");
   if (optimizeFlag != 11) 
      timeConst.pageWriteAdjusted = 1;
	  
   /* AP, 15.01.2008: Added, Reset the extended address marker and  */
   /*                 set the extended adress byte if neccessary.   */
   ExtAddr = 0; 
   if (device.flashLimit > 0x0000FFFF){
      writeCmd = LOAD_EXT_ADDR;
	  clockOutCommand(writeCmd);
   }
   
   for (address = 0; address < bufLimit; address++) {
      if (device.pageMode) {
         /*
          * To speed things up: Skip locations containing 0xffff
          * (The device has been erased before writing started)
          */
         if (flashBuf[address] != 0xffff) {
            loadCodePage(address, flashBuf[address]); 
            writeFlg = 1;
         }
         if (writeFlg && (address > 0) && 
                  ((address & (device.pageSize - 1)) == device.pageSize - 1)) {
            writeFlg = 0;
            if (writeCodePage(flashBuf, address) == 1) {
               if (++idx < 6) {
                  /*
                   * recalculate first address of the failed page
                   * once only. Note: The calculation below puts 
                   * address at the last address value of the previous
                   * page. But address is incremented _after_ this
                   * calculation, _before_ loadCodePage is called
                   * again.
                   */
                  if (idx == 2) address = address - device.pageSize;
                  if (timeConst.pageWriteAdjusted == 1) writeRetries++;
               } else {
                  printf("!!\n");
                  return (1);
               }
               if (optimizeFlag == 11) {
                  timeConst.pageWrite += timeConst.pageWrite / 2;
                  if (timeConst.pageWrite > 1.5 * timeConst.pageWriteDefault) {
                     printf("!!\n");
                     return (1);
                  }
                  timeConst.pageWriteAdjusted = 1;
               }
            } else {
               if (optimizeFlag == 11 && timeConst.pageWriteAdjusted == 0) {
                  timeConst.pageWrite -= timeConst.pageWrite / 2;
               }
               idx = 1;
            }
         }
      } else {
         if (flashBuf[address] != 0xffff) {
            if (writeFlashVerified((unsigned int) address, 
                         flashBuf[address], optimizeFlag) == 1) {
               printf("!!\n");
               return (1);
            }
         }
      }
      if (address >= (previous + bufLimit / 64)) {
         putchar('o');
         fflush(stdout);
         previous = address;
      } 
   }

   /*
    * In page mode, a sequence like pageWrite reducing from 64ms
    * to 32, 16, 8 and 4ms, then adjusting back upwards to 6ms 
    * and finally 9ms is valid; this would cause writeRetries
    * to be incremented once. Therefore:
    */
   if (device.pageMode && writeRetries == 1)
      writeRetries = 0; 
   printf("\n");
   return (0);
}

/* (expects flashBuf[] to be filled with 0xFFFF)                     */
/* Loops readCodeWord into flashBuf[]                                */
/* Power bits prior state assumed on; left on at return.             */
/* SCK prior state assumed lo; left lo at return.                    */
/* AP, 15.01.2008: - Set the extended adress byte if necessary       */ 

void readFlashArea(unsigned int flashBuf[], long bufLimit) {

   long address, previous, writeCmd;

   previous = 0;
   printf("...............................................................\r");
 
   if (device.flashLimit > 0x0000FFFF){
      writeCmd = LOAD_EXT_ADDR;
	  clockOutCommand(writeCmd);
   }

   for (address = 0; address < bufLimit; address++) {
      flashBuf[address] = readCodeWord(address);
      if (address >= (previous + bufLimit / 64)) {
         putchar('o');
         fflush(stdout);
         previous = address;
      } 
   }
   printf("\n");
}