-
Notifications
You must be signed in to change notification settings - Fork 109
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
How drivers API should look like #374
Comments
In general, it should be possible to use Low level APIs can be designed by simply looking at the system call documentation, but I believe that designing a good higher-level API requires some understanding of how that API will be used. E.g. if I tried to design a high-level interface to the touch driver, I would probably create a poor API, because I'm not a user of that interface myself. Therefore, my recommendation is that all high level APIs should be designed by contributors who use that API.
It's definitely possible to virtualize alarms without needing dynamic memory allocation -- the Tock kernel already does so. However, given that |
Having just written my first driver API, I think the "both" option is the closest to ideal. I think there is an additional criterion, relevant for niche/small projects: how easy is it to write the thing? I found the console driver quite tedious to write, and the motivation I might have spent going an extra mile went into writing tests instead. With that in mind, my personal heuristic translates into the following hierarchy:
It favors having a slow/hungry, but friendly and maintainable driver, rather than not having one at all. To summarize: write as high level as makes sense without allocations. If you still need allocations, create a new higher-level crate. |
Is there any sample for this API? |
I am porting over to Tock 2.0 several drivers and I stumbled upon the following questions.
Here are a few of my thoughts to be used as s starting point for this topic.
Definitions
Before I go into details, I'll briefly describe the terminology used.
PinMode::High
to 1 an vice versa). This API style mimics the exact system call interface.Pin
,OutputPin
,InputPin
)API Variants
The question is what kind of level do we want to provide in libtock 2.0? I'm presenting here four possible scenarios:
Low Level Only
Libtock will offer only the low level driver API. This means a more or less 1:1 mapping to the system call interface and transforming the basic data types into meaningful Rust structures. This kind of API is exposed by the #369 (button driver).
Using upcalls would look like:
✅ low memory footprint
✅ no heap allocation
❌ users will have to write a lot of boilerplate
❌ users will have write additional code
High Level Only
Libtock 2.0 will offer only a high level driver API, fully abstracting the actual system call interface. Such an example is provided by #367 (gpio driver). Data types are annotated for better understanding.
Using upcalls would look like (not actually implemented now):
This approach requires heap allocation to at least be able to store a
Vec
of pins and redirect events to each of them.✅ easy to use, less boilerplate code
✅ users are not required to write additional code
❌ most of the API requires heap allocation
❌ larger memory footprint
Low and high level (depending on the driver)
Libtock 2.0 will provide both low and high level APIs, depending on the driver. This is how #367 (gpio driver) is implemented right now. The high level API is used for pin access (read and write) while for handling interrupts only a low level API is exposed.
Another example that comes into mind is the timer driver. If only a low level API is provided, users will not be able to set multiple alarms.
✅ small memory footprint
✔️ easier to use, less boilerplate code
✔️ users are not required to write a lot of additional code
❌ some of the API requires heap allocation
Low level + additional high level API crates libraries
Libtock 2.0 provides out of the box the low level API drivers. These drivers do not use any memory allocation and have a small footprint.
Besides these standard API, libtock 2.0 provides additional crates for high level API. For instance, libtock could provide the
gpio
driver and an additionalpin
driver crate that sits on top ofgpio
. Thepin
driver can than use memory allocation.Simple applications can choose not to include the
pin
crate, thus not requiring any memory allocation and reducing their footprint. More complex applications can include thepin
crate, but at a higher resources cost.The current status
The text was updated successfully, but these errors were encountered: