Skip to content

Commit 9360a3b

Browse files
committed
add uid for some stm32
1 parent 041f510 commit 9360a3b

File tree

9 files changed

+277
-257
lines changed

9 files changed

+277
-257
lines changed

.idea/cmake.xml

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hw/bsp/board_api.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -112,32 +112,33 @@ static inline void board_led_off(void) {
112112
// Get USB Serial number string from unique ID if available. Return number of character.
113113
// Input is string descriptor from index 1 (index 0 is type + len)
114114
static inline size_t board_usb_get_serial(uint16_t desc_str1[], size_t max_chars) {
115-
uint8_t serial_id[16] TU_ATTR_ALIGNED(4);
116-
size_t serial_len;
115+
uint8_t uid[16] TU_ATTR_ALIGNED(4);
116+
size_t uid_len;
117117

118118
if ( board_get_unique_id ) {
119-
serial_len = board_get_unique_id(serial_id, sizeof(serial_id));
119+
uid_len = board_get_unique_id(uid, sizeof(uid));
120120
}else {
121121
// fixed serial string is 01234567889ABCDEF
122-
*((uint32_t*)(uintptr_t) serial_id) = 0x67452301;
123-
*((uint32_t*)(uintptr_t) (serial_id+4)) = 0xEFCDAB89;
124-
serial_len = 8;
122+
uint32_t* uid32 = (uint32_t*) (uintptr_t) uid;
123+
uid32[0] = 0x67452301;
124+
uid32[1] = 0xEFCDAB89;
125+
uid_len = 8;
125126
}
126127

127-
if ( serial_len > max_chars / 2 ) serial_len = max_chars / 2;
128+
if ( uid_len > max_chars / 2 ) uid_len = max_chars / 2;
128129

129-
for ( size_t i = 0; i < serial_len; i++ ) {
130+
for ( size_t i = 0; i < uid_len; i++ ) {
130131
for ( size_t j = 0; j < 2; j++ ) {
131132
const char nibble_to_hex[16] = {
132133
'0', '1', '2', '3', '4', '5', '6', '7',
133134
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
134135
};
135-
uint8_t const nibble = (serial_id[i] >> (j * 4)) & 0xf;
136+
uint8_t const nibble = (uid[i] >> (j * 4)) & 0xf;
136137
desc_str1[i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE
137138
}
138139
}
139140

140-
return 2*serial_len;
141+
return 2 * uid_len;
141142
}
142143

143144
// TODO remove

hw/bsp/stm32f0/family.c

+43-44
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
//--------------------------------------------------------------------+
3232
// Forward USB interrupt events to TinyUSB IRQ Handler
3333
//--------------------------------------------------------------------+
34-
void USB_IRQHandler(void)
35-
{
34+
void USB_IRQHandler(void) {
3635
tud_int_handler(0);
3736
}
3837

@@ -41,8 +40,7 @@ void USB_IRQHandler(void)
4140
//--------------------------------------------------------------------+
4241
UART_HandleTypeDef UartHandle;
4342

44-
void board_init(void)
45-
{
43+
void board_init(void) {
4644
board_stm32f0_clock_init();
4745

4846
// Enable All GPIOs clocks
@@ -68,7 +66,7 @@ void board_init(void)
6866
#endif
6967

7068
// LED
71-
GPIO_InitTypeDef GPIO_InitStruct;
69+
GPIO_InitTypeDef GPIO_InitStruct;
7270
GPIO_InitStruct.Pin = LED_PIN;
7371
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
7472
GPIO_InitStruct.Pull = GPIO_PULLUP;
@@ -83,20 +81,20 @@ void board_init(void)
8381
HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);
8482

8583
// Uart
86-
GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN;
87-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
88-
GPIO_InitStruct.Pull = GPIO_PULLUP;
89-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
84+
GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN;
85+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
86+
GPIO_InitStruct.Pull = GPIO_PULLUP;
87+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
9088
GPIO_InitStruct.Alternate = UART_GPIO_AF;
9189
HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct);
9290

93-
UartHandle.Instance = UART_DEV;
94-
UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE;
91+
UartHandle.Instance = UART_DEV;
92+
UartHandle.Init.BaudRate = CFG_BOARD_UART_BAUDRATE;
9593
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
96-
UartHandle.Init.StopBits = UART_STOPBITS_1;
97-
UartHandle.Init.Parity = UART_PARITY_NONE;
98-
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
99-
UartHandle.Init.Mode = UART_MODE_TX_RX;
94+
UartHandle.Init.StopBits = UART_STOPBITS_1;
95+
UartHandle.Init.Parity = UART_PARITY_NONE;
96+
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
97+
UartHandle.Init.Mode = UART_MODE_TX_RX;
10098
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
10199
HAL_UART_Init(&UartHandle);
102100

@@ -116,56 +114,59 @@ void board_init(void)
116114
// Board porting API
117115
//--------------------------------------------------------------------+
118116

119-
void board_led_write(bool state)
120-
{
121-
GPIO_PinState pin_state = (GPIO_PinState) (state ? LED_STATE_ON : (1-LED_STATE_ON));
117+
void board_led_write(bool state) {
118+
GPIO_PinState pin_state = (GPIO_PinState)(state ? LED_STATE_ON : (1 - LED_STATE_ON));
122119
HAL_GPIO_WritePin(LED_PORT, LED_PIN, pin_state);
123120
}
124121

125-
uint32_t board_button_read(void)
126-
{
122+
uint32_t board_button_read(void) {
127123
return BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN);
128124
}
129125

130-
int board_uart_read(uint8_t* buf, int len)
131-
{
132-
(void) buf; (void) len;
126+
size_t board_get_unique_id(uint8_t id[], size_t max_len) {
127+
(void) max_len;
128+
volatile uint32_t * stm32_uuid = (volatile uint32_t *) UID_BASE;
129+
uint32_t* id32 = (uint32_t*) (uintptr_t) id;
130+
uint8_t const len = 12;
131+
132+
id32[0] = stm32_uuid[0];
133+
id32[1] = stm32_uuid[1];
134+
id32[2] = stm32_uuid[2];
135+
136+
return len;
137+
}
138+
139+
int board_uart_read(uint8_t *buf, int len) {
140+
(void) buf;
141+
(void) len;
133142
return 0;
134143
}
135144

136-
int board_uart_write(void const * buf, int len)
137-
{
138-
HAL_UART_Transmit(&UartHandle, (uint8_t*)(uintptr_t) buf, len, 0xffff);
145+
int board_uart_write(void const *buf, int len) {
146+
HAL_UART_Transmit(&UartHandle, (uint8_t * )(uintptr_t)
147+
buf, len, 0xffff);
139148
return len;
140149
}
141150

142-
#if CFG_TUSB_OS == OPT_OS_NONE
151+
#if CFG_TUSB_OS == OPT_OS_NONE
143152
volatile uint32_t system_ticks = 0;
144-
void SysTick_Handler (void)
145-
{
153+
154+
void SysTick_Handler(void) {
146155
HAL_IncTick();
147156
system_ticks++;
148157
}
149158

150-
uint32_t board_millis(void)
151-
{
159+
uint32_t board_millis(void) {
152160
return system_ticks;
153161
}
162+
154163
#endif
155164

156-
void HardFault_Handler (void)
157-
{
158-
__asm("BKPT #0\n");
165+
void HardFault_Handler(void) {
166+
__asm("BKPT #0\n");
159167
}
160168

161169
#ifdef USE_FULL_ASSERT
162-
/**
163-
* @brief Reports the name of the source file and the source line number
164-
* where the assert_param error has occurred.
165-
* @param file: pointer to the source file name
166-
* @param line: assert_param error line source number
167-
* @retval None
168-
*/
169170
void assert_failed(const char* file, uint32_t line)
170171
{
171172
(void) file; (void) line;
@@ -178,7 +179,5 @@ void assert_failed(const char* file, uint32_t line)
178179

179180
// Required by __libc_init_array in startup code if we are compiling using
180181
// -nostdlib/-nostartfiles.
181-
void _init(void)
182-
{
183-
182+
void _init(void) {
184183
}

hw/bsp/stm32f1/family.c

+35-38
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,23 @@
3131
//--------------------------------------------------------------------+
3232
// Forward USB interrupt events to TinyUSB IRQ Handler
3333
//--------------------------------------------------------------------+
34-
void USB_HP_IRQHandler(void)
35-
{
34+
void USB_HP_IRQHandler(void) {
3635
tud_int_handler(0);
3736
}
3837

39-
void USB_LP_IRQHandler(void)
40-
{
38+
void USB_LP_IRQHandler(void) {
4139
tud_int_handler(0);
4240
}
4341

44-
void USBWakeUp_IRQHandler(void)
45-
{
42+
void USBWakeUp_IRQHandler(void) {
4643
tud_int_handler(0);
4744
}
4845

4946
//--------------------------------------------------------------------+
5047
// MACRO TYPEDEF CONSTANT ENUM
5148
//--------------------------------------------------------------------+
5249

53-
void board_init(void)
54-
{
50+
void board_init(void) {
5551
board_stm32f1_clock_init();
5652

5753
// Enable All GPIOs clocks
@@ -60,7 +56,7 @@ void board_init(void)
6056
__HAL_RCC_GPIOC_CLK_ENABLE();
6157
__HAL_RCC_GPIOD_CLK_ENABLE();
6258

63-
#if CFG_TUSB_OS == OPT_OS_NONE
59+
#if CFG_TUSB_OS == OPT_OS_NONE
6460
// 1ms tick timer
6561
SysTick_Config(SystemCoreClock / 1000);
6662

@@ -72,7 +68,7 @@ void board_init(void)
7268
#endif
7369

7470
// LED
75-
GPIO_InitTypeDef GPIO_InitStruct;
71+
GPIO_InitTypeDef GPIO_InitStruct;
7672
GPIO_InitStruct.Pin = LED_PIN;
7773
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
7874
GPIO_InitStruct.Pull = LED_STATE_ON ? GPIO_PULLDOWN : GPIO_PULLUP;
@@ -102,56 +98,59 @@ void board_init(void)
10298
// Board porting API
10399
//--------------------------------------------------------------------+
104100

105-
void board_led_write(bool state)
106-
{
107-
GPIO_PinState pin_state = (GPIO_PinState) (state ? LED_STATE_ON : (1-LED_STATE_ON));
101+
void board_led_write(bool state) {
102+
GPIO_PinState pin_state = (GPIO_PinState)(state ? LED_STATE_ON : (1 - LED_STATE_ON));
108103
HAL_GPIO_WritePin(LED_PORT, LED_PIN, pin_state);
109104
}
110105

111-
uint32_t board_button_read(void)
112-
{
106+
uint32_t board_button_read(void) {
113107
return BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN);
114108
}
115109

116-
int board_uart_read(uint8_t* buf, int len)
117-
{
118-
(void) buf; (void) len;
110+
size_t board_get_unique_id(uint8_t id[], size_t max_len) {
111+
(void) max_len;
112+
volatile uint32_t * stm32_uuid = (volatile uint32_t *) UID_BASE;
113+
uint32_t* id32 = (uint32_t*) (uintptr_t) id;
114+
uint8_t const len = 12;
115+
116+
id32[0] = stm32_uuid[0];
117+
id32[1] = stm32_uuid[1];
118+
id32[2] = stm32_uuid[2];
119+
120+
return len;
121+
}
122+
123+
int board_uart_read(uint8_t *buf, int len) {
124+
(void) buf;
125+
(void) len;
119126
return 0;
120127
}
121128

122-
int board_uart_write(void const * buf, int len)
123-
{
124-
(void) buf; (void) len;
129+
int board_uart_write(void const *buf, int len) {
130+
(void) buf;
131+
(void) len;
125132
return 0;
126133
}
127134

128-
#if CFG_TUSB_OS == OPT_OS_NONE
135+
#if CFG_TUSB_OS == OPT_OS_NONE
129136
volatile uint32_t system_ticks = 0;
130-
void SysTick_Handler (void)
131-
{
137+
138+
void SysTick_Handler(void) {
132139
HAL_IncTick();
133140
system_ticks++;
134141
}
135142

136-
uint32_t board_millis(void)
137-
{
143+
uint32_t board_millis(void) {
138144
return system_ticks;
139145
}
146+
140147
#endif
141148

142-
void HardFault_Handler (void)
143-
{
149+
void HardFault_Handler(void) {
144150
__asm("BKPT #0\n");
145151
}
146152

147153
#ifdef USE_FULL_ASSERT
148-
/**
149-
* @brief Reports the name of the source file and the source line number
150-
* where the assert_param error has occurred.
151-
* @param file: pointer to the source file name
152-
* @param line: assert_param error line source number
153-
* @retval None
154-
*/
155154
void assert_failed(const char *file, uint32_t line)
156155
{
157156
/* USER CODE BEGIN 6 */
@@ -163,7 +162,5 @@ void assert_failed(const char *file, uint32_t line)
163162

164163
// Required by __libc_init_array in startup code if we are compiling using
165164
// -nostdlib/-nostartfiles.
166-
void _init(void)
167-
{
168-
165+
void _init(void) {
169166
}

0 commit comments

Comments
 (0)