Skip to content
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

STM32 Can implementation; DemoCan Example #110

Merged
merged 25 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9c7736b
Remove pywin32 from requirements.txt
samparent97 Apr 16, 2024
e458ae9
Fix mcal target includes
samparent97 Apr 16, 2024
2525418
Remove -e from echo command into makefile
samparent97 Apr 16, 2024
0fe707e
WIP: building democan with stm32
samparent97 Apr 16, 2024
7acb363
Add foo and bar demo can projects
samparent97 Apr 17, 2024
220034d
Remove print
samparent97 Apr 17, 2024
1dc453c
Add postbuild to raspi platform
samparent97 Apr 18, 2024
473483a
Revert postbuild change
samparent97 Apr 18, 2024
c1e8723
Remove all uneeded info for demo can dbc
samparent97 Apr 20, 2024
55cbc74
Add Dashboard enable to ioc
samparent97 Apr 20, 2024
cd62aeb
Move tick blocking to project specific binding
samparent97 Apr 20, 2024
9aa67e4
Add tick timestamp to rx messages
samparent97 Apr 20, 2024
4be595f
Add temp debug msg
samparent97 Apr 20, 2024
85e9a0e
Remove os lib from cmake for can demo projects
samparent97 Apr 20, 2024
858211d
Remove line added by clangd
samparent97 Apr 20, 2024
9bf1c7c
Fix missing start var
samparent97 Apr 20, 2024
75d8f8c
Update main.cc
samparent97 Apr 20, 2024
dcbfa55
Remove debug msg
samparent97 Apr 20, 2024
e9aac80
Remove iostream import
samparent97 Apr 20, 2024
e2f8d78
Remove os remenants
samparent97 Apr 20, 2024
cd1f433
Fix all clang format checks
samparent97 Apr 20, 2024
d2d870e
build: :construction_worker: Improve the CubeMX autogeneration code. …
BlakeFreer Apr 21, 2024
3184622
Various fixes
samparent97 Apr 21, 2024
3665ed2
Fix postbuild
samparent97 Apr 21, 2024
b27b381
Move the delay in projects
samparent97 Apr 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions firmware/cmake/build_cubemx.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,41 @@
# This file is included in the cubemx/CMakeLists.txt file of all projects'
# platforms that use the stm32f767 mcal

message(STATUS "Building CubeMX objects with the auto-generated Makefile")

message(STATUS "Generating from CubeMX and adding custom targets.")
execute_process(
# Calls generate_cubemx.mk in the context of this file, which is adjacent to
# the board_config.ioc file.
COMMAND ${CMAKE_MAKE_PROGRAM} "--file" "${CMAKE_SOURCE_DIR}/cmake/generate_cubemx.mk"
COMMAND ${CMAKE_MAKE_PROGRAM} "--file=${CMAKE_SOURCE_DIR}/cmake/generate_cubemx.mk"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE status
)

if(status AND NOT status EQUAL 0)
message(FATAL_ERROR
"Failed to generate from CubeMX. You should manually 'Generate Code' \
before building."
)
endif()

set(CUSTOM_MAKEFILE "${CMAKE_CURRENT_SOURCE_DIR}/CustomMakefile.mk")

message(STATUS "Building CubeMX objects with the auto-generated Makefile")
execute_process(
# Build all cubemx sources to their objects. This target is added to the
# cubemx Makefile by generate_cubemx.mk.
COMMAND ${CMAKE_MAKE_PROGRAM} "objects"
COMMAND ${CMAKE_MAKE_PROGRAM} "objects" "--file=${CUSTOM_MAKEFILE}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

# Get variables from makefile

function(extract_make_variable output makefile var_name)
# Get a variable from a Makefile
# This requires the Makefile to have the following target & recipe
#
# %.value:
# @echo $($*)
#
# generate_cubemx.mk appends this to the cubemx Makefile
# generate_cubemx.mk appends this target to the cubemx Makefile
# See custom_cubemx_targets.mk
execute_process(
COMMAND ${CMAKE_MAKE_PROGRAM} "--file=${makefile}" "${var_name}.value" "--no-print-directory"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
Expand All @@ -40,22 +50,22 @@ function(extract_make_variable output makefile var_name)
endfunction()

# Get C_INCLUDES, split string to a list, and remove the -I prefix
extract_make_variable(include_str "${CMAKE_CURRENT_SOURCE_DIR}/Makefile" "C_INCLUDES")
extract_make_variable(include_str "${CUSTOM_MAKEFILE}" "C_INCLUDES")
separate_arguments(C_INCLUDES NATIVE_COMMAND ${include_str})
list(TRANSFORM C_INCLUDES REPLACE "^-I" "")

# Get Global #defines
extract_make_variable(c_def_str "${CMAKE_CURRENT_SOURCE_DIR}/Makefile" "C_DEFS")
extract_make_variable(c_def_str "${CUSTOM_MAKEFILE}" "C_DEFS")
separate_arguments(C_DEFS NATIVE_COMMAND ${c_def_str})
list(TRANSFORM C_DEFS REPLACE "^-D" "")

# Get MCU which includes the CPU name (cortex-m7), floating point unit, etc
extract_make_variable(MCU "${CMAKE_CURRENT_SOURCE_DIR}/Makefile" "MCU")
extract_make_variable(MCU "${CUSTOM_MAKEFILE}" "MCU")
separate_arguments(MCU NATIVE_COMMAND ${MCU})

# Get the linker flags. These are needed since we manually link the objects
# instead of using the cubemx Makefile.
extract_make_variable(ldflags_str "${CMAKE_CURRENT_SOURCE_DIR}/Makefile" "LDFLAGS")
extract_make_variable(ldflags_str "${CUSTOM_MAKEFILE}" "LDFLAGS")
separate_arguments(LDFLAGS NATIVE_COMMAND ${ldflags_str})
# Need to fully specify the link script path
list(TRANSFORM LDFLAGS REPLACE "^-T" "-T${CMAKE_CURRENT_SOURCE_DIR}/")
Expand Down
17 changes: 17 additions & 0 deletions firmware/cmake/custom_cubemx_targets.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

# The following targets are appended to the end of the autogenerated Makefile
# from CubeMX.

# Add a recipe for building the sources to objects without linking them.
# This is used by build_cubemx.cmake
objects: $(OBJECTS)


# Add a recipe for printing any Makefile variable to stdout
# This is used by build_cubemx.cmake.
# Example
# $ make LIBS.value
# -lc -lm -lnosys

%.value:
@echo $($*)
32 changes: 16 additions & 16 deletions firmware/cmake/generate_cubemx.mk
samparent97 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ else
endif

IOC_FILE = board_config.ioc
AUTOGEN_CUBEMX = ON

CUBEMX_GEN_SCRIPT = cubemx_script.txt

# Get the directory of this file since it is called from other locations
BUILD_SYS_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
CUSTOM_TARGETS_FILE = $(BUILD_SYS_DIR)/custom_cubemx_targets.mk

# Copy the CubeMX makefile and custom targets into a new makefile
CustomMakefile.mk: Makefile $(CUSTOM_TARGETS_FILE)
@echo "Creating $@ from [$^]"
cat Makefile > $@
cat $(CUSTOM_TARGETS_FILE) >> $@

# This recipe will execute whenever IOC_FILE has a newer timestamp than the
# Makefile, i.e. whenever IOC_FILE has been updated but new code has not been
# generated.

Makefile: $(IOC_FILE)
@echo "Autogenerating from CubeMX. If you don't want to do this, you must manually 'Generate Code' before building."
# Create an file containing commands to generate the cubemx code.
echo "config load \"$(IOC_FILE)\"" > $(CUBEMX_GEN_SCRIPT)
echo "project generate ./" >> $(CUBEMX_GEN_SCRIPT)
echo "exit" >> $(CUBEMX_GEN_SCRIPT)
@printf 'config load "%s"\n' $(IOC_FILE) > $(CUBEMX_GEN_SCRIPT)
@printf 'project generate ./\n' >> $(CUBEMX_GEN_SCRIPT)
@printf 'exit\n' >> $(CUBEMX_GEN_SCRIPT)

# Run the cubemx program to generate code.
java -jar "$(CUBEMX_PATH)" -q "$(CUBEMX_GEN_SCRIPT)"

rm $(CUBEMX_GEN_SCRIPT)

# Add a recipe for building the sources to objects without linking them.
# This is used by build_cubemx.cmake
echo "" >> Makefile
echo 'objects: $$(OBJECTS)' >> Makefile

# Add a recipe for printing any Makefile variable to stdout
# This is used by build_cubemx.cmake
echo "" >> Makefile
echo "%.value:" >> Makefile
echo -e "\t@echo \$$(\$$*)" >> Makefile
rm $(CUBEMX_GEN_SCRIPT)
105 changes: 42 additions & 63 deletions firmware/dbcs/DEMO_CAN.dbc
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,52 @@ VERSION ""


NS_ :
NS_DESC_
CM_
BA_DEF_
BA_
VAL_
CAT_DEF_
CAT_
FILTER
BA_DEF_DEF_
EV_DATA_
ENVVAR_DATA_
SGTYPE_
SGTYPE_VAL_
BA_DEF_SGTYPE_
BA_SGTYPE_
SIG_TYPE_REF_
VAL_TABLE_
SIG_GROUP_
SIG_VALTYPE_
SIGTYPE_VALTYPE_
BO_TX_BU_
BA_DEF_REL_
BA_REL_
BA_DEF_DEF_REL_
BU_SG_REL_
BU_EV_REL_
BU_BO_REL_
SG_MUL_VAL_
NS_DESC_
CM_
BA_DEF_
BA_
VAL_
CAT_DEF_
CAT_
FILTER
BA_DEF_DEF_
EV_DATA_
ENVVAR_DATA_
SGTYPE_
SGTYPE_VAL_
BA_DEF_SGTYPE_
BA_SGTYPE_
SIG_TYPE_REF_
VAL_TABLE_
SIG_GROUP_
SIG_VALTYPE_
SIGTYPE_VALTYPE_
BO_TX_BU_
BA_DEF_REL_
BA_REL_
BA_DEF_DEF_REL_
BU_SG_REL_
BU_EV_REL_
BU_BO_REL_
SG_MUL_VAL_

BS_:

BU_: FOO BAR


BO_ 940 TempSensors: 8 FOO
SG_ Sensor1 : 54|10@1- (0.2,0) [0|0] "degC" BAR
SG_ Sensor2 : 44|10@1- (0.2,0) [0|0] "degC" BAR
SG_ Sensor3 : 34|10@1- (0.2,0) [0|0] "degC" BAR
SG_ Sensor4 : 24|10@1- (0.2,0) [0|0] "degC" BAR
SG_ Sensor5 : 14|10@1- (0.2,0) [0|0] "degC" BAR
SG_ Sensor6 : 4|10@1- (0.2,0) [0|0] "degC" BAR

BO_ 581 VehicleInfo: 8 BAR
SG_ WheelSpeed : 26|32@0+ (0.00390625,0) [0|250.996] "km/h" FOO
SG_ RequestedSpeed : 3|24@0+ (0.00390625,0) [0|250.996] "km/h" FOO


CM_ BO_ 940 "Temperature Sensor Values";
CM_ BO_ 581 "Pack Contactor States";
CM_ SG_ 581 WheelSpeed "Wheel-Based Vehicle Speed: Speed of the vehicle as calculated from wheel or tailshaft speed.";
BA_DEF_ SG_ "SPN" INT 0 524287;
BA_DEF_ BO_ "VFrameFormat" ENUM "StandardCAN","ExtendedCAN","reserved","J1939PG";
BA_DEF_ "DatabaseVersion" STRING ;
BA_DEF_ "BusType" STRING ;
BA_DEF_ "ProtocolType" STRING ;
BA_DEF_ "DatabaseCompiler" STRING ;
BA_DEF_DEF_ "SPN" 0;
BA_DEF_DEF_ "VFrameFormat" "J1939PG";
BA_DEF_DEF_ "DatabaseVersion" "DEMO PLUS";
BA_DEF_DEF_ "BusType" "";
BA_DEF_DEF_ "ProtocolType" "";
BA_DEF_DEF_ "DatabaseCompiler" "";
BA_ "ProtocolType" "J1939";
BA_ "BusType" "CAN";
BA_ "DatabaseCompiler" "CSS ELECTRONICS (WWW.CSSELECTRONICS.COM)";
BA_ "DatabaseVersion" "1.0.0";
BA_ "VFrameFormat" BO_ 2364540158 3;
BA_ "VFrameFormat" BO_ 2566844926 3;
BA_ "SPN" SG_ 2364540158 EngineSpeed 190;
BA_ "SPN" SG_ 2566844926 WheelBasedVehicleSpeed 84;
SG_ Sensor1 : 54|10@1- (0.2,0) [0|0] "degC" BAR
SG_ Sensor2 : 44|10@1- (0.2,0) [0|0] "degC" BAR
SG_ Sensor3 : 34|10@1- (0.2,0) [0|0] "degC" BAR
SG_ Sensor4 : 24|10@1- (0.2,0) [0|0] "degC" BAR
SG_ Sensor5 : 14|10@1- (0.2,0) [0|0] "degC" BAR
SG_ Sensor6 : 4|10@1- (0.2,0) [0|0] "degC" BAR

BO_ 941 TempSensorsReply: 8 BAR
SG_ Sensor1 : 54|10@1- (0.2,0) [0|0] "degC" FOO
SG_ Sensor2 : 44|10@1- (0.2,0) [0|0] "degC" FOO
SG_ Sensor3 : 34|10@1- (0.2,0) [0|0] "degC" FOO
SG_ Sensor4 : 24|10@1- (0.2,0) [0|0] "degC" FOO
SG_ Sensor5 : 14|10@1- (0.2,0) [0|0] "degC" FOO
SG_ Sensor6 : 4|10@1- (0.2,0) [0|0] "degC" FOO
19 changes: 15 additions & 4 deletions firmware/mcal/raspi/periph/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>

#include <chrono>
#include <cstdint>
#include <cstring>
#include <iostream>
Expand All @@ -26,7 +28,9 @@ namespace mcal::raspi::periph {

class CanBase : public shared::periph::CanBase {
public:
CanBase(std::string can_iface) : iface_(can_iface){};
CanBase(std::string can_iface) : iface_(can_iface) {
program_start_ = std::chrono::steady_clock::now();
};

void Setup() {
// Create a socket
Expand Down Expand Up @@ -94,6 +98,15 @@ class CanBase : public shared::periph::CanBase {
std::mutex queue_mtx_;
std::thread reader_thread_;

std::chrono::steady_clock::time_point program_start_;

inline uint32_t get_tick() {
std::chrono::milliseconds elapsed_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - program_start_);
return static_cast<uint32_t>(elapsed_ms.count());
}

void StartReading() {
struct can_frame frame;

Expand All @@ -109,9 +122,7 @@ class CanBase : public shared::periph::CanBase {
rawMsg.header.id = frame.can_id;
rawMsg.header.data_len = frame.can_dlc;
rawMsg.header.is_extended_frame = frame.can_id & CAN_EFF_FLAG;

std::cout << "Can base frame " << std::hex << rawMsg.header.id
<< " data len " << rawMsg.header.data_len << std::endl;
rawMsg.tick_timestamp = get_tick();

std::copy(frame.data, frame.data + kMaxMsgBytes, rawMsg.data);

Expand Down
2 changes: 1 addition & 1 deletion firmware/mcal/stm32f767/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ add_library(mcal-stm32f767 INTERFACE)
add_subdirectory(periph)
add_subdirectory(os)

target_include_directories(mcal-stm32f767 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/..)
target_include_directories(mcal-stm32f767 INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/../..)

target_link_libraries(mcal-stm32f767 INTERFACE shared)
3 changes: 3 additions & 0 deletions firmware/mcal/stm32f767/os/tick.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

#pragma once

#include <sys/types.h>

#include <cstdint>

#include "cmsis_os2.h"
#include "shared/os/os.h"
#include "shared/os/tick.h"
#include "stm32f7xx_hal.h"

// TODO: Add comments and handle errors more robustly
namespace os {
Expand Down
Loading
Loading