From cc63e19332bb8bf4134ef27116deeb1935ddf365 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Feb 2019 21:53:41 +1100 Subject: [PATCH] stm32/mphalport: Add mp_hal_get_mac() helper function. --- ports/stm32/mphalport.c | 11 +++++++++++ ports/stm32/mphalport.h | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c index 206221721b..9e59eb6138 100644 --- a/ports/stm32/mphalport.c +++ b/ports/stm32/mphalport.c @@ -162,3 +162,14 @@ void mp_hal_pin_config_speed(mp_hal_pin_obj_t pin_obj, uint32_t speed) { uint32_t pin = pin_obj->pin; gpio->OSPEEDR = (gpio->OSPEEDR & ~(3 << (2 * pin))) | (speed << (2 * pin)); } + +MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) { + // Generate a random locally administered MAC address (LAA) + uint8_t *id = (uint8_t *)MP_HAL_UNIQUE_ID_ADDRESS; + buf[0] = 0x02; // LAA range + buf[1] = (id[11] << 4) | (id[10] & 0xf); + buf[2] = (id[9] << 4) | (id[8] & 0xf); + buf[3] = (id[7] << 4) | (id[6] & 0xf); + buf[4] = id[2]; + buf[5] = (id[0] << 2) | idx; +} diff --git a/ports/stm32/mphalport.h b/ports/stm32/mphalport.h index a60365087e..d828bb9dd4 100644 --- a/ports/stm32/mphalport.h +++ b/ports/stm32/mphalport.h @@ -79,3 +79,12 @@ void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio); void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt); bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint8_t fn, uint8_t unit); void mp_hal_pin_config_speed(mp_hal_pin_obj_t pin_obj, uint32_t speed); + +enum { + MP_HAL_MAC_WLAN0 = 0, + MP_HAL_MAC_WLAN1, + MP_HAL_MAC_BDADDR, + MP_HAL_MAC_ETH0, +}; + +void mp_hal_get_mac(int idx, uint8_t buf[6]);