arduino / arduino/ArduinoCore-avr

64K problems: pins_arduino.h _PGM arrays pushed out of range of pgm_read_byte()

Open
#174 21 comments 1 reaction 1 assignee Claimed by @cmaglie View on GitHub
bug
Dominant language
C
Stars
1.5k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

A simple program (say, Blink) that includes more than 64k of pgmspace data (for MEGA, MEGA2560, MEGA ADK, various 1284 boards) will fail to work correctly.
One of the reasons is that Arduino.h includes macro definitions like:

```cpp
#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
```

Due to link order, the `digital_pin_to_port_PGM[]` array will be pushed AFTER the explicitly defined pgmspace variables, and it will no longer be readable by `pgm_read_byte`.

Using `pgm_read_byte_far()` seems like overkill.
It turns out that apparently gcc has a similar problem with pgmspace variables that IT uses, because the default linker map includes two entries for progmem data:

```cpp
/* For data that needs to reside in the lower 64k of progmem. */
*(.progmem.gcc*)
*(.progmem*)
```

This means that MEGA and etc can be fixed by a relative simple patch to their `pins_arduino.h` file, putting the pin tables in section `.progmem.gcc.arduinocore` instead of the normal `.progmem` (as per the attached diff file)

### Additional context

#### Additional reports

- https://github.com/arduino/ArduinoCore-avr/issues/600

#### Related

- https://github.com/arduino/ArduinoCore-avr/pull/601
- https://github.com/MCUdude/MegaCore/issues/226
- https://github.com/MCUdude/MegaCore/pull/227

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.