Skip to content

Commit

Permalink
Merge pull request #14 from dmadison/boards-mkr
Browse files Browse the repository at this point in the history
Arduino MKR Boards Support
  • Loading branch information
dmadison authored Oct 31, 2020
2 parents eba7b3d + 7a68ac3 commit f505a15
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/ServoInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ServoInputPin : public ServoInputSignal {
public:
ServoInputPin() {
ServoInputPin<Pin>::PinMask = PIN_TO_BITMASK(Pin);
ServoInputPin<Pin>::Port = PIN_TO_BASEREG(Pin);
ServoInputPin<Pin>::PortRegister = PIN_TO_BASEREG(Pin);
pinMode(Pin, INPUT_PULLUP);

attachInterrupt();
Expand Down Expand Up @@ -168,7 +168,7 @@ class ServoInputPin : public ServoInputSignal {
static void SERVOINPUT_ISR_FLAG isr() {
static unsigned long start = 0;

const boolean state = DIRECT_PIN_READ(Port, PinMask);
const boolean state = DIRECT_PIN_READ(PortRegister, PinMask);

if (state == HIGH) { // rising edge
start = micros();
Expand All @@ -181,7 +181,7 @@ class ServoInputPin : public ServoInputSignal {

protected:
static IO_REG_TYPE PinMask;
static volatile IO_REG_TYPE* Port;
static volatile IO_REG_TYPE* PortRegister;

static volatile boolean changed;
static volatile unsigned long pulseDuration;
Expand All @@ -197,7 +197,7 @@ class ServoInputPin : public ServoInputSignal {
};

template<uint8_t Pin> IO_REG_TYPE ServoInputPin<Pin>::PinMask;
template<uint8_t Pin> volatile IO_REG_TYPE* ServoInputPin<Pin>::Port;
template<uint8_t Pin> volatile IO_REG_TYPE* ServoInputPin<Pin>::PortRegister;

template<uint8_t Pin> volatile boolean ServoInputPin<Pin>::changed = false;
template<uint8_t Pin> volatile unsigned long ServoInputPin<Pin>::pulseDuration = 0;
Expand Down
11 changes: 11 additions & 0 deletions src/ServoInput_Platforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// This file borrows the formating and naming conventions from Paul Stoffregen's
// Encoder library. Thanks Paul!
// https://github.com/PaulStoffregen/Encoder/blob/master/utility/direct_pin_read.h

#ifndef ServoInput_Platforms_h
#define ServoInput_Platforms_h

Expand All @@ -40,6 +44,13 @@
#define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
#define SERVOINPUT_ISR_FLAG ICACHE_RAM_ATTR

#elif defined(__SAMD21G18A__) // Arduino MKR boards, Arm Cortex-M0 SAMD21

#define IO_REG_TYPE uint32_t
#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
#define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)

#else
#error "The ServoInput library does not support this board (platform). Please create a feature request here: https://github.com/dmadison/ServoInput/"
#endif
Expand Down

0 comments on commit f505a15

Please sign in to comment.