Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Does the current rust generated flatbuffer code work in no_std? What about no_std + alloc?

If not, what needs to be done to get this to work in a no_std environment? I'd like to use flatbuffers on a microcontroller with Rust, and while I think I will have access to alloc, in general I'd like to avoid alloc whenever possible

You must be logged in to vote

Replies: 1 comment

Comment options

hey @TheButlah, i dont know if you already find the solution, but right now just playing with flatbuffers on rust embedded target, I successfully to run flatbuffer to building and parsing inside STM32F405RG (run on qemu), you can find detailed code in my repo https://github.com/ahsanu123/firmoto/blob/ae9b772c4fa6ac4675f165e728279937c8112663/nostd_playground/src/bin/nostd_firmoto_schema_usage.rs

Rust Code

#![no_main]
#![no_std]

extern crate alloc;

use core::mem::MaybeUninit;
use cortex_m_semihosting::hprintln;
use embedded_alloc::LlffHeap as Heap;
use flatbuffers::FlatBufferBuilder;
use nostd_playground::{
    self as _,
    firmoto_schema_generated::firmoto::{
        Operation, OperationArgs, OperationType, Value, ValueArgs, ValueType,
    },
};

#[global_allocator]
static HEAP: Heap = Heap::empty();

#[cortex_m_rt::entry]
fn main() -> ! {
    const HEAP_SIZE: usize = 4096;
    static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
    unsafe {
        HEAP.init(&raw mut HEAP_MEM as usize, HEAP_SIZE);
    }

    let mut builder = FlatBufferBuilder::with_capacity(2048);

    let op_name = builder.create_string("init_spi");

    let arg_sck = builder.create_string("sck");
    let arg_miso = builder.create_string("miso");
    let arg_mosi = builder.create_string("mosi");

    let arg1 = Value::create(
        &mut builder,
        &ValueArgs {
            name: Some(arg_sck),
            valtype: ValueType::U8,
            value: Some(arg_sck),
        },
    );

    let arg2 = Value::create(
        &mut builder,
        &ValueArgs {
            name: Some(arg_miso),
            valtype: ValueType::U8,
            value: Some(arg_miso),
        },
    );

    let arg3 = Value::create(
        &mut builder,
        &ValueArgs {
            name: Some(arg_mosi),
            valtype: ValueType::U8,
            value: Some(arg_mosi),
        },
    );

    let init_spi_args = builder.create_vector(&[arg1, arg2, arg3]);

    let init_spi = Operation::create(
        &mut builder,
        &OperationArgs {
            name: Some(op_name),
            optype: OperationType::SPI,
            args: Some(init_spi_args),
            reval: None,
        },
    );

    builder.finish(init_spi, None);

    let buf = builder.finished_data();

    let parsed_init_spi = flatbuffers::root::<Operation>(buf).unwrap();

    assert!(parsed_init_spi.name().is_some());
    let parsed_cmd_name = parsed_init_spi.name().unwrap();

    hprintln!("command name: {}", parsed_cmd_name);

    let parsed_args = parsed_init_spi.args().unwrap();

    parsed_args.iter().for_each(|item| {
        let args_name = item.name().unwrap();
        let args_type = item.valtype().0;
        let args_value = item.value().unwrap();

        hprintln!("\t - args name:{}", args_name);
        hprintln!("\t \t * type: {}", args_type);
        hprintln!("\t \t * type: {}", args_value);
    });

    nostd_playground::exit()
}

Qemu Command

  cargo build
  qemu-system-arm \
    -cpu cortex-m4 \
    -machine olimex-stm32-h405 \
    -nographic \
    -semihosting-config enable=on,target=native \
    -kernel ./target/thumbv7em-none-eabi/debug/nostd_firmoto_schema_usage

Output

qemu-system-arm -cpu cortex-m4 -machine olimex-stm32-h405 -nographic -semihosting-config enable=on,target=native -kernel ./target/thumbv7em-none-eabi/debug/nostd_firmoto_schema_usage
command name: init_spi
         - args name:sck
                 * type: 0
                 * type: sck
         - args name:miso
                 * type: 0
                 * type: miso
         - args name:mosi
                 * type: 0
                 * type: mosi
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.