Skip to content

Commit e44c98e

Browse files
committed
regactored to use RegisterDecoder
1 parent eecebce commit e44c98e

File tree

5 files changed

+70
-130
lines changed

5 files changed

+70
-130
lines changed

csv_loader.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,7 @@
44
import json
55
class CSVRegisterMapLoader(RegisterMapLoader):
66
def __init__(self, csv_files=None, serial_device=None):
7-
self._map = {0: {}, 1: {}, 2: {}, 3: {}, -1:
8-
{
9-
127:{
10-
'name': 'REG_BANK_SEL',
11-
'address': 127,
12-
0: '',
13-
1: '',
14-
2: '',
15-
3: '',
16-
4: 'USER_BANK[1:0]',
17-
5: 'USER_BANK[1:0]',
18-
6: '',
19-
7: '',
20-
'last_read_value': None}}}
7+
218
self._map = {0:{}}
229
# self._map = {0: {}}
2310
# print("CSV files:", csv_files)

i2c_transactions.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# TODO: Measure time between calls/
21
import os
32
import json
43
from saleae.analyzers import HighLevelAnalyzer, AnalyzerFrame, StringSetting, NumberSetting, ChoicesSetting
4+
from register_decoder import RegisterDecoder
5+
import csv_loader
6+
# import json_loader
57
# https://support.saleae.com/extensions/analyzer-frame-types
68

79
class Transaction:
@@ -104,8 +106,12 @@ def __init__(self):
104106
self.register_map_file = None
105107
self.current_bank = 0
106108
self.current_map = {}
107-
self.register_map_file = '/Users/bs/dev/tooling/i2c_txns/maps/register_map_v1.json'
109+
# take from setting
110+
self.register_map_file = '/Users/bs/dev/tooling/i2c_txns/maps/as7341_map.csv'
111+
108112
self._load_register_map()
113+
self.decoder = RegisterDecoder(register_map=self.register_map)
114+
109115

110116
def get_capabilities(self):
111117
'''
@@ -131,41 +137,44 @@ def get_capabilities(self):
131137
}
132138

133139
def _load_register_map(self):
134-
if os.path.exists(self.register_map_file):
135-
print("loading register map from %s"%self.register_map_file)
136-
with open(self.register_map_file) as f:
137-
self.register_map = json.load(f)
138-
else:
140+
if not os.path.exists(self.register_map_file):
139141
raise FileNotFoundError("no register map found at %s"%self.register_map_file)
140142

141-
self.current_map = self.register_map[0]
142143

144+
print("loading register map from %s"%self.register_map_file)
145+
if self.register_map_file.endswith(".csv"):
146+
from csv_loader import CSVRegisterMapLoader
147+
map_loader = CSVRegisterMapLoader(self.register_map_file)
143148

149+
elif self.register_map_file.endswith(".json"):
150+
from json_loader import JSONRegisterMapLoader
151+
map_loader = JSONRegisterMapLoader(self.register_map_file)
152+
else:
153+
raise AttributeError("Provided register map does not have a supported extension: [json, csv]"%)
154+
155+
if map_loader.map is None :
156+
raise AttributeError("Register MapLoader could not load a map")
157+
self.register_map = map_loader.map
144158

145159
def process_transaction(self):
146160
txn = self.current_transaction
147161
address_byte = txn.data.pop(0)
148-
if self.mode == MODE_AUTO_INCREMENT_ADDR_MSB_HIGH:
149-
address_byte &= 0x7F # clear any MSB used for auto increment
162+
# if self.mode == MODE_AUTO_INCREMENT_ADDR_MSB_HIGH:
163+
# address_byte &= 0x7F # clear any MSB used for auto increment
150164

151165
############ register naming #####################
152-
# TODO: update register map to use int keys
166+
153167
address_key = str(address_byte)
154168
if address_key in self.current_map.keys():
155169
register_name = self.current_map[address_key]['name']
156170
else:
157171
# TODO: WRITE UNKNOWN DOES NOT DISPLAY CORRECTLY, READ UNKNOWN DOES
158172
register_name = "UNKNOWN[%s]"%hex(address_byte)
159-
print("\tUNKNOWN: ", hex(address_byte))
160-
print("\tUNKNOWN: frame:", str(self.current_frame))
161-
print("\tUNKNOWN: transaction:", str(txn))
162173

163174
txn.register_name = register_name
164175
txn.register_address = address_byte
176+
# transaction_string = self.decoder.decode(self.current_transaction)
165177

166-
if register_name == "BANK":
167-
bank_val = (txn.data[0] & 0x30)>>4
168-
self.current_map = self.register_map[bank_val]
169178
###################################################
170179
transaction_string = str(txn)
171180
print(transaction_string)

map_loader/__init__.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

register_decoder.py

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,16 @@
55
import itertools
66

77
from sys import argv
8-
DEBUG2 = True
9-
DEBUG = True
10-
VERBOSE = True
11-
DEBUG = False
8+
DEBUG = 3
129
VERBOSE = False
1310
ROW_NUMBER_OFFSET = 2
1411
ROW_NUMBER_OFFSET = 2
1512
bank0 = None
1613
bank1 = None
1714
print("*"*100)
1815
BITFIELD_REGEX = '^([^\[]+)\[(\d):(\d)\]$' # matches WHO_AM_I[7:0], DISABLE_ACCEL[5:3] etc.
19-
# AccelDLPFFreq.add_values(
20-
21-
# ( 1, 246.0, None),
22-
# ( 2, 111.4, None),
23-
# ( 3, 50.4, None),
24-
# ( 4, 23.9, None),
25-
# ( 5, 11.5, None),
26-
# ( 6, 5.7, None),
27-
# ( 7, 473, None),
28-
# GUYR
29-
# DEVICE_RESET was set
30-
# SLEEP was unset
31-
# ACCEL_DLPFCFG was changed to FREQ_246_0HZ_3DB
32-
# GYRO_FS_SEL was set
33-
# GYRO_DLPFCFG was changed to FREQ_196_6HZ_3DB
34-
# GYRO_SMPLRT_DIV was changed to 0xa
35-
# I2C_MST_CLK was changed to 0x7
36-
# I2C_MST_P_NSR was set
37-
# I2C_MST_EN was set
38-
# I2C_MST_RST was set
39-
# FIFO_WM_EN was changed to 0xc
40-
# I2C_SLV0_NACK was set
41-
# synched: ./csv_test.sh at Wed Jun 3 09:44:28 PDT 2020
16+
17+
4218
hardcoded_cvs = {
4319
"GYRO_FS_SEL" : [
4420
"±250 dps",
@@ -71,18 +47,18 @@
7147

7248
}
7349
def debug_print(*args, **kwargs):
74-
if DEBUG:
50+
if DEBUG >=3:
7551
print("\t\t\t\t\tDEBUG:", *args, **kwargs)
7652

7753
def newp(new_s, tabs=6):
7854
print("%s \t%s"%("\t"*tabs, new_s))
7955

8056
def verbose_print(*args, **kwargs):
81-
if VERBOSE:
57+
if DEBUG >=3:
8258
print("LOG:", *args, **kwargs)
8359

8460
def verbose_debug(*args, **kwargs):
85-
if VERBOSE and DEBUG:
61+
if DEBUG >=3:
8662
verbose_print("DEBUG:", *args, **kwargs)
8763
def pretty(d, indent=0):
8864
for key, value in d.items():
@@ -99,9 +75,9 @@ def __init__(self, register_map=None):
9975
self.prev_single_byte_write = None
10076
self.current_bank = -1
10177
# print("reg map length:", len(self.register_map))
102-
# print("********* REG MAP?!*************")
103-
# pretty(self.register_map)
104-
# print("*************************************")
78+
print("********* REG MAP?!*************")
79+
pretty(self.register_map)
80+
print("*************************************")
10581
def decode(self, row_num, row):
10682

10783
if len(self.register_map) is 1 and self.current_bank is -1:
@@ -147,9 +123,26 @@ def decode(self, row_num, row):
147123

148124
########### Decode #################
149125
self.decode_bytes(rw, b0, b1)
126+
def decode_transaction(self, reg_txn):
127+
# reg_txn.is_read
128+
# reg_txn.i2c_node_addr #sensor/outgoing address
129+
# reg_txn.register_address #destination of write, source of read
130+
# reg_txn.data # ints/non-string list
131+
if reg_txn.is_read:
132+
rw = "READ"
133+
else:
134+
rw= "WRITE"
135+
if DEBUG >=3:
136+
print("[%s"%reg_txn.i2c_node_addr)
150137

138+
self.decode_bytes(rw, reg_txn.data[0], reg_txn.data[1])
139+
140+
return "[UNDER CONSTRUCTION]"
141+
142+
# TODO: Take bool, for rw
143+
# TODO: Return string, print from caller
151144
def decode_bytes(self, rw, b0, b1):
152-
if DEBUG2:
145+
if DEBUG >=2:
153146
if not b0:
154147
b0s = " "
155148
else:
@@ -238,7 +231,7 @@ def bitfield_change_str(self, bitfield, unset_bitmask, set_bitmask, new_value):
238231
else:
239232
bf_value = hex(bf_value)
240233
change_str = "%s was changed to %s"%(bf_name, bf_value)
241-
if DEBUG2 and change_str:
234+
if DEBUG >=2 and change_str:
242235
change_str = "\t%s"%change_str
243236
return change_str
244237

@@ -314,18 +307,4 @@ def _b( num):
314307
source_files = sys.argv[2:]
315308
map_loader = None
316309

317-
if source_files[0].endswith(".json"):
318-
from register_decoder.map_loader.json_loader import JSONRegisterMapLoader
319-
map_loader = JSONRegisterMapLoader(source_files[0])
320-
elif source_files[0].endswith(".csv"):
321-
from register_decoder.map_loader.csv_loader import CSVRegisterMapLoader
322-
map_loader = CSVRegisterMapLoader(source_files)
323-
if map_loader.map is None :
324-
raise AttributeError("MAP is None")
325-
326-
decoder = RegisterDecoder(register_map=map_loader.map)
327-
328-
with open(sys.argv[1], newline="") as csvfile:
329-
reader = csv.DictReader(csvfile)
330-
for row_num, row in enumerate(reader):
331-
decoder.decode(row_num, row)
310+

tests/csv_test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
#!/usr/bin/env bash
22
clear
3+
# self._map = {0: {}, 1: {}, 2: {}, 3: {}, -1:
4+
# {
5+
# 127:{
6+
# 'name': 'REG_BANK_SEL',
7+
# 'address': 127,
8+
# 0: '',
9+
# 1: '',
10+
# 2: '',
11+
# 3: '',
12+
# 4: 'USER_BANK[1:0]',
13+
# 5: 'USER_BANK[1:0]',
14+
# 6: '',
15+
# 7: '',
16+
# 'last_read_value': None}}}
317
python register_decoder/decoder.py test.csv maps/bank0.csv maps/bank1.csv maps/bank2.csv maps/bank3.csv
418

0 commit comments

Comments
 (0)