Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1fe5bf4
m_can: add the base code for M_CAN
ryedwards Nov 16, 2022
30cc7fa
m_can: add support for Error State Indicator
marckleinebudde Jan 19, 2023
79cf063
m_can: add RX timestamp support
marckleinebudde Jan 20, 2023
120ee0a
m_can: add support for PHY power switching
lichtfeind Jan 23, 2023
bfd1e58
board: legacy: add GPIO setup
lichtfeind Jan 23, 2023
f55e746
board: legacy: add support for LED configuration
ghent360 Feb 13, 2023
521e299
stm32g0b1: remove unused USB_GPIO_Port
marckleinebudde Jun 4, 2024
0e4dcb1
stm32g0b1: PLL setup: use 40 MHz CAN clock, 64 MHz CPU clock
marckleinebudde Dec 17, 2022
1c2e320
stm32g0b1: add clock config for external oscillator
lichtfeind Jul 20, 2023
a83906d
boards: stm32g0b1: move budgetcan and CONVERTDEVICE_xCANFD to board f…
marckleinebudde Jan 24, 2023
4486f27
CMakeLists: switch on STM32G0B1
marckleinebudde Dec 15, 2022
f0fc855
boards: add NUCLEO-G0B1RE
marckleinebudde Dec 16, 2022
9bcc941
boards: add CandleLightFD
lichtfeind Jun 20, 2023
349f064
boards: add intive GmbH 2C2L-USB
intive-hubert-denkmair Apr 26, 2024
38a9a19
boards: add WeActStudio USB2CANFDV1
romainreignier Oct 9, 2024
a134f2c
boards: legacy: add dual channel variant of STM32F4 Dev Board
dickenhobelix Apr 8, 2025
7b6b751
stm32-hal: import stm32g4xx-hal-driver
pgreenland Jan 14, 2024
33b6e4f
stm32-hal: import stm32g4xx cmsis
pgreenland Jan 14, 2024
02b73e5
stm32-hal: add stm32g4xx support
pgreenland Jan 14, 2024
131ae22
stm32g431: add support
pgreenland Jan 14, 2024
5adc2a5
boards: add Makerbase Canable2-MKS
pgreenland Jan 14, 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
40 changes: 39 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add_compile_options(
set(CPUFLAGS_F0 -mcpu=cortex-m0)
set(CPUFLAGS_F4 -mcpu=cortex-m4)
set(CPUFLAGS_G0 -mcpu=cortex-m0)
set(CPUFLAGS_G4 -mcpu=cortex-m4)

add_link_options(
--specs=nano.specs
Expand Down Expand Up @@ -214,6 +215,15 @@ populate_ldscript(CPU_FAMILY STM32G0B1xx
HEAP_SIZE 1k
)

populate_ldscript(CPU_FAMILY STM32G431xx
FLASH_START 0x08000000
FLASH_SIZE 32k
RAM_START 0x20000000
RAM_SIZE 32k
STACK_SIZE 2k
HEAP_SIZE 1k
)

######### commands for adding each target have a lot in common: make helper func.
# one helper func per STM32 CPU family

Expand Down Expand Up @@ -267,11 +277,24 @@ function(add_g0b1_target TGTNAME)
add_target_common(${TGTNAME} STM32G0B1xx)
target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD_${TGTNAME} STM32G0 CONFIG_M_CAN)
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_STM32G0B1xx STM32_USB_Device_Library_STM32G0B1xx)
endfunction()

function(add_g431_target TGTNAME)
add_target_common(${TGTNAME} STM32G431xx)
target_compile_definitions(${TGTNAME}_fw PRIVATE BOARD_${TGTNAME} STM32G4 CONFIG_M_CAN)
target_compile_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_G4})
target_sources(${TGTNAME}_fw PRIVATE "src/boards/g431-${TGTNAME}.c")
target_sources(${TGTNAME}_fw PRIVATE "src/can/m_can.c")
target_sources(${TGTNAME}_fw PRIVATE "src/device/device_g4.c")
target_link_options(${TGTNAME}_fw BEFORE PRIVATE ${CPUFLAGS_G4})
target_link_libraries(${TGTNAME}_fw PRIVATE STM32_HAL_STM32G431xx STM32_USB_Device_Library_STM32G431xx)
endfunction()

########## generate list of targets.
# the "_fw" part is appended automatically
set(TGTF042_LIST
Expand All @@ -290,10 +313,18 @@ set(TGTF072_LIST
)
set(TGTF407_LIST
"STM32F4_DevBoard"
"STM32F4_DevBoardDualChannel"
)
set(TGTG0B1_LIST
"2C2L_USB"
"CONVERTDEVICE_xCANFD"
"WeActStudio_USB2CANFDV1"
"budgetcan"
"candleLightFD"
"nucleo_g0b1re"
)
set(TGTG431_LIST
"CANable2_MKS"
)

foreach (TGTNAME IN LISTS TGTF042_LIST)
Expand All @@ -318,12 +349,19 @@ 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()
endforeach()

foreach (TGTNAME IN LISTS TGTG431_LIST)
option(BUILD_${TGTNAME} "Build firmware for \"${TGTNAME}\" (default=yes)" ON)
if (BUILD_${TGTNAME})
add_g431_target(${TGTNAME})
endif()
endforeach()

message("*******************")
message("You may now:\n\t-compile all targets ('make')\n\t-compile a single target (e.g. 'make cantact_fw'")
message("\t-flash a device (e.g. 'make flash-cantact_fw'")
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This is firmware for certain STM32F042x/STM32F072xB-based USB-CAN adapters, nota
- ConvertDevice-xCANFD: <https://github.com/ConvertDevice/xCANFD> (STM32G0B1CBT6)
- DSD TECH SH-C30A: <https://www.deshide.com/product-details.html?pid=384242&_t=1671089557> (STM32F072xB)
- FYSETC UCAN: <https://www.fysetc.com/products/fysetc-ucan-board-based-on-stm32f072-usb-to-can-adapter-support-with-canable-candlelight-klipper-firmware> (STM32F072xB)
- WeActStudio USB2CANFDV1: https://github.com/WeActStudio/WeActStudio.USB2CANFDV1 (STM32G0B1CBT6)

Of important note is that the common STM32F103 will NOT work with this firmware because its hardware cannot use both USB and CAN simultaneously.
Beware also the smaller packages in the F042 series which map a USB and CAN_TX signal on the same pin and are therefore unusable !
Expand Down
15 changes: 15 additions & 0 deletions include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,24 @@
#include "config.h"
#include "usbd_gs_can.h"

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

struct board_channel_config {
#if defined(CONFIG_BXCAN)
CAN_TypeDef *interface;
#elif defined(CONFIG_M_CAN)
FDCAN_GlobalTypeDef *interface;
#endif
struct led_config leds[LED_MAX];
};

struct board_config {
struct board_channel_config channel[NUM_CAN_CHANNEL];
void (*setup)(USBD_GS_CAN_HandleTypeDef *hcan);
#ifdef CONFIG_PHY
void (*phy_power_set)(can_data_t *channel, bool enable);
#endif
Expand All @@ -51,6 +61,11 @@ struct board_config {

extern const struct board_config config;

static inline void board_setup(USBD_GS_CAN_HandleTypeDef *hcan)
{
config.setup(hcan);
}

#ifdef CONFIG_PHY
#define SET_PHY_POWER_FN(set_fn) \
.phy_power_set = (set_fn),
Expand Down
2 changes: 2 additions & 0 deletions include/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ THE SOFTWARE.
typedef struct {
#if defined (CONFIG_BXCAN)
CAN_TypeDef *instance;
#elif defined(CONFIG_M_CAN)
FDCAN_HandleTypeDef channel;
#endif
struct list_head list_from_host;
led_data_t leds;
Expand Down
129 changes: 93 additions & 36 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,33 +325,79 @@ THE SOFTWARE.
#define TERM_Mode GPIO_MODE_OUTPUT_PP
#define TERM_Active_High 1

#elif defined(BOARD_STM32F4_DevBoardDualChannel)
#define USBD_PRODUCT_STRING_FS "STM32F4VE Dev Board Dual Channel"
#define USBD_MANUFACTURER_STRING "misc"
#define DFU_INTERFACE_STRING_FS "STM32F4VE firmware upgrade interface"

#define TIM2_CLOCK_SPEED 84000000

#define CAN_INTERFACE CAN1
#define CAN_INTERFACE2 CAN2
#define CAN_CLOCK_SPEED 42000000
#define NUM_CAN_CHANNEL 2

#define CONFIG_PHY 1
#define CONFIG_PHY_SILENT 1
#define CAN_S_Pin GPIO_PIN_10
#define CAN_S_GPIO_Port GPIOA

#define LEDRX_GPIO_Port GPIOA
#define LEDRX_Pin GPIO_PIN_6
#define LEDRX_Mode GPIO_MODE_OUTPUT_OD
#define LEDRX_Active_High 0

#define LEDTX_GPIO_Port GPIOA
#define LEDTX_Pin GPIO_PIN_7
#define LEDTX_Mode GPIO_MODE_OUTPUT_OD
#define LEDTX_Active_High 0

#define USB_GPIO_Port GPIOA
#define USB_Pin_DM GPIO_PIN_11
#define USB_Pin_DP GPIO_PIN_12

#define CONFIG_TERMINATION 1
#define TERM_GPIO_Port GPIOB
#define TERM_Pin GPIO_PIN_3
#define TERM_Mode GPIO_MODE_OUTPUT_PP
#define TERM_Active_High 1

/*************** STM32G0B1 ***************/

#elif defined(BOARD_2C2L_USB)
#define USBD_PRODUCT_STRING_FS "intive 2C2L-USB gs_usb"
#define USBD_MANUFACTURER_STRING "intive"
#define DFU_INTERFACE_STRING_FS "intive 2C2L-USB firmware upgrade interface"

#define HSE_OSC_SPEED 8000000
#define TIM2_CLOCK_SPEED 64000000

#define CAN_CLOCK_SPEED 40000000
#define NUM_CAN_CHANNEL 2
#define CONFIG_CANFD 1

#elif defined(BOARD_CONVERTDEVICE_xCANFD)
#define USBD_PRODUCT_STRING_FS "ConvertDevice xCANFD"
#define USBD_MANUFACTURER_STRING "ConvertDevice"
#define DFU_INTERFACE_STRING_FS "ConvertDevice xCANFD firmware upgrade interface"

#define TIM2_CLOCK_SPEED 64000000

#define CAN_INTERFACE FDCAN1
#define CAN_CLOCK_SPEED 64000000
#define CAN_CLOCK_SPEED 40000000
#define NUM_CAN_CHANNEL 1
#define CONFIG_CANFD 1

#define LEDRX_GPIO_Port GPIOA
#define LEDRX_Pin GPIO_PIN_0
#define LEDRX_Mode GPIO_MODE_OUTPUT_PP
#define LEDRX_Active_High 0
#elif defined(BOARD_WeActStudio_USB2CANFDV1)
#define USBD_PRODUCT_STRING_FS (uint8_t*) "USB2CANFDV1 gs_usb"
#define USBD_MANUFACTURER_STRING (uint8_t*) "WeActStudio"
#define DFU_INTERFACE_STRING_FS (uint8_t*) "USB2CANFDV1 firmware upgrade interface"

#define LEDTX_GPIO_Port GPIOA
#define LEDTX_Pin GPIO_PIN_1
#define LEDTX_Mode GPIO_MODE_OUTPUT_PP
#define LEDTX_Active_High 0
#define HSE_OSC_SPEED 16000000
#define TIM2_CLOCK_SPEED 64000000

#define USB_GPIO_Port GPIOA
#define USB_Pin_DM GPIO_PIN_11
#define USB_Pin_DP GPIO_PIN_12
#define CAN_CLOCK_SPEED 40000000
#define NUM_CAN_CHANNEL 1
#define CONFIG_CANFD 1

#elif defined(BOARD_budgetcan)
#define USBD_PRODUCT_STRING_FS "budgetcan gs_usb"
Expand All @@ -360,36 +406,47 @@ THE SOFTWARE.

#define TIM2_CLOCK_SPEED 64000000

#define CAN_INTERFACE FDCAN1
#define CAN_INTERFACE2 FDCAN2
#define CAN_CLOCK_SPEED 64000000
#define CAN_CLOCK_SPEED 40000000
#define NUM_CAN_CHANNEL 2
#define CONFIG_CANFD 1

#define CONFIG_PHY 1
#define CONFIG_PHY_STANDBY 1
#define nCANSTBY_Port GPIOA
#define nCANSTBY_Pin GPIO_PIN_0 /* control xceiver standby, active low */
#define nCANSTBY_Active_High 0
#define CONFIG_TERMINATION 1

#define LEDRX_GPIO_Port GPIOB
#define LEDRX_Pin GPIO_PIN_4
#define LEDRX_Mode GPIO_MODE_OUTPUT_PP
#define LEDRX_Active_High 1
#elif defined(BOARD_candleLightFD)
#define USBD_PRODUCT_STRING_FS "candleLightFD gs_usb"
#define USBD_MANUFACTURER_STRING "candleLightFD"
#define DFU_INTERFACE_STRING_FS "candleLightFD firmware upgrade interface"

#define LEDTX_GPIO_Port GPIOB
#define LEDTX_Pin GPIO_PIN_3
#define LEDTX_Mode GPIO_MODE_OUTPUT_PP
#define LEDTX_Active_High 1
#define HSE_OSC_SPEED 8000000
#define TIM2_CLOCK_SPEED 64000000

#define USB_GPIO_Port GPIOA
#define USB_Pin_DM GPIO_PIN_11
#define USB_Pin_DP GPIO_PIN_12
#define CAN_CLOCK_SPEED 40000000
#define NUM_CAN_CHANNEL 2
#define CONFIG_CANFD 1

#define TERM_GPIO_Port GPIOA
#define TERM_Pin GPIO_PIN_1
#define TERM_Mode GPIO_MODE_OUTPUT_PP
#define TERM_Active_High 1
#elif defined(BOARD_nucleo_g0b1re)
#define USBD_PRODUCT_STRING_FS "NUCLEO-G0B1RE gs_usb"
#define USBD_MANUFACTURER_STRING "STMicroelectronics"
#define DFU_INTERFACE_STRING_FS "NUCLEO-G0B1RE firmware upgrade interface"

#define TIM2_CLOCK_SPEED 64000000

#define CAN_CLOCK_SPEED 64000000
#define NUM_CAN_CHANNEL 1
#define CONFIG_CANFD 1

/*************** STM32G431 ***************/

#elif defined(BOARD_CANable2_MKS)
#define USBD_PRODUCT_STRING_FS (uint8_t*) "CANable2-MKS gs_usb"
#define USBD_MANUFACTURER_STRING (uint8_t*) "CANable2-MKS"
#define DFU_INTERFACE_STRING_FS (uint8_t*) "CANable2-MKS firmware upgrade interface"

#define TIM2_CLOCK_SPEED 160000000

#define CAN_CLOCK_SPEED 80000000
#define NUM_CAN_CHANNEL 1
#define CONFIG_CANFD 1

#else
#error please define BOARD
Expand Down
3 changes: 3 additions & 0 deletions include/usbd_gs_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ typedef struct {
#elif defined(STM32G0)
# define USB_INTERFACE USB_DRD_FS
# define USB_INTERRUPT USB_UCPD1_2_IRQn
#elif defined(STM32G4)
# define USB_INTERFACE USB
# define USB_INTERRUPT USB_LP_IRQn
#endif

uint8_t USBD_GS_CAN_Init(USBD_GS_CAN_HandleTypeDef *hcan, USBD_HandleTypeDef *pdev);
Expand Down
54 changes: 54 additions & 0 deletions libs/STM32_HAL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(STM32_HAL)
set(STM32F0_HALDIR stm32f0xx-hal-driver)
set(STM32F4_HALDIR stm32f4xx-hal-driver)
set(STM32G0_HALDIR stm32g0xx-hal-driver)
set(STM32G4_HALDIR stm32g4xx-hal-driver)

set(STM32F0_SOURCES
config/stm32f0xx_hal_conf.h
Expand Down Expand Up @@ -175,6 +176,54 @@ set(STM32G0_SOURCES
include/cmsis/device/system_stm32g0xx.h
)

set(STM32G4_SOURCES
config/stm32g4xx_hal_conf.h

${STM32G4_HALDIR}/Inc/stm32g4xx_hal_def.h
${STM32G4_HALDIR}/Inc/stm32g4xx_hal.h
${STM32G4_HALDIR}/Src/stm32g4xx_hal.c

${STM32G4_HALDIR}/Inc/stm32g4xx_hal_cortex.h
${STM32G4_HALDIR}/Src/stm32g4xx_hal_cortex.c

${STM32G4_HALDIR}/Inc/stm32g4xx_hal_fdcan.h
${STM32G4_HALDIR}/Src/stm32g4xx_hal_fdcan.c

${STM32G4_HALDIR}/Inc/stm32g4xx_hal_gpio_ex.h
${STM32G4_HALDIR}/Inc/stm32g4xx_hal_gpio.h
${STM32G4_HALDIR}/Src/stm32g4xx_hal_gpio.c

${STM32G4_HALDIR}/Inc/stm32g4xx_hal_pcd.h
${STM32G4_HALDIR}/Src/stm32g4xx_hal_pcd.c
${STM32G4_HALDIR}/Inc/stm32g4xx_hal_pcd_ex.h
${STM32G4_HALDIR}/Src/stm32g4xx_hal_pcd_ex.c

${STM32G4_HALDIR}/Inc/stm32g4xx_hal_pwr.h
${STM32G4_HALDIR}/Src/stm32g4xx_hal_pwr.c
${STM32G4_HALDIR}/Inc/stm32g4xx_hal_pwr_ex.h
${STM32G4_HALDIR}/Src/stm32g4xx_hal_pwr_ex.c

${STM32G4_HALDIR}/Inc/stm32g4xx_hal_rcc.h
${STM32G4_HALDIR}/Src/stm32g4xx_hal_rcc.c
${STM32G4_HALDIR}/Inc/stm32g4xx_hal_rcc_ex.h
${STM32G4_HALDIR}/Src/stm32g4xx_hal_rcc_ex.c

${STM32G4_HALDIR}/Inc/stm32g4xx_ll_usb.h
${STM32G4_HALDIR}/Src/stm32g4xx_ll_usb.c

src/cmsis/system_stm32g4xx.c
${STM32G4_HALDIR}/Inc/Legacy/stm32_hal_legacy.h

include/cmsis/cmsis_compiler.h
include/cmsis/cmsis_device.h
include/cmsis/cmsis_gcc.h
include/cmsis/cmsis_version.h
include/cmsis/core_cm4.h
include/cmsis/device/stm32g4xx.h
include/cmsis/device/stm32g431xx.h
include/cmsis/device/system_stm32g4xx.h
)

set(INCLUDE_DIRS
include/
include/cmsis
Expand All @@ -201,3 +250,8 @@ add_library(STM32_HAL_STM32G0B1xx STATIC ${STM32G0_SOURCES})
target_include_directories(STM32_HAL_STM32G0B1xx PUBLIC ${INCLUDE_DIRS} ${STM32G0_HALDIR}/Inc)
target_compile_options(STM32_HAL_STM32G0B1xx PRIVATE ${CPUFLAGS_G0} -Wno-unused-parameter)
target_compile_definitions(STM32_HAL_STM32G0B1xx PUBLIC STM32G0B1xx)

add_library(STM32_HAL_STM32G431xx STATIC ${STM32G4_SOURCES})
target_include_directories(STM32_HAL_STM32G431xx PUBLIC ${INCLUDE_DIRS} ${STM32G4_HALDIR}/Inc)
target_compile_options(STM32_HAL_STM32G431xx PRIVATE ${CPUFLAGS_G4} -Wno-unused-parameter)
target_compile_definitions(STM32_HAL_STM32G431xx PUBLIC STM32G431xx)
2 changes: 2 additions & 0 deletions libs/STM32_HAL/config/hal_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ THE SOFTWARE.
# include "stm32f4xx_hal.h"
#elif defined(STM32G0)
# include "stm32g0xx_hal.h"
#elif defined(STM32G4)
# include "stm32g4xx_hal.h"
#endif
Loading
Loading