Skip to content

Commit 3f38e9a

Browse files
author
James Foster
committed
Possible fix for Arduino-CI#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).
1 parent 5351b02 commit 3f38e9a

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [Unreleased]
99
### Added
1010
- Add `__AVR__` to defines when compiling
11+
- Add support for `diditalPinToPort()`, `digitalPinToBitMask()`, and `portOutputRegister()`
1112

1213
### Changed
1314
- Move repository from https://github.com/ianfixes/arduino_ci to https://github.com/Arduino-CI/arduino_ci
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <ArduinoUnitTests.h>
2+
#include <Arduino.h>
3+
4+
// https://github.com/arduino-libraries/Ethernet/blob/master/src/utility/w5100.h#L337
5+
6+
unittest(test)
7+
{
8+
uint8_t ss_pin = 12;
9+
uint8_t ss_port = digitalPinToPort(ss_pin);
10+
assertEqual(12, ss_port);
11+
uint8_t *ss_pin_reg = portOutputRegister(ss_port);
12+
assertEqual(GODMODE()->pMmapPort(ss_port), ss_pin_reg);
13+
uint8_t ss_pin_mask = digitalPinToBitMask(ss_pin);
14+
assertEqual(1, ss_pin_mask);
15+
16+
assertEqual((int) 1, (int) *ss_pin_reg); // verify initial value
17+
*(ss_pin_reg) &= ~ss_pin_mask; // set SS
18+
assertEqual((int) 0, (int) *ss_pin_reg); // verify value
19+
*(ss_pin_reg) |= ss_pin_mask; // clear SS
20+
assertEqual((int) 1, (int) *ss_pin_reg); // verify value
21+
}
22+
23+
unittest_main()

cpp/arduino/Godmode.h

+20
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ unsigned long micros();
3030
#define NUM_SERIAL_PORTS 0
3131
#endif
3232

33+
// These definitions allow the following to compile (see issue #193):
34+
// https://github.com/arduino-libraries/Ethernet/blob/master/src/utility/w5100.h:341
35+
// add padding because some boards (__MK20DX128__) offset from the given address
36+
#define MMAP_PORTS_SIZE (MOCK_PINS_COUNT + 256)
37+
#define digitalPinToBitMask(pin) (1)
38+
#define digitalPinToPort(pin) (pin)
39+
#define portOutputRegister(port) (GODMODE()->pMmapPort(port))
40+
3341
class GodmodeState {
3442
private:
3543
struct PortDef {
@@ -43,6 +51,8 @@ class GodmodeState {
4351
uint8_t mode;
4452
};
4553

54+
uint8_t mmapPorts[MMAP_PORTS_SIZE];
55+
4656
static GodmodeState* instance;
4757

4858
public:
@@ -87,12 +97,19 @@ class GodmodeState {
8797
spi.readDelayMicros = 0;
8898
}
8999

100+
void resetMmapPorts() {
101+
for (int i = 0; i < MMAP_PORTS_SIZE; ++i) {
102+
mmapPorts[i] = 1;
103+
}
104+
}
105+
90106
void reset() {
91107
resetClock();
92108
resetPins();
93109
resetInterrupts();
94110
resetPorts();
95111
resetSPI();
112+
resetMmapPorts();
96113
seed = 1;
97114
}
98115

@@ -112,6 +129,9 @@ class GodmodeState {
112129
return instance->micros;
113130
}
114131

132+
uint8_t* pMmapPort(uint8_t port) { return &mmapPorts[port]; }
133+
uint8_t mmapPortValue(uint8_t port) { return mmapPorts[port]; }
134+
115135
// C++ 11, declare as public for better compiler error messages
116136
GodmodeState(GodmodeState const&) = delete;
117137
void operator=(GodmodeState const&) = delete;

0 commit comments

Comments
 (0)