Skip to content

Commit 07afcf2

Browse files
authored
Merge pull request #5557 from MikeDK/hal_cpu_uid
HAL API for CPU UID
2 parents 7be79f9 + cbfae38 commit 07afcf2

File tree

9 files changed

+548
-23
lines changed

9 files changed

+548
-23
lines changed

drivers/DeviceUid.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "hal/device_uid_api.h"
18+
#include "platform/mbed_assert.h"
19+
#include "platform/CriticalSectionLock.h"
20+
#include "drivers/DeviceUid.h"
21+
22+
#if DEVICE_DEVICEUID
23+
24+
namespace mbed {
25+
26+
uint8_t DeviceUid::_uidbuf[MBED_DEVICEUID_SIZE] = {0};
27+
uint8_t* DeviceUid::_uidptr = NULL;
28+
char DeviceUid::_strbuf[DEVICEUID_STRING_BUFFER_SIZE] = {'\0'};
29+
char* DeviceUid::_strptr = NULL;
30+
#ifndef MBED_DEVICEUID_STR_SIZE_MAX
31+
const char DeviceUid::_hexChars[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
32+
#endif
33+
34+
DeviceUid::DeviceUid()
35+
{
36+
populate_uid_buf();
37+
}
38+
39+
void DeviceUid::populate_uid_buf()
40+
{
41+
if (_uidptr == NULL) {
42+
CriticalSectionLock lock;
43+
device_uid_get_uid(_uidbuf);
44+
_uidptr = _uidbuf;
45+
}
46+
}
47+
48+
void DeviceUid::populate_str_buf()
49+
{
50+
if (_strptr == NULL) {
51+
CriticalSectionLock lock;
52+
#ifdef MBED_DEVICEUID_STR_SIZE_MAX
53+
device_uid_get_str(_strbuf);
54+
_strptr = _strbuf;
55+
#else
56+
int pos = 0;
57+
populate_uid_buf();
58+
for (int i = 0; i < MBED_DEVICEUID_SIZE; ++i) {
59+
_strbuf[pos++] = _hexChars[_uidptr[i] >> 4];
60+
_strbuf[pos++] = _hexChars[_uidptr[i] & 0x0F];
61+
}
62+
_strbuf[pos] = '\0';
63+
_strptr = _strbuf;
64+
#endif
65+
}
66+
}
67+
68+
} // namespace mbed
69+
70+
#endif

drivers/DeviceUid.h

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef MBED_DEVICEUID_H
17+
#define MBED_DEVICEUID_H
18+
19+
#include "platform/platform.h"
20+
21+
#if defined(DEVICE_DEVICEUID) || defined(DOXYGEN_ONLY)
22+
23+
#ifdef MBED_DEVICEUID_STR_SIZE_MAX
24+
#define DEVICEUID_STRING_BUFFER_SIZE MBED_DEVICEUID_STR_SIZE_MAX
25+
#else
26+
#define DEVICEUID_STRING_BUFFER_SIZE (MBED_DEVICEUID_SIZE * 2 + 1)
27+
#endif
28+
29+
namespace mbed {
30+
/** \addtogroup drivers */
31+
32+
/** DEVICE UID reader class
33+
*
34+
* @note Synchronization level: Interrupt safe
35+
* @ingroup drivers
36+
*/
37+
class DeviceUid {
38+
public:
39+
/** DeviceUid constructor
40+
*/
41+
DeviceUid();
42+
43+
/** Get size of DEVICE UID in bytes
44+
*
45+
* @return Size of device's DEVICE UID in bytes
46+
*/
47+
int size()
48+
{
49+
return MBED_DEVICEUID_SIZE;
50+
}
51+
52+
/** Get DEVICE UID data pointer
53+
*
54+
* @return Pointer to uid data buffer
55+
*/
56+
const uint8_t *data()
57+
{
58+
populate_uid_buf();
59+
return _uidptr;
60+
}
61+
62+
/** Get DEVICE UID vendor string
63+
*
64+
* @return Pointer to zero terminated uid vendor string
65+
*
66+
* @note
67+
* If vendor did not define MBED_DEVICEUID_STR_SIZE_MAX, DeviceUid driver will
68+
* assume the HAL interface function device_uid_get_str() is not implemented
69+
* on the target, and instead the driver will construct just an ASCII
70+
* string out of the uid byte buffer contents.
71+
*
72+
*/
73+
const char *c_str()
74+
{
75+
populate_str_buf();
76+
return _strptr;
77+
}
78+
79+
/** Overload operator for byte pointer
80+
*
81+
* @return Pointer to uid data buffer
82+
*/
83+
operator const uint8_t*()
84+
{
85+
populate_uid_buf();
86+
return _uidptr;
87+
}
88+
89+
operator const char*()
90+
{
91+
populate_str_buf();
92+
return _strptr;
93+
}
94+
95+
/** Overload operator for array subscript
96+
*
97+
* @param x DEVICE UID Byte index
98+
*
99+
* @return Byte located at index x
100+
*/
101+
uint8_t operator[](int x)
102+
{
103+
if (x >= 0 && x < MBED_DEVICEUID_SIZE) {
104+
populate_uid_buf();
105+
return _uidptr[x];
106+
}
107+
108+
return 0x00;
109+
}
110+
111+
private:
112+
void populate_uid_buf();
113+
void populate_str_buf();
114+
115+
static uint8_t _uidbuf[MBED_DEVICEUID_SIZE];
116+
static uint8_t* _uidptr;
117+
static char _strbuf[DEVICEUID_STRING_BUFFER_SIZE];
118+
static char* _strptr;
119+
#ifndef MBED_DEVICEUID_STR_SIZE_MAX
120+
static const char _hexChars[16];
121+
#endif
122+
};
123+
124+
} // namespace mbed
125+
126+
#endif
127+
128+
#endif

hal/device_uid_api.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/** \addtogroup hal */
2+
/** @{*/
3+
/* mbed Microcontroller Library
4+
* Copyright (c) 2017 ARM Limited
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
#ifndef MBED_DEVICE_UID_API_H
19+
#define MBED_DEVICE_UID_API_H
20+
21+
#include <stddef.h>
22+
#include "device.h"
23+
24+
#if DEVICE_DEVICEUID
25+
26+
#if !defined(MBED_DEVICEUID_SIZE) || (MBED_DEVICEUID_SIZE == 0)
27+
#error "DEVICE UID Vendor implementation must define macro MBED_DEVICEUID_SIZE with the non-zero uid size in bytes!"
28+
#endif
29+
30+
#if !defined(MBED_DEVICEUID_STR_SIZE_MAX) || (MBED_DEVICEUID_STR_SIZE_MAX == 0)
31+
#warning "DEVICE UID max vendor string length not defined or zero! device_uid_get_str() HAL interface is disabled!"
32+
#endif
33+
34+
#ifdef __cplusplus
35+
extern "C" {
36+
#endif
37+
38+
/**
39+
* \defgroup hal_device_uid DEVICEUID hal functions
40+
* @{
41+
*/
42+
43+
/** Get DEVICE UID data bytes
44+
*
45+
* @param uid Byte buffer for uid. Must be of size MBED_DEVICEUID_SIZE
46+
*
47+
*/
48+
void device_uid_get_uid(uint8_t *uid);
49+
50+
#ifdef MBED_DEVICEUID_STR_SIZE_MAX
51+
/** Get DEVICE UID vendor string
52+
*
53+
* @param str Character buffer for vendor specific UID string. Must be of size MBED_DEVICEUID_STR_SIZE_MAX
54+
*
55+
* @note
56+
* Implementing this function on target side can be used to provide a vendor specific
57+
* string describing the contents of the UID.
58+
* The string length including terminating zero character must not exceed MBED_DEVICEUID_STR_SIZE_MAX bytes!
59+
*
60+
*/
61+
void device_uid_get_str(char *str);
62+
#endif
63+
64+
/**@}*/
65+
66+
#ifdef __cplusplus
67+
}
68+
#endif
69+
70+
#endif
71+
72+
#endif
73+
/** @}*/

mbed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#include "drivers/RawSerial.h"
7171
#include "drivers/UARTSerial.h"
7272
#include "drivers/FlashIAP.h"
73+
#include "drivers/DeviceUid.h"
7374

7475
// mbed Internal components
7576
#include "drivers/Timer.h"

platform/CriticalSectionLock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define MBED_CRITICALSECTIONLOCK_H
2020

2121
#include "platform/mbed_critical.h"
22+
#include "platform/mbed_toolchain.h"
2223

2324
namespace mbed {
2425

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2017 Nordic Semiconductor ASA
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this list
9+
* of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
12+
* integrated circuit in a product or a software update for such product, must reproduce
13+
* the above copyright notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the distribution.
15+
*
16+
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
17+
* used to endorse or promote products derived from this software without specific prior
18+
* written permission.
19+
*
20+
* 4. This software, with or without modification, must only be used with a
21+
* Nordic Semiconductor ASA integrated circuit.
22+
*
23+
* 5. Any software provided in binary or object form under this license must not be reverse
24+
* engineered, decompiled, modified and/or disassembled.
25+
*
26+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
30+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36+
*
37+
*/
38+
39+
#include "device.h"
40+
41+
#if DEVICE_DEVICEUID
42+
43+
#include "nrf.h"
44+
#include "device_uid_api.h"
45+
46+
#define UID_WORDS 2
47+
48+
void device_uid_get_uid(uint8_t *uid)
49+
{
50+
int pos = 0;
51+
52+
for (int i = (UID_WORDS-1); i >= 0; --i) {
53+
for (int j = 3; j >= 0; --j) {
54+
uid[pos++] = (uint8_t)((NRF_FICR->DEVICEID[i] >> (j*8)) & 0xFF);
55+
}
56+
}
57+
}
58+
59+
#endif

0 commit comments

Comments
 (0)