Skip to content

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented Sep 28, 2025

  • drivers: smbus: provide packet-error-checking (pec) routines
  • tests: drivers: smbus: add packet error correction (pec) testsuite
  • drivers: smbus: stm32: support packet-error-checking (pec)

@cfriedt cfriedt force-pushed the stm32-smbus-pec-patch branch 3 times, most recently from e22919f to 57a4ac9 Compare October 3, 2025 15:36
@cfriedt cfriedt marked this pull request as ready for review October 3, 2025 15:40
@zephyrbot zephyrbot added area: Tests Issues related to a particular existing or missing test platform: STM32 ST Micro STM32 labels Oct 3, 2025
@cfriedt cfriedt force-pushed the stm32-smbus-pec-patch branch from 57a4ac9 to d34b26a Compare October 3, 2025 15:53
Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

Minor typos in header file inline description comments. Otherwise LGTM.

Comment on lines 124 to 125
* @note This function is intended for SMBus drivers that do not have hardware PEC support and
* therefore must rely on software PEC calculation.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe these should be guarded by a Kconfig symbol then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

Copy link
Member Author

Choose a reason for hiding this comment

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

Woops - I still need to add some ifdefs to smbus_util.c for this

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

*count = msgs[2].len;
ret = smbus_read_check_pec(data->config, periph_addr, msgs, num_msgs);
if (ret < 0) {
return ret;
Copy link
Member Author

Choose a reason for hiding this comment

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

It would be good to investigate capturing error statistics, in this block, in a subsequent PR.

Also mentioned here
https://github.com/zephyrproject-rtos/zephyr/pull/96691/files#r2413732281

etienne-lms
etienne-lms previously approved these changes Oct 8, 2025
mathieuchopstm
mathieuchopstm previously approved these changes Oct 8, 2025
In the case that SMBus hardware does not automatically perform packet
error checking (PEC), provide generic inline functions in
`zephyr/drivers/smbus.h` that can be used by drivers to perform PEC in
software.

Signed-off-by: Chris Friedt <[email protected]>
Signed-off-by: Andrew Lewycky <[email protected]>
Add tests for SMBus packet error correction (PEC). The two primary
utilities tested are

* smbus_pec(): compute the PEC byte for a Block Write
* smbus_read_check_pec(): verify a PEC after
  * Read Byte
  * Read Word
  * Block Read

Signed-off-by: Chris Friedt <[email protected]>
Add support for SMBus packet error checking (PEC) to the stm32 driver.
This feature allows SMBust communication to be slightly more robust in
the presence of noise, in that packet errors can be detected on the
receive side.

Signed-off-by: Andrew Lewycky <[email protected]>
Signed-off-by: Chris Friedt <[email protected]>
@cfriedt cfriedt dismissed stale reviews from mathieuchopstm and etienne-lms via f665447 October 8, 2025 15:14
@cfriedt cfriedt force-pushed the stm32-smbus-pec-patch branch from 3ae7fc6 to f665447 Compare October 8, 2025 15:14
@cfriedt
Copy link
Member Author

cfriedt commented Oct 8, 2025

@etienne-lms @mathieuchopstm - sorry to ask for a re-approval. Just a minor change from the previous commit (to ensure that unnecessary code is not compiled.

The difference is here
https://github.com/zephyrproject-rtos/zephyr/compare/3ae7fc69c9e9cde833d7214404d2ce783ca90fb7..f6654476ccba5e08134f890c3bb19b98c78979e9

Copy link

sonarqubecloud bot commented Oct 8, 2025

SMBus device driver initialization priority.

config SMBUS_SOFT_PEC
bool "SMBus software PEC support"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this option configurable by the project? It seems rather a driver private config.
... hmmm, i see at least the test sample needs to enable it.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's configurable by the project. Technically could be used by any driver or other code.

@mathieuchopstm
Copy link
Contributor

@etienne-lms @mathieuchopstm - sorry to ask for a re-approval. Just a minor change from the previous commit (to ensure that unnecessary code is not compiled.

The difference is here https://github.com/zephyrproject-rtos/zephyr/compare/3ae7fc69c9e9cde833d7214404d2ce783ca90fb7..f6654476ccba5e08134f890c3bb19b98c78979e9

No problem - as a matter of fact, I was wondering if this wasn't missing when approving 😉

@erwango
Copy link
Member

erwango commented Oct 9, 2025

@benediktibk Would you have time to have a look ?

depends on DT_HAS_ST_STM32_SMBUS_ENABLED
depends on I2C_STM32
select PINCTRL
select SMBUS_SOFT_PEC
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose there are two ways to look at this:

  • PEC is a feature, which is implemented in the STM32 SMBus driver
  • PEC is a capability which is required by the SMBus driver

With the select here it is more the latter one, although it should actually be the first one? But this then would also mean that everything has to be ifdef-ed in the driver itself, so I'm not sure if it is actually a good idea.

Copy link
Contributor

Choose a reason for hiding this comment

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

Slowly I start to understand it, you actually need this config-flag so that the utility functions for PEC (which themselves require CRC) are only pulled in when necessary?

Copy link
Member Author

@cfriedt cfriedt Oct 9, 2025

Choose a reason for hiding this comment

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

With the select here it is more the latter one, although it should actually be the first one?

"Should" is potentially debatable. E.g. intel_pch_smbus.c implicitly supports PEC.

if (auxs & PCH_SMBUS_AUXS_CRC_ERROR) {

The stm32 driver is the only other smbus driver in-tree.

It's almost completely platform-independent, actually, with the exception of i2c_stm32_smbalert_enable(), i2c_stm32_smbalert_disable(), and i2c_stm32_set_smbus_mode().

Personally, I would feel better if there was a uniform API, so that PEC can at least be configured by the application, but it isn't disabled by default here (the Zephyr convention), so I can see your point.

everything has to be ifdef-ed in the driver itself, so I'm not sure if it is actually a good idea.

SMBUS_SOFT_PEC could use depends on CRC instead of select, and the stm32 driver could use conditional compilation throughout.

I'm ok to go either way.

@benediktibk , @erwango , @etienne-lms - do you have preferences?

Copy link
Contributor

Choose a reason for hiding this comment

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

everything has to be ifdef-ed in the driver itself, so I'm not sure if it is actually a good idea.

SMBUS_SOFT_PEC could use depends on CRC instead of select, and the stm32 driver could use conditional compilation throughout.

I'm ok to go either way.

I'd go for SMBUS_SOFT_PEC select'ing CRC and the consumer drivers select'ing SMBUS_SOFT_PEC as done in PR right now. This avoids duplicating the select CRC in every driver's Kconfig and is just more sensical IMO.

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

Labels

area: SMBus area: Tests Issues related to a particular existing or missing test platform: STM32 ST Micro STM32

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants