ser_no version 1.01
Tue Aug 31 09:35:40 EDT 2010

A program to create sequential serial numbers in a format directly usable by AVRDUDE.

By John DeArmond, jgd@fluxeon.com

This is freeware.  Do with it what you like. I'd prefer that you leave my name attached
but won't cry if you don't.

The Problem

A commercial product that I have designed must be serialized.  I'm using the AT90PWM316
which is not supported by the inexpensive stand-alone programmers that do
serialization.  And unfortunately AVRDUDE doesn't support serialization.
However, AVRDUDE does have the "write immediate" sub-option of -U that lets me
accomplish my goal.

The Solution

The solution is two parts.  One is a C program called ser_no.c.  This program
generates a sequential serial number every time it is invoked.  It also
maintains a simple database of serial numbers with time and date stamps.
This database is a simple flat tab delimited file that can be read into
a spreadsheet or database so that other data such as customer names 
can be associated with the serial numbers.

The other part is a *nix shell script called "atmel" that runs things.  It invokes 
ser_no to get the next serial number and then invokes AVRDUDE to do the 
actual programming.

There is a second C program called "gen_file" which generates the initial 
serial number database file. It is quite flexible, as we'll see in a minute.

Zip contents

This package comes in a zip file that contains the following:


atmel		The script that does the actual programming	
c		Compiler script.  Not worth a make file
gen_file.c	The source for gen_file
Readme.txt	This file
serial_no.txt	An example database file.  This is the default if none is specified
ser_no.c	The C source for ser_no.


Ser_no

If ser_no is invoked with the -? option, it will display the following.


Usage: ser_no [options]
Options:
   -?                         Display this usage.
   -h                         Display this usage.
   -f file                    File that contains the serial number.
   -d date                    Over-ride system date.
   -r                         Repeat the last number
   -R                         Specify the PCB revision number
   -e                         rEvert - discard last serial number.

version 1.02 09/01/2010 


The options are as follows:

-f file		Specify a file name.  This name should be a fully qualified
		path to the serial number database file.  If -f is not 
		specified then ser_no looks for "./serial_no.txt"

-d date		Lets you substitute any string for the system date to be 
		written to the second field of the database record.  If this
		option is not used then the system date is written

-e		rEvert.  Push back the last emitted serial number.  Physically,
		what happens is that the last record of the serial number file
		is truncated.  This option is useful when the subsequent 
		device programming fails for some reason.  ser_no emits
		no output when this option is invoked.

-R		Specify the PCB revision code.  This gets stored ONLY in the database.

-r		Repeat the last number

Output

ser_no's normal output is thus


0,0,0,0,0,0,0,0,5,6

That is, the serial number 0000000056.  The next invocation of the program generates


0,0,0,0,0,0,0,0,5,7

Why all the commas?  Because AVRDUDE requires them to do a multi-byte 
"write immediate" using the -U option.

In my products I reserve the first 10 bytes of EEROM for the serial number 
and the "atmel" script reflects that.  You can change where it is written by
editing the "atmel" script.  You can change the size of the serial number
by editing ser_no.c and recompiling.

Gen_File

gen_file is a quick and dirty utility to initialize a database.  ser_no requires 
that the database exist before it will run.  I did it this way deliberately to make 
sure that the intended database is updated upon each invocation.

Gen_file must have exactly 4 arguments of the form

gen_file  file_name start_number end_number pcb_rev_number

The two numbers can be the same, in which case gen_file will create "file_name" 
containing one record.  A good way to start a new database is

gen_file serial_no.txt 0 0 1234

That writes one record with the serial number 0.  Therefore the first device 
programmed starts at 1.

This is quite flexible.  You can specify any numbers up to 10 digits long.  You can
use this facility to, for example, encode the revision number in the serial number.

Example:
	Suppose that the code revision is 1.23 and you want to start the serial numbers
	at 100.  Invoke gen_file thus:

genfile serial_no.txt 1230000100 1230000100

That creates a database file with one record, the serial number being 1230000100.
The first invocation of ser_no would generate the serial number

1,2,3,0,0,0,0,1,0,1

and the next

1,2,3,0,0,0,0,1,0,2

Database Format

This is an example of the database that is contained in serial_no.txt


1234000000	5678	Tue Aug 31 09:27:46 2010
1234000001	5678	Tue Aug 31 09:28:09 2010

There is a tab between the fields, making it easy to parse as a tab
delimited file into database programs and spreadsheets.

Windows Users

You're outta luck from this end.  I'm a Linux dude through and through.  I did
make a feeble attempt to compile the code for Windows using a cross compiler
but the library seemed to not support truncate() so that didn't fly.  Even
without truncate(), the Windows version contaminated the database file with
those pesky ^M characters at the end of each line.

This program will compile under Cygwin using gcc. 
If someone tries it, please let me know.  I'm not sure how the -e option
will work since the program depends on there being a single "newline" 
character at the end of each line of text. Maybe the windows version of 
the stdlib will patch over that.  You'll just have to test and see.

Closing

I'm not hosting this program in any particular place.  I put it up on 
http://www.avrfreaks.com as a project.  STRIKE THAT!  They didn't want it.  I'll
probably just put it on my website and let google lead you there.

Update:  Program is now hosted on my site at: http://www.neon-john.com/Programming/Index.htm
	The current revision will always be there.

Given the simplicity of what it
does, I doubt that there will be any revisions to the program.  You can
always write and ask. I'm at jgd@fluxeon.com or jgd@neon-john.com. My wonderful
and exciting website is http://www.neon-john.com.  And if you happen to ever
need a small induction heater, be sure to check out http://www.fluxeon.com.

If you hack something else useful into the program, I'd love to receive
a copy.

John DeArmond
Aug 31, 2010
-- end --

