Skip to content

Commit d2a44fb

Browse files
committed
v 0.7.0 with runtime pin IO
1 parent 2363417 commit d2a44fb

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,5 @@ The Arduino Blink example sketch built for the Uno takes 924 bytes of flash, but
66
Download [a release](https://github.com/nerdralph/ArduinoShrink/releases), and extract the files into your Arduino/libraries folder. Select ArduinoShrink from the Sketch, Include Library menu. This will replace several Arduino functions with smaller and faster versions. The Arduino pin number must be known at compile time if you are using this library.
77

88
## Design and Implemenation
9-
I've written <a href="http://nerdralph.blogspot.com/2021/04/honey-i-shrunk-arduino-core.html">a blog post</a> discussing the implementation details of ArduinoShrink.
10-
11-
## Planned Improvements
12-
Update digitalWrite() to work when the pin is not known at compile time.
9+
I've written <a href="http://nerdralph.blogspot.com/2021/04/honey-i-shrunk-arduino-core.html">a blog post</a> discussing the implementation details of ArduinoShrink. Version 0.7.0 supports manipulation of pins that are not known at compile-time. When the pin is known at compiletime, digitalWrite() will still compile to a single "sbi" or "cbi" instruction.
1310

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoShrink
2-
version=0.6.0
2+
version=0.7.0
33
author=Ralph Doncaster
44
maintainer=
55
sentence=

src/wiring.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// (c) Ralph Doncaster 2020
22
// ArduinoShrink
33

4+
#define ARDUINO_MAIN
45
#include <Arduino.h>
56
#include <util/delay.h>
67
#include "as_common.h"

src/wiring_digital.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,47 @@
22
// thanks to Bill W for inspiration
33
// github.com/WestfW/Duino-hacks/blob/master/fastdigitalIO/fastdigitalIO.h
44
// 20200709 v0.1.0 release
5+
// 20200410 alias _to_xxx_PGM[] arrays as __flash for runtime pin manipulation
56

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[];
817

918
typedef volatile uint8_t* ioreg_p;
1019

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)
1223
{
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));
1428
}
1529

16-
inline ioreg_p pin_to_ddr(uint8_t pin)
30+
ALWAYS_INLINE ioreg_p pin_to_ddr(uint8_t pin)
1731
{
1832
// DDR address is 1 less than PORT address
1933
return pin_to_port(pin) - 1;
2034
}
2135

22-
inline ioreg_p pin_to_pinreg(uint8_t pin)
36+
ALWAYS_INLINE ioreg_p pin_to_pinreg(uint8_t pin)
2337
{
2438
// PIN address is 2 less than PORT address
2539
return pin_to_port(pin) - 2;
2640
}
2741

28-
inline uint8_t pin_to_bit(uint8_t pin)
42+
ALWAYS_INLINE uint8_t pin_to_bit(uint8_t pin)
2943
{
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);
3146
}
3247

3348
void pinMode(uint8_t pin, uint8_t mode)

0 commit comments

Comments
 (0)