EasyOpenTherm.cpp 15.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
/*
 *    https://github.com/Jeroen88/EasyOpenTherm
 *    https://www.tindie.com/products/Metriot/OpenTherm-adapter/
 *
 *    EasyOpenTherm is a library to communicate with OpenTherm compatible devices
 *    Copyright (C) 2022  Jeroen Döll <info@metriot.nl>
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/


#include "EasyOpenTherm.h"




                        OpenTherm::OpenTherm(uint8_t                          rxPin,
                                              uint8_t                         txPin,
                                              time_t                          timeoutMs,
                                              bool                            primary): _rxPin(rxPin), _txPin(txPin), _timeoutMs(timeoutMs), _primary(primary) {
  _OTP = new OTPhysicalLayer(_rxPin, _txPin, primary);

  if(!_OTP) Serial.println("OpenTherm Out of Memory, fail on assert()");
  assert(_OTP != NULL);     // check for Out of Memory
}


                        OpenTherm::~OpenTherm() {
  delete _OTP;  
}


bool                    OpenTherm::status(uint8_t &                           secondaryFlags) {
  uint8_t primaryFlags = uint8_t(OpenTherm::STATUS_FLAGS::PRIMARY_CH_ENABLE) | uint8_t(OpenTherm::STATUS_FLAGS::PRIMARY_DHW_ENABLE) | uint8_t(OpenTherm::STATUS_FLAGS::PRIMARY_COOLING_ENABLE) | uint8_t(OpenTherm::STATUS_FLAGS::PRIMARY_OTC_ENABLE);

  return status(primaryFlags, secondaryFlags);
}


bool                    OpenTherm::status(uint8_t                             primaryFlags,
                                          uint8_t &                           secondaryFlags) {
  secondaryFlags = 0x00;        // See OpenTherm Protocol Specification v2.2 page 25
  return readWrite(READ_WRITE_DATA_ID::STATUS, primaryFlags, secondaryFlags);  
}


bool                    OpenTherm::read(READ_DATA_ID                          msgID,
                                        uint16_t &                            value) {
  OTDataLinkLayer data;
  data.set(OTDataLinkLayer::MSG_TYPE::PRIMARY_TO_SECONDARY_READ_DATA, uint8_t(msgID), 0x0000);

  if(_execute(data)) {
    value = data.value();

    return true;
  }

  return false;
}


bool                    OpenTherm::read(READ_DATA_ID                          msgID,
                                        int16_t &                             value) {    // signed integer
  OTDataLinkLayer data;
  data.set(OTDataLinkLayer::MSG_TYPE::PRIMARY_TO_SECONDARY_READ_DATA, uint8_t(msgID), 0x0000);

  if(_execute(data)) {
    value = int16_t(data.value());

    return true;
  }

  return false;
}


bool                    OpenTherm::read(READ_DATA_ID                          msgID,
                                        uint8_t &                             valueMSB,
                                        uint8_t &                             valueLSB) {
  OTDataLinkLayer data;
  data.set(OTDataLinkLayer::MSG_TYPE::PRIMARY_TO_SECONDARY_READ_DATA, uint8_t(msgID), 0x0000);

  if(_execute(data)) {
    valueMSB = data.valueMSB();
    valueLSB = data.valueLSB();

    return true;
  }

  return false;
}


bool                    OpenTherm::read(READ_DATA_ID                          msgID,
                                        int8_t &                              valueMSB,         // signed intergers
                                        int8_t &                              valueLSB) {
  OTDataLinkLayer data;
  data.set(OTDataLinkLayer::MSG_TYPE::PRIMARY_TO_SECONDARY_READ_DATA, uint8_t(msgID), 0x0000);

  if(_execute(data)) {
    valueMSB = int8_t(data.valueMSB());
    valueLSB = int8_t(data.valueLSB());

    return true;
  }

  return false;
}


bool                    OpenTherm::read(READ_DATA_ID                          msgID,
                                        float &                               value) {
  OTDataLinkLayer data;
  data.set(OTDataLinkLayer::MSG_TYPE::PRIMARY_TO_SECONDARY_READ_DATA, uint8_t(msgID), 0x0000);

  if(_execute(data)) {
    value = float(data.value()) / 256.0;

    return true;
  }

  return false;
}


bool                    OpenTherm::write(WRITE_DATA_ID                        msgID,
                                          uint16_t                            value) {
  OTDataLinkLayer data;
  data.set(OTDataLinkLayer::MSG_TYPE::PRIMARY_TO_SECONDARY_WRITE_DATA, uint8_t(msgID), value);

  return _execute(data);
}


bool                    OpenTherm::write(WRITE_DATA_ID                        msgID,
                                          uint8_t                             valueMSB,
                                          uint8_t                             valueLSB) {
  OTDataLinkLayer data;
  data.set(OTDataLinkLayer::MSG_TYPE::PRIMARY_TO_SECONDARY_WRITE_DATA, uint8_t(msgID), valueMSB, valueLSB);

  return _execute(data);
}


bool                    OpenTherm::write(WRITE_DATA_ID                        msgID,
                                          float                               value) {
  OTDataLinkLayer data;
  data.set(OTDataLinkLayer::MSG_TYPE::PRIMARY_TO_SECONDARY_WRITE_DATA, uint8_t(msgID), uint16_t(value * 256.0f));

  return _execute(data);
}


bool                    OpenTherm::readWrite(READ_WRITE_DATA_ID               msgID,
                                              uint8_t                         valueMSB,
                                              uint8_t &                       valueLSB) {
  OTDataLinkLayer data;
  data.set(OTDataLinkLayer::MSG_TYPE::PRIMARY_TO_SECONDARY_READ_DATA, uint8_t(msgID), valueMSB, valueLSB);

  if(_execute(data)) {
    valueLSB = data.valueLSB();

    return true;
  }

  return false;
}


OpenTherm::ERROR_CODES  OpenTherm::error() {
  return _lastError;
}


bool                    OpenTherm::_execute(OTDataLinkLayer &                 data) {
  _lastError = ERROR_CODES::OK;

  uint32_t startMillis = millis();
  for(;;) {
    if(millis() - startMillis >= _timeoutMs) {
      _lastError = ERROR_CODES::SEND_TIMEOUT;

      _OTP->reset();

      return false;
    }

    if(_OTP->send(data.frame())) break;
  }

  startMillis = millis();
  for(;;) {
    if(millis() - startMillis >= _timeoutMs) {
      _lastError = ERROR_CODES::RECEIVE_TIMEOUT;
      _OTP->reset();

      return false;
    }

    uint32_t frame;
    if(_OTP->receive(frame)) {
      data.set(frame);

      if(data.isValid()) {

        return true;
      } else {
        if(data.parity()) _lastError = ERROR_CODES::PARITY_ERROR;
        else if(data.dataInvalid()) _lastError = ERROR_CODES::INVALID_DATA;
        else if(data.unknownDataID()) _lastError = ERROR_CODES::UNKNOWN_DATA_ID;
        else _lastError = ERROR_CODES::UNKNOWN_ERROR;

        return false;
      }
    }
  }

  return false;
}




                        OTDataLinkLayer::OTDataLinkLayer() {
  _frame = 0;
}

                        OTDataLinkLayer::OTDataLinkLayer(uint32_t             frame) {
  _frame = frame;
}


void                    OTDataLinkLayer::set(uint32_t                         frame) {
  _frame = frame;
}


void                    OTDataLinkLayer::set(MSG_TYPE                         msgType,
                                              uint8_t                         dataID,
                                              uint16_t                        value) {
  _frame = uint32_t(msgType) | (uint32_t(dataID) << 16) | uint32_t(value);
  if(!_parity(_frame)) _frame |= 0x80000000;
}


void                    OTDataLinkLayer::set(MSG_TYPE                         msgType,
                                              uint8_t                         dataID,
                                              uint8_t                         valueMSB,
                                              uint8_t                         valueLSB) {
  _frame = uint32_t(msgType) | (uint32_t(dataID) << 16) | (uint32_t(valueMSB) << 8) | uint32_t(valueLSB);
  if(!_parity(_frame)) _frame |= 0x80000000;
}


bool                    OTDataLinkLayer::parity() {
  return _parity(_frame);
}

OTDataLinkLayer::MSG_TYPE OTDataLinkLayer::type() {
  return OTDataLinkLayer::MSG_TYPE(_frame & 0x70000000);
}


uint8_t                 OTDataLinkLayer::dataID() {
  return uint8_t((_frame & 0xff0000) >> 16);
}


uint16_t                OTDataLinkLayer::value() {
  return uint16_t(_frame & 0xffff);
}


uint8_t                 OTDataLinkLayer::valueMSB() {
  return uint8_t((_frame & 0xff00) >> 8);
}


uint8_t                 OTDataLinkLayer::valueLSB() {
  return uint8_t(_frame & 0xff);
}


uint32_t                OTDataLinkLayer::frame() {
  return _frame;
}


bool                    OTDataLinkLayer::isValid() {
  return parity() && (type() == MSG_TYPE::SECONDARY_TO_PRIMARY_READ_ACK || type() == MSG_TYPE::SECONDARY_TO_PRIMARY_WRITE_ACK || type() == MSG_TYPE::PRIMARY_TO_SECONDARY_READ_DATA || type() == MSG_TYPE::PRIMARY_TO_SECONDARY_WRITE_DATA);
}


bool                    OTDataLinkLayer::dataInvalid() {
  return type() == MSG_TYPE::SECONDARY_TO_PRIMARY_DATA_INVALID || type() == MSG_TYPE::PRIMARY_TO_SECONDARY_INVALID_DATA;
}


bool                    OTDataLinkLayer::unknownDataID() {
  return type() == MSG_TYPE::SECONDARY_TO_PRIMARY_UNKNOWN_DATA_ID;      // Secondary device does not select the DATA-ID, this function should not be called from a secondary
}


// https://stackoverflow.com/questions/21617970/how-to-check-if-value-has-even-parity-of-bits-or-odd
bool                    OTDataLinkLayer::_parity(uint32_t                     frame) {
  frame ^= frame >> 16;
  frame ^= frame >> 8;
  frame ^= frame >> 4;
  frame ^= frame >> 2;
  frame ^= frame >> 1;
  return (~frame) & 1;
}




OTPhysicalLayer * OTPPtr = NULL;

#if defined(ESP32)
void IRAM_ATTR          OTPGenericISR() {
#elif defined(ESP8266)
void ICACHE_RAM_ATTR    OTPGenericISR() {
#else
void                    OTPGenericISR() {
#endif
  if(OTPPtr) OTPPtr->handleInterrupt();
}


                        OTPhysicalLayer::OTPhysicalLayer(uint8_t              rxPin,
                                                          uint8_t             txPin,
                                                          bool                primary): _rxPin(rxPin), _txPin(txPin), _primary(primary) {
  
  pinMode(_rxPin, INPUT);
  pinMode(_txPin, OUTPUT);
  digitalWrite(_txPin, HIGH);       // idle

  if(OTPPtr != NULL) {
    Serial.println("Only one instance of OTPhysicalLayer() may be active at the time. Executing will fail on an assert(false)");
  }
  assert(OTPPtr == NULL);    // For now only one instance allowed, later a list OTPPtr could be a <list> and the ISR a timer ISR (this is needed because now the ISR is attached to a specific pin)
  OTPPtr = this;

  attachInterrupt(digitalPinToInterrupt(_rxPin), OTPGenericISR, CHANGE);
}


                        OTPhysicalLayer::~OTPhysicalLayer() {
  OTPPtr = NULL;

  detachInterrupt(digitalPinToInterrupt(_rxPin));
}


bool                    OTPhysicalLayer::send(uint32_t                        frame) {
  if(_state != STATE::READY && _state != STATE::INVALID) {
    return false;
  }

  if(_state == STATE::READY && millis() - _lastReceivedTimestampMs < 100) {

    return false;     // Wait at least 100 ms after receiving the final bit of the latest frame before sending a new frame
  }

  sendBit(HIGH);                    // start bit

  uint32_t mask = 0x80000000UL;
  while(mask) {                     // data bits
    sendBit((frame & mask) ? HIGH : LOW);
    mask >>= 1;
  }

  sendBit(HIGH);                    // stop bit

  digitalWrite(_txPin, HIGH);       // idle

  _frame = 0;
  _state = STATE::WAITING;

  _lastSentTimestampMs = millis();

  return true;
}


bool                    OTPhysicalLayer::receive(uint32_t &                   frame) {
  if(_state == STATE::INVALID) return false;                  // ::send() will set _state to STATE::WAITING

  if(_state != STATE::READY) {                                // ::handleInterrupt() will set _state to STATE::READY upon receiving a complete frame (including start and stop bits)
    if(_primary && millis() - _lastSentTimestampMs > 800) {   // ::send() will set _lastSentTimestampMs to after sending the final bit. A secondary never times out, it keeps on listning to the primary
      _state = STATE::INVALID;    // timeout
    }

    return false;
  }

  frame = _frame;                                             // _state == STATE::READY so a frame is available. The frame may be retrieved by calling ::receive() until the next ::send() is called

  return true;
}


void                    OTPhysicalLayer::reset() {
  _state = STATE::INVALID;
}


void                    OTPhysicalLayer::sendBit(uint8_t                      val) {
  digitalWrite(_txPin, (val == HIGH) ? LOW : HIGH);
  delayMicroseconds(500);
  digitalWrite(_txPin, (val == HIGH) ? HIGH : LOW);
  delayMicroseconds(500);
}


 void                   OTPhysicalLayer::handleInterrupt() {
  static volatile uint32_t lastTimestamp;
  static volatile uint32_t mask;

  if(_state == STATE::INVALID) return;                        // Start reception after _send() has set _state to STATE::WAITING
                                                              // ::handleInterrupt() passes from STATE_WATING to STATE::START_BIT, to STATE::RECEIVING for the data bits, to _state STATE::READY after the stop bit
  if(_state == STATE::READY) {
    if(!_primary && digitalRead(_rxPin) == HIGH) {
      _state = STATE::WAITING;
      _frame = 0;
    } else {

      return;                                                 // Nothing to do for a primary device in _state is STATE::READY
    }
  }

  uint32_t timestamp = micros();
  if(_state == STATE::WAITING) {                              // First bit received after sending is the start bit. Init mask for first data bit (the most significant bit is send first)
    if(digitalRead(_rxPin) == HIGH) {           // start bit
      lastTimestamp = timestamp;
      mask = 0x80000000UL;
      _state = STATE::START_BIT;

    } else {
      _state = STATE::INVALID;
    }
  } else if(_state == STATE::START_BIT) {                     // First bit received after the start bit is the first data bit
    if(timestamp - lastTimestamp < 750 && digitalRead(_rxPin) == LOW) {
      lastTimestamp = timestamp;
      _state = STATE::RECEIVING;
    } else {
      _state = STATE::INVALID;
    }
  } else if(_state == STATE::RECEIVING) {                     // Record all received data bits until mask == 0; then alle data bits are consumed and this must be the final stop bit. Set _state to STATE::READY signaliing that a frame is available
    if(timestamp - lastTimestamp > 750) {
      if(mask) {                              // data bit
        if(digitalRead(_rxPin) == LOW) _frame |= mask;
        lastTimestamp = timestamp;
        mask >>= 1;
      } else {                                // stop bit
        _lastReceivedTimestampMs = millis();
        _state = STATE::READY;
      }
    }
  }
}