aboutsummaryrefslogtreecommitdiff
path: root/software
diff options
context:
space:
mode:
authorMDC Service <michael.schmid@mdc-service.de>2022-05-26 11:12:48 +0200
committerGitHub <noreply@github.com>2022-05-26 11:12:48 +0200
commite03b240c34dd1b92d486d0bda45c234837d51f42 (patch)
tree0e6628e410993a573b4cc0fe5186a0a25ce72ca6 /software
parentc3c24556605f92c19d633a5da58abbdb60a1a8da (diff)
Add files via upload
Diffstat (limited to 'software')
-rw-r--r--software/main/Kconfig.projbuild183
-rw-r--r--software/main/PCF8575.cpp457
-rw-r--r--software/main/PCF8575.h191
-rw-r--r--software/main/iic_expander_door.cpp227
-rw-r--r--software/main/iic_expander_door.h27
-rw-r--r--software/main/main.cpp488
-rw-r--r--software/main/main.h134
-rw-r--r--software/main/uart.cpp90
-rw-r--r--software/main/uart.h25
9 files changed, 1822 insertions, 0 deletions
diff --git a/software/main/Kconfig.projbuild b/software/main/Kconfig.projbuild
new file mode 100644
index 0000000..be15538
--- /dev/null
+++ b/software/main/Kconfig.projbuild
@@ -0,0 +1,183 @@
+menu "Example Configuration"
+
+ config EXAMPLE_USE_SPI_ETHERNET
+ bool
+
+ choice EXAMPLE_ETHERNET_TYPE
+ prompt "Ethernet Type"
+ default EXAMPLE_USE_INTERNAL_ETHERNET if IDF_TARGET_ESP32
+ default EXAMPLE_USE_W5500
+ help
+ Select which kind of Ethernet will be used in the example.
+
+ config EXAMPLE_USE_INTERNAL_ETHERNET
+ depends on IDF_TARGET_ESP32
+ select ETH_USE_ESP32_EMAC
+ bool "Internal EMAC"
+ help
+ Select internal Ethernet MAC controller.
+
+ config EXAMPLE_USE_DM9051
+ bool "DM9051 Module"
+ select EXAMPLE_USE_SPI_ETHERNET
+ select ETH_USE_SPI_ETHERNET
+ select ETH_SPI_ETHERNET_DM9051
+ help
+ Select external SPI-Ethernet module (DM9051).
+
+ config EXAMPLE_USE_W5500
+ bool "W5500 Module"
+ select EXAMPLE_USE_SPI_ETHERNET
+ select ETH_USE_SPI_ETHERNET
+ select ETH_SPI_ETHERNET_W5500
+ help
+ Select external SPI-Ethernet module (W5500).
+
+ config EXAMPLE_USE_KSZ8851SNL
+ bool "KSZ8851SNL Module"
+ select EXAMPLE_USE_SPI_ETHERNET
+ select ETH_USE_SPI_ETHERNET
+ select ETH_SPI_ETHERNET_KSZ8851SNL
+ help
+ Select external SPI-Ethernet module (KSZ8851SNL).
+ endchoice # EXAMPLE_ETHERNET_TYPE
+
+ if EXAMPLE_USE_INTERNAL_ETHERNET
+ choice EXAMPLE_ETH_PHY_MODEL
+ prompt "Ethernet PHY Device"
+ default EXAMPLE_ETH_PHY_IP101
+ help
+ Select the Ethernet PHY device to use in the example.
+
+ config EXAMPLE_ETH_PHY_IP101
+ bool "IP101"
+ help
+ IP101 is a single port 10/100 MII/RMII/TP/Fiber Fast Ethernet Transceiver.
+ Goto http://www.icplus.com.tw/pp-IP101G.html for more information about it.
+
+ config EXAMPLE_ETH_PHY_RTL8201
+ bool "RTL8201/SR8201"
+ help
+ RTL8201F/SR8201F is a single port 10/100Mb Ethernet Transceiver with auto MDIX.
+ Goto http://www.corechip-sz.com/productsview.asp?id=22 for more information about it.
+
+ config EXAMPLE_ETH_PHY_LAN8720
+ bool "LAN8720"
+ help
+ LAN8720A is a small footprint RMII 10/100 Ethernet Transceiver with HP Auto-MDIX Support.
+ Goto https://www.microchip.com/LAN8720A for more information about it.
+
+ config EXAMPLE_ETH_PHY_DP83848
+ bool "DP83848"
+ help
+ DP83848 is a single port 10/100Mb/s Ethernet Physical Layer Transceiver.
+ Goto http://www.ti.com/product/DP83848J for more information about it.
+
+ config EXAMPLE_ETH_PHY_KSZ8041
+ bool "KSZ8041"
+ help
+ The KSZ8041 is a single supply 10Base-T/100Base-TX Physical Layer Transceiver.
+ Goto https://www.microchip.com/wwwproducts/en/KSZ8041 for more information about it.
+
+ config EXAMPLE_ETH_PHY_KSZ8081
+ bool "KSZ8081"
+ help
+ The KSZ8081 is a single supply 10Base-T/100Base-TX Physical Layer Transceiver.
+ Goto https://www.microchip.com/wwwproducts/en/KSZ8081 for more information about it.
+ endchoice # EXAMPLE_ETH_PHY_MODEL
+
+ config EXAMPLE_ETH_MDC_GPIO
+ int "SMI MDC GPIO number"
+ default 23
+ help
+ Set the GPIO number used by SMI MDC.
+
+ config EXAMPLE_ETH_MDIO_GPIO
+ int "SMI MDIO GPIO number"
+ default 18
+ help
+ Set the GPIO number used by SMI MDIO.
+ endif # EXAMPLE_USE_INTERNAL_ETHERNET
+
+ if EXAMPLE_USE_SPI_ETHERNET
+ config EXAMPLE_ETH_SPI_HOST
+ int "SPI Host Number"
+ range 0 2
+ default 1
+ help
+ Set the SPI host used to communicate with the SPI Ethernet Controller.
+
+ config EXAMPLE_ETH_SPI_SCLK_GPIO
+ int "SPI SCLK GPIO number"
+ range 0 34 if IDF_TARGET_ESP32
+ range 0 46 if IDF_TARGET_ESP32S2
+ range 0 19 if IDF_TARGET_ESP32C3
+ default 18 if IDF_TARGET_ESP32
+ default 20 if IDF_TARGET_ESP32S2
+ default 6 if IDF_TARGET_ESP32C3
+ help
+ Set the GPIO number used by SPI SCLK.
+
+ config EXAMPLE_ETH_SPI_MOSI_GPIO
+ int "SPI MOSI GPIO number"
+ range 0 34 if IDF_TARGET_ESP32
+ range 0 46 if IDF_TARGET_ESP32S2
+ range 0 19 if IDF_TARGET_ESP32C3
+ default 23 if IDF_TARGET_ESP32
+ default 19 if IDF_TARGET_ESP32S2
+ default 7 if IDF_TARGET_ESP32C3
+ help
+ Set the GPIO number used by SPI MOSI.
+
+ config EXAMPLE_ETH_SPI_MISO_GPIO
+ int "SPI MISO GPIO number"
+ range 0 34 if IDF_TARGET_ESP32
+ range 0 46 if IDF_TARGET_ESP32S2
+ range 0 19 if IDF_TARGET_ESP32C3
+ default 19 if IDF_TARGET_ESP32
+ default 18 if IDF_TARGET_ESP32S2
+ default 2 if IDF_TARGET_ESP32C3
+ help
+ Set the GPIO number used by SPI MISO.
+
+ config EXAMPLE_ETH_SPI_CS_GPIO
+ int "SPI CS GPIO number"
+ range 0 34 if IDF_TARGET_ESP32
+ range 0 46 if IDF_TARGET_ESP32S2
+ range 0 19 if IDF_TARGET_ESP32C3
+ default 16 if IDF_TARGET_ESP32
+ default 21 if IDF_TARGET_ESP32S2
+ default 10 if IDF_TARGET_ESP32C3
+ help
+ Set the GPIO number used by SPI CS.
+
+ config EXAMPLE_ETH_SPI_CLOCK_MHZ
+ int "SPI clock speed (MHz)"
+ range 5 80
+ default 12 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32C3
+ default 36 if IDF_TARGET_ESP32S2
+ help
+ Set the clock speed (MHz) of SPI interface.
+
+ config EXAMPLE_ETH_SPI_INT_GPIO
+ int "Interrupt GPIO number"
+ default 17 if IDF_TARGET_ESP32
+ default 4 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3
+ help
+ Set the GPIO number used by the SPI Ethernet module interrupt line.
+ endif # EXAMPLE_USE_SPI_ETHERNET
+
+ config EXAMPLE_ETH_PHY_RST_GPIO
+ int "PHY Reset GPIO number"
+ default 5
+ help
+ Set the GPIO number used to reset PHY chip.
+ Set to -1 to disable PHY chip hardware reset.
+
+ config EXAMPLE_ETH_PHY_ADDR
+ int "PHY Address"
+ range 0 31
+ default 1
+ help
+ Set PHY address according your board schematic.
+endmenu
diff --git a/software/main/PCF8575.cpp b/software/main/PCF8575.cpp
new file mode 100644
index 0000000..da6508e
--- /dev/null
+++ b/software/main/PCF8575.cpp
@@ -0,0 +1,457 @@
+/*
+ * PCF8575.cpp
+ *
+ * Created on: 26.02.2022
+ * Author: steffen
+ * SPDX-FileCopyrightText: 2022 MDC Service <info@mdc-service.de>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+
+#include "PCF8575.h"
+#include "Wire.h"
+
+/**
+ * Constructor
+ * @param address: i2c address
+ */
+PCF8575::PCF8575(uint8_t address){
+ _wire = &Wire;
+
+ _address = address;
+};
+
+/**
+ * Construcor
+ * @param address: i2c address
+ * @param interruptPin: pin to set interrupt
+ * @param interruptFunction: function to call when interrupt raised
+ */
+PCF8575::PCF8575(uint8_t address, uint8_t interruptPin, void (*interruptFunction)() ){
+ _wire = &Wire;
+
+ _address = address;
+ _interruptPin = interruptPin;
+ _interruptFunction = interruptFunction;
+ _usingInterrupt = true;
+};
+
+#if !defined(__AVR) && !defined(__STM32F1__)
+ /**
+ * Constructor
+ * @param address: i2c address
+ * @param sda: sda pin
+ * @param scl: scl pin
+ */
+ PCF8575::PCF8575(uint8_t address, uint8_t sda, uint8_t scl){
+ _wire = &Wire;
+
+ _address = address;
+ _sda = sda;
+ _scl = scl;
+ };
+
+ /**
+ * Constructor
+ * @param address: i2c address
+ * @param sda: sda pin
+ * @param scl: scl pin
+ * @param interruptPin: pin to set interrupt
+ * @param interruptFunction: function to call when interrupt raised
+ */
+ PCF8575::PCF8575(uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)() ){
+ _wire = &Wire;
+
+ _address = address;
+ _sda = sda;
+ _scl = scl;
+
+ _interruptPin = interruptPin;
+ _interruptFunction = interruptFunction;
+
+ _usingInterrupt = true;
+ };
+#endif
+
+#ifdef ESP32
+ /**
+ * Constructor
+ * @param address: i2c address
+ */
+ PCF8575::PCF8575(TwoWire *pWire, uint8_t address){
+ _wire = pWire;
+
+ _address = address;
+ };
+
+ /**
+ * Construcor
+ * @param address: i2c address
+ * @param interruptPin: pin to set interrupt
+ * @param interruptFunction: function to call when interrupt raised
+ */
+ PCF8575::PCF8575(TwoWire *pWire, uint8_t address, uint8_t interruptPin, void (*interruptFunction)() ){
+ _wire = pWire;
+
+ _address = address;
+ _interruptPin = interruptPin;
+ _interruptFunction = interruptFunction;
+ _usingInterrupt = true;
+ };
+
+ /**
+ * Constructor
+ * @param address: i2c address
+ * @param sda: sda pin
+ * @param scl: scl pin
+ */
+ PCF8575::PCF8575(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl){
+ _wire = pWire;
+
+ _address = address;
+ _sda = sda;
+ _scl = scl;
+ };
+
+ /**
+ * Constructor
+ * @param address: i2c address
+ * @param sda: sda pin
+ * @param scl: scl pin
+ * @param interruptPin: pin to set interrupt
+ * @param interruptFunction: function to call when interrupt raised
+ */
+ PCF8575::PCF8575(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)() ){
+ _wire = pWire;
+
+ _address = address;
+ _sda = sda;
+ _scl = scl;
+
+ _interruptPin = interruptPin;
+ _interruptFunction = interruptFunction;
+
+ _usingInterrupt = true;
+ };
+#endif
+
+/**
+ * wake up i2c controller
+ */
+void PCF8575::begin(){
+ #if !defined(__AVR) && !defined(__STM32F1__)
+ _wire->begin(_sda, _scl);
+ #else
+ // Default pin for AVR some problem on software emulation
+ // #define SCL_PIN _scl
+ // #define SDA_PIN _sda
+ _wire->begin();
+ #endif
+
+// Serial.println( writeMode, BIN);
+// Serial.println( readMode, BIN);
+
+ // Check if there are pins to set low
+ if (writeMode>0 || readMode>0){
+ DEBUG_PRINTLN("Set write mode");
+ _wire->beginTransmission(_address);
+ DEBUG_PRINT(" ");
+ DEBUG_PRINT("usedPin pin ");
+
+
+ uint16_t usedPin = writeMode | readMode;
+ DEBUG_PRINTLN( ~usedPin, BIN);
+// Serial.println( ~usedPin, BIN);
+
+ _wire->write((uint8_t) ~usedPin);
+ _wire->write((uint8_t) (~(usedPin >> 8)));
+
+ DEBUG_PRINTLN("Start end trasmission if stop here check pullup resistor.");
+ _wire->endTransmission();
+ }
+
+ // If using interrupt set interrupt value to pin
+ if (_usingInterrupt){
+ DEBUG_PRINTLN("Using interrupt pin (not all pin is interrupted)");
+ ::pinMode(_interruptPin, INPUT_PULLUP);
+ attachInterrupt(digitalPinToInterrupt(_interruptPin), (*_interruptFunction), FALLING );
+ }
+
+ // inizialize last read
+ lastReadMillis = millis();
+}
+
+/**
+ * Set if fin is OUTPUT or INPUT
+ * @param pin: pin to set
+ * @param mode: mode, supported only INPUT or OUTPUT (to semplify)
+ */
+void PCF8575::pinMode(uint8_t pin, uint8_t mode){
+ DEBUG_PRINT("Set pin ");
+ DEBUG_PRINT(pin);
+ DEBUG_PRINT(" as ");
+ DEBUG_PRINTLN(mode);
+
+ if (mode == OUTPUT){
+ writeMode = writeMode | bit(pin);
+ readMode = readMode & ~bit(pin);
+// DEBUG_PRINT("writeMode: ");
+// DEBUG_PRINT(writeMode, BIN);
+// DEBUG_PRINT("readMode: ");
+// DEBUG_PRINTLN(readMode, BIN);
+
+ }else if (mode == INPUT){
+ writeMode = writeMode & ~bit(pin);
+ readMode = readMode | bit(pin);
+// DEBUG_PRINT("writeMode: ");
+// DEBUG_PRINT(writeMode, BIN);
+// DEBUG_PRINT("readMode: ");
+// DEBUG_PRINTLN(readMode, BIN);
+ }
+ else{
+ DEBUG_PRINTLN("Mode non supported by PCF8575")
+ }
+ DEBUG_PRINT("Write mode: ");
+ DEBUG_PRINTLN(writeMode, BIN);
+
+};
+
+/**
+ * Read value from i2c and bufferize it
+ * @param force
+ */
+void PCF8575::readBuffer(bool force){
+ if (millis() > PCF8575::lastReadMillis+READ_ELAPSED_TIME || _usingInterrupt || force){
+ _wire->requestFrom(_address,(uint8_t)2);// Begin transmission to PCF8575 with the buttons
+ lastReadMillis = millis();
+ if(_wire->available()) // If uint16_ts are available to be recieved
+ {
+ uint16_t iInput = _wire->read();// Read a uint16_t
+ iInput |= _wire->read() << 8;// Read a uint16_t
+ if ((iInput & readMode)>0){
+ byteBuffered = byteBuffered | (uint16_t)iInput;
+ }
+ }
+ }
+}
+
+#ifndef PCF8575_LOW_MEMORY
+ /**
+ * Read value of all INPUT pin
+ * Debounce read more fast than 10millis, non managed for interrupt mode
+ * @return
+ */
+ PCF8575::DigitalInput PCF8575::digitalReadAll(void){
+ DEBUG_PRINTLN("Read from buffer");
+ _wire->requestFrom(_address,(uint8_t)2);// Begin transmission to PCF8575 with the buttons
+ lastReadMillis = millis();
+ if(_wire->available()) // If uint16_ts are available to be recieved
+ {
+ DEBUG_PRINTLN("Data ready");
+ uint16_t iInput = _wire->read();// Read a uint16_t
+ iInput |= _wire->read() << 8;// Read a uint16_t
+
+ if ((iInput & readMode)>0){
+ DEBUG_PRINT("Input ");
+ DEBUG_PRINTLN((uint16_t)iInput, BIN);
+
+ byteBuffered = byteBuffered | (uint16_t)iInput;
+ DEBUG_PRINT("byteBuffered ");
+ DEBUG_PRINTLN(byteBuffered, BIN);
+ }
+ }
+
+ DEBUG_PRINT("Buffer value ");
+ DEBUG_PRINTLN(byteBuffered, BIN);
+#ifdef NOT_SEQUENTIAL_PINOUT
+ if ((bit(0) & readMode)>0) digitalInput.p00 = ((byteBuffered & bit(0))>0)?HIGH:LOW;
+ if ((bit(1) & readMode)>0) digitalInput.p01 = ((byteBuffered & bit(1))>0)?HIGH:LOW;
+ if ((bit(2) & readMode)>0) digitalInput.p02 = ((byteBuffered & bit(2))>0)?HIGH:LOW;
+ if ((bit(3) & readMode)>0) digitalInput.p03 = ((byteBuffered & bit(3))>0)?HIGH:LOW;
+ if ((bit(4) & readMode)>0) digitalInput.p04 = ((byteBuffered & bit(4))>0)?HIGH:LOW;
+ if ((bit(5) & readMode)>0) digitalInput.p05 = ((byteBuffered & bit(5))>0)?HIGH:LOW;
+ if ((bit(6) & readMode)>0) digitalInput.p06 = ((byteBuffered & bit(6))>0)?HIGH:LOW;
+ if ((bit(7) & readMode)>0) digitalInput.p07 = ((byteBuffered & bit(7))>0)?HIGH:LOW;
+ if ((bit(8) & readMode)>0) digitalInput.p10 = ((byteBuffered & bit(8))>0)?HIGH:LOW;
+ if ((bit(9) & readMode)>0) digitalInput.p11 = ((byteBuffered & bit(9))>0)?HIGH:LOW;
+ if ((bit(10) & readMode)>0) digitalInput.p12 = ((byteBuffered & bit(10))>0)?HIGH:LOW;
+ if ((bit(11) & readMode)>0) digitalInput.p13 = ((byteBuffered & bit(11))>0)?HIGH:LOW;
+ if ((bit(12) & readMode)>0) digitalInput.p14 = ((byteBuffered & bit(12))>0)?HIGH:LOW;
+ if ((bit(13) & readMode)>0) digitalInput.p15 = ((byteBuffered & bit(13))>0)?HIGH:LOW;
+ if ((bit(14) & readMode)>0) digitalInput.p16 = ((byteBuffered & bit(14))>0)?HIGH:LOW;
+ if ((bit(15) & readMode)>0) digitalInput.p17 = ((byteBuffered & bit(15))>0)?HIGH:LOW;
+#else
+ if ((bit(0) & readMode)>0) digitalInput.p0 = ((byteBuffered & bit(0))>0)?HIGH:LOW;
+ if ((bit(1) & readMode)>0) digitalInput.p1 = ((byteBuffered & bit(1))>0)?HIGH:LOW;
+ if ((bit(2) & readMode)>0) digitalInput.p2 = ((byteBuffered & bit(2))>0)?HIGH:LOW;
+ if ((bit(3) & readMode)>0) digitalInput.p3 = ((byteBuffered & bit(3))>0)?HIGH:LOW;
+ if ((bit(4) & readMode)>0) digitalInput.p4 = ((byteBuffered & bit(4))>0)?HIGH:LOW;
+ if ((bit(5) & readMode)>0) digitalInput.p5 = ((byteBuffered & bit(5))>0)?HIGH:LOW;
+ if ((bit(6) & readMode)>0) digitalInput.p6 = ((byteBuffered & bit(6))>0)?HIGH:LOW;
+ if ((bit(7) & readMode)>0) digitalInput.p7 = ((byteBuffered & bit(7))>0)?HIGH:LOW;
+ if ((bit(8) & readMode)>0) digitalInput.p8 = ((byteBuffered & bit(8))>0)?HIGH:LOW;
+ if ((bit(9) & readMode)>0) digitalInput.p9 = ((byteBuffered & bit(9))>0)?HIGH:LOW;
+ if ((bit(10) & readMode)>0) digitalInput.p10 = ((byteBuffered & bit(10))>0)?HIGH:LOW;
+ if ((bit(11) & readMode)>0) digitalInput.p11 = ((byteBuffered & bit(11))>0)?HIGH:LOW;
+ if ((bit(12) & readMode)>0) digitalInput.p12 = ((byteBuffered & bit(12))>0)?HIGH:LOW;
+ if ((bit(13) & readMode)>0) digitalInput.p13 = ((byteBuffered & bit(13))>0)?HIGH:LOW;
+ if ((bit(14) & readMode)>0) digitalInput.p14 = ((byteBuffered & bit(14))>0)?HIGH:LOW;
+ if ((bit(15) & readMode)>0) digitalInput.p15 = ((byteBuffered & bit(15))>0)?HIGH:LOW;
+#endif
+ if ((readMode & byteBuffered)>0){
+ byteBuffered = ~readMode & byteBuffered;
+ DEBUG_PRINT("Buffer hight value readed set readed ");
+ DEBUG_PRINTLN(byteBuffered, BIN);
+ }
+ DEBUG_PRINT("Return value ");
+ return digitalInput;
+ };
+#else
+ /**
+ * Read value of all INPUT pin in byte format for low memory usage
+ * Debounce read more fast than 10millis, non managed for interrupt mode
+ * @return
+ */
+ uint16_t PCF8575::digitalReadAll(void){
+ DEBUG_PRINTLN("Read from buffer");
+ _wire->requestFrom(_address,(uint8_t)2);// Begin transmission to PCF8575 with the buttons
+ lastReadMillis = millis();
+ if(_wire->available()) // If uint16_ts are available to be recieved
+ {
+ DEBUG_PRINTLN("Data ready");
+ uint16_t iInput = _wire->read();// Read a uint16_t
+ iInput |= _wire->read() << 8;// Read a uint16_t
+
+ if ((iInput & readMode)>0){
+ DEBUG_PRINT("Input ");
+ DEBUG_PRINTLN((uint16_t)iInput, BIN);
+
+ byteBuffered = byteBuffered | (uint16_t)iInput;
+ DEBUG_PRINT("byteBuffered ");
+ DEBUG_PRINTLN(byteBuffered, BIN);
+ }
+ }
+
+ DEBUG_PRINT("Buffer value ");
+ DEBUG_PRINTLN(byteBuffered, BIN);
+
+ uint16_t byteRead = byteBuffered;
+
+ if ((readMode & byteBuffered)>0){
+ byteBuffered = ~readMode & byteBuffered;
+ DEBUG_PRINT("Buffer hight value readed set readed ");
+ DEBUG_PRINTLN(byteBuffered, BIN);
+ }
+ DEBUG_PRINT("Return value ");
+ return byteRead;
+ };
+#endif
+
+/**
+ * Read value of specified pin
+ * Debounce read more fast than 10millis, non managed for interrupt mode
+ * @param pin
+ * @return
+ */
+uint8_t PCF8575::digitalRead(uint8_t pin){
+ uint8_t value = LOW;
+ if ((bit(pin) & writeMode)>0){
+ DEBUG_PRINTLN("Pin in write mode, return value");
+ DEBUG_PRINT("Write data ");
+ DEBUG_PRINT(writeByteBuffered, BIN);
+ DEBUG_PRINT(" for pin ");
+ DEBUG_PRINT(pin);
+ DEBUG_PRINT(" bin value ");
+ DEBUG_PRINT(bit(pin), BIN);
+ DEBUG_PRINT(" value ");
+ DEBUG_PRINTLN(value);
+
+ if ((bit(pin) & writeByteBuffered)>0){
+ value = HIGH;
+ }else{
+ value = LOW;
+ }
+ return value;
+ }
+
+ DEBUG_PRINT("Read pin ");
+ DEBUG_PRINTLN(pin);
+ // Check if pin already HIGH than read and prevent reread of i2c
+ if ((bit(pin) & byteBuffered)>0){
+ DEBUG_PRINTLN("Pin already up");
+ value = HIGH;
+ }else if ((/*(bit(pin) & byteBuffered)<=0 && */millis() > PCF8575::lastReadMillis+READ_ELAPSED_TIME) /*|| _usingInterrupt*/){
+ DEBUG_PRINTLN("Read from buffer");
+ _wire->requestFrom(_address,(uint8_t)2);// Begin transmission to PCF8575 with the buttons
+ lastReadMillis = millis();
+ if(_wire->available()) // If bytes are available to be recieved
+ {
+ DEBUG_PRINTLN("Data ready");
+ uint16_t iInput = _wire->read();// Read a uint16_t
+ iInput |= _wire->read() << 8;// Read a uint16_t
+
+// Serial.println(iInput, BIN);
+
+ if ((iInput & readMode)>0){
+ DEBUG_PRINT("Input ");
+ DEBUG_PRINTLN((uint16_t)iInput, BIN);
+
+ byteBuffered = byteBuffered | (uint16_t)iInput;
+ DEBUG_PRINT("byteBuffered ");
+ DEBUG_PRINTLN(byteBuffered, BIN);
+
+ if ((bit(pin) & byteBuffered)>0){
+ value = HIGH;
+ }
+ }
+ }
+ }
+ DEBUG_PRINT("Buffer value ");
+ DEBUG_PRINTLN(byteBuffered, BIN);
+ // If HIGH set to low to read buffer only one time
+ if (value==HIGH){
+ byteBuffered = ~bit(pin) & byteBuffered;
+ DEBUG_PRINT("Buffer hight value readed set readed ");
+ DEBUG_PRINTLN(byteBuffered, BIN);
+ }
+ DEBUG_PRINT("Return value ");
+ DEBUG_PRINTLN(value);
+ return value;
+};
+
+/**
+ * Write on pin
+ * @param pin
+ * @param value
+ */
+void PCF8575::digitalWrite(uint8_t pin, uint8_t value){
+ DEBUG_PRINTLN("Begin trasmission");
+ _wire->beginTransmission(_address); //Begin the transmission to PCF8575
+ if (value==HIGH){
+ writeByteBuffered = writeByteBuffered | bit(pin);
+ }else{
+ writeByteBuffered = writeByteBuffered & ~bit(pin);
+ }
+// DEBUG_PRINT("Write data ");
+// DEBUG_PRINT(writeByteBuffered, BIN);
+// DEBUG_PRINT(" for pin ");
+// DEBUG_PRINT(pin);
+// DEBUG_PRINT(" bin value ");
+// DEBUG_PRINT(bit(pin), BIN);
+// DEBUG_PRINT(" value ");
+// DEBUG_PRINTLN(value);
+
+// Serial.print(" --> ");
+// Serial.println(writeByteBuffered);
+// Serial.println((uint8_t) writeByteBuffered);
+// Serial.println((uint8_t) (writeByteBuffered >> 8));
+
+ writeByteBuffered = writeByteBuffered & writeMode;
+ _wire->write((uint8_t) writeByteBuffered);
+ _wire->write((uint8_t) (writeByteBuffered >> 8));
+ DEBUG_PRINTLN("Start end trasmission if stop here check pullup resistor.");
+
+ _wire->endTransmission();
+};
+
+
diff --git a/software/main/PCF8575.h b/software/main/PCF8575.h
new file mode 100644
index 0000000..ab64c47
--- /dev/null
+++ b/software/main/PCF8575.h
@@ -0,0 +1,191 @@
+/*
+ * PCF8575.h
+ *
+ * Created on: 26.02.2022
+ * Author: steffen
+ * SPDX-FileCopyrightText: 2022 MDC Service <info@mdc-service.de>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+
+#ifndef PCF8575_h
+#define PCF8575_h
+
+#include "Wire.h"
+
+#if ARDUINO >= 100
+#include "Arduino.h"
+#else
+#include "WProgram.h"
+#endif
+
+// Uncomment to enable printing out nice debug messages.
+// #define PCF8575_DEBUG
+
+// Uncomment for low memory usage this prevent use of complex DigitalInput structure and free 7byte of memory
+// #define PCF8575_LOW_MEMORY
+
+// Define where debug output will be printed.
+#define DEBUG_PRINTER Serial
+
+// Define to manage original pinout of pcf8575
+// like datasheet but not sequential
+//#define NOT_SEQUENTIAL_PINOUT
+
+// Setup debug printing macros.
+#ifdef PCF8575_DEBUG
+ #define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
+ #define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
+#else
+ #define DEBUG_PRINT(...) {}
+ #define DEBUG_PRINTLN(...) {}
+#endif
+
+#define READ_ELAPSED_TIME 10
+
+//#define P0 B00000001
+//#define P1 B00000010
+//#define P2 B00000100
+//#define P3 B00001000
+//#define P4 B00010000
+//#define P5 B00100000
+//#define P6 B01000000
+//#define P7 B10000000
+//
+#ifdef NOT_SEQUENTIAL_PINOUT
+ #define P00 0
+ #define P01 1
+ #define P02 2
+ #define P03 3
+ #define P04 4
+ #define P05 5
+ #define P06 6
+ #define P07 7
+ #define P10 8
+ #define P11 9
+ #define P12 10
+ #define P13 11
+ #define P14 12
+ #define P15 13
+ #define P16 14
+ #define P17 15
+#else
+ #define P0 0
+ #define P1 1
+ #define P2 2
+ #define P3 3
+ #define P4 4
+ #define P5 5
+ #define P6 6
+ #define P7 7
+ #define P8 8
+ #define P9 9
+ #define P10 10
+ #define P11 11
+ #define P12 12
+ #define P13 13
+ #define P14 14
+ #define P15 15
+#endif
+
+#include <math.h>
+
+
+class PCF8575 {
+public:
+
+ PCF8575(uint8_t address);
+ PCF8575(uint8_t address, uint8_t interruptPin, void (*interruptFunction)() );
+
+#if !defined(__AVR) && !defined(__STM32F1__)
+ PCF8575(uint8_t address, uint8_t sda, uint8_t scl);
+ PCF8575(uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)());
+#endif
+
+#ifdef ESP32
+ ///// changes for second i2c bus
+ PCF8575(TwoWire *pWire, uint8_t address);
+ PCF8575(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl);
+
+ PCF8575(TwoWire *pWire, uint8_t address, uint8_t interruptPin, void (*interruptFunction)() );
+ PCF8575(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl, uint8_t interruptPin, void (*interruptFunction)());
+#endif
+
+ void begin();
+ void pinMode(uint8_t pin, uint8_t mode);
+
+ void readBuffer(bool force = true);
+ uint8_t digitalRead(uint8_t pin);
+ #ifndef PCF8575_LOW_MEMORY
+ struct DigitalInput {
+#ifdef NOT_SEQUENTIAL_PINOUT
+ uint8_t p00;
+ uint8_t p01;
+ uint8_t p02;
+ uint8_t p03;
+ uint8_t p04;
+ uint8_t p05;
+ uint8_t p06;
+ uint8_t p07;
+ uint8_t p10;
+ uint8_t p11;
+ uint8_t p12;
+ uint8_t p13;
+ uint8_t p14;
+ uint8_t p15;
+ uint8_t p16;
+ uint8_t p17;
+#else
+ uint8_t p0;
+ uint8_t p1;
+ uint8_t p2;
+ uint8_t p3;
+ uint8_t p4;
+ uint8_t p5;
+ uint8_t p6;
+ uint8_t p7;
+ uint8_t p8;
+ uint8_t p9;
+ uint8_t p10;
+ uint8_t p11;
+ uint8_t p12;
+ uint8_t p13;
+ uint8_t p14;
+ uint8_t p15;
+#endif
+ } digitalInput;
+
+
+ DigitalInput digitalReadAll(void);
+ #else
+ uint16_t digitalReadAll(void);
+ #endif
+ void digitalWrite(uint8_t pin, uint8_t value);
+
+private:
+ uint8_t _address;
+
+ #if defined(__AVR) || defined(__STM32F1__)
+ uint8_t _sda;
+ uint8_t _scl;
+ #else
+ uint8_t _sda = SDA;
+ uint8_t _scl = SCL;
+ #endif
+
+ TwoWire *_wire;
+
+ bool _usingInterrupt = false;
+ uint8_t _interruptPin = 2;
+ void (*_interruptFunction)(){};
+
+ uint16_t writeMode = 0;
+ uint16_t readMode = 0;
+ uint16_t byteBuffered = 0;
+ unsigned long lastReadMillis = 0;
+
+ uint16_t writeByteBuffered = 0;
+
+};
+
+#endif
diff --git a/software/main/iic_expander_door.cpp b/software/main/iic_expander_door.cpp
new file mode 100644
index 0000000..c0f0385
--- /dev/null
+++ b/software/main/iic_expander_door.cpp
@@ -0,0 +1,227 @@
+/*
+ * iic.cpp
+ *
+ * Created on: 26.02.2022
+ * Author: steffen
+ * SPDX-FileCopyrightText: 2022 MDC Service <info@mdc-service.de>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include "iic.h"
+
+/**
+ * I2C_read: read a register of the port expander
+ *
+ * @param slave_addr I2C slave address
+ * @param reg_addr register to read
+ * @param data result array
+ * @param len number of bytes to read
+ * @returns Value of the register.
+ */
+
+esp_err_t I2C_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t len) {
+ return i2c_master_write_read_device(I2C_MASTER_NUM, slave_addr, &reg_addr, 1, data, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+}
+
+/**
+ * I2C_register_write_byte: write a byte to the port expander
+ *
+ * @param slave_addr I2C slave address
+ * @param reg_addr register to read
+ * @param data value to write
+ * @returns number of bytes written.
+ */
+esp_err_t I2C_register_write_byte(uint8_t slave_addr, uint8_t reg_addr, uint8_t data) {
+ uint8_t write_buf[2] = { reg_addr, data };
+ return i2c_master_write_to_device(I2C_MASTER_NUM, slave_addr, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+}
+
+/**
+ * I2C_init: write a initalize the port expander
+ *
+ * @returns > then 0 if success, otherwise -1.
+ */
+esp_err_t I2C_init(void) {
+ int res1,res2;
+ int I2C_master_port = I2C_MASTER_NUM;
+ i2c_config_t I2C_conf = { .mode = I2C_MODE_MASTER, .sda_io_num = I2C_MASTER_SDA_IO, .scl_io_num = I2C_MASTER_SCL_IO, .sda_pullup_en = GPIO_PULLUP_ENABLE, .scl_pullup_en = GPIO_PULLUP_ENABLE, .master { .clk_speed = I2C_MASTER_FREQ_HZ, } };
+ i2c_param_config(I2C_master_port, &I2C_conf);
+ res1=i2c_driver_install(I2C_master_port, I2C_conf.mode,
+ I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
+
+ I2C_buf[0] = 0x7F; // write data to output port 1
+ I2C_buf[1] = 0x00; // write data to output port 1
+ res1 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ res2 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP2, I2C_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nMaster wrote %d %d %02X\n", res1,res2,I2C_buf[0]);
+
+ I2C_buf[0] = 0x02; // write data to output port 1
+ I2C_buf[1] = 0xFF; // write data to output port 1
+ I2C_buf[2] = 0xFF; // write data to output port 1
+ I2C_buf[3] = 0x00; // write data to output port 1
+ I2C_buf[4] = 0x00; // write data to output port 1
+ res1 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 5, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ res2 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP2, I2C_buf, 5, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nMaster wrote %d %d %02X\n", res1,res2,I2C_buf[0]);
+
+ I2C_buf[0] = 0x11; // write data to output port 1
+ I2C_buf[1] = 0x10; // write data to output port 1
+ res1 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ res2 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP2, I2C_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nMaster wrote %d %d %02X\n", res1,res2,I2C_buf[0]);
+
+
+
+ // printf("\nI2C wrote 0x%02X%02X: %d\n", I2C_buf[0], I2C_buf[1], res1);
+// I2C_buf[0] = 0x00; // lese 3 bytes
+// res2 = i2c_master_read_from_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 1, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+
+
+ /*
+ I2C_buf[0] = 0x8C; // write config port 0 with auto inc
+ I2C_buf[1] = 0xFF; // Port 0 Input
+ I2C_buf[2] = 0x00; // Port 1 Output
+ I2C_buf[3] = 0xFF; // Port 2 Input
+ res = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 4, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nMaster wrote %02X %02X: %d\n", I2C_buf[0], I2C_buf[1], res);
+
+ I2C_buf[0] = 0x05; // write data to output port 1
+ I2C_buf[1] = 0x00;
+ res = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nMaster wrote %02X %02X: %d\n", I2C_buf[0], I2C_buf[1], res);
+*/
+ return res1;
+}
+
+/**
+ * ADC_init: write a initalize the ADC
+ *
+ * @returns > then 0 if success, otherwise -1.
+ */
+esp_err_t ADC_init(void) {
+ int res;
+
+ I2C_buf[0] = 0x01; // -> config register
+ I2C_buf[1] = 0x40; // 0 100 000 0 A0-GND +-6.144 CONT
+ I2C_buf[2] = 0x0; // O00 0 0 0 11 8 SPS no comp
+ res = i2c_master_write_to_device(I2C_MASTER_NUM, ADC_DEFAULT_IIC_ADDR, I2C_buf, 3, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nMaster wrote %02X %02X: %d\n", I2C_buf[0], I2C_buf[1], res);
+ return res;
+}
+
+/**
+ * ADC_init: read all ADC inputs
+ *
+ * @returns > then 0 if success, otherwise -1.
+ */
+int ADCReadAll(void) {
+// for i in range(ADC_CHAN_NUM):
+// data=self.bus.read_i2c_block_data(self.addr,REG_RAW_DATA_START+i,2)
+// val=data[1]<<8|data[0]
+// array.append(val)
+ int res1=0,res2,i;
+ I2C_buf[0] = 0x00; // conversion register
+ I2C_buf[1] = 0x00;
+ I2C_buf[2] = 0x00;
+ res1 = i2c_master_write_to_device(I2C_MASTER_NUM, ADC_DEFAULT_IIC_ADDR, I2C_buf, 1, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+// printf("\nI2C wrote 0x%02X: %d\n", I2C_buf[0], res1);
+
+ for (i=0;i<256;i++) {
+ res2 = i2c_master_read_from_device(I2C_MASTER_NUM, ADC_DEFAULT_IIC_ADDR, I2C_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("I2C read %02x -> ",i);
+// for (i=0;i<4;i++) {
+ printf(" %02X %02X ",I2C_buf[0],I2C_buf[1]);
+// }
+ printf(" %d\n", res2);
+ }
+// printf(" #:S%d R:%d ", res1, res2);
+ fflush(stdout);
+
+ return res1;
+}
+
+/**
+ * DoorIsClosed: test a door, if a switch is closed
+ *
+ * @param doormask specifies what doors to test
+ * @returns 1 of open, otherwise 0
+ */
+int DoorIsClosed(int doormask) {
+ int res1,res2;
+ I2C_buf[0] = 0x80; // Setze CMD read port 0
+ I2C_buf[1] = 0x00;
+ I2C_buf[2] = 0x00;
+ res1 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 1, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+// printf("\nI2C wrote 0x%02X%02X: %d\n", I2C_buf[0], I2C_buf[1], res1);
+ I2C_buf[0] = 0x00; // lese 3 bytes
+ res2 = i2c_master_read_from_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 3, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+// printf("\nI2C read 0x %02X %02X %02X: %d\n", I2C_buf[0], I2C_buf[1], I2C_buf[2], res2);
+ printf(" #:S%d R:%d ", res1, res2);
+ fflush(stdout);
+ // Open 0x0F = 0000 1111
+ // 1 closed 0x0B = 0000 1011
+ // 2 closed 0x0E = 0000 1110
+ // 3 closed 0x0D = 0000 1101
+ // 4 closed 0x07 = 0000 0111
+ if ((I2C_buf[LOCKR_PORT] & doormask)==0) {
+ return 1;
+ } else {
+ return 0;
+ }
+
+}
+
+/**
+ * DoorOpen: open a door lock
+ *
+ * @param door number of the door to open
+ * @returns 0 if successfull
+ */
+int DoorOpen(int door) {
+ int mask=0;
+ int maskr=0;
+ int res;
+ int to=10;
+ switch (door) { // we allow only one door to open
+ case 1: {
+ mask = LOCK1;
+ maskr= LOCK1R;
+ } break;
+ case 2: {
+ mask = LOCK2;
+ maskr= LOCK2R;
+ } break;
+ case 3: {
+ mask = LOCK3;
+ maskr= LOCK3R;
+ } break;
+ default: {
+ mask = LOCK4;
+ maskr= LOCK4R;
+ }
+ }
+
+ I2C_buf[0] = 0x05;
+ I2C_buf[1] = mask;
+ printf("\nMASTER wrote %02X %02X\n", I2C_buf[0], I2C_buf[1]);fflush(stdout);
+ res = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nI2C wrote 0x%02X%02X: %d\n", I2C_buf[0], I2C_buf[1], res);fflush(stdout);
+ while (to>0) {
+ if (DoorIsClosed(maskr)==0) {
+ to=0;
+ } else {
+ to--;
+ vTaskDelay(1);
+ }
+ }
+ I2C_buf[0] = 0x05;
+ I2C_buf[1] = 0x00;
+ printf("\nMASTER wrote 0x%02X%02X\n", I2C_buf[0], I2C_buf[1]);fflush(stdout);
+ res = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nI2C wrote 0x%02X%02X: %d\n", I2C_buf[0], I2C_buf[1], res);
+
+ return 0;
+}
+
+
+
diff --git a/software/main/iic_expander_door.h b/software/main/iic_expander_door.h
new file mode 100644
index 0000000..6776979
--- /dev/null
+++ b/software/main/iic_expander_door.h
@@ -0,0 +1,27 @@
+/*
+ * iic.h
+ *
+ * Created on: 26.02.2022
+ * Author: steffen
+ * SPDX-FileCopyrightText: 2022 MDC Service <info@mdc-service.de>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#ifndef MAIN_IIC_H_
+#define MAIN_IIC_H_
+#include "main.h"
+
+static uint8_t I2C_buf[16];
+
+esp_err_t I2C_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t len) ;
+esp_err_t I2C_register_write_byte(uint8_t slave_addr, uint8_t reg_addr, uint8_t data);
+//uint8_t write_buf[2] = { reg_addr, data };
+// return i2c_master_write_to_device(I2C_MASTER_NUM, slave_addr, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+//}
+esp_err_t I2C_init(void);
+esp_err_t ADC_init(void);
+int ADCReadAll(void);
+int DoorIsClosed(int doormask);
+int DoorOpen(int door);
+
+#endif /* MAIN_IIC_H_ */
diff --git a/software/main/main.cpp b/software/main/main.cpp
new file mode 100644
index 0000000..a7147bb
--- /dev/null
+++ b/software/main/main.cpp
@@ -0,0 +1,488 @@
+/*
+ * main.cpp
+ *
+ * Created on: 26.02.2022
+ * Author: steffen
+ * SPDX-FileCopyrightText: 2022 MDC Service <info@mdc-service.de>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+
+#include "main.h"
+#include "sdkconfig.h"
+
+#include "SimplePgSQL.h"
+#include "MU80X.h"
+#include "HMI.h"
+#include "etc.h"
+#include "uart.h"
+#include "iic.h"
+#include "config.h"
+
+// PGSQL
+#define PGBufferSize 16384
+#define PGCharset "utf-8"
+#define PGUser "mega"
+#define PGPassword "osteoglossum"
+static PGconnection *PGconn;
+static unsigned char PGbuffer[PGBufferSize];
+
+// ETC
+const char *TAG = "SCALADIS";
+//static int cnt = 0;
+//static int protcnt = 0;
+static int serlen;
+static int network_connected = 0;
+MU80X *UHF;
+HMI *LCD;
+
+
+unsigned long long ReadAuth(void);
+
+/** ETH */
+/**
+ * eth_event_handler: Ethernet event handler
+ *
+ * @param arg argument pointer
+ * @param event_base type of the event
+ * @param event_id id of the event
+ * @param event_data event data pointer
+ */
+static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
+ uint8_t mac_addr[6] = { 0 };
+ /* we can get the ethernet driver handle from event data */
+ esp_eth_handle_t eth_handle = *(esp_eth_handle_t*) event_data;
+
+ switch (event_id) {
+ case ETHERNET_EVENT_CONNECTED:
+ esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
+ ESP_LOGI(TAG, "Eth Link Up");
+ ESP_LOGI(TAG, "Eth MAC %02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
+ break;
+ case ETHERNET_EVENT_DISCONNECTED:
+ ESP_LOGI(TAG, "Eth Link Down");
+ break;
+ case ETHERNET_EVENT_START:
+ ESP_LOGI(TAG, "Eth Started");
+ break;
+ case ETHERNET_EVENT_STOP:
+ ESP_LOGI(TAG, "Eth Stopped");
+ break;
+ default:
+ break;
+ }
+}
+
+/**
+ * got_ip_event_handler: event called after received the IP address
+ *
+ * @param arg argument pointer
+ * @param event_base type of the event
+ * @param event_id id of the event
+ * @param event_data event data pointer
+ */
+static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
+ ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data;
+ const esp_netif_ip_info_t *ip_info = &event->ip_info;
+
+ ESP_LOGI(TAG, "Ethernet Got IP Address");
+ ESP_LOGI(TAG, "~~~~~~~~~~~");
+ ESP_LOGI(TAG, "ETHIP :" IPSTR, IP2STR(&ip_info->ip));
+ ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
+ ESP_LOGI(TAG, "ETHGW :" IPSTR, IP2STR(&ip_info->gw));
+ ESP_LOGI(TAG, "~~~~~~~~~~~");
+ network_connected = 1;
+}
+
+/**
+ * ETH_init: initalize Ethernet
+ *
+ * @param dhcp to use DHCP, set this to 0
+ */
+static void ETH_init(int dhcp) {
+ ESP_ERROR_CHECK(esp_netif_init());
+ ESP_ERROR_CHECK(esp_event_loop_create_default());
+
+ // Create new default instance of esp-netif for Ethernet
+ esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
+ esp_netif_t *eth_netif = esp_netif_new(&cfg);
+
+ // Init MAC and PHY configs to default
+ eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
+ eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
+
+ phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
+ phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
+ mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
+ mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
+ esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
+ esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
+ esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
+ esp_eth_handle_t eth_handle = NULL;
+ ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
+ ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
+
+ if (dhcp == 0) {
+ strcpy(netip, (char*) "192.168.178.32");
+ strcpy(netgw, (char*) "192.168.178.1");
+ strcpy(netma, (char*) "255.255.255.0");
+ strcpy(srvip, (char*) "192.168.178.32");
+ srvpo = 5432;
+ boxid = 1;
+ subbx = 1;
+ ReadConfig();
+
+ esp_netif_ip_info_t ip_info;
+ memset(&ip_info, 0, sizeof(esp_netif_ip_info_t));
+ esp_netif_str_to_ip4((const char*) netip, &ip_info.ip);
+ esp_netif_str_to_ip4((const char*) netgw, &ip_info.gw);
+ esp_netif_str_to_ip4((const char*) netma, &ip_info.netmask);
+ esp_netif_set_ip_info(eth_netif, &ip_info);
+ }
+ ESP_ERROR_CHECK(esp_eth_start(eth_handle));
+}
+
+/** UHF */
+/**
+ * UHF_init: initalize the Chafon UHF RFID reader
+ *
+ */
+void UHF_init(void) {
+ int res;
+ UHF = new MU80X(UHF_UART_CHANNEL);
+ res = UHF->iSetRFRegion();
+ res = UHF->iSetPower(30);
+ UHF->DumpBuffer(UHF->RecvBuf, res);
+ UHF->iClearBuffer();
+}
+
+/**
+ * UHF_loop: need to be called regularly, to get notified if RFID-tags inventory change
+ *
+ */
+void UHF_loop(void) {
+ int i;
+ unsigned long long ID;
+ for (i = 0; i < 1000; i++) {
+ if ((ID=ReadAuth())>0) {
+ printf("ID:%lld",ID);fflush(stdout);
+ }
+ UHF->iBufferInventory(7, 0, 10);
+ printf("%d -> Tags in Buffer:%d Tags seen:%d\n", i, UHF->TagsInBuffer,UHF->TagsSeen);
+ fflush(stdout);
+ vTaskDelay(10);
+ };
+ while (0) {
+ vTaskDelay(100);
+ };
+}
+
+/**
+ * ReadAuth: read authentification
+ *
+ * @returns Value of the authbuff.
+ */
+unsigned long long ReadAuth(void) {
+ char authbuff[1024];
+ char *pos;
+ int received = 0;
+ int timeout = 60;
+ serlen = uart_read_bytes(RDR_UART_CHANNEL, authbuff, 256, 1);
+ if (serlen > 0) {
+
+ pos = strchr(authbuff, '\n');
+ if (pos) {
+ *pos = '\0';
+ }
+
+ if (strncmp(authbuff, "CONFIG", 6) == 0) { // we have to receive the init file
+ printf("Ready to receive config file !\n");
+ fflush(stdout);
+ while ((received == 0) & (timeout > 0)) {
+ serlen = uart_read_bytes(RDR_UART_CHANNEL, authbuff, 1023, 100);
+ if (serlen) {
+ authbuff[serlen] = 0;
+ WriteConfig(authbuff);
+ received = 1;
+ } else {
+ vTaskDelay(100);
+ timeout--;
+ printf("TO:%d\n", timeout);
+ fflush(stdout);
+ }
+ }
+ }
+ }
+ authbuff[serlen]=0;
+ printf("SERLEN:%d->%s\n", serlen,authbuff);
+ fflush(stdout);
+ return sscanf("%X",authbuff);
+}
+
+/** GPIO **/
+/**
+ * IO_init: initalize the GPIO
+ *
+ */
+void IO_init(void) {
+ // outputs
+
+#define GPO_BIT_MASK (1ULL << PHY_PWR)
+ gpio_config_t o_conf;
+ o_conf.intr_type = GPIO_INTR_DISABLE;
+ o_conf.mode = GPIO_MODE_OUTPUT;
+ o_conf.pin_bit_mask = GPO_BIT_MASK;
+ o_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
+ o_conf.pull_up_en = GPIO_PULLUP_DISABLE;
+ gpio_config(&o_conf);
+ gpio_set_level((gpio_num_t) PHY_PWR, 1);
+
+ // inputs
+ /*
+ #define GPI_BIT_MASK ((1ULL << SWITCH)|(1ULL << SWITCH))
+ gpio_config_t i_conf;
+ i_conf.intr_type = GPIO_INTR_DISABLE;
+ i_conf.mode = GPIO_MODE_INPUT;
+ i_conf.pin_bit_mask = GPI_BIT_MASK;
+ i_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
+ i_conf.pull_up_en = GPIO_PULLUP_DISABLE;
+ gpio_config(&i_conf);
+ */
+}
+
+/** GPIO **/
+/**
+ * HMI_init: initalize the HMI
+ *
+ */
+void HMI_init(void) {
+ LCD = new HMI(LCD_UART_CHANNEL);
+}
+
+/** PGSQL **/
+/**
+ * PGInit: initalize the PostgreSQL connection
+ *
+ */
+void PGInit(void) {
+ PGconn = new PGconnection(0,PGbuffer,PGBufferSize);
+}
+
+
+/**
+ * getInfo: functional loop
+ *
+ */
+int getInfo(char *info) {
+ int rc, i;
+ int pgstat = 0;
+ int cnt = 1000;
+ char *msg;
+ char lbuf[1024];
+ rc = PGconn->PGsetDbLogin(srvip, srvpo, dbnam, PGUser, PGPassword, PGCharset);
+ printf("Login result:%d\n", rc);
+ if (rc < 1)
+ return -1;
+ pgstat = 1;
+ while (cnt > 0) {
+
+ if (pgstat == 1) { // we are connected to srv/db, now we need to rcv the msgs from srv
+ rc = PGconn->PGstatus();
+ printf("Status:%d\n", rc);
+ fflush(stdout);
+ if (rc == CONNECTION_BAD || rc == CONNECTION_NEEDED) {
+ printf("ERROR: %s", PGconn->PGgetMessage());
+ pgstat = -1;
+ } else if (rc == CONNECTION_OK) {
+ pgstat = 2;
+ printf("Ready to submit qry\n");
+ }
+ }
+
+ if (pgstat == 2) {
+ sprintf(lbuf, "SELECT name,gname FROM accounts WHERE id=(cast(x'%s' AS int));", info);
+ rc = PGconn->PGexecute(lbuf);
+ printf("EXEC result:%d\n", rc);
+// vTaskDelay(1);
+ if (rc == 0) {
+ pgstat = 3;
+ } else {
+ cnt = 0;
+ }
+ }
+ if (pgstat == 3) {
+ rc = PGconn->PGgetData();
+ // printf("RC:%d\n",rc);fflush(stdout);
+ if (rc < 0) {
+ printf("Get Data Error:%d\n", rc);
+ fflush(stdout);
+ } else if (rc > 0) {
+ if (rc & PG_RSTAT_HAVE_COLUMNS) {
+ printf("We got columns !\n");
+ fflush(stdout);
+ int cols = PGconn->PGnfields();
+ printf("Cols: %d\n", cols);
+ fflush(stdout);
+ /*
+ for (i = 0; i < cols; i++) {
+ if (i) printf(" | ");
+ printf(" %s ",PGconn->PGgetColumn(i));
+ }
+ */
+ printf("\n==========\n");
+ fflush(stdout);
+ } else if (rc & PG_RSTAT_HAVE_ROW) {
+ // printf("We got rows !\n");fflush (stdout);
+ for (i = 0; i < PGconn->PGnfields(); i++) {
+ // if (i) printf(" | ");
+ // msg = PGconn->PGgetValue(i);
+ sprintf(lbuf, "Info:\r\n%s\r\n%s", PGconn->PGgetValue(1), PGconn->PGgetValue(0));
+ // if (!msg) msg = (char *) "NULL";
+ // printf(" %s", msg);fflush(stdout);
+ }
+ // printf("\n");fflush(stdout);
+ } else if (rc & PG_RSTAT_HAVE_SUMMARY) {
+ printf("Rows affected: ");
+ printf("%d\n", PGconn->PGntuples());
+ } else if (rc & PG_RSTAT_HAVE_MESSAGE) {
+ printf("We got msg !\n");
+ fflush(stdout);
+ msg = PGconn->PGgetMessage();
+ if (!msg)
+ msg = (char*) "NULL";
+ printf("MSG: %s\n", msg);
+ fflush(stdout);
+ }
+ if (rc & PG_RSTAT_READY) {
+ printf("We made it !\n");
+ fflush(stdout);
+ cnt = 0;
+ break;
+ }
+ }
+ } // pgstat==3
+ vTaskDelay(1);
+ cnt--;
+ } // while (cnt>0)
+ PGconn->PGclose();
+ return rc;
+}
+
+/**
+ * init: initialize all modules
+ *
+ */
+static void init(void) {
+// int res;
+// printf("UART-Init:\n\n");
+// UART_init();
+// printf("IO-Init:\n\n");
+// IO_init();
+// printf("ETH-Init:\n\n");
+// ETH_init(0);
+// printf("UHF-Init:\n\n");
+// UHF_init();
+// printf("HMI-Init:\n\n");
+// HMI_init();
+ printf("I2C-Init:\n\n");
+ ESP_ERROR_CHECK(I2C_init());
+// printf("ADC-Init:\n\n");
+// ESP_ERROR_CHECK(ADC_init());
+ ESP_LOGI(TAG, "I2C initialized successfully");
+
+}
+
+/**
+ * main_loop:
+ *
+ */
+static void main_loop() {
+ int res1 = 0, res2 = 0;
+ printf("Main loop\n");
+ fflush(stdout);
+// res1 = LCD->iSendProt(6, (char*) "page 0", 1);
+ printf("res:%d\n", res1);
+ fflush(stdout);
+// int door=1;
+
+// UHF_loop();
+// EditConfig();
+// UHF_loop();
+
+ while (1) {
+// ADCReadAll();
+
+ I2C_buf[0] = 0x02; // write data to output port 1
+ I2C_buf[1] = 0x55; // write data to output port 1
+ I2C_buf[2] = 0xAA; // write data to output port 1
+ res1 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 3, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ // printf("\nI2C wrote 0x%02X%02X: %d\n", I2C_buf[0], I2C_buf[1], res1);
+// I2C_buf[0] = 0x00; // lese 3 bytes
+// res2 = i2c_master_read_from_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 1, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+
+ printf("\nMaster wrote %d %d %02X\n", res1,res2,I2C_buf[0]);
+
+
+ vTaskDelay(500 / portTICK_PERIOD_MS);
+ I2C_buf[0] = 0x02; // write data to output port 1
+ I2C_buf[1] = 0xAA; // write data to output port 1
+ I2C_buf[2] = 0x55; // write data to output port 1
+ res1 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 3, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ // printf("\nI2C wrote 0x%02X%02X: %d\n", I2C_buf[0], I2C_buf[1], res1);
+// I2C_buf[0] = 0x00; // lese 3 bytes
+// res2 = i2c_master_read_from_device(I2C_MASTER_NUM, I2C_PORTEXP1, I2C_buf, 1, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+
+ printf("\nMaster wrote %d %d %02X\n", res1,res2,I2C_buf[0]);
+
+
+ vTaskDelay(500 / portTICK_PERIOD_MS);
+ }
+
+ while (0) {
+ printf("Closed 1:%d\n",DoorIsClosed(LOCK1R));fflush(stdout);
+ printf("Closed 2:%d\n",DoorIsClosed(LOCK2R));fflush(stdout);
+ printf("Closed 3:%d\n",DoorIsClosed(LOCK3R));fflush(stdout);
+ printf("Closed 4:%d\n",DoorIsClosed(LOCK4R));fflush(stdout);
+ /*
+ res1 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP, I2C_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nI2C wrote 0x%02X%02X: %d\n", I2C_buf[0], I2C_buf[1], res1);
+
+ vTaskDelay(1);
+
+ I2C_buf[0] = 0x05;
+ I2C_buf[1] = 0x00;
+ res1 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP, I2C_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nI2C wrote 0x%02X%02X: %d\n", I2C_buf[0], I2C_buf[1], res1);
+
+ I2C_buf[0] = 0x80; // Setze CMD read port 0
+ I2C_buf[1] = 0x00;
+ I2C_buf[2] = 0x00;
+ res1 = i2c_master_write_to_device(I2C_MASTER_NUM, I2C_PORTEXP, I2C_buf, 1, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nI2C wrote 0x%02X%02X: %d\n", I2C_buf[0], I2C_buf[1], res1);
+ I2C_buf[0] = 0x00; // lese 3 bytes
+ res2 = i2c_master_read_from_device(I2C_MASTER_NUM, I2C_PORTEXP, I2C_buf, 3, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
+ printf("\nI2C read 0x %02X %02X %02X: %d\n", I2C_buf[0], I2C_buf[1], I2C_buf[2], res2);
+ vTaskDelay(2000 / portTICK_PERIOD_MS);
+ printf(" #:S%d R:%d ", res1, res2);
+ fflush(stdout);
+ */
+
+ vTaskDelay(10000 / portTICK_PERIOD_MS);
+ DoorOpen(1);
+// vTaskDelay(10);
+ DoorOpen(2);
+ vTaskDelay(10);
+ DoorOpen(3);
+ vTaskDelay(10);
+ DoorOpen(4);
+ vTaskDelay(10);
+ printf("\n\n\n");fflush(stdout);
+ }
+}
+
+extern "C" {
+void app_main(void) {
+ init();
+ main_loop();
+}
+}
diff --git a/software/main/main.h b/software/main/main.h
new file mode 100644
index 0000000..803c2fb
--- /dev/null
+++ b/software/main/main.h
@@ -0,0 +1,134 @@
+/*
+ * main.h
+ *
+ * Created on: 26.02.2022
+ * Author: steffen
+ * SPDX-FileCopyrightText: 2022 MDC Service <info@mdc-service.de>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+
+#ifndef MAIN_MAIN_H_
+#define MAIN_MAIN_H_
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <freertos/FreeRTOS.h>
+#include <freertos/task.h>
+#include <esp_netif.h>
+#include <esp_eth.h>
+#include <esp_log.h>
+#include <esp_spiffs.h>
+#include <esp_err.h>
+#include <driver/uart.h>
+#include <driver/gpio.h>
+#include <driver/i2c.h>
+#include <lwip/err.h>
+#include <lwip/sockets.h>
+
+
+// +3,3V // Pin 1
+// GND // Pin 2
+// ESP_EN // Pin 3
+#define UHF_RXD_MISO 36 // pin 4 UART
+#define LCD_RXD_MISO 39 // pin 5 UART
+// NC pin 6
+#define I2Cint 35 // pin 7 EXP
+#define SCL 32 // pin 8 EXP
+#define SDA 33 // pin 9 EXP
+#define EMAC_RXD0_RMII 25 // pin 10 ETH
+#define EMAC_RXD1_RMII 26 // pin 11 ETH
+#define EMAC_RX_CRS_DV 27 // pin 12 ETH
+#define HS2_CLK 14 // pin 13 ETH
+#define PHY_PWR 12 // pin 14 ETH
+// // pin 15 GND
+#define I2C_SDA_40p 13 // pin 16 40p
+// NC pin 17 bis 22
+#define HS2_CMD 15 // pin 23 ETH
+#define HS2_DATA0 02 // pin 24 ETH
+#define ETH_CLKREF 0 // pin 25 ETH
+#define UHF_TXD_MOSI 04 // pin 26 UART
+#define LCD_TXD_MOSI 16 // pin 27 UART
+#define EMAC_CLK_OUT_180 17 // pin 28 ETH
+#define SPI_CS 05 // pin 29 40p
+#define MDIO_RMII 18 // pin 30 ETH
+#define EMAC_TXD0_RMII 19 // pin 31 ETH
+//NC pin 32
+#define EMAC_TX_EN_RMII 21 // pin 33 ETH
+#define RDR_RXD_MISO 03 // pin 34 UART
+#define RDR_TXD_MOSI 01 // pin 35 UART
+#define EMAC_TXD1_RMII 22 // pin 36 ETH
+#define MDC_RMII 23 // pin 37 ETH
+//GND pin 38, 39
+#define RDR_UART_CHANNEL 0
+#define RDR_UART_BAUD_RATE 9600
+//#define RDR_UART_BAUD_RATE 115200
+#define RDR_UART_RX_PIN RDR_RXD_MISO
+#define RDR_UART_TX_PIN RDR_TXD_MOSI
+
+#define LCD_UART_CHANNEL 1
+#define LCD_UART_BAUD_RATE 115200
+#define LCD_UART_RX_PIN LCD_RXD_MISO
+#define LCD_UART_TX_PIN LCD_TXD_MOSI
+
+#define UHF_UART_CHANNEL 2 // for easy adaptation in the future
+#define UHF_UART_BAUD_RATE 115200
+#define UHF_UART_RX_PIN UHF_RXD_MISO
+#define UHF_UART_TX_PIN UHF_TXD_MOSI
+
+#define I2C_MASTER_SCL_IO SCL
+#define I2C_MASTER_SDA_IO SDA
+#define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
+#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
+#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
+#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
+#define I2C_MASTER_TIMEOUT_MS 1000
+
+#define I2C_PORTEXP1 0x58
+#define I2C_PORTEXP2 0x5A
+
+#define ADC_DEFAULT_IIC_ADDR 0x48
+#define ADC_CHAN_NUM 8
+#define ADC_REG_RAW_DATA_START 0x10
+#define ADC_REG_VOL_START 0x20
+#define ADC_REG_RTO_START 0x30
+#define ADC_REG_SET_ADDR 0xC0
+
+// GPA Output
+// GPB Input
+
+#define LOCK1 0x40
+#define LOCK2 0x10
+#define LOCK3 0x20
+#define LOCK4 0x80
+#define LOCK_MASK LOCK1+LOCK2+LOCK3+LOCK4
+#define LOCKPORT 0x01
+
+#define LOCK1R 0x04
+#define LOCK2R 0x01
+#define LOCK3R 0x02
+#define LOCK4R 0x08
+#define LOCKR_MASK LOCK1R+LOCK2R+LOCK3R+LOCK4R
+#define LOCKR_PORT 0x02
+
+#define TASK_STACK_SIZE 2048
+#define READ_BUF_SIZE 1024
+
+#define STX 0x02
+#define ETX 0x03
+#define EOT 0x04
+
+#define MAX_TAGS 256
+
+typedef struct {
+ unsigned char b[12];
+ unsigned char ant;
+ unsigned char rssi;
+ unsigned char count;
+} Tag_t;
+
+
+
+
+#endif /* MAIN_MAIN_H_ */
diff --git a/software/main/uart.cpp b/software/main/uart.cpp
new file mode 100644
index 0000000..39a91b7
--- /dev/null
+++ b/software/main/uart.cpp
@@ -0,0 +1,90 @@
+/*
+ * uart.cpp
+ *
+ * Created on: 26.02.2022
+ * Author: steffen
+ * SPDX-FileCopyrightText: 2022 MDC Service <info@mdc-service.de>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+
+#include "uart.h"
+
+/**
+ * UART_init: initalization of the UARTS
+ *
+ */
+void UART_init(void) {
+ int intr_alloc_flags = ESP_INTR_FLAG_IRAM;
+
+ vTaskDelay(100);
+#ifdef DEBUG
+ printf("LCD\n");
+ fflush(stdout);
+#endif
+ uart_config_t lcd_uart_config = {
+ .baud_rate = LCD_UART_BAUD_RATE,
+ .data_bits = UART_DATA_8_BITS,
+ .parity = UART_PARITY_DISABLE,
+ .stop_bits = UART_STOP_BITS_1,
+ .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
+ .source_clk = UART_SCLK_APB,
+ };
+
+ ESP_ERROR_CHECK(uart_driver_install(LCD_UART_CHANNEL, READ_BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
+ ESP_ERROR_CHECK(uart_param_config(LCD_UART_CHANNEL, &lcd_uart_config));
+ ESP_ERROR_CHECK(uart_set_pin(LCD_UART_CHANNEL, LCD_UART_TX_PIN, LCD_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
+
+#ifdef DEBUG
+ printf("UHF\n");
+ fflush(stdout);
+#endif
+ uart_config_t uhf_uart_config = {
+ .baud_rate = UHF_UART_BAUD_RATE,
+ .data_bits = UART_DATA_8_BITS,
+ .parity = UART_PARITY_DISABLE,
+ .stop_bits = UART_STOP_BITS_1,
+ .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
+ .source_clk = UART_SCLK_APB,
+ };
+
+ ESP_ERROR_CHECK(uart_driver_install(UHF_UART_CHANNEL, READ_BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
+ ESP_ERROR_CHECK(uart_param_config(UHF_UART_CHANNEL, &uhf_uart_config));
+ ESP_ERROR_CHECK(uart_set_pin(UHF_UART_CHANNEL, UHF_UART_TX_PIN, UHF_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
+}
+
+/**
+ * UART_read: read from UART
+ *
+ * @param channel channel to read from.
+ * @param buf result buffer.
+ * @param len length of the result buffer.
+ * @returns number of bytes read.
+ */
+int UART_read(int channel, void *buf, int len = READ_BUF_SIZE) {
+ return (uart_read_bytes(channel, buf, len, 1));
+}
+
+/**
+ * UART_write: write to UART
+ *
+ * @param channel channel to read from.
+ * @param buf writing buffer.
+ * @param len length of the writing buffer.
+ * @returns number of bytes written.
+ */
+int UART_write(int channel, void *buf, int len) {
+ return (uart_write_bytes(channel, serout, len));
+}
+
+/**
+ * UART_debug: write a debug msg to UART
+ *
+ * @param buf writing buffer.
+ * @param len length of the writing buffer.
+ */
+void UART_debug(char *buf, int len) {
+ UART_write(0, (void*) buf, len);
+}
+
+
diff --git a/software/main/uart.h b/software/main/uart.h
new file mode 100644
index 0000000..358592b
--- /dev/null
+++ b/software/main/uart.h
@@ -0,0 +1,25 @@
+/*
+ * uart.h
+ *
+ * Created on: 26.02.2022
+ * Author: steffen
+ * SPDX-FileCopyrightText: 2022 MDC Service <info@mdc-service.de>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#ifndef MAIN_UART_H_
+#define MAIN_UART_H_
+#include "main.h"
+
+//static char serin[READ_BUF_SIZE + 16];
+static char serout[READ_BUF_SIZE + 16];
+
+void UART_init(void);
+void UART_debug(char *buf, int len);
+int UART_read(int channel, void *buf, int len);
+int UART_write(int channel, void *buf, int len);
+
+
+
+
+#endif /* MAIN_UART_H_ */