Author Topic: Hardcoded product serial number in PIC18Fxxx  (Read 8894 times)

0 Members and 1 Guest are viewing this topic.

Offline matkarTopic starter

  • Regular Contributor
  • *
  • Posts: 153
  • Country: si
  • Sixty percent of the time it works EVERY time.
Hardcoded product serial number in PIC18Fxxx
« on: August 07, 2014, 03:33:27 pm »
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.
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 17211
  • Country: us
  • DavidH
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #1 on: August 07, 2014, 04:40:35 pm »
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.
 

Offline sunnyhighway

  • Frequent Contributor
  • **
  • Posts: 276
  • Country: nl
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #2 on: August 07, 2014, 04:44:49 pm »
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.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #3 on: August 07, 2014, 05:05:10 pm »
you  can probably write a small program for that. Otherwise, many 1-wire / eeprom devices have unique ID.

================================
https://dannyelectronics.wordpress.com/
 

Offline matkarTopic starter

  • Regular Contributor
  • *
  • Posts: 153
  • Country: si
  • Sixty percent of the time it works EVERY time.
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #4 on: August 07, 2014, 07:41:55 pm »
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?
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #5 on: August 07, 2014, 08:14:02 pm »
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.
================================
https://dannyelectronics.wordpress.com/
 

Offline wraper

  • Supporter
  • ****
  • Posts: 17661
  • Country: lv
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #6 on: August 07, 2014, 09:01:43 pm »
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.
 

Offline matkarTopic starter

  • Regular Contributor
  • *
  • Posts: 153
  • Country: si
  • Sixty percent of the time it works EVERY time.
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #7 on: August 07, 2014, 10:31:45 pm »
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 thread http://www.microchip.com/forums/tm.aspx?m=302919&mpage=1&key=&#302919 they talk about using command line scripts.

 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #8 on: August 07, 2014, 10:41:26 pm »
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.
================================
https://dannyelectronics.wordpress.com/
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4195
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #9 on: August 08, 2014, 06:29:32 am »
Quote
20.6 ID Locations
Eight memory locations (200000h-200007h) are
designated as ID locations, where the user can store
checksum or other code identification numbers. These
locations are both readable and writable during normal
execution through the TBLRD and TBLWT instructions
or during program/verify. The ID locations can be read
when the device is code-protected.
Your production script should request an ID from database, program the chip, program ID locations and enable protections.
 

Offline matkarTopic starter

  • Regular Contributor
  • *
  • Posts: 153
  • Country: si
  • Sixty percent of the time it works EVERY time.
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #10 on: August 08, 2014, 05:39:05 pm »
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: [Select]
:10000000020A0001020803040800000000000000A6
:1000100001010001020205060800000000000000C6
:1000200001010001020205060800000000000000B6
:00000001FF
In the first line 02 is the first byte in internal eeprom, 0A second, and so on.
The bold values represent the serial number: :10000000020A0001020803040800000000000000A6
My 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.FindAndReplace
incrementing: http://stackoverflow.com/questions/21102693/how-to-increment-a-value-in-a-txt-file-in-a-batch-file
and converting decimal values to/from hex: http://blogs.msdn.com/b/joshpoley/archive/2011/06/29/hex-conversion-via-a-batch-file.aspx
but unfortunately I'm not experienced in shell programming at all.

Code: [Select]
ECHO Incrementing serial number in hex file...

cd "C:"
REM Next_serial.txt contains the serial number for the next device to be programmed
if not exist Next_serial.txt >Next_serial.txt echo 0
for /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>null

I'd really appreciate if someone can give me some guidance on how it is done.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #11 on: August 08, 2014, 05:43:48 pm »
Quote
unfortunately I'm not experienced in shell programming at all.

You sure are good at C, right?

The last byte is checksum so it should change as well.
================================
https://dannyelectronics.wordpress.com/
 

Offline jaxbird

  • Frequent Contributor
  • **
  • Posts: 778
  • Country: 00
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #12 on: August 08, 2014, 05:55:25 pm »
...
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.

I'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.

Analog Discovery Projects: http://www.thestuffmade.com
Youtube random project videos: https://www.youtube.com/user/TheStuffMade
 

Offline matkarTopic starter

  • Regular Contributor
  • *
  • Posts: 153
  • Country: si
  • Sixty percent of the time it works EVERY time.
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #13 on: August 08, 2014, 07:14:14 pm »
You sure are good at C, right?

Firstly 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.


Quote
The last byte is checksum so it should change as well.

Thank 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: [Select]
@ECHO OFF
REM A command batch for programming a PIC device and increase the serial number in hex file after successful write
REM By: Mat Kar
SET DEVICE = P18Fxxx
ECHO PIC FLASH PROGRAMMER WITH SERIAL NUMBER AUTO INCREMENT
ECHO.
REM PK3CMD.exe, test.hex and this batch file must be in the same folder

REM For erasing the device enable the following block by commenting "GOTO skip_erase" line.
::*************************************************
GOTO skip_erase
ECHO Starting erase...
START /WAIT PK3CMD /P18Fxxx /V5.0 /E
IF %ERRORLEVEL% == 0 (
ECHO Device sucessfully erased!
) ELSE (
ECHO A problem occured during device erase!
ECHO Press any key to exit from erroneous erase.
pause>null
exit
)
:skip_erase
::*************************************************

REM For programming the device enable the following block by commenting "GOTO skip_program" line.
::*************************************************
GOTO skip_program
ECHO Starting programming...
START /WAIT PK3CMD /P18Fxxx /V5.0 /Ftest.hex
IF %ERRORLEVEL% == 0 (
ECHO Device sucessfully programmed!
) ELSE (
ECHO A problem occured during device programming!
ECHO Press any key to exit from erroneous programming.
pause>null
exit
)
:skip_program
::*************************************************

REM For incrementing the serial number after successful write enable the following block by commenting "GOTO skip_increment" line.
::*************************************************
GOTO skip_increment
REM here comes the code for serial number update......
ECHO Serial number successfully updated!
:skip_increment
::*************************************************

ECHO Press any key to exit.
pause>null

Note:
- 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.


« Last Edit: August 08, 2014, 07:22:47 pm by matkar »
 

Offline JTR

  • Regular Contributor
  • *
  • Posts: 107
  • Country: au
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #14 on: August 09, 2014, 01:11:42 pm »
Pretty certain that the pickit 3 now supports the SQTP standard and if so, there is a possible solution right there.
 

Offline matkarTopic starter

  • Regular Contributor
  • *
  • Posts: 153
  • Country: si
  • Sixty percent of the time it works EVERY time.
Re: Hardcoded product serial number in PIC18Fxxx
« Reply #15 on: August 09, 2014, 02:01:47 pm »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf