You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
4.1 KiB
114 lines
4.1 KiB
#ifndef _LowLevel_h |
|
#define _LowLevel_h |
|
|
|
#include <inttypes.h> |
|
|
|
#if ARDUINO >= 100 |
|
#include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc |
|
#else |
|
#include "WProgram.h" // for delayMicroseconds |
|
#include "pins_arduino.h" // for digitalPinToBitMask, etc |
|
#endif |
|
|
|
#if defined(__AVR__) |
|
#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) |
|
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) |
|
#define IO_REG_TYPE uint8_t |
|
#define IO_REG_ASM asm("r30") |
|
#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) |
|
#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask)) |
|
#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask)) |
|
#define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask)) |
|
#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask)) |
|
|
|
#elif defined(__MK20DX128__) || defined(__MK20DX256__) |
|
#define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) |
|
#define PIN_TO_BITMASK(pin) (1) |
|
#define IO_REG_TYPE uint8_t |
|
#define IO_REG_ASM |
|
#define DIRECT_READ(base, mask) (*((base)+512)) |
|
#define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0) |
|
#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1) |
|
#define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1) |
|
#define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1) |
|
|
|
#elif defined(__SAM3X8E__) |
|
// Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due. |
|
// http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268 |
|
// If you have trouble with OneWire on Arduino Due, please check the |
|
// status of delayMicroseconds() before reporting a bug in OneWire! |
|
#define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER)) |
|
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) |
|
#define IO_REG_TYPE uint32_t |
|
#define IO_REG_ASM |
|
#define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0) |
|
#define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask)) |
|
#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask)) |
|
#define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask)) |
|
#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask)) |
|
#ifndef PROGMEM |
|
#define PROGMEM |
|
#endif |
|
#ifndef pgm_read_byte |
|
#define pgm_read_byte(addr) (*(const uint8_t *)(addr)) |
|
#endif |
|
|
|
#elif defined(__PIC32MX__) |
|
#define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) |
|
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) |
|
#define IO_REG_TYPE uint32_t |
|
#define IO_REG_ASM |
|
#define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10 |
|
#define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08 |
|
#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04 |
|
#define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24 |
|
#define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28 |
|
|
|
#else |
|
#error "Please define I/O register types here" |
|
#endif |
|
|
|
class Pin |
|
{ |
|
private: |
|
IO_REG_TYPE mask_; |
|
volatile IO_REG_TYPE *reg_; |
|
byte interruptNumber_; |
|
|
|
public: |
|
Pin() |
|
: mask_(0) |
|
, reg_(0) |
|
, interruptNumber_((byte)-1) |
|
{ } |
|
|
|
Pin(uint8_t pin) |
|
{ |
|
mask_ = PIN_TO_BITMASK(pin); |
|
reg_ = PIN_TO_BASEREG(pin); |
|
|
|
switch (pin) |
|
{ |
|
case 2: interruptNumber_ = 0; break; |
|
case 3: interruptNumber_ = 1; break; |
|
default: interruptNumber_ = (byte)-1; |
|
} |
|
} |
|
|
|
inline void inputMode() { DIRECT_MODE_INPUT(reg_, mask_); } |
|
inline void outputMode() { DIRECT_MODE_OUTPUT(reg_, mask_); } |
|
|
|
inline bool read() { return DIRECT_READ(reg_, mask_) == 1; } |
|
inline void writeLow() { DIRECT_WRITE_LOW(reg_, mask_); } |
|
inline void writeHigh() { DIRECT_WRITE_HIGH(reg_, mask_); } |
|
inline void write(bool value) { if (value) writeHigh(); else writeLow(); } |
|
|
|
inline void attachInterrupt(void (*handler)(), int mode) |
|
{ |
|
EIFR |= (1 << INTF0); // clear any pending interrupt (we want to call the handler only for interrupts happending after it is attached) |
|
::attachInterrupt(interruptNumber_, handler, mode); |
|
} |
|
inline void detachInterrupt() { ::detachInterrupt(interruptNumber_); } |
|
}; |
|
|
|
#endif
|
|
|