Skip to content

pin macro digitalPinHasPWM returns invalid results #1831

Closed
@jnsbyr

Description

@jnsbyr

Basic Infos

Hardware

Hardware: ESP8266 - all boards
Core Version: 2.1.0

Description

The macro digitalPinHasPWM(p) from variants/.../pins_arduino.h has a similar problem as A0 from issue #1766.

digitalPinHasPWM returns true for pins 1 to 17 with an Adafruit Huzzah board. But it should return true for 0 to 16 (or even better 0 to 5 and 12 to 16, because you should not do PWM on your flash IOs). The reason is that the macro is intended to return a boolean (0/1) result but current macro returns NOT_A_PIN for pin 17 but that resolves to -1 and that is true but should be false and it otherwise returns the pin number and for pin 0 that is false but should be true.

With the macro digitalPinToInterrupt the case is a little bit different. It should return the number of the external interrupt for a specific pin to be used with attachInterrupt(). With AVR architecture this results in 0 for pin 2 and 1 for pin 3 and otherwise NOT_AN_INTERRUPT (-1) for all other pins being an offset for the interrupt vector address of INT0. For the ESP8266 the current solution seems to return the right values but the macro should be defined using the constant NOT_AN_INTERRUPT instead of NOT_A_PIN.

Possible Solution

  • minimum change
#define digitalPinToInterrupt(p)    (((p) < EXTERNAL_NUM_INTERRUPTS)? (p) : NOT_AN_INTERRUPT)
#define digitalPinHasPWM(p)         (((p) < NUM_DIGITAL_PINS)? 1 : 0)
  • saveguard approach
#define digitalPinHasPWM(p)         (((p) < NUM_DIGITAL_PINS && ((p) < 6 || (p) > 11))? 1 : 0)

in all variants of pins_arduino.h

This issue blocks firmata/arduino#278.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions