zsup / firmware-rust

Firmware libraries for running Rust on Particle devices (feature/rust branch)
https://www.particle.io/
Other
27 stars 5 forks source link

Use bindgen to produce automatic bindings for our C interfaces #6

Open zsup opened 8 years ago

japaric commented 8 years ago

I can look into this. What C interfaces require bindings? And which header files are the entry points, i.e. which headers do you directly include in your application, e.g. #include <hal/foo.h>?

zsup commented 8 years ago

ping @m-mcgowan for comment here

m-mcgowan commented 8 years ago

Hi @japaric! Our C interfaces are those exported as dynamic linked libraries - you can find them by grepping the source for DYNALIB_FN - ecluding dynalib/inc/dynalib.h which is where the macro is defined.

mbp:firmware mat1$ grep -l -r --include=*.h --exclude=./dynalib/inc/dynalib.h DYNALIB_FN  .
./communication/src/communication_dynalib.h
./hal/inc/hal_dynalib.h
./hal/inc/hal_dynalib_can.h
./hal/inc/hal_dynalib_cellular.h
./hal/inc/hal_dynalib_concurrent.h
./hal/inc/hal_dynalib_core.h
./hal/inc/hal_dynalib_gpio.h
./hal/inc/hal_dynalib_i2c.h
./hal/inc/hal_dynalib_ota.h
./hal/inc/hal_dynalib_peripherals.h
./hal/inc/hal_dynalib_socket.h
./hal/inc/hal_dynalib_spi.h
./hal/inc/hal_dynalib_usart.h
./hal/inc/hal_dynalib_wlan.h
./modules/photon/system-part1/inc/wifi_dynalib.h
./modules/shared/stm32f2xx/inc/module_system_part1_init_dynalib.h
./modules/shared/stm32f2xx/inc/user_dynalib.h
./rt-dynalib/inc/rt_dynalib.h
./services/inc/services_dynalib.h
./system/inc/system_dynalib.h
./system/inc/system_dynalib_cloud.h
./system/inc/system_dynalib_net.h

The dynamic linking is taken care of automatically for you by including these header files and defining DYNALIB_IMPORT. For example see https://github.com/spark/firmware/tree/develop/system-dynalib/src. When the dynamic interface is imported, a statically linked stub is created for each C function that dynamically invokes the target function. I'm guessing that bindgen could then create bindings for these statically linked stubs.

japaric commented 8 years ago

I finally got around to work on this. I used bindgen on the $module_hal.h headers instead of the dynalibs ones but I think there's no difference in the produced function signatures. You can see my progress here. So far, I've only generated bindings for the gpio and delay modules then tested these bindings on my photon Cargo project (see japaric/photon#3).

Next I'm going to explore building a "rustic" wiring API on top of these HAL bindings. I plan to generate bindings for the other modules (UART, I2C, etc) as I need them. But, if someone wants to play with the other modules right now, let me know and I'll generate the bindings for you!

cc @dbrgn

dbrgn commented 8 years ago

That's cool. As I said, I'll probably give the high level abstraction a stab next week. This week is very busy.