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 65 commits into
base: dt
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
0c7805b
rust: zephyr-build: Conversion of Device Tree
d3zd3z Jul 18, 2024
9475bfa
Create blinky application
d3zd3z Sep 16, 2024
77ecd5c
zephyr: sys: Create wrappers for gpio and flash
d3zd3z Sep 16, 2024
ca898a2
zephyr: sys: gpio: Add is_ready method
d3zd3z Sep 16, 2024
fc2b384
zephyr: sys: gpio: Add configure and pin toggle
d3zd3z Sep 16, 2024
4fc314a
Eliminate warnings in the auto-generated device tree
d3zd3z Oct 16, 2024
0a87815
zephyr-build: YAML-based augment code
d3zd3z Oct 18, 2024
644378c
zephyr: Add unsafe constructor to device types
d3zd3z Oct 18, 2024
6741961
zephyr: Enforce uniqueness on device tree wrappers
d3zd3z Oct 18, 2024
09b99bf
zephyr: Make gpio interface much more "unsafe"
d3zd3z Oct 18, 2024
d1ed7b6
zephyr-build: devicetree: Move augments into module
d3zd3z Oct 18, 2024
e071963
zephyr-build: Include DT node presence configs
d3zd3z Oct 18, 2024
3bda0a6
samples: blinky: Domonstrate conditional DT compilation
d3zd3z Oct 18, 2024
3ded677
zephyr: device: Split gpio and flash to own files
d3zd3z Oct 18, 2024
ed49246
platforms: Remove mps2
d3zd3z Oct 25, 2024
15dae9b
zephyr: gpio/flash: Allow constructor to be unused
d3zd3z Oct 29, 2024
c250afb
dt-rust: Add the nrf51 flash controller
d3zd3z Oct 29, 2024
c0bf963
zephyr-sys: Bump to newer bindgen
d3zd3z Oct 29, 2024
1a482f8
zephyr-build: Use parsed DT for both uses
d3zd3z Oct 29, 2024
31b5e49
samples: blink: Fix for upstream API changes
d3zd3z Nov 22, 2024
97200c1
zephyr: sys: device: gpio: Allow Gpio::new to be unused
d3zd3z Dec 18, 2024
69fe79e
Various cleanups from github comments
d3zd3z Dec 18, 2024
f59ecdd
zephyr: device: Use AtomicBool instead of AtomicUsize
d3zd3z Dec 18, 2024
4469875
Numerous minor fixes from github reviews
d3zd3z Dec 18, 2024
0c6f939
CMakeLists: Add missing decls added to doc generation
d3zd3z Dec 18, 2024
1289301
zephyr: device: gpio: Add Send to Gpio and GpioPin
d3zd3z Dec 30, 2024
8173b0e
zephyr-build: devicetree: Clean up compatable check
d3zd3z Jan 14, 2025
2460e09
samples: blinky: Minor cleanups
d3zd3z Jan 14, 2025
baffb4f
samples: blink: Update dependency version
d3zd3z Jan 14, 2025
1143ae9
samples: blinky: Remove user thread test
d3zd3z Jan 14, 2025
6382f57
cmake: Consistent expansion of include path
d3zd3z Dec 27, 2024
805156e
zephyr: Add initial implementation of work queue and async support
d3zd3z Jan 7, 2025
c89b2b1
zephyr: work: Support showing work in Segger SystemView
d3zd3z Jan 7, 2025
2a9c50a
samples: work-philosophers
d3zd3z Jan 7, 2025
3745c12
zephyr: Add numerous missing conditionals around alloc
d3zd3z Jan 7, 2025
a148a8c
zephyr: work: Create ContextExt
d3zd3z Jan 7, 2025
efe18d5
zephyr: work: Fix broken doc links
d3zd3z Jan 8, 2025
d8295cf
zephyr: work: futures: Ensure JoinHandle is Send when it can be
d3zd3z Jan 10, 2025
82b5e12
samples: work-philosphers: Async Semaphore based solution
d3zd3z Jan 10, 2025
28b3465
TMP: Non working hand philosopher demo
d3zd3z Jan 11, 2025
85fe22d
zephyr: work/kio: Timeout support for work
d3zd3z Jan 11, 2025
76ca352
zephyr: device: Add uart
d3zd3z Oct 18, 2024
7a209c8
zephyr: device: Add led-strip wrapper
d3zd3z Oct 18, 2024
d93ab30
zephyr: device: gpio: Add a few more methods
d3zd3z Oct 18, 2024
32573c2
hack: Add DT augment for a bbq-kbd-matrix
d3zd3z Oct 18, 2024
13ed0e6
zephyr: Add Leds driver
d3zd3z Oct 19, 2024
f67bc90
zephyr-build: Fix dt node names
d3zd3z Oct 19, 2024
9dd2530
zephyr-sys: Bring thread_analyzer symbols in if selected
d3zd3z Oct 22, 2024
0adff73
zephyr-sys: Add bindings for usb/hid
d3zd3z Oct 29, 2024
6df0d15
zephyr: device: uart: Add IRQ mode
d3zd3z Oct 31, 2024
0134ba3
zephyr: device: uart: Implement a blocking transmit
d3zd3z Oct 31, 2024
7b048a3
zephyr: device: uart: Add `is_dtr_set` convenience
d3zd3z Nov 1, 2024
1a4ef5a
zephyr: device: uart: Add timeout to 'write' entry
d3zd3z Nov 1, 2024
f889d60
zephyr: device: uart: Fix lifetimes of 'inner' method on UartIrq
d3zd3z Nov 1, 2024
6462a0b
zephyr: device: uart: Remove redundant 'device' field
d3zd3z Nov 2, 2024
0899f67
zephyr: device: uart: Move UartIrq to own crate
d3zd3z Nov 2, 2024
673c5c5
zephyr: device: uart: wip add ring size parameters
d3zd3z Nov 2, 2024
65c77e8
zephyr: device: uart: irq: Replace write with async API
d3zd3z Nov 2, 2024
e8c2a2f
zephyr: device: uart: irq: panic on drop
d3zd3z Nov 2, 2024
d745eb8
zephyr: device: uart: irq: Implement async read
d3zd3z Nov 2, 2024
4eea74c
zephyr: device: uart: irq: Add accessors for async read
d3zd3z Nov 2, 2024
389948d
zephyr: uart: Conditionalize irq handling for alloc
d3zd3z Nov 19, 2024
0480107
zephyr-build: All augments for gpios
d3zd3z Oct 18, 2024
415ffed
build: devicetree: augment: Fix method rename from 'main'
d3zd3z Jan 16, 2025
4c85315
zephyr: SEGGER: Remove extraneous line
d3zd3z Jan 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

set(RUST_MODULE_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "")

# Initially, we just have a single DT augment file.
set(DT_AUGMENTS "${CMAKE_CURRENT_LIST_DIR}/dt-rust.yaml" CACHE INTERNAL "")

# Zephyr targets are defined through Kconfig. We need to map these to
# an appropriate llvm target triple. This sets `RUST_TARGET` in the
# parent scope, or an error if the target is not yet supported by
Expand Down Expand Up @@ -54,7 +57,7 @@ endfunction()
function(get_include_dirs target dirs)
get_target_property(include_dirs ${target} INTERFACE_INCLUDE_DIRECTORIES)
if(include_dirs)
set(${dirs} ${include_dirs} PARENT_SCOPE)
set(${dirs} "${include_dirs}" PARENT_SCOPE)
else()
set(${dirs} "" PARENT_SCOPE)
endif()
Expand Down Expand Up @@ -138,6 +141,8 @@ ZEPHYR_DTS = \"${ZEPHYR_DTS}\"
INCLUDE_DIRS = \"${include_dirs}\"
INCLUDE_DEFINES = \"${include_defines}\"
WRAPPER_FILE = \"${WRAPPER_FILE}\"
BINARY_DIR_INCLUDE_GENERATED = \"${BINARY_DIR_INCLUDE_GENERATED}\"
DT_AUGMENTS = \"${DT_AUGMENTS}\"

[patch.crates-io]
${config_paths}
Expand All @@ -161,6 +166,8 @@ ${config_paths}
INCLUDE_DIRS="${include_dirs}"
INCLUDE_DEFINES="${include_defines}"
WRAPPER_FILE="${WRAPPER_FILE}"
DT_AUGMENTS="${DT_AUGMENTS}"
BINARY_DIR_INCLUDE_GENERATED="${BINARY_DIR_INCLUDE_GENERATED}"
cargo build
${rust_build_type_arg}

Expand Down Expand Up @@ -201,6 +208,8 @@ ${config_paths}
INCLUDE_DIRS="${include_dirs}"
INCLUDE_DEFINES="${include_defines}"
WRAPPER_FILE="${WRAPPER_FILE}"
DT_AUGMENTS="${DT_AUGMENTS}"
BINARY_DIR_INCLUDE_GENERATED="${BINARY_DIR_INCLUDE_GENERATED}"
cargo doc
${rust_build_type_arg}

Expand Down
173 changes: 173 additions & 0 deletions dt-rust.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# Description of how to augment the devicetree for Rust.
#
# Each entry describes an augmentation that will be added to matching nodes in the device tree.
# The full syntax is described (indirectly) in `zephyr-build/src/devicetree/config.rs`.

# Gpio controllers match for every node that has a `gpio-controller` property. This is one of the
# few instances were we can actually just match on a property.
- name: gpio-controller
rules:
- type: has_prop
value: gpio-controller
actions:
- type: instance
value:
raw:
type: myself
value:
args: []
device: crate::device::gpio::Gpio

# The gpio-leds node will have #children nodes describing each led. We'll match on the parent
# having this compatible property. The nodes themselves are built out of the properties associated
# with each gpio.
- name: gpio-leds
rules:
- type: compatible
value:
names:
- gpio-leds
level: 1
actions:
- type: instance
value:
raw:
type: phandle
value: gpios
device: crate::device::gpio::GpioPin

# Flash controllers don't have any particular property to identify them, so we need a list of
# compatible values that should match.
- name: flash-controller
rules:
- type: compatible
value:
names:
- "nordic,nrf52-flash-controller"
- "nordic,nrf51-flash-controller"
- "raspberrypi,pico-flash-controller"
level: 0
actions:
- type: instance
value:
raw:
type: myself
value:
args: []
device: crate::device::flash::FlashController

# Flash partitions exist as children of a node compatible with "soc-nv-flash" that itself is a child
# of the controller itself.
# TODO: Get the write and erase property from the DT if present.
- name: flash-partition
rules:
- type: compatible
value:
names:
- "fixed-partitions"
level: 1
- type: compatible
value:
names:
- "soc-nv-flash"
level: 2
actions:
- type: instance
value:
raw:
type: parent
value:
level: 3
args:
- type: reg
device: "crate::device::flash::FlashPartition"

# Uart devices. This just has to be a list of devices that implement this interface.
- name: uart
rules:
- type: compatible
value:
names:
- "arm,pl011"
# The nordic driver needs to be separate because they have a separate Kconfig for each uart
# block.
# - "nordic,nrf-uarte"
- "zephyr,cdc-acm-uart"
level: 0
actions:
- type: instance
value:
raw:
type: myself
value:
args: []
device: "crate::device::uart::Uart"
kconfig: CONFIG_SERIAL

- name: led-strip
rules:
- type: or
value:
- type: compatible
value:
names:
- "worldsemi,wd2812-spi"
level: 0
- type: compatible
value:
names:
- "worldsemi,ws2812-rpi_pico-pio"
level: 1
actions:
- type: instance
value:
raw:
type: myself
value:
args: []
device: "crate::device::led_strip::LedStrip"
kconfig: CONFIG_LED_STRIP

- name: pwm-leds
rules:
- type: compatible
value:
names:
- "pwm-leds"
level: 0
actions:
- type: instance
value:
raw:
type: myself
value:
args:
- type: child_count
device: "crate::device::led::Leds"

# This doesn't really belong here, and can be moved once we support modules having their own augment
# files.
- name: bbq-kbd-matrix
rules:
- type: compatible
value:
names:
- "bbq-kbd-matrix"
level: 0
actions:
- type: gpio_pins
value:
property: "row-gpios"
getter: "get_rows"
- type: gpio_pins
value:
property: "col-gpios"
getter: "get_cols"

# Generate a pseudo node that matches all of the labels across the tree with their nodes.
- name: labels
rules:
- type: root
actions:
- type: labels

2 changes: 0 additions & 2 deletions etc/platforms.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
-p mps2/an385
-p mps2/an521/cpu0
-p qemu_cortex_m0
-p qemu_cortex_m3
-p qemu_riscv32
Expand Down
7 changes: 7 additions & 0 deletions samples/blinky/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0 OR MIT

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blinky)

rust_cargo_application()
20 changes: 20 additions & 0 deletions samples/blinky/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2024 Linaro LTD
# SPDX-License-Identifier: Apache-2.0

[package]
# This must be rustapp for now.
name = "rustapp"
version = "0.1.0"
edition = "2021"
description = "Blink an LED forever using the GPIO API"
license = "Apache-2.0 OR MIT"

[lib]
crate-type = ["staticlib"]

[dependencies]
zephyr = "0.1.0"
log = "0.4.22"

[build-dependencies]
zephyr-build = "0.1.0"
97 changes: 97 additions & 0 deletions samples/blinky/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
.. zephyr:code-sample:: blinky
:name: Blinky
:relevant-api: gpio_interface

Blink an LED forever using the GPIO API.

Overview
********

The Blinky sample blinks an LED forever using the :ref:`GPIO API <gpio_api>`.

The source code shows how to:

#. Get a pin specification from the :ref:`devicetree <dt-guide>` as a
:c:struct:`gpio_dt_spec`
#. Configure the GPIO pin as an output
#. Toggle the pin forever

See :zephyr:code-sample:`pwm-blinky` for a similar sample that uses the PWM API instead.

.. _blinky-sample-requirements:

Requirements
************

Your board must:

#. Have an LED connected via a GPIO pin (these are called "User LEDs" on many of
Zephyr's :ref:`boards`).
#. Have the LED configured using the ``led0`` devicetree alias.

Building and Running
********************

Build and flash Blinky as follows, changing ``reel_board`` for your board:

.. zephyr-app-commands::
:zephyr-app: samples/basic/blinky
:board: reel_board
:goals: build flash
:compact:

After flashing, the LED starts to blink and messages with the current LED state
are printed on the console. If a runtime error occurs, the sample exits without
printing to the console.

Build errors
************

You will see a build error at the source code line defining the ``struct
gpio_dt_spec led`` variable if you try to build Blinky for an unsupported
board.

On GCC-based toolchains, the error looks like this:

.. code-block:: none

error: '__device_dts_ord_DT_N_ALIAS_led_P_gpios_IDX_0_PH_ORD' undeclared here (not in a function)

Adding board support
********************

To add support for your board, add something like this to your devicetree:

.. code-block:: DTS

/ {
aliases {
led0 = &myled0;
};

leds {
compatible = "gpio-leds";
myled0: led_0 {
gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
};
};
};

The above sets your board's ``led0`` alias to use pin 13 on GPIO controller
``gpio0``. The pin flags :c:macro:`GPIO_ACTIVE_HIGH` mean the LED is on when
the pin is set to its high state, and off when the pin is in its low state.

Tips:

- See :dtcompatible:`gpio-leds` for more information on defining GPIO-based LEDs
in devicetree.

- If you're not sure what to do, check the devicetrees for supported boards which
use the same SoC as your target. See :ref:`get-devicetree-outputs` for details.

- See :zephyr_file:`include/zephyr/dt-bindings/gpio/gpio.h` for the flags you can use
in devicetree.

- If the LED is built in to your board hardware, the alias should be defined in
your :ref:`BOARD.dts file <devicetree-in-out-files>`. Otherwise, you can
define one in a :ref:`devicetree overlay <set-devicetree-overlays>`.
9 changes: 9 additions & 0 deletions samples/blinky/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() {
// This call will make make config entries available in the code for every device tree node, to
// allow conditional compilation based on whether it is present in the device tree.
// For example, it will be possible to have:
// ```rust
// #[cfg(dt = "aliases::led0")]
// ```
zephyr_build::dt_cfgs();
}
10 changes: 10 additions & 0 deletions samples/blinky/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CONFIG_GPIO=y

CONFIG_RUST=y
CONFIG_RUST_ALLOC=y

CONFIG_DEBUG=y
CONFIG_MAIN_STACK_SIZE=8192

# Verify that userspace builds work.
# CONFIG_USERSPACE=y
13 changes: 13 additions & 0 deletions samples/blinky/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# See doc/develop/test/twister.rst for what is here.
sample:
name: Blinky Sample
tests:
sample.basic.blinky:
tags:
- LED
- gpio
filter: dt_enabled_alias_with_parent_compat("led0", "gpio-leds")
depends_on: gpio
harness: led
integration_platforms:
- frdm_k64f
Loading