Skip to content

Conversation

@william-tang914
Copy link
Contributor

This change replaces static memory allocation for FlexCAN message buffers with dynamic allocation using a per-instance heap. The motivation is to support FlexCAN instances with varying message buffer counts and CAN FD configurations.
For CAN FD instances, message buffer count is adjusted based on 64-byte payload size (7 buffers per RAM block vs 32 for 8-byte payload).

Key changes:

  • Add CAN_MCUX_FLEXCAN_HEAP_SIZE Kconfig option (default 2048 bytes) to configure heap size for message buffer allocation
  • Replace compile-time MCUX_FLEXCAN_MAX_MB/RX/TX macros with runtime calculation in mcux_flexcan_get_mb_config()
  • Add dynamic allocation of rx_allocs, tx_allocs, rx_cbs, and tx_cbs arrays using k_heap during driver initialization
  • Update all array bounds checks and loop limits to use runtime values stored in driver data structure
  • Remove Kconfig symbol CAN_MAX_MB beacuse such information is added to device tree.
  • Remove range limitation in Kconfig symbol CAN_MAX_FILTER because such validation is added in mcux_flexcan_get_mb_config()

Fixes #92798

Add the `nxp,max-mb` device tree property to all NXP FlexCAN controller
nodes across various SoC families to specify the maximum number of
8-byte payload message buffers supported by each FlexCAN instance.

This change updates device tree source files for multiple NXP SoC
families including Kinetis K6x, RT10xx, RT11xx, RT118x, MCX, S32K,
S32Z series, and i.MX8MP/i.MX93. The property values are set based on
hardware specifications for each specific FlexCAN instance.

The `nxp,max-mb` property is now required in the FlexCAN dts binding
and enables the driver to properly configure message buffer limits at
runtime. Values range from 16 to 128 message buffers depending on
the specific SoC and FlexCAN instance capabilities.

This property addition ensures proper resource allocation and prevents
buffer overflow issues in FlexCAN driver implementations.

Signed-off-by: William Tang <william.tang@nxp.com>
This change replaces static memory allocation for FlexCAN message buffers
with dynamic allocation using a per-instance heap. The motivation is to
support FlexCAN instances with varying message buffer counts and CAN FD
configurations.

For CAN FD instances, message buffer count is adjusted based on 64-byte
payload size (7 buffers per RAM block vs 32 for 8-byte payload).

Key changes:
- Add CAN_MCUX_FLEXCAN_HEAP_SIZE Kconfig option (default 2048 bytes) to
  configure heap size for message buffer allocation
- Replace compile-time MCUX_FLEXCAN_MAX_MB/RX/TX macros with runtime
  calculation in mcux_flexcan_get_mb_config()
- Add dynamic allocation of rx_allocs, tx_allocs, rx_cbs, and tx_cbs
  arrays using k_heap during driver initialization
- Update all array bounds checks and loop limits to use runtime values
  stored in driver data structure
- Remove Kconfig symbol `CAN_MAX_MB` beacuse such information is added
  to device tree.
- Remove range limitation in Kconfig symbol `CAN_MAX_FILTER` because
  such validation is added in mcux_flexcan_get_mb_config()

Fixes zephyrproject-rtos#92798

Signed-off-by: William Tang <william.tang@nxp.com>
@william-tang914 william-tang914 force-pushed the feature/support-message-buffer-dynamic-memory-allocation branch from a824dfb to fefe776 Compare November 14, 2025 02:46
@sonarqubecloud
Copy link

@william-tang914 william-tang914 marked this pull request as draft November 14, 2025 07:30
Copy link
Member

@henrikbrixandersen henrikbrixandersen left a comment

Choose a reason for hiding this comment

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

Dynamic allocation is not the solution here. This can be derived at build timer per instance (as is done in other drivers as well, see e.g.

#define CAN_MCAN_DT_CALLBACKS_DEFINE(node_id, _name) \
).

On a side note, I do have a plan for a Zephyr-native FlexCAN driver, but the more workarounds and #ifdefs that are added, the more I realise that the NXP HAL hides way too many differences in the IP implementations covered under the market name "FlexCAN", which are very, very difficult to capture in a single driver.

@PetervdPerk-NXP
Copy link
Contributor

Dynamic allocation is not the solution here. This can be derived at build timer per instance (as is done in other drivers as well, see e.g.

Indeed we've got tiny MCX parts with little flash/ram with FlexCAN support, rather avoid using dynamic memory for something as as simple as CAN in an RTOS.

On a side note, I do have a plan for a Zephyr-native FlexCAN driver, but the more workarounds and #ifdefs that are added, the more I realise that the NXP HAL hides way too many differences in the IP implementations covered under the market name "FlexCAN", which are very, very difficult to capture in a single driver.

About your driver, @henrikbrixandersen do you already have something in place? I was planning to create my own HAL-less FlexCAN driver, but I put that on hold when I heard you were working on one as well.

If so, would it be something to do developement in the open? Opening a branch somewhere to work on that?

Regarding different IP implementation, I would just focus on i.MXRT10XX and later (S32K, IMX9 seem to be quite similar). And then utilize the Zephyr compatible string to override the driver for these newer parts. RXFIFO isn't that interesting to implement because typically not all instances on a single chip (typically only primary) have that feature.

@william-tang914
Copy link
Contributor Author

Hi @henrikbrixandersen , following your advice, I create another draft PR.
#99483

@william-tang914 william-tang914 deleted the feature/support-message-buffer-dynamic-memory-allocation branch November 18, 2025 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

can: can_mcux_flexcan.c allows a mailbox overrun resulting in an imprecise data bus fault

6 participants