Skip to content

Commit 8a12f71

Browse files
committed
Added CPU UID target implementation for STM32 devices
1 parent 31d6a28 commit 8a12f71

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

targets/TARGET_STM/cpu_uid_api.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/* mbed Microcontroller Library
2+
*******************************************************************************
3+
* Copyright (c) 2017, STMicroelectronics
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*******************************************************************************
29+
*/
30+
#include "device.h"
31+
32+
#if DEVICE_CPUUID
33+
34+
#if defined(TARGET_STM32F0)
35+
#include "stm32f0xx_ll_utils.h"
36+
#elif defined(TARGET_STM32F1)
37+
#include "stm32f1xx_ll_utils.h"
38+
#elif defined(TARGET_STM32F2)
39+
#include "stm32f2xx_ll_utils.h"
40+
#elif defined(TARGET_STM32F3)
41+
#include "stm32f3xx_ll_utils.h"
42+
#elif defined(TARGET_STM32F4)
43+
#include "stm32f4xx_ll_utils.h"
44+
#elif defined(TARGET_STM32F7)
45+
#include "stm32f7xx_ll_utils.h"
46+
#elif defined(TARGET_STM32L0)
47+
#include "stm32l0xx_ll_utils.h"
48+
#elif defined(TARGET_STM32L1)
49+
#include "stm32l1xx_ll_utils.h"
50+
#elif defined(TARGET_STM32L4)
51+
#include "stm32l4xx_ll_utils.h"
52+
#else
53+
#error "Unsupported STM32 device!"
54+
#endif
55+
#include "cpu_uid_api.h"
56+
57+
#define UID_LENGTH 12
58+
#define UID_WORDS 3
59+
60+
61+
int cpu_uid_get_length(void)
62+
{
63+
return UID_LENGTH;
64+
}
65+
66+
void cpu_uid_get_uid(uint8_t *uid)
67+
{
68+
uint32_t uid_buf[UID_WORDS];
69+
int pos = 0;
70+
71+
uid_buf[0] = LL_GetUID_Word0();
72+
uid_buf[1] = LL_GetUID_Word1();
73+
uid_buf[2] = LL_GetUID_Word2();
74+
75+
for (int i = (UID_WORDS-1); i >= 0; --i)
76+
{
77+
for (int j = 3; j >= 0; --j)
78+
{
79+
uid[pos] = (uint8_t)((uid_buf[i] >> (j*8)) & 0xFF);
80+
++pos;
81+
}
82+
}
83+
}
84+
85+
#endif

0 commit comments

Comments
 (0)