Skip to content

Conversation

@Ayushkothari96
Copy link

Fixes #99191

Add conditional compilation to support different STM32 XSPI HAL capabilities across product lines.

Related PRs:

Problem:
After adding XSPI1 compatibility macros (hal_stm32#328), the XSPI PSRAM driver still had STM32H5-incompatible code that assumed all STM32 XSPI peripherals have the same features (XSPI Manager, 16-line data mode, MaxTran/MemorySelect fields).

Solution:

  • Use XSPIM (XSPI Manager) configuration only when xspi_mgr clock exists (STM32H7RS)
  • Use 16-line data mode when HAL_XSPI_DATA_16_LINES is defined (STM32H7RS), otherwise 8-line (STM32H5)
  • Include MaxTran and MemorySelect Init fields only when XSPIM exists

Benefits:

  • STM32H7RS: Uses full hardware capabilities (16-line XSPI with dual-port manager)
  • STM32H5 (H573): Works with simpler single-port XSPI/OCTOSPI implementation (8-line max)
  • Automatic adaptation based on devicetree and HAL feature detection

Testing:

  • Verified compilation on STM32H573I-DK with XSPI PSRAM driver enabled
  • Driver object file successfully created: memc_stm32_xspi_psram.c.obj
  • No code changes needed when switching between STM32 variants

@github-actions
Copy link

Hello @Ayushkothari96, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

Add conditional compilation to support different STM32 XSPI HAL
capabilities across product lines:

- Use XSPIM (XSPI Manager) configuration when available (STM32H7RS)
- Use 16-line data mode on STM32H7RS, 8-line on STM32H5
- Include MaxTran and MemorySelect Init fields only when XSPIM exists

This allows the driver to automatically use full hardware
capabilities on STM32H7RS (16-line XSPI with dual-port manager)
while maintaining compatibility with STM32H5 series that have
simpler single-port XSPI/OCTOSPI implementation with 8-line max.

Related to zephyrproject-rtos/hal_stm32#328

Signed-off-by: Ayush Kothari <[email protected]>
@sonarqubecloud
Copy link

@JarmouniA
Copy link
Contributor

Already addressed by #99194

@erwango
Copy link
Member

erwango commented Nov 17, 2025

Thanks but a similar PR was already opended last week: #99194
Would you mind have a look at this one first ?

Comment on lines +344 to +347
#else
/* STM32H5 max is 8 lines */
cmd.DataMode = HAL_XSPI_DATA_8_LINES;
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

Use more generic wording:

Suggested change
#else
/* STM32H5 max is 8 lines */
cmd.DataMode = HAL_XSPI_DATA_8_LINES;
#endif
#else /* 16 lines mode not supported */
cmd.DataMode = HAL_XSPI_DATA_8_LINES;
#endif

cmd.AddressWidth = HAL_XSPI_ADDRESS_32_BITS;
cmd.AddressDTRMode = HAL_XSPI_ADDRESS_DTR_ENABLE;
#if defined(HAL_XSPI_DATA_16_LINES)
/* Use 16-line if DT requests and hardware supports it */
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/* Use 16-line if DT requests and hardware supports it */
/* Use 16 lines mode if DT requests and hardware supports it */

Comment on lines +220 to +222
#if DT_CLOCKS_HAS_NAME(STM32_XSPI_NODE, xspi_mgr)
XSPIM_CfgTypeDef cfg = {0};
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

Why move this?

return -EIO;
}
}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#endif
#endif /* DT_CLOCKS_HAS_NAME(STM32_XSPI_NODE, xspi_mgr) */

Comment on lines +424 to +430
#if DT_CLOCKS_HAS_NAME(STM32_XSPI_NODE, xspi_mgr)
.MaxTran = 0U, /* Communication regulation (STM32H7RS only) */
#endif
.Refresh = 0x81U,
.MemorySelect = HAL_XSPI_CSSEL_NCS1,
#if DT_CLOCKS_HAS_NAME(STM32_XSPI_NODE, xspi_mgr)
.MemorySelect = HAL_XSPI_CSSEL_NCS1, /* Chip select (STM32H7RS only) */
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

Regroup both under same check:

			.Refresh = 0x81U,
#if DT_CLOCKS_HAS_NAME(STM32_XSPI_NODE, xspi_mgr)
			.MaxTran = 0U,  /* Communication regulation (STM32H7RS only) */
			.MemorySelect = HAL_XSPI_CSSEL_NCS1,  /* Chip select (STM32H7RS only) */
#endif

+ if series-specific, it should be under #if SOC_SERIES - either change condition or comment

@Ayushkothari96
Copy link
Author

Ayushkothari96 commented Nov 20, 2025

Thanks but a similar PR was already opended last week: #99194 Would you mind have a look at this one first ?

Hello @erwango
Thanks for the heads-up on PR #99194. I've reviewed it - both fix the compilation, but they differ fundamentally:

PR #99194: Adds #ifdef guards in the driver using HAL macro detection

My PR: Fixes the root cause by adding XSPI1→OCTOSPI1 compatibility macros in hal_stm32 (PR #328) + uses DT-based detection (DT_CLOCKS_HAS_NAME)
I can add the build test from #99194 if that would help, but the fundamental approach here prevents this issue ecosystem-wide rather than patching one driver

@erwango
Copy link
Member

erwango commented Nov 20, 2025

My PR: Fixes the root cause by adding XSPI1→OCTOSPI1 compatibility macros in hal_stm32 (PR #328) + uses DT-based detection (DT_CLOCKS_HAS_NAME)

While I agree it seems cleaner, I prefer the other one. Indeed we avoid patching the HAL to stay closer to upstream and avoid maintaining downstream patches. Unless ... the patch you propose could be accepted in https://github.com/STMicroelectronics/STM32CubeH5

@Ayushkothari96
Copy link
Author

My PR: Fixes the root cause by adding XSPI1→OCTOSPI1 compatibility macros in hal_stm32 (PR #328) + uses DT-based detection (DT_CLOCKS_HAS_NAME)

While I agree it seems cleaner, I prefer the other one. Indeed we avoid patching the HAL to stay closer to upstream and avoid maintaining downstream patches. Unless ... the patch you propose could be accepted in https://github.com/STMicroelectronics/STM32CubeH5

Fair point about staying clos to upstream. However, stm32_hal_legacy.h exists specifically for these compatibility cases, it has already contains similar macros for other STM32 series.
Anyways would you want me to wait for upstream STM32CubeH5 acceptance before proceeding?

@erwango
Copy link
Member

erwango commented Nov 20, 2025

it has already contains similar macros for other STM32 series.

Agreed, but it remains upstream code nonetheless.

Anyways would you want me to wait for upstream STM32CubeH5 acceptance before proceeding?

Yes. But note that it can take some time before getting a response, so I propose we don't block #99194 and get back to it if your proposal is accepted.

@Ayushkothari96
Copy link
Author

it has already contains similar macros for other STM32 series.

Agreed, but it remains upstream code nonetheless.

Anyways would you want me to wait for upstream STM32CubeH5 acceptance before proceeding?

Yes. But note that it can take some time before getting a response, so I propose we don't block #99194 and get back to it if your proposal is accepted.

Understood, I'll propose the compatibility macros to STMicroelectronics/STM32CubeH5 and can revisit this approach if they accept it. In the meantime, I'm fine with proceeding with #99194 to unblock users. Thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

STM32 XSPI PSRAM driver does not compile for STM32H573

5 participants