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
da4a67e
added id to CAN RX callback
ahoysal Jan 27, 2026
05214b4
Added enum as CAN identifier in RX
ahoysal Jan 27, 2026
9261965
Automatic CMake Format: Standardized formatting automatically
github-actions[bot] Jan 27, 2026
f26d44f
Automatic Clang-Format: Standardized formatting automatically
github-actions[bot] Jan 27, 2026
d36a3ba
Changed CAN_RX_Callback to take in ID, data, size
VihanJ Jan 27, 2026
9e1af22
Automatic Clang-Format: Standardized formatting automatically
github-actions[bot] Jan 27, 2026
499d15c
Automatic CMake Format: Standardized formatting automatically
github-actions[bot] Jan 27, 2026
4337a58
added formatting for uint32_t id in can_tests.c
VihanJ Jan 27, 2026
2b00473
Automatic Clang-Format: Standardized formatting automatically
github-actions[bot] Jan 27, 2026
98923cf
cleand up can.c
VihanJ Jan 27, 2026
82e7709
added basepri masking, also tried to prevent spurious NVIC interrupts…
VihanJ Jan 29, 2026
6d6d9ff
added basepri masking, also tried to prevent spurious NVIC interrupts…
VihanJ Jan 29, 2026
18af90b
Refactored to use statically sized spsc buffer
VihanJ Jan 30, 2026
ebc37b8
passed human testing
VihanJ Feb 4, 2026
7245169
added notes in readme
VihanJ Feb 11, 2026
28b8e14
Merge remote-tracking branch 'origin/main' into CAN_ISR
VihanJ Feb 15, 2026
1b2adde
tried to fix CAN data baud rate in ECU/Core/Src/main.c
VihanJ Feb 19, 2026
622bf6c
Merge branch 'main' into CAN_ISR
VihanJ Feb 24, 2026
6afe33a
Automatic Clang-Format: Standardized formatting automatically
github-actions[bot] Feb 24, 2026
837d821
Merge branch 'main' into CAN_ISR
dchansen06 Feb 24, 2026
7c779ca
Merge branch 'main' into CAN_ISR
dchansen06 Feb 24, 2026
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
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@
"target/stm32g4x.cfg"
],
"searchDir": [],
"preLaunchTask": "CMake: configure and build G4ADCTESTING",
"preLaunchTask": "CMake: configure and build G4CANTESTING",
"showDevDebugOutput": "raw",
"svdPath": "${workspaceFolder}/Lib/Vendor/CMSIS_5/SVD/STM32G474.svd",
"swoConfig": {
Expand Down
8 changes: 4 additions & 4 deletions ECU/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,17 @@ void CAN_Configure()
canCfg.hal_fdcan_init.NominalSyncJumpWidth = 16;
canCfg.hal_fdcan_init.NominalTimeSeg1 = 127; // Updated for 170MHz: (1+127+42)*1 = 170 ticks -> 1 Mbps
canCfg.hal_fdcan_init.NominalTimeSeg2 = 42;
canCfg.hal_fdcan_init.DataPrescaler = 8;
canCfg.hal_fdcan_init.DataPrescaler = 2;
canCfg.hal_fdcan_init.DataSyncJumpWidth = 16;
canCfg.hal_fdcan_init.DataTimeSeg1 = 15; // Updated for 170MHz: (1+15+5)*8 = 168 ticks -> ~5 Mbps
canCfg.hal_fdcan_init.DataTimeSeg2 = 5;
canCfg.hal_fdcan_init.DataTimeSeg1 = 12; // Updated for 170MHz: 170 MHz/((1+12+4)*2) = 5 Mbps
canCfg.hal_fdcan_init.DataTimeSeg2 = 4;
canCfg.hal_fdcan_init.StdFiltersNbr = 1;
canCfg.hal_fdcan_init.ExtFiltersNbr = 0;

canCfg.rx_callback = NULL;
canCfg.rx_interrupt_priority = 15; // TODO: Maybe make these not hardcoded
canCfg.tx_interrupt_priority = 15;
canCfg.tx_buffer_length = CAN_TX_BUFFER_LENGTH;
// canCfg.tx_buffer_length = CAN_TX_BUFFER_LENGTH;

// RX shared settings
canCfg.init_rx_gpio.Mode = GPIO_MODE_AF_PP;
Expand Down
38 changes: 24 additions & 14 deletions Lib/Peripherals/CAN/Inc/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef struct {
uint32_t tx_interrupt_priority;

// Circular Buffer
uint32_t tx_buffer_length;
// uint32_t tx_buffer_capacity;

GPIO_TypeDef *rx_gpio; // Instance name, like GPIOA, GPIOB, etc.
GPIO_InitTypeDef init_rx_gpio; // GPIO Parameters - set correct Alternate Function, no pullup/pulldown, high/very_high frequency
Expand All @@ -34,17 +34,37 @@ typedef struct {
// additional parameters
} CANConfig;

#define FDCAN_MAX_DATA_BYTES 64
typedef struct {
FDCAN_TxHeaderTypeDef tx_header;
uint8_t data[FDCAN_MAX_DATA_BYTES];
} FDCANTxMessage;
typedef struct {
FDCAN_RxHeaderTypeDef rx_header;
uint8_t data[FDCAN_MAX_DATA_BYTES];
} FDCANRxMessage;

// FDCAN peripheral for STM32G4
typedef struct {
FDCAN_HandleTypeDef *hal_fdcanP;
CircularBuffer *tx_buffer;
uint32_t tx_buffer_length;
FDCAN_HandleTypeDef *hal_fdcanP; // DO NOT REORDER THIS

// TX buffer
volatile FDCANTxMessage *tx_buffer;
volatile uint32_t tx_capacity;
volatile uint32_t tx_tail;
volatile uint32_t tx_elements;

// RX Callback
CAN_RXCallback rx_callback;

uint32_t rx_interrupt_priority;
uint32_t tx_interrupt_priority;

// for release
GPIO_TypeDef *rx_gpio;
uint32_t rx_pin;
GPIO_TypeDef *tx_gpio;
uint32_t tx_pin;
uint32_t Clock_Source;

// state
Expand All @@ -54,16 +74,6 @@ typedef struct {
// error states
} CANHandle;

#define FDCAN_MAX_DATA_BYTES 64
typedef struct {
FDCAN_TxHeaderTypeDef tx_header;
uint8_t data[FDCAN_MAX_DATA_BYTES];
} FDCANTxMessage;
typedef struct {
FDCAN_RxHeaderTypeDef rx_header;
uint8_t data[FDCAN_MAX_DATA_BYTES];
} FDCANRxMessage;

CANHandle *can_init(const CANConfig *config); // user must supply an rx callback function
int can_start(CANHandle *handle);
int can_stop(CANHandle *handle);
Expand Down
9 changes: 5 additions & 4 deletions Lib/Peripherals/CAN/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Verify ISR safety, no race conditions, atomic read/writes
-Shouldn't disable GPIOs in the MSP layers when releasing, might affect other peripherals

IDEAS for other features:
-
- DMA support for copying 64 bytes from circular buffer
- abstract to different STM families besides STM32G4
- Rx Buffering
- TX Buffering policy, do we spread them out over multiple TX buffers
Expand All @@ -43,7 +45,6 @@ IDEAS for other features:
- TX FIFO vs Queue policy (only allow FIFOS)
- Add support for RXFifo1


TESTING- ----------------------------------------------
USE LOGOMATIC for return status -
either returns through semihosting or debug cores
Expand All @@ -55,7 +56,6 @@ Testing framework
- All tests are run from the top level function in can_test.c

- can_test.c should initialize everything properly.
- May have to create platform specific asserts when testing state
- use LOGOMATIC to return errors or throw asserts

- Platform testing, such as in G4PERTESTING just needs include "can_test.h" and call top level function
Expand All @@ -70,6 +70,7 @@ Library Centric Testing:
- Test the implementation in each library.

HAL_Rewrite:
- Alternatively, rewrite without using HAL, just use CMSIS definitions.
- PROS: Would look good on your Github.
- Alternatively, rewrite without using HAL, just use CMSIS definitions.
- There isn't actually that much going on underneath HAL, it just looks painful because of CMSIS
- PROS: Would look good on your Github.
- CONS: takes too long
Loading
Loading