| 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" |
| 24 |
|
| 25 |
CM16Flash::CM16Flash(CCOMPort* pPort, unsigned char cuCPUType) |
| 26 |
{ |
| 27 |
assert( pPort ); |
| 28 |
|
| 29 |
m_pPort = pPort; |
| 30 |
m_CPUType = cuCPUType; |
| 31 |
} |
| 32 |
|
| 33 |
CM16Flash::~CM16Flash() |
| 34 |
{ |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
/* send a byte with the minimum delay */ |
| 39 |
int CM16Flash::sendCommandByte(unsigned char ucCommand) |
| 40 |
{ |
| 41 |
assert( m_pPort ); |
| 42 |
|
| 43 |
/* apparently this is important */ |
| 44 |
usleep(M16_DELAY); |
| 45 |
|
| 46 |
m_pPort->sendBytes(&ucCommand, 1); |
| 47 |
|
| 48 |
/* this can only succeed */ |
| 49 |
return 1; |
| 50 |
} |
| 51 |
|
| 52 |
/* send a byte without delay */ |
| 53 |
int CM16Flash::sendByte(unsigned char ucVal) |
| 54 |
{ |
| 55 |
assert( m_pPort ); |
| 56 |
m_pPort->sendBytes(&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 CM16Flash::initComms() |
| 64 |
{ |
| 65 |
assert( 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_pPort->setPortOptions(B9600); |
| 73 |
|
| 74 |
/* now follow what the datasheet says */ |
| 75 |
printf("\nLooking for M16C/R8C..."); |
| 76 |
fflush(stdout); |
| 77 |
sendCommandByte(M16_CONFIRM_CLOCK); |
| 78 |
|
| 79 |
usleep(M16_DELAY); |
| 80 |
|
| 81 |
printf("\nConfirming..."); |
| 82 |
fflush(stdout); |
| 83 |
|
| 84 |
/* send the 16 0s */ |
| 85 |
for( n = 0; n < 16; n++ ) { |
| 86 |
sendCommandByte(0); |
| 87 |
} |
| 88 |
|
| 89 |
usleep(M16_DELAY); |
| 90 |
m_pPort->flush(); |
| 91 |
sendCommandByte(M16_CONFIRM_CLOCK); |
| 92 |
|
| 93 |
if( m_pPort->readBytes(&ucReply, 1) == 0 ) { |
| 94 |
printf("\nM16 Flash ERROR: the board did not respond."); |
| 95 |
fflush(stdout); |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
if( M16_CONFIRM_CLOCK == ucReply ) { |
| 100 |
printf("board replied with clock confirm of %X.", ucReply); |
| 101 |
fflush(stdout); |
| 102 |
} |
| 103 |
else { |
| 104 |
printf("board does not have a 10MHz or 16MHz clock, quitting."); |
| 105 |
fflush(stdout); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
if( readStatusRegisters(ucSRDs) == 0 ) { |
| 110 |
printf("\nM16 Flash ERROR: failed reading back SRDs."); |
| 111 |
fflush(stdout); |
| 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 CM16Flash::sendID(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(stdout); |
| 131 |
|
| 132 |
if (m_CPUType == R8C_CPU) { |
| 133 |
lAddress = R8C_ID_ADDRESS; |
| 134 |
} else { |
| 135 |
lAddress = M16_ID_ADDRESS; |
| 136 |
} |
| 137 |
|
| 138 |
/* we need to send 0x070FFFD4 according to the source (fanwp.pdp:138) */ |
| 139 |
sendCommandByte(M16_ID_CHECK); |
| 140 |
sendByte(ADDRESS_LOW(lAddress)); |
| 141 |
sendByte(ADDRESS_MIDDLE(lAddress)); |
| 142 |
sendByte(ADDRESS_HIGH(lAddress)); |
| 143 |
|
| 144 |
/* send size of ID bytes */ |
| 145 |
sendByte(M16_ID_SIZE); |
| 146 |
|
| 147 |
/* send the ID bytes */ |
| 148 |
for( n = 0; n < 7; n++ ) |
| 149 |
sendByte(pIDCode[n]); |
| 150 |
|
| 151 |
printf("OK."); |
| 152 |
fflush(stdout); |
| 153 |
|
| 154 |
/* clear dstatus */ |
| 155 |
sendCommandByte(0x50); |
| 156 |
|
| 157 |
/* read back SRDs to confirm ID was OK */ |
| 158 |
if( readStatusRegisters(ucSRDs) == 0 ) { |
| 159 |
printf("\nM16 Flash ERROR: failed reading back SRDs after ID."); |
| 160 |
fflush(stdout); |
| 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_SR10) == 0) || ((ucSRDs[1] & M16_SR11) == 0) ) { |
| 166 |
printf("\nWARNING: ID check did not succeed. "); |
| 167 |
printf("Try cycling power or different ID."); |
| 168 |
fflush(stdout); |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
/* getting here is very good */ |
| 173 |
printf("\nID verified, additional commands may now be issued."); |
| 174 |
fflush(stdout); |
| 175 |
return 1; |
| 176 |
} |
| 177 |
|
| 178 |
/* read back SRD0 and SRD1 */ |
| 179 |
int CM16Flash::readStatusRegisters(unsigned char* pSRD) |
| 180 |
{ |
| 181 |
assert( m_pPort ); |
| 182 |
assert( 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 |
sendCommandByte(M16_READ_SRD); |
| 189 |
|
| 190 |
if( m_pPort->readBytes(pSRD, 2) == 0 ) { |
| 191 |
printf("\nM16 Flash ERROR: board timed out."); |
| 192 |
fflush(stdout); |
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
//printf("OK."); |
| 197 |
//fflush(stdout); |
| 198 |
return 1; |
| 199 |
} |
| 200 |
|
| 201 |
/* clear SRD0 and SRD1 */ |
| 202 |
int CM16Flash::clearStatusRegisters() |
| 203 |
{ |
| 204 |
assert( m_pPort ); |
| 205 |
|
| 206 |
sendCommandByte(M16_CLEAR_SRD); |
| 207 |
|
| 208 |
return 1; |
| 209 |
} |
| 210 |
|
| 211 |
int CM16Flash::readVersionInfo(char* pszVersionInfo) |
| 212 |
{ |
| 213 |
assert( m_pPort ); |
| 214 |
assert( pszVersionInfo ); |
| 215 |
|
| 216 |
printf("\nReading boot ROM version info... "); |
| 217 |
fflush(stdout); |
| 218 |
|
| 219 |
/* tell it we want a page of ROM at the address given */ |
| 220 |
sendCommandByte(M16_READ_VERSION_INFO); |
| 221 |
|
| 222 |
pszVersionInfo[8] = 0; |
| 223 |
|
| 224 |
if( m_pPort->readBytes((unsigned char*)pszVersionInfo, 8) == 0 ) { |
| 225 |
printf("\nM16 Flash ERROR: board timed out."); |
| 226 |
fflush(stdout); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
printf(pszVersionInfo); |
| 231 |
// printf("OK."); |
| 232 |
fflush(stdout); |
| 233 |
return 1; |
| 234 |
} |
| 235 |
|
| 236 |
/* read a page of data back from the chip */ |
| 237 |
int CM16Flash::readPage(CPage* pPage) |
| 238 |
{ |
| 239 |
assert( m_pPort ); |
| 240 |
assert( pPage ); |
| 241 |
|
| 242 |
printf("\nReading page at %lX...", pPage->getAddress()); |
| 243 |
fflush(stdout); |
| 244 |
|
| 245 |
/* tell it we want a page of ROM at the address given */ |
| 246 |
sendCommandByte(M16_READ_PAGE); |
| 247 |
sendByte(ADDRESS_MIDDLE(pPage->getAddress())); |
| 248 |
sendByte(ADDRESS_HIGH(pPage->getAddress())); |
| 249 |
|
| 250 |
/* await a reply */ |
| 251 |
if( m_pPort->readBytes(pPage->getPage(), M16_PAGE_SIZE) == 0 ) { |
| 252 |
printf("\nM16 Flash ERROR: board timed out."); |
| 253 |
perror("\n"); |
| 254 |
fflush(stdout); |
| 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 CM16Flash::writePage(CPage* pPage) |
| 265 |
{ |
| 266 |
assert( pPage ); |
| 267 |
assert( 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 |
sendCommandByte(M16_LOCK_DISABLE); |
| 274 |
|
| 275 |
clearStatusRegisters(); |
| 276 |
|
| 277 |
if( readStatusRegisters(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->getAddress()); |
| 287 |
fflush(stdout); |
| 288 |
|
| 289 |
/* we are writing a page */ |
| 290 |
sendCommandByte(M16_PAGE_WRITE); |
| 291 |
sendByte(ADDRESS_MIDDLE(pPage->getAddress())); |
| 292 |
sendByte(ADDRESS_HIGH(pPage->getAddress())); |
| 293 |
|
| 294 |
for( n = 0; n < M16_PAGE_SIZE; n++ ) { |
| 295 |
sendByte(pPage->getByte(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_CPUType == R8C_CPU) && (pPage->getAddress() == 0x00FF00)) { |
| 302 |
printf(" New ID: %02X:%02X:%02X:%02X:%02X:%02X:%02X", |
| 303 |
pPage->getByte(0xDF), |
| 304 |
pPage->getByte(0xE3), |
| 305 |
pPage->getByte(0xEB), |
| 306 |
pPage->getByte(0xEF), |
| 307 |
pPage->getByte(0xF3), |
| 308 |
pPage->getByte(0xF7), |
| 309 |
pPage->getByte(0xFB)); |
| 310 |
} |
| 311 |
|
| 312 |
fflush(stdout); |
| 313 |
|
| 314 |
clearStatusRegisters(); |
| 315 |
|
| 316 |
/* now check if it worked */ |
| 317 |
if( readStatusRegisters(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 CM16Flash::eraseBlock(long lAddress) |
| 328 |
{ |
| 329 |
assert( m_pPort ); |
| 330 |
|
| 331 |
unsigned char ucSRDs[2]; |
| 332 |
|
| 333 |
sendCommandByte(M16_LOCK_DISABLE); |
| 334 |
|
| 335 |
sendCommandByte(M16_ERASE_BLOCK); |
| 336 |
sendByte(ADDRESS_MIDDLE(lAddress)); |
| 337 |
sendByte(ADDRESS_HIGH(lAddress)); |
| 338 |
sendByte(M16_ERASE_VERIFY); |
| 339 |
|
| 340 |
/* status register check should be done here */ |
| 341 |
readStatusRegisters(ucSRDs); |
| 342 |
|
| 343 |
return 1; |
| 344 |
} |
| 345 |
|
| 346 |
/* erase the entire chip */ |
| 347 |
int CM16Flash::eraseChip() |
| 348 |
{ |
| 349 |
int n; |
| 350 |
int count; |
| 351 |
int lBlocks[7]; |
| 352 |
|
| 353 |
if (m_CPUType == R8C_CPU) { |
| 354 |
|
| 355 |
lBlocks[0] = R8C_BLOCK0_BEGIN; |
| 356 |
lBlocks[1] = R8C_BLOCK1_BEGIN; |
| 357 |
count = R8C_BLOCK_COUNT; |
| 358 |
} else { |
| 359 |
|
| 360 |
lBlocks[6] = M16_BLOCK6_BEGIN; |
| 361 |
lBlocks[5] = M16_BLOCK5_BEGIN; |
| 362 |
lBlocks[4] = M16_BLOCK4_BEGIN; |
| 363 |
lBlocks[3] = M16_BLOCK3_BEGIN; |
| 364 |
lBlocks[2] = M16_BLOCK2_BEGIN; |
| 365 |
lBlocks[1] = M16_BLOCK1_BEGIN; |
| 366 |
lBlocks[0] = M16_BLOCK0_BEGIN; |
| 367 |
count = M16_BLOCK_COUNT; |
| 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(stdout); |
| 376 |
|
| 377 |
if( eraseBlock(lBlocks[n]) == 0 ) { |
| 378 |
printf("\nERROR."); |
| 379 |
fflush(stdout); |
| 380 |
/* this needs some intelligence here */ |
| 381 |
return 1; |
| 382 |
} |
| 383 |
|
| 384 |
printf("OK."); |
| 385 |
fflush(stdout); |
| 386 |
} |
| 387 |
|
| 388 |
return 1; |
| 389 |
} |