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 48 commits into
base: dt
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
926d801
Add primitive printk-based logging
d3zd3z Sep 16, 2024
909b9cc
rust: zephyr-build: Conversion of Device Tree
d3zd3z Jul 18, 2024
6677efd
Create blinky application
d3zd3z Sep 16, 2024
ad4c847
zephyr: sys: Create wrappers for gpio and flash
d3zd3z Sep 16, 2024
8c04ce7
zephyr: sys: gpio: Add is_ready method
d3zd3z Sep 16, 2024
7d22c64
zephyr: sys: gpio: Add configure and pin toggle
d3zd3z Sep 16, 2024
196f205
Eliminate warnings in the auto-generated device tree
d3zd3z Oct 16, 2024
0c05c14
zephyr: Fill in some missing doc comments
d3zd3z Oct 16, 2024
4fc07d4
zephyr-build: YAML-based augment code
d3zd3z Oct 18, 2024
ec27e6f
zephyr: Add unsafe constructor to device types
d3zd3z Oct 18, 2024
588f2d0
zephyr: Enforce uniqueness on device tree wrappers
d3zd3z Oct 18, 2024
87e4519
zephyr: Make gpio interface much more "unsafe"
d3zd3z Oct 18, 2024
57717db
samples: blinky: Use the newly unsafe gpio calls
d3zd3z Oct 18, 2024
3e7f428
zephyr-build: devicetree: Move augments into module
d3zd3z Oct 18, 2024
3e288e5
zephyr-build: Include DT node presence configs
d3zd3z Oct 18, 2024
7adb807
samples: blinky: Domonstrate conditional DT compilation
d3zd3z Oct 18, 2024
ccc689c
zephyr: device: Split gpio and flash to own files
d3zd3z Oct 18, 2024
9f25f08
platforms: Remove mps2
d3zd3z Oct 25, 2024
bfd3fe1
zephyr: Fix unused_imports warnings with no alloc
d3zd3z Oct 29, 2024
1715b23
zephyr: gpio/flash: Allow constructor to be unused
d3zd3z Oct 29, 2024
2b627c5
dt-rust: Add the nrf51 flash controller
d3zd3z Oct 29, 2024
143c59e
zephyr-sys: Bump to newer bindgen
d3zd3z Oct 29, 2024
b56316b
zephyr-build: Use parsed DT for both uses
d3zd3z Oct 29, 2024
aa9e117
zephyr: device: Add uart
d3zd3z Oct 18, 2024
1acce4f
zephyr: device: Add led-strip wrapper
d3zd3z Oct 18, 2024
c5ef641
zephyr: device: gpio: Add a few more methods
d3zd3z Oct 18, 2024
77aec8f
zephyr-build: All augments for gpios
d3zd3z Oct 18, 2024
fe8d035
zephyr: sync: channel: Allow sending of raw messages
d3zd3z Oct 18, 2024
8c685f2
hack: Add DT augment for a bbq-kbd-matrix
d3zd3z Oct 18, 2024
521ae8c
zephyr: Add Leds driver
d3zd3z Oct 19, 2024
5ee07a4
zephyr-build: Fix dt node names
d3zd3z Oct 19, 2024
c246f51
zephyr-sys: Bring thread_analyzer symbols in if selected
d3zd3z Oct 22, 2024
343c044
zephyr-sys: Add bindings for usb/hid
d3zd3z Oct 29, 2024
46f7f3b
zephyr: Move the sync::Mutex into it's own file
d3zd3z Oct 31, 2024
cea439d
zephyr: sync: Implement SpinMutex
d3zd3z Oct 31, 2024
e81c744
zephyr: device: uart: Add IRQ mode
d3zd3z Oct 31, 2024
4985edf
zephyr: device: uart: Implement a blocking transmit
d3zd3z Oct 31, 2024
8c31b7a
zephyr: device: uart: Add `is_dtr_set` convenience
d3zd3z Nov 1, 2024
b91ba47
zephyr: device: uart: Add timeout to 'write' entry
d3zd3z Nov 1, 2024
6213961
zephyr: device: uart: Fix lifetimes of 'inner' method on UartIrq
d3zd3z Nov 1, 2024
2ae7821
zephyr: device: uart: Remove redundant 'device' field
d3zd3z Nov 2, 2024
f98166d
zephyr: device: uart: Move UartIrq to own crate
d3zd3z Nov 2, 2024
12a808d
zephyr: device: uart: wip add ring size parameters
d3zd3z Nov 2, 2024
ea0b84b
zephyr: device: uart: irq: Replace write with async API
d3zd3z Nov 2, 2024
2bfd951
zephyr: device: uart: irq: panic on drop
d3zd3z Nov 2, 2024
492fba6
zephyr: device: uart: irq: Implement async read
d3zd3z Nov 2, 2024
900f6d0
zephyr: device: uart: irq: Add accessors for async read
d3zd3z Nov 2, 2024
e3cc500
cmake: Set the release/debug based on Kconfig
d3zd3z Sep 24, 2024
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
19 changes: 15 additions & 4 deletions 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 @@ -77,8 +80,13 @@ function(rust_cargo_application)

# TODO: Make sure RUSTFLAGS is not set.

# TODO: Let this be configurable, or based on Kconfig debug?
set(RUST_BUILD_TYPE debug)
if(CONFIG_DEBUG)
set(RUST_BUILD_TYPE "debug")
set(rust_build_type_arg "")
else()
set(RUST_BUILD_TYPE "release")
set(rust_build_type_arg "--release")
endif()
set(BUILD_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${RUST_TARGET}/${RUST_BUILD_TYPE}")

set(CARGO_TARGET_DIR "${CMAKE_CURRENT_BINARY_DIR}/rust/target")
Expand Down Expand Up @@ -133,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 @@ -151,9 +161,10 @@ ${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
# TODO: release flag if release build
# --release
${rust_build_type_arg}

# Override the features according to the shield given. For a general case,
# this will need to come from a variable or argument.
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

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 = "A sample hello world application in Rust"
license = "Apache-2.0 or MIT"

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

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

[build-dependencies]
zephyr-build = "3.7.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>`.
3 changes: 3 additions & 0 deletions samples/blinky/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
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
12 changes: 12 additions & 0 deletions samples/blinky/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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