Rahix / avr-device

Register access crate for AVR microcontrollers
Apache License 2.0
170 stars 66 forks source link

RFC: Add statically checked macro for safe struct Peripherals creation #105

Open mbuesch opened 2 years ago

mbuesch commented 2 years ago

The Peripherals::take() function has two disadvantages:

This change uses the linker to statically ensure that only one Peripherals instance is constructed globally. It introduces a new macro to avr_device that is used just like:

let dp = avr_device::peripherals!(atmega328p);

In case of multiple macro invocations, the linker will abort with

multiple definition of `__ERROR__avr_device__peripherals__macro_must_only_be_used_once__'

TODO:

mbuesch commented 2 years ago

As we need support from the svd2rust tool to properly implement this, the macro should probably also be moved into the generated code. The svd2rust could get a new option to select between static and dynamic peripherals struct checking. avr-device could then always request generation of the static macro.

Rahix commented 1 year ago

Hi, sorry, I completely lost track of this. I agree with you, I think this should be discussed with the svd2rust project and integrated there.