File: avrp.c

package info (click to toggle)
avrp 1.0beta3-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 204 kB
  • ctags: 194
  • sloc: ansic: 2,463; makefile: 40; perl: 6; sh: 5
file content (409 lines) | stat: -rw-r--r-- 11,876 bytes parent folder | download | duplicates (3)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/***********************************************************************
 *  avrp - Atmel AVR programming software to use with Atmel's
 *         serial-port programmers.
 *  Copyright (C) 1997-1998 Jon Anders Haugum
 *
 *  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; see the file COPYING.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *
 *
 *  Author of avrp can be reached at:
 *     email: jonah@colargol.tihlde.hist.no
 *     www: http://www.colargol.tihlde.hist.no/~jonah/el/avrp.html
 *     Postal address: Jon Anders Haugum
 *                     �vre M�llenbergsgt 52
 *                     7014 Trondheim
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "avrp.h"


const char *Title = "avrp 1.0 beta 3 (28 September 1998)\n"
                    "Copyright (C) 1997-1998 Jon Anders Haugum\n"
                    "   avrp comes with NO WARRANTY, to the extent permitted by law.\n"
                    "   You may redistribute copies of avrp under the terms\n"
                    "   of the GNU General Public License.\n"
                    "   For more information about these matters, see the files named COPYING.\n";

const char *Usage =
#ifdef __amigaos__
	"usage: avrp [-s <serial device:serial unit>] [-a <chip type>] [-prv]\n"
#else
	"usage: avrp [-s <serial port>] [-a <chip type>] [-prv]\n"
#endif
	"            [-f <filename>] [-e <filename>] [-l <lockmode>]\n"
        "            [--enable <fuse(s)>] [--disable <fuse(s)>]\n"
	"            [-d <deffile>] [-qIh] [--version] [--help]\n"
	"\n"
#ifdef __amigaos__
	"   -s : Specifies which serial port and unit to use (e.g. serial.device:0)\n"
#elif _WIN32
	"   -s : Specifies which serial port to use (e.g. COM2)\n"
#else
	"   -s : Specifies which serial port to use (e.g. /dev/ttyS1)\n"
#endif
	"   -a : Specifies chip to program/read (use '-a list' to get a list)\n"
	"   -p : Program a chip (-f or -e must be specified)\n"
	"   -r : Read a chip    (-f or -e must be specified)\n"
	"   -v : Verify         (-f or -e must be specified)\n"
	"   -f : Flashfile to write/read\n"
	"   -e : Eepromfile to write/read\n"
	"   -l : Lockmode (use '-l list' to get a list of modes)\n"
	"   -q : Quiet. Do not print out progress (little speedup)\n"
	"   -d : Specifies name and path to avrp.def\n"
	"   -I : Ignore chip signature\n"
	"   --enable   : Enable fuse(s)\n"
	"   --disable  : Disable fuse(s)\n"
	"   --autoinc  : Programmer support automatic address incrementation\n"
	"   --version  : Version information\n"
	"   --help, -h : This help text\n"
	"\n"
	"Report bugs to jonah@colargol.tihlde.hist.no\n";


int main(int argc, char *argv[])
	{
	int ShowUsage = False;
	char *temp_env;
	struct ProgramInfo *PI;
	struct args *args;

	args = alloc_args(ARG_COUNT);
	if(args)
		{
		define_arg(args, ARG_SERIALPORT, ARGTYPE_STRING,       's', NULL,      getenv("AVRP_SERIALPORT"));
		define_arg(args, ARG_AVRTYPE   , ARGTYPE_STRING,       'a', NULL,      getenv("AVRP_AVRTYPE"));
		define_arg(args, ARG_PROG      , ARGTYPE_BOOLEAN,      'p', NULL,      False);
		define_arg(args, ARG_READ      , ARGTYPE_BOOLEAN,      'r', NULL,      False);
		define_arg(args, ARG_VERIFY    , ARGTYPE_BOOLEAN,      'v', NULL,      False);
		define_arg(args, ARG_FLASH     , ARGTYPE_STRING,       'f', NULL,      NULL);
		define_arg(args, ARG_EEPROM    , ARGTYPE_STRING,       'e', NULL,      NULL);
		define_arg(args, ARG_QUICK     , ARGTYPE_BOOLEAN,      'q', NULL,      False);
		temp_env = getenv("AVRP_DEFFILE");
		define_arg(args, ARG_DEFFILE   , ARGTYPE_STRING,       'd', NULL,      temp_env ? temp_env : DEFFILENAME);
		define_arg(args, ARG_IGNORESIG , ARGTYPE_BOOLEAN,      'I', NULL,      False);
		define_arg(args, ARG_LOCKMODE  , ARGTYPE_STRING,       'l', NULL,      NULL);
		define_arg(args, ARG_ENABLE    , ARGTYPE_STRING_MULTI,   0, "enable",  NULL);
		define_arg(args, ARG_DISABLE   , ARGTYPE_STRING_MULTI,   0, "disable", NULL);
		define_arg(args, ARG_AUTOINC   , ARGTYPE_BOOLEAN,        0, "autoinc", False);
		define_arg(args, ARG_VER       , ARGTYPE_BOOLEAN,        0, "version", False);
		define_arg(args, ARG_HELP      , ARGTYPE_BOOLEAN,      'h', "help",    False);
		if(read_args(args, argc, argv))
			{
			if(!GET_ARG(args, ARG_HELP) && ((argc != 1) || GET_ARG(args, ARG_SERIALPORT)))
				{
				if(!GET_ARG(args, ARG_VER))
					{
					PI = GetPI(args);
					if(PI)
						{
						if(GET_ARG(args, ARG_SERIALPORT))
							{
							PI->Ser = OpenSer((char *)GET_ARG(args, ARG_SERIALPORT));
							if(PI->Ser)
								{
								if(InquireHW(PI))
									{
									if(DetectAVRType(PI))
										DoTheTalk(PI);
									}
								else
									printf("%s: No programmer found\n", (char *)GET_ARG(args, ARG_SERIALPORT));
								CloseSer(PI->Ser);
								}
							}
						else
#ifdef __amigaos__
#define AMIG_PRINT	 "Use: -s <serial device:serial unit>\n"
#else
#define AMIG_PRINT	 "Use: -s <serial port>\n"
#endif
							printf("Missing argument: No serial port specified\n"
								AMIG_PRINT
									 "Or set the environment variable: AVRP_SERIALPORT\n");
						FreePI(PI);
						}
					}
				else
					printf("%s", Title);
				}
			else
				ShowUsage = True;
			}
		free_args(args);
		}
	else
		{
		ShowUsage = True;
		printf("\n");
		}
	if(ShowUsage)
		{
		if(argc == 1)
			printf("%s\n", Title);
		printf("%s", Usage);
		}
	exit(EXIT_SUCCESS);
	}


int DetectAVRType(struct ProgramInfo *PI)
	{
	int several_match = False, chiplist = False;
	struct ChipNode *CN;

	if(GET_ARG(PI->args, ARG_AVRTYPE))
		{
		if(nocase_strcmp((char *)GET_ARG(PI->args, ARG_AVRTYPE), "list"))
			{
			for(CN = PI->FirstChipNode; CN; CN = CN->Next)
				if(nocase_strstr(CN->Name, (char *)GET_ARG(PI->args, ARG_AVRTYPE)))
					{
					if(PI->SelectedChipNode)
						several_match = True;
					PI->SelectedChipNode = CN;
					printf("Selected chip: %s %s %s with %dKB flash\n", CN->VendorNode->Name,
					       CN->FamilyNode->Name, CN->Name, CN->FamilyNode->FlashSize);
					}
			}
		else
			chiplist = True;
		if(several_match)
			printf("Error: There were several chips that matched: %s\n"
			       "       You must be more specific\n", (char *)GET_ARG(PI->args, ARG_AVRTYPE));
		else if(PI->SelectedChipNode)
			{
			if(PI->SelectedChipNode->Supported)
				return(True);
			else
				printf("Error: %s is not supported by your programmer\n", PI->SelectedChipNode->Name);
			}
		else
			{
			if(!chiplist)
				printf("Error: Unable to find a match to '%s' in %s\n", (char *)GET_ARG(PI->args, ARG_AVRTYPE),
				       (char *)GET_ARG(PI->args, ARG_DEFFILE));
			printf("The following chips are defined:\n");
			for(CN = PI->FirstChipNode; CN; CN = CN->Next)
				{
				if(CN->Supported)
					printf("   %s (%s %s with %d kB flash)\n", CN->Name, CN->VendorNode->Name,
					       CN->FamilyNode->Name, CN->FamilyNode->FlashSize);
				else
					printf("   %s (%s %s with %d kB flash) Not supported by programmer\n",
					       CN->Name, CN->VendorNode->Name, CN->FamilyNode->Name,
					       CN->FamilyNode->FlashSize);
				}
			}
		}
	else
		{
		printf("Missing argument: Type of AVR must be specified\n"
		       "Use: -a <AVR type>\n"
		       "Or set the environment variable: AVRP_AVRTYPE\n"
		       "Shortest unique abbreviation can be used\n"
		       "List of all supported chips can be obtained with: -a list\n");
		}
	return(False);
	}


struct ProgramInfo *GetPI(struct args *args)
	{
	int ok = False;
	struct ProgramInfo *PI;
	struct ChipNode *CN;
	struct VendorNode *VN;
	struct FamilyNode *FamN;
	struct ArchNode *AN;
	struct FuseNode *FuseN;
	struct LockNode *LN;

	PI = (struct ProgramInfo *)calloc(1, sizeof(struct ProgramInfo));
	if(PI)
		{
		PI->args = args;
		if(ReadDefFile(PI))
			{
			ok = True;
			for(FamN = PI->FirstFamilyNode; FamN; FamN = FamN->Next)
				{
				for(AN = PI->FirstArchNode; AN; AN = AN->Next)
					if(!strcmp(FamN->Name, AN->Name))
						{
						FamN->ArchNode = AN;
						break;
						}
				if(!FamN->ArchNode)
					{
					ok = False;
					printf("Error: %s: Can't find architecture %s\n",
					       (char *)GET_ARG(PI->args, ARG_DEFFILE), FamN->Name);
					}
				}
			for(CN = PI->FirstChipNode; CN; CN = CN->Next)
				{
				for(VN = PI->FirstVendorNode; VN; VN = VN->Next)
					if(VN->Code == ((CN->Sig >> 16) & 0xFF))
						{
						CN->VendorNode = VN;
						break;
						}
				for(FamN = PI->FirstFamilyNode; FamN; FamN = FamN->Next)
					if(FamN->Code == ((CN->Sig >> 8) & 0xFF))
						{
						CN->FamilyNode = FamN;
						break;
						}
				if(!(CN->VendorNode && CN->FamilyNode))
					{
					ok = False;
					printf("Error: %s: No vendor and family matched the signature of %s\n",
					       (char *)GET_ARG(PI->args, ARG_DEFFILE), CN->Name);
					}
				else if(!CN->VendorNode)
					{
					ok = False;
					printf("Error: %s: No vendor matched the signature of %s\n",
					       (char *)GET_ARG(PI->args, ARG_DEFFILE), CN->Name);
					}
				else if(!CN->FamilyNode)
					{
					ok = False;
					printf("Error: %s: No family matched the signature of %s\n",
					       (char *)GET_ARG(PI->args, ARG_DEFFILE), CN->Name);
					}
				if(CN->FuseType != 0)
					{
					for(FuseN = PI->FirstFuseNode; FuseN; FuseN = FuseN->Next)
						if(FuseN->Num == CN->FuseType)
							{
							CN->FuseNode = FuseN;
							break;
							}
					if(!CN->FuseNode)
						{
						ok = False;
						printf("Error: %s: Can't find fusetype %d for %s\n",
						       (char *)GET_ARG(PI->args, ARG_DEFFILE), CN->FuseType, CN->Name);
						}
					}
				if(CN->LockType != 0)
					{
					for(LN = PI->FirstLockNode; LN; LN = LN->Next)
						if(LN->Num == CN->LockType)
							{
							CN->LockNode = LN;
							break;
							}
					if(!CN->LockNode)
						{
						ok = False;
						printf("Error: %s: Can't find locktype %d for %s\n",
						       (char *)GET_ARG(PI->args, ARG_DEFFILE), CN->LockType, CN->Name);
						}
					}
				}
			}
		if(!ok)
			{
			FreePI(PI);
			PI = NULL;
			}
		}
	else
		printf("Error: Unable to allocate memory\n");
	return(PI);
	}


void FreePI(struct ProgramInfo *PI)
	{
	void *Node;
	struct ChipNode *CN;
	struct VendorNode *VN;
	struct FamilyNode *FamN;
	struct ArchNode *AN;
	struct FuseNode *FuseN;
	struct FuseBitNode *FBN;
	struct LockNode *LN;
	struct LockBitNode *LBN;

	for(CN = PI->FirstChipNode; CN;)
		{
		if(CN->Name) free(CN->Name);
		Node = CN;
		CN = CN->Next;
		free(Node);
		}
	for(VN = PI->FirstVendorNode; VN;)
		{
		if(VN->Name) free(VN->Name);
		Node = VN;
		VN = VN->Next;
		free(Node);
		}
	for(FamN = PI->FirstFamilyNode; FamN;)
		{
		if(FamN->Name) free(FamN->Name);
		Node = FamN;
		FamN = FamN->Next;
		free(Node);
		}
	for(AN = PI->FirstArchNode; AN;)
		{
		if(AN->Name) free(AN->Name);
		Node = AN;
		AN = AN->Next;
		free(Node);
		}
	for(FuseN = PI->FirstFuseNode; FuseN;)
		{
		for(FBN = FuseN->FirstFuseBitNode; FBN;)
			{
			if(FBN->ShortName) free(FBN->ShortName);
			if(FBN->LongName) free(FBN->LongName);
			Node = FBN;
			FBN = FBN->Next;
			free(Node);
			}
		Node = FuseN;
		FuseN = FuseN->Next;
		free(Node);
		}
	for(LN = PI->FirstLockNode; LN;)
		{
		for(LBN = LN->FirstLockBitNode; LBN;)
			{
			if(LBN->LongName) free(LBN->LongName);
			Node = LBN;
			LBN = LBN->Next;
			free(Node);
			}
		Node = LN;
		LN = LN->Next;
		free(Node);
		}
	free(PI);
	}