From 3f38e9ada635af7b20a3ee1bff0a9a67aa06d601 Mon Sep 17 00:00:00 2001 From: James Foster Date: Thu, 5 Nov 2020 18:58:29 -0800 Subject: [PATCH 1/3] Possible fix for #193 It appears that some boards use memory-mapped I/O to read/write pins. To support that we have an array that can hold pin values. A more elaborate approach would be to connect to the pin logs, but this seems like a decent first step (and allows some files to compile that didn't compile before). --- CHANGELOG.md | 1 + .../TestSomething/test/outputRegister.cpp | 23 +++++++++++++++++++ cpp/arduino/Godmode.h | 20 ++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 SampleProjects/TestSomething/test/outputRegister.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index fdfd1083..1f44cc59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added - Add `__AVR__` to defines when compiling +- Add support for `diditalPinToPort()`, `digitalPinToBitMask()`, and `portOutputRegister()` ### Changed - Move repository from https://github.com/ianfixes/arduino_ci to https://github.com/Arduino-CI/arduino_ci diff --git a/SampleProjects/TestSomething/test/outputRegister.cpp b/SampleProjects/TestSomething/test/outputRegister.cpp new file mode 100644 index 00000000..ea6c9b3a --- /dev/null +++ b/SampleProjects/TestSomething/test/outputRegister.cpp @@ -0,0 +1,23 @@ +#include +#include + +// https://github.com/arduino-libraries/Ethernet/blob/master/src/utility/w5100.h#L337 + +unittest(test) +{ + uint8_t ss_pin = 12; + uint8_t ss_port = digitalPinToPort(ss_pin); + assertEqual(12, ss_port); + uint8_t *ss_pin_reg = portOutputRegister(ss_port); + assertEqual(GODMODE()->pMmapPort(ss_port), ss_pin_reg); + uint8_t ss_pin_mask = digitalPinToBitMask(ss_pin); + assertEqual(1, ss_pin_mask); + + assertEqual((int) 1, (int) *ss_pin_reg); // verify initial value + *(ss_pin_reg) &= ~ss_pin_mask; // set SS + assertEqual((int) 0, (int) *ss_pin_reg); // verify value + *(ss_pin_reg) |= ss_pin_mask; // clear SS + assertEqual((int) 1, (int) *ss_pin_reg); // verify value +} + +unittest_main() diff --git a/cpp/arduino/Godmode.h b/cpp/arduino/Godmode.h index 12fa1b51..589e53df 100644 --- a/cpp/arduino/Godmode.h +++ b/cpp/arduino/Godmode.h @@ -30,6 +30,14 @@ unsigned long micros(); #define NUM_SERIAL_PORTS 0 #endif +// These definitions allow the following to compile (see issue #193): +// https://github.com/arduino-libraries/Ethernet/blob/master/src/utility/w5100.h:341 +// add padding because some boards (__MK20DX128__) offset from the given address +#define MMAP_PORTS_SIZE (MOCK_PINS_COUNT + 256) +#define digitalPinToBitMask(pin) (1) +#define digitalPinToPort(pin) (pin) +#define portOutputRegister(port) (GODMODE()->pMmapPort(port)) + class GodmodeState { private: struct PortDef { @@ -43,6 +51,8 @@ class GodmodeState { uint8_t mode; }; + uint8_t mmapPorts[MMAP_PORTS_SIZE]; + static GodmodeState* instance; public: @@ -87,12 +97,19 @@ class GodmodeState { spi.readDelayMicros = 0; } + void resetMmapPorts() { + for (int i = 0; i < MMAP_PORTS_SIZE; ++i) { + mmapPorts[i] = 1; + } + } + void reset() { resetClock(); resetPins(); resetInterrupts(); resetPorts(); resetSPI(); + resetMmapPorts(); seed = 1; } @@ -112,6 +129,9 @@ class GodmodeState { return instance->micros; } + uint8_t* pMmapPort(uint8_t port) { return &mmapPorts[port]; } + uint8_t mmapPortValue(uint8_t port) { return mmapPorts[port]; } + // C++ 11, declare as public for better compiler error messages GodmodeState(GodmodeState const&) = delete; void operator=(GodmodeState const&) = delete; From 3c5b83998f8999f7c0732741468568511c017b4c Mon Sep 17 00:00:00 2001 From: James Foster Date: Sat, 7 Nov 2020 19:20:57 -0800 Subject: [PATCH 2/3] Limit mock to AVR. --- cpp/arduino/Godmode.h | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/cpp/arduino/Godmode.h b/cpp/arduino/Godmode.h index 589e53df..de7a299e 100644 --- a/cpp/arduino/Godmode.h +++ b/cpp/arduino/Godmode.h @@ -30,14 +30,6 @@ unsigned long micros(); #define NUM_SERIAL_PORTS 0 #endif -// These definitions allow the following to compile (see issue #193): -// https://github.com/arduino-libraries/Ethernet/blob/master/src/utility/w5100.h:341 -// add padding because some boards (__MK20DX128__) offset from the given address -#define MMAP_PORTS_SIZE (MOCK_PINS_COUNT + 256) -#define digitalPinToBitMask(pin) (1) -#define digitalPinToPort(pin) (pin) -#define portOutputRegister(port) (GODMODE()->pMmapPort(port)) - class GodmodeState { private: struct PortDef { @@ -51,7 +43,7 @@ class GodmodeState { uint8_t mode; }; - uint8_t mmapPorts[MMAP_PORTS_SIZE]; + uint8_t mmapPorts[MOCK_PINS_COUNT]; static GodmodeState* instance; @@ -98,7 +90,7 @@ class GodmodeState { } void resetMmapPorts() { - for (int i = 0; i < MMAP_PORTS_SIZE; ++i) { + for (int i = 0; i < MOCK_PINS_COUNT; ++i) { mmapPorts[i] = 1; } } @@ -159,5 +151,16 @@ void detachInterrupt(uint8_t interrupt); inline void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0) {} inline void noTone(uint8_t _pin) {} +// These definitions allow the following to compile (see issue #193): +// https://github.com/arduino-libraries/Ethernet/blob/master/src/utility/w5100.h:341 +// we allow one byte per port which "wastes" 224 bytes, but makes the code easier +#if defined(__AVR__) + #define digitalPinToBitMask(pin) (1) + #define digitalPinToPort(pin) (pin) + #define portOutputRegister(port) (GODMODE()->pMmapPort(port)) +#else + // we don't (yet) support other boards +#endif + GodmodeState* GODMODE(); From e9d37db98d44806c4885020cb28fea60245aed9a Mon Sep 17 00:00:00 2001 From: James Foster Date: Sat, 7 Nov 2020 19:22:30 -0800 Subject: [PATCH 3/3] Update tests to only test for AVR. --- SampleProjects/TestSomething/test/outputRegister.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/SampleProjects/TestSomething/test/outputRegister.cpp b/SampleProjects/TestSomething/test/outputRegister.cpp index ea6c9b3a..fa12255f 100644 --- a/SampleProjects/TestSomething/test/outputRegister.cpp +++ b/SampleProjects/TestSomething/test/outputRegister.cpp @@ -1,9 +1,11 @@ #include #include +// added to fix https://github.com/Arduino-CI/arduino_ci/issues/193 // https://github.com/arduino-libraries/Ethernet/blob/master/src/utility/w5100.h#L337 -unittest(test) +#if defined(__AVR__) +unittest(portOutputRegister) { uint8_t ss_pin = 12; uint8_t ss_port = digitalPinToPort(ss_pin); @@ -19,5 +21,6 @@ unittest(test) *(ss_pin_reg) |= ss_pin_mask; // clear SS assertEqual((int) 1, (int) *ss_pin_reg); // verify value } +#endif unittest_main()