Skip to content

Commit 44bc171

Browse files
sommersofttannewt
authored andcommitted
Added Unique ID Functions/Updated Support Matrix (adafruit#527)
shared_bindings/index.rst: updated Support Matrix format as discussed in PR adafruit#503 & Issue adafruit#448. shared-bindings/microcontroller/Processor.c & .h: added UID lookup functionality for use with all ports. Fixes adafruit#462.
1 parent dfa6b6e commit 44bc171

9 files changed

Lines changed: 95 additions & 28 deletions

File tree

ports/atmel-samd/common-hal/microcontroller/Processor.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,20 @@ uint32_t common_hal_mcu_processor_get_frequency(void) {
230230
// TODO(tannewt): Determine this dynamically.
231231
return CONF_CPU_FREQUENCY;
232232
}
233+
234+
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
235+
#ifdef SAMD21
236+
uint32_t* id_addresses[4] = {(uint32_t *) 0x0080A00C, (uint32_t *) 0x0080A040,
237+
(uint32_t *) 0x0080A044, (uint32_t *) 0x0080A048};
238+
#endif
239+
#ifdef SAMD51
240+
uint32_t* id_addresses[4] = {(uint32_t *) 0x008061FC, (uint32_t *) 0x00806010,
241+
(uint32_t *) 0x00806014, (uint32_t *) 0x00806018};
242+
#endif
243+
244+
for (int i=0; i<4; i++) {
245+
for (int k=0; k<4; k++) {
246+
raw_id[4 * i + k] = (*(id_addresses[i]) >> k * 8) & 0xf;
247+
}
248+
}
249+
}

ports/atmel-samd/common-hal/microcontroller/Processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
2828
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
2929

30+
#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH 16
31+
3032
#include "py/obj.h"
3133

3234
typedef struct {

ports/esp8266/common-hal/microcontroller/Processor.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@ float common_hal_mcu_processor_get_temperature(void) {
3838
uint32_t common_hal_mcu_processor_get_frequency(void) {
3939
return mp_hal_get_cpu_freq();
4040
}
41+
42+
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
43+
uint32_t id = system_get_chip_id();
44+
for (int i=0; i<4; i++){
45+
raw_id[i] = id >> (i * 8);
46+
}
47+
}

ports/esp8266/common-hal/microcontroller/Processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#ifndef MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
2828
#define MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
2929

30+
#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH 4
31+
3032
#include "py/obj.h"
3133

3234
typedef struct {

ports/nrf/common-hal/microcontroller/Processor.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ uint32_t common_hal_mcu_processor_get_frequency(void) {
3535
return 64000000ul;
3636
}
3737

38+
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
39+
40+
uint32_t* id_addresses[2] = {(uint32_t *) 0x060, (uint32_t *) 0x064};
41+
42+
for (int i=0; i<2; i++) {
43+
for (int k=0; k<4; k++) {
44+
raw_id[4 * i + k] = (*(id_addresses[i]) >> k * 8) & 0xf;
45+
}
46+
}
47+
}

ports/nrf/common-hal/microcontroller/Processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
2828
#define MICROPY_INCLUDED_NRF_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
2929

30+
#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH 8
31+
3032
#include "py/obj.h"
3133

3234
typedef struct {

shared-bindings/index.rst

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,39 @@ limited. For example, a microcontroller without analog features will not have
1111

1212
Support Matrix
1313
---------------
14+
NOTE 1: **All Supported** means the following ports are supported: SAMD21, SAMD21 Express,
15+
SAMD51, SAMD51 Express, and ESP8266.
1416

15-
================= ======= ============== =======
16-
Module / Port SAMD21 SAMD21 Express ESP8266
17-
================= ======= ============== =======
18-
`analogio` **Yes** **Yes** **Yes**
19-
`audiobusio` **Yes** **Yes** No
20-
`audioio` No **Yes** No
21-
`bitbangio` No **Yes** **Yes**
22-
`board` **Yes** **Yes** **Yes**
23-
`busio` **Yes** **Yes** **Yes**
24-
`digitalio` **Yes** **Yes** **Yes**
25-
`gamepad` No **Yes** No
26-
`math` **Yes** **Yes** **Yes**
27-
`microcontroller` **Yes** **Yes** **Yes**
28-
`multiterminal` No No **Yes**
29-
`neopixel_write` **Yes** **Yes** **Yes**
30-
`nvm` No **Yes** No
31-
`os` **Yes** **Yes** **Yes**
32-
`pulseio` **Yes** **Yes** No
33-
`random` **Yes** **Yes** **Yes**
34-
`storage` **Yes** **Yes** **Yes**
35-
`struct` **Yes** **Yes** **Yes**
36-
`supervisor` **Yes** **Yes** No
37-
`time` **Yes** **Yes** **Yes**
38-
`touchio` **Yes** **Yes** No
39-
`uheap` Debug Debug Debug
40-
`usb_hid` **Yes** **Yes** No
41-
================= ======= ============== =======
17+
NOTE 2: **SAMD** and/or **SAMD Express** without additional numbers, means both SAMD21 & SAMD51 versions
18+
are supported.
19+
20+
================= ===============
21+
Module Supported Ports
22+
================= ===============
23+
`analogio` **All Supported**
24+
`audiobusio` **SAMD/SAMD Express**
25+
`audioio` **SAMD Express**
26+
`bitbangio` **SAMD Express, ESP8266**
27+
`board` **All Supported**
28+
`busio` **All Supported**
29+
`digitalio` **All Supported**
30+
`gamepad` **SAMD Express**
31+
`math` **All Supported**
32+
`microcontroller` **All Supported**
33+
`multiterminal` **ESP8266**
34+
`neopixel_write` **All Supported**
35+
`nvm` **SAMD Express**
36+
`os` **All Supported**
37+
`pulseio` **SAMD/SAMD Express**
38+
`random` **All Supported**
39+
`storage` **All Supported**
40+
`struct` **All Supported**
41+
`supervisor` **SAMD/SAMD Express**
42+
`time` **All Supported**
43+
`touchio` **SAMD/SAMD Express**
44+
`uheap` **Debug (All)**
45+
`usb_hid` **SAMD/SAMD Express**
46+
================= ===============
4247

4348
Modules
4449
---------

shared-bindings/microcontroller/Processor.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
#include "py/objproperty.h"
3434

35-
#include "py/objproperty.h"
3635
#include "py/runtime.h"
3736

3837
//| .. currentmodule:: microcontroller
@@ -93,9 +92,31 @@ const mp_obj_property_t mcu_processor_temperature_obj = {
9392
},
9493
};
9594

95+
//| .. attribute:: uid
96+
//|
97+
//| Return the unique id (aka serial number) of the chip.
98+
//| Returns a bytearray object.
99+
//|
100+
STATIC mp_obj_t mcu_processor_get_uid(mp_obj_t self) {
101+
uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH];
102+
common_hal_mcu_processor_get_uid(raw_id);
103+
return mp_obj_new_bytearray(sizeof(raw_id), raw_id);
104+
}
105+
106+
MP_DEFINE_CONST_FUN_OBJ_1(mcu_processor_get_uid_obj, mcu_processor_get_uid);
107+
108+
const mp_obj_property_t mcu_processor_uid_obj = {
109+
.base.type = &mp_type_property,
110+
.proxy = {(mp_obj_t)&mcu_processor_get_uid_obj, // getter
111+
(mp_obj_t)&mp_const_none_obj, // no setter
112+
(mp_obj_t)&mp_const_none_obj, // no deleter
113+
},
114+
};
115+
96116
STATIC const mp_rom_map_elem_t mcu_processor_locals_dict_table[] = {
97117
{ MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&mcu_processor_frequency_obj) },
98118
{ MP_ROM_QSTR(MP_QSTR_temperature), MP_ROM_PTR(&mcu_processor_temperature_obj) },
119+
{ MP_ROM_QSTR(MP_QSTR_uid), MP_ROM_PTR(&mcu_processor_uid_obj) },
99120
};
100121

101122
STATIC MP_DEFINE_CONST_DICT(mcu_processor_locals_dict, mcu_processor_locals_dict_table);

shared-bindings/microcontroller/Processor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ const mp_obj_type_t mcu_processor_type;
3535

3636
uint32_t common_hal_mcu_processor_get_frequency(void);
3737
float common_hal_mcu_processor_get_temperature(void);
38+
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]);
3839

3940
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PROCESSOR_H

0 commit comments

Comments
 (0)