libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
desig_common_all.c
Go to the documentation of this file.
1/*
2 * This file is part of the libopencm3 project.
3 *
4 * Copyright (C) 2012 Karl Palsson <karlp@ŧweak.net.au>
5 *
6 * This library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
22void desig_get_unique_id_as_string(char *string, unsigned int string_len)
23{
24 int i, len;
25 uint32_t uid_buf[3];
26 uint8_t *uid = (uint8_t *)uid_buf;
27 const char chars[] = "0123456789ABCDEF";
28
29 desig_get_unique_id(uid_buf);
30
31 /* Each byte produces two characters */
32 len = (2 * sizeof(uid_buf) < string_len) ?
33 2 * sizeof(uid_buf) : string_len - 1;
34
35 for (i = 0; i < len; i += 2) {
36 string[i] = chars[(uid[i / 2] >> 4) & 0x0F];
37 string[i + 1] = chars[(uid[i / 2] >> 0) & 0x0F];
38 }
39
40 string[len] = '\0';
41}
42
43void desig_get_unique_id_as_dfu(char *string) {
44 uint32_t uid_buf[3];
45 uint8_t *uid = (uint8_t *)uid_buf;
46
47 desig_get_unique_id(uid_buf);
48
49 uint8_t serial[6];
50 serial[0] = uid[11];
51 serial[1] = uid[10] + uid[2];
52 serial[2] = uid[9];
53 serial[3] = uid[8] + uid[0];
54 serial[4] = uid[7];
55 serial[5] = uid[6];
56
57 uint8_t *ser = &serial[0];
58 uint8_t *end = &serial[6];
59 const char hex_digit[] = "0123456789ABCDEF";
60
61 for (; ser < end; ser++) {
62 *string++ = hex_digit[(*ser >> 4) & 0x0f];
63 *string++ = hex_digit[(*ser >> 0) & 0x0f];
64 }
65 *string = '\0';
66}
void desig_get_unique_id(uint32_t *result)
Read the full 96 bit unique identifier Note: ST specifies that bits 31..16 are also reserved for futu...
Definition: desig.c:45
void desig_get_unique_id_as_dfu(char *string)
Generate the same serial number from the unique id registers as the DFU bootloader.
void desig_get_unique_id_as_string(char *string, unsigned int string_len)
Read the full 96 bit unique identifier and return it as a zero-terminated string.