Replies: 2 comments 1 reply
-
I have asked myself the same thing, but as far as I can tell, there is no verification of any kind (at runtime or compile time) that devices passed to a driver actually belong to that driver. The worst case scenario here is not just a fatal error, but rather completely unpredictable behaviour when some other driver's function pointers are called with completely mismatched arguments and driver data. In practice, I've never had this happen, but it's definitely a footgun. |
Beta Was this translation helpful? Give feedback.
1 reply
-
See also: #24961 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a project that has a HAL to access the low-level peripherals. I am developing a zephyr port and I would like to know if I can know if a specific device tree node is of a certain type at compile time. The types that I am looking for are:
From the yaml file I see that all of the properties are optional except for the gpio controller. So far I can check if the HAL has access to a GPIO peripheral with the following code:
However for the UART and SPI peripheral the properties are optional, which means I cannot verify if the device I get from
DEVICE_DT_GET
is binded to the correct driver type (i.e., spi-controller, uart-controller). From what I have checked so far it seems that each hardware vendor (espressif, nordic,etc) use these device bindings but due to hardware/driver differences it would be difficult to check if a specific device node is of a specific peripheral type by using its node properties.The only option I have found is that instead of at compile time I check if the device is ready -->
device_is_ready()
However, this still doesnt tell me if the device node is of a certain peripheral type.
If I blindly use a device tree node and call a specific zephyr driver API, in the worst case I would get fatal run-time error as the
struct device
which is passed to all of the zephyr APIs, does a typecast to the underlying peripheral API. For example for the UART APIFor example if I pass a
struct device
that is for SPI (instead of UART), the typecast toconst struct uart_driver_api *
would be incorrect and probably fail. In the best case ifapi->config_get
points to NULL then I get a nice return error. However, in the worst case the program will crash due to different function prototypes or jumping to an invalid address (due to the incorrect type cast).For this reason I am trying to detect at compile time if the node I am using is of the correct type before I use the specific zephyr API. This wouldnt be a problem I stick to one vendor as I can check the device node properties but since I want to make it generic and target more zephyr boards I am trying to see if at compile time I can detect this.
If there is any documentation about this or something that I incorrectly stated please let me know :)
Beta Was this translation helpful? Give feedback.
All reactions