Skip to content

Commit

Permalink
stm32/mphalport: Put PYBD specific MAC code in board specific file.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgeorge committed Aug 28, 2019
1 parent 08c1fe5 commit b1e0484
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
26 changes: 26 additions & 0 deletions ports/stm32/boards/PYBD_SF2/board_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,23 @@
* THE SOFTWARE.
*/

#include <string.h>
#include "py/mphal.h"
#include "storage.h"

#if defined(STM32F722xx) || defined(STM32F723xx) || defined(STM32F732xx) || defined(STM32F733xx)
#define OTP_ADDR (0x1ff079e0)
#else
#define OTP_ADDR (0x1ff0f3c0)
#endif
#define OTP ((pyb_otp_t*)OTP_ADDR)

typedef struct _pyb_otp_t {
uint16_t series;
uint16_t rev;
uint8_t mac[6];
} pyb_otp_t;

void mboot_board_early_init(void) {
// Enable 500mA on WBUS-DIP28
mp_hal_pin_config(pyb_pin_W23, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_UP, 0);
Expand All @@ -44,3 +58,15 @@ void board_sleep(int value) {
mp_spiflash_deepsleep(&spi_bdev.spiflash, value);
mp_spiflash_deepsleep(&spi_bdev2.spiflash, value);
}

void mp_hal_get_mac(int idx, uint8_t buf[6]) {
// Check if OTP region has a valid MAC address, and use it if it does
if (OTP->series == 0x00d1 && OTP->mac[0] == 'H' && OTP->mac[1] == 'J' && OTP->mac[2] == '0') {
memcpy(buf, OTP->mac, 6);
buf[5] += idx;
return;
}

// Generate a random locally administered MAC address (LAA)
mp_hal_generate_laa_mac(idx, buf);
}
29 changes: 7 additions & 22 deletions ports/stm32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,8 @@ void mp_hal_pin_config_speed(mp_hal_pin_obj_t pin_obj, uint32_t speed) {
/*******************************************************************************/
// MAC address

typedef struct _pyb_otp_t {
uint16_t series;
uint16_t rev;
uint8_t mac[6];
} pyb_otp_t;

#if defined(STM32F722xx) || defined(STM32F723xx) || defined(STM32F732xx) || defined(STM32F733xx)
#define OTP_ADDR (0x1ff079e0)
#else
#define OTP_ADDR (0x1ff0f3c0)
#endif
#define OTP ((pyb_otp_t*)OTP_ADDR)

MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) {
// Check if OTP region has a valid MAC address, and use it if it does
if (OTP->series == 0x00d1 && OTP->mac[0] == 'H' && OTP->mac[1] == 'J' && OTP->mac[2] == '0') {
memcpy(buf, OTP->mac, 6);
buf[5] += idx;
return;
}

// Generate a random locally administered MAC address (LAA)
// Generate a random locally administered MAC address (LAA)
void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]) {
uint8_t *id = (uint8_t *)MP_HAL_UNIQUE_ID_ADDRESS;
buf[0] = 0x02; // LAA range
buf[1] = (id[11] << 4) | (id[10] & 0xf);
Expand All @@ -199,6 +179,11 @@ MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) {
buf[5] = (id[0] << 2) | idx;
}

// A board can override this if needed
MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) {
mp_hal_generate_laa_mac(idx, buf);
}

void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) {
static const char hexchr[16] = "0123456789ABCDEF";
uint8_t mac[6];
Expand Down
1 change: 1 addition & 0 deletions ports/stm32/mphalport.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,6 @@ enum {
MP_HAL_MAC_ETH0,
};

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

0 comments on commit b1e0484

Please sign in to comment.