-
Notifications
You must be signed in to change notification settings - Fork 4
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
Timestamped Frames #10
base: master
Are you sure you want to change the base?
Changes from 13 commits
4629e6f
20f88aa
abdaaa7
235d4c3
cda72b5
e40b75c
cf24235
0069c3e
a11eb1c
fb6cf2e
c60f943
71c463e
bff9a8b
3989e45
d9404a3
1377b3e
acc0883
616a1f5
dd9d9db
cbd2495
0863f19
ac7ab57
0cfa2d9
827eeef
04a93f5
55267dc
18be57b
f7dc936
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ def single_handler(frame, name_prepends, num_tabs, fw): | |
tot_name = coord(name_prepends, frame.name, prefix=False) | ||
fw( | ||
'\t' * num_tabs + 'case CANlib_{}_key:'.format(tot_name) + '\n' + | ||
'\t' * (num_tabs + 1) + 'CANlib_Handle_{}(&frame);\n'.format(tot_name) + | ||
'\t' * (num_tabs + 1) + 'CANlib_Handle_{}(ts_frame);\n'.format(tot_name) + | ||
'\t' * (num_tabs + 1) + 'break;\n' | ||
) | ||
|
||
|
@@ -20,7 +20,7 @@ def multplxd_handler(frame, name_prepends, num_tabs, fw): | |
fw('\t' * num_tabs + 'case {}_key:\n'.format(coord(name_prepends, frame.name))) | ||
key_size = ceil(frame.slice.length / 8) * 8 | ||
key_name = '_'.join([name_prepends,frame.name, 'key']) | ||
fw('\t' * (num_tabs + 1) + 'to_bitstring(frame.data, &bitstring);' '\n') | ||
fw('\t' * (num_tabs + 1) + 'to_bitstring(frame->data, &bitstring);' '\n') | ||
fw( | ||
'\t' * (num_tabs + 1) + 'uint{}_t {} = EXTRACT(bitstring, {}, {});\n'.format(key_size, key_name, frame.slice.start, frame.slice.length) + '\t' * (num_tabs + 1) + 'switch(' + key_name + ') {' '\n' | ||
) | ||
|
@@ -78,28 +78,68 @@ def write(can, computers, output_path=computer_c_dir_path): | |
|
||
fw('\t}\n}\n\n') | ||
|
||
fw( | ||
'CAN_Raw_Bus_T CANlib_GetConceptualBus(CAN_Raw_Bus_T bus) {\n' | ||
'\tswitch (bus) {\n' | ||
) | ||
|
||
for busnm, rawnm in computer.participation['name']['can'].mapping.items(): | ||
fw('\t\tcase {}:\n'.format(rawnm) + '\t\t\treturn {};\n'.format(busnm)) | ||
|
||
fw('\t\tdefault:\n\t\t\treturn INVALID_BUS;\n') | ||
|
||
fw('\t}\n}\n\n') | ||
|
||
for busnm, bus in computer.participation['name']['can'].subscribe.items(): | ||
fw( | ||
'static void CANlib_update_can_{}(void)'.format(busnm) + '{\n' + | ||
'\tFrame frame;\n' | ||
) | ||
if any(is_multplxd(msg) for msg in bus): | ||
fw('\tuint64_t bitstring;\n') | ||
fw( | ||
'\tCANlib_ReadFrame(&frame, {});\n'.format(busnm) + | ||
'\tswitch(frame.id) {\n' | ||
'static void CANlib_HandleFrame_{}(Timestamped_Frame* ts_frame)'.format(busnm) + '{\n' + | ||
'\tswitch(ts_frame->frame.id) {\n' | ||
) | ||
|
||
for msg in bus: | ||
msg_handler(msg, busnm, fw) | ||
|
||
fw('\t\tdefault:\n\t\t\treturn;\n') | ||
fw('\t}\n') | ||
|
||
fw( | ||
'}\n\n' | ||
) | ||
|
||
for busnm, bus in computer.participation['name']['can'].subscribe.items(): | ||
fw( | ||
'static void CANlib_update_can_{}(void)'.format(busnm) + '{\n' + | ||
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. should be |
||
'\tTimestamped_Frame ts_frame;\n' | ||
) | ||
if any(is_multplxd(msg) for msg in bus): | ||
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. Why is this here? Unused |
||
fw('\tuint64_t bitstring;\n') | ||
|
||
fw( | ||
'\tif (CANlib_ReadFrame(&(ts_frame.frame), {})) {{\n'.format(busnm) + | ||
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. You don't set the timestamp so it's initialized with junk. Here you can set it with get tick |
||
'\t\tCANlib_HandleFrame_{}(&ts_frame);\n'.format(busnm) + | ||
'\t}\n' + | ||
'}\n\n' | ||
) | ||
|
||
fw('void CANlib_update_can() {\n') | ||
for busnm in computer.participation['name']['can'].subscribe.keys(): | ||
fw('\tCANlib_update_can_{}();\n'.format(busnm)) | ||
fw('}\n\n') | ||
|
||
fw('void CANlib_HandleFrame(CAN_Raw_Bus_T raw_bus) {\n') | ||
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. How is this different than the update? Where can a user of interrupts provide a timestamped frame and have it accounted for? |
||
if len(computer.participation['name']['can'].subscribe.keys()) > 0: # check if computer receives messages | ||
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. It's not this part's job to make a function unusable if nobody subscribes to it. It should be generated and just not be exposed to the user. |
||
fw('\tTimestamped_Frame ts_frame;\n') | ||
fw('\tswitch(raw_bus) {\n') | ||
for bus in computer.participation['name']['can'].subscribe.keys(): | ||
fw( | ||
'\t\tcase {}:\n'.format(computer.participation['name']['can'].mapping[bus]) + | ||
'\t\t\tif (CANlib_ReadFrame(&(ts_frame.frame), {})) {{\n'.format(bus) + | ||
'\t\t\t\tCANlib_HandleFrame_{}(&ts_frame);\n'.format(bus) + | ||
'\t\t\t}\n' + | ||
'\t\t\tbreak;\n' | ||
) | ||
fw('\t\tdefault:\n\t\t\tbreak;\n') | ||
fw('\t}\n') | ||
else: # prevent unused warning | ||
fw('\tUNUSED(raw_bus);\n') | ||
fw('}\n') |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,19 +25,22 @@ def define_pub_frame(frame, name_prepends, busnm, fw): | |
def define_sub_frame(frame, name_prepends, fw): | ||
tot_name = coord(name_prepends, frame.name, | ||
prefix=False) | ||
fw('void CANlib_Handle_{}(Frame *frame)'.format( | ||
tot_name, tot_name) + ' {\n' + '\tCANlib_Unpack_{}(frame, &CANlib_{}_Input);\n'.format(tot_name, tot_name) + '}\n\n') | ||
fw('void CANlib_Handle_{}(Timestamped_Frame *ts_frame) {{\n'.format(tot_name, tot_name)) | ||
fw('\tCANlib_{}_Input.timestamp = HAL_GetTick();\n'.format(tot_name)) | ||
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. This should be coming in as a function parameter. Devices could be buffering frames on their own and have the timestamp be in the past. |
||
fw('\tCANlib_Unpack_{}(&(ts_frame->frame), &(CANlib_{}_Input.msg));\n'.format(tot_name,tot_name)) | ||
fw('}\n\n') | ||
|
||
|
||
def define_struct(frame, name_prepends, fw): | ||
tot_name = coord(name_prepends, frame.name) | ||
fw('{}_T {}_Input;\n'.format( | ||
fw('{}_Timestamped_T {}_Input;\n'.format( | ||
tot_name, tot_name)) | ||
|
||
|
||
def write(can, output_path=send_recieve_path): | ||
with open(output_path, 'w') as f: | ||
fw = f.write | ||
fw('#include <stm32f4xx_hal.h>\n') | ||
fw('#include "pack_unpack.h"\n\n') | ||
|
||
for bus in can.bus: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,10 @@ | |
|
||
#include "drivers/inc/stm32f4xx.h" | ||
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. Oh this really shouldn't be here. 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. The intent is that each driver header includes driver.h at the bottom, after defining the native types. Then concepts like |
||
#include "bus.h" | ||
#include <stdbool.h> | ||
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. There is a driver specific error type, we shouldn't need bool. |
||
|
||
CANlib_Transmit_Error_T CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus); | ||
void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus); | ||
bool CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus); | ||
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. Maybe introduce |
||
CAN_Raw_Bus_T CANlib_GetRawBus(CANlib_Bus_T bus); | ||
|
||
#endif // __DRIVER_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,13 @@ | |
#include "bus.h" | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
|
||
typedef uint32_t Time_T; // in ms | ||
typedef HAL_StatusTypeDef CANlib_Transmit_Error_T; | ||
typedef HAL_StatusTypeDef CANlib_Init_Error_T; | ||
|
||
CANlib_Transmit_Error_T CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus); | ||
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. This should not be here and driver.h should be included at the bottom. |
||
void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus); | ||
bool CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus); | ||
|
||
#endif // __STM32F4XX_CAN_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
#include "driver.h" | ||
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. This should include the stm specific driver/inc/xxx.h |
||
#include <stdint.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
|
||
extern CAN_HandleTypeDef hcan1; | ||
extern CAN_HandleTypeDef hcan2; | ||
|
@@ -38,7 +39,7 @@ HAL_StatusTypeDef CANlib_TransmitFrame(Frame *frame, CANlib_Bus_T bus) { | |
return HAL_CAN_AddTxMessage(hcan, &pHeader, frame->data, &pTxMailbox); | ||
} | ||
|
||
void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { | ||
bool CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { | ||
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. If it returns anything it should probably be HAL_StatusTypeDef. 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. The thing is |
||
CAN_Raw_Bus_T raw_bus = CANlib_GetRawBus(bus); | ||
CAN_HandleTypeDef *hcan; | ||
switch(raw_bus) { | ||
|
@@ -52,22 +53,23 @@ void CANlib_ReadFrame(Frame *frame, CANlib_Bus_T bus) { | |
hcan = &hcan3; | ||
break; | ||
default: | ||
return; | ||
return false; | ||
} | ||
|
||
uint8_t data[8] = {}; | ||
CAN_RxHeaderTypeDef pHeader; | ||
for (int fifo = 0; fifo < 2; fifo++) { // There are 2 receive FIFOs | ||
if (HAL_CAN_GetRxFifoFillLevel(hcan, fifo) > 0) { | ||
HAL_CAN_GetRxMessage(hcan, fifo, &pHeader, data); | ||
if (HAL_CAN_GetRxMessage(hcan, fifo, &pHeader, data) != HAL_OK) { | ||
continue; | ||
} | ||
frame->id = pHeader.IDE == CAN_ID_STD ? pHeader.StdId : pHeader.ExtId; | ||
frame->dlc = pHeader.DLC; | ||
|
||
memcpy(frame->data, data, sizeof(data)); | ||
frame->extended = pHeader.IDE == CAN_ID_EXT; | ||
return; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
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.
What is this used for?