arduino / arduino/ArduinoCore-avr

Pin numbers for leonardo analog pins >= A6 are weird / duplicate

Open
#187 4 comments 0 reactions 1 assignee Claimed by @cmaglie View on GitHub
Dominant language
C
Stars
1.5k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

While looking into arduino/Arduino#1741 (and _totally_ misunderstanding that issue at first), I found some weirdness with the `A6` to `A11` constants in leonardo's pins_arduino.h.

[The documentation says](http://arduino.cc/en/Main/ArduinoBoardMicro):

> Analog Inputs: A0-A5, A6 - A11 (on digital pins 4, 6, 8, 9, 10, and 12)

However, the values of the A6..A11 constants are not those, 4, 6, etc. but different pin numbers starting at 24:

https://github.com/arduino/Arduino/blob/ide-1.5.x/hardware/arduino/avr/variants/leonardo/pins_arduino.h#L120

```
static const uint8_t A6 = 24; // D4
static const uint8_t A7 = 25; // D6
static const uint8_t A8 = 26; // D8
static const uint8_t A9 = 27; // D9
static const uint8_t A10 = 28; // D10
static const uint8_t A11 = 29; // D12
```

This should work as expected, because the
`digital_pin_to_port_PGM` and `digital_pin_to_bit_mask_PGM` contain the
same values for 4 and 24, for 6 and 26, etc:

```
const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
PD, // D0 - PD2
PD, // D1 - PD3
PD, // D2 - PD1
PD, // D3 - PD0
PD, // D4 - PD4
PC, // D5 - PC6
PD, // D6 - PD7
PE, // D7 - PE6

(...)
PD, // D24 / D4 - A6 - PD4
PD, // D25 / D6 - A7 - PD7
PB, // D26 / D8 - A8 - PB4
PB, // D27 / D9 - A9 - PB5
PB, // D28 / D10 - A10 - PB6
PD, // D29 / D12 - A11 - PD6
};

const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
_BV(2), // D0 - PD2
_BV(3), // D1 - PD3
_BV(1), // D2 - PD1
_BV(0), // D3 - PD0
_BV(4), // D4 - PD4
_BV(6), // D5 - PC6
_BV(7), // D6 - PD7
_BV(6), // D7 - PE6
(...)

_BV(4), // D24 / D4 - A6 - PD4
_BV(7), // D25 / D6 - A7 - PD7
_BV(4), // D26 / D8 - A8 - PB4
_BV(5), // D27 / D9 - A9 - PB5
_BV(6), // D28 / D10 - A10 - PB6
_BV(6), // D29 / D12 - A11 - PD6
};
```

So using things like `digitalWrite(A6)` should work as expected, but comparing pin numbers will not (A6 != 4). Also, the number of digital pins (`NUM_DIGITAL_PINS` is now set to 30, while the last 6 really are duplicates of 6 earlier ones).

Tracing back to history, it seems things were originally different, but changed here: ba5d66c26f88848fbab6cd5cc23d1c19a7380a32 by @zeveland.

I presume that this change was made so that the Ax constants are now all consecutive, so the translation from pin number to analog channel is a matter of subtracting a constant. However, I think that this is not really the correct fix for this problem: It should instead be possible for pins_arduino.h to supply a different (non-linear) pin-to-channel mapping if required.

I already had half a plan to clean up the analog pin handling in pins_arduino.h, so if I ever get around to that, I can also consider this issue.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.