Skip to content
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

A perpetual WIP PR #22

Draft
wants to merge 42 commits into
base: dt
Choose a base branch
from
Draft

A perpetual WIP PR #22

wants to merge 42 commits into from

Conversation

d3zd3z
Copy link
Collaborator

@d3zd3z d3zd3z commented Oct 18, 2024

This PR holds onto changes that would be considered "work in progress", but can be reviewed. Ideally, this should be based on top of other pending PRs so that the actual change here is kept minimal.

Built upon #15

@d3zd3z d3zd3z changed the base branch from main to dt October 18, 2024 22:21
d3zd3z added a commit to tangybbq/keyboard-firmware that referenced this pull request Oct 18, 2024
Feedback on zephyr-lang-rust has resulted in numerous API changes.  The
good news is that I only have a small number of commits on top of
zephyr-lang-rust to support this keyboard firmware, and all currently
live in: zephyrproject-rtos/zephyr-lang-rust#22
@d3zd3z d3zd3z force-pushed the dt branch 2 times, most recently from 601ced8 to d4754e1 Compare October 22, 2024 18:38
@d3zd3z d3zd3z force-pushed the hack branch 2 times, most recently from 69930a4 to 4985edf Compare November 1, 2024 14:35
@d3zd3z d3zd3z force-pushed the dt branch 2 times, most recently from e640970 to 7e8a847 Compare November 21, 2024 20:27
This code parses the DTS file generated by the Zephyr build, along with a
few entries from the generated header file, to build a representation of
the device tree.  There is a notion of "augments" that add various
methods.  This is currently just hard-coded.

Signed-off-by: David Brown <[email protected]>
Blinky is a rust port of the samples/blinky application from the main
zephyr repo.  It performs the same function, but using the DT and GPIO
abstractions provided in the zephyr::sys module.

Signed-off-by: David Brown <[email protected]>
Create two modules with wrappers for Zephyr gpio and flash devices.
These have no methods, but allow the DT generated code to compile, with
`get_instance` methods that return these values.

Signed-off-by: David Brown <[email protected]>
The is_ready method on both `Gpio` and `GpioPin` will ultimately call
the underlying `device_is_ready` entry.

Signed-off-by: David Brown <[email protected]>
Create wrappers for config and pin toggle on the gpios.  This is enough
to allow the blink app to work.

Signed-off-by: David Brown <[email protected]>
Move the module declaration for the device tree up into `lib.rs` to
allow insertion of allow directives to eliminate documentation warnings
on the generated devicetree.

Signed-off-by: David Brown <[email protected]>
Instead of hardcoding all of the augments in the code, put these
augments into a data structure.  Load the actual rules from a yaml file
(with a future ability to extend this with per-app or per-module defined
augments.

Convert all of the existing hardcoded augment rules into the initial
augment file.
Add constructors to the individual device types.  These are unsafe, and
are all referenced from the generated devicetree code.
Move all of the device implementations into a `zephyr::device` module.

In this module, add a `Unique` type that supports the constructors on
the device requiring a unique instance.

The device tree augmentation code adds a declaration for these
uniqueness markers for each instance, passing it into the constructor.

Signed-off-by: David Brown <[email protected]>
Gpios in Zephyr are inherently unsafe.  There is a shared controller
that is not just used by the pins defined here, but extensively across
various drivers.

As such, make all of the gpio pin operations themselves unsafe.

We can help, a little bit, to at least enforce uniqueness with the
Rust drivers that use gpios by requiring them to take a mutable instance
of `GpioToken`, which has a singleton getter.

Signed-off-by: David Brown <[email protected]>
Prevents a warning on boards where there are no gpios or flash
controllers are defined.

Signed-off-by: David Brown <[email protected]>
This allows a build on the nrf51, preventing an error when the
partitions are detected, but the controller wasn't.

Signed-off-by: David Brown <[email protected]>
This fixes a weird issue with bindgen missing the `__device_dts_ord_nn`
declarations in some circumstances.  It is unclear when this was
occuring, and hopefully it doesn't return at some point.

Signed-off-by: David Brown <[email protected]>
Instead of trying to hand off data through a file between the build of
different crates, which was causing build failures in applications that
need to use cfgs based on the presence of DT nodes, instead, just parse
the DT again, and recalculate the node tree from it.

This should fix build issues with the all nodes txt file missing.

Signed-off-by: David Brown <[email protected]>
Several things have become unsafe, so use those in unsafe blocks.
The GPIO driver now has a token that must be passed to each action, to
enforce single threadded use.

Signed-off-by: David Brown <[email protected]>
Add a simplistic uart wrapper, and drivers around it.

All methods are unsafe.

Signed-off-by: David Brown <[email protected]>
Add a simple wrapper for the led strip driver.

Signed-off-by: David Brown <[email protected]>
Add methods for get and set of pin logical values.

Signed-off-by: David Brown <[email protected]>
This isn't really quite right, as the "unique" values here are per pin,
and if the pin were to be used across the system, nothing would enforce
these to be unique.

Signed-off-by: David Brown <[email protected]>
This is a special keyboard matrix type used by the bbq-keyboard
firmware.  It doesn't belong here, and can be moved out, once we support
additional augment files.

Signed-off-by: David Brown <[email protected]>
Add support for Leds, and a simple augment for the pwm drivers.
Fix the DT node names in the generated `dt_cfgs` entries to match the
names of the nodes.  Need to apply `fix_id` in a few places to make sure
non-identifier characters are converted to underscores.

Signed-off-by: David Brown <[email protected]>
Enable use of the thread analyzer, if it is enabled.

Signed-off-by: David Brown <[email protected]>
Add these bindings so that app code can directly use these.

Signed-off-by: David Brown <[email protected]>
Add an `.into_irq()` to the Uart device that turns it into an interrupt
driven interface.  Currently, read is implemented, with a `.try_read()`
method that will try, with a timeout, to read data from the interface.

The methods are all marked as 'unsafe' currently, until a more thorough
safety analysis can be made.

Signed-off-by: David Brown <[email protected]>
A blocking write allows us to do the write without needing an
intermediate copy of the data.  The thread's buffer is directly copied
into the uart's fifo.

To make this safe, the write call must block until all of the data has
been placed in the fifo.

The uart.h irq interface is a little quirky.  Namely, the uart fifo can
only be accessed from the irq handler.  To make this possible, the
drivers will implement usually a soft interrupt to the irq handler, for
the initial transmission.

There is also an async interface in uart.h, but numerous devices don't
support it.

Signed-off-by: David Brown <[email protected]>
Add a timeout parameter to the 'write' method on the UartIrq interface.
The method now returns the number of bytes that were actually written.

Signed-off-by: David Brown <[email protected]>
This method had a lifetime error, and allowed the inner uart to be
leaked beyond its lifetime.

This should prevent the returned device from outliving the UartIrq, or
even being used after mutable methods on the UartIrq have been used.
For now, don't even return a mutable method until we evaluate what
should be permissible.  This at least allows reading the line status.

In addition, since the return Uart is not owned by the caller, it is not
permissible to call any of the `into_*` methods.

Signed-off-by: David Brown <[email protected]>
With the 'Uart' encapsulated within the `UartIrq`, having a separate
field for device is redundant with the one inside of `Uart`.  Remove
this redundant field, and change the references to it to use the one
inside the Uart.

Signed-off-by: David Brown <[email protected]>
Move this specific type into its own crate.

Signed-off-by: David Brown <[email protected]>
Add ring size parameters to the UartIrq type.  These will later be used
to migrate the interface to an async interface (not rust asyc).

Signed-off-by: David Brown <[email protected]>
Update the writing to be an async interface.  Instead of a blocking
write, there is a `write_enqueue` to add the message, and a `write_wait`
which is a blocking wait for a write to handle.

For this to work, the UartIrq needs to own the buffer for the duration
of the write.  At this point, the buffers are just `Vec<u8>`, but a
future change will make this more general, so that, for example, Pooled
buffers can be used.  The buffer should never be dropped within this
code.

Signed-off-by: David Brown <[email protected]>
Because this interface has registered IRQs, prevent drops.  They are
probably safe due to the use of the leaked Arc, but the data is in fact
leaked.  For now, just cause Drop to panic, as normal use won't ever
drop this.

Signed-off-by: David Brown <[email protected]>
Change the read interface to also be async.  The user must enqueue one
or more buffers for the driver to place data into.

Signed-off-by: David Brown <[email protected]>
The result from the async_write holds the buffer, but the fields are
private.  Provide two accessors to make this result useful:

- as_slice: returns a slice of the useful part of the buffer
- into_inner: Transforms into the buffer, so it can be reused.

Signed-off-by: David Brown <[email protected]>
Currently, the IRQ handling code requires a SpinMutex which is only
available with Arc, which requires allocation.

Disable this use of the driver in this situation.

Signed-off-by: David Brown <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant