From f4df95e6a0e866ccb6e41429dc6bbfd5efcca9b3 Mon Sep 17 00:00:00 2001 From: Ole Odendahl Date: Sun, 23 Oct 2022 11:38:12 +0200 Subject: [PATCH] Added mac converter class for easy reading of mac address from microcontroller. --- flashing/.gitignore | 1 + flashing/mac_converter.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 flashing/.gitignore create mode 100644 flashing/mac_converter.py diff --git a/flashing/.gitignore b/flashing/.gitignore new file mode 100644 index 0000000..ba0430d --- /dev/null +++ b/flashing/.gitignore @@ -0,0 +1 @@ +__pycache__/ \ No newline at end of file diff --git a/flashing/mac_converter.py b/flashing/mac_converter.py new file mode 100644 index 0000000..800c7e7 --- /dev/null +++ b/flashing/mac_converter.py @@ -0,0 +1,31 @@ +from pyocd.core.target import Target + + +class MacConverter(): + + MEMORY_ADDRESS = 268435620 # 0x100000A4 + + def __init__(self, target: Target) -> None: + self.target = target + + def get_mac_address(self) -> str: + memory_blocks = self._get_blocks() + + return self._convert_blocks(memory_blocks) + + def _get_blocks(self): + memory_blocks = self.target.read_memory_block32(self.MEMORY_ADDRESS, 2) + + return memory_blocks + + def _convert_blocks(self, memory_blocks: list[int]) -> str: + first_block = f'{memory_blocks[0]:x}'.rjust(8, '0') + + modified_second_block = memory_blocks[1] | 49152 # Bitwise OR with 0xc000 + + second_block = f'{modified_second_block:x}' + + mac_string = (second_block[4:] + first_block) + prettified_mac = ':'.join(mac_string[i:i+2] for i in range(0, len(mac_string), 2)) + + return prettified_mac.upper()