Hardcoded product serial number in PIC18Fxxx - Page 1
Forum section: electronics / general

author: matkar
author_rank: Regular Contributor
timestamp: August 07, 2014, 03:33:27 pm
content: I'm searching for a fast way to make multiple fabricated product distinct by a serial number. I could dedicate some EEPROM memory for it and write one by one from a PC application over a serial connection, but I think there must be better solutions.How is it done professionally? Is it possible to set a mechanism in MPLAB(x) that increases the value at desired flash (or eeprom) location automatically every time I flash a device? I have PICKIT3 and ICD2 programmers.Is it possible to record a list of programmed serial numbers so I can later print product labels with serial number?I'm mainly interested in PICs but it won't hurt to know what you use on other microcontrollers.

author: David Hess
author_rank: Super Contributor
timestamp: August 07, 2014, 04:40:35 pm
content: I thought MPLAB supported programming an incremented serial number.Microchip makes series EEPROMs with preprogrammed unique serial numbers. There are also chips with preprogrammed unique Ethernet MAC addresses available.

author: sunnyhighway
author_rank: Frequent Contributor
timestamp: August 07, 2014, 04:44:49 pm
content: Any 1-WIRE device would serve your purpose, as they already have a unique address (aka serial-number) on them.The simplest one is the DS2401, which has no other functionality than being addressable.

author: dannyf
author_rank: Super Contributor
timestamp: August 07, 2014, 05:05:10 pm
content: you can probably write a small program for that. Otherwise, many 1-wire / eeprom devices have unique ID.

author: matkar
author_rank: Regular Contributor
timestamp: August 07, 2014, 07:41:55 pm
content: MAC address eeproms are not an option. It doesn't have to be a unique serial number and the circuit design has already been made.I'd like this number on a microcontroller. Till now I'm aware of this options:1. Reserve a location in eeprom and write a piece of code that will write the serial number on a special command received from a dedicated PC application using eg. serial port.2. Increment flash or eeprom defined serial number value in firmware and recompile it every time before flashing.3. Figuring out which location(s) in hex file represent the serial number and change those values accordingly before flashing.But none of the above options are "automatic". I'm the one who has to be conscious not to make a mistake (burn two devices with the same number, setting a wrong value, different values in flash and on the label...)How is this done on the industrial level?I haven't found this option in MPLAB. I might have overlooked.Are there some universal programmers that have this capability?

author: dannyf
author_rank: Super Contributor
timestamp: August 07, 2014, 08:14:02 pm
content: Two solutions:1) use a mcu with unique ID;2) code for the possibility of externally requesting and programming an ID: the code could be written so that when it boots up, it looks to see if it has a valid ID. If not, it will request one and programmed that new ID. The request can be answered by another mcu that keeps track of the previous ID and upon each request, increments the ID.However, such a system can be easily bypassed but I guess that's not your goal anyway.

author: wraper
author_rank: Unknown
timestamp: August 07, 2014, 09:01:43 pm
content: Never developed a device on PIC and never used MPLAB. But likely will need soon with individual serial requirement. Therefore did a brief search about it. Seems that there is some SQTP thing for this purpose in advanced mode. So might be worth to google about it.

author: matkar
author_rank: Regular Contributor
timestamp: August 07, 2014, 10:31:45 pm
content: I've looked at some universal programmers and it seems these devices usually have auto increment function. For example:http://www.elnec.com/products/universal-programmers/smartprog2/A PRO MATE programmer and already mentioned SQTP from Microchip does the trick as well.Both solutions are a bit costly though.In this threadhttp://www.microchip.com/forums/tm.aspx?m=302919&mpage=1&key=&#302919they talk about using command line scripts.

author: dannyf
author_rank: Super Contributor
timestamp: August 07, 2014, 10:41:26 pm
content: The 2nd guy had the right idea. the hex file has an identifier for eeprom, followed by a standard hex file lines that specify the address + content of the eeprom.You can write a piece of code to read and modify that file and then use the command line tool to burn the hex file.

author: Jeroen3
author_rank: Super Contributor
timestamp: August 08, 2014, 06:29:32 am
content: QuoteYour production script should request an ID from database, program the chip, program ID locations and enable protections.

author: matkar
author_rank: Regular Contributor
timestamp: August 08, 2014, 05:39:05 pm
content: In a pursuit to automate the matter as much as possible I've decided to try to write a batch script. I want to increment the value on the location, where serial number is written in hex file. Here are the last four lines of my hex file:Code::10000000020A0001020803040800000000000000A6:1000100001010001020205060800000000000000C6:1000200001010001020205060800000000000000B6:00000001FFIn the first line 02 is the first byte in internal eeprom, 0A second, and so on.The bold values represent the serial number: :10000000020A0001020803040800000000000000A6My idea is to search for the line that starts with the correct string ":10000000", read the string from 13th to 16th character in that line, transform the value to decimal, increment the value by 1, transform back to hex string and write it back in file to the same location.I have found some references discussing finding and replacing strings:http://www.dostips.com/DtCodeBatchFiles.php#Batch.FindAndReplaceincrementing:http://stackoverflow.com/questions/21102693/how-to-increment-a-value-in-a-txt-file-in-a-batch-fileand converting decimal values to/from hex:http://blogs.msdn.com/b/joshpoley/archive/2011/06/29/hex-conversion-via-a-batch-file.aspxbut unfortunately I'm not experienced in shell programming at all.Code:ECHO Incrementing serial number in hex file...cd "C:"REM Next_serial.txt contains the serial number for the next device to be programmedif not exist Next_serial.txt >Next_serial.txt echo 0for /f %%x in (Next_serial.txt) do (set /a var=%%x+1)>Next_serial.txt echo %var%ECHO Serial number successfully updated!ECHO Press any key to exit.pause>nullI'd really appreciate if someone can give me some guidance on how it is done.

author: dannyf
author_rank: Super Contributor
timestamp: August 08, 2014, 05:43:48 pm
content: QuoteYou sure are good at C, right?The last byte is checksum so it should change as well.

author: jaxbird
author_rank: Frequent Contributor
timestamp: August 08, 2014, 05:55:25 pm
content: Quote from: dannyf on August 07, 2014, 08:14:02 pmI've used this method, outlined by dannyf, with great success in previous projects. My main requirement was not globally uniqueness, just local, but using this method means you don't need to recompile or modify the hex for each item.If you need it to be globally unique, just use a good GUID generating algorithm.you could even implement it internally in the mcu if you have enough 'random' parameters to base your 'GUID' algorithm on.

author: matkar
author_rank: Regular Contributor
timestamp: August 08, 2014, 07:14:14 pm
content: Quote from: dannyf on August 08, 2014, 05:43:48 pmFirstly I tought of writing it in VB, but decided to try it in shell. Before this I have written a batch that programs the microcontroller with specified hex. By using this two batches (or even merging the two together) I have a fully automatic method using a PICKIT3 programmer which I already have.Plus I get an easy access to change parameters (part and hex name) by editing batch files instead of recompiling an exe application.QuoteThank you very much for the warning. I would have missed that...Here is a batch that erases and programs the microcontroller with specified hex:Code:@ECHO OFFREM A command batch for programming a PIC device and increase the serial number in hex file after successful writeREM By: Mat KarSET DEVICE = P18FxxxECHO PIC FLASH PROGRAMMER WITH SERIAL NUMBER AUTO INCREMENTECHO.REM PK3CMD.exe, test.hex and this batch file must be in the same folderREM For erasing the device enable the following block by commenting "GOTO skip_erase" line.::*************************************************GOTO skip_eraseECHO Starting erase...START /WAIT PK3CMD /P18Fxxx /V5.0 /EIF %ERRORLEVEL% == 0 (ECHO Device sucessfully erased!) ELSE (ECHO A problem occured during device erase!ECHO Press any key to exit from erroneous erase.pause>nullexit):skip_erase::*************************************************REM For programming the device enable the following block by commenting "GOTO skip_program" line.::*************************************************GOTO skip_programECHO Starting programming...START /WAIT PK3CMD /P18Fxxx /V5.0 /Ftest.hexIF %ERRORLEVEL% == 0 (ECHO Device sucessfully programmed!) ELSE (ECHO A problem occured during device programming!ECHO Press any key to exit from erroneous programming.pause>nullexit):skip_program::*************************************************REM For incrementing the serial number after successful write enable the following block by commenting "GOTO skip_increment" line.::*************************************************GOTO skip_incrementREM here comes the code for serial number update......ECHO Serial number successfully updated!:skip_increment::*************************************************ECHO Press any key to exit.pause>nullNote:- instead of P18Fxxx put the exact part number- if you power the device from an external power supply omit "/V5.0" from START /WAIT PK3CMD /P18Fxxx/V5.0/Ftest.hex and START /WAIT PK3CMD /P18Fxxx/V5.0/E .If you want to power the device form PICKIT3 verify part voltage and set the voltage accordingly. For 3.3V devices write /V3.3 instead of /V5.0.

author: JTR
author_rank: Regular Contributor
timestamp: August 09, 2014, 01:11:42 pm
content: Pretty certain that the pickit 3 now supports the SQTP standard and if so, there is a possible solution right there.

author: matkar
author_rank: Regular Contributor
timestamp: August 09, 2014, 02:01:47 pm
content: Yes, since MPLAB version 8.33 it is supposed to support this, but I can't find a single reference on how to set this up. In PK3CMD help I can see there is support for SQTP, but how to use it?Why don't they just add this functionality into MPLAB like there is a SQTP tab for PM3 programmer under PM3's Settings?If it is possible to do it, please let me know how.