Added mac converter class for easy reading of mac address from microcontroller.
This commit is contained in:
parent
785c8d7874
commit
f4df95e6a0
2 changed files with 32 additions and 0 deletions
1
flashing/.gitignore
vendored
Normal file
1
flashing/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
__pycache__/
|
||||
31
flashing/mac_converter.py
Normal file
31
flashing/mac_converter.py
Normal file
|
|
@ -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()
|
||||
Loading…
Add table
Reference in a new issue