add pwrkey

This commit is contained in:
Chelios 2024-10-09 08:20:11 +03:00
parent bfe6187e14
commit 35e9f8c4d4
3 changed files with 57 additions and 18 deletions

View file

@ -25,6 +25,7 @@ CONF_UART_EVENT_TASK_STACK_SIZE = "uart_event_task_stack_size"
CONF_UART_EVENT_TASK_PRIORITY = "uart_event_task_priority"
CONF_UART_EVENT_QUEUE_SIZE = "uart_event_queue_size"
CONF_POWER_PIN = "power_pin"
CONF_PWRKEY_PIN = "pwrkey_pin"
ModemType = modem_ns.enum("ModemType")
MODEM_TYPES = {
@ -48,8 +49,11 @@ CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(ModemComponent),
cv.Required(CONF_TYPE): cv.enum(MODEM_TYPES, upper=True),
cv.Required(CONF_RESET_PIN): pins.internal_gpio_output_pin_schema,
# There are modules without pwrkey pin and without power pin,
# only a reset pin.
cv.Optional(CONF_POWER_PIN): pins.internal_gpio_output_pin_schema,
cv.Optional(CONF_PWRKEY_PIN): pins.internal_gpio_output_pin_schema,
cv.Required(CONF_RESET_PIN): pins.internal_gpio_output_pin_schema,
cv.Required(CONF_TX_PIN): pins.internal_gpio_output_pin_number,
cv.Required(CONF_RX_PIN): pins.internal_gpio_output_pin_number,
cv.Optional(CONF_APN, default="internet"): cv.string,
@ -71,13 +75,15 @@ async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
cg.add(var.set_type(config[CONF_TYPE]))
# cg.add(var.set_reset_pin(config[CONF_RESET_PIN]))
if reset_pin := config.get(CONF_RESET_PIN, None):
pin = await cg.gpio_pin_expression(reset_pin)
cg.add(var.set_reset_pin(pin))
if power_pin := config.get(CONF_POWER_PIN, None):
pin = await cg.gpio_pin_expression(power_pin)
cg.add(var.set_power_pin(pin))
if pwrkey_pin := config.get(CONF_PWRKEY_PIN, None):
pin = await cg.gpio_pin_expression(pwrkey_pin)
cg.add(var.set_pwrkey_pin(pin))
cg.add(var.set_tx_pin(config[CONF_TX_PIN]))
cg.add(var.set_rx_pin(config[CONF_RX_PIN]))

View file

@ -47,7 +47,26 @@ void ModemComponent::setup() {
esp_log_level_set("uart_terminal", ESP_LOG_VERBOSE);
ESP_LOGCONFIG(TAG, "Setting up modem...");
esp_modem_hard_reset();
this->reset_pin_->setup();
if (this->power_pin_) {
ESP_LOGD(TAG, "power_pin_ ON");
this->power_pin_->setup();
this->power_pin_->digital_write(true);
vTaskDelay(pdMS_TO_TICKS(2000)); // NOLINT
}
if (this->pwrkey_pin_) {
ESP_LOGD(TAG, "pwrkey pin used");
this->pwrkey_pin_->setup();
this->pwrkey_pin_->digital_write(false);
vTaskDelay(pdMS_TO_TICKS(2000)); // NOLINT
this->pwrkey_pin_->digital_write(true);
}
//esp_modem_hard_reset();
if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
// Delay here to allow power to stabilise before Modem is initialized.
delay(300); // NOLINT
@ -88,13 +107,6 @@ void ModemComponent::setup() {
esp_netif_flags_t flags = esp_netif_get_flags(this->modem_netif_);
this->started_ = true;
// power pin on
if (this->power_pin_) {
this->power_pin_->setup();
this->power_pin_->digital_write(false);
this->reset_pin_->setup();
}
}
void ModemComponent::loop() {
@ -133,12 +145,23 @@ void ModemComponent::loop() {
}
}
bool power_on(){
return true;
}
bool power_off(){
return true;
}
bool use_pwrkey(){
return true;
}
void ModemComponent::dump_config() {
this->dump_connect_params();
ESP_LOGCONFIG(TAG, "Modem:");
ESP_LOGCONFIG(TAG, " Power Pin: %d", this->power_pin_->get_pin());
ESP_LOGCONFIG(TAG, " Type: %d", this->type_);
ESP_LOGCONFIG(TAG, " Reset Pin: %d", this->reset_pin_->get_pin());
ESP_LOGCONFIG(TAG, " Reset Pin: %u", this->reset_pin_->get_pin());
ESP_LOGCONFIG(TAG, " Power pin : %s", (this->power_pin_) ? this->power_pin_->dump_summary().c_str() : "Not defined");
ESP_LOGCONFIG(TAG, " APN: %s", this->apn_.c_str());
ESP_LOGCONFIG(TAG, " TX Pin: %d", this->tx_pin_);
ESP_LOGCONFIG(TAG, " RX Pin: %d", this->rx_pin_);
@ -163,12 +186,16 @@ void ModemComponent::dump_connect_params() {
}
void ModemComponent::esp_modem_hard_reset() {
this->reset_pin_->digital_write(false);
ESP_LOGD(TAG, "reset_pin_ 0");
vTaskDelay(pdMS_TO_TICKS(110)); // NOLINT
this->reset_pin_->digital_write(true);
ESP_LOGD(TAG, "reset_pin_ 1");
time_hard_reset_modem = millis();
//gpio_set_direction(gpio_num_t(this->reset_pin_->get_pin()), GPIO_MODE_OUTPUT);
//gpio_set_level(gpio_num_t(this->reset_pin_->get_pin()), 0);
// this->reset_pin_->digital_write(false);
// ESP_LOGD(TAG, "reset_pin_ 0");
// vTaskDelay(pdMS_TO_TICKS(110)); // NOLINT
// //gpio_set_level(gpio_num_t(this->reset_pin_->get_pin()), 1);
// this->reset_pin_->digital_write(true);
// ESP_LOGD(TAG, "reset_pin_ 1");
// time_hard_reset_modem = millis();
}
int ModemComponent::get_rssi() {
@ -257,6 +284,7 @@ void ModemComponent::start_connect_() {
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_pwrkey_pin(InternalGPIOPin *pwrkey_pin) { this->pwrkey_pin_ = pwrkey_pin; }
void ModemComponent::set_type(ModemType type) { this->type_ = type; }
void ModemComponent::set_reset_pin(InternalGPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
void ModemComponent::set_apn(const std::string &apn) { this->apn_ = apn; }

View file

@ -41,6 +41,7 @@ class ModemComponent : public Component {
bool can_proceed() override;
bool is_connected();
void set_power_pin(InternalGPIOPin *power_pin);
void set_pwrkey_pin(InternalGPIOPin *pwrkey_pin);
void set_type(ModemType type);
void set_reset_pin(InternalGPIOPin *reset_pin);
void set_apn(const std::string &apn);
@ -60,6 +61,9 @@ class ModemComponent : public Component {
protected:
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
bool power_on();
bool power_off();
bool use_pwrkey();
void start_connect_();
void esp_modem_hard_reset();
int get_rssi();
@ -70,6 +74,7 @@ class ModemComponent : public Component {
ModemType type_{MODEM_TYPE_UNKNOWN};
InternalGPIOPin *reset_pin_{nullptr};
InternalGPIOPin *power_pin_{nullptr};
InternalGPIOPin *pwrkey_pin_{nullptr};
int tx_pin_{-1};
int rx_pin_{-1};
std::string apn_{""};