Skip to content
Snippets Groups Projects
Commit 3bbb3bd3 authored by Micah Elizabeth Scott's avatar Micah Elizabeth Scott
Browse files

Use the entire 128-bit CPU serial number

parent aa7839e7
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,6 @@ OPTIONS = -DF_CPU=48000000 ...@@ -22,7 +22,6 @@ OPTIONS = -DF_CPU=48000000
# Sources # Sources
C_FILES = \ C_FILES = \
mk20dx128.c \ mk20dx128.c \
nonstd.c \
pins_teensy.c \ pins_teensy.c \
usb_desc.c \ usb_desc.c \
usb_dev.c \ usb_dev.c \
......
/* Teensyduino Core Library
* http://www.pjrc.com/teensy/
* Copyright (c) 2013 PJRC.COM, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* 1. The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* 2. If the Software is incorporated into a build system that allows
* selection among a list of target devices, then similar target
* devices manufactured by PJRC.COM must be included in the list of
* target devices and selectable in the same manner.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
size_t strlen(const char *s)
{
size_t n=0;
while (*s++) n++;
return n;
}
char * ultoa(unsigned long val, char *buf, int radix)
{
unsigned digit;
int i=0, j;
char t;
while (1) {
digit = val % radix;
buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
val /= radix;
if (val == 0) break;
i++;
}
buf[i + 1] = 0;
for (j=0; j < i; j++, i--) {
t = buf[j];
buf[j] = buf[i];
buf[i] = t;
}
return buf;
}
char * ltoa(long val, char *buf, int radix)
{
if (val >= 0) {
return ultoa(val, buf, radix);
} else {
buf[0] = '-';
ultoa(-val, buf + 1, radix);
return buf;
}
}
...@@ -174,32 +174,33 @@ struct usb_string_descriptor_struct usb_string_product_name_default = { ...@@ -174,32 +174,33 @@ struct usb_string_descriptor_struct usb_string_product_name_default = {
3, 3,
PRODUCT_NAME PRODUCT_NAME
}; };
// 32-digit hex string, corresponding to the MK20DX128's built-in unique 128-bit ID.
struct usb_string_descriptor_struct usb_string_serial_number_default = { struct usb_string_descriptor_struct usb_string_serial_number_default = {
12, 2 + 32 * 2,
3, 3,
{0,0,0,0,0,0,0,0,0,0} {0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0}
}; };
static void toHexWide(uint32_t value, uint16_t *buffer)
{
static const uint8_t digits[] = "0123456789ABCDEF";
unsigned i;
for (i = 0; i < 8; i++) {
buffer[i] = digits[value >> 28];
value <<= 4;
}
}
void usb_init_serialnumber(void) void usb_init_serialnumber(void)
{ {
char buf[11]; toHexWide(SIM_UIDH, usb_string_serial_number_default.wString + 8*0);
uint32_t i, num; toHexWide(SIM_UIDMH, usb_string_serial_number_default.wString + 8*1);
toHexWide(SIM_UIDML, usb_string_serial_number_default.wString + 8*2);
__disable_irq(); toHexWide(SIM_UIDL, usb_string_serial_number_default.wString + 8*3);
FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
FTFL_FCCOB0 = 0x41;
FTFL_FCCOB1 = 15;
FTFL_FSTAT = FTFL_FSTAT_CCIF;
while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
num = *(uint32_t *)&FTFL_FCCOB7;
__enable_irq();
ultoa(num, buf, 10);
for (i=0; i<10; i++) {
char c = buf[i];
if (!c) break;
usb_string_serial_number_default.wString[i] = c;
}
usb_string_serial_number_default.bLength = i * 2 + 2;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment