Hi! I have a question I was hoping to get some help with! We (https://github.com/bitcraze) make open source'd nano quadcopters. They are sort of flying Arduinos and it is possible to construct decks for them, in the same way you can construct shields for Arduino.
When you do, you write a deck driver. I am attempting to write a deck driver in Rust (this is in STM32/FreeRTOS land). One thing I am having problem getting around is that the interface for deck drivers in our C API is that you fill out a struct DeckDriver
and then call a magic macro DECK_DRIVER
that is defined as (struct deck_driver is typedef'd to DeckDriver):
#define DECK_DRIVER(NAME) const struct deck_driver * driver_##NAME __attribute__((section(".deckDriver." #NAME), used)) = &(NAME)
Putting a pointer to the deck_driver structure at a special place so that the code later can iterate over all registered deck drivers.
So that the basic register driver part of the driver looks like:
static const DeckDriver helloDriver = {
.name = "myHello",
.init = helloInit,
.test = helloTest,
};
DECK_DRIVER(helloDriver);
(Making your first Deck driver | Bitcraze)
Now How would I best interface this to Rust? I've used bindgen to get binding to the DeckDriver structure, how do I make it so a pointer to that structure end up in the .deckDriver section?
This is my naive attempt that does not work (cannot be shared between threads safely):
#[no_mangle]
static example_driver: DeckDriver = DeckDriver {
name: b"RustExampleDriver\0".as_ptr() as *mut u8,
init: Some(rustExampleInit),
test: Some(rustExampleTest),
usedPeriph: 0,
usedGpio: 0,
requiredEstimator: 0,
requiredLowInterferenceRadioMode: false,
vid: 0,
pid: 0,
memoryDef: core::ptr::null(),
};
#[no_mangle]
#[link_section = ".deckDriver"]
static driver_rustExampleDriver: *const DeckDriver = &example_driver;