Pulls unique serial number from CPU and prints to serial console and displays on LCD. Used to generate a guaranteed unique ID for IoT apps where the IoT device needs to become it's own access point. Should work on native hardware unlike the MBED MAC address which would not be available on some custom PCB. Tested on KL46Z but should also work on most STM CPU.

Dependencies:   SLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002   by Joseph Ellsworth CTO A2WH.
00003   Freeuse for all.  No promises,  No warranty *
00004   Please contact us http://a2wh.com for help with custom design projects.
00005 
00006   Most STM and Freescale ARM chips include a unique Serail Number embedded
00007   in the CPU.  This is a perfect number to use in IoT as a SSID.  I use
00008   this to produce the WIFi SID so I can walk up with a phone and download
00009   telemetry.   I found a blog discussion for mbed that suggested using the
00010   MAC but this assumes mbed hardware whereas this version uses the serial
00011   number built in to the CPU so it should work on native boards.
00012 */
00013 
00014 #include "mbed.h"
00015 #include "SLCD.h"
00016 //#include "lcd_scroll.h"
00017 
00018 
00019 #define uniqueSerialAdd_kl_freescale (unsigned long *)0x40048058
00020 #define uniqueSerialAddr_stm32 (unsigned long *)0x1FFFF7E8
00021 #define uniqueSerialAddr uniqueSerialAdd_kl_freescale
00022 #define lcdlen 4
00023 // Change to enable for address for your chip family
00024 // Note some ealry stm L3 chips were produced with all
00025 // chip ID set to FFF.  I have not seen any but did find
00026 // it mentioned in a blog.
00027 
00028 
00029 
00030 DigitalOut gpo(D0);
00031 DigitalOut led(LED_RED);
00032 Serial pc(USBTX, USBRX);
00033 SLCD slcd;
00034 
00035 
00036 /* format the unique serial number as a string. we could use
00037 as the router SID for Lot.  Supress leading zeros to save
00038 space */
00039 void getUniqueIDAsStr(char *destStr)
00040 {
00041     unsigned long *Unique = uniqueSerialAddr;
00042     sprintf(destStr, "S%luX%luX%luX\000", Unique[0], Unique[1], Unique[2]);
00043 }
00044 
00045 
00046 void scroll(int lcdLen, char *message, float charDelay)
00047 {
00048     short msgLen = strlen(message);
00049     short maxStartndx = msgLen - lcdLen;
00050     short currndx = 0;
00051     short currpos = 0;
00052     short winndx;
00053     slcd.clear();
00054 
00055     slcd.Home();
00056     for (currndx = 0; currndx <= maxStartndx; currndx++) {
00057         for (winndx = 0; winndx < lcdLen ; winndx++) {
00058             currpos = currndx + winndx;
00059             if (message[currpos] == 0) {
00060                 break;
00061             }
00062             slcd.putc(message[currpos]);
00063         }
00064         wait(charDelay);
00065     }
00066     currndx++;
00067 
00068 }
00069 
00070 
00071 int main()
00072 {
00073     pc.baud(9600);
00074     unsigned long *Unique = uniqueSerialAddr;
00075     printf("Simple Serial# %08luX %08luX %08luX\n", Unique[0], Unique[1], Unique[2]);
00076 
00077     char uniqueAsStr[33];
00078     getUniqueIDAsStr(uniqueAsStr);
00079     pc.printf("Serial as SID %s\n", uniqueAsStr);   // send back to PC
00080 
00081 
00082     while (true) {
00083         gpo = !gpo; // toggle pin
00084         led = !led; // toggle led
00085 
00086         slcd.clear();
00087         scroll(lcdlen, uniqueAsStr, 1.2);
00088         wait(2.0);
00089     }
00090 }