| 1 |
/*************************************************************************** |
| 2 |
flashm16.cxx - A flash programmer for an M16C/62. |
| 3 |
------------------- |
| 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 |
|
| 10 |
2006-08-22: set version to "0.1" |
| 11 |
2005-11-18: modified by Thomas Fischl <[email protected]> |
| 12 |
- added support for R8C |
| 13 |
- added support for S1-mot-files |
| 14 |
2005-11-20: - minor fixes |
| 15 |
- show new ID when written |
| 16 |
|
| 17 |
***************************************************************************/ |
| 18 |
|
| 19 |
/*************************************************************************** |
| 20 |
* * |
| 21 |
* This program is free software; you can redistribute it and/or modify * |
| 22 |
* it under the terms of the GNU General Public License as published by * |
| 23 |
* the Free Software Foundation; either version 2 of the License, or * |
| 24 |
* (at your option) any later version. * |
| 25 |
* * |
| 26 |
***************************************************************************/ |
| 27 |
|
| 28 |
|
| 29 |
/* |
| 30 |
|
| 31 |
A flash programmer for the M16C/62. |
| 32 |
|
| 33 |
GMR 06-September-2002 with (as ever)inspired additions by Jim on 18-September-2002. |
| 34 |
|
| 35 |
TTDs: |
| 36 |
Make sure the COM port times out after a while - rather than waiting forever. |
| 37 |
|
| 38 |
Make sure that the status registers are checked after every operation - not done at the |
| 39 |
moment. |
| 40 |
|
| 41 |
Give the user the option to change the baud rate, currently defaults to the safe but |
| 42 |
slow 9600. |
| 43 |
|
| 44 |
Give the user the option to read back pages and verify them against what was supposed |
| 45 |
to have been written (all the code for reading has been written, just simple stuff |
| 46 |
required). |
| 47 |
|
| 48 |
Embed the PIC16F84 programmer source into this. |
| 49 |
|
| 50 |
Add all the functions that can be used in the Windows version. |
| 51 |
*/ |
| 52 |
|
| 53 |
#include "cm16flash.h" |
| 54 |
|
| 55 |
#define VERSION "0.1" |
| 56 |
|
| 57 |
void displayUsage(void) |
| 58 |
{ |
| 59 |
printf("Programs an M16C/62 or R8C microcontroller in asynchronous mode.\n\n"); |
| 60 |
printf("Usage: flash [COM_DEV] [CPU_TYPE] [FILE] [ID1:...:ID7] [OPTIONS]\n"); |
| 61 |
printf("Examples:\n"); |
| 62 |
printf("\t flash /dev/ttyS1 M16C simple.s 0:0:0:0:0:0:0\n"); |
| 63 |
printf("\t flash /dev/ttyS1 R8C test.mot ff:ff:ff:ff:ff:ff:ff\n\n"); |
| 64 |
fflush(stdout); |
| 65 |
} |
| 66 |
|
| 67 |
int main(int argc, char* argv[]) |
| 68 |
{ |
| 69 |
CCOMPort port; |
| 70 |
unsigned char ucSRDs[2]; |
| 71 |
char szVersion[9]; |
| 72 |
unsigned int nID[7] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 73 |
CMOTFile mot; |
| 74 |
CPage* p; |
| 75 |
unsigned char ucCPUType = 0; |
| 76 |
|
| 77 |
printf("M16C-Flash Programmer Version %s\n", VERSION); |
| 78 |
fflush(stdout); |
| 79 |
|
| 80 |
/* check for sufficient parameters */ |
| 81 |
if( argc < 5 ) { |
| 82 |
displayUsage(); |
| 83 |
exit(-1); |
| 84 |
} |
| 85 |
|
| 86 |
/* check for a COM port device */ |
| 87 |
if( port.openPort(argv[1]) == 0 ) { |
| 88 |
printf("\nM16 Flash Error: could not open specified serial device.\n"); |
| 89 |
fflush(stdout); |
| 90 |
exit(-1); |
| 91 |
} |
| 92 |
|
| 93 |
/* now check the CPU type */ |
| 94 |
if( strcmp(argv[2], "M16C") == 0 ) { |
| 95 |
ucCPUType = M16_CPU; |
| 96 |
} else if( strcmp(argv[2], "R8C") == 0 ) { |
| 97 |
ucCPUType = R8C_CPU; |
| 98 |
} |
| 99 |
|
| 100 |
if (ucCPUType == 0) { |
| 101 |
printf("\nM16 Fash Error: you must specify a cpu type.\n"); |
| 102 |
fflush(stdout); |
| 103 |
exit(-1); |
| 104 |
} |
| 105 |
|
| 106 |
/* now try and read the file they have given us */ |
| 107 |
if( mot.readFile(argv[3], M16_PAGE_SIZE) == 0 ) { |
| 108 |
printf("\nM16 Flash Error: the input file was not acceptable.\n"); |
| 109 |
fflush(stdout); |
| 110 |
|
| 111 |
/* free up the pages so we can make a clean exit */ |
| 112 |
if( mot.getFirstPage() != NULL ) |
| 113 |
delete mot.getFirstPage(); |
| 114 |
|
| 115 |
port.closePort(); |
| 116 |
exit(-1); |
| 117 |
} |
| 118 |
|
| 119 |
/* get the ID */ |
| 120 |
if( sscanf(argv[4], "%2X:%2X:%2X:%2X:%2X:%2X:%2X", |
| 121 |
&nID[0], |
| 122 |
&nID[1], |
| 123 |
&nID[2], |
| 124 |
&nID[3], |
| 125 |
&nID[4], |
| 126 |
&nID[5], |
| 127 |
&nID[6]) != 7 ) { |
| 128 |
|
| 129 |
printf("\nM16 Flash Error: badly formatted ID string.\n"); |
| 130 |
fflush(stdout); |
| 131 |
|
| 132 |
/* free up the pages so we can make a clean exit */ |
| 133 |
if( mot.getFirstPage() != NULL ) |
| 134 |
delete mot.getFirstPage(); |
| 135 |
|
| 136 |
port.closePort(); |
| 137 |
exit(-1); |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
/* create the CM16Flash object */ |
| 142 |
CM16Flash flash(&port, ucCPUType); |
| 143 |
|
| 144 |
/* do the initial probing for the device */ |
| 145 |
if( flash.initComms() == 0 ) { |
| 146 |
|
| 147 |
/* free up the pages so we can make a clean exit */ |
| 148 |
if( mot.getFirstPage() != NULL ) |
| 149 |
delete mot.getFirstPage(); |
| 150 |
|
| 151 |
port.closePort(); |
| 152 |
exit(-1); |
| 153 |
} |
| 154 |
|
| 155 |
/* get status registers */ |
| 156 |
if( flash.readStatusRegisters(ucSRDs) == 0 ) { |
| 157 |
printf("\nM16 Flash Error: could not read status registers.\n"); |
| 158 |
fflush(stdout); |
| 159 |
|
| 160 |
/* free up the pages so we can make a clean exit */ |
| 161 |
if( mot.getFirstPage() != NULL ) |
| 162 |
delete mot.getFirstPage(); |
| 163 |
|
| 164 |
port.closePort(); |
| 165 |
exit(-1); |
| 166 |
} |
| 167 |
|
| 168 |
/* get boot ROM version info */ |
| 169 |
if( flash.readVersionInfo(szVersion) == 0 ) { |
| 170 |
printf("\nM16 Flash Error: couldn't read boot loader ROM code version.\n"); |
| 171 |
fflush(stdout); |
| 172 |
|
| 173 |
/* free up the pages so we can make a clean exit */ |
| 174 |
if( mot.getFirstPage() != NULL ) |
| 175 |
delete mot.getFirstPage(); |
| 176 |
|
| 177 |
port.closePort(); |
| 178 |
exit(-1); |
| 179 |
} |
| 180 |
|
| 181 |
/* send ID check */ |
| 182 |
if( flash.sendID(nID) == 0 ) { |
| 183 |
printf("\nM16 Flash Error: ID check failed.\n"); |
| 184 |
fflush(stdout); |
| 185 |
|
| 186 |
/* free up the pages so we can make a clean exit */ |
| 187 |
if( mot.getFirstPage() != NULL ) |
| 188 |
delete mot.getFirstPage(); |
| 189 |
|
| 190 |
port.closePort(); |
| 191 |
exit(-1); |
| 192 |
} |
| 193 |
|
| 194 |
/* erase the chip */ |
| 195 |
if( flash.eraseChip() == 0 ) { |
| 196 |
printf("\nM16 Flash Error: erasing of all blocks failed.\n"); |
| 197 |
fflush(stdout); |
| 198 |
|
| 199 |
/* free up the pages so we can make a clean exit */ |
| 200 |
if( mot.getFirstPage() != NULL ) |
| 201 |
delete mot.getFirstPage(); |
| 202 |
|
| 203 |
port.closePort(); |
| 204 |
exit(-1); |
| 205 |
} |
| 206 |
|
| 207 |
/* this is the loop that does the writing */ |
| 208 |
p = mot.getFirstPage(); |
| 209 |
while (p != NULL) { |
| 210 |
flash.writePage(p); |
| 211 |
p = p->getNext(); |
| 212 |
} |
| 213 |
|
| 214 |
/* free up the pages so we can make a clean exit */ |
| 215 |
if( mot.getFirstPage() != NULL ) |
| 216 |
delete mot.getFirstPage(); |
| 217 |
|
| 218 |
port.closePort(); |
| 219 |
|
| 220 |
printf("\nfinished.\n"); |
| 221 |
|
| 222 |
return 0; |
| 223 |
} |