From 50bc2464ce6d6e4f8bb16ee88e18e2c81252e2d6 Mon Sep 17 00:00:00 2001 From: hama Date: Wed, 15 Jun 2016 19:53:40 +0200 Subject: [PATCH] SquareWaveTimer works, readFromGPIO() --- Server/bcm2835.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++ Server/bcm2835.hpp | 9 ++++++--- Server/gpiotimer.cpp | 2 +- Server/gpiotimer.hpp | 1 - util/timer.cpp | 2 +- 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/Server/bcm2835.cpp b/Server/bcm2835.cpp index bccf8aa..9881481 100644 --- a/Server/bcm2835.cpp +++ b/Server/bcm2835.cpp @@ -113,6 +113,18 @@ Bcm2835* Bcm2835::instance(Announcer* logger){ return m_instance; } +/** + * Reads the state of a pin. + * + * @param pin the pin to read + * @return pmHigh or pmLow + */ +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. * @@ -174,6 +186,41 @@ void Bcm2835::setMode(PinNumber pin, PinMode mode) } } +/** + * 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 setMode(pin, mInput) + * + * @param pin the pin to change + * @param pullUpNotDown true: the pullup resistor is switched on
+ * false: 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. * diff --git a/Server/bcm2835.hpp b/Server/bcm2835.hpp index 6293577..9157300 100644 --- a/Server/bcm2835.hpp +++ b/Server/bcm2835.hpp @@ -7,8 +7,8 @@ enum PinState { }; enum PinMode { mUndef, - mInput, - mOutput + mOutput, + mInput }; /** @@ -48,7 +48,9 @@ private: 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); /** @@ -60,6 +62,7 @@ public: } 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: diff --git a/Server/gpiotimer.cpp b/Server/gpiotimer.cpp index 69e8ab9..80c87f3 100644 --- a/Server/gpiotimer.cpp +++ b/Server/gpiotimer.cpp @@ -54,7 +54,7 @@ SquareWaveTimer::SquareWaveTimer(PinNumber pin, uint64_t pause, int count, m_low(low), m_nextHigh(false){ // high + low = factor 2: - m_count = count * 2; + m_taskCount = count * 2; } /** diff --git a/Server/gpiotimer.hpp b/Server/gpiotimer.hpp index 76a8e4c..58f0d5b 100644 --- a/Server/gpiotimer.hpp +++ b/Server/gpiotimer.hpp @@ -30,7 +30,6 @@ public: public: virtual void timerTask(); private: - int m_count; uint64_t m_pause; uint64_t m_high; uint64_t m_low; diff --git a/util/timer.cpp b/util/timer.cpp index 4ff3faf..5b2b039 100644 --- a/util/timer.cpp +++ b/util/timer.cpp @@ -1,5 +1,5 @@ #include "util.hpp" -//#define TRACE_ON +#define TRACE_ON #include "trace.hpp" /** * Constructor. -- 2.39.5