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

Lots of improvements, FDCAN preparation #45

Merged
merged 51 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
41ad5e8
add alternative constructors for more pin configuration options
duckylotl Dec 28, 2024
f099f77
add setMode function to set a mode by arg. Better hints that modes ar…
duckylotl Dec 29, 2024
c2ae037
allow configuration of the IRQ priorities, default to lowest priority
duckylotl Dec 29, 2024
0817a12
be specific with IRQ aliasing, add notes
duckylotl Dec 30, 2024
ca37a27
enable / disable the Rx0 IRQ as well
duckylotl Dec 30, 2024
7d0bb7c
add hook for the CEC Handler for the F0 Platform.
duckylotl Dec 30, 2024
fe2af09
detect USB Driver and print incompatibility error
duckylotl Dec 30, 2024
6cbfeb8
Add workaround (enabled by user) for F1 in case USB is used. Uses FIF…
duckylotl Dec 30, 2024
9239e50
add constructor that takes digital pins as arguments
duckylotl Dec 30, 2024
8cdb947
move CAN_HandleTypeDef object into the class together with a pointer …
duckylotl Dec 31, 2024
064f6d9
protect HAL calls from uninitialized handle
duckylotl Jan 1, 2025
817fa8a
fix: do not enable the Tx interrupt flag if TX IRQ is blocked by USB
duckylotl Jan 1, 2025
4898233
fix: handle the special case with shared USB IRQ in enableMBInterrupt…
duckylotl Jan 1, 2025
be7ae1a
fix: don't setup not connected pin
duckylotl Jan 1, 2025
374018c
move peripheral resolving and registration with globals into begin()
duckylotl Jan 1, 2025
fb81317
let setBaudrate() be called before begin(). Save baudrate and use on …
duckylotl Jan 1, 2025
a9038c1
allow restarting bus with other baudrate
duckylotl Jan 1, 2025
807198b
only initialize filters on first start, not on each re-start
duckylotl Jan 1, 2025
8a821fe
add end() function to de-initialize and release can peripheral
duckylotl Jan 1, 2025
53271d9
on F1 platform verify that both pins are using the same alternate fun…
duckylotl Jan 1, 2025
f3aa639
fix: set SlaveStartFilterBank for all ConfigFilter calls. And cleanup…
duckylotl Jan 2, 2025
a05be3d
turn off unused filters during init
duckylotl Jan 2, 2025
b583569
add feedback to baudrate calculation, don't start with impossible clo…
duckylotl Jan 2, 2025
dc12061
use HAL function to get bus clock directly
duckylotl Jan 2, 2025
6d50b4f
remove redundant mode member variable
duckylotl Jan 2, 2025
ff8fbfc
move static init value initialization from begin() to init()
duckylotl Jan 2, 2025
53d09a0
add proper support for all filter types. Legacy function still has ol…
duckylotl Jan 2, 2025
f44452a
change FIFO target to action in prep for FDCAN
duckylotl Jan 2, 2025
473b58d
add setFilter variant that sets filter state without changing filter …
duckylotl Jan 2, 2025
ea1900b
fix: add CAN2 filter index offset
duckylotl Jan 2, 2025
a2d634f
fix: properly only set state without clobbering other filter settings…
duckylotl Jan 2, 2025
221a84c
add macros to select default FIFO based on platform limitations
duckylotl Jan 2, 2025
972ca0e
use custom filter functions for filter initialization
duckylotl Jan 2, 2025
6e07d48
read all pending messages. Multiple message events can happen before …
duckylotl Jan 2, 2025
6b1f21f
rename getAPB1Clock() to more generic getCanPeripheralClock()
duckylotl Jan 5, 2025
60a9e2d
remove CanHandle arg from baudrate calculation functions, use member …
duckylotl Jan 5, 2025
50edb2f
formatting, group functions according to operational state
duckylotl Jan 5, 2025
c13ee3e
change STM32 style filter function return value to true on success. K…
duckylotl Jan 5, 2025
a9bcfa3
fix: check filter bank bounds
duckylotl Jan 5, 2025
f594cf6
fix: check if instance is initialized
duckylotl Jan 5, 2025
6add54a
fix: rename state arg to enabled
duckylotl Jan 5, 2025
db1bca5
add discovery functions for type and amount of filter banks
duckylotl Jan 5, 2025
053cfb3
Rename enums to upper snake case
duckylotl Jan 5, 2025
0cb3e84
add setters for pre-begin() configuration options
duckylotl Jan 5, 2025
8e3e6b5
add additional workaround for USB+CAN on F3 Platform. If USB IRQs are…
duckylotl Jan 5, 2025
eb6de54
fix: don't use ifdef for combined logic
duckylotl Jan 6, 2025
392b5e1
fix: don't lock up (Error_Handler) when calling hasPeripheral or free…
duckylotl Jan 8, 2025
b1351e6
allocatePeripheral() takes peripheral as arg and sets up handle.Insta…
duckylotl Jan 8, 2025
89dd4d9
add destructor, stop peripheral and release everything
duckylotl Jan 15, 2025
7589260
enable RX pullup. Eases use of loopback modes without external circui…
duckylotl Jan 18, 2025
9900652
update readme
duckylotl Jan 22, 2025
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
Prev Previous commit
Next Next commit
Add workaround (enabled by user) for F1 in case USB is used. Uses FIF…
…O1 instead of 0. Need to call polling function for TX event handling.
  • Loading branch information
duckylotl committed Dec 30, 2024
commit 6cbfeb8e46b3969055ef212ec2b17c0b16ee13d2
50 changes: 50 additions & 0 deletions STM32_CAN.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "STM32_CAN.h"

#include "core_debug.h"

#ifdef HAL_CEC_MODULE_ENABLED && defined(STM32_CAN1_SHARED_WITH_CEC)
/** Pointer to CEC_HandleTypeDef structure that contains
* the configuration information for the specified CEC.
Expand Down Expand Up @@ -175,12 +177,18 @@ void STM32_CAN::begin( bool retransmission ) {
HAL_NVIC_SetPriority(CAN1_IRQn_AIO, preemptPriority, subPriority);
HAL_NVIC_EnableIRQ(CAN1_IRQn_AIO);
#else
#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
/** CAN Tx and Rx0 blocked by USB, only using Rx1 */
HAL_NVIC_SetPriority(CAN1_RX1_IRQn, preemptPriority, subPriority);
HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);
#else
// NVIC configuration for CAN1 Reception complete interrupt
HAL_NVIC_SetPriority(CAN1_RX0_IRQn, preemptPriority, subPriority);
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn );
// NVIC configuration for CAN1 Transmission complete interrupt
HAL_NVIC_SetPriority(CAN1_TX_IRQn, preemptPriority, subPriority);
HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
#endif
#endif /** else defined(CAN1_IRQn_AIO) */

n_pCanHandle->Instance = CAN1;
Expand Down Expand Up @@ -245,7 +253,11 @@ void STM32_CAN::setBaudRate(uint32_t baud)
HAL_CAN_Start( n_pCanHandle );

// Activate CAN RX notification
#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
HAL_CAN_ActivateNotification( n_pCanHandle, CAN_IT_RX_FIFO1_MSG_PENDING);
#else
HAL_CAN_ActivateNotification( n_pCanHandle, CAN_IT_RX_FIFO0_MSG_PENDING);
#endif

// Activate CAN TX notification
HAL_CAN_ActivateNotification( n_pCanHandle, CAN_IT_TX_MAILBOX_EMPTY);
Expand Down Expand Up @@ -302,9 +314,19 @@ bool STM32_CAN::write(CAN_message_t &CAN_tx_msg, bool sendMB)
bool STM32_CAN::read(CAN_message_t &CAN_rx_msg)
{
bool ret;
#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
__HAL_CAN_DISABLE_IT(n_pCanHandle, CAN_IT_RX_FIFO1_MSG_PENDING);
#else
__HAL_CAN_DISABLE_IT(n_pCanHandle, CAN_IT_RX_FIFO0_MSG_PENDING);
#endif

ret = removeFromRingBuffer(rxRing, CAN_rx_msg);

#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
__HAL_CAN_ENABLE_IT(n_pCanHandle, CAN_IT_RX_FIFO1_MSG_PENDING);
#else
__HAL_CAN_ENABLE_IT(n_pCanHandle, CAN_IT_RX_FIFO0_MSG_PENDING);
#endif
return ret;
}

Expand All @@ -315,6 +337,12 @@ bool STM32_CAN::setFilter(uint8_t bank_num, uint32_t filter_id, uint32_t mask, I
sFilterConfig.FilterBank = bank_num;
sFilterConfig.FilterMode = filter_mode;
sFilterConfig.FilterScale = filter_scale;
#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
if(fifo == CAN_RX_FIFO0)
{
core_debug("WARNING: RX0 IRQ is blocked by USB Driver. Events only handled by polling and RX1 events!\n");
}
#endif
sFilterConfig.FilterFIFOAssignment = fifo;
sFilterConfig.FilterActivation = ENABLE;

Expand Down Expand Up @@ -406,7 +434,11 @@ void STM32_CAN::initializeFilters()
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
#else
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
#endif
sFilterConfig.FilterActivation = ENABLE;
#ifdef CAN2
// Filter banks from 14 to 27 are for Can2, so first for Can2 is bank 14. This is not relevant for devices with only one CAN
Expand Down Expand Up @@ -913,14 +945,22 @@ extern "C" void HAL_CAN_TxMailbox2CompleteCallback( CAN_HandleTypeDef *CanHandle
}

// This is called by RX0_IRQHandler when there is message at RX FIFO0 buffer
#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
extern "C" void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *CanHandle)
#else
extern "C" void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *CanHandle)
#endif
{
CAN_message_t rxmsg;
CAN_RxHeaderTypeDef RxHeader;
//bool state = Disable_Interrupts();

// move the message from RX FIFO0 to RX ringbuffer
#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
if (HAL_CAN_GetRxMessage( CanHandle, CAN_RX_FIFO1, &RxHeader, rxmsg.buf ) == HAL_OK)
#else
if (HAL_CAN_GetRxMessage( CanHandle, CAN_RX_FIFO0, &RxHeader, rxmsg.buf ) == HAL_OK)
#endif
{
if ( RxHeader.IDE == CAN_ID_STD )
{
Expand Down Expand Up @@ -978,7 +1018,12 @@ extern "C" void CAN1_IRQHandler_AIO(void)
#else

// RX IRQ handlers
#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
/** If USB blocks TX and RX0 IRQs, will use RX1 by default*/
extern "C" void CAN1_RX1_IRQHandler(void)
#else
extern "C" void CAN1_RX0_IRQHandler(void)
#endif
{
HAL_CAN_IRQHandler(&hcan1);
}
Expand All @@ -997,7 +1042,12 @@ extern "C" void CAN3_RX0_IRQHandler(void)
#endif

// TX IRQ handlers
#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
/** If USB blocks TX and RX0 IRQs, need to poll for Tx events*/
extern "C" void STM32_CAN_Poll_IRQ_Handler(void)
#else
extern "C" void CAN1_TX_IRQHandler(void)
#endif
{
HAL_CAN_IRQHandler(&hcan1);
}
Expand Down
10 changes: 7 additions & 3 deletions STM32_CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ to same folder with sketch and haven #define HAL_CAN_MODULE_ENABLED there. See e
#ifndef STM32_CAN_H
#define STM32_CAN_H

#include <Arduino.h>

/** Handling special cases for IRQ Handlers */
#if defined(STM32F0xx)
#if defined(STM32F042x6) || defined(STM32F072xB) || defined(STM32F091xC) || defined(STM32F098xx)
Expand Down Expand Up @@ -128,11 +130,13 @@ to same folder with sketch and haven #define HAL_CAN_MODULE_ENABLED there. See e
#endif
#endif

#ifdef STM32_CAN1_TX_RX0_BLOCKED_BY_USB
#error "USB and CAN interrupts are shared on the F1/F3 platform, driver is not compatible with USBDevice of Arduino core.
#if defined(STM32_CAN1_TX_RX0_BLOCKED_BY_USB) && !defined(STM32_CAN_USB_WORKAROUND_POLLING)
#error "USB and CAN interrupts are shared on the F1/F3 platform, driver is not compatible with USBDevice of Arduino core. Can define STM32_CAN_USB_WORKAROUND_POLLING to disable error msg and call STM32_CAN_Poll_IRQ_Handler to poll for Tx IRQ events. Only use FIFO 1."
#elif defined(USBCON) && defined(STM32_CAN_USB_WORKAROUND_POLLING)
#warning "CAN IRQ Handler is used by USBDevice driver, call STM32_CAN_Poll_IRQ_Handler() frequently to handle CAN events."
extern "C" void STM32_CAN_Poll_IRQ_Handler(void);
#endif

#include <Arduino.h>

// This struct is directly copied from Teensy FlexCAN library to retain compatibility with it. Not all are in use with STM32.
// Source: https://github.com/tonton81/FlexCAN_T4/
Expand Down