fix reset

This commit is contained in:
Chelios 2024-10-14 10:32:44 +03:00
parent 95ae144e70
commit cdd068c2b1
3 changed files with 60 additions and 62 deletions

View file

@ -100,8 +100,8 @@ async def to_code(config):
if CORE.using_esp_idf: if CORE.using_esp_idf:
add_idf_sdkconfig_option("CONFIG_LWIP_PPP_SUPPORT", True) add_idf_sdkconfig_option("CONFIG_LWIP_PPP_SUPPORT", True)
add_idf_sdkconfig_option("CONFIG_TASK_WDT_TIMEOUT_S", 60) add_idf_sdkconfig_option("CONFIG_TASK_WDT_TIMEOUT_S", 60)
add_idf_sdkconfig_option("CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT", False) # add_idf_sdkconfig_option("CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT", False)
add_idf_sdkconfig_option("CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE", True) # add_idf_sdkconfig_option("CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE", True)
add_idf_component( add_idf_component(
name="esp_modem", name="esp_modem",
repo="https://github.com/espressif/esp-protocols.git", repo="https://github.com/espressif/esp-protocols.git",

View file

@ -105,13 +105,32 @@ void ModemComponent::loop() {
} }
switch (this->state_) { switch (this->state_) {
case ModemComponentState::STOPPED: case ModemComponentState::STOPPED:
this->turn_on_modem(); this->set_state(ModemComponentState::TURNING_ON_POWER);
break; break;
case ModemComponentState::TURNING_ON_POWER: // time_check_pwrkey case ModemComponentState::TURNING_ON_POWER: // time_check_pwrkey
if (power_pin_) {
this->power_pin_->digital_write(true);
time_turn_on_modem = millis();
ESP_LOGD(TAG, "Modem turn on");
} else {
ESP_LOGD(TAG, "Can't turn on modem power pin because it is not configured, go to turn on pwrkey");
this->turn_on_pwrkey(); this->turn_on_pwrkey();
}
this->set_state(ModemComponentState::TURNING_ON_PWRKEY);
this->dce_init(); this->dce_init();
break; break;
case ModemComponentState::TURNING_ON_PWRKEY: case ModemComponentState::TURNING_ON_PWRKEY:
if (pwrkey_pin_) {
this->pwrkey_pin_->digital_write(false);
ESP_LOGD(TAG, "pwrkey turn on");
} else {
ESP_LOGD(TAG, "Can't turn on pwrkey pin because it is not configured, go to reset modem");
this->turn_on_reset(); // how do if reset dont work
break;
}
this->set_state(ModemComponentState::TURNING_OFF_PWRKEY);
break;
case ModemComponentState::TURNING_OFF_PWRKEY:
this->turn_off_pwrkey(); this->turn_off_pwrkey();
break; break;
case ModemComponentState::SYNC: case ModemComponentState::SYNC:
@ -150,7 +169,14 @@ void ModemComponent::loop() {
} }
break; break;
case ModemComponentState::TURNING_ON_RESET: case ModemComponentState::TURNING_ON_RESET:
this->turn_off_reset(); this->reset_pin_->digital_write(false);
this->set_state(ModemComponentState::TURNING_OFF_RESET);
break;
case ModemComponentState::TURNING_OFF_RESET:
this->reset_pin_->digital_write(true);
ESP_LOGD(TAG, "turn off reset");
this->set_state(ModemComponentState::SYNC);
break;
case ModemComponentState::TURNING_OFF_POWER: case ModemComponentState::TURNING_OFF_POWER:
if (time_turn_off_modem + TURN_OFF_MODEM_TIME < now) { if (time_turn_off_modem + TURN_OFF_MODEM_TIME < now) {
this->turn_on_modem(); this->turn_on_modem();
@ -189,6 +215,7 @@ bool ModemComponent::check_modem_component_state_timings() {
const int now = millis(); const int now = millis();
ModemComponentStateTiming timing = this->modemComponentStateTimingMap[this->state_]; ModemComponentStateTiming timing = this->modemComponentStateTimingMap[this->state_];
if (timing.time_limit && ((time_change_state + timing.time_limit) < now)) { if (timing.time_limit && ((time_change_state + timing.time_limit) < now)) {
ESP_LOGE(TAG, "State time limit %s", this->state_to_string(this->state_));
this->turn_on_reset(); this->turn_on_reset();
} }
if (!timing.poll_period) { if (!timing.poll_period) {
@ -203,7 +230,7 @@ bool ModemComponent::check_modem_component_state_timings() {
} }
void ModemComponent::set_state(ModemComponentState state) { void ModemComponent::set_state(ModemComponentState state) {
ESP_LOGCONFIG(TAG, "Mode component change state from %s to %s", this->state_to_string(this->state_), ESP_LOGCONFIG(TAG, "Modem component change state from %s to %s", this->state_to_string(this->state_),
this->state_to_string(state)); this->state_to_string(state));
this->state_ = state; this->state_ = state;
time_change_state = millis(); time_change_state = millis();
@ -217,6 +244,8 @@ const char *ModemComponent::state_to_string(ModemComponentState state) {
return "TURNING_ON_POWER"; return "TURNING_ON_POWER";
case ModemComponentState::TURNING_ON_PWRKEY: case ModemComponentState::TURNING_ON_PWRKEY:
return "TURNING_ON_PWRKEY"; return "TURNING_ON_PWRKEY";
case ModemComponentState::TURNING_OFF_PWRKEY:
return "TURNING_OFF_PWRKEY";
case ModemComponentState::SYNC: case ModemComponentState::SYNC:
return "SYNC"; return "SYNC";
case ModemComponentState::REGISTRATION_IN_NETWORK: case ModemComponentState::REGISTRATION_IN_NETWORK:
@ -227,6 +256,8 @@ const char *ModemComponent::state_to_string(ModemComponentState state) {
return "CONNECTED"; return "CONNECTED";
case ModemComponentState::TURNING_ON_RESET: case ModemComponentState::TURNING_ON_RESET:
return "TURNING_ON_RESET"; return "TURNING_ON_RESET";
case ModemComponentState::TURNING_OFF_RESET:
return "TURNING_OFF_RESET";
case ModemComponentState::TURNING_OFF_POWER: case ModemComponentState::TURNING_OFF_POWER:
return "TURNING_OFF_POWER"; return "TURNING_OFF_POWER";
} }
@ -234,15 +265,15 @@ const char *ModemComponent::state_to_string(ModemComponentState state) {
} }
void ModemComponent::turn_on_modem() { void ModemComponent::turn_on_modem() {
if (power_pin_) { // if (power_pin_) {
this->power_pin_->digital_write(true); // this->power_pin_->digital_write(true);
time_turn_on_modem = millis(); // time_turn_on_modem = millis();
ESP_LOGD(TAG, "Modem turn on"); // ESP_LOGD(TAG, "Modem turn on");
this->set_state(ModemComponentState::TURNING_ON_POWER); // this->set_state(ModemComponentState::TURNING_ON_POWER);
} else { // } else {
ESP_LOGD(TAG, "Can't turn on modem power pin because it is not configured, go to turn on pwrkey"); // ESP_LOGD(TAG, "Can't turn on modem power pin because it is not configured, go to turn on pwrkey");
this->turn_on_pwrkey(); // this->turn_on_pwrkey();
} // }
} }
void ModemComponent::turn_off_modem() { void ModemComponent::turn_off_modem() {
@ -253,14 +284,14 @@ void ModemComponent::turn_off_modem() {
} }
void ModemComponent::turn_on_pwrkey() { void ModemComponent::turn_on_pwrkey() {
if (pwrkey_pin_) { // if (pwrkey_pin_) {
this->pwrkey_pin_->digital_write(false); // this->pwrkey_pin_->digital_write(false);
ESP_LOGD(TAG, "pwrkey turn on"); // ESP_LOGD(TAG, "pwrkey turn on");
this->set_state(ModemComponentState::TURNING_ON_PWRKEY); // this->set_state(ModemComponentState::TURNING_ON_PWRKEY);
} else { // } else {
ESP_LOGD(TAG, "Can't turn on pwrkey pin because it is not configured, go to reset modem"); // ESP_LOGD(TAG, "Can't turn on pwrkey pin because it is not configured, go to reset modem");
this->turn_on_reset(); // this->turn_on_reset();
} // }
// vTaskDelay(pdMS_TO_TICKS(500)); // NOLINT // vTaskDelay(pdMS_TO_TICKS(500)); // NOLINT
} }
@ -276,12 +307,6 @@ void ModemComponent::turn_on_reset() {
this->set_state(ModemComponentState::TURNING_ON_RESET); this->set_state(ModemComponentState::TURNING_ON_RESET);
} }
void ModemComponent::turn_off_reset() {
this->reset_pin_->digital_write(true);
ESP_LOGD(TAG, "turn off reset");
this->set_state(ModemComponentState::SYNC);
}
void ModemComponent::dump_config() { void ModemComponent::dump_config() {
this->dump_connect_params(); this->dump_connect_params();
ESP_LOGCONFIG(TAG, "Modem:"); ESP_LOGCONFIG(TAG, "Modem:");
@ -343,7 +368,6 @@ network::IPAddress ModemComponent::get_ip_address() {
void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int event_id, void *event_data) { void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int event_id, void *event_data) {
ESP_LOGD(TAG, "IP event! %" PRIu32, event_id); ESP_LOGD(TAG, "IP event! %" PRIu32, event_id);
if (event_id == IP_EVENT_PPP_GOT_IP) { if (event_id == IP_EVENT_PPP_GOT_IP) {
global_modem_component->connected_ = true;
global_modem_component->set_state(ModemComponentState::CONNECTED); global_modem_component->set_state(ModemComponentState::CONNECTED);
esp_netif_dns_info_t dns_info; esp_netif_dns_info_t dns_info;
@ -368,33 +392,6 @@ void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base
} }
} }
void ModemComponent::start_connect_() {
this->connect_begin_ = millis();
this->status_set_warning();
turn_on_reset();
esp_err_t err;
err = esp_netif_set_hostname(this->modem_netif_, App.get_name().c_str());
if (err != ERR_OK) {
ESP_LOGD(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
}
// esp_netif_dhcp_status_t status = ESP_NETIF_DHCP_INIT;
// restart ppp connection
this->dce->exit_data();
int rssi, ber;
esp_modem::command_result errr = this->dce->get_signal_quality(rssi, ber);
// esp_err_t err = esp_modem::esp_modem_get_signal_quality(dce, &rssi, &ber);
if (errr != esp_modem::command_result::OK) {
ESP_LOGE(TAG, "esp_modem_get_signal_quality failed with");
return;
}
ESP_LOGD(TAG, "Signal quality: rssi=%d, ber=%d", rssi, ber);
this->dce->set_data();
// this->status_set_warning();
}
bool ModemComponent::is_connected() { return this->state_ == ModemComponentState::CONNECTED; } bool ModemComponent::is_connected() { return this->state_ == ModemComponentState::CONNECTED; }
void ModemComponent::set_power_pin(InternalGPIOPin *power_pin) { this->power_pin_ = power_pin; } void ModemComponent::set_power_pin(InternalGPIOPin *power_pin) { this->power_pin_ = power_pin; }
void ModemComponent::set_pwrkey_pin(InternalGPIOPin *pwrkey_pin) { this->pwrkey_pin_ = pwrkey_pin; } void ModemComponent::set_pwrkey_pin(InternalGPIOPin *pwrkey_pin) { this->pwrkey_pin_ = pwrkey_pin; }

View file

@ -29,11 +29,13 @@ enum class ModemComponentState {
STOPPED, STOPPED,
TURNING_ON_POWER, TURNING_ON_POWER,
TURNING_ON_PWRKEY, TURNING_ON_PWRKEY,
TURNING_OFF_PWRKEY,
SYNC, SYNC,
REGISTRATION_IN_NETWORK, REGISTRATION_IN_NETWORK,
CONNECTING, CONNECTING,
CONNECTED, CONNECTED,
TURNING_ON_RESET, TURNING_ON_RESET,
TURNING_OFF_RESET,
TURNING_OFF_POWER, TURNING_OFF_POWER,
}; };
@ -76,12 +78,14 @@ class ModemComponent : public Component {
std::map<ModemComponentState, ModemComponentStateTiming> modemComponentStateTimingMap = { std::map<ModemComponentState, ModemComponentStateTiming> modemComponentStateTimingMap = {
{ModemComponentState::STOPPED, ModemComponentStateTiming(0, 0)}, {ModemComponentState::STOPPED, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_POWER, ModemComponentStateTiming(0, 0)}, {ModemComponentState::TURNING_ON_POWER, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(2000, 15000)}, {ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_OFF_PWRKEY, ModemComponentStateTiming(2000, 0)},
{ModemComponentState::SYNC, ModemComponentStateTiming(2000, 15000)}, {ModemComponentState::SYNC, ModemComponentStateTiming(2000, 15000)},
{ModemComponentState::REGISTRATION_IN_NETWORK, ModemComponentStateTiming(2000, 15000)}, {ModemComponentState::REGISTRATION_IN_NETWORK, ModemComponentStateTiming(2000, 15000)},
{ModemComponentState::CONNECTING, ModemComponentStateTiming(2000, 15000)}, {ModemComponentState::CONNECTING, ModemComponentStateTiming(2000, 15000)},
{ModemComponentState::CONNECTED, ModemComponentStateTiming(0, 0)}, {ModemComponentState::CONNECTED, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_ON_RESET, ModemComponentStateTiming(110, 0)}, {ModemComponentState::TURNING_ON_RESET, ModemComponentStateTiming(0, 0)},
{ModemComponentState::TURNING_OFF_RESET, ModemComponentStateTiming(2000, 0)},
{ModemComponentState::TURNING_OFF_POWER, ModemComponentStateTiming(2000, 0)}, {ModemComponentState::TURNING_OFF_POWER, ModemComponentStateTiming(2000, 0)},
}; };
@ -95,9 +99,7 @@ class ModemComponent : public Component {
void turn_off_modem(); void turn_off_modem();
void turn_on_pwrkey(); void turn_on_pwrkey();
void turn_off_pwrkey(); void turn_off_pwrkey();
void start_connect_();
void turn_on_reset(); void turn_on_reset();
void turn_off_reset();
int get_rssi(); int get_rssi();
int get_modem_voltage(); int get_modem_voltage();
const char *get_state(); const char *get_state();
@ -124,7 +126,6 @@ class ModemComponent : public Component {
int change_state_{0}; int change_state_{0};
bool started_{false}; bool started_{false};
bool connected_{false};
ModemComponentState state_{ModemComponentState::STOPPED}; ModemComponentState state_{ModemComponentState::STOPPED};
int connect_begin_; int connect_begin_;