Skip to content

Commit

Permalink
several bugfixes and improvements in daemon:
Browse files Browse the repository at this point in the history
- removed specific handling of different GPIO pins inside constructor of pigpiodhandler
- added missing connect between signal and slot for registering callbacks in pigpiodhandler
- fixed wrong assignment of EXT_TRIGGER signal for HW version 2
- added a reverse lookup function (GPIO_PIN bcmToGpioSignal(unsigned int)) of the GPIO signal (eg. EVT_AND) and the BCM GPIO pin in gpio_mapping.h
- extended the pin initializations in Daemon::connectToPigpiod() which was incomplete for several signals
- put function to the STATUS1 LED which shows now the status of succesful MQTT connection to the server
- small other mods, mostly rephrasing output strings a bit
  • Loading branch information
hangeza committed Sep 20, 2020
1 parent 6cea2cb commit 6f70af5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 56 deletions.
89 changes: 61 additions & 28 deletions muondetector-daemon/src/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static int setup_unix_signal_handlers()
int Daemon::sighupFd[2];
int Daemon::sigtermFd[2];
int Daemon::sigintFd[2];

void Daemon::handleSigTerm()
{
snTerm->setEnabled(false);
Expand All @@ -110,10 +111,16 @@ void Daemon::handleSigTerm()
if (verbose > 1) {
cout << "\nSIGTERM received" << endl;
}

// save ublox config in built-in flash to provide latest orbit info of sats for next start-up
// and effectively reduce time-to-first-fix (TTFF)
emit UBXSaveCfg();

emit aboutToQuit();
exit(0);
snTerm->setEnabled(true);
}

void Daemon::handleSigHup()
{
snHup->setEnabled(false);
Expand All @@ -124,10 +131,16 @@ void Daemon::handleSigHup()
if (verbose > 1) {
cout << "\nSIGHUP received" << endl;
}
emit aboutToQuit();

// save ublox config in built-in flash to provide latest orbit info of sats for next start-up
// and effectively reduce time-to-first-fix (TTFF)
emit UBXSaveCfg();

emit aboutToQuit();
exit(0);
snHup->setEnabled(true);
}

void Daemon::handleSigInt()
{
snInt->setEnabled(false);
Expand All @@ -145,7 +158,12 @@ void Daemon::handleSigInt()
qDebug().nospace() << "0x" << hex << (uint8_t)(it.key() >> 8) << " 0x" << hex << (uint8_t)(it.key() & 0xff) << " " << dec << it.value();
}
}
emit aboutToQuit();

// save ublox config in built-in flash to provide latest orbit info of sats for next start-up
// and effectively reduce time-to-first-fix (TTFF)
emit UBXSaveCfg();

emit aboutToQuit();
exit(0);
snInt->setEnabled(true);
}
Expand Down Expand Up @@ -648,9 +666,8 @@ Daemon::~Daemon() {
}

void Daemon::connectToPigpiod(){
const QVector<unsigned int> gpio_pins({ GPIO_PINMAP[TDC_INTB], GPIO_PINMAP[PREAMP_1], GPIO_PINMAP[PREAMP_2],
GPIO_PINMAP[UBIAS_EN], GPIO_PINMAP[EVT_AND], GPIO_PINMAP[EVT_XOR],
/*GPIO_PINMAP[ADC_READY],*/ GPIO_PINMAP[TIMEPULSE], GPIO_PINMAP[GAIN_HL] });
const QVector<unsigned int> gpio_pins({ GPIO_PINMAP[EVT_AND], GPIO_PINMAP[EVT_XOR],
GPIO_PINMAP[TIMEPULSE], GPIO_PINMAP[EXT_TRIGGER] });
pigHandler = new PigpiodHandler(gpio_pins);
tdc7200 = new TDC7200(GPIO_PINMAP[TDC_INTB]);
pigThread = new QThread();
Expand Down Expand Up @@ -688,6 +705,7 @@ void Daemon::connectToPigpiod(){
connect(this, &Daemon::GpioSetPullUp, pigHandler, &PigpiodHandler::setPullUp);
connect(this, &Daemon::GpioSetPullDown, pigHandler, &PigpiodHandler::setPullDown);
connect(this, &Daemon::GpioSetState, pigHandler, &PigpiodHandler::setGpioState);
connect(this, &Daemon::GpioRegisterForCallback, pigHandler, &PigpiodHandler::registerForCallback);
connect(pigHandler, &PigpiodHandler::signal, this, &Daemon::sendGpioPinEvent);
connect(pigHandler, &PigpiodHandler::samplingTrigger, this, &Daemon::sampleAdc0Event);
connect(pigHandler, &PigpiodHandler::eventInterval, this, [this](quint64 nsecs)
Expand Down Expand Up @@ -754,19 +772,29 @@ void Daemon::connectToPigpiod(){
emit GpioSetState(GPIO_PINMAP[PREAMP_1],preampStatus[0]);
emit GpioSetState(GPIO_PINMAP[PREAMP_2],preampStatus[1]);
emit GpioSetState(GPIO_PINMAP[GAIN_HL],gainSwitch);
emit GpioSetOutput(GPIO_PINMAP[STATUS1]);
emit GpioSetOutput(GPIO_PINMAP[STATUS2]);
emit GpioSetState(GPIO_PINMAP[STATUS1], 0);
emit GpioSetState(GPIO_PINMAP[STATUS2], 0);
emit GpioSetPullUp(GPIO_PINMAP[ADC_READY]);
emit GpioRegisterForCallback(GPIO_PINMAP[ADC_READY], 1);

if (HW_VERSION>1) {
emit GpioSetInput(GPIO_PINMAP[PREAMP_FAULT]);
emit GpioRegisterForCallback(GPIO_PINMAP[PREAMP_FAULT], 0);
emit GpioSetPullUp(GPIO_PINMAP[PREAMP_FAULT]);
emit GpioSetInput(GPIO_PINMAP[TDC_INTB]);
emit GpioRegisterForCallback(GPIO_PINMAP[TDC_INTB], 0);
emit GpioSetInput(GPIO_PINMAP[TIME_MEAS_OUT]);
emit GpioRegisterForCallback(GPIO_PINMAP[TIME_MEAS_OUT], 0);
}
if (HW_VERSION>=3) {
emit GpioSetOutput(GPIO_PINMAP[IN_POL1]);
emit GpioSetOutput(GPIO_PINMAP[IN_POL2]);
emit GpioSetState(GPIO_PINMAP[IN_POL1],polarity1);
emit GpioSetState(GPIO_PINMAP[IN_POL2],polarity2);
}

if (HW_VERSION>1) {
emit GpioSetInput(GPIO_PINMAP[PREAMP_FAULT]);
emit GpioRegisterForCallback(GPIO_PINMAP[PREAMP_FAULT], 0);
//emit GpioSetPullUp(GPIO_PINMAP[PREAMP_FAULT]);
}
}

void Daemon::connectToGps() {
Expand Down Expand Up @@ -1190,27 +1218,26 @@ void Daemon::receivedTcpMessage(TcpMessage tcpMessage) {
*(tcpMessage.dStream) >> nrEntries;
for (int i=0; i<nrEntries; i++) {
GnssConfigStruct config;
*(tcpMessage.dStream) >> config.gnssId >> config.resTrkCh >>
config.maxTrkCh >> config.flags;
*(tcpMessage.dStream) >> config.gnssId >> config.resTrkCh >> config.maxTrkCh >> config.flags;
configs.push_back(config);
}
emit setGnssConfig(configs);
usleep(150000L);
emit sendPollUbxMsg(UBX_CFG_GNSS);
}
if (msgID == TCP_MSG_KEY::MSG_UBX_CFG_TP5){
UbxTimePulseStruct tp;
*(tcpMessage.dStream) >> tp;
emit UBXSetCfgTP5(tp);
emit sendPollUbxMsg(UBX_CFG_TP5);
return;
}
if (msgID == TCP_MSG_KEY::MSG_UBX_CFG_SAVE){
emit UBXSaveCfg();
emit sendPollUbxMsg(UBX_CFG_TP5);
emit sendPollUbxMsg(UBX_CFG_GNSS);
return;
}
}
if (msgID == TCP_MSG_KEY::MSG_UBX_CFG_TP5){
UbxTimePulseStruct tp;
*(tcpMessage.dStream) >> tp;
emit UBXSetCfgTP5(tp);
emit sendPollUbxMsg(UBX_CFG_TP5);
return;
}
if (msgID == TCP_MSG_KEY::MSG_UBX_CFG_SAVE){
emit UBXSaveCfg();
emit sendPollUbxMsg(UBX_CFG_TP5);
emit sendPollUbxMsg(UBX_CFG_GNSS);
return;
}
if (msgID == TCP_MSG_KEY::MSG_QUIT_CONNECTION){
QString closeAddress;
*(tcpMessage.dStream) >> closeAddress;
Expand Down Expand Up @@ -1468,8 +1495,14 @@ void Daemon::sendMqttStatus(bool connected){
TcpMessage tcpMessage(TCP_MSG_KEY::MSG_MQTT_STATUS);
*(tcpMessage.dStream) << connected;
if (connected != mqttConnectionStatus) {
if (connected) qInfo()<<"MQTT (re)connected";
else qWarning()<<"MQTT connection lost";
if (connected) {
qInfo()<<"MQTT (re)connected";
emit GpioSetState(GPIO_PINMAP[STATUS1],1);
}
else {
qWarning()<<"MQTT connection lost";
emit GpioSetState(GPIO_PINMAP[STATUS1],0);
}
}
mqttConnectionStatus=connected;
emit sendTcpMessage(tcpMessage);
Expand Down
9 changes: 8 additions & 1 deletion muondetector-daemon/src/gpio_mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static const std::map<GPIO_PIN, unsigned int> GPIO_PINMAP_VERSIONS[MAX_HW_VER+1]
{ PREAMP_FAULT , 23 },
{ TDC_INTB, 20 },
{ TDC_STATUS, 21 },
{ EXT_TRIGGER, 21 }
{ EXT_TRIGGER, 16 }
} ,
{
/* Pin mapping, HW Version 3 */
Expand All @@ -109,4 +109,11 @@ static const std::map<GPIO_PIN, unsigned int> GPIO_PINMAP_VERSIONS[MAX_HW_VER+1]

extern std::map<GPIO_PIN, unsigned int> GPIO_PINMAP;

inline GPIO_PIN bcmToGpioSignal(unsigned int bcmGpioNumber) {
std::map<GPIO_PIN, unsigned int>::const_iterator i = GPIO_PINMAP.cbegin();
while (i != GPIO_PINMAP.cend() && i->second!=bcmGpioNumber) ++i;
if (i==GPIO_PINMAP.cend()) return UNDEFINED_PIN;
return i->first;
}

#endif //GPIO_MAPPING_H
12 changes: 6 additions & 6 deletions muondetector-daemon/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ int main(int argc, char *argv[])
parser.addVersionOption();

// add module path for example /dev/gps0 or /dev/ttyAMA0
parser.addPositionalArgument("device", QCoreApplication::translate("main", "Path to gps device\n"
"for example: /dev/ttyAMA0"));
parser.addPositionalArgument("device", QCoreApplication::translate("main", "path to u-blox GNSS device\n"
"e.g. /dev/ttyAMA0"));

// login option
QCommandLineOption mqttLoginOption(QStringList() << "l" << "login",
Expand All @@ -158,7 +158,7 @@ int main(int argc, char *argv[])

// station ID (some name for individual stations if someone has multiple)
QCommandLineOption stationIdOption("id",
QCoreApplication::translate("main", "Set station ID"),
QCoreApplication::translate("main", "set station ID"),
QCoreApplication::translate("main", "ID"));
parser.addOption(stationIdOption);

Expand Down Expand Up @@ -260,15 +260,15 @@ int main(int argc, char *argv[])

// preamps on:
QCommandLineOption preamp1Option(QStringList() << "pre1" << "preamp1",
QCoreApplication::translate("main", "pre-amplifier 1 on or off"));
QCoreApplication::translate("main", "preamplifier channel 1 on/off (0/1)"));
parser.addOption(preamp1Option);
QCommandLineOption preamp2Option(QStringList() << "pre2" << "preamp2",
QCoreApplication::translate("main", "pre-amplifier 2 on or off"));
QCoreApplication::translate("main", "preamplifier channel 2 on/off (0/1)"));
parser.addOption(preamp2Option);

// gain:
QCommandLineOption gainOption(QStringList() << "g" << "gain",
QCoreApplication::translate("main", "gain high"));
QCoreApplication::translate("main", "peak detector high gain select"));
parser.addOption(gainOption);

// event trigger for ADC
Expand Down
37 changes: 19 additions & 18 deletions muondetector-daemon/src/pigpiodhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ static void cbFunction(int user_pi, unsigned int user_gpio,
}
}

PigpiodHandler::PigpiodHandler(QVector<unsigned int> gpio_pins, unsigned int spi_freq, uint32_t spi_flags, QObject *parent)
PigpiodHandler::PigpiodHandler(QVector<unsigned int> gpioPins, unsigned int spi_freq, uint32_t spi_flags, QObject *parent)
: QObject(parent)
{
startOfProgram = QDateTime::currentDateTimeUtc();
Expand All @@ -217,30 +217,27 @@ PigpiodHandler::PigpiodHandler(QVector<unsigned int> gpio_pins, unsigned int spi
qFatal("Could not connect to pigpio daemon. Is pigpiod running? Start with sudo pigpiod -s 1");
return;
}

isInitialised = true;

// gpioPins=gpio_pins;
for (auto& gpio_pin : gpio_pins) {
set_mode(pi, gpio_pin, PI_INPUT);
if (gpio_pin==GPIO_PINMAP[ADC_READY]) set_pull_up_down(pi, gpio_pin, PI_PUD_UP);
if (gpio_pin==GPIO_PINMAP[TDC_INTB]){
int result=callback(pi, gpio_pin, FALLING_EDGE, cbFunction);
if (result<0) {
qCritical()<<"error registering gpio callback for BCM pin"<<gpio_pin;
}
} else {
for (auto& gpioPin : gpioPins) {
//if (GPIO_SIGNAL_MAP[gpioPin].direction!=DIR_IN) continue;
set_mode(pi, gpioPin, PI_INPUT);
// if (gpioPin==ADC_READY) set_pull_up_down(pi, GPIO_PINMAP[gpioPin], PI_PUD_UP);

//qDebug() << "set callback for pin " << gpio_pin;
int result=callback(pi, gpio_pin, RISING_EDGE, cbFunction);
int result=callback(pi, gpioPin, RISING_EDGE, cbFunction);
if (result<0) {
qCritical()<<"error registering gpio callback for BCM pin"<<gpio_pin;
qCritical()<<"error registering gpio callback for BCM pin"<<gpioPin;
}
}
// callback(pi, gpio_pin, FALLING_EDGE, cbFunction);
// if (value==pigif_bad_malloc||
// value==pigif_dublicate_callback||
// value==pigif_bad_callback){
// continue;
// }
}
isInitialised = true;
gpioClockTimeMeasurementTimer.setInterval(GPIO_CLOCK_MEASUREMENT_INTERVAL_MS);
gpioClockTimeMeasurementTimer.setSingleShot(false);
connect(&gpioClockTimeMeasurementTimer, &QTimer::timeout, this, &PigpiodHandler::measureGpioClockTime);
Expand Down Expand Up @@ -270,7 +267,11 @@ void PigpiodHandler::setGpioState(unsigned int gpio, bool state) {
}

void PigpiodHandler::registerForCallback(unsigned int gpio, bool edge) {
callback(pi, gpio, edge?FALLING_EDGE:RISING_EDGE, cbFunction);
int result=callback(pi, gpio, edge?FALLING_EDGE:RISING_EDGE, cbFunction);
if (result<0) {
GPIO_PIN pin=bcmToGpioSignal(gpio);
qCritical()<<"error registering gpio callback for BCM pin"<<GPIO_SIGNAL_MAP[pin].name;
}
}

void PigpiodHandler::writeSpi(uint8_t command, std::string data){
Expand All @@ -281,7 +282,7 @@ void PigpiodHandler::writeSpi(uint8_t command, std::string data){
}
char txBuf[data.size()+1];
txBuf[0] = (char)command;
for (int i = 1; i < data.size() +1; i++){
for (unsigned int i = 1; i < data.size() +1; i++){
txBuf[i] = data[i-1];
}
/*qDebug() << "trying to write: ";
Expand Down Expand Up @@ -309,7 +310,7 @@ void PigpiodHandler::readSpi(uint8_t command, unsigned int bytesToRead){
char rxBuf[bytesToRead+1];
char txBuf[bytesToRead+1];
txBuf[0] = (char)command;
for (int i = 1; i < bytesToRead; i++){
for (unsigned int i = 1; i < bytesToRead; i++){
txBuf[i] = 0;
}
if (spi_xfer(pi, spiHandle, txBuf, rxBuf, bytesToRead+1)!=1+bytesToRead){
Expand All @@ -318,7 +319,7 @@ void PigpiodHandler::readSpi(uint8_t command, unsigned int bytesToRead){
}

std::string data;
for (int i = 1; i < bytesToRead+1; i++){
for (unsigned int i = 1; i < bytesToRead+1; i++){
data += rxBuf[i];
}
// qDebug() << "read back from reg "<<hex<<command<<":";
Expand Down
2 changes: 1 addition & 1 deletion muondetector-daemon/src/pigpiodhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PigpiodHandler : public QObject
{
Q_OBJECT
public:
explicit PigpiodHandler(QVector<unsigned int> gpio_pins = DEFAULT_VECTOR, unsigned int spi_freq = 61035,
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
QDateTime startOfProgram, lastSamplingTime; // the exact time when the program starts (Utc)
Expand Down
8 changes: 6 additions & 2 deletions muondetector-shared/src/gpio_pin_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ enum GPIO_PIN { UBIAS_EN,
};

enum SIGNAL_DIRECTION { DIR_UNDEFINED, DIR_IN, DIR_OUT, DIR_IO };
//enum SIGNAL_POLARITY { POL_UNDEFINED, POL_POSITIVE, POL_NEGATIVE, POL_ANY };

struct GpioSignalDescriptor {
QString name;
SIGNAL_DIRECTION direction;
// SIGNAL_POLARITY polarity;
};

static const QMap<GPIO_PIN, GpioSignalDescriptor> GPIO_SIGNAL_MAP =
Expand Down Expand Up @@ -72,8 +74,10 @@ static const QMap<GPIO_PIN, QString> GPIO_PIN_NAMES =
{ STATUS3, "STATUS3" },
{ PREAMP_FAULT, "PREAMP_FAULT" },
{ EXT_TRIGGER, "EXT_TRIGGER" },
{ TDC_INTB, "TDC_INTB" },
{ TDC_STATUS, "TDC_STATUS" },
{ TDC_INTB, "TDC_INTB" },
{ TDC_STATUS, "TDC_STATUS" },
{ IN_POL1, "IN_POL1" },
{ IN_POL2, "IN_POL2" },
{ UNDEFINED_PIN, "UNDEFINED_PIN" }
};

Expand Down

0 comments on commit 6f70af5

Please sign in to comment.