Debian Codegraph @

cm16flash.cxx

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /***************************************************************************
2 cm16flash - class for handling flash programming of an M16C
3 microprocessor.
4
5 begin : Wed Sep 25 18:58:35 BST 2002
6 copyright : (C) 2002 by Greg Reynolds, Jim Hawkridge
7 email : [email protected]
8
9 2005-11-18: modified by Thomas Fischl <[email protected]>
10 - added support for R8C
11 2005-11-20: - show new ID
12
13 ***************************************************************************/
14
15 /***************************************************************************
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 ***************************************************************************/
23 #include "cm16flash.h"cxx . . $ `<file>/cm16flash.h`/
24
25 CM16Flashcxx . . $ CM16Flash#::CM16Flashcxx . . $ CM16Flash#CM16Flash(12ac35f22111cf35).(CCOMPortcxx . . $ CCOMPort#* pPort, unsigned char cuCPUType)
26 {
27 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( pPort );
28
29 m_pPortcxx . . $ CM16Flash#m_pPort. = pPort;
30 m_CPUTypecxx . . $ CM16Flash#m_CPUType. = cuCPUType;
31 }
32
33 CM16Flashcxx . . $ CM16Flash#::~cxx . . $ CM16Flash#`~CM16Flash`(ced63f7c635d850d).CM16Flashcxx . . $ CM16Flash#()
34 {
35
36 }
37
38 /* send a byte with the minimum delay */
39 int CM16Flashcxx . . $ CM16Flash#::sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(unsigned char ucCommand)
40 {
41 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_pPort );
42
43 /* apparently this is important */
44 usleep(M16_DELAYcxx . . $ `cm16flash.h:36:9`!);
45
46 m_pPortcxx . . $ CM16Flash#m_pPort.->sendBytescxx . . $ CCOMPort#sendBytes(87fdc3b2e2b212a8).(&ucCommand, 1);
47
48 /* this can only succeed */
49 return 1;
50 }
51
52 /* send a byte without delay */
53 int CM16Flashcxx . . $ CM16Flash#::sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(unsigned char ucVal)
54 {
55 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_pPort );
56 m_pPortcxx . . $ CM16Flash#m_pPort.->sendBytescxx . . $ CCOMPort#sendBytes(87fdc3b2e2b212a8).(&ucVal, 1);
57
58 /* this can only succeed */
59 return 1;
60 }
61
62 /* do the initial comms as per the datasheet page 257 */
63 int CM16Flashcxx . . $ CM16Flash#::initCommscxx . . $ CM16Flash#initComms(b126dc7c1de90089).()
64 {
65 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_pPort );
66
67 unsigned char ucReply;
68 unsigned char ucSRDs[2];
69 int n;
70
71 /* set the board rate to required */
72 m_pPortcxx . . $ CM16Flash#m_pPort.->setPortOptionscxx . . $ CCOMPort#setPortOptions(4c679297bf94b9eb).(B9600cxx . . $ `<external>/14258770537406596697/termios-baud.h:37:9`!);
73
74 /* now follow what the datasheet says */
75 printf("\nLooking for M16C/R8C...");
76 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
77 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_CONFIRM_CLOCKcxx . . $ `cm16flash.h:35:9`!);
78
79 usleep(M16_DELAYcxx . . $ `cm16flash.h:36:9`!);
80
81 printf("\nConfirming...");
82 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
83
84 /* send the 16 0s */
85 for( n = 0; n < 16; n++ ) {
86 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(0);
87 }
88
89 usleep(M16_DELAYcxx . . $ `cm16flash.h:36:9`!);
90 m_pPortcxx . . $ CM16Flash#m_pPort.->flushcxx . . $ CCOMPort#flush(b126dc7c1de90089).();
91 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_CONFIRM_CLOCKcxx . . $ `cm16flash.h:35:9`!);
92
93 if( m_pPortcxx . . $ CM16Flash#m_pPort.->readBytescxx . . $ CCOMPort#readBytes(7405b416d0535413).(&ucReply, 1) == 0 ) {
94 printf("\nM16 Flash ERROR: the board did not respond.");
95 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
96 return 0;
97 }
98
99 if( M16_CONFIRM_CLOCKcxx . . $ `cm16flash.h:35:9`! == ucReply ) {
100 printf("board replied with clock confirm of %X.", ucReply);
101 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
102 }
103 else {
104 printf("board does not have a 10MHz or 16MHz clock, quitting.");
105 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
106 return 0;
107 }
108
109 if( readStatusRegisterscxx . . $ CM16Flash#readStatusRegisters(b2e27cdb9bef2230).(ucSRDs) == 0 ) {
110 printf("\nM16 Flash ERROR: failed reading back SRDs.");
111 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
112 return 0;
113 }
114
115 printf("\nStatusregister: %2X, %2X\n", ucSRDs[0], ucSRDs[1]);
116
117 return 1;
118 }
119
120 /* send it the ID so that we can send all other commands */
121 int CM16Flashcxx . . $ CM16Flash#::sendIDcxx . . $ CM16Flash#sendID(4ebf403dd87dd80).(unsigned int pIDCode[7])
122 {
123 long lAddress;
124 int n;
125 unsigned char ucSRDs[2];
126
127 printf("\nSending ID data %2X:%2X:%2X:%2X:%2X:%2X:%2X...", pIDCode[0],
128 pIDCode[1], pIDCode[2], pIDCode[3], pIDCode[4], pIDCode[5],
129 pIDCode[6]);
130 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
131
132 if (m_CPUTypecxx . . $ CM16Flash#m_CPUType. == R8C_CPUcxx . . $ `cm16flash.h:32:9`!) {
133 lAddress = R8C_ID_ADDRESScxx . . $ `cm16flash.h:61:9`!;
134 } else {
135 lAddress = M16_ID_ADDRESScxx . . $ `cm16flash.h:44:9`!;
136 }
137
138 /* we need to send 0x070FFFD4 according to the source (fanwp.pdp:138) */
139 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_ID_CHECKcxx . . $ `cm16flash.h:43:9`!);
140 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(ADDRESS_LOWcxx . . $ `cm16flash.h:29:9`!(lAddress));
141 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(ADDRESS_MIDDLEcxx . . $ `cm16flash.h:28:9`!(lAddress));
142 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(ADDRESS_HIGHcxx . . $ `cm16flash.h:27:9`!(lAddress));
143
144 /* send size of ID bytes */
145 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(M16_ID_SIZEcxx . . $ `cm16flash.h:45:9`!);
146
147 /* send the ID bytes */
148 for( n = 0; n < 7; n++ )
149 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(pIDCode[n]);
150
151 printf("OK.");
152 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
153
154 /* clear dstatus */
155 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(0x50);
156
157 /* read back SRDs to confirm ID was OK */
158 if( readStatusRegisterscxx . . $ CM16Flash#readStatusRegisters(b2e27cdb9bef2230).(ucSRDs) == 0 ) {
159 printf("\nM16 Flash ERROR: failed reading back SRDs after ID.");
160 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
161 return 0;
162 }
163
164 /* SRD1 bit 3 and bit 2 are set if ID verified ok (62aeds.pdf:254) */
165 if( ((ucSRDs[1] & M16_SR10cxx . . $ `cm16flash.h:47:9`!) == 0) || ((ucSRDs[1] & M16_SR11cxx . . $ `cm16flash.h:46:9`!) == 0) ) {
166 printf("\nWARNING: ID check did not succeed. ");
167 printf("Try cycling power or different ID.");
168 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
169 return 0;
170 }
171
172 /* getting here is very good */
173 printf("\nID verified, additional commands may now be issued.");
174 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
175 return 1;
176 }
177
178 /* read back SRD0 and SRD1 */
179 int CM16Flashcxx . . $ CM16Flash#::readStatusRegisterscxx . . $ CM16Flash#readStatusRegisters(b2e27cdb9bef2230).(unsigned char* pSRD)
180 {
181 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_pPort );
182 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( pSRD );
183
184 //printf("\nReading status registers...");
185 //fflush(stdout);
186
187 /* tell it we want a page of ROM at the address given */
188 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_READ_SRDcxx . . $ `cm16flash.h:40:9`!);
189
190 if( m_pPortcxx . . $ CM16Flash#m_pPort.->readBytescxx . . $ CCOMPort#readBytes(7405b416d0535413).(pSRD, 2) == 0 ) {
191 printf("\nM16 Flash ERROR: board timed out.");
192 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
193 return 0;
194 }
195
196 //printf("OK.");
197 //fflush(stdout);
198 return 1;
199 }
200
201 /* clear SRD0 and SRD1 */
202 int CM16Flashcxx . . $ CM16Flash#::clearStatusRegisterscxx . . $ CM16Flash#clearStatusRegisters(b126dc7c1de90089).()
203 {
204 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_pPort );
205
206 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_CLEAR_SRDcxx . . $ `cm16flash.h:41:9`!);
207
208 return 1;
209 }
210
211 int CM16Flashcxx . . $ CM16Flash#::readVersionInfocxx . . $ CM16Flash#readVersionInfo(1ef8e6ec083aabb9).(char* pszVersionInfo)
212 {
213 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_pPort );
214 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( pszVersionInfo );
215
216 printf("\nReading boot ROM version info... ");
217 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
218
219 /* tell it we want a page of ROM at the address given */
220 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_READ_VERSION_INFOcxx . . $ `cm16flash.h:42:9`!);
221
222 pszVersionInfo[8] = 0;
223
224 if( m_pPortcxx . . $ CM16Flash#m_pPort.->readBytescxx . . $ CCOMPort#readBytes(7405b416d0535413).((unsigned char*)pszVersionInfo, 8) == 0 ) {
225 printf("\nM16 Flash ERROR: board timed out.");
226 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
227 return 0;
228 }
229
•230 printf("%s", pszVersionInfo);
231 // printf("OK.");
232 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
233 return 1;
234 }
235
236 /* read a page of data back from the chip */
237 int CM16Flashcxx . . $ CM16Flash#::readPagecxx . . $ CM16Flash#readPage(e0fa5e7da13f4c7b).(CPagecxx . . $ CPage#* pPage)
238 {
239 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_pPort );
240 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( pPage );
241
242 printf("\nReading page at %lX...", pPage->getAddresscxx . . $ CPage#getAddress(da6335ded91d79c2).());
243 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
244
245 /* tell it we want a page of ROM at the address given */
246 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_READ_PAGEcxx . . $ `cm16flash.h:37:9`!);
247 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(ADDRESS_MIDDLEcxx . . $ `cm16flash.h:28:9`!(pPage->getAddress()));
248 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(ADDRESS_HIGHcxx . . $ `cm16flash.h:27:9`!(pPage->getAddress()));
249
250 /* await a reply */
251 if( m_pPortcxx . . $ CM16Flash#m_pPort.->readBytescxx . . $ CCOMPort#readBytes(7405b416d0535413).(pPage->getPagecxx . . $ CPage#getPage(37d8967d10cb1e7b).(), M16_PAGE_SIZEcxx . . $ `cm16flash.h:39:9`!) == 0 ) {
252 printf("\nM16 Flash ERROR: board timed out.");
253 perror("\n");
254 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
255 return 0;
256 }
257
258 printf("OK.");
259
260 return 1;
261 }
262
263 /* write a page of data to the chip, not erase must be capped first */
264 int CM16Flashcxx . . $ CM16Flash#::writePagecxx . . $ CM16Flash#writePage(e0fa5e7da13f4c7b).(CPagecxx . . $ CPage#* pPage)
265 {
266 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( pPage );
267 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_pPort );
268
269 int n;
270 unsigned char ucSRDs[2];
271
272 /* disable the lock bit - this might not be necessary after an erase */
273 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_LOCK_DISABLEcxx . . $ `cm16flash.h:48:9`!);
274
275 clearStatusRegisterscxx . . $ CM16Flash#clearStatusRegisters(b126dc7c1de90089).();
276
277 if( readStatusRegisterscxx . . $ CM16Flash#readStatusRegisters(b2e27cdb9bef2230).(ucSRDs) == 0 )
278 return 0;
279
280 /* check for success here */
281 //printf("\n\nSRD0 = %lX, SRD1 = %lX.", ucSRDs[0], ucSRDs[1]);
282 //fflush(stdout);
283 //printf("OK.");
284 //fflush(stdout);
285
286 printf("\nWriting page %6lX...", pPage->getAddresscxx . . $ CPage#getAddress(da6335ded91d79c2).());
287 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
288
289 /* we are writing a page */
290 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_PAGE_WRITEcxx . . $ `cm16flash.h:38:9`!);
291 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(ADDRESS_MIDDLEcxx . . $ `cm16flash.h:28:9`!(pPage->getAddress()));
292 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(ADDRESS_HIGHcxx . . $ `cm16flash.h:27:9`!(pPage->getAddress()));
293
294 for( n = 0; n < M16_PAGE_SIZEcxx . . $ `cm16flash.h:39:9`!; n++ ) {
295 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(pPage->getBytecxx . . $ CPage#getByte(bc0fba5608a0bfe7).(n));
296 // printf("\b\b\b\b\b\b\b\b\b%6lX...", pPage->getAddress() + n);
297 // fflush(stdout);
298 }
299
300 printf("OK.");
301 if ((m_CPUTypecxx . . $ CM16Flash#m_CPUType. == R8C_CPUcxx . . $ `cm16flash.h:32:9`!) && (pPage->getAddresscxx . . $ CPage#getAddress(da6335ded91d79c2).() == 0x00FF00)) {
302 printf(" New ID: %02X:%02X:%02X:%02X:%02X:%02X:%02X",
303 pPage->getBytecxx . . $ CPage#getByte(bc0fba5608a0bfe7).(0xDF),
304 pPage->getBytecxx . . $ CPage#getByte(bc0fba5608a0bfe7).(0xE3),
305 pPage->getBytecxx . . $ CPage#getByte(bc0fba5608a0bfe7).(0xEB),
306 pPage->getBytecxx . . $ CPage#getByte(bc0fba5608a0bfe7).(0xEF),
307 pPage->getBytecxx . . $ CPage#getByte(bc0fba5608a0bfe7).(0xF3),
308 pPage->getBytecxx . . $ CPage#getByte(bc0fba5608a0bfe7).(0xF7),
309 pPage->getBytecxx . . $ CPage#getByte(bc0fba5608a0bfe7).(0xFB));
310 }
311
312 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
313
314 clearStatusRegisterscxx . . $ CM16Flash#clearStatusRegisters(b126dc7c1de90089).();
315
316 /* now check if it worked */
317 if( readStatusRegisterscxx . . $ CM16Flash#readStatusRegisters(b2e27cdb9bef2230).(ucSRDs) == 0 )
318 return 0;
319
320 /* need to check value here */
321
322 return 1;
323
324 }
325
326 /* erase a block, which is not the same size as a page, (62aeds.pdf:220) */
327 int CM16Flashcxx . . $ CM16Flash#::eraseBlockcxx . . $ CM16Flash#eraseBlock(a9e4e82268fd7bfa).(long lAddress)
328 {
329 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!( m_pPort );
330
331 unsigned char ucSRDs[2];
332
333 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_LOCK_DISABLEcxx . . $ `cm16flash.h:48:9`!);
334
335 sendCommandBytecxx . . $ CM16Flash#sendCommandByte(b27dbea426b7b93).(M16_ERASE_BLOCKcxx . . $ `cm16flash.h:49:9`!);
336 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(ADDRESS_MIDDLEcxx . . $ `cm16flash.h:28:9`!(lAddress));
337 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(ADDRESS_HIGHcxx . . $ `cm16flash.h:27:9`!(lAddress));
338 sendBytecxx . . $ CM16Flash#sendByte(b27dbea426b7b93).(M16_ERASE_VERIFYcxx . . $ `cm16flash.h:50:9`!);
339
340 /* status register check should be done here */
341 readStatusRegisterscxx . . $ CM16Flash#readStatusRegisters(b2e27cdb9bef2230).(ucSRDs);
342
343 return 1;
344 }
345
346 /* erase the entire chip */
347 int CM16Flashcxx . . $ CM16Flash#::eraseChipcxx . . $ CM16Flash#eraseChip(b126dc7c1de90089).()
348 {
349 int n;
350 int count;
351 int lBlocks[7];
352
353 if (m_CPUTypecxx . . $ CM16Flash#m_CPUType. == R8C_CPUcxx . . $ `cm16flash.h:32:9`!) {
354
355 lBlocks[0] = R8C_BLOCK0_BEGINcxx . . $ `cm16flash.h:63:9`!;
356 lBlocks[1] = R8C_BLOCK1_BEGINcxx . . $ `cm16flash.h:64:9`!;
357 count = R8C_BLOCK_COUNTcxx . . $ `cm16flash.h:62:9`!;
358 } else {
359
360 lBlocks[6] = M16_BLOCK6_BEGINcxx . . $ `cm16flash.h:53:9`!;
361 lBlocks[5] = M16_BLOCK5_BEGINcxx . . $ `cm16flash.h:54:9`!;
362 lBlocks[4] = M16_BLOCK4_BEGINcxx . . $ `cm16flash.h:55:9`!;
363 lBlocks[3] = M16_BLOCK3_BEGINcxx . . $ `cm16flash.h:56:9`!;
364 lBlocks[2] = M16_BLOCK2_BEGINcxx . . $ `cm16flash.h:57:9`!;
365 lBlocks[1] = M16_BLOCK1_BEGINcxx . . $ `cm16flash.h:58:9`!;
366 lBlocks[0] = M16_BLOCK0_BEGINcxx . . $ `cm16flash.h:59:9`!;
367 count = M16_BLOCK_COUNTcxx . . $ `cm16flash.h:52:9`!;
368 }
369
370 /* erase every block on the chip */
371 for( n = 0; n < count; n++ ) {
372
373 /* we have to erase the page first */
374 printf("\nErasing block at %6X...", lBlocks[n]);
375 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
376
377 if( eraseBlockcxx . . $ CM16Flash#eraseBlock(a9e4e82268fd7bfa).(lBlocks[n]) == 0 ) {
378 printf("\nERROR.");
379 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
380 /* this needs some intelligence here */
381 return 1;
382 }
383
384 printf("OK.");
385 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
386 }
387
388 return 1;
389 }