Skip to content

Samples: Introduce device_deinit() sample #89927

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

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

Conversation

bjarki-andreasen
Copy link
Contributor

@bjarki-andreasen bjarki-andreasen commented May 14, 2025

Introduce sample which switches ownership of a pin between a GPIO controller and a SPI controller driver, specifically the CS. This specific usecase has been requested before to be able to monitor the CS pin using GPIO to then wake up and switch to SPI when asserted. Its also a generally useful and portable sample of device_deinit(), device_init() and deferred init.

With this, the SPI API is updated with new headers for the _DEINIT variants of SPI_DEVICE_DT_DEFINE, and select SPI device drivers are updated to implement device deinit, drivers for the nRF SPI, nRF SPIM, and NXP LPSPI

@bjarki-andreasen bjarki-andreasen changed the title Samples: Introduce device_deinit() sample Samples: Introduce device_deinit() sample May 14, 2025
@bjarki-andreasen bjarki-andreasen marked this pull request as ready for review May 14, 2025 09:16
@github-actions github-actions bot added area: Samples Samples area: SPI SPI bus platform: nRF Nordic nRFx platform: NXP Drivers NXP Semiconductors, drivers area: MFD labels May 14, 2025
@bjarki-andreasen
Copy link
Contributor Author

Note I'm all for chaning the name of the sample and its location :)

Add DEVICE_DEINIT variants of SPI_DEVICE_ macros. These include

- SPI_DEVICE_DT_DEINIT_DEFINE()
- SPI_DEVICE_DT_INST_DEINIT_DEFINE()

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
Implement device deinit fn for nrf spim device driver.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
Implement deinit op in spi_nrfx_spi.c

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
Introduce clearirqhandler which clears the irq handler of a
peripheral. Useful for device_deinit().

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
Implement deinit op for NXP LPSPI device driver.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
Introduce device deinit sample which demonstrates how to share
hardware resources between application and device drivers using
device_deinit(), specifically between a GPIO port and SPI
peripheral.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
Copy link

@carlescufi carlescufi added the DNM This PR should not be merged (Do Not Merge) label Jun 12, 2025
Copy link
Contributor

@kartben kartben left a comment

Choose a reason for hiding this comment

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

very very cool!

*/

&pinctrl {
lpuart0_default: lpuart0_default {
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
lpuart0_default: lpuart0_default {
lpspi0_default: lpspi0_default {

};

&lpspi0 {
pinctrl-0 = <&lpuart0_default>;
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
pinctrl-0 = <&lpuart0_default>;
pinctrl-0 = <&lpspi0_default>;

Comment on lines +1 to +4
.. zephyr:code-sample:: deinit
:name: Device deinit
:relevant-api: gpio_interface spi_interface device_model

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
.. zephyr:code-sample:: deinit
:name: Device deinit
:relevant-api: gpio_interface spi_interface device_model
.. zephyr:code-sample:: deinit
:name: Device deinitialization
:relevant-api: gpio_interface spi_interface device_model
Initialize and deinitialize device drivers at runtime.


static int spi_nrfx_deinit(const struct device *dev)
{
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that when CONFIG_PM_DEVICE=n this function should actually call spim_suspend(dev);

Copy link
Contributor

Choose a reason for hiding this comment

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

@kl-cruz FYI

Copy link
Contributor Author

Choose a reason for hiding this comment

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

100% agree, this PR is a bit out of date, see

static int spi_nrfx_deinit(const struct device *dev)
{
#if defined(CONFIG_PM_DEVICE)
enum pm_device_state state;
/*
* PM must have suspended the device before driver can
* be deinitialized
*/
(void)pm_device_state_get(dev, &state);
return state == PM_DEVICE_STATE_SUSPENDED ||
state == PM_DEVICE_STATE_OFF ?
0 : -EBUSY;
#else
/* PM suspend implementation does everything we need */
spim_suspend(dev);
#endif
return 0;
}

@kartben kartben added this to the v4.3.0 milestone Jul 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: MFD area: Samples Samples area: SPI SPI bus DNM This PR should not be merged (Do Not Merge) platform: nRF Nordic nRFx platform: NXP Drivers NXP Semiconductors, drivers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants