From 5c2f3480e70a61c224882ff131222af88d9db4ce Mon Sep 17 00:00:00 2001 From: hama Date: Mon, 13 Jun 2016 23:52:11 +0200 Subject: [PATCH] client and cpidjinn work --- .gitignore | 2 ++ Server/bcm2835.cpp | 16 ++++++++++++---- Server/bcm2835.hpp | 1 + Server/client.cpp | 5 +++-- Server/gpioprocessor.cpp | 6 ++++-- Server/gpiotimer.cpp | 27 ++++++++++++++++----------- Server/gpiotimer.hpp | 5 +++-- util/timer.cpp | 4 ++-- 8 files changed, 43 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 8fab621..1638e53 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ *.o +cpidjinn +client Server/cpidjinn Server/client *.kdev4 diff --git a/Server/bcm2835.cpp b/Server/bcm2835.cpp index 8a5ba93..bccf8aa 100644 --- a/Server/bcm2835.cpp +++ b/Server/bcm2835.cpp @@ -46,6 +46,7 @@ Bcm2835::Bcm2835(Announcer* logger, bool simulation) : m_bscoBase = m_spioBase + blocksize; m_pwmBase = m_bscoBase + blocksize; m_bsc1Base = m_pwmBase + blocksize; + m_endOfRange = m_bsc1Base + blocksize; m_valid = true; // for RasPI2 we find the data in the device tree: } else { @@ -217,9 +218,16 @@ void Bcm2835::writeToGPIO(PinNumber pin, PinState state){ * @param value value to write */ void Bcm2835::writeWord(volatile uint32_t* address, uint32_t value){ - // atomic access: - __sync_synchronize(); - *address = value; - __sync_synchronize(); + if (m_simulation && (address < m_bcmBase || address >= m_endOfRange)){ + m_logger->sayf(LOG_ERROR, "writeWord: invalid address: %lld [%lld, %lld]\n", + (long long int) address, + (long long int) m_bcmBase, + (long long int) m_endOfRange); + } else { + // atomic access: + __sync_synchronize(); + *address = value; + __sync_synchronize(); + } } diff --git a/Server/bcm2835.hpp b/Server/bcm2835.hpp index 6277d2d..6293577 100644 --- a/Server/bcm2835.hpp +++ b/Server/bcm2835.hpp @@ -77,6 +77,7 @@ private: volatile uint32_t* m_bscoBase; volatile uint32_t* m_pwmBase; volatile uint32_t* m_bsc1Base; + volatile uint32_t* m_endOfRange; uint32_t* m_simulatorMemory; Announcer* m_logger; PinInfo m_pins[pinCount]; diff --git a/Server/client.cpp b/Server/client.cpp index 5be8432..cd28f7a 100644 --- a/Server/client.cpp +++ b/Server/client.cpp @@ -34,12 +34,12 @@ static void prepareMelody(PinNumber pin, DynBuffer& buffer){ addTone(duration, 659255, buffer); } static void prepareBlink(PinNumber pin, int count, int high, int low, DynBuffer& buffer){ - buffer.clear().append("MELO"); + printf("pin: %d count: %d high: %d low: %d\n", pin, count, high, low); + buffer.clear().append("BLNK"); buffer.appendAsLE((int) pin, 1); buffer.appendAsLE(count, 4); buffer.appendAsLE(high * 1000, 4); buffer.appendAsLE(low * 1000, 4); - } void usage(const char* msg, const char* argument){ printf("Usage: client []\n" @@ -121,6 +121,7 @@ int main (int argc, char **argv) { TcpClient client(&logger); if (client.connect(host, port)){ client.sendAndReceive(buffer); + printf("Received: %s\n", buffer.str()); } client.disconnect(); return 0; diff --git a/Server/gpioprocessor.cpp b/Server/gpioprocessor.cpp index ba835d1..75a0780 100644 --- a/Server/gpioprocessor.cpp +++ b/Server/gpioprocessor.cpp @@ -29,7 +29,7 @@ GPIOProcessor::~GPIOProcessor() * OUT: answer for the client */ void GPIOProcessor::blink(DynBuffer& buffer){ - if (buffer.length() != 4 + 1 + 2*4){ + if (buffer.length() != 4 + 1 + 3*4){ buffer.clear().appendFormatted("ERR wrong packet length %d instead of %d", buffer.length(), 4 + 1 + 2*4); } else { @@ -37,7 +37,9 @@ void GPIOProcessor::blink(DynBuffer& buffer){ int count = buffer.valueOfLE(4, 5); int periodHigh = buffer.valueOfLE(4, 5 + 4); int periodLow = buffer.valueOfLE(4, 5 + 2*4); - new SquareWaveTimer(pin, 0, count, periodHigh, periodLow, m_logger, &m_pool); + SquareWaveTimer* timer = new SquareWaveTimer(pin, 0, count, periodHigh, periodLow, + this, m_logger, &m_pool); + timer->start(); printf("Pin: %d count: %d high: %d low: %d\n", (int) pin, count, periodHigh, periodLow); buffer.set("OK "); } diff --git a/Server/gpiotimer.cpp b/Server/gpiotimer.cpp index 9d16853..69e8ab9 100644 --- a/Server/gpiotimer.cpp +++ b/Server/gpiotimer.cpp @@ -8,9 +8,11 @@ * @param logger the logger * @param pool the pool managing the timer threads */ -GPIOTimer::GPIOTimer(PinNumber pin, uint64_t pause, Announcer* logger, ThreadPool* pool) : +GPIOTimer::GPIOTimer(PinNumber pin, uint64_t pause, GPIOProcessor* gpioProcessor, + Announcer* logger, ThreadPool* pool) : Timer(1, 0, pause, uMicroSeconds, true, logger, pool), - m_pin(pin) + m_pin(pin), + m_processor(gpioProcessor) { } @@ -33,17 +35,20 @@ void GPIOTimer::switchState(PinState state){ /** * Constructor. * - * @param pin the target pin - * @param pause the pause (in microseconds) to wait before starting the wave - * @param count the number of high states to create - * @param high the duration of the high state in microseconds - * @param low the duration of the low state in microseconds - * @param logger the logger - * @param pool the pool managing the timer threads + * @param pin the target pin + * @param pause the pause (in microseconds) to wait before starting the wave + * @param count the number of high states to create + * @param high the duration of the high state in microseconds + * @param low the duration of the low state in microseconds + * @param gpioProcessor access to the SoC + * @param logger the logger + * @param pool the pool managing the timer threads */ SquareWaveTimer::SquareWaveTimer(PinNumber pin, uint64_t pause, int count, - uint64_t high, uint64_t low, Announcer* logger, ThreadPool* pool) : - GPIOTimer(pin, pause, logger, pool), + uint64_t high, uint64_t low, + GPIOProcessor* gpioProcessor, + Announcer* logger, ThreadPool* pool) : + GPIOTimer(pin, pause, gpioProcessor, logger, pool), m_pause(pause), m_high(high), m_low(low), diff --git a/Server/gpiotimer.hpp b/Server/gpiotimer.hpp index 9834b75..76a8e4c 100644 --- a/Server/gpiotimer.hpp +++ b/Server/gpiotimer.hpp @@ -6,7 +6,7 @@ */ class GPIOTimer : public Timer { public: - GPIOTimer(PinNumber pin, uint64_t pause, Announcer* logger, ThreadPool* pool); + GPIOTimer(PinNumber pin, uint64_t pause, GPIOProcessor* gpioProcessor, Announcer* logger, ThreadPool* pool); virtual ~GPIOTimer(); private: // not implemented, private to avoid usage @@ -25,7 +25,8 @@ protected: class SquareWaveTimer : public GPIOTimer{ public: SquareWaveTimer(PinNumber pin, uint64_t pause, int count, - uint64_t high, uint64_t low, Announcer* logger, ThreadPool* pool); + uint64_t high, uint64_t low, + GPIOProcessor* gpioProcessor, Announcer* logger, ThreadPool* pool); public: virtual void timerTask(); private: diff --git a/util/timer.cpp b/util/timer.cpp index 13a0a93..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. @@ -37,7 +37,7 @@ void Timer::run(){ sleep(m_startDelay); } while(! m_shouldStop && m_taskCount-- > 0){ - trace2("Timer::run(%d): %d\n", threadId(), m_count); + trace2("Timer::run(%d): %d\n", threadId(), m_taskCount); timerTask(); sleep(m_delay); } -- 2.39.5