stm32/mphalport: Put PYBD specific MAC code in board specific file.

pull/1/head
Damien George 2019-08-21 23:06:56 +10:00
parent 08c1fe5569
commit b1e04848ef
3 changed files with 34 additions and 22 deletions

View File

@ -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);
@ -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);
}

View File

@ -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);
@ -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];

View File

@ -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);