Programming EEProm with uniq serial number

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Freaks,
I looking for an easy method to assign a serial number to each mega48 we program and store it in eeprom so it can spit it out when needed.
We currently use dos stk500 batch files in production to program our units using avrisp-mkii's.
So I guess I need a way to create a .eep file (hex file really) containing the unique serial number on demand. I can program the .eep file, I just need a way to gen it easily, on a WinXP box without requiring the person doing the programming to type in a number as that would be error prone. Any ideas?

TIA
JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to say that avr-objcopy makes it very easy to do binary to iHex file conversions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'll look at that, thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you know the address of the serial number, just write a C program or Matlab script (that can be called from the batch file) to create the EEPROM hex file. Just don't forget to include the end of record line!

Here's an example of program I wrote to create some unique "selfid" and "targetid" values in EEPROM for a wireless system. It reads a log file to see the last ID numbers used, increments them, updates an extant EEPROM hex file (making sure to update appropriate checksums), and updates the log file. I'm sure there are more efficient ways of doing it, but it works for me...

#include 
#include 


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Typdefs/Enums~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#define START_OF_IDS 			72		//Byte number of TargetID and SelfID: 72 - 79 (8 total) -- zero indexed
#define IDS_START_OF_LINE		46		//Byte number of beginning of line containing ID numbers (not including start of record: ':')
#define MAX_LINE_LENGTH			30		//Really 22
#define NUM_BYTES_PER_LINE		20		//Not including : or 

#define MAX_PERSONALITY_SIZE	255		//EEPROM = 4096 bytes, we start at address 3840

int main(void) {
	FILE *recordFile, *eepFile;
	char fileLog[] = "fileRecord.txt";
	char eepFileName[] = "systemEEP_mlb - Copy.eep";
	uint8_t readResult, i, tempIDLower, tempIDUpper;
	char tempChar[3];
	char lastSecKey[20] = {0};	//16 + 1 (NULL)
	uint32_t lastSelfID = 0;
	uint32_t lastTargetID = 0;
	uint8_t preIDLineData[IDS_START_OF_LINE] = {0};
	uint8_t postIDLineData[MAX_PERSONALITY_SIZE] = {0};
	uint8_t postIDctr = 0;
	uint8_t lineData[MAX_LINE_LENGTH] = {0};
	uint16_t checksum = 0;
	uint8_t checksum8 = 0;
	int32_t nextByte;
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//Begin section handling unique IDs~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	recordFile = fopen(fileLog, "r+");
	if(recordFile == NULL) {
		printf("\nFile did not exist.  Creating new one\n");
		recordFile = fopen(fileLog, "w");
		lastSelfID = 2;
		lastTargetID = 1;
		strcpy(lastSecKey, "123456789abcdefg");
	}
	else {
		do {
			readResult = fscanf(recordFile, "%d %d %s", &lastSelfID, &lastTargetID, lastSecKey);
		} while(readResult == 3);

		//Increment
		lastSelfID = lastSelfID + 2;
		lastTargetID = lastTargetID + 2;
		strcpy(lastSecKey, "123456789abcdefg");
	}
	
	fprintf(recordFile, "%d\t%d\t%s\n", lastSelfID, lastTargetID, lastSecKey);
	
	fclose(recordFile);

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//End section handling unique IDs~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//Begin section updating system EEPROM File~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	//Update EEPROM file
	eepFile = fopen(eepFileName, "r");
	if(eepFile == NULL) {
		printf("\nEEPROM file does not exist.\n");
	}
	else {
		//Get data before the line w/ IDs
		for(i = 0; i < IDS_START_OF_LINE; i++) {
			preIDLineData[i] = fgetc(eepFile);
		}
		//Get line of data w/ IDs
		for(i = 0; i < NUM_BYTES_PER_LINE; i++) {
			tempChar[0] = fgetc(eepFile);
			tempChar[1] = fgetc(eepFile);
			tempChar[2] = 0;

			lineData[i] = (uint8_t)strtol(tempChar, NULL, 16);
		}

		//Get remainder of data
		//First: get rid of old checkum (two bytes)
		fgetc(eepFile);
		fgetc(eepFile);
		//Then get rest of data
		for(i = 0; i < MAX_PERSONALITY_SIZE; i++) {
			nextByte = fgetc(eepFile);
			if(nextByte == EOF) {
				postIDctr = i - 1;
				break;
			}
			else {
				postIDLineData[i] = (char)nextByte;
			}
		}

		//Insert new ID numbers
		tempIDLower = (uint8_t)(lastSelfID & 0xFF);
		tempIDUpper = (uint8_t)(lastSelfID >> 8);
		lineData[13] = tempIDLower;
		lineData[14] = tempIDUpper;
		tempIDLower = (uint8_t)(lastTargetID & 0xFF);
		tempIDUpper = (uint8_t)(lastTargetID >> 8);
		lineData[15] = tempIDLower;
		lineData[16] = tempIDUpper;


		//Calculate new checksum for line:
		for(i = 0; i < NUM_BYTES_PER_LINE; i++) {
			checksum = checksum + lineData[i];
		}
		checksum = ~checksum + 1;
		checksum8 = (uint8_t)(checksum & 0xFF);

		fclose(eepFile);		//Close (done reading)

		eepFile = fopen(eepFileName, "w");	//Open for writing (over-write everything)

		//Re-write the lead in data (before line containing IDs)
		for(i = 0; i < IDS_START_OF_LINE; i++) {
			fputc(preIDLineData[i], eepFile);
		}
		//Write out lines containing IDs)
		fprintf(eepFile, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
		lineData[0], lineData[1], lineData[2], lineData[3], lineData[4], lineData[5], lineData[6],
		lineData[7], lineData[8], lineData[9], lineData[10], lineData[11], lineData[12], lineData[13],
		lineData[14], lineData[15], lineData[16], lineData[17], lineData[18], lineData[19]);
		fprintf(eepFile, "%02X", checksum8);

		fprintf(eepFile, "%s", postIDLineData);
		fclose(eepFile);

	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//End section updating system EEPROM File~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	return(0);
}

Note, if there is no other data in the EEPROM file, you can just create a new file from scratch, rather than editing an existing one.

Science is not consensus. Science is numbers.