Skip to content

Commit

Permalink
Use standard C++ timer for long timeouts (#1327), remove unused code (#…
Browse files Browse the repository at this point in the history
…1328)

* Replace timer in WaitSignal()

' Remove unused code

* Remove unused file
  • Loading branch information
uweseimet committed Nov 14, 2023
1 parent 94753a6 commit def2745
Show file tree
Hide file tree
Showing 18 changed files with 37 additions and 149 deletions.
35 changes: 20 additions & 15 deletions cpp/hal/bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,28 @@ const char* BUS::GetPhaseStrRaw(phase_t current_phase) {
return it != phase_str_mapping.end() ? it->second : "INVALID";
}

//---------------------------------------------------------------------------
//
// Phase Table
// Reference Table 8: https://www.staff.uni-mainz.de/tacke/scsi/SCSI2-06.html
// This determines the phase based upon the Msg, C/D and I/O signals.
// Phase Table
// Reference Table 8: https://www.staff.uni-mainz.de/tacke/scsi/SCSI2-06.html
// This determines the phase based upon the Msg, C/D and I/O signals.
//
//---------------------------------------------------------------------------
// |MSG|C/D|I/O| Phase
// | 0 | 0 | 0 | DATA OUT
// | 0 | 0 | 1 | DATA IN
// | 0 | 1 | 0 | COMMAND
// | 0 | 1 | 1 | STATUS
// | 1 | 0 | 0 | RESERVED
// | 1 | 0 | 1 | RESERVED
// | 1 | 1 | 0 | MESSAGE OUT
// | 1 | 1 | 1 | MESSAGE IN
const array<phase_t, 8> BUS::phase_table = {
// | MSG|C/D|I/O |
phase_t::dataout, // | 0 | 0 | 0 |
phase_t::datain, // | 0 | 0 | 1 |
phase_t::command, // | 0 | 1 | 0 |
phase_t::status, // | 0 | 1 | 1 |
phase_t::reserved, // | 1 | 0 | 0 |
phase_t::reserved, // | 1 | 0 | 1 |
phase_t::msgout, // | 1 | 1 | 0 |
phase_t::msgin // | 1 | 1 | 1 |
phase_t::dataout,
phase_t::datain,
phase_t::command,
phase_t::status,
phase_t::reserved,
phase_t::reserved,
phase_t::msgout,
phase_t::msgin
};

//---------------------------------------------------------------------------
Expand Down
7 changes: 1 addition & 6 deletions cpp/hal/bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ class BUS : public PinControl
// Operation modes definition
enum class mode_e {
TARGET = 0,
INITIATOR = 1,
MONITOR = 2,
INITIATOR = 1
};

static int GetCommandByteCount(uint8_t);
Expand All @@ -86,7 +85,6 @@ class BUS : public PinControl

// Get the string phase name, based upon the raw data
static const char *GetPhaseStrRaw(phase_t current_phase);
virtual int GetMode(int pin) = 0;

virtual uint32_t Acquire() = 0;
virtual unique_ptr<DataSample> GetSample(uint64_t timestamp = 0) = 0;
Expand All @@ -97,9 +95,6 @@ class BUS : public PinControl
// SEL signal event polling
virtual bool PollSelectEvent() = 0;

// Clear SEL signal event
virtual void ClearSelectEvent() = 0;

virtual bool GetSignal(int pin) const = 0;
// Get SCSI input signal value
virtual void SetSignal(int pin, bool ast) = 0;
Expand Down
1 change: 0 additions & 1 deletion cpp/hal/data_sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class DataSample
virtual bool GetREQ() const = 0;
virtual bool GetACT() const = 0;
virtual uint8_t GetDAT() const = 0;
virtual bool GetDP() const = 0;

virtual uint32_t GetRawCapture() const = 0;

Expand Down
14 changes: 0 additions & 14 deletions cpp/hal/data_sample_raspberry.cpp

This file was deleted.

6 changes: 1 addition & 5 deletions cpp/hal/data_sample_raspberry.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ class DataSample_Raspberry final : public DataSample
{
return GetSignal(PIN_ACT);
}
bool GetDP() const override
{
return GetSignal(PIN_DP);
}
uint8_t GetDAT() const override
{
uint8_t ret_val = 0;
Expand Down Expand Up @@ -106,4 +102,4 @@ class DataSample_Raspberry final : public DataSample

private:
uint32_t data = 0;
};
};
45 changes: 14 additions & 31 deletions cpp/hal/gpiobus.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Powered by XM6 TypeG Technology.
// Copyright (C) 2016-2020 GIMONS
// Powered by XM6 TypeG Technology.
// Copyright (C) 2016-2020 GIMONS
// Copyright (C) 2023 Uwe Seimet
//
//---------------------------------------------------------------------------

Expand All @@ -18,6 +19,7 @@
#ifdef __linux__
#include <sys/epoll.h>
#endif
#include <chrono>

using namespace std;

Expand Down Expand Up @@ -403,42 +405,23 @@ bool GPIOBUS::PollSelectEvent()
#endif
}

//---------------------------------------------------------------------------
//
// Cancel SEL signal event
//
//---------------------------------------------------------------------------
void GPIOBUS::ClearSelectEvent()
{
GPIO_FUNCTION_TRACE
}

//---------------------------------------------------------------------------
//
// Wait for signal change
//
//---------------------------------------------------------------------------
bool GPIOBUS::WaitSignal(int pin, bool ast)
{
// Get current time
const uint32_t now = SysTimer::GetTimerLow();

// Calculate timeout (3000ms)
const uint32_t timeout = 3000 * 1000;
const auto now = chrono::steady_clock::now();

// Wait up to 3 s
do {
// Immediately upon receiving a reset
Acquire();
if (GetRST()) {
return false;
}

// Check for the signal edge
if (GetSignal(pin) == ast) {
return true;
}
} while ((SysTimer::GetTimerLow() - now) < timeout);

// We timed out waiting for the signal
// Abort on a reset
if (GetRST()) {
return false;
}
} while ((chrono::duration_cast<chrono::seconds>(chrono::steady_clock::now() - now).count()) < 3);

return false;
}
2 changes: 0 additions & 2 deletions cpp/hal/gpiobus.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ class GPIOBUS : public BUS

// SEL signal event polling
bool PollSelectEvent() override;
// Clear SEL signal event
void ClearSelectEvent() override;

protected:
virtual void MakeTable() = 0;
Expand Down
11 changes: 0 additions & 11 deletions cpp/hal/gpiobus_raspberry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,17 +642,6 @@ void GPIOBUS_Raspberry::SetDAT(uint8_t dat)
#endif // SIGNAL_CONTROL_MODE
}

bool GPIOBUS_Raspberry::GetDP() const
{
return GetSignal(PIN_DP);
}

//---------------------------------------------------------------------------
//
// Create work table
//
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
//
// Signal table
Expand Down
8 changes: 0 additions & 8 deletions cpp/hal/gpiobus_raspberry.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ class GPIOBUS_Raspberry : public GPIOBUS
// Set REQ signal
void SetREQ(bool ast) override;

bool GetDP() const override;

// Get DAT signal
uint8_t GetDAT() override;
// Set DAT signal
Expand Down Expand Up @@ -174,12 +172,6 @@ class GPIOBUS_Raspberry : public GPIOBUS
// Set Control Signal
void SetMode(int pin, int mode) override;
// Set SCSI I/O mode
int GetMode(int pin) override
{
// Not implemented (or needed for thist gpio bus type)
(void)pin;
return -1;
}
bool GetSignal(int pin) const override;
// Get SCSI input signal value
void SetSignal(int pin, bool ast) override;
Expand Down
5 changes: 0 additions & 5 deletions cpp/hal/gpiobus_virtual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,6 @@ void GPIOBUS_Virtual::SetREQ(bool ast)
SetSignal(PIN_REQ, ast);
}

bool GPIOBUS_Virtual::GetDP() const
{
return GetSignal(PIN_DP);
}

//---------------------------------------------------------------------------
//
// Get data signals
Expand Down
8 changes: 0 additions & 8 deletions cpp/hal/gpiobus_virtual.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ class GPIOBUS_Virtual final : public GPIOBUS
void SetREQ(bool ast) override;
// Set REQ signal

bool GetDP() const override;

bool WaitREQ(bool ast) override
{
return WaitSignal(PIN_REQ, ast);
Expand All @@ -120,12 +118,6 @@ class GPIOBUS_Virtual final : public GPIOBUS
// Set Control Signal
void SetMode(int pin, int mode) override;
// Set SCSI I/O mode
int GetMode(int pin) override
{
// Not implemented (or needed for thist gpio bus type)
(void)pin;
return -1;
}
bool GetSignal(int pin) const override;
// Get SCSI input signal value
void SetSignal(int pin, bool ast) override;
Expand Down
5 changes: 1 addition & 4 deletions cpp/hal/pin_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ class PinControl
// Set ENB signal
virtual void SetENB(bool ast) = 0;

// Get parity signal
virtual bool GetDP() const = 0;

// GPIO pin direction setting
virtual void PinConfig(int pin, int mode) = 0;
// GPIO pin pull up/down resistor setting
Expand All @@ -65,4 +62,4 @@ class PinControl

PinControl() = default;
virtual ~PinControl() = default;
};
};
6 changes: 0 additions & 6 deletions cpp/hal/systimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ uint32_t SysTimer::GetTimerLow()
return systimer_ptr->GetTimerLow();
}

// Get system timer high byte
uint32_t SysTimer::GetTimerHigh()
{
return systimer_ptr->GetTimerHigh();
}

// Sleep for N nanoseconds
void SysTimer::SleepNsec(uint32_t nsec)
{
Expand Down
5 changes: 0 additions & 5 deletions cpp/hal/systimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class PlatformSpecificTimer
virtual void Init() = 0;
// Get system timer low byte
virtual uint32_t GetTimerLow() = 0;
// Get system timer high byte
virtual uint32_t GetTimerHigh() = 0;
// Sleep for N nanoseconds
virtual void SleepNsec(uint32_t nsec) = 0;
// Sleep for N microseconds
Expand All @@ -48,16 +46,13 @@ class SysTimer
static void Init();
// Get system timer low byte
static uint32_t GetTimerLow();
// Get system timer high byte
static uint32_t GetTimerHigh();
// Sleep for N nanoseconds
static void SleepNsec(uint32_t nsec);
// Sleep for N microseconds
static void SleepUsec(uint32_t usec);

private:
static bool initialized;
static bool is_allwinnner;
static bool is_raspberry;

static std::unique_ptr<PlatformSpecificTimer> systimer_ptr;
Expand Down
10 changes: 0 additions & 10 deletions cpp/hal/systimer_raspberry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,6 @@ uint32_t SysTimer_Raspberry::GetTimerLow()
return systaddr[SYST_CLO];
}

//---------------------------------------------------------------------------
//
// Get system timer high byte
//
//---------------------------------------------------------------------------
uint32_t SysTimer_Raspberry::GetTimerHigh()
{
return systaddr[SYST_CHI];
}

//---------------------------------------------------------------------------
//
// Sleep in nanoseconds
Expand Down
2 changes: 0 additions & 2 deletions cpp/hal/systimer_raspberry.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class SysTimer_Raspberry : public PlatformSpecificTimer
void Init() override;
// Get system timer low byte
uint32_t GetTimerLow() override;
// Get system timer high byte
uint32_t GetTimerHigh() override;
// Sleep for N nanoseconds
void SleepNsec(uint32_t nsec) override;
// Sleep for N microseconds
Expand Down
13 changes: 0 additions & 13 deletions cpp/test/gpiobus_raspberry_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,3 @@ TEST(GpiobusRaspberry, GetREQ)
bus.Acquire();
EXPECT_EQ(false, bus.GetREQ());
}

TEST(GpiobusRaspberry, GetDP)
{
SetableGpiobusRaspberry bus;

bus.TestSetGpios(0x00);
bus.TestSetGpioPin(PIN_DP, true);
bus.Acquire();
EXPECT_EQ(true, bus.GetDP());
bus.TestSetGpioPin(PIN_DP, false);
bus.Acquire();
EXPECT_EQ(false, bus.GetDP());
}
3 changes: 0 additions & 3 deletions cpp/test/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,18 @@ class MockBus : public BUS //NOSONAR Having many fields/methods cannot be avoide
MOCK_METHOD(void, SetENB, (bool), (override));
MOCK_METHOD(uint8_t, GetDAT, (), (override));
MOCK_METHOD(void, SetDAT, (uint8_t), (override));
MOCK_METHOD(bool, GetDP, (), (const override));
MOCK_METHOD(uint32_t, Acquire, (), (override));
MOCK_METHOD(int, CommandHandShake, (vector<uint8_t>&), (override));
MOCK_METHOD(int, ReceiveHandShake, (uint8_t *, int), (override));
MOCK_METHOD(int, SendHandShake, (uint8_t *, int, int), (override));
MOCK_METHOD(bool, GetSignal, (int), (const override));
MOCK_METHOD(void, SetSignal, (int, bool), (override));
MOCK_METHOD(bool, PollSelectEvent, (), (override));
MOCK_METHOD(void, ClearSelectEvent, (), (override));
MOCK_METHOD(unique_ptr<DataSample>, GetSample, (uint64_t), (override));
MOCK_METHOD(void, PinConfig, (int, int), (override));
MOCK_METHOD(void, PullConfig, (int , int ), (override));
MOCK_METHOD(void, SetControl, (int , bool ), (override));
MOCK_METHOD(void, SetMode, (int , int ), (override));
MOCK_METHOD(int, GetMode, (int ), (override));

MockBus() = default;
~MockBus() override = default;
Expand Down

0 comments on commit def2745

Please sign in to comment.