-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DRAFT] Sending CAN messages on peripherals 1 and 2 #142
Draft
sethbaird01
wants to merge
8
commits into
master
Choose a base branch
from
feature/sethbaird01/can_periph2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ad7531
prelim support for split-can
sethbaird01 42e4dff
split-can on tx
sethbaird01 3ec9826
what the fuck is a junction node!!!!🦅🦅🦅🇺🇸🇺🇸
sethbaird01 ab7aa31
fix schema's rx periph
sethbaird01 743d1d7
rx messages breakdown
sethbaird01 48039f3
fix gen_switch_case regression
sethbaird01 abe499e
comments for later
sethbaird01 b2178e2
split tx messages on can1 and can2
sethbaird01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ | |
gen_can_enums_start = "BEGIN AUTO CAN ENUMERATIONS" | ||
gen_can_enums_stop = "END AUTO CAN ENUMERATIONS" | ||
|
||
DEFAULT_PERIPHERAL = "CAN1" | ||
DEFAULT_PERIPHERAL_NODE = "CAN1" | ||
|
||
def find_rx_messages(rx_names): | ||
""" | ||
|
@@ -79,35 +79,79 @@ def gen_send_macro(lines, msg_config, peripheral): | |
lines.append(f" }} while(0)\n") | ||
|
||
def gen_filter_lines(lines, rx_msg_configs, peripheral): | ||
# peripheral being passed in is node-wise | ||
""" generates hardware filters for a set of message definitions for a specific peripheral """ | ||
on_mask = False | ||
filter_bank = 0 | ||
filter_bank_max = 27 | ||
|
||
if peripheral == "CAN1": | ||
filter_bank = 0 | ||
filter_bank_max = 14 | ||
elif peripheral == "CAN2": | ||
filter_bank = 14 | ||
filter_bank_max = 27 | ||
else: | ||
print(f"Unknown CAN peripheral {peripheral}") | ||
tlp = peripheral | ||
on_mask_periph = {"CAN1": False, "CAN2": False} # separated for safety (i don't know what this does) | ||
filter_bank_periph = {"CAN1": 0, "CAN2": 14} | ||
filter_bank_max_periph = {"CAN1": 14, "CAN2": 27} | ||
|
||
# these lines were default behavior, just moved into tuples above and symbols renamed | ||
# if peripheral == "CAN1": | ||
# filter_bank = 0 | ||
# filter_bank_max = 14 | ||
# elif peripheral == "CAN2": | ||
# filter_bank = 14 | ||
# filter_bank_max = 27 | ||
|
||
can1_msgs = [] | ||
can2_msgs = [] | ||
|
||
# look for messages on each bus | ||
|
||
for msg in rx_msg_configs: | ||
if(filter_bank > filter_bank_max): | ||
|
||
if 'can_peripheral_override' in msg: | ||
print(f"rx can override detected in {msg}") | ||
# use overridden periph | ||
peripheral = msg['can_peripheral_override'] | ||
else: | ||
peripheral = tlp | ||
|
||
if peripheral == "CAN1": | ||
can1_msgs.append(msg) | ||
if peripheral == "CAN2": | ||
can2_msgs.append(msg) | ||
|
||
# split old loop into two to ensure correct filter writing in genned code | ||
for msg in can1_msgs: | ||
peripheral = "CAN1" | ||
if(filter_bank_periph[peripheral] > filter_bank_max_periph[peripheral]): | ||
generator.log_error(f"Max filter bank reached for node containing msg {msg['msg_name']}") | ||
quit(1) | ||
|
||
# For extended id vs standard id | ||
shift_phrase = f"(ID_{msg['msg_name'].upper()} << 3) | 4" if ('is_normal' not in msg or msg['is_normal'] == False) else \ | ||
f"(ID_{msg['msg_name'].upper()} << 21)" | ||
if not on_mask: | ||
lines.append(f" CAN1->FA1R |= (1 << {filter_bank}); // configure bank {filter_bank}\n") | ||
lines.append(f" CAN1->sFilterRegister[{filter_bank}].FR1 = {shift_phrase};\n") | ||
on_mask = True | ||
f"(ID_{msg['msg_name'].upper()} << 21)" | ||
if not on_mask_periph[peripheral]: | ||
lines.append(f" CAN1->FA1R |= (1 << {filter_bank_periph[peripheral]}); // configure bank {filter_bank_periph[peripheral]}\n") | ||
lines.append(f" CAN1->sFilterRegister[{filter_bank_periph[peripheral]}].FR1 = {shift_phrase};\n") | ||
on_mask_periph[peripheral] = True | ||
else: | ||
lines.append(f" CAN1->sFilterRegister[{filter_bank}].FR2 = {shift_phrase};\n") | ||
on_mask = False | ||
filter_bank += 1 | ||
lines.append(f" CAN1->sFilterRegister[{filter_bank_periph[peripheral]}].FR2 = {shift_phrase};\n") | ||
on_mask_periph[peripheral] = False | ||
filter_bank_periph[peripheral] += 1 | ||
|
||
for msg in can2_msgs: | ||
peripheral = "CAN2" | ||
if(filter_bank_periph[peripheral] > filter_bank_max_periph[peripheral]): | ||
generator.log_error(f"Max filter bank reached for node containing msg {msg['msg_name']}") | ||
quit(1) | ||
|
||
# For extended id vs standard id | ||
shift_phrase = f"(ID_{msg['msg_name'].upper()} << 3) | 4" if ('is_normal' not in msg or msg['is_normal'] == False) else \ | ||
f"(ID_{msg['msg_name'].upper()} << 21)" | ||
if not on_mask_periph[peripheral]: | ||
lines.append(f" CAN1->FA1R |= (1 << {filter_bank_periph[peripheral]}); // configure bank {filter_bank_periph[peripheral]}\n") | ||
lines.append(f" CAN1->sFilterRegister[{filter_bank_periph[peripheral]}].FR1 = {shift_phrase};\n") | ||
on_mask_periph[peripheral] = True | ||
else: | ||
lines.append(f" CAN1->sFilterRegister[{filter_bank_periph[peripheral]}].FR2 = {shift_phrase};\n") | ||
on_mask_periph[peripheral] = False | ||
filter_bank_periph[peripheral] += 1 | ||
|
||
|
||
|
||
|
||
def gen_switch_case(lines, rx_msg_configs, rx_callbacks, ind=""): | ||
""" generates switch case for receiving messages """ | ||
|
@@ -154,30 +198,30 @@ def configure_node(node_config, node_paths): | |
print("Configuring Node " + node_config['node_name']) | ||
|
||
# Junction node? | ||
is_junc = False | ||
junc_config = None | ||
if 'is_junction' in node_config and node_config['is_junction']: | ||
is_junc = True | ||
print(f"Treating {node_config['node_name']} as junction") | ||
global can_config | ||
for bus in can_config['busses']: | ||
for node in bus['nodes']: | ||
if node['node_name'] == node_config['node_name'] and node['can_peripheral'] != node_config['can_peripheral']: | ||
junc_config = node | ||
break | ||
if junc_config: break | ||
# is_junc = False | ||
# junc_config = None | ||
# if 'is_junction' in node_config and node_config['is_junction']: | ||
# is_junc = True | ||
# print(f"Treating {node_config['node_name']} as junction") | ||
# global can_config | ||
# for bus in can_config['busses']: | ||
# for node in bus['nodes']: | ||
# if node['node_name'] == node_config['node_name'] and node['can_peripheral'] != node_config['can_peripheral']: | ||
# junc_config = node | ||
# break | ||
# if junc_config: break | ||
|
||
# Combine message definitions | ||
raw_msg_defs = [] | ||
raw_msg_defs += node_config['tx'] | ||
if is_junc: raw_msg_defs += junc_config['tx'] | ||
# if is_junc: raw_msg_defs += junc_config['tx'] | ||
receiving_msg_defs = [] | ||
node_specific_rx_msg_defs = find_rx_messages([rx_config["msg_name"] for rx_config in node_config['rx']]) | ||
receiving_msg_defs += node_specific_rx_msg_defs | ||
junc_rx_msg_defs = [] | ||
if is_junc: | ||
junc_rx_msg_defs += find_rx_messages([rx_config['msg_name'] for rx_config in junc_config['rx']]) | ||
receiving_msg_defs += junc_rx_msg_defs | ||
# junc_rx_msg_defs = [] | ||
# if is_junc: | ||
# junc_rx_msg_defs += find_rx_messages([rx_config['msg_name'] for rx_config in junc_config['rx']]) | ||
# receiving_msg_defs += junc_rx_msg_defs | ||
for new_msg in receiving_msg_defs: | ||
if new_msg not in raw_msg_defs: | ||
raw_msg_defs.append(new_msg) | ||
|
@@ -201,14 +245,19 @@ def configure_node(node_config, node_paths): | |
|
||
# Send Macros, requires knowledge of CAN peripheral | ||
macro_lines = [] | ||
periph = DEFAULT_PERIPHERAL | ||
periph = DEFAULT_PERIPHERAL_NODE | ||
if 'can_peripheral' in node_config: periph = node_config['can_peripheral'] | ||
for msg in node_config['tx']: | ||
periph = DEFAULT_PERIPHERAL_NODE | ||
|
||
if 'can_peripheral_override' in msg: | ||
periph = msg['can_peripheral_override'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation seems a bit too deep. I have no idea if python will get mad at this or not. |
||
# print(f"found override in {msg}") | ||
gen_send_macro(macro_lines, msg, periph) | ||
if is_junc: | ||
periph = junc_config['can_peripheral'] | ||
for msg in junc_config['tx']: | ||
gen_send_macro(macro_lines, msg, periph) | ||
# if is_junc: | ||
# periph = junc_config['can_peripheral'] | ||
# for msg in junc_config['tx']: | ||
# gen_send_macro(macro_lines, msg, periph) | ||
h_lines = generator.insert_lines(h_lines, gen_send_macro_start, gen_send_macro_stop, macro_lines) | ||
|
||
# Message update periods | ||
|
@@ -263,7 +312,7 @@ def configure_node(node_config, node_paths): | |
|
||
# Rx callbacks | ||
rx_callbacks = [rx_config for rx_config in node_config['rx'] if ("callback" in rx_config and rx_config["callback"])] | ||
if is_junc: rx_callbacks += [rx_config for rx_config in junc_config['rx'] if ("callback" in rx_config and rx_config["callback"])] | ||
# if is_junc: rx_callbacks += [rx_config for rx_config in junc_config['rx'] if ("callback" in rx_config and rx_config["callback"])] | ||
extern_callback_lines = [f"extern void {rx_config['msg_name']}_CALLBACK(CanMsgTypeDef_t* msg_header_a);\n" for rx_config in rx_callbacks if ("arg_type" in rx_config and rx_config["arg_type"]=="header")] | ||
extern_callback_lines += [f"extern void {rx_config['msg_name']}_CALLBACK(CanParsedData_t* msg_data_a);\n" for rx_config in rx_callbacks if (("fault" not in rx_config) and (("arg_type" not in rx_config) or rx_config["arg_type"]=="msg_data"))] | ||
extern_callback_lines += "extern void handleCallbacks(uint16_t id, bool latched);\n" | ||
|
@@ -274,7 +323,7 @@ def configure_node(node_config, node_paths): | |
|
||
|
||
rx_irq_names = [rx_config['msg_name'] for rx_config in node_config['rx'] if ("irq" in rx_config and rx_config["irq"])] | ||
if is_junc: rx_irq_names += [rx_config['msg_name'] for rx_config in junc_config['rx'] if ("irq" in rx_config and rx_config["irq"])] | ||
# if is_junc: rx_irq_names += [rx_config['msg_name'] for rx_config in junc_config['rx'] if ("irq" in rx_config and rx_config["irq"])] | ||
extern_callback_lines = [f"extern void {msg_name}_IRQ(CanParsedData_t* msg_data_a);\n" for msg_name in rx_irq_names] | ||
h_lines = generator.insert_lines(h_lines, gen_irq_extern_start, gen_irq_extern_stop, extern_callback_lines) | ||
|
||
|
@@ -291,27 +340,29 @@ def configure_node(node_config, node_paths): | |
|
||
# Rx switch case | ||
case_lines = [] | ||
periph = DEFAULT_PERIPHERAL | ||
periph = DEFAULT_PERIPHERAL_NODE | ||
if 'can_peripheral' in node_config: periph = node_config['can_peripheral'] | ||
|
||
ind = "" | ||
if is_junc: | ||
ind = " " | ||
# add if statement for distinguishing between peripherals | ||
case_lines.append(f" if (msg_header.Bus == {periph})\n") | ||
case_lines.append(f" {{\n") | ||
# if is_junc: | ||
# ind = " " | ||
# # add if statement for distinguishing between peripherals | ||
# case_lines.append(f" if (msg_header.Bus == {periph})\n") | ||
# case_lines.append(f" {{\n") | ||
gen_switch_case(case_lines, node_specific_rx_msg_defs, rx_callbacks, ind=ind) | ||
if is_junc: | ||
periph = junc_config['can_peripheral'] | ||
case_lines.append(" }\n") | ||
case_lines.append(f" else if (msg_header.Bus == {periph})\n") | ||
case_lines.append(" {\n") | ||
gen_switch_case(case_lines, junc_rx_msg_defs, rx_callbacks, ind=ind) | ||
case_lines.append(" }\n") | ||
# if is_junc: | ||
# periph = junc_config['can_peripheral'] | ||
# case_lines.append(" }\n") | ||
# case_lines.append(f" else if (msg_header.Bus == {periph})\n") | ||
# case_lines.append(" {\n") | ||
# gen_switch_case(case_lines, junc_rx_msg_defs, rx_callbacks, ind=ind) | ||
# case_lines.append(" }\n") | ||
c_lines = generator.insert_lines(c_lines, gen_switch_case_start, gen_switch_case_stop, case_lines) | ||
|
||
# Stale checking | ||
stale_lines = [] | ||
for msg in receiving_msg_defs: | ||
if 'can_peripheral_override' in msg: periph = msg['can_peripheral_override'] | ||
if msg['msg_period'] > 0: | ||
stale_lines.append(f" CHECK_STALE(can_data.{msg['msg_name']}.stale,\n") | ||
stale_lines.append(f" sched.os_ticks, can_data.{msg['msg_name']}.last_rx,\n") | ||
|
@@ -321,10 +372,10 @@ def configure_node(node_config, node_paths): | |
# Hardware filtering | ||
filter_lines = [] | ||
if not ("accept_all_messages" in node_config and node_config["accept_all_messages"]): | ||
periph = DEFAULT_PERIPHERAL | ||
periph = DEFAULT_PERIPHERAL_NODE | ||
if "can_peripheral" in node_config: periph = node_config['can_peripheral'] | ||
gen_filter_lines(filter_lines, node_specific_rx_msg_defs, periph) | ||
if is_junc: gen_filter_lines(filter_lines, junc_rx_msg_defs, junc_config['can_peripheral']) | ||
# if is_junc: gen_filter_lines(filter_lines, junc_rx_msg_defs, junc_config['can_peripheral']) | ||
|
||
c_lines = generator.insert_lines(c_lines, gen_filter_start, gen_filter_stop, filter_lines) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small thing, if you are talking about lines 85-87, those are dictionaries and not tuples.