-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_num.c
61 lines (42 loc) · 1.59 KB
/
serial_num.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "app_usbd.h"
#if NRF_SDK_VER_MAJOR >= 15
#define SERIAL_NUMBER_STRING_SIZE (12)
/**@brief Serial number generated.
*
* Serial number generated by the @ref serial_number_string_create function.
*/
uint8_t g_extern_serial_number[SERIAL_NUMBER_STRING_SIZE + 1];
void serial_num_generate(void)
{
const uint16_t serial_num_high_bytes = (uint16_t)NRF_FICR->DEVICEADDR[1] | 0xC000; // The masking makes the address match the Random Static BLE address.
const uint32_t serial_num_low_bytes = NRF_FICR->DEVICEADDR[0];
char* p_buf = (char*)g_extern_serial_number;
int buf_len = SERIAL_NUMBER_STRING_SIZE + 1;
sprintf(p_buf,
"%04x%08x",
serial_num_high_bytes,
serial_num_low_bytes);
}
#else
#define SERIAL_NUMBER_STRING_SIZE (16)
/**@brief Serial number generated.
*
* Serial number generated by the @ref serial_number_string_create function.
*/
uint16_t g_extern_serial_number[SERIAL_NUMBER_STRING_SIZE + 1];
void serial_num_generate(void)
{
static char ser_number[1 + 2 * 8] = { 0 };
g_extern_serial_number[0] = (uint16_t)APP_USBD_DESCRIPTOR_STRING << 8 |
sizeof(g_extern_serial_number);
uint32_t dev_id_hi = NRF_FICR->DEVICEID[1];
uint32_t dev_id_lo = NRF_FICR->DEVICEID[0];
sprintf(ser_number + 1, "%08x%08x", dev_id_hi, dev_id_lo);
for(int i = 0; i < 1 + 2 * 8; i++) {
g_extern_serial_number[i] = ser_number[i];
}
}
#endif