1 /*
2  * Copyright (c) 2021 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT nxp_lpc_uid
8 
9 #include <zephyr/drivers/hwinfo.h>
10 #include <string.h>
11 #include <zephyr/sys/byteorder.h>
12 
13 #define UID_WORD_COUNT (DT_INST_REG_SIZE(0) / sizeof(uint32_t))
14 
15 struct uid {
16 	uint32_t id[UID_WORD_COUNT];
17 };
18 
z_impl_hwinfo_get_device_id(uint8_t * buffer,size_t length)19 ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
20 {
21 	volatile const uint32_t * const uid_addr = (uint32_t *) DT_INST_REG_ADDR(0);
22 	struct uid dev_id;
23 
24 	if (buffer == NULL) {
25 		return 0;
26 	}
27 
28 	for (size_t i = 0 ; i < UID_WORD_COUNT ; i++) {
29 		dev_id.id[i] = sys_cpu_to_be32(uid_addr[i]);
30 	}
31 
32 	if (length > sizeof(dev_id.id)) {
33 		length = sizeof(dev_id.id);
34 	}
35 
36 	memcpy(buffer, dev_id.id, length);
37 
38 	return length;
39 }
40