@@ -92,45 +92,9 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
9292STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_freq_obj , 0 , 1 , machine_freq );
9393
9494STATIC mp_obj_t machine_unique_id (void ) {
95- // Each device has a unique 128-bit serial number which is a concatenation of four 32-bit
96- // words contained at the following addresses. The uniqueness of the serial number is
97- // guaranteed only when using all 128 bits.
98- // Atmel SAM D21E / SAM D21G / SAM D21J
99- // SMART ARM-Based Microcontroller
100- // DATASHEET
101- // 9.6 (SAMD51) or 9.3.3 (or 10.3.3 depending on which manual)(SAMD21) Serial Number
102- //
103- // EXAMPLE (SAMD21)
104- // ----------------
105- // OpenOCD:
106- // Word0:
107- // > at91samd21g18.cpu mdw 0x0080A00C 1
108- // 0x0080a00c: 6e27f15f
109- // Words 1-3:
110- // > at91samd21g18.cpu mdw 0x0080A040 3
111- // 0x0080a040: 50534b54 332e3120 ff091645
112- //
113- // MicroPython (this code and same order as shown in Arduino IDE)
114- // >>> binascii.hexlify(machine.unique_id())
115- // b'6e27f15f50534b54332e3120ff091645'
116-
117- #if defined(MCU_SAMD21 )
118- uint32_t * id_addresses [4 ] = {(uint32_t * )0x0080A00C , (uint32_t * )0x0080A040 ,
119- (uint32_t * )0x0080A044 , (uint32_t * )0x0080A048 };
120- #elif defined(MCU_SAMD51 )
121- uint32_t * id_addresses [4 ] = {(uint32_t * )0x008061FC , (uint32_t * )0x00806010 ,
122- (uint32_t * )0x00806014 , (uint32_t * )0x00806018 };
123- #endif
124- uint8_t raw_id [16 ];
125-
126- for (int i = 0 ; i < 4 ; i ++ ) {
127- for (int k = 0 ; k < 4 ; k ++ ) {
128- // 'Reverse' the read bytes into a 32 bit word (Consistent with Arduino)
129- raw_id [4 * i + k ] = (* (id_addresses [i ]) >> (24 - k * 8 )) & 0xff ;
130- }
131- }
132-
133- return mp_obj_new_bytes ((byte * )& raw_id , sizeof (raw_id ));
95+ samd_unique_id_t id ;
96+ samd_get_unique_id (& id );
97+ return mp_obj_new_bytes ((byte * )& id .bytes , sizeof (id .bytes ));
13498}
13599STATIC MP_DEFINE_CONST_FUN_OBJ_0 (machine_unique_id_obj , machine_unique_id );
136100
0 commit comments