33import logging
44import math
55
6- from edgepi .calibration .eeprom_constants import EEPROMInfo
6+ from edgepi .calibration .eeprom_constants import (
7+ EEPROMInfo ,
8+ EdgePiMemoryInfo ,
9+ MessageFieldNumber ,
10+ EdgePiEEPROMData )
11+ from edgepi .calibration .eeprom_mapping_pb2 import EepromLayout
712from edgepi .peripherals .i2c import I2CDevice
813
9- #TODO: Maybe use protobuff
10- #TODO: EEPROM should return structured data class of parameters to calibration class.
14+
1115class EdgePiEEPROM (I2CDevice ):
1216 '''
1317 Helper class to read eeprom using I2C
@@ -16,6 +20,7 @@ class EdgePiEEPROM(I2CDevice):
1620
1721 def __init__ (self ):
1822 self .log = logging .getLogger (__name__ )
23+ self .eeprom_layout = EepromLayout ()
1924 super ().__init__ (self .__dev_path )
2025
2126 def __pack_mem_address (self , page_addr : int = None , byte_addr : int = None ):
@@ -44,6 +49,67 @@ def __byte_address_generation(self, memory_address: int = None):
4449 self .log .debug (f'Page address = { page_addr } , byte Address = { byte_addr } ' )
4550 return page_addr , byte_addr
4651
52+ def __allocated_memory (self ):
53+ '''
54+ The first two bytes represenst the allocated memory in Edgepi reserved memory space. This
55+ function returns the length of memory to read.
56+ Args:
57+ N/A
58+ Return:
59+ length (int): size of memory to read
60+ '''
61+ length = self .sequential_read (EdgePiMemoryInfo .USED_SPACE .value , 2 )
62+ return (length [0 ]<< 8 )| length [1 ]
63+
64+ def __read_edgepi_reserved_memory (self ):
65+ '''
66+ Read Edgepi reserved memory space to retreive parameters. This function will return byte
67+ strings, that can be converted into protocol buffer message format
68+ Args:
69+ N/A
70+ Return:
71+ Byte_string (bytes): strings of bytes read from the eeprom
72+ '''
73+ mem_size = self .__allocated_memory ()
74+ buff_list = self .sequential_read (EdgePiMemoryInfo .BUFF_START .value , mem_size )
75+ return bytes (buff_list )
76+
77+ def get_message_of_interest (self , msg : MessageFieldNumber = None ):
78+ """
79+ This function filters out the message according to the specified field number passed as
80+ parameter.
81+ Args:
82+ msg (MessageFieldNumber): protocol buffer message field index number for ListFields()
83+ function
84+ Return:
85+ pb message specified by the message field number. ex) if message field of DAC is passed,
86+ the dac message will be returned
87+ """
88+ self .eeprom_layout .ParseFromString (self .__read_edgepi_reserved_memory ())
89+ return self .eeprom_layout .ListFields ()[msg .value - 1 ][1 ]
90+
91+ def get_edgepi_reserved_data (self ):
92+ """
93+ Read Edgepi reserved memory space and populate dataclass
94+ Args:
95+ N/A
96+ Return:
97+ eeprom_data (EdgePiEEPROMData): dataclass containing eeprom values
98+ """
99+ # pylint: disable=no-member
100+ self .eeprom_layout .ParseFromString (self .__read_edgepi_reserved_memory ())
101+ eeprom_data = EdgePiEEPROMData ()
102+ eeprom_data .dac_calib_parms = eeprom_data .message_to_list (self .eeprom_layout .dac )
103+ eeprom_data .adc_calib_parms = eeprom_data .message_to_list (self .eeprom_layout .adc )
104+ eeprom_data .rtd_calib_parms = eeprom_data .message_to_list (self .eeprom_layout .rtd )
105+ eeprom_data .tc_calib_parms = eeprom_data .message_to_list (self .eeprom_layout .tc )
106+ eeprom_data .config_key = eeprom_data .keys_to_str (self .eeprom_layout .config_key )
107+ eeprom_data .data_key = eeprom_data .keys_to_str (self .eeprom_layout .data_key )
108+ eeprom_data .serial = self .eeprom_layout .serial_number
109+ eeprom_data .model = self .eeprom_layout .model
110+ eeprom_data .client_id = self .eeprom_layout .client_id
111+ return eeprom_data
112+
47113 def sequential_read (self , mem_addr : int = None , length : int = None ):
48114 '''
49115 Read operation reads the specified number of memory location starting from provided address.
0 commit comments