Re: cannot find "__device_[device name]" #api


Bolivar, Marti
 

Hi Justin,

"Justin Huang via Lists.Zephyrproject.Org"
<justin.y.huang=live.com@lists.zephyrproject.org> writes:

Hi,

I am building a device driver for Zephyr and when calling the macro, DEVICE_GET(), like DEVICE_GET(my_dev), I get a build error complaining about undefined __device_my_dev.
I tried to locate where the variable is assembled during the build process but have no luck so far.
DEVICE_GET() might not be the right thing here. Are you trying to get a
pointer to your device from the device driver C file itself, or from
some other file?

If you're trying to get a pointer to your device from some application
source code, you should probably be using device_get_binding() instead of
DEVICE_GET().


I have placed the following in the driver source code:

DEVICE_INIT(my_dev, CONFIG_MYDEV_NAME, my_dev_init,
my_dev_device_ctrl, NULL,
APPLICATION, CONFIG_MYDEV_INIT_PRIORITY);

Could you please shed some light on where to look for what might go
wrong?
If (and only if) the DEVICE_GET() line is in the same file as the above
DEVICE_INIT(), then it should work properly when paired with
DEVICE_DECLARE(), as shown with example code in this documentation page:

https://docs.zephyrproject.org/latest/reference/drivers/index.html#single-driver-multiple-instances

However, note that DEVICE_INIT allocates a static struct device, so you
can't use DEVICE_GET() to access that device from another file.

If you're trying to get a pointer to your struct device from another
C file, you need to use something like this instead:

struct device *mydev = device_get_binding(CONFIG_MYDEV_NAME);

BR,
Martí


Thanks,
Justin

Join users@lists.zephyrproject.org to automatically receive all group messages.