Skip to content

Conversation

Jackb18b
Copy link
Member

@Jackb18b Jackb18b commented Sep 9, 2025

Added dual CAN/FDCAN driver for stm32s.

Code marked as draft until tested on hardware.

@justinlopez1 justinlopez1 requested a review from Copilot September 9, 2025 17:15
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a comprehensive dual CAN/FDCAN driver for STM32 microcontrollers to support both bxCAN (STM32L4) and FDCAN (STM32U5) peripherals. The driver provides hardware filter configuration, callback-based message routing, and RTOS-based transmit/receive handling.

Key changes:

  • Complete rewrite of the CAN driver with unified API for both CAN and FDCAN peripherals
  • Addition of CANFrame class for message handling with mutex-based thread safety
  • Implementation of hardware filtering and callback system for message routing

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
resources/Drivers/SG_Drivers/CAN/Src/CanDriver.cpp Complete implementation of dual CAN/FDCAN driver with filtering, callbacks, and RTOS integration
resources/Drivers/SG_Drivers/CAN/Include/CanDriverApi.hpp API definitions and type aliases for CAN/FDCAN compatibility
resources/Drivers/SG_Drivers/CAN/Include/CanDriver.hpp Header with CANDevice and CANFrame class definitions
firmware/CAN-DevBoard/user/src/user_threads.cpp Example usage of the new CAN driver API
firmware/NUCLEOL476RG-FreeRTOS-ExampleProject/CMakeLists.txt Build system updates to include CAN driver
Various header/source files Code formatting and include order improvements

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

* @param msg
* @return HAL_StatusTypeDef
*/
HAL_StatusTypeDef Send(CANFrame* msg);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
HAL_StatusTypeDef Send(CANFrame* msg);
static HAL_StatusTypeDef Send(CANFrame* msg);

Make this static

* @param id The CAN identifier to match against (11-bit or 29-bit depending on @p id_type).
* @return const CanCallback* if found, nullptr if no connected callback
*/
const CanCallback* find_by_range(uint32_t id);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to private

* @param id The CAN identifier to match against (11-bit or 29-bit depending on @p id_type).
* @return const CanCallback* if found, nullptr if no connected callback
*/
const CanCallback* find_by_id(uint32_t id);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to private

Comment on lines +263 to +273
CanHandle_t* hcan_ = nullptr;

std::vector<CanFilter_t> filters_;
std::vector<IdEntry> idCallbacks_;
std::vector<RangeEntry> rangeCallbacks_;
CanCallback allCallback_ = nullptr;

osMessageQueueId_t tx_queue_ = osMessageQueueNew(TX_QUEUE_SIZE, sizeof(CANFrame*), NULL);

CANDevice(const CANDevice&) = delete;
CANDevice& operator=(const CANDevice&) = delete;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean up formatting

Comment on lines +22 to +36
#define THREAD_PRIORITY osPriorityAboveNormal /* Priority of Rx and Tx threads */

#define NUM_FILTER_BANKS 14
#define NUM_CAN_CALLBACKS 16

#define THREAD_STACK_SIZE_WORDS 512

#define TX_QUEUE_SIZE 3 /* Size of Tx message queue */
#define TX_TIMEOUT 10 /* Timeout for tx thread in ms */

#define MAX_CAN_ID 0x7FFu

#ifndef CANDEVICE_MAX_BUSES
#define CANDEVICE_MAX_BUSES 2
#endif
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean up formatting and add more comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant