forked from youen/OneWireArduinoSlave
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.
94 lines
3.5 KiB
94 lines
3.5 KiB
10 years ago
|
#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;
|
||
|
|
||
|
public:
|
||
|
inline Pin(uint8_t pin)
|
||
|
{
|
||
|
mask = PIN_TO_BITMASK(pin);
|
||
|
reg = PIN_TO_BASEREG(pin);
|
||
|
}
|
||
|
|
||
|
inline void inputMode() { DIRECT_MODE_INPUT(reg, mask); }
|
||
|
inline void outputMode() { DIRECT_MODE_OUTPUT(reg, mask); }
|
||
|
|
||
|
inline bool read() { return DIRECT_READ(reg, mask); }
|
||
|
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(); }
|
||
|
};
|
||
|
|
||
|
#endif
|