Skip to content

Commit 0cd8d59

Browse files
authored
Eeprom protobuf (#131)
* protobuf implementation #126 #124 * test added * pylint fix and update dependencies * Update requirements_test.txt * delete Todos * suggested fixes * suggested name fix
1 parent 1864d83 commit 0cd8d59

File tree

9 files changed

+723
-8
lines changed

9 files changed

+723
-8
lines changed

requirements_test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ tomli==2.0.1
1212
bitstring==3.1.9
1313
pytest-mock==3.7.0
1414
pytest-cov==3.0.0
15+
protobuf==3.20.3

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
packages=setuptools.find_packages(where="src", exclude=["test_edgepi"]),
2626
package_dir={"": "src"},
2727
python_requires=">=3.6",
28-
install_requires=["python-periphery >= 2.3.0", "bitstring >= 3.1.9"],
28+
install_requires=["python-periphery >= 2.3.0", "bitstring >= 3.1.9", "protobuf==3.20.*"],
2929
)

src/edgepi/calibration/edgepi_eeprom.py

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
import logging
44
import 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
712
from 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+
1115
class 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.

src/edgepi/calibration/eeprom_constants.py

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'''Address map of eeprom'''
22

33
from enum import Enum
4+
from dataclasses import dataclass
5+
from edgepi.calibration.eeprom_mapping_pb2 import EepromLayout
46

57
class EEPROMInfo(Enum):
68
"""
@@ -19,13 +21,13 @@ class ModuleNames(Enum):
1921
RTD = 0x2
2022
TC = 0x3
2123

24+
# TODO: to be deleted once protobuf is implemented
2225
class DACParamAddr(Enum):
2326
"""
2427
EdgePi DAC Calibration Parameter Addresses
2528
Each parameter, gain and offset, are 4 bytes long 0x200~0x23F
2629
Ex) CH0_gain = 0x200~0x203, CH0_offset = 0x204~0x207
2730
"""
28-
2931
CH0 = 0x200
3032
CH1 = 0x208
3133
CH2 = 0x210
@@ -35,13 +37,12 @@ class DACParamAddr(Enum):
3537
CH6 = 0x230
3638
CH7 = 0x238
3739
LEN = 63
38-
40+
# TODO: to be deleted once protobuf is implemented
3941
class ADCParamAddr(Enum):
4042
"""
4143
EdgePi DAC Calibration Parameter Addresses
4244
Each parameter, gain and offset, are 4 bytes long
4345
"""
44-
4546
CH0 = 0x240
4647
CH1 = 0x248
4748
CH2 = 0x240
@@ -54,7 +55,7 @@ class ADCParamAddr(Enum):
5455
DIFF2 =0x258
5556
DIFF3 =0x260
5657
DIFF4 =0x268
57-
58+
# TODO: to be deleted once protobuf is implemented
5859
class MemoryAddr(Enum):
5960
"""
6061
Memory offset values
@@ -66,3 +67,82 @@ class MemoryAddr(Enum):
6667
RTD = 0x2C0
6768
END = 0x3FF
6869
CH_OFFSET = 0x8
70+
71+
class EdgePiMemoryInfo(Enum):
72+
"""
73+
Information regarding Edgepi reserved memory space
74+
"""
75+
USED_SPACE = 0x00
76+
BUFF_START = 0x02
77+
78+
class MessageFieldNumber(Enum):
79+
"""
80+
MessageField index number to be used for ListFields() function. The function lists fields start
81+
from index 0
82+
"""
83+
DAC=1
84+
ADC=2
85+
RTD=3
86+
TC=4
87+
CONFIGS_KEY=5
88+
DATA_KEY=6
89+
SERIAL=7
90+
MODEL=8
91+
CLIENT_ID=9
92+
93+
@dataclass
94+
class Keys:
95+
"""
96+
Dataclass to store key strings
97+
"""
98+
certificate: str = None
99+
private: str = None
100+
101+
@dataclass
102+
class EdgePiEEPROMData:
103+
# pylint: disable=too-many-instance-attributes
104+
"""
105+
Dataclass to store edgepi reserved values
106+
dac_list (list): list of calibration parameters
107+
adc_list (list): list of calibration parameters
108+
rtd_list (list): list of calibration parameters
109+
tc_list (list): list of calibration parameters
110+
config_key (Keys): dataclass
111+
data_key (Keys): dataclass
112+
serial (str)
113+
model (str)
114+
client_id (str)
115+
"""
116+
dac_calib_parms: list = None
117+
adc_calib_parms: list = None
118+
rtd_calib_parms: list = None
119+
tc_calib_parms: list = None
120+
config_key: Keys = None
121+
data_key: Keys = None
122+
serial: str = None
123+
model: str = None
124+
client_id: str = None
125+
126+
def message_to_list(self, data_to_unpack: EepromLayout = None):
127+
"""
128+
Function to unpack message to list
129+
Args:
130+
data_to_unpack: EepromLayout message modules
131+
Returns:
132+
calib_list: 1-D array
133+
"""
134+
calib_list=[]
135+
for ch in data_to_unpack.calibs:
136+
calib_list.append(ch.gain)
137+
calib_list.append(ch.offset)
138+
return calib_list
139+
140+
def keys_to_str(self, data_to_unpack: EepromLayout = None):
141+
"""
142+
Function to unpack message to string
143+
Args:
144+
data_to_unpack: EepromLayout message keys
145+
Returns:
146+
Keys (dataclass): keys values
147+
"""
148+
return Keys(certificate = data_to_unpack.certificate, private = data_to_unpack.private_key)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
syntax = "proto3";
2+
3+
message EepromLayout{
4+
message ModuleCalibParams{
5+
message ChannelCalib {
6+
optional float gain = 1;
7+
optional float offset = 2;
8+
}
9+
repeated ChannelCalib calibs = 1;
10+
}
11+
message AwsKey{
12+
string private_key = 1;
13+
string certificate = 2;
14+
}
15+
ModuleCalibParams dac = 1;
16+
ModuleCalibParams adc = 2;
17+
ModuleCalibParams rtd = 3;
18+
ModuleCalibParams tc = 4;
19+
AwsKey config_key = 5;
20+
AwsKey data_key =6;
21+
string serial_number = 7;
22+
string model = 8;
23+
string client_id = 9;
24+
}

0 commit comments

Comments
 (0)