Prefix global variables

This commit is contained in:
Attila Body 2025-06-19 10:21:03 +02:00
parent afd998adb6
commit c74c1a6970
Signed by: abody
GPG key ID: BD0C6214E68FB5CF
2 changed files with 104 additions and 104 deletions

View file

@ -16,24 +16,24 @@ static const char *TAG = "ot-example";
#define OT_IN_PIN 4;
#define OT_OUT_PIN 5;
gpio_num_t pin_in = OT_IN_PIN;
gpio_num_t pin_out = OT_OUT_PIN;
gpio_num_t g_pin_in = OT_IN_PIN;
gpio_num_t g_pin_out = OT_OUT_PIN;
typedef uint8_t byte;
bool esp_ot_is_slave;
bool g_esp_ot_is_slave;
void (*esp_ot_process_response_callback)(unsigned long, open_therm_response_status_t);
volatile unsigned long response;
volatile unsigned long g_response;
volatile esp_ot_opentherm_status_t esp_ot_status;
volatile esp_ot_opentherm_status_t g_esp_ot_status;
volatile open_therm_response_status_t esp_ot_response_status;
volatile open_therm_response_status_t g_esp_ot_response_status;
volatile unsigned long esp_ot_response_timestamp;
volatile unsigned long g_esp_ot_response_timestamp;
volatile byte esp_ot_response_bit_index;
volatile byte g_esp_ot_response_bit_index;
/**
* Initialize opentherm: gpio, install isr, basic data
@ -50,42 +50,42 @@ esp_err_t esp_ot_init(
ESP_LOGE("ISR", "Error with state %s", esp_err_to_name(err));
}
pin_in = _pin_in;
pin_out = _pin_out;
g_pin_in = _pin_in;
g_pin_out = _pin_out;
// Initialize the GPIO
gpio_config_t io_conf;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1ULL << pin_in);
io_conf.pin_bit_mask = (1ULL << g_pin_in);
io_conf.intr_type = GPIO_INTR_ANYEDGE;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&io_conf);
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL << pin_out);
io_conf.pin_bit_mask = (1ULL << g_pin_out);
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
gpio_isr_handler_add(pin_in, esp_ot_handle_interrupt, NULL);
gpio_isr_handler_add(g_pin_in, esp_ot_handle_interrupt, NULL);
esp_ot_is_slave = _esp_ot_is_slave;
g_esp_ot_is_slave = _esp_ot_is_slave;
esp_ot_process_response_callback = esp_ot_process_responseCallback;
response = 0;
g_response = 0;
esp_ot_response_status = OT_STATUS_NONE;
g_esp_ot_response_status = OT_STATUS_NONE;
esp_ot_response_timestamp = 0;
g_esp_ot_response_timestamp = 0;
gpio_intr_enable(pin_in);
gpio_intr_enable(g_pin_in);
esp_ot_status = OT_READY;
g_esp_ot_status = OT_READY;
ESP_LOGI(TAG, "Initialize opentherm with in: %d out: %d", pin_in, pin_out);
ESP_LOGI(TAG, "Initialize opentherm with in: %d out: %d", g_pin_in, g_pin_out);
return ESP_OK;
}
@ -156,7 +156,7 @@ unsigned long esp_ot_build_get_boiler_temperature_request()
*/
bool IRAM_ATTR esp_ot_is_ready()
{
return esp_ot_status == OT_READY;
return g_esp_ot_status == OT_READY;
}
/**
@ -166,7 +166,7 @@ bool IRAM_ATTR esp_ot_is_ready()
*/
int IRAM_ATTR esp_ot_read_state()
{
return gpio_get_level(pin_in);
return gpio_get_level(g_pin_in);
}
/**
@ -176,7 +176,7 @@ int IRAM_ATTR esp_ot_read_state()
*/
void esp_ot_set_active_state()
{
gpio_set_level(pin_out, 0);
gpio_set_level(g_pin_out, 0);
}
/**
@ -186,7 +186,7 @@ void esp_ot_set_active_state()
*/
void esp_ot_set_idle_state()
{
gpio_set_level(pin_out, 1);
gpio_set_level(g_pin_out, 1);
}
/**
@ -208,7 +208,7 @@ void esp_ot_process_response()
{
if (esp_ot_process_response_callback != NULL) {
// ESP_LOGI("PROCESS RESPONSE", "esp_ot_process_response, %ld, %d", response, esp_ot_response_status);
esp_ot_process_response_callback(response, esp_ot_response_status);
esp_ot_process_response_callback(g_response, g_esp_ot_response_status);
}
}
@ -394,42 +394,42 @@ void IRAM_ATTR esp_ot_handle_interrupt()
{
// ESP_DRAM_LOGI("esp_ot_handle_interrupt", "%ld", status);
if (esp_ot_is_ready()) {
if (esp_ot_is_slave && esp_ot_read_state() == OT_INPUT_ACTIVE) {
esp_ot_status = OT_RESPONSE_WAITING;
if (g_esp_ot_is_slave && esp_ot_read_state() == OT_INPUT_ACTIVE) {
g_esp_ot_status = OT_RESPONSE_WAITING;
} else {
return;
}
}
unsigned long newTs = esp_timer_get_time();
if (esp_ot_status == OT_RESPONSE_WAITING) {
if (g_esp_ot_status == OT_RESPONSE_WAITING) {
if (esp_ot_read_state() == OT_INPUT_ACTIVE) {
// ESP_DRAM_LOGI("BIT", "Set start bit");
esp_ot_status = OT_RESPONSE_START_BIT;
esp_ot_response_timestamp = newTs;
g_esp_ot_status = OT_RESPONSE_START_BIT;
g_esp_ot_response_timestamp = newTs;
} else {
// ESP_DRAM_LOGI("BIT", "Set OT_RESPONSE_INVALID");
esp_ot_status = OT_RESPONSE_INVALID;
esp_ot_response_timestamp = newTs;
g_esp_ot_status = OT_RESPONSE_INVALID;
g_esp_ot_response_timestamp = newTs;
}
} else if (esp_ot_status == OT_RESPONSE_START_BIT) {
if ((newTs - esp_ot_response_timestamp < 750) && esp_ot_read_state() == OT_INPUT_INACTIVE) {
esp_ot_status = OT_RESPONSE_RECEIVING;
esp_ot_response_timestamp = newTs;
esp_ot_response_bit_index = 0;
} else if (g_esp_ot_status == OT_RESPONSE_START_BIT) {
if ((newTs - g_esp_ot_response_timestamp < 750) && esp_ot_read_state() == OT_INPUT_INACTIVE) {
g_esp_ot_status = OT_RESPONSE_RECEIVING;
g_esp_ot_response_timestamp = newTs;
g_esp_ot_response_bit_index = 0;
} else {
esp_ot_status = OT_RESPONSE_INVALID;
esp_ot_response_timestamp = newTs;
g_esp_ot_status = OT_RESPONSE_INVALID;
g_esp_ot_response_timestamp = newTs;
}
} else if (esp_ot_status == OT_RESPONSE_RECEIVING) {
if ((newTs - esp_ot_response_timestamp) > 750) {
if (esp_ot_response_bit_index < 32) {
response = (response << 1) | (esp_ot_read_state() == OT_INPUT_INACTIVE);
esp_ot_response_timestamp = newTs;
esp_ot_response_bit_index++;
} else if (g_esp_ot_status == OT_RESPONSE_RECEIVING) {
if ((newTs - g_esp_ot_response_timestamp) > 750) {
if (g_esp_ot_response_bit_index < 32) {
g_response = (g_response << 1) | (esp_ot_read_state() == OT_INPUT_INACTIVE);
g_esp_ot_response_timestamp = newTs;
g_esp_ot_response_bit_index++;
} else { // stop bit
esp_ot_status = OT_RESPONSE_READY;
esp_ot_response_timestamp = newTs;
g_esp_ot_status = OT_RESPONSE_READY;
g_esp_ot_response_timestamp = newTs;
}
}
}
@ -443,8 +443,8 @@ void IRAM_ATTR esp_ot_handle_interrupt()
void process()
{
PORT_ENTER_CRITICAL;
esp_ot_opentherm_status_t st = esp_ot_status;
unsigned long ts = esp_ot_response_timestamp;
esp_ot_opentherm_status_t st = g_esp_ot_status;
unsigned long ts = g_esp_ot_response_timestamp;
PORT_EXIT_CRITICAL;
if (st == OT_READY) {
@ -452,24 +452,24 @@ void process()
}
unsigned long newTs = esp_timer_get_time();
if (st != OT_NOT_INITIALIZED && st != OT_DELAY && (newTs - ts) > 1000000) {
esp_ot_status = OT_READY;
g_esp_ot_status = OT_READY;
ESP_LOGI("SET STATUS", "set status to READY"); // here READY
esp_ot_response_status = OT_STATUS_TIMEOUT;
g_esp_ot_response_status = OT_STATUS_TIMEOUT;
esp_ot_process_response();
} else if (st == OT_RESPONSE_INVALID) {
ESP_LOGE("SET STATUS", "set status to OT_RESPONSE_INVALID"); // here OT_RESPONSE_INVALID
esp_ot_status = OT_DELAY;
esp_ot_response_status = OT_STATUS_INVALID;
g_esp_ot_status = OT_DELAY;
g_esp_ot_response_status = OT_STATUS_INVALID;
esp_ot_process_response();
} else if (st == OT_RESPONSE_READY) {
esp_ot_status = OT_DELAY;
esp_ot_response_status = (esp_ot_is_slave ? esp_ot_is_valid_request(response) : esp_ot_is_valid_response(response))
? OT_STATUS_SUCCESS
: OT_STATUS_INVALID;
g_esp_ot_status = OT_DELAY;
g_esp_ot_response_status = (g_esp_ot_is_slave ? esp_ot_is_valid_request(g_response) : esp_ot_is_valid_response(g_response))
? OT_STATUS_SUCCESS
: OT_STATUS_INVALID;
esp_ot_process_response();
} else if (st == OT_DELAY) {
if ((newTs - ts) > (esp_ot_is_slave ? 20000 : 100000)) {
esp_ot_status = OT_READY;
if ((newTs - ts) > (g_esp_ot_is_slave ? 20000 : 100000)) {
g_esp_ot_status = OT_READY;
}
}
}
@ -490,9 +490,9 @@ bool esp_ot_send_request_async(unsigned long request)
return false;
}
PORT_ENTER_CRITICAL;
esp_ot_status = OT_REQUEST_SENDING;
response = 0;
esp_ot_response_status = OT_STATUS_NONE;
g_esp_ot_status = OT_REQUEST_SENDING;
g_response = 0;
g_esp_ot_response_status = OT_STATUS_NONE;
PORT_EXIT_CRITICAL;
// vTaskSuspendAll();
@ -504,8 +504,8 @@ bool esp_ot_send_request_async(unsigned long request)
esp_ot_send_bit(1); // stop bit
esp_ot_set_idle_state();
esp_ot_response_timestamp = esp_timer_get_time();
esp_ot_status = OT_RESPONSE_WAITING;
g_esp_ot_response_timestamp = esp_timer_get_time();
g_esp_ot_status = OT_RESPONSE_WAITING;
// xTaskResumeAll();
@ -529,7 +529,7 @@ unsigned long esp_ot_send_request(unsigned long request)
process();
vPortYield();
}
return response;
return g_response;
}
/**
@ -950,7 +950,7 @@ uint16_t esp_ot_get_fault_code()
*/
open_therm_response_status_t esp_ot_get_last_response_status()
{
return esp_ot_response_status;
return g_esp_ot_response_status;
}
/**
@ -970,9 +970,9 @@ bool esp_ot_send_response(unsigned long request)
return false;
}
esp_ot_status = OT_REQUEST_SENDING;
response = 0;
esp_ot_response_status = OT_STATUS_NONE;
g_esp_ot_status = OT_REQUEST_SENDING;
g_response = 0;
g_esp_ot_response_status = OT_STATUS_NONE;
// vTaskSuspendAll();
@ -984,7 +984,7 @@ bool esp_ot_send_response(unsigned long request)
}
esp_ot_send_bit(1); // stop bit
esp_ot_set_idle_state();
esp_ot_status = OT_READY;
g_esp_ot_status = OT_READY;
// xTaskResumeAll();
return true;
@ -997,5 +997,5 @@ bool esp_ot_send_response(unsigned long request)
*/
unsigned long esp_ot_get_last_response()
{
return response;
return g_response;
}