-
Notifications
You must be signed in to change notification settings - Fork 8.2k
drivers: can: mcux: flexcan: add dynamic memory allocation for MB #99306
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
drivers: can: mcux: flexcan: add dynamic memory allocation for MB #99306
Conversation
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>
a824dfb to
fefe776
Compare
|
henrikbrixandersen
left a comment
There was a problem hiding this 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.
zephyr/include/zephyr/drivers/can/can_mcan.h
Line 1216 in 3cf7b20
| #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.
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.
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 |
|
Hi @henrikbrixandersen , following your advice, I create another draft PR. |



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:
CAN_MAX_MBbeacuse such information is added to device tree.CAN_MAX_FILTERbecause such validation is added in mcux_flexcan_get_mb_config()Fixes #92798