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

[nrf noup] drivers: i2c_nrfx_twim: add asynchronous write and exclusive access API #2624

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ankuns
Copy link
Contributor

@ankuns ankuns commented Mar 17, 2025

This commit provides an extension to the i2c_nrfx_twim driver. It introduces possibility to:

  • acquire/release exclusive access to the i2c bus
  • perform asynchronous i2c write transfers.

The provided API is necessary for use cases when, i2c transfers must be started from interrupt context, while still allowing to share the i2c bus in multi-tasking environment.

This PR is necessary to implement the temperature compensation feature for the nRF2220 Front-End Module that works with MPSL and still allows to share i2c bus with drivers implementing the ordinary i2c.h API.
This is covered by PR nrfconnect/sdk-nrf#20960

The extension is not welcome in upstream
zephyrproject-rtos/zephyr#86721
It works only for Nordic SoCs, but the MPSL also works only with Nordic SoCs.
Upstream-first approach can't be applied in this case.

I do not encourage anyone to use the proposed extension API, however it is necessary to implement the nRF2220 temperature compensation feature.

Usage:
Will be shortly presented with introduction of the nRF2220 FEM temperature compensation.
From a thread context call i2c_nrfx_twim_exclusive_access_acquire
We have the exclusive access to the i2c bus, which prevents other users of the i2c from messing with the underlying hardware driver.
Then we can perform write transfers over the i2c bus to an i2c slave device, possibly from interrupt context by calling i2c_nrfx_twim_async_transfer_begin.
When we are done we call i2c_nrfx_twim_exclusive_access_release.

The transfers i2c_nrfx_twim_async_transfer_begin for the nRF2220 temperature compensation must happen within a timeslot negotiated with MPSL scheduler.

@nordic-piks
Copy link
Contributor

Please add tests/samples for this new features at driver.

@masz-nordic
Copy link
Contributor

masz-nordic commented Mar 17, 2025

The extension is not welcome in upstream

How so? I do not see change requests, just discussion.

This PR is necessary to implement the temperature compensation feature for the nRF2220 Front-End Module that works with MPSL and still allows to share i2c bus with drivers implementing the ordinary i2c.h API.

Does that mean the TWIM peripheral is used at the same time in Zephyr driver and a baremetal driver?

@ankuns
Copy link
Contributor Author

ankuns commented Mar 18, 2025

@masz-nordic

Does that mean the TWIM peripheral is used at the same time in Zephyr driver and a baremetal driver?

No, the Zephyr driver is used only. The nrf2220 FEM from driver from MPSL does not have it's own i2c driver. It requires to provide functions operating on i2c bus by an interface structure containing function pointers to actual implementation. In NCS these pointers point to a thin wrappers over the i2c driver. The bare metal is not supported, but in case of need such structure pointing to appropriate i2c implementation can be provided in bare metal application.

This approach was enough while nrf2220 needed i2c only at bootup stage, to write initial set of registers. The i2c was not used never again. But the nrf2220 temperature compensation feature is different. The i2c needs to be used also after bootup while device is operating. It needs to be able to perform an i2c write transfer from an interrupt given by the MPSL scheduler (it governs access to the radio and thus the FEM, the transfers may be perfromed while the nrf2220 is in very particular state, "bypass"). So the idea is to first take the exclusive access to the i2c bus, then request MPSL scheduler for a timeslot (yes in this time we prevent other i2c transfers from happening because we hold exclusive access, that's intended). When MPSL scheduler gives us a timeslot we have to perfrom i2c write, without delays (the MPSL scheduler requirement). We need asynchronous write transfers to perform that.

This approach has the benefit that the i2c bus can still be shared with other ordinary Zephyr devices/features following the ordinary zephyr/drivers/i2c.h api (at least for I2C_SHELL feature that is used for testing). The only limitations imposed by the nrf2220 temperature compensation feature are:

  • rarely (when temperature compensation registers are to be written to the nrf2220), we take exclusive access and negotiate mpsl timeslot.
  • we must use i2c_nrfx_twim implementation of the driver, but MPSL works only on nRF devices, so not really a limitation.

@ankuns ankuns force-pushed the nrfx_twim_async_api_noup branch from 4b9bd26 to 840548a Compare March 19, 2025 11:21
@ankuns
Copy link
Contributor Author

ankuns commented Mar 19, 2025

@nordic-piks

Please add tests/samples for this new features at driver.

Added drivers.i2c.nrfx_twim_async_api

@ankuns ankuns force-pushed the nrfx_twim_async_api_noup branch from 840548a to 19f2df8 Compare March 19, 2025 16:04
ankuns added 2 commits March 20, 2025 09:00
…ve access API

This commit provides an extension to the i2c_nrfx_twim driver.
It introduces possibility to:
- acquire/release exclusive access to the i2c bus
- perform asynchronous i2c write transfers.

The provided API is necessary for use cases when, i2c transfers
must be started from interrupt context, while still allowing to
share the i2c bus in multi-tasking environment.

Signed-off-by: Andrzej Kuros <andrzej.kuros@nordicsemi.no>
This commit provides a set of tests for the i2c_nrfx_twim driver
extension.

Signed-off-by: Andrzej Kuros <andrzej.kuros@nordicsemi.no>
@ankuns ankuns force-pushed the nrfx_twim_async_api_noup branch from 19f2df8 to fb830d8 Compare March 20, 2025 08:00
Copy link
Contributor

@carlescufi carlescufi left a comment

Choose a reason for hiding this comment

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

This is too big for a noup, it needs discussion for better integration.

@ankuns
Copy link
Contributor Author

ankuns commented Mar 20, 2025

@carlescufi I agree, that why there is
#2585
and upstream
zephyrproject-rtos/zephyr#86721
Added you to a chat on Teams.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants