Using struct device from outside context in driver #driver #pdm #nrf52832


Frederik David Damsgaard Popp
 

Hello Zephyr Development Community

I am currently working on implementing the Nordic NRFX PDM driver into Zephyr.
My pull request is here if you're interested: https://github.com/zephyrproject-rtos/zephyr/pull/27671

However with this driver, I am not quite sure how to properly gain access to the device struct for the driver, in a function called from the wrong context.

I'll elaborate:
I have several driver functions, which can be accessed from the Application context, and a few for the drivers internal use.
One of the internals is an event_handler, which is called from the nrfx PDM HAL driver module.
Being called from this context, means that it has no knowledge of the device struct.

Just for now, to get it working, I have a global pointer to the device struct, which I obviously would like to avoid, but is working since there can only be one instance of the driver.

I took a look at some other nrfx drivers:
- In the i2c_twi driver, the HAL module was changed to include a pointer to the device.
- In the adc driver, the driver simply uses DEVICE_GET(adc_0), since there is only one instance.

So my question is - which would be the correct way of accessing this device structure from a different context?