24static const char *
const TAG =
"opentherm";
27OpenTherm *OpenTherm::instance =
nullptr;
40 device_timeout_(device_timeout) {
41 this->isr_in_pin_ = in_pin->to_isr();
42 this->isr_out_pin_ = out_pin->to_isr();
45bool OpenTherm::initialize() {
47 OpenTherm::instance =
this;
49 this->in_pin_->pin_mode(gpio::FLAG_INPUT);
50 this->in_pin_->setup();
51 this->out_pin_->pin_mode(gpio::FLAG_OUTPUT);
52 this->out_pin_->setup();
53 this->out_pin_->digital_write(
true);
56 return this->init_esp32_timer_();
62void OpenTherm::listen() {
64 this->timeout_counter_ = this->device_timeout_ * 5;
66 this->mode_ = OperationMode::LISTEN;
70 this->start_read_timer_();
73void OpenTherm::send(OpenthermData &data) {
75 this->data_ =
data.type;
76 this->data_ = (this->data_ << 12) |
data.id;
77 this->data_ = (this->data_ << 8) |
data.valueHB;
78 this->data_ = (this->data_ << 8) |
data.valueLB;
79 if (!check_parity_(this->data_)) {
80 this->data_ = this->data_ | 0x80000000;
85 this->mode_ = OperationMode::WRITE;
87 this->start_write_timer_();
90bool OpenTherm::get_message(OpenthermData &data) {
91 if (this->mode_ == OperationMode::RECEIVED) {
92 data.type = (this->data_ >> 28) & 0x7;
93 data.id = (this->data_ >> 16) & 0xFF;
94 data.valueHB = (this->data_ >> 8) & 0xFF;
95 data.valueLB = this->data_ & 0xFF;
101bool OpenTherm::get_protocol_error(OpenThermError &error) {
102 if (this->mode_ != OperationMode::ERROR_PROTOCOL) {
106 error.error_type = this->error_type_;
107 error.bit_pos = this->bit_pos_;
108 error.capture = this->capture_;
109 error.clock = this->clock_;
110 error.data = this->data_;
115void OpenTherm::stop() {
117 this->mode_ = OperationMode::IDLE;
120void IRAM_ATTR OpenTherm::read_() {
123 this->mode_ = OperationMode::READ;
126 this->start_read_timer_();
131bool IRAM_ATTR OpenTherm::timer_isr(gptimer_handle_t timer,
const gptimer_alarm_event_data_t *edata,
void *user_ctx) {
132 auto *arg =
static_cast<OpenTherm *
>(user_ctx);
134bool IRAM_ATTR OpenTherm::timer_isr(OpenTherm *arg) {
136 if (arg->mode_ == OperationMode::LISTEN) {
137 if (arg->timeout_counter_ == 0) {
138 arg->mode_ = OperationMode::ERROR_TIMEOUT;
142 bool const value = arg->isr_in_pin_.digital_read();
146 if (arg->timeout_counter_ > 0) {
147 arg->timeout_counter_--;
149 }
else if (arg->mode_ == OperationMode::READ) {
150 bool const value = arg->isr_in_pin_.digital_read();
151 uint8_t
const last = (arg->capture_ & 1);
154 if (arg->clock_ == 1 && arg->capture_ > 0xF) {
156 arg->mode_ = OperationMode::ERROR_PROTOCOL;
157 arg->error_type_ = ProtocolErrorType::NO_TRANSITION;
160 }
else if (arg->clock_ == 1 || arg->capture_ > 0xF) {
162 if (arg->bit_pos_ == BitPositions::STOP_BIT) {
164 auto stop_bit_error = arg->verify_stop_bit_(last);
165 if (stop_bit_error == ProtocolErrorType::NO_ERROR) {
166 arg->mode_ = OperationMode::RECEIVED;
171 arg->mode_ = OperationMode::ERROR_PROTOCOL;
172 arg->error_type_ = stop_bit_error;
178 arg->bit_read_(last);
186 }
else if (arg->capture_ > 0xFF) {
188 arg->mode_ = OperationMode::ERROR_PROTOCOL;
189 arg->error_type_ = ProtocolErrorType::NO_CHANGE_TOO_LONG;
193 arg->capture_ = (arg->capture_ << 1) | value;
194 }
else if (arg->mode_ == OperationMode::WRITE) {
196 if (arg->bit_pos_ == 33 || arg->bit_pos_ == 0) {
197 arg->write_bit_(1, arg->clock_);
199 arg->write_bit_(
read_bit(arg->data_, arg->bit_pos_ - 1), arg->clock_);
201 if (arg->clock_ == 0) {
202 if (arg->bit_pos_ <= 0) {
203 arg->mode_ = OperationMode::SENT;
217void IRAM_ATTR OpenTherm::esp8266_timer_isr() { OpenTherm::timer_isr(OpenTherm::instance); }
220void IRAM_ATTR OpenTherm::bit_read_(uint8_t value) {
221 this->data_ = (this->data_ << 1) | value;
227 return check_parity_(this->data_) ? ProtocolErrorType::NO_ERROR : ProtocolErrorType::PARITY_ERROR;
229 return ProtocolErrorType::INVALID_STOP_BIT;
233void IRAM_ATTR OpenTherm::write_bit_(uint8_t high, uint8_t clock) {
235 this->isr_out_pin_.digital_write(!high);
237 this->isr_out_pin_.digital_write(high);
243bool OpenTherm::init_esp32_timer_() {
245 gptimer_config_t config = {
246 .clk_src = GPTIMER_CLK_SRC_DEFAULT,
247 .direction = GPTIMER_COUNT_UP,
248 .resolution_hz = 1000000,
251 esp_err_t result = gptimer_new_timer(&config, &this->timer_handle_);
252 if (result != ESP_OK) {
253 ESP_LOGE(TAG,
"Failed to create timer: %s", esp_err_to_name(result));
257 gptimer_event_callbacks_t cbs = {
258 .on_alarm = OpenTherm::timer_isr,
260 result = gptimer_register_event_callbacks(this->timer_handle_, &cbs,
this);
261 if (result != ESP_OK) {
262 ESP_LOGE(TAG,
"Failed to register timer callback: %s", esp_err_to_name(result));
263 gptimer_del_timer(this->timer_handle_);
264 this->timer_handle_ =
nullptr;
268 result = gptimer_enable(this->timer_handle_);
269 if (result != ESP_OK) {
270 ESP_LOGE(TAG,
"Failed to enable timer: %s", esp_err_to_name(result));
271 gptimer_del_timer(this->timer_handle_);
272 this->timer_handle_ =
nullptr;
279void IRAM_ATTR OpenTherm::start_esp32_timer_(uint64_t alarm_value) {
281 this->timer_error_ = ESP_OK;
282 this->timer_error_type_ = TimerErrorType::NO_TIMER_ERROR;
284 this->alarm_config_.alarm_count = alarm_value;
285 this->timer_error_ = gptimer_set_alarm_action(this->timer_handle_, &this->alarm_config_);
286 if (this->timer_error_ != ESP_OK) {
287 this->timer_error_type_ = TimerErrorType::SET_ALARM_VALUE_ERROR;
290 this->timer_error_ = gptimer_start(this->timer_handle_);
291 if (this->timer_error_ != ESP_OK) {
292 this->timer_error_type_ = TimerErrorType::TIMER_START_ERROR;
296void OpenTherm::report_and_reset_timer_error() {
297 if (this->timer_error_ == ESP_OK) {
301 ESP_LOGE(TAG,
"Error occured while manipulating timer (%s): %s", this->timer_error_to_str(this->timer_error_type_),
302 esp_err_to_name(this->timer_error_));
304 this->timer_error_ = ESP_OK;
309void IRAM_ATTR OpenTherm::start_read_timer_() {
310 InterruptLock
const lock;
311 this->start_esp32_timer_(200);
315void IRAM_ATTR OpenTherm::start_write_timer_() {
316 InterruptLock
const lock;
317 this->start_esp32_timer_(500);
320void IRAM_ATTR OpenTherm::stop_timer_() {
321 InterruptLock
const lock;
323 this->timer_error_ = ESP_OK;
324 this->timer_error_type_ = TimerErrorType::NO_TIMER_ERROR;
326 this->timer_error_ = gptimer_stop(this->timer_handle_);
327 if (this->timer_error_ != ESP_OK) {
328 this->timer_error_type_ = TimerErrorType::TIMER_PAUSE_ERROR;
331 this->timer_error_ = gptimer_set_raw_count(this->timer_handle_, 0);
332 if (this->timer_error_ != ESP_OK) {
333 this->timer_error_type_ = TimerErrorType::SET_COUNTER_VALUE_ERROR;
341void IRAM_ATTR OpenTherm::start_read_timer_() {
342 InterruptLock
const lock;
343 timer1_attachInterrupt(OpenTherm::esp8266_timer_isr);
344 timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
349void IRAM_ATTR OpenTherm::start_write_timer_() {
350 InterruptLock
const lock;
351 timer1_attachInterrupt(OpenTherm::esp8266_timer_isr);
352 timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
356void IRAM_ATTR OpenTherm::stop_timer_() {
357 InterruptLock
const lock;
359 timer1_detachInterrupt();
363void OpenTherm::report_and_reset_timer_error() {}
368bool IRAM_ATTR OpenTherm::check_parity_(
uint32_t val) {
377#define TO_STRING_MEMBER(name) \
381const char *OpenTherm::operation_mode_to_str(OperationMode
mode) {
383 TO_STRING_MEMBER(IDLE)
384 TO_STRING_MEMBER(LISTEN)
385 TO_STRING_MEMBER(READ)
386 TO_STRING_MEMBER(RECEIVED)
387 TO_STRING_MEMBER(WRITE)
388 TO_STRING_MEMBER(SENT)
389 TO_STRING_MEMBER(ERROR_PROTOCOL)
390 TO_STRING_MEMBER(ERROR_TIMEOUT)
391 TO_STRING_MEMBER(ERROR_TIMER)
396const char *OpenTherm::protocol_error_to_str(ProtocolErrorType error_type) {
397 switch (error_type) {
398 TO_STRING_MEMBER(NO_ERROR)
399 TO_STRING_MEMBER(NO_TRANSITION)
400 TO_STRING_MEMBER(INVALID_STOP_BIT)
401 TO_STRING_MEMBER(PARITY_ERROR)
402 TO_STRING_MEMBER(NO_CHANGE_TOO_LONG)
407const char *OpenTherm::timer_error_to_str(TimerErrorType error_type) {
408 switch (error_type) {
409 TO_STRING_MEMBER(NO_TIMER_ERROR)
410 TO_STRING_MEMBER(SET_ALARM_VALUE_ERROR)
411 TO_STRING_MEMBER(TIMER_START_ERROR)
412 TO_STRING_MEMBER(TIMER_PAUSE_ERROR)
413 TO_STRING_MEMBER(SET_COUNTER_VALUE_ERROR)
418const char *OpenTherm::message_type_to_str(MessageType message_type) {
419 switch (message_type) {
420 TO_STRING_MEMBER(READ_DATA)
421 TO_STRING_MEMBER(READ_ACK)
422 TO_STRING_MEMBER(WRITE_DATA)
423 TO_STRING_MEMBER(WRITE_ACK)
424 TO_STRING_MEMBER(INVALID_DATA)
425 TO_STRING_MEMBER(DATA_INVALID)
426 TO_STRING_MEMBER(UNKNOWN_DATAID)
432const char *OpenTherm::message_id_to_str(MessageId
id) {
434 TO_STRING_MEMBER(STATUS)
435 TO_STRING_MEMBER(CH_SETPOINT)
436 TO_STRING_MEMBER(CONTROLLER_CONFIG)
437 TO_STRING_MEMBER(DEVICE_CONFIG)
438 TO_STRING_MEMBER(COMMAND_CODE)
439 TO_STRING_MEMBER(FAULT_FLAGS)
440 TO_STRING_MEMBER(REMOTE)
441 TO_STRING_MEMBER(COOLING_CONTROL)
442 TO_STRING_MEMBER(CH2_SETPOINT)
443 TO_STRING_MEMBER(CH_SETPOINT_OVERRIDE)
444 TO_STRING_MEMBER(TSP_COUNT)
445 TO_STRING_MEMBER(TSP_COMMAND)
446 TO_STRING_MEMBER(FHB_SIZE)
447 TO_STRING_MEMBER(FHB_COMMAND)
448 TO_STRING_MEMBER(MAX_MODULATION_LEVEL)
449 TO_STRING_MEMBER(MAX_BOILER_CAPACITY)
450 TO_STRING_MEMBER(ROOM_SETPOINT)
451 TO_STRING_MEMBER(MODULATION_LEVEL)
452 TO_STRING_MEMBER(CH_WATER_PRESSURE)
453 TO_STRING_MEMBER(DHW_FLOW_RATE)
454 TO_STRING_MEMBER(DAY_TIME)
455 TO_STRING_MEMBER(DATE)
456 TO_STRING_MEMBER(YEAR)
457 TO_STRING_MEMBER(ROOM_SETPOINT_CH2)
458 TO_STRING_MEMBER(ROOM_TEMP)
459 TO_STRING_MEMBER(FEED_TEMP)
460 TO_STRING_MEMBER(DHW_TEMP)
461 TO_STRING_MEMBER(OUTSIDE_TEMP)
462 TO_STRING_MEMBER(RETURN_WATER_TEMP)
463 TO_STRING_MEMBER(SOLAR_STORE_TEMP)
464 TO_STRING_MEMBER(SOLAR_COLLECT_TEMP)
465 TO_STRING_MEMBER(FEED_TEMP_CH2)
466 TO_STRING_MEMBER(DHW2_TEMP)
467 TO_STRING_MEMBER(EXHAUST_TEMP)
468 TO_STRING_MEMBER(FAN_SPEED)
469 TO_STRING_MEMBER(FLAME_CURRENT)
470 TO_STRING_MEMBER(ROOM_TEMP_CH2)
471 TO_STRING_MEMBER(REL_HUMIDITY)
472 TO_STRING_MEMBER(DHW_BOUNDS)
473 TO_STRING_MEMBER(CH_BOUNDS)
474 TO_STRING_MEMBER(OTC_CURVE_BOUNDS)
475 TO_STRING_MEMBER(DHW_SETPOINT)
476 TO_STRING_MEMBER(MAX_CH_SETPOINT)
477 TO_STRING_MEMBER(OTC_CURVE_RATIO)
478 TO_STRING_MEMBER(HVAC_STATUS)
479 TO_STRING_MEMBER(REL_VENT_SETPOINT)
480 TO_STRING_MEMBER(DEVICE_VENT)
481 TO_STRING_MEMBER(HVAC_VER_ID)
482 TO_STRING_MEMBER(REL_VENTILATION)
483 TO_STRING_MEMBER(REL_HUMID_EXHAUST)
484 TO_STRING_MEMBER(EXHAUST_CO2)
485 TO_STRING_MEMBER(SUPPLY_INLET_TEMP)
486 TO_STRING_MEMBER(SUPPLY_OUTLET_TEMP)
487 TO_STRING_MEMBER(EXHAUST_INLET_TEMP)
488 TO_STRING_MEMBER(EXHAUST_OUTLET_TEMP)
489 TO_STRING_MEMBER(EXHAUST_FAN_SPEED)
490 TO_STRING_MEMBER(SUPPLY_FAN_SPEED)
491 TO_STRING_MEMBER(REMOTE_VENTILATION_PARAM)
492 TO_STRING_MEMBER(NOM_REL_VENTILATION)
493 TO_STRING_MEMBER(HVAC_NUM_TSP)
494 TO_STRING_MEMBER(HVAC_IDX_TSP)
495 TO_STRING_MEMBER(HVAC_FHB_SIZE)
496 TO_STRING_MEMBER(HVAC_FHB_IDX)
497 TO_STRING_MEMBER(RF_SIGNAL)
498 TO_STRING_MEMBER(DHW_MODE)
499 TO_STRING_MEMBER(OVERRIDE_FUNC)
500 TO_STRING_MEMBER(SOLAR_MODE_FLAGS)
501 TO_STRING_MEMBER(SOLAR_ASF)
502 TO_STRING_MEMBER(SOLAR_VERSION_ID)
503 TO_STRING_MEMBER(SOLAR_PRODUCT_ID)
504 TO_STRING_MEMBER(SOLAR_NUM_TSP)
505 TO_STRING_MEMBER(SOLAR_IDX_TSP)
506 TO_STRING_MEMBER(SOLAR_FHB_SIZE)
507 TO_STRING_MEMBER(SOLAR_FHB_IDX)
508 TO_STRING_MEMBER(SOLAR_STARTS)
509 TO_STRING_MEMBER(SOLAR_HOURS)
510 TO_STRING_MEMBER(SOLAR_ENERGY)
511 TO_STRING_MEMBER(SOLAR_TOTAL_ENERGY)
512 TO_STRING_MEMBER(FAILED_BURNER_STARTS)
513 TO_STRING_MEMBER(BURNER_FLAME_LOW)
514 TO_STRING_MEMBER(OEM_DIAGNOSTIC)
515 TO_STRING_MEMBER(BURNER_STARTS)
516 TO_STRING_MEMBER(CH_PUMP_STARTS)
517 TO_STRING_MEMBER(DHW_PUMP_STARTS)
518 TO_STRING_MEMBER(DHW_BURNER_STARTS)
519 TO_STRING_MEMBER(BURNER_HOURS)
520 TO_STRING_MEMBER(CH_PUMP_HOURS)
521 TO_STRING_MEMBER(DHW_PUMP_HOURS)
522 TO_STRING_MEMBER(DHW_BURNER_HOURS)
523 TO_STRING_MEMBER(OT_VERSION_CONTROLLER)
524 TO_STRING_MEMBER(OT_VERSION_DEVICE)
525 TO_STRING_MEMBER(VERSION_CONTROLLER)
526 TO_STRING_MEMBER(VERSION_DEVICE)
532void OpenTherm::debug_data(OpenthermData &data) {
533 char type_buf[9], id_buf[9], hb_buf[9], lb_buf[9];
536 ESP_LOGD(TAG,
"type: %s; id: %u; HB: %u; LB: %u; uint_16: %u; float: %f",
540void OpenTherm::debug_error(OpenThermError &error)
const {
541 ESP_LOGD(TAG,
"data: 0x%08" PRIx32
"; clock: %u; capture: 0x%08" PRIx32
"; bit_pos: %u", error.data, this->clock_,
542 error.capture, error.bit_pos);
545float OpenthermData::f88() {
return ((
float) this->s16()) / 256.0; }
547void OpenthermData::f88(
float value) { this->s16((int16_t) (value * 256)); }
549uint16_t OpenthermData::u16() {
550 uint16_t
const value = this->valueHB;
551 return (value << 8) | this->valueLB;
554void OpenthermData::u16(uint16_t value) {
555 this->valueLB = value & 0xFF;
556 this->valueHB = (value >> 8) & 0xFF;
559int16_t OpenthermData::s16() {
560 int16_t
const value = this->valueHB;
561 return (value << 8) | this->valueLB;
564void OpenthermData::s16(int16_t value) {
565 this->valueLB = value & 0xFF;
566 this->valueHB = (value >> 8) & 0xFF;
BedjetMode mode
BedJet operating mode.
OpenTherm(InternalGPIOPin *in_pin, InternalGPIOPin *out_pin, int32_t device_timeout=800)
const std::vector< uint8_t > & data
constexpr T read_bit(T value, uint8_t bit)
const char * message_type_to_str(MessageType t)
Providing packet encoding functions for exchanging data with a remote host.
char * format_bin_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as binary string to buffer.