8000 stm32/mphalport: Add support for having MAC in OTP region. · shazz/micropython@370a811 · GitHub
[go: up one dir, main page]

Skip to content

Commit 370a811

Browse files
committed
stm32/mphalport: Add support for having MAC in OTP region.
1 parent 10e173a commit 370a811

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

ports/stm32/mphalport.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,30 @@ void mp_hal_pin_config_speed(mp_hal_pin_obj_t pin_obj, uint32_t speed) {
151151
gpio->OSPEEDR = (gpio->OSPEEDR & ~(3 << (2 * pin))) | (speed << (2 * pin));
152152
}
153153

154+
/*******************************************************************************/
155+
// MAC address
156+
157+
typedef struct _pyb_otp_t {
158+
uint16_t series;
159+
uint16_t rev;
160+
uint8_t mac[6];
161+
} pyb_otp_t;
162+
163+
#if defined(STM32F722xx) || defined(STM32F723xx) || defined(STM32F732xx) || defined(STM32F733xx)
164+
#define OTP_ADDR (0x1ff079e0)
165+
#else
166+
#define OTP_ADDR (0x1ff0f3c0)
167+
#endif
168+
#define OTP ((pyb_otp_t*)OTP_ADDR)
169+
154170
MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) {
171+
// Check if OTP region has a valid MAC address, and use it if it does
172+
if (OTP->series == 0x00d1 && OTP->mac[0] == 'H' && OTP->mac[1] == 'J' && OTP->mac[2] == '0') {
173+
memcpy(buf, OTP->mac, 6);
174+
buf[5] += idx;
175+
return;
176+
}
177+
155178
// Generate a random locally administered MAC address (LAA)
156179
uint8_t *id = (uint8_t *)MP_HAL_UNIQUE_ID_ADDRESS;
157180
buf[0] = 0x02; // LAA range
@@ -161,3 +184,12 @@ MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) {
161184
buf[4] = id[2];
162185
buf[5] = (id[0] << 2) | idx;
163186
}
187+
188+
void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) {
189+
static const char hexchr[16] = "0123456789ABCDEF";
190+
uint8_t mac[6];
191+
mp_hal_get_mac(idx, mac);
192+
for (; chr_len; ++chr_off, --chr_len) {
193+
*dest++ = hexchr[mac[chr_off >> 1] >> (4 * (1 - (chr_off & 1))) & 0xf];
194+
}
195+
}

ports/stm32/mphalport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,4 @@ enum {
8888
};
8989

9090
void mp_hal_get_mac(int idx, uint8_t buf[6]);
91+
void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest);

0 commit comments

Comments
 (0)
0