|
2 | 2 | // thanks to Bill W for inspiration |
3 | 3 | // github.com/WestfW/Duino-hacks/blob/master/fastdigitalIO/fastdigitalIO.h |
4 | 4 | // 20200709 v0.1.0 release |
| 5 | +// 20200410 alias _to_xxx_PGM[] arrays as __flash for runtime pin manipulation |
5 | 6 |
|
6 | | -#define ARDUINO_MAIN |
7 | | -#include <Arduino.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <avr/io.h> |
| 9 | + |
| 10 | +// todo: use constants.h from PicoCore |
| 11 | +enum {LOW, HIGH}; // for digitalWrite() |
| 12 | +enum {INPUT, OUTPUT, INPUT_PULLUP}; // for pinMode() |
| 13 | + |
| 14 | +extern __flash const uint16_t port_to_output_PGM[]; |
| 15 | +extern __flash const uint8_t digital_pin_to_port_PGM[]; |
| 16 | +extern __flash const uint8_t digital_pin_to_bit_mask_PGM[]; |
8 | 17 |
|
9 | 18 | typedef volatile uint8_t* ioreg_p; |
10 | 19 |
|
11 | | -inline ioreg_p pin_to_port(uint8_t pin) |
| 20 | +#define ALWAYS_INLINE __attribute__((always_inline)) inline |
| 21 | + |
| 22 | +ALWAYS_INLINE ioreg_p pin_to_port(uint8_t pin) |
12 | 23 | { |
13 | | - return (ioreg_p) port_to_output_PGM[digital_pin_to_port_PGM[pin]]; |
| 24 | + // use pointer instead of array operators due to avr-gcc bug: |
| 25 | + // https://www.avrfreaks.net/forum/avr-gcc-have-i-told-you-lately-i-hate-you |
| 26 | + //return (ioreg_p) port_to_output_PGM[digital_pin_to_port_PGM[pin]]; |
| 27 | + return (ioreg_p) *(port_to_output_PGM + *(digital_pin_to_port_PGM +pin)); |
14 | 28 | } |
15 | 29 |
|
16 | | -inline ioreg_p pin_to_ddr(uint8_t pin) |
| 30 | +ALWAYS_INLINE ioreg_p pin_to_ddr(uint8_t pin) |
17 | 31 | { |
18 | 32 | // DDR address is 1 less than PORT address |
19 | 33 | return pin_to_port(pin) - 1; |
20 | 34 | } |
21 | 35 |
|
22 | | -inline ioreg_p pin_to_pinreg(uint8_t pin) |
| 36 | +ALWAYS_INLINE ioreg_p pin_to_pinreg(uint8_t pin) |
23 | 37 | { |
24 | 38 | // PIN address is 2 less than PORT address |
25 | 39 | return pin_to_port(pin) - 2; |
26 | 40 | } |
27 | 41 |
|
28 | | -inline uint8_t pin_to_bit(uint8_t pin) |
| 42 | +ALWAYS_INLINE uint8_t pin_to_bit(uint8_t pin) |
29 | 43 | { |
30 | | - return digital_pin_to_bit_mask_PGM[pin]; |
| 44 | + //return digital_pin_to_bit_mask_PGM[pin]; |
| 45 | + return *(digital_pin_to_bit_mask_PGM + pin); |
31 | 46 | } |
32 | 47 |
|
33 | 48 | void pinMode(uint8_t pin, uint8_t mode) |
|
0 commit comments