diff --git a/lib/RS485/RS485_Driver.cpp b/lib/RS485/RS485_Driver.cpp index 9228ed1..0bc0588 100644 --- a/lib/RS485/RS485_Driver.cpp +++ b/lib/RS485/RS485_Driver.cpp @@ -1,14 +1,7 @@ #include "RS485_driver.h" #include #include -#include -#ifdef ESP32 #include -#else -#define be16toh(x) __bswap16(x) -#define htobe16(x) __bswap16(x) -#define htole16(x) x -#endif uint8_t data[][8] = { // ESP32-S3-POE-ETH-8DI-8RO Control Command (RS485 receiving data) @@ -37,7 +30,7 @@ uint8_t Send_Data[][8] = { {0x01, 0x05, 0x00, 0xFF, 0x00, 0x00, 0xFD, 0xFA}, // Modbus RTU Relay ALL OFF }; -#define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE +// #define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE void printBytes(const std::vector &b) { Serial0.flush(); @@ -56,50 +49,41 @@ namespace drivers //////////// RS485 //////////// //////////////////////////////// -#ifdef ESP32 - RS485::RS485(const uint32_t baud, const SerialConfig conf) + RS485::RS485(const uint32_t baud, const SerialConfig conf): m_serial(Serial1) { LOG_INFO("Init serial port 1"); - // m_serial = std::make_unique(1); // RS485 is hardwired to serial port 1 - Serial1.begin(baud, conf, 18, 17); - Serial1.flush(); + // RS485 is hardwired to serial port 1 + m_serial.begin(baud, conf, 18, 17); + m_serial.flush(); } -#else - RS485::RS485(const uint32_t baud) - { - LOG_INFO("Init serial port 1"); - m_serial = std::make_unique(PORT); // RS485 is hardwired to serial port 1 - m_serial->begin(baud); - } -#endif const bool RS485::write(const std::vector data) { - return data.size() == Serial1.write(data.data(), data.size()); + return data.size() == m_serial.write(data.data(), data.size()); } const bool RS485::readAll(std::vector &data) { - const uint32_t avail(Serial1.available()); + const uint32_t avail(m_serial.available()); if (avail == 0) return true; data.resize(avail); - return data.size() == m_serial->readBytes(data.data(), avail); + return data.size() == m_serial.readBytes(data.data(), avail); } const bool RS485::readN(const uint16_t nBytes, std::vector &data) { data.resize(nBytes); - if (Serial1.readBytes(data.data(), nBytes) == nBytes) + if (m_serial.readBytes(data.data(), nBytes) == nBytes) return true; return false; } const bool RS485::readUntil(const uint8_t ch, std::vector &data) { - const uint32_t avail(Serial1.available()); + const uint32_t avail(m_serial.available()); data.resize(avail); - Serial1.readBytesUntil(ch, data.data(), avail); + m_serial.readBytesUntil(ch, data.data(), avail); data.shrink_to_fit(); return true; } @@ -107,7 +91,6 @@ namespace drivers //////////////////////////////// //////////// MODBUS //////////// //////////////////////////////// -#ifdef ESP32 MODBUS::MODBUS(const uint32_t baud, const SerialConfig conf) : RS485::RS485(baud, conf) { std::vector garbage; @@ -115,12 +98,7 @@ namespace drivers LOG_INFO("Init MODBUS Master Mode"); m_crc.reset(CRC16_MODBUS_POLYNOME, CRC16_MODBUS_INITIAL, CRC16_MODBUS_XOR_OUT, CRC16_MODBUS_REV_IN, CRC16_MAXIM_REV_OUT); } -#else - MODBUS::MODBUS(const uint32_t baud) : RS485::RS485(baud) - { - LOG_INFO("Init MODBUS Master Mode"); - } -#endif + // Func 0x01 const bool MODBUS::readCoils(const uint8_t device, const uint16_t reg, const uint16_t num, std::vector &coils) { @@ -157,7 +135,7 @@ namespace drivers const bool MODBUS::writeCoil(const uint8_t device, const uint16_t coil, const bool value) { constexpr uint8_t func = 0x05; - LOG_DEBUG("Write single coils: dev[", device, "], coil[", coil, "], value[", value ? "true" : "false", "]"); + LOG_DEBUG("Write single coil: dev[", device, "], coil[", coil, "], value[", value ? "true" : "false", "]"); return writeBinary(device, func, coil, 1, {value}); } @@ -181,7 +159,7 @@ namespace drivers const bool MODBUS::writeRegisters(const uint8_t device, const uint16_t reg, const std::vector &values) { constexpr uint8_t func = 0x10; - LOG_DEBUG("Write multi coils: dev[", device, "], start[", reg, "], num[", values.size(), "]"); + LOG_DEBUG("Write multi registers: dev[", device, "], start[", reg, "], num[", values.size(), "]"); return writeInteger(device, func, reg, values.size(), values); } @@ -196,7 +174,7 @@ namespace drivers LOG_ERROR("Failed send readBinary command"); return false; } - const uint16_t nRespDataBytes = 1 + (uint16_t)(bits / 8); // 1 bit for every coil, if not 8 mutiple padded with zeroes + const uint16_t nRespDataBytes = (uint16_t)ceil(bits / 8.0f); // 1 bit for every coil, if not 8 mutiple padded with zeroes const uint16_t expectedRespLen = (RESP_HEADER_SIZE + RESP_CRC_SIZE) + nRespDataBytes; // device + function + nbytes + data[] + crc(16b) std::vector response; if (!readN(expectedRespLen, response)) @@ -228,10 +206,9 @@ namespace drivers const std::vector respData(response.begin() + RESP_HEADER_SIZE, response.end() - sizeof(crc_t)); for (auto it = respData.begin(); it < respData.end(); it++) { - const auto v = *it; for (uint8_t j(0); j < 8 && bitNum < bits; j++) { - const auto cv((0x01 << j) && v); + const bool cv((0x01 << j) & *it); out.push_back(cv); bitNum++; } diff --git a/lib/RS485/RS485_Driver.h b/lib/RS485/RS485_Driver.h index 4f51b71..bbbd5fa 100644 --- a/lib/RS485/RS485_Driver.h +++ b/lib/RS485/RS485_Driver.h @@ -24,18 +24,14 @@ namespace drivers static const uint8_t PORT = 1; public: -#ifdef ESP32 RS485(const uint32_t baud, const SerialConfig conf); -#else - RS485(const uint32_t baud); -#endif const bool write(const std::vector data); const bool readAll(std::vector &data); const bool readN(const uint16_t nBytes, std::vector &data); const bool readUntil(const uint8_t ch, std::vector &data); private: - std::unique_ptr m_serial; + HardwareSerial &m_serial; }; class MODBUS : public RS485 @@ -65,11 +61,8 @@ namespace drivers typedef uint16_t crc_t; public: -#ifdef ESP32 + MODBUS(const uint32_t baud, const SerialConfig conf); -#else - MODBUS(const uint32_t baud); -#endif // Func 0x01 const bool readCoils(const uint8_t device, const uint16_t reg, const uint16_t num, std::vector &coils); diff --git a/platformio.ini b/platformio.ini index 21123a2..acb4022 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,13 +19,22 @@ lib_deps = robtillaart/CRC@^1.0.3 hideakitai/DebugLog@^0.8.4 -[env:nucleo_f401re] -platform = ststm32 -board = nucleo_f401re -framework = arduino +[env:esp32-s3-waveshare8-debug] +platform = ${env:esp32-s3-waveshare8.platform} +board = ${env:esp32-s3-waveshare8.board} +framework = ${env:esp32-s3-waveshare8.framework} lib_deps = bblanchon/ArduinoJson@^7.4.2 arduino-libraries/NTPClient@^3.2.1 knolleary/PubSubClient@^2.8 robtillaart/CRC@^1.0.3 hideakitai/DebugLog@^0.8.4 +build_type = debug +build_flags = + -O0 + -g3 + -ggdb + -fno-inline + -fno-ipa-sra + -fno-tree-sra + -fno-builtin diff --git a/src/main.cpp b/src/main.cpp index 2ea3650..d0c3991 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -103,11 +103,12 @@ void setup() void loop() { - const uint8_t devAddress(0xAA); + const uint8_t tempBoardAddr(0xAA); + const uint8_t relayBoardAddr(0x01); const uint8_t baseRegister(0x00); LOG_INFO("Looop"); std::vector results; - bool success = bus.readHoldingRegisters(devAddress, baseRegister, 8, results); + bool success = bus.readHoldingRegisters(tempBoardAddr, baseRegister, 8, results); if (success) { for (auto i(0); i < results.size(); i++) @@ -116,19 +117,19 @@ void loop() } results.clear(); } - + delay(100); for (auto i(0); i < 8; i++) { - LOG_DEBUG("\nCHANNEL ", i); + LOG_DEBUG("\n\nCHANNEL ", i); std::vector values; - bus.writeCoil(0x01, (uint8_t)i, true); - delay(500); - bus.readCoils(0x01,0x00,8, values); - for (auto v: values ) { - LOG_DEBUG("Coil", i, v ? "True" : "False"); + bus.writeCoil(relayBoardAddr, (uint8_t)i, true); + bus.readCoils(relayBoardAddr,0x00,8, values); + for (auto j(0); j < values.size(); j++) { + LOG_DEBUG("Coil", j, values.at(j) ? "True" : "False"); } - bus.writeCoil(0x01, (uint8_t)i, false); - delay(500); + delay(1000); + bus.writeCoil(relayBoardAddr, (uint8_t)i, false); + delay(1000); } delay(5000); } \ No newline at end of file