rogerclarkmelbourne / Arduino_STM32

Arduino STM32. Hardware files to support STM32 boards, on Arduino IDE 1.8.x including LeafLabs Maple and other generic STM32F103 boards
Other
2.53k stars 1.26k forks source link

Missing _BV definition #121

Closed sweetlilmre closed 8 years ago

sweetlilmre commented 9 years ago

Hardly a burning issue but _BV seems to be undefined in the default set of includes and I couldn't seem to find it elsewhere. I'm trying to maintain a 'portable' set of code between a Maple Mini and Arduino Nano platform and this is a minor inconvenience. As this is a fairly common macro, perhaps it should be included somewhere?

sweetlilmre commented 9 years ago

More on this. Seems its a known Due / ARM etc. core issue as described here: https://github.com/arduino/Arduino/issues/1834

No movement on this since mid 2014 :(

rogerclarkmelbourne commented 9 years ago

What does it do ?

I we can probably add it into one of the headers.

We have had to add various other macro's etc, as the original codebase we use is based on Arduino 0022, and as there is no overall API spec for the Arduino API, we only tend to know about omissions when people try to use things.

phenom128 commented 9 years ago

from atmel http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__avr__sfr_1ga11643f271076024c395a93800b3d9546.html

define _BV(bit) \

(1 << (bit))

include <avr/io.h>

On 22 October 2015 at 23:11, Roger Clark notifications@github.com wrote:

What does it do ?

I we can probably add it into one of the headers.

We have had to add various other macro's etc, as the original codebase we use is based on Arduino 0022, and as there is no overall API spec for the Arduino API, we only tend to know about omissions when people try to use things.

— Reply to this email directly or view it on GitHub https://github.com/rogerclarkmelbourne/Arduino_STM32/issues/121#issuecomment-150370465 .

rogerclarkmelbourne commented 9 years ago

Can you test that

define _BV(bit) \

(1 << (bit)) 

Does do what Atmel claim it does

"Converts a bit number into a byte value."

I can't see the on an ARM platform that this is going to result in a byte value.

More like an int, which is 32 bits

If this is supposed to return a byte, then I suspect the macro would need to cast the result back to a byte

phenom128 commented 9 years ago

well it was atmel avr libc, apols fuzzy thinking or lack thereof

is'nt it returning a bit mask that the user has to apply in some way?

srp

On 23 October 2015 at 00:52, Roger Clark notifications@github.com wrote:

Can you test that

define _BV(bit) \

(1 << (bit))

Does do what Atmel claim it does

"Converts a bit number into a byte value."

I can't see the on an ARM platform that this is going to result in a byte value.

More like an int, which is 32 bits

If this is supposed to return a byte, then I suspect the macro would need to cast the result back to a byte

— Reply to this email directly or view it on GitHub https://github.com/rogerclarkmelbourne/Arduino_STM32/issues/121#issuecomment-150390051 .

rogerclarkmelbourne commented 9 years ago

Yes, The result of this could be used as a bit mask

I think there are some other macros that we already have that do the same thing, but I understand that if you have a lib that uses this macro that it needs to be in the core

BTW. Does you code include avr/io.h ? i.e do we need a file with that name (and we'd stick the single marco in it, as using the whole AVR header could be problematic and cause conflicts

phenom128 commented 9 years ago

if 'our' code already has this macro albeit by another name and that macro is 'visible' within 'our' include path, then a simple wrapper in that file would that suffice?

stephen

On 23 October 2015 at 01:33, Roger Clark notifications@github.com wrote:

Yes, The result of this could be used as a bit mask

I think there are some other macros that we already have that do the same thing, but I understand that if you have a lib that uses this macro that it needs to be in the core

BTW. Does you code include avr/io.h ? i.e do we need a file with that name (and we'd stick the single marco in it, as using the whole AVR header could be problematic and cause conflicts

— Reply to this email directly or view it on GitHub https://github.com/rogerclarkmelbourne/Arduino_STM32/issues/121#issuecomment-150395236 .

rogerclarkmelbourne commented 9 years ago

My mistake

The macros we have, don't do this.

I was thinking of this set

define digitalPinToPort(P) ( PIN_MAP[P].gpio_device )

define digitalPinToBitMask(P) ( BIT(PIN_MAP[P].gpio_bit) )

define portOutputRegister(port) ( &(port->regs->ODR) )

define portInputRegister(port) ( &(port->regs->IDR) )

define portSetRegister(pin) ( &(PIN_MAP[pin].gpio_device->regs->BSRR) )

define portClearRegister(pin) ( &(PIN_MAP[pin].gpio_device->regs->BRR) )

define portConfigRegister(pin) ( &(PIN_MAP[pin].gpio_device->regs->CRL) )

I'm not sure if @sweetlilmre needs to be able to include avr/io.h

This will dictate where the macro gets defined.

sweetlilmre commented 9 years ago

No need to include avr/io.h in a 'standard' Arduino build e.g.:

void setup() {
    int two = _BV(1);
}

void loop() {
}

will work. I think the broader set of definitions supplied in avr/sfr_defs.h would be very useful, though the comments in https://github.com/arduino/Arduino/issues/1834 hint at some complexity. I could have a go at implementation.

rogerclarkmelbourne commented 9 years ago

I'm not sure how useful it would be to implement the other macros, as from the issue you linked to, they seem to be associated with direct register access to the AVR hardware, which is going to be different on any other hardware, (Due or STM32 or Teensy etc)

So you could be wasting you time porting those.

Actually, I suspect they will compile OK, but will be of little value.

BTW. What code do you have that uses the _BV value, as if this also does direct hardware access, aimed at the AVR hardware, it will not work on STM32 (or Due). So you may need to completely rewrite whatever it is that uses _BV

sweetlilmre commented 9 years ago

The code uses _BV for pure bit value creation so there is no direct HW access. Perhaps the other macros are not in scope, but the final comment with the construct

#define _VOLVAL(_v)  (*((volatile typeof(_v) *) & _v)) // force value to be volatile
#define bit_is_set(sfr, bit) (_VOLVAL(sfr) & _BV(bit))

Looks promising. At any rate, some header that is included by default that contained:

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

Would be great.

rogerclarkmelbourne commented 9 years ago

Probably put it in cores\maple\wirish.h

Its already got these definitions which are similar

define lowByte(w) ((w) & 0xFF)

define highByte(w) (((w) >> 8) & 0xFF)

define bitRead(value, bit) (((value) >> (bit)) & 0x01)

define bitSet(value, bit) ((value) |= (1UL << (bit)))

define bitClear(value, bit) ((value) &= ~(1UL << (bit)))

define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : \

                                               bitClear(value, bit))

define bit(b) (1UL << (b))

in fact the last one does the same job I think. Just by a different name

rogerclarkmelbourne commented 8 years ago

Do you want em to add

ifndef _BV

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

endif

to wirish,h ?

stevstrong commented 8 years ago

I think it would make sense to add it in order to maintain Arduino compatibility, I also often used the macro.

sweetlilmre commented 8 years ago

I would second that :)

rogerclarkmelbourne commented 8 years ago

OK. Thats done.