Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate daemon to libgpiod #48

Draft
wants to merge 56 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
2c2ea97
Merge pull request #41 from MuonPi/dev
hangeza Feb 25, 2021
954588c
Started modifying pigpiodhandler class to switch over to libgpiod. No…
hangeza Feb 28, 2021
bd42fc4
added start() and stop() slots to PigpiodHandler class to always have…
hangeza Mar 8, 2021
6874389
Implemented pure usage of libgpiod in PigpiodHandler class. Signals f…
hangeza Mar 9, 2021
98624a7
Cleaned up PigpiodHandler class a bit and implemented one signal to f…
hangeza Mar 8, 2021
921d09c
small change in debug output of detected edges
hangeza Mar 23, 2021
7d32d00
Further implemented functionality equal to the old pigpiod implementa…
hangeza Jul 10, 2021
8f301b1
Added RateBuffer class for handling of all GPIO rates. Still to be im…
hangeza Jul 11, 2021
ecbc059
Embedded RateBuffer in daemon. All rate handling should be supervised…
hangeza Jul 11, 2021
6188502
Added measurement of gpio interval time (in histogram "gpioEventInter…
hangeza Jul 12, 2021
0a4d40a
Renamed enum GPIO_PIN to the more reasonable GPIO_SIGNAL, since this …
hangeza Jul 13, 2021
d00fd6f
fixed wrong position of default argument in constructor of i2cdevice …
hangeza Jul 14, 2021
cb60a3b
Merge branch 'dev' into migrate-daemon-to-libgpiod
hangeza Jul 14, 2021
ea0f327
added missing linkage against pthread in daemon.cmake
hangeza Jul 14, 2021
3c5eda7
Updated preinst script to add muonuser to gpio group. This is require…
hangeza Jul 14, 2021
0af2564
- modified all control scripts for prerequisites etc. wrt libgpiod
hangeza Jul 15, 2021
2b6697b
adjust measured TPDiff values symmetrically around 0, i.e. to fall in…
hangeza Jul 16, 2021
d4fb9e1
Update source/daemon/include/daemon.h
hangeza Jul 16, 2021
98f4d8c
Update source/daemon/include/pigpiodhandler.h
hangeza Jul 16, 2021
59cdb10
Update source/daemon/include/pigpiodhandler.h
hangeza Jul 16, 2021
8732af1
Update source/daemon/include/pigpiodhandler.h
hangeza Jul 16, 2021
4b209be
Update source/daemon/include/pigpiodhandler.h
hangeza Jul 16, 2021
1db133c
Update source/daemon/include/pigpiodhandler.h
hangeza Jul 16, 2021
207dd0f
Update source/daemon/include/pigpiodhandler.h
hangeza Jul 16, 2021
d7c1136
Update source/daemon/include/pigpiodhandler.h
hangeza Jul 16, 2021
750d0f2
Update source/daemon/src/ratebuffer.cpp
hangeza Jul 16, 2021
98dd05a
Update source/gui/src/mainwindow.cpp
hangeza Jul 16, 2021
23340b1
Update source/gui/src/parametermonitorform.cpp
hangeza Jul 16, 2021
40b7512
Update source/gui/src/status.cpp
hangeza Jul 16, 2021
4016e1b
Update source/daemon/config/preinst
hangeza Jul 16, 2021
ad7e129
Some clean-up and minor mods:
hangeza Jul 18, 2021
6f4bcff
- Renamed signal and throttledSignal signals in PigpioHandler and Rat…
hangeza Jul 20, 2021
1319d93
Further work on libgpiod functionality for event handling:
hangeza Jan 14, 2022
3db51a3
Fixed merge conflicts and adapted code at some places for proper comp…
hangeza Jan 16, 2022
1073433
deleted some now obsolete files
hangeza Jan 16, 2022
5b0e74d
Merge branch 'dev' into migrate-daemon-to-libgpiod
hangeza Jan 16, 2022
2b74c00
Fixed some lines which slipped through at the last merge. Compiles now
hangeza Jan 16, 2022
80f040b
Restored muondetector-daemon service file which was corrupted from a …
hangeza Feb 8, 2022
b86d232
renamed pin_bias to bias_flags
hangeza Feb 8, 2022
95a3f17
removed commented-out lines
hangeza Feb 8, 2022
e9030c6
added suffix string "libgpiod" to version strings (e.g. used in packa…
hangeza Feb 19, 2022
f4eb121
removed several debug outputs and decreased the status2 LED flash tim…
hangeza Feb 19, 2022
1445d35
improved preinst script to check whether muonuser exists and already …
hangeza Feb 20, 2022
45196fc
Merge branch 'dev' into migrate-daemon-to-libgpiod
hangeza Feb 19, 2023
f1a0444
applied clang-format
hangeza Feb 19, 2023
e2521c4
added constant with deadtime increment and increased this value to 50us
hangeza Feb 19, 2023
c7b8ad8
removed shadowed redeclaration
hangeza Feb 19, 2023
0e19e60
removed blank line
hangeza Feb 19, 2023
ea3159c
Merge commit '5da24ae77037c3344817bf227996218eee60b371' into v2.1.0
SimonGLM May 24, 2023
6cecacd
Merge pull request #90 from MuonPi/v2.1.0
SimonGLM May 24, 2023
a2d240f
Updated changelog
SimonGLM May 24, 2023
90d6c46
Merge branch 'v2.1.0' - Updated changelogs
SimonGLM May 24, 2023
a099785
Merge branch 'dev' into migrate-daemon-to-libgpiod
hangeza May 24, 2023
d38a9ec
applied clang-format
hangeza May 24, 2023
ffe1523
Merge tag 'v2.1.0' into migrate-daemon-to-libgpiod
hangeza May 24, 2023
eec0256
improved event timestamp handling
hangeza May 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cmake/daemon.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if(NOT WIN32) # added to make program editable in qt-creator on windows
find_library(CRYPTOPP crypto++ REQUIRED)
find_library(CONFIGPP config++ REQUIRED)
if (${MUONDETECTOR_ON_RASPBERRY})
find_library(PIGPIOD_IF2 pigpiod_if2 REQUIRED)
find_library(LIBGPIOD gpiod REQUIRED)
endif ()
find_library(RT rt REQUIRED)
find_library(MOSQUITTO mosquitto REQUIRED)
Expand Down Expand Up @@ -56,12 +56,13 @@ set(MUONDETECTOR_DAEMON_SOURCE_FILES
"${MUONDETECTOR_DAEMON_SRC_DIR}/utility/gpio_mapping.cpp"
"${MUONDETECTOR_DAEMON_SRC_DIR}/logengine.cpp"
"${MUONDETECTOR_DAEMON_SRC_DIR}/utility/geohash.cpp"
"${MUONDETECTOR_DAEMON_SRC_DIR}/utility/ratebuffer.cpp"
"${MUONDETECTOR_DAEMON_SRC_DIR}/utility/kalman_gnss_filter.cpp"
"${MUONDETECTOR_DAEMON_SRC_DIR}/geoposmanager.cpp"
"${MUONDETECTOR_DAEMON_SRC_DIR}/utility/ratebuffer.cpp"

"${MUONDETECTOR_I2C_SOURCE_FILES}"
"${MUONDETECTOR_SPI_SOURCE_FILES}"
# "${MUONDETECTOR_SPI_SOURCE_FILES}"
)


Expand Down Expand Up @@ -109,12 +110,13 @@ set(MUONDETECTOR_DAEMON_HEADER_FILES
"${MUONDETECTOR_DAEMON_HEADER_DIR}/utility/gpio_mapping.h"
"${MUONDETECTOR_DAEMON_HEADER_DIR}/logengine.h"
"${MUONDETECTOR_DAEMON_HEADER_DIR}/utility/geohash.h"
"${MUONDETECTOR_DAEMON_HEADER_DIR}/utility/ratebuffer.h"
"${MUONDETECTOR_DAEMON_HEADER_DIR}/utility/kalman_gnss_filter.h"
"${MUONDETECTOR_DAEMON_HEADER_DIR}/geoposmanager.h"
"${MUONDETECTOR_DAEMON_HEADER_DIR}/utility/ratebuffer.h"

"${MUONDETECTOR_I2C_HEADER_FILES}"
"${MUONDETECTOR_SPI_HEADER_FILES}"
#"${MUONDETECTOR_SPI_HEADER_FILES}"
)

set(MUONDETECTOR_LOGIN_SOURCE_FILES
Expand Down Expand Up @@ -166,7 +168,7 @@ target_include_directories(muondetector-daemon PUBLIC
target_link_libraries(muondetector-daemon
Qt5::Network Qt5::SerialPort
crypto++
pigpiod_if2
gpiod
rt
config++
mosquitto
Expand Down
2 changes: 1 addition & 1 deletion cmake/version.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(PROJECT_VERSION_MAJOR 2)
set(PROJECT_VERSION_MINOR 1)
set(PROJECT_VERSION_PATCH 0)
set(PROJECT_VERSION_ADDITIONAL "")
set(PROJECT_VERSION_ADDITIONAL "libgpiod")
7 changes: 6 additions & 1 deletion daemon/config/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ muondetector-daemon (2.1.0) ; urgency=low
* Bugfixes:
- removed wrong QT connect call which resulted in a warning in log messages after daemon startup
- fixed reconnect to MQTT broker after connection was lost temporarily
- fixed bug in I2C subsystem which lead to a stuck bus after certain read operations and preventing detection of a device in the next logical read access
-
6cecacd
May 24, 2023
Git stats

1,254 c

* Updates:
- restructured data logging system:
Expand Down
1 change: 0 additions & 1 deletion daemon/config/conffiles
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/etc/muondetector/muondetector.conf
/etc/systemd/system/pigpiod.service.d/pigpiod.conf
4 changes: 1 addition & 3 deletions daemon/config/muondetector-daemon.service
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[Unit]
Description=muondetector-daemon - Daemon for custom muondetector board
Requires=pigpiod.service
BindsTo=pigpiod.service
After=pigpiod.service sockets.target network-online.target
After=sockets.target network-online.target

[Service]
Type=simple
Expand Down
1 change: 0 additions & 1 deletion daemon/config/postinst
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/bash -e
deb-systemd-helper enable pigpiod.service
deb-systemd-helper enable muondetector-daemon.service
8 changes: 7 additions & 1 deletion daemon/config/preinst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/bin/bash -e

mkdir -p /var/muondetector
useradd muonuser -g users -G dialout,pi,i2c,users -s /usr/sbin/nologin -r -N -M -b /var/muondetector || echo "User already exists"
if id muonuser &> /dev/null; then
# transitional code until all older versions of muondetector-daemon are updated.
echo "muonuser exists, adding to group gpio."
adduser muonuser gpio
else
useradd muonuser -g users -G dialout,pi,i2c,users,gpio -s /usr/sbin/nologin -r -N -M -b /var/muondetector
fi
chown muonuser:users /var/muondetector
chmod g+w /var/muondetector
19 changes: 15 additions & 4 deletions daemon/include/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <custom_io_operators.h>
#include "logengine.h"
#include "utility/ratebuffer.h"
#include "logparameter.h"
#include "pigpiodhandler.h"
#include "hardware/spidevices.h"
Expand Down Expand Up @@ -121,7 +122,8 @@ public slots:
void onGpsMonHW2Updated(const GnssMonHw2Struct& hw2);
void receivedTcpMessage(TcpMessage tcpMessage);
void pollAllUbxMsgRate();
void sendGpioPinEvent(uint8_t gpio_pin);
void onGpioPinEvent(unsigned int gpio, EventTime event_time);
void sendGpioPinEvent(unsigned int gpio, EventTime event_time);
void onGpsPropertyUpdatedGeodeticPos(const GnssPosStruct& pos);
void UBXReceivedVersion(const QString& swString, const QString& hwString, const QString& protString);
void sampleAdc0Event();
Expand Down Expand Up @@ -153,18 +155,19 @@ public slots:
void UBXSetMinMaxSVs(uint8_t minSVs, uint8_t maxSVs);
void UBXSetMinCNO(uint8_t minCNO);
void GpioSetInput(unsigned int gpio);
void GpioSetOutput(unsigned int gpio);
void GpioSetOutput(unsigned int gpio, bool initState = false);
void GpioSetPullUp(unsigned int gpio);
void GpioSetPullDown(unsigned int gpio);
void GpioSetState(unsigned int gpio, bool state);
void GpioRegisterForCallback(unsigned int gpio, bool edge); // false=falling, true=rising
void GpioRegisterForCallback(unsigned int gpio, PigpiodHandler::EventEdge edge); // false=falling, true=rising
void UBXSetCfgTP5(const UbxTimePulseStruct& tp);
void UBXSetAopCfg(bool enable = true, uint16_t maxOrbErr = 0);
void UBXSaveCfg(uint8_t devMask = QtSerialUblox::DEV_BBR | QtSerialUblox::DEV_FLASH);
void setSamplingTriggerSignal(GPIO_SIGNAL signalName);
void timeMarkIntervalCountUpdate(uint16_t newCounts, double lastInterval);
void requestMqttConnectionStatus();
void eventMessage(const QString& messageString);
void eventInterval(quint64 nsecs);

private slots:
void onRateBufferReminder();
Expand Down Expand Up @@ -227,7 +230,15 @@ private slots:
std::shared_ptr<DeviceFunction<DeviceType::IO_EXTENDER>> io_extender_p;
std::shared_ptr<Adafruit_SSD1306> oled_p {};
std::shared_ptr<UbloxI2c> ublox_i2c_p {};

/*
float biasVoltage = 0.;
bool biasON = false;
GPIO_SIGNAL eventTrigger { UNDEFINED_SIGNAL };
bool gainSwitch = false;
bool preampStatus[2];
uint8_t pcaPortMask = 0;
QVector<float> dacThresh; // do not give values here because of push_back in constructor of deamon
*/
QPointer<PigpiodHandler> pigHandler;
QPointer<TDC7200> tdc7200;
bool spiDevicePresent = false;
Expand Down
94 changes: 48 additions & 46 deletions daemon/include/pigpiodhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,80 @@

#include "utility/gpio_mapping.h"
#include <gpio_pin_definitions.h>
#include <memory>
#include <muondetector_structs.h>
#include <mutex>
#include <thread>

#define XOR_RATE 0
#define AND_RATE 1
#define COMBINED_RATE 2

static QVector<unsigned int> DEFAULT_VECTOR;
#include <gpiod.h>

class PigpiodHandler : public QObject {
Q_OBJECT

public:
explicit PigpiodHandler(QVector<unsigned int> gpioPins = DEFAULT_VECTOR, unsigned int spi_freq = 61035,
uint32_t spi_flags = 0, QObject* parent = nullptr);
// can't make it private because of access of PigpiodHandler with global pointer
enum PinBias : std::uint8_t {
BiasDisabled = 0x00,
PullDown = 0x01,
PullUp = 0x02,
ActiveLow = 0x04,
OpenDrain = 0x08,
OpenSource = 0x10
};
enum class EventEdge {
RisingEdge,
FallingEdge,
BothEdges
};

static constexpr unsigned int UNDEFINED_GPIO { 256 };

explicit PigpiodHandler(std::vector<unsigned int> gpioPins, QObject* parent = nullptr);
~PigpiodHandler();

QDateTime startOfProgram, lastSamplingTime; // the exact time when the program starts (Utc)
QElapsedTimer elapsedEventTimer;
GPIO_SIGNAL samplingTriggerSignal = EVT_XOR;

double clockMeasurementSlope = 0.;
double clockMeasurementOffset = 0.;
uint64_t gpioTickOverflowCounter = 0;
quint64 lastTimeMeasurementTick = 0;

bool isInhibited() const { return inhibit; }
void setInhibited(bool inh = true) { inhibit = inh; }

signals:
void signal(uint8_t gpio_pin);
void samplingTrigger();
void eventInterval(quint64 nsecs);
void timePulseDiff(qint32 usecs);

// spi related signals
void spiData(uint8_t reg, std::string data);
void event(unsigned int gpio_pin, EventTime timestamp);

public slots:
void start();
void stop();
bool initialised();
void setInput(unsigned int gpio);
void setOutput(unsigned int gpio);
void setPullUp(unsigned int gpio);
void setPullDown(unsigned int gpio);
void setGpioState(unsigned int gpio, bool state);
void setSamplingTriggerSignal(GPIO_SIGNAL signalName) { samplingTriggerSignal = signalName; }
void registerForCallback(unsigned int gpio, bool edge); // false=falling, true=rising

// spi related slots
void writeSpi(uint8_t command, std::string data);
void readSpi(uint8_t command, unsigned int bytesToRead);
bool setPinInput(unsigned int gpio);
bool setPinOutput(unsigned int gpio, bool initState);

bool setPinBias(unsigned int gpio, std::uint8_t bias_flags);
bool setPinState(unsigned int gpio, bool state);
bool registerInterrupt(unsigned int gpio, EventEdge edge);
bool unRegisterInterrupt(unsigned int gpio);
void setInhibited(bool inh = true) { inhibit = inh; }

private:
bool isInitialised = false;
bool spiInitialised = false;
bool spiInitialise(); // will be executed at first spi read/write command
bool isSpiInitialised();
unsigned int spiClkFreq; //3906250; // TDC7200: up to 20MHz SCLK
// Raspi: Core clock speed of 250MHz can be devided by any even number from 2 to 65536
// => 3.814kHz to 125MHz
/*
* spi_flags consists of the least significant 22 bits.

21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
b b b b b b R T n n n n W A u2 u1 u0 p2 p1 p0 m m
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
word size bits msb msb only-3wire 3wire aux CEx? activ-low? spi-mode
*/
unsigned int spiFlags; // fixed value for now
bool isInitialised { false };

QTimer gpioClockTimeMeasurementTimer;

void measureGpioClockTime();
bool inhibit = false;
int verbose = 0;
void reloadInterruptSettings();
[[gnu::hot]] void eventHandler(struct gpiod_line* line);
[[gnu::hot]] void processEvent(unsigned int gpio, std::shared_ptr<gpiod_line_event> line_event);

bool inhibit { false };
int verbose { 0 };
gpiod_chip* fChip { nullptr };
std::map<unsigned int, gpiod_line*> fInterruptLineMap {};
std::map<unsigned int, gpiod_line*> fLineMap {};
bool fThreadRunning { false };
std::map<unsigned int, std::unique_ptr<std::thread>> fThreads {};
std::mutex fMutex;
};

#endif // PIGPIODHANDLER_H
14 changes: 6 additions & 8 deletions daemon/include/utility/gpio_mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

#define MAX_HW_VER 3

// define pins on the raspberry pi, UBIAS_EN is the power on/off pin for bias voltage
// PREAMP_1/2 enables the DC voltage to power the preamp through the signal cable
// Mapping between signals of the MuonPi hardware interface and the actual GPIO pins of the RaspberryPi
// ATTENTION:
// TO MAKE IT MORE SIMPLE THERE WILL BE ONLY PIGPIO NAMING STANDARD,
// NO WIRING PI FROM NOW
// All pins are numbered according to the BCM designation

static const std::map<GPIO_SIGNAL, unsigned int> GPIO_PINMAP_VERSIONS[MAX_HW_VER + 1] = {
{
Expand All @@ -27,9 +25,9 @@ static const std::map<GPIO_SIGNAL, unsigned int> GPIO_PINMAP_VERSIONS[MAX_HW_VER
{ STATUS1, 13 },
{ STATUS2, 19 },
{ STATUS3, 26 },
{ TDC_INTB, 20 },
{ TDC_STATUS, 21 },
{ EXT_TRIGGER, 21 } },
{ TDC_INTB, 24 },
{ TDC_STATUS, 25 },
{ EXT_TRIGGER, 16 } },
{ /* Pin mapping, HW Version 2 */
{ UBIAS_EN, 26 },
{ PREAMP_1, 4 },
Expand Down Expand Up @@ -74,7 +72,7 @@ inline GPIO_SIGNAL bcmToGpioSignal(unsigned int bcmGpioNumber)
while (i != GPIO_PINMAP.cend() && i->second != bcmGpioNumber)
++i;
if (i == GPIO_PINMAP.cend())
return UNDEFINED_PIN;
return UNDEFINED_SIGNAL;
return i->first;
}

Expand Down
14 changes: 7 additions & 7 deletions daemon/include/utility/ratebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

using namespace std::literals;

constexpr double MAX_AVG_RATE { 100. };
constexpr unsigned long MAX_BURST_MULTIPLICITY { 10 };
constexpr std::chrono::microseconds MAX_BUFFER_TIME { 60s };
constexpr double MAX_AVG_RATE { 500. };
constexpr unsigned long MAX_BURST_MULTIPLICITY { 20 };
constexpr std::chrono::microseconds MAX_BUFFER_TIME { 30s };
constexpr std::chrono::microseconds MAX_DEADTIME { static_cast<unsigned long>(1e+6 / MAX_AVG_RATE) };
constexpr std::chrono::microseconds DEADTIME_INCREMENT { 50 };

Expand Down Expand Up @@ -65,12 +65,12 @@ class EventRateBuffer : public QObject {
[[nodiscard]] auto lastEventTime() const -> EventTime;

signals:
void filteredEvent(uint8_t gpio, EventTime event_time);
void eventIntervalSignal(uint8_t gpio, std::chrono::nanoseconds ns);
void filteredEvent(unsigned int gpio, EventTime event_time);
void eventIntervalSignal(unsigned int gpio, std::chrono::nanoseconds ns);

public slots:
//void onEvent(unsigned int gpio, EventTime event_time);
void onEvent(uint8_t gpio);
void onEvent(unsigned int gpio, EventTime event_time);
//void onEvent(uint8_t gpio);

private:
double fRateLimit { MAX_AVG_RATE };
Expand Down
Loading