Skip to content

Commit 1ec463e

Browse files
petejohanson-adikartben
authored andcommitted
drivers: hwinfo: Memoize the USN fetched on MAX32
To avoid issues with multiple calls to fetch the USN returning garbage data, memoize the returned USN value and re-use it on subsequent calls. Signed-off-by: Pete Johanson <[email protected]>
1 parent 91ab372 commit 1ec463e

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

drivers/hwinfo/hwinfo_max32.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,33 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include <zephyr/kernel.h>
78
#include <zephyr/drivers/hwinfo.h>
89
#include <soc.h>
910
#include <wrap_max32_sys.h>
1011
#include <string.h>
1112
#include <zephyr/sys/byteorder.h>
1213

14+
static struct k_spinlock device_id_lock;
15+
static bool initialized;
16+
static uint8_t usn[MXC_SYS_USN_LEN];
17+
1318
ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
1419
{
15-
uint8_t usn[MXC_SYS_USN_LEN];
16-
int error;
20+
int ret;
21+
k_spinlock_key_t key;
22+
23+
key = k_spin_lock(&device_id_lock);
24+
25+
if (!initialized) {
26+
ret = Wrap_MXC_SYS_GetUSN(usn);
27+
28+
if (ret != E_NO_ERROR) {
29+
/* Error reading USN */
30+
goto exit;
31+
}
1732

18-
error = Wrap_MXC_SYS_GetUSN(usn);
19-
if (error != E_NO_ERROR) {
20-
/* Error reading USN */
21-
return error;
33+
initialized = true;
2234
}
2335

2436
if (length > sizeof(usn)) {
@@ -27,6 +39,9 @@ ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
2739

2840
/* Provide device ID in big endian */
2941
sys_memcpy_swap(buffer, usn, length);
42+
ret = length;
3043

31-
return length;
44+
exit:
45+
k_spin_unlock(&device_id_lock, key);
46+
return ret;
3247
}

0 commit comments

Comments
 (0)