arduino_ci icon indicating copy to clipboard operation
arduino_ci copied to clipboard

_BV macro missing in unit test

Open RobTillaart opened this issue 3 years ago • 6 comments

Issue Summary

problem occurred in a unit test where I used the _BV() macro which is normally supported in Arduino sketches. Replacing the test code with a normal shift (1UL << (x)) solved the issue. However people might expect to be able to use the same code constructs as in the sketches / libraries, just after #include "Arduino.h"

RobTillaart avatar Dec 09 '20 17:12 RobTillaart

Is this the use of a documented or undocumented Arduino API? My search suggests that it's not universal:

via avrfreaks.net

as far as I'm aware, _BV is only defined in the avr-libc headers. Extracted from avr/sfr_defs.h in avr-libc:

#define _BV(bit) (1 << (bit))

ianfixes avatar Dec 23 '20 04:12 ianfixes

I'm pretty sure that _BV is not documented Arduino API. As you mentioned, it is made available by avr-libc, and also used by the AVR Arduino core, so it is de facto exposed by Arduino.h on AVR, but not guaranteed on other platforms (some might define it, but you can't really rely on this). I would even say that code should not rely on Arduino.h exposing _BV, even on AVR, and they should just include avr/sfr_defs.h or some other header that includes that directly, but I guess there's too much code that already assumes that the macro is available on AVR.

I would suggest to add an avr/sfr_defs.h in Arduino-ci with this macro, and include that from Arduino.h on AVR only, for compatibility with AVR-specific sketches.

matthijskooijman avatar Dec 23 '20 09:12 matthijskooijman

My sense is to only prioritize this if there is some "official" (Arduino-hosted) documentation explaining it, since there are still a bunch of official functions that need implementation.

ianfixes avatar Dec 24 '20 05:12 ianfixes

low priority indeed, as workaround is simple.

Thoughts

  • maybe get usage out of core Arduino code?
  • check how often it is used?

RobTillaart avatar Dec 24 '20 08:12 RobTillaart

Is the other option here to put it behind some kind of #ifdef? Maybe some more use cases here would sharpen the boundaries of this issue

ianfixes avatar Dec 12 '22 15:12 ianfixes

As this issue has no reactions for 2 yrs the priority is low. For me I try not to use the macro (as macros can have side effects).

That said, I searched the Arduino Libraries section (1.8.19) and the BV macro is used in multiple libs incl SPI and SWSerial. Maybe that is still a point of attention?

RobTillaart avatar Dec 12 '22 21:12 RobTillaart