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

add support for multichannel and CAN-FD #139

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
ffc7fb7
editorconfig: apply indent with 2 spaces for yml files
marckleinebudde Jun 4, 2024
a41b0bd
workflows: pr: update actions/checkout to v4
marckleinebudde Jun 4, 2024
819891f
workflows: update CI to compile on various Debian based distributions
marckleinebudde Jun 4, 2024
8c9f715
gitignore: add .ccls-cache
marckleinebudde Jun 17, 2023
c07abdf
cmake: FindDFUSuffix: convert to Unix line endings
marckleinebudde Jun 5, 2024
a897d8f
cmake: fix indention
marckleinebudde Jun 3, 2024
72fe9e0
cmake: remove whitespace after '(' and before ')'
marckleinebudde Jun 5, 2024
56e0622
cmake: fix dependencies for flash targets
marckleinebudde Jun 5, 2024
4ebdff3
cmake: flash-target: use :leave instead of USB reset
marckleinebudde Jun 5, 2024
7a97b30
cmake: cleanup helper functions comments
marckleinebudde Jun 5, 2024
56e4515
cmake: create linker map by default
marckleinebudde Jun 3, 2024
a876208
cmake: rename STM32G0B1XK -> STM32G0B1xx
marckleinebudde Jun 3, 2024
b88e959
cmake: stm32g0b1xx: decrease flash size to 128k
marckleinebudde Jun 3, 2024
b07d812
dfu: add support for STM32G0B1
marckleinebudde Feb 11, 2023
5fb636c
usbd: move cast from strings to uint8_t to USBD_GetString()
marckleinebudde Jun 5, 2024
b62d47d
can: can_check_bittiming_failed(): introduce function to check bittiming
marckleinebudde Jan 28, 2023
f0004f9
usbd_gs_can: USBD_GS_CAN_Config_Request(): add missing initialization…
marckleinebudde Jun 4, 2024
4dad6a1
usbd_gs_can: add USB defines for SMT32G0
ryedwards Nov 16, 2022
4ae1a7c
usbd_gs_can: add support for GS_USB_BREQ_DATA_BITTIMING and GS_USB_BR…
ghent360 Jan 19, 2023
8aac542
m_can: add the base code for M_CAN
ryedwards Nov 16, 2022
bfb830d
m_can: add support for Error State Indicator
marckleinebudde Jan 19, 2023
4114b89
m_can: add RX timestamp support
marckleinebudde Jan 20, 2023
e50c191
stm32g0b1: remove unused USB_GPIO_Port
marckleinebudde Jun 4, 2024
2954867
stm32g0b1: PLL setup: use 40 MHz CAN clock, 64 MHz CPU clock
marckleinebudde Dec 17, 2022
911c1ac
stm32g0b1: add clock config for external oscillator
lichtfeind Jul 20, 2023
411e825
boards: stm32g0b1: move budgetcan and CONVERTDEVICE_xCANFD to board f…
marckleinebudde Jan 24, 2023
a17e381
board: legacy: introduce legacy boards and move CAN_INTERFACE to it
lichtfeind Jan 23, 2023
3ef5ef6
board: legacy: add support for PHY power switching
lichtfeind Jan 23, 2023
dbeffb6
board: legacy: PHY power switching: add silent pin support
lichtfeind Jan 23, 2023
6722baa
board: legacy: legacy_phy_power_set: turn on silent mode during shutdown
marckleinebudde Jan 24, 2023
2b00879
board: legacy: add GPIO setup
lichtfeind Jan 23, 2023
d08e6a3
termination: pass can_data_t instead of only the channel number
marckleinebudde Jan 24, 2023
7fffb6e
board: legacy: add support for termination_set
lichtfeind Jan 23, 2023
e240d81
CMakeLists.txt: switch on STM32G0B1
marckleinebudde Dec 15, 2022
d238c03
re-work the led config
ghent360 Feb 13, 2023
aef7489
boards: move LED config from config.h -> board files
marckleinebudde Jun 9, 2023
ff06b71
cmake: sort TGTxxx_LIST alphabetically
marckleinebudde Jun 4, 2024
fd7be8a
config: sort STM32F042 devices alphabetically
marckleinebudde Jun 5, 2024
bb4cf89
config: sort STM32F072 devices alphabetically
marckleinebudde Jun 5, 2024
a7e1116
config: sort STM32F407 devices alphabetically
marckleinebudde Jun 5, 2024
3765fa4
config: sort STM32G0B1 devices alphabetically
marckleinebudde Jun 5, 2024
c9ddf81
boards: add NUCLEO-G0B1RE
marckleinebudde Dec 16, 2022
6c53ee4
boards: add CandleLightFD
lichtfeind Jun 20, 2023
267022a
boards: add intive GmbH 2C2L-USB
intive-hubert-denkmair Apr 26, 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
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[*.yml]
indent_size = 2
indent_style = space

[lib/**]
indent_size = 2
indent_style = space
53 changes: 53 additions & 0 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: compile

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
release:
- "ubuntu:20.04"
- "ubuntu:22.04"
- "ubuntu:24.04"
- "ubuntu:rolling"
- "debian:oldstable-slim"
- "debian:stable-slim"
- "debian:testing-slim"
- "debian:unstable-slim"

steps:
- uses: actions/checkout@v4

- name: Prepare ${{ matrix.release }} container
run: |
podman version
podman run --name stable -di --userns=keep-id:uid=1000,gid=1000 -v "$PWD":/home -w /home ${{ matrix.release }} bash
podman exec -i stable uname -a
podman exec -i stable id
podman exec -i -u root stable apt update
podman exec -e DEBIAN_FRONTEND='noninteractive' -i -u root stable apt install -o APT::Install-Suggests=false -qy \
clang \
cmake \
gcc-arm-none-eabi \
git \
make

- name: Configure & Build with arm-none-eabi-gcc
env:
toolchain: arm-none-eabi-gcc
run: |
podman exec -i stable cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE=cmake/${toolchain}.cmake -B build-${toolchain}
podman exec -i stable cmake --build build-${toolchain}

- name: Show logs
if: ${{ failure() }}
run: |
for log in build-*/CMakeFiles/{CMakeOutput.log,CMakeConfigureLog.yaml}; do \
if [ -e ${log} ]; then \
echo "---------------- ${log} ----------------"; \
cat ${log}; \
fi; \
done
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
name: Check C Styling
steps:
- name: Checkout this commit
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: uncrustify check
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.ccls-cache/
/.idea/
/.vscode/
/Debug/
Expand Down
70 changes: 49 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ add_link_options(

add_subdirectory(libs/STM32_HAL)
add_subdirectory(libs/STM32_USB_Device_Library)
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake )
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)


# Add a custom target that produces version.h, plus
Expand All @@ -65,6 +65,7 @@ set(
include/usbd_gs_can.h src/usbd_gs_can.c
src/usbd_conf.c

include/board.h
include/can.h
include/can_common.h src/can_common.c
include/device.h
Expand Down Expand Up @@ -110,19 +111,22 @@ find_package(DFUSuffix)

function(dfu_flash target)
if (DFU_SUFFIX_EXECUTABLE)
add_custom_command( TARGET ${target}
add_custom_command(TARGET ${target}
DEPENDS ${target}
BYPRODUCTS ${target}.dfu
COMMAND ${CMAKE_OBJCOPY} -O binary ${target} ${target}.dfu
COMMAND ${DFU_SUFFIX_EXECUTABLE} --add ${target}.dfu --vid 1d50 --pid 606f 1>/dev/null
COMMENT "create and sign dfu bin file: ${TGTNAME}_fw"
)

add_custom_target( flash-${target}
dfu-util -a 0 -R -s 0x08000000 -D ${target}.dfu
add_custom_target(flash-${target}
dfu-util -a 0 -s 0x08000000:leave -D ${target}.dfu
DEPENDS ${target}.dfu
)
else()
add_custom_target( flash-${target}
dfu-util -d 1d50:606f -a 0 -R -s 0x08000000 -D ${target}.bin
add_custom_target(flash-${target}
dfu-util -d 1d50:606f -a 0 -s 0x08000000:leave -D ${target}.bin
DEPENDS ${target}.bin
)
endif()
endfunction()
Expand All @@ -140,9 +144,9 @@ endfunction()
# at configure time.

function(populate_ldscript)
set(prefix LDV)
set(options)
set(oneValueArgs
set(prefix LDV)
set(options)
set(oneValueArgs
CPU_FAMILY
FLASH_START
FLASH_SIZE
Expand Down Expand Up @@ -192,23 +196,23 @@ populate_ldscript(CPU_FAMILY STM32F407XE
HEAP_SIZE 1k
)

populate_ldscript(CPU_FAMILY STM32G0B1XK
populate_ldscript(CPU_FAMILY STM32G0B1xx
FLASH_START 0x08000000
FLASH_SIZE 512k
FLASH_SIZE 128k
RAM_START 0x20000000
RAM_SIZE 144k
STACK_SIZE 2k
HEAP_SIZE 1k
)

######### commands for adding each target have a lot in common: make helper func.
# Split into two categories, F042-based and F072-based.
# one helper func per STM32 CPU family

function(add_target_common TGTNAME CPU_FAMILY)
add_executable(${TGTNAME}_fw ${SOURCE_FILES})
add_dependencies(${TGTNAME}_fw version_h)
target_include_directories(${TGTNAME}_fw PRIVATE include/ ${CMAKE_CURRENT_BINARY_DIR})
target_link_options(${TGTNAME}_fw PRIVATE -T ${CPU_FAMILY}_processed.ld)
target_link_options(${TGTNAME}_fw PRIVATE -T ${CPU_FAMILY}_processed.ld LINKER:-Map=${TGTNAME}_fw.map)
make_bin_file(${TGTNAME}_fw)
dfu_flash(${TGTNAME}_fw)
show_object_size(${TGTNAME}_fw)
Expand All @@ -218,6 +222,7 @@ function(add_f042_target TGTNAME)
add_target_common(${TGTNAME} STM32F042X6)
target_compile_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F0})
target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD_${TGTNAME} STM32F0)
target_sources(${TGTNAME}_fw PRIVATE "src/boards/legacy.c")
target_sources(${TGTNAME}_fw PRIVATE "src/can/bxcan.c")
target_sources(${TGTNAME}_fw PRIVATE "src/device/device_f0.c")
target_link_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F0})
Expand All @@ -228,38 +233,61 @@ function(add_f072_target TGTNAME)
add_target_common(${TGTNAME} STM32F072XB)
target_compile_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F0})
target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD_${TGTNAME} STM32F0)
target_sources(${TGTNAME}_fw PRIVATE "src/boards/legacy.c")
target_sources(${TGTNAME}_fw PRIVATE "src/can/bxcan.c")
target_sources(${TGTNAME}_fw PRIVATE "src/device/device_f0.c")
target_link_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F0})
target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32F072xB STM32_USB_Device_Library_STM32F072xB)

endfunction()

function(add_f407_target TGTNAME)
add_target_common(${TGTNAME} STM32F407XE)
target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD_${TGTNAME} STM32F4)
target_compile_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F4})
target_sources(${TGTNAME}_fw PRIVATE "src/boards/legacy.c")
target_sources(${TGTNAME}_fw PRIVATE "src/can/bxcan.c")
target_sources(${TGTNAME}_fw PRIVATE "src/device/device_f4.c")
target_link_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_F4})
target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32F407xE STM32_USB_Device_Library_STM32F407xE)
endfunction()

function(add_g0b1_target TGTNAME)
add_target_common(${TGTNAME} STM32G0B1XK)
add_target_common(${TGTNAME} STM32G0B1xx)
target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD_${TGTNAME} STM32G0)
target_compile_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_G0})
target_sources(${TGTNAME}_fw PRIVATE "src/boards/g0b1-${TGTNAME}.c")
target_sources(${TGTNAME}_fw PRIVATE "src/can/m_can.c")
target_sources(${TGTNAME}_fw PRIVATE "src/device/device_g0.c")
target_link_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_G0})
target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32G0B1xK STM32_USB_Device_Library_STM32G0B1xK)
target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32G0B1xx STM32_USB_Device_Library_STM32G0B1xx)
endfunction()

########## generate list of targets.
# the "_fw" part is appended automatically
set(TGTF042_LIST "cantact" "canalyze" "canable" "usb2can" "cannette")
set(TGTF072_LIST "candleLight" "CANable_MKS" "CONVERTDEVICE_xCAN" "DSD_TECH_SH_C30A" "FYSETC_UCAN")
set(TGTF407_LIST "STM32F4_DevBoard")
set(TGTG0B1_LIST "budgetcan" "CONVERTDEVICE_xCANFD")
set(TGTF042_LIST
"canable"
"canalyze"
"cannette"
"cantact"
"usb2can"
)
set(TGTF072_LIST
"CANable_MKS"
"CONVERTDEVICE_xCAN"
"DSD_TECH_SH_C30A"
"FYSETC_UCAN"
"candleLight"
)
set(TGTF407_LIST
"STM32F4_DevBoard"
)
set(TGTG0B1_LIST
"2C2L_USB"
"CONVERTDEVICE_xCANFD"
"budgetcan"
"candleLightFD"
"nucleo_g0b1re"
)

foreach (TGTNAME IN LISTS TGTF042_LIST)
option(BUILD_${TGTNAME} "Build firmware for \"${TGTNAME}\" (default=yes)" ON)
Expand All @@ -283,7 +311,7 @@ foreach (TGTNAME IN LISTS TGTF407_LIST)
endforeach()

foreach (TGTNAME IN LISTS TGTG0B1_LIST)
option(BUILD_${TGTNAME} "Build firmware for \"${TGTNAME}\" (default=yes)" OFF)
option(BUILD_${TGTNAME} "Build firmware for \"${TGTNAME}\" (default=yes)" ON)
if (BUILD_${TGTNAME})
add_g0b1_target(${TGTNAME})
endif()
Expand Down
17 changes: 8 additions & 9 deletions cmake/FindDFUSuffix.cmake
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# find and set DFU_SUFFIX_EXECUTABLE

find_program ( DFU_SUFFIX_EXECUTABLE
NAMES dfu-suffix
DOC "dfu-suffix executable"
)

mark_as_advanced ( DFU_SUFFIX_EXECUTABLE )

# find and set DFU_SUFFIX_EXECUTABLE

find_program(DFU_SUFFIX_EXECUTABLE
NAMES dfu-suffix
DOC "dfu-suffix executable"
)

mark_as_advanced(DFU_SUFFIX_EXECUTABLE)
8 changes: 8 additions & 0 deletions cmake/arm-none-eabi-clang.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET arm-none-eabi)

SET (CMAKE_C_COMPILER_WORKS 1)
SET (CMAKE_CXX_COMPILER_WORKS 1)
8 changes: 8 additions & 0 deletions cmake/arm-none-eabi-gcc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_C_COMPILER_TARGET arm-none-eabi)

SET (CMAKE_C_COMPILER_WORKS 1)
SET (CMAKE_CXX_COMPILER_WORKS 1)
55 changes: 55 additions & 0 deletions include/board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

/*
* The MIT License (MIT)
*
* Copyright (c) 2023 Pengutronix,
* Jonas Martin <[email protected]>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#include "usbd_gs_can.h"

struct LEDConfig {
GPIO_TypeDef *port;
uint16_t pin;
bool active_high;
};

struct BoardChannelConfig {
#if defined(STM32G0)
FDCAN_GlobalTypeDef *interface;
#else
CAN_TypeDef *interface;
#endif
struct LEDConfig leds[LED_MAX];
};

struct BoardConfig {
struct BoardChannelConfig channels[NUM_CAN_CHANNEL];
void (*setup)(USBD_GS_CAN_HandleTypeDef *hcan);
void (*phy_power_set)(can_data_t *channel, bool enable);
void (*termination_set)(can_data_t *channel, enum gs_can_termination_state state);
};

extern const struct BoardConfig config;
26 changes: 25 additions & 1 deletion include/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ THE SOFTWARE.
#include <stdint.h>
#include <stdbool.h>

#include "config.h"
#include "gs_usb.h"
#include "hal_include.h"
#include "led.h"
#include "list.h"

typedef struct {
#if defined(STM32G0)
FDCAN_HandleTypeDef channel;
#else
CAN_TypeDef *instance;
#endif
struct list_head list_from_host;
led_data_t leds;
uint32_t reg_esr_old;
Expand All @@ -47,9 +52,28 @@ typedef struct {
} can_data_t;

extern const struct gs_device_bt_const CAN_btconst;
extern const struct gs_device_bt_const_extended CAN_btconst_ext;

#if defined(STM32G0)
void can_init(can_data_t *channel, FDCAN_GlobalTypeDef *instance);
#else
void can_init(can_data_t *channel, CAN_TypeDef *instance);
bool can_set_bittiming(can_data_t *channel, const struct gs_device_bittiming *timing);
#endif
void can_set_bittiming(can_data_t *channel, const struct gs_device_bittiming *timing);

#ifdef CONFIG_CANFD
void can_set_data_bittiming(can_data_t *channel, const struct gs_device_bittiming *timing);
#else
static inline bool can_set_data_bittiming(can_data_t *channel,
const struct gs_device_bittiming *timing)
{
(void)channel;
(void)timing;

return false;
}
#endif

void can_enable(can_data_t *channel, uint32_t mode);
void can_disable(can_data_t *channel);
bool can_is_enabled(can_data_t *channel);
Expand Down
1 change: 1 addition & 0 deletions include/can_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ THE SOFTWARE.
#include "can.h"
#include "usbd_gs_can.h"

bool can_check_bittiming_ok(const struct can_bittiming_const *btc, const struct gs_device_bittiming *timing);
void CAN_SendFrame(USBD_GS_CAN_HandleTypeDef *hcan, can_data_t *channel);
void CAN_ReceiveFrame(USBD_GS_CAN_HandleTypeDef *hcan, can_data_t *channel);
void CAN_HandleError(USBD_GS_CAN_HandleTypeDef *hcan, can_data_t *channel);
Loading
Loading