diff --git a/src/arduino_platform.cpp b/src/arduino_platform.cpp index 874f3b1..58d9986 100644 --- a/src/arduino_platform.cpp +++ b/src/arduino_platform.cpp @@ -157,26 +157,6 @@ int ArduinoPlatform::readWriteSpi(uint8_t *data, size_t len) return 0; } -void ArduinoPlatform::setupGpio(uint32_t dwPin, uint32_t dwMode) -{ - pinMode(dwPin, dwMode); -} - -void ArduinoPlatform::closeGpio(uint32_t dwPin) -{ - // not used -} - -void ArduinoPlatform::writeGpio(uint32_t dwPin, uint32_t dwVal) -{ - digitalWrite(dwPin, dwVal); -} - -uint32_t ArduinoPlatform::readGpio(uint32_t dwPin) -{ - return digitalRead(dwPin); -} - void print(const char* s) { ArduinoPlatform::SerialDebug->print(s); diff --git a/src/arduino_platform.h b/src/arduino_platform.h index 53a32a8..d7b6f69 100644 --- a/src/arduino_platform.h +++ b/src/arduino_platform.h @@ -40,11 +40,6 @@ class ArduinoPlatform : public Platform void closeSpi() override; int readWriteSpi (uint8_t *data, size_t len) override; - virtual void setupGpio(uint32_t dwPin, uint32_t dwMode) override; - virtual void closeGpio(uint32_t dwPin) override; - virtual void writeGpio(uint32_t dwPin, uint32_t dwVal) override; - virtual uint32_t readGpio(uint32_t dwPin) override; - static Stream* SerialDebug; protected: diff --git a/src/knx/bits.h b/src/knx/bits.h index de25682..3e6a95e 100644 --- a/src/knx/bits.h +++ b/src/knx/bits.h @@ -26,9 +26,11 @@ #define RISING 4 void delay(uint32_t millis); +void delayMicroseconds (unsigned int howLong); uint32_t millis(); void pinMode(uint32_t dwPin, uint32_t dwMode); void digitalWrite(uint32_t dwPin, uint32_t dwVal); +uint32_t digitalRead(uint32_t dwPin); typedef void (*voidFuncPtr)(void); void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode); diff --git a/src/knx/platform.h b/src/knx/platform.h index 903ad32..5a082aa 100644 --- a/src/knx/platform.h +++ b/src/knx/platform.h @@ -33,11 +33,6 @@ class Platform virtual void closeSpi() = 0; virtual int readWriteSpi (uint8_t *data, size_t len) = 0; - virtual void setupGpio(uint32_t dwPin, uint32_t dwMode) = 0; - virtual void closeGpio(uint32_t dwPin) = 0; - virtual void writeGpio(uint32_t dwPin, uint32_t dwVal) = 0; - virtual uint32_t readGpio(uint32_t dwPin) = 0; - virtual uint8_t* getEepromBuffer(uint16_t size) = 0; virtual void commitToEeprom() = 0; diff --git a/src/knx/rf_physical_layer.cpp b/src/knx/rf_physical_layer.cpp index 03f551a..5b5c9c8 100644 --- a/src/knx/rf_physical_layer.cpp +++ b/src/knx/rf_physical_layer.cpp @@ -250,9 +250,9 @@ void RfPhysicalLayer::spiWriteRegister(uint8_t spi_instr, uint8_t value) tbuf[0] = spi_instr | WRITE_SINGLE_BYTE; tbuf[1] = value; uint8_t len = 2; - _platform.writeGpio(SPI_SS_PIN, LOW); + digitalWrite(SPI_SS_PIN, LOW); _platform.readWriteSpi(tbuf, len); - _platform.writeGpio(SPI_SS_PIN, HIGH); + digitalWrite(SPI_SS_PIN, HIGH); } uint8_t RfPhysicalLayer::spiReadRegister(uint8_t spi_instr) @@ -261,9 +261,9 @@ uint8_t RfPhysicalLayer::spiReadRegister(uint8_t spi_instr) uint8_t rbuf[2] = {0}; rbuf[0] = spi_instr | READ_SINGLE_BYTE; uint8_t len = 2; - _platform.writeGpio(SPI_SS_PIN, LOW); + digitalWrite(SPI_SS_PIN, LOW); _platform.readWriteSpi(rbuf, len); - _platform.writeGpio(SPI_SS_PIN, HIGH); + digitalWrite(SPI_SS_PIN, HIGH); value = rbuf[1]; //printf("SPI_arr_0: 0x%02X\n", rbuf[0]); //printf("SPI_arr_1: 0x%02X\n", rbuf[1]); @@ -275,9 +275,9 @@ uint8_t RfPhysicalLayer::spiWriteStrobe(uint8_t spi_instr) uint8_t tbuf[1] = {0}; tbuf[0] = spi_instr; //printf("SPI_data: 0x%02X\n", tbuf[0]); - _platform.writeGpio(SPI_SS_PIN, LOW); + digitalWrite(SPI_SS_PIN, LOW); _platform.readWriteSpi(tbuf, 1); - _platform.writeGpio(SPI_SS_PIN, HIGH); + digitalWrite(SPI_SS_PIN, HIGH); return tbuf[0]; } @@ -285,9 +285,9 @@ void RfPhysicalLayer::spiReadBurst(uint8_t spi_instr, uint8_t *pArr, uint8_t len { uint8_t rbuf[len + 1]; rbuf[0] = spi_instr | READ_BURST; - _platform.writeGpio(SPI_SS_PIN, LOW); + digitalWrite(SPI_SS_PIN, LOW); _platform.readWriteSpi(rbuf, len + 1); - _platform.writeGpio(SPI_SS_PIN, HIGH); + digitalWrite(SPI_SS_PIN, HIGH); for (uint8_t i=0; i0 on GDO2 - statusGDO2 = _platform.readGpio(GPIO_GDO2_PIN); + statusGDO2 = digitalRead(GPIO_GDO2_PIN); if(prevStatusGDO2 != statusGDO2) { prevStatusGDO2 = statusGDO2; @@ -562,7 +558,7 @@ void RfPhysicalLayer::loop() } // Detect falling edge 1->0 on GDO0 - statusGDO0 = _platform.readGpio(GPIO_GDO0_PIN); + statusGDO0 = digitalRead(GPIO_GDO0_PIN); if(prevStatusGDO0 != statusGDO0) { prevStatusGDO0 = statusGDO0; @@ -655,7 +651,7 @@ void RfPhysicalLayer::loop() } // Detect rising edge 0->1 on GDO2 - statusGDO2 = _platform.readGpio(GPIO_GDO2_PIN); + statusGDO2 = digitalRead(GPIO_GDO2_PIN); if(prevStatusGDO2 != statusGDO2) { prevStatusGDO2 = statusGDO2; @@ -731,7 +727,7 @@ void RfPhysicalLayer::loop() } // Detect falling edge 1->0 on GDO0 - statusGDO0 = _platform.readGpio(GPIO_GDO0_PIN); + statusGDO0 = digitalRead(GPIO_GDO0_PIN); if(prevStatusGDO0 != statusGDO0) { prevStatusGDO0 = statusGDO0; diff --git a/src/linux_platform.cpp b/src/linux_platform.cpp index 8b67a39..758d642 100644 --- a/src/linux_platform.cpp +++ b/src/linux_platform.cpp @@ -572,10 +572,18 @@ void println(void) void pinMode(uint32_t dwPin, uint32_t dwMode) { + gpio_export(dwPin); + gpio_direction(dwPin, dwMode); } void digitalWrite(uint32_t dwPin, uint32_t dwVal) { + gpio_write(dwPin, dwVal); +} + +uint32_t digitalRead(uint32_t dwPin) +{ + return gpio_read(dwPin); } typedef void (*voidFuncPtr)(void); @@ -593,47 +601,24 @@ void LinuxPlatform::cmdLineArgs(int argc, char** argv) _args[argc] = 0; } -void LinuxPlatform::setupGpio(uint32_t dwPin, uint32_t dwMode) -{ - gpio_export(dwPin); - gpio_direction(dwPin, dwMode); -} - -void LinuxPlatform::closeGpio(uint32_t dwPin) -{ - gpio_unexport(dwPin); - // Set direction to input always if we do not need the GPIO anymore? Unsure... - //gpio_direction(dwPin, INPUT); -} - -void LinuxPlatform::writeGpio(uint32_t dwPin, uint32_t dwVal) -{ - gpio_write(dwPin, dwVal); -} - -uint32_t LinuxPlatform::readGpio(uint32_t dwPin) -{ - return gpio_read(dwPin); -} - -/* Datenpuffer fuer die GPIO-Funktionen */ +/* Buffer size for string operations (e.g. snprintf())*/ #define MAXBUFFER 100 -/* GPIO-Pin aktivieren - * Schreiben der Pinnummer nach /sys/class/gpio/export - * Ergebnis: 0 = O.K., -1 = Fehler +/* Activate GPIO-Pin + * Write GPIO pin number to /sys/class/gpio/export + * Result: 0 = success, -1 = error */ int gpio_export(int pin) { - char buffer[MAXBUFFER]; /* Output Buffer */ - ssize_t bytes; /* Datensatzlaenge */ - int fd; /* Filedescriptor */ - int res; /* Ergebnis von write */ + char buffer[MAXBUFFER]; /* Output Buffer */ + ssize_t bytes; /* Used Buffer length */ + int fd; /* Filedescriptor */ + int res; /* Result from write() */ fd = open("/sys/class/gpio/export", O_WRONLY); if (fd < 0) { - perror("Kann nicht auf export schreiben!\n"); + perror("Could not export GPIO pin(open)!\n"); return(-1); } @@ -642,7 +627,7 @@ int gpio_export(int pin) if (res < 0) { - perror("Kann Pin nicht aktivieren (write)!\n"); + perror("Could not export GPIO pin(write)!\n"); return(-1); } @@ -652,21 +637,21 @@ int gpio_export(int pin) return(0); } -/* GPIO-Pin deaktivieren - * Schreiben der Pinnummer nach /sys/class/gpio/unexport - * Ergebnis: 0 = O.K., -1 = Fehler +/* Deactivate GPIO pin + * Write GPIO pin number to /sys/class/gpio/unexport + * Result: 0 = success, -1 = error */ int gpio_unexport(int pin) { - char buffer[MAXBUFFER]; /* Output Buffer */ - ssize_t bytes; /* Datensatzlaenge */ - int fd; /* Filedescriptor */ - int res; /* Ergebnis von write */ + char buffer[MAXBUFFER]; /* Output Buffer */ + ssize_t bytes; /* Used Buffer length */ + int fd; /* Filedescriptor */ + int res; /* Result from write() */ fd = open("/sys/class/gpio/unexport", O_WRONLY); if (fd < 0) { - perror("Kann nicht auf unexport schreiben!\n"); + perror("Could not unexport GPIO pin(open)!\n"); return(-1); } @@ -675,7 +660,7 @@ int gpio_unexport(int pin) if (res < 0) { - perror("Kann Pin nicht deaktivieren (write)!\n"); + perror("Could not unexport GPIO pin(write)!\n"); return(-1); } @@ -683,22 +668,22 @@ int gpio_unexport(int pin) return(0); } -/* Datenrichtung GPIO-Pin festlegen - * Schreiben Pinnummer nach /sys/class/gpioXX/direction - * Richtung dir: 0 = Lesen, 1 = Schreiben - * Ergebnis: 0 = O.K., -1 = Fehler +/* Set GPIO pin mode (input/output) + * Write GPIO pin number to /sys/class/gpioXX/direction + * Direction: 0 = input, 1 = output + * Result: 0 = success, -1 = error */ int gpio_direction(int pin, int dir) { - char path[MAXBUFFER]; /* Buffer fuer Pfad */ - int fd; /* Filedescriptor */ - int res; /* Ergebnis von write */ + char path[MAXBUFFER]; /* Buffer for path */ + int fd; /* Filedescriptor */ + int res; /* Result from write() */ snprintf(path, MAXBUFFER, "/sys/class/gpio/gpio%d/direction", pin); fd = open(path, O_WRONLY); if (fd < 0) { - perror("Kann Datenrichtung nicht setzen (open)!\n"); + perror("Could not set mode for GPIO pin(open)!\n"); return(-1); } @@ -711,7 +696,7 @@ int gpio_direction(int pin, int dir) if (res < 0) { - perror("Kann Datenrichtung nicht setzen (write)!\n"); + perror("Could not set mode for GPIO pin(write)!\n"); return(-1); } @@ -719,26 +704,26 @@ int gpio_direction(int pin, int dir) return(0); } -/* vom GPIO-Pin lesen - * Ergebnis: -1 = Fehler, 0/1 = Portstatus +/* Read from GPIO pin + * Result: -1 = error, 0/1 = GPIO pin state */ int gpio_read(int pin) { - char path[MAXBUFFER]; /* Buffer fuer Pfad */ - int fd; /* Filedescriptor */ - char result[MAXBUFFER] = {0}; /* Buffer fuer Ergebnis */ + char path[MAXBUFFER]; /* Buffer for path */ + int fd; /* Filedescriptor */ + char result[MAXBUFFER] = {0}; /* Buffer for result */ snprintf(path, MAXBUFFER, "/sys/class/gpio/gpio%d/value", pin); fd = open(path, O_RDONLY); if (fd < 0) { - perror("Kann vom GPIO nicht lesen (open)!\n"); + perror("Could not read from GPIO(open)!\n"); return(-1); } if (read(fd, result, 3) < 0) { - perror("Kann vom GPIO nicht lesen (read)!\n"); + perror("Could not read from GPIO(read)!\n"); return(-1); } @@ -746,21 +731,21 @@ int gpio_read(int pin) return(atoi(result)); } -/* auf GPIO schreiben - * Ergebnis: -1 = Fehler, 0 = O.K. +/* Write to GPIO pin + * Result: -1 = error, 0 = success */ int gpio_write(int pin, int value) { - char path[MAXBUFFER]; /* Buffer fuer Pfad */ + char path[MAXBUFFER]; /* Buffer for path */ int fd; /* Filedescriptor */ - int res; /* Ergebnis von write */ + int res; /* Result from write()*/ snprintf(path, MAXBUFFER, "/sys/class/gpio/gpio%d/value", pin); fd = open(path, O_WRONLY); if (fd < 0) { - perror("Kann auf GPIO nicht schreiben (open)!\n"); + perror("Could not write to GPIO(open)!\n"); return(-1); } @@ -773,7 +758,7 @@ int gpio_write(int pin, int value) if (res < 0) { - perror("Kann auf GPIO nicht schreiben (write)!\n"); + perror("Could not write to GPIO(write)!\n"); return(-1); } @@ -781,23 +766,22 @@ int gpio_write(int pin, int value) return(0); } -/* GPIO-Pin auf Detektion einer Flanke setzen. - * Fuer die Flanke (edge) koennen folgende Parameter gesetzt werden: - * 'r' (rising) - steigende Flanke, - * 'f' (falling) - fallende Flanke, - * 'b' (both) - beide Flanken. +/* Set GPIO pin edge detection + * 'r' (rising) + * 'f' (falling) + * 'b' (both) */ int gpio_edge(unsigned int pin, char edge) { - char path[MAXBUFFER]; /* Buffer fuer Pfad */ - int fd; /* Filedescriptor */ + char path[MAXBUFFER]; /* Buffer for path */ + int fd; /* Filedescriptor */ snprintf(path, MAXBUFFER, "/sys/class/gpio/gpio%d/edge", pin); fd = open(path, O_WRONLY | O_NONBLOCK ); if (fd < 0) { - perror("gpio_edge: Kann auf GPIO nicht schreiben (open)!\n"); + perror("Could not set GPIO edge detection(open)!\n"); return(-1); } @@ -816,46 +800,43 @@ int gpio_edge(unsigned int pin, char edge) return 0; } -/* Warten auf Flanke am GPIO-Pin. - * Eingabewerte: pin: GPIO-Pin - * timeout: Wartezeit in Millisekunden - * Der Pin muss voher eingerichtet werden (export, - * direction, edge) - * Rueckgabewerte: <0: Fehler, 0: poll() Timeout, - * 1: Flanke erkannt, Pin lieferte "0" - * 2: Flanke erkannt, Pin lieferte "1" +/* Wait for edge on GPIO pin + * timeout in milliseconds + * Result: <0: error, 0: poll() Timeout, + * 1: edge detected, GPIO pin reads "0" + * 2: edge detected, GPIO pin reads "1" */ int gpio_wait(unsigned int pin, int timeout) { - char path[MAXBUFFER]; /* Buffer fuer Pfad */ - int fd; /* Filedescriptor */ - struct pollfd polldat[1]; /* Variable fuer poll() */ - char buf[MAXBUFFER]; /* Lesepuffer */ - int rc; /* Hilfsvariablen */ + char path[MAXBUFFER]; /* Buffer for path */ + int fd; /* Filedescriptor */ + struct pollfd polldat[1]; /* Variable for poll() */ + char buf[MAXBUFFER]; /* Read buffer */ + int rc; /* Result */ - /* GPIO-Pin dauerhaft oeffnen */ + /* Open GPIO pin */ snprintf(path, MAXBUFFER, "/sys/class/gpio/gpio%d/value", pin); fd = open(path, O_RDONLY | O_NONBLOCK ); if (fd < 0) { - perror("gpio_wait: Kann von GPIO nicht lesen (open)!\n"); + perror("Could not wait for GPIO edge(open)!\n"); return(-1); } - /* poll() vorbereiten */ + /* prepare poll() */ memset((void*)buf, 0, sizeof(buf)); memset((void*)polldat, 0, sizeof(polldat)); polldat[0].fd = fd; polldat[0].events = POLLPRI; - /* eventuell anstehende Interrupts loeschen */ + /* clear any existing detected edges before */ lseek(fd, 0, SEEK_SET); rc = read(fd, buf, MAXBUFFER - 1); rc = poll(polldat, 1, timeout); if (rc < 0) { /* poll() failed! */ - perror("gpio_wait: Poll-Aufruf ging schief!\n"); + perror("Could not wait for GPIO edge(poll)!\n"); close(fd); return(-1); } @@ -870,7 +851,7 @@ int gpio_wait(unsigned int pin, int timeout) { if (rc < 0) { /* read() failed! */ - perror("gpio_wait: Kann von GPIO nicht lesen (read)!\n"); + perror("Could not wait for GPIO edge(read)!\n"); close(fd); return(-2); } diff --git a/src/linux_platform.h b/src/linux_platform.h index cda3af9..a5605cd 100644 --- a/src/linux_platform.h +++ b/src/linux_platform.h @@ -5,7 +5,6 @@ #include #include "knx/platform.h" -extern void delayMicroseconds (unsigned int howLong); extern int gpio_direction(int pin, int dir); extern int gpio_read(int pin); extern int gpio_write(int pin, int value); @@ -55,12 +54,6 @@ public: void closeSpi() override; int readWriteSpi (uint8_t *data, size_t len) override; - //gpio - virtual void setupGpio(uint32_t dwPin, uint32_t dwMode) override; - virtual void closeGpio(uint32_t dwPin) override; - virtual void writeGpio(uint32_t dwPin, uint32_t dwVal) override; - virtual uint32_t readGpio(uint32_t dwPin) override; - //memory uint8_t* getEepromBuffer(uint16_t size) override; void commitToEeprom() override;