return m_instance;
}
+/**
+ * Reads the state of a pin.
+ *
+ * @param pin the pin to read
+ * @return <i>pmHigh</i> or <i>pmLow</i>
+ */
+PinState Bcm2835::readFromGPIO(PinNumber pin){
+ const int offsetPinLevel0 = 0x0034 / sizeof(uint32_t);
+ uint32_t value = readWord(m_gpioBase + offsetPinLevel0 + pin / 32);
+ PinState rc = (value & (1 << (pin % 32))) != 0 ? psHigh : psLow;
+ return rc;
+}
/**
* Reads a word from a memory mapped register.
*
}
}
+/**
+ * Sleep a given number of microseconds.
+ *
+ * @param microseconds time to wait
+ */
+void Bcm2835::delay(int microseconds){
+ usleep(microseconds);
+}
+/**
+ * Sets the pullup or pulldown resistor for an input pin.
+ *
+ * Note: must be called after <i>setMode(pin, mInput)</i>
+ *
+ * @param pin the pin to change
+ * @param pullUpNotDown <i>true</i>: the pullup resistor is switched on<br>
+ * <i>false</i>: the pulldown resistor is switched on
+ */
+void Bcm2835::setInputPullX(PinNumber pin, bool pullUpNotDown){
+ const int offsetPullXEnable = 0x0094;
+ const int offsetPullXEnableClock0 = 0x0098;
+
+ writeWord(m_gpioBase + offsetPullXEnable / sizeof (uint32_t),
+ pullUpNotDown ? 2 : 1);
+ // wait 150 cycles:
+ delay(10);
+ // address the pin:
+ writeWord(m_gpioBase + offsetPullXEnableClock0 / sizeof (uint32_t) + pin/32,
+ 1 << (pin % 32));
+ // wait 150 cycles, the setting will be done by clock
+ delay(10);
+ // remove the binding:
+ writeWord(m_gpioBase + offsetPullXEnable / sizeof (uint32_t), 0);
+ writeWord(m_gpioBase + offsetPullXEnableClock0 / sizeof (uint32_t) + pin/32, 0);
+}
+
/**
* Sets the state of an output pin.
*
};
enum PinMode {
mUndef,
- mInput,
- mOutput
+ mOutput,
+ mInput
};
/**
public:
static Bcm2835* instance(Announcer* logger);
public:
- uint32_t readWord(volatile uint32_t* addr);
+ void delay(int microseconds);
+ PinState readFromGPIO(PinNumber pin);
+ void setInputPullX(PinNumber pin, bool pullUpNotDown);
void setMode(PinNumber pin, PinMode mode);
void setState(PinNumber pin, PinState state);
/**
}
void writeToGPIO(PinNumber pin, PinState state);
protected:
+ uint32_t readWord(volatile uint32_t* addr);
void setBits(volatile uint32_t* addr, uint32_t value, uint32_t mask);
void writeWord(volatile uint32_t* addr, uint32_t value);
private: