Commit f8f80ffe authored by Klint Youngmeyer's avatar Klint Youngmeyer
Browse files

Add support for reading/writing the ETH MAC Address to the 0x080E0000 block of flash on the MitiPy.

A CSV file with the MAC address needs to be placed on the flash. The format
for a MitiPy follows:

131,<REV>,XX:XX:XX:XX:XX:XX

    The 131 indicates a MitiPy
    The <REV> should be replacede with the hardware rev
    The XXs should be replaced with the MAC address

The MAC address will only be written once, or if the file changes.
parent 870ea0f1
from micropython import const
import struct
import time
import uzlib
import machine
import stm
import ubinascii
FLASH_KEY1 = 0x45670123
FLASH_KEY2 = 0xCDEF89AB
def check_mem_contains(addr, buf):
mem8 = stm.mem8
r = range(len(buf))
for off in r:
if mem8[addr + off] != buf[off]:
return False
return True
def flash_wait_not_busy():
while stm.mem32[stm.FLASH + stm.FLASH_SR] & 1 << 16:
machine.idle()
def flash_unlock():
stm.mem32[stm.FLASH + stm.FLASH_KEYR] = FLASH_KEY1
stm.mem32[stm.FLASH + stm.FLASH_KEYR] = FLASH_KEY2
def flash_lock():
stm.mem32[stm.FLASH + stm.FLASH_CR] = 1 << 31 # LOCK
def flash_write(addr, buf):
flash_wait_not_busy()
cr = 1 << 0 # PSIZE = 8 bits # PG
stm.mem32[stm.FLASH + stm.FLASH_CR] = cr
for off in range(0, len(buf)):
stm.mem8[addr + off] = buf[off]
flash_wait_not_busy()
stm.mem32[stm.FLASH + stm.FLASH_CR] = 0
def mac_write(addr, data):
if check_mem_contains(addr, data):
return None
print("Writing MAC")
flash_unlock()
flash_write(addr, data)
flash_lock()
print("Done")
def install_mac(address, filename):
raw_csv_data = ""
try:
with open(filename, 'r') as fd:
raw_csv_data = fd.read().strip('\n')
except OSError:
return None
raw_data = raw_csv_data.split(',')
if len(raw_data) != 3:
return None
data = struct.pack("<H", int(raw_data[0]))
data += struct.pack("<H", int(raw_data[1]))
data += ubinascii.unhexlify(raw_data[2].replace(':', ''))
mac_write(address, data)
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018-2019 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include "py/mphal.h"
#include "storage.h"
#include "stdio.h"
#define OTP_ADDR (0x080E0000)
#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 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 == 0x0083 && OTP->mac[0] == 0x28 && OTP->mac[1] == 0xBC && OTP->mac[2] == 0x56) {
memcpy(buf, OTP->mac, 6);
return;
}
// Generate a random locally administered MAC address (LAA)
mp_hal_generate_laa_mac(idx, buf);
}
include('$(PORT_DIR)/boards/manifest.py')
freeze('$(MPY_DIR)/drivers/EMAC', ('umqtt/robust.py', 'umqtt/simple.py'))
freeze('$(MPY_DIR)/drivers/EMAC', ('battery.py', 'hidrive.py', 'logging.py', 'rs485.py', 'test.py', 'socket_test.py'))
freeze('$(MPY_DIR)/drivers/EMAC', ('battery.py', 'hidrive.py', 'logging.py', 'rs485.py', 'test.py', 'socket_test.py', 'mac_install.py'))
......@@ -43,6 +43,12 @@ static const char fresh_boot_py[] =
"import machine\r\n"
"import pyb\r\n"
"pyb.country('US') # ISO 3166-1 Alpha-2 code, eg US, GB, DE, AU\r\n"
#if defined(MITIPY)
"\r\n"
"import mac_install\r\n"
"mac_install.install_mac(0x080E0000, '/flash/.macaddr')\r\n"
"del mac_install\r\n"
#endif
#if defined(CUTIPY) || defined(MITIPY)
"\r\n"
"# Uncomment to set UART2 to be a serial REPL at 115200 baud.\r\n"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment