added external gsm module reset

This commit is contained in:
Bogdan Pilyugin 2022-10-11 16:19:09 +02:00
parent aeb8bf1f37
commit 61c05053fd
3 changed files with 939 additions and 915 deletions

1283
Kconfig

File diff suppressed because it is too large Load Diff

View File

@ -114,5 +114,6 @@ void StartSystemTimer(void);
uint32_t GetUpTime(void); uint32_t GetUpTime(void);
void RegEthReset(void (*eth_rst)(uint8_t level)); void RegEthReset(void (*eth_rst)(uint8_t level));
void RegGSMReset(void (*gsm_rst)(uint8_t level));
#endif /* MAIN_INCLUDE_NETTRANSPORT_H_ */ #endif /* MAIN_INCLUDE_NETTRANSPORT_H_ */

View File

@ -1,277 +1,293 @@
/* Copyright 2022 Bogdan Pilyugin /* Copyright 2022 Bogdan Pilyugin
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* File name: GSMTransport.c * File name: GSMTransport.c
* Project: ChargePointMainboard * Project: ChargePointMainboard
* Created on: 2022-07-21 * Created on: 2022-07-21
* Author: Bogdan Pilyugin * Author: Bogdan Pilyugin
* Description: * Description:
*/ */
#include <string.h> #include <string.h>
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_netif.h" #include "esp_netif.h"
#include "esp_netif_ppp.h" #include "esp_netif_ppp.h"
#include "esp_event.h" #include "esp_event.h"
#include "esp_modem_api.h" #include "esp_modem_api.h"
#include "esp_log.h" #include "esp_log.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "SystemConfiguration.h" #include "SystemConfiguration.h"
#include "NetTransport.h" #include "NetTransport.h"
#if CONFIG_WEBGUIAPP_GPRS_ENABLE
#if CONFIG_WEBGUIAPP_GPRS_ENABLE static EventGroupHandle_t event_group = NULL;
static EventGroupHandle_t event_group = NULL; static const int CONNECT_BIT = BIT0;
static const int CONNECT_BIT = BIT0; static const char *TAG = "GSMTransport";
static const char *TAG = "GSMTransport"; static int ResetType = 0;
static int ResetType = 0; static bool isPPPinitializing = false;
static bool isPPPinitializing = false; #endif
#endif
static bool isPPPConn = false;
static bool isPPPConn = false; TaskHandle_t initTaskhandle;
TaskHandle_t initTaskhandle;
#define PPP_MODEM_TIMEOUT 40
#define PPP_MODEM_TIMEOUT 40 MODEM_INFO mod_info = { "-", "-", "-", "-" };
esp_netif_t *ppp_netif;
MODEM_INFO mod_info = { "-", "-", "-", "-" }; esp_modem_dce_t *dce;
esp_netif_t *ppp_netif; TaskHandle_t trasporttask;
esp_modem_dce_t *dce;
TaskHandle_t trasporttask; static void (*gsm_reset)(uint8_t level) = NULL;
void RegGSMReset(void (*gsm_rst)(uint8_t level))
{
gsm_reset = gsm_rst;
esp_netif_t* GetPPPNetifAdapter(void) }
{
return ppp_netif; esp_netif_t* GetPPPNetifAdapter(void)
} {
return ppp_netif;
MODEM_INFO* GetPPPModemInfo(void) }
{
return &mod_info; MODEM_INFO* GetPPPModemInfo(void)
} {
return &mod_info;
bool isPPPConnected(void) }
{
return isPPPConn; bool isPPPConnected(void)
} {
return isPPPConn;
#if CONFIG_WEBGUIAPP_GPRS_ENABLE }
static void on_ppp_changed(void *arg, esp_event_base_t event_base,
int32_t event_id, #if CONFIG_WEBGUIAPP_GPRS_ENABLE
void *event_data) static void on_ppp_changed(void *arg, esp_event_base_t event_base,
{ int32_t event_id,
ESP_LOGI(TAG, "PPP state changed event %d", event_id); void *event_data)
if (event_id == NETIF_PPP_ERRORUSER) {
{ ESP_LOGI(TAG, "PPP state changed event %d", event_id);
/* User interrupted event from esp-netif */ if (event_id == NETIF_PPP_ERRORUSER)
esp_netif_t *netif = event_data; {
ESP_LOGI(TAG, "User interrupted event from netif:%p", netif); /* User interrupted event from esp-netif */
} esp_netif_t *netif = event_data;
} ESP_LOGI(TAG, "User interrupted event from netif:%p", netif);
}
static void on_ip_event(void *arg, esp_event_base_t event_base, }
int32_t event_id,
void *event_data) static void on_ip_event(void *arg, esp_event_base_t event_base,
{ int32_t event_id,
ESP_LOGD(TAG, "IP event! %d", event_id); void *event_data)
if (event_id == IP_EVENT_PPP_GOT_IP) {
{ ESP_LOGD(TAG, "IP event! %d", event_id);
esp_netif_dns_info_t dns_info; if (event_id == IP_EVENT_PPP_GOT_IP)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data; esp_netif_dns_info_t dns_info;
esp_netif_t *netif = event->esp_netif;
ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "Modem Connect to PPP Server"); esp_netif_t *netif = event->esp_netif;
ESP_LOGI(TAG, "~~~~~~~~~~~~~~");
ESP_LOGI(TAG, "IP : " IPSTR, IP2STR(&event->ip_info.ip)); ESP_LOGI(TAG, "Modem Connect to PPP Server");
ESP_LOGI(TAG, "Netmask : " IPSTR, IP2STR(&event->ip_info.netmask)); ESP_LOGI(TAG, "~~~~~~~~~~~~~~");
ESP_LOGI(TAG, "Gateway : " IPSTR, IP2STR(&event->ip_info.gw)); ESP_LOGI(TAG, "IP : " IPSTR, IP2STR(&event->ip_info.ip));
esp_netif_get_dns_info(netif, 0, &dns_info); ESP_LOGI(TAG, "Netmask : " IPSTR, IP2STR(&event->ip_info.netmask));
ESP_LOGI(TAG, "Name Server1: " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4)); ESP_LOGI(TAG, "Gateway : " IPSTR, IP2STR(&event->ip_info.gw));
esp_netif_get_dns_info(netif, 1, &dns_info); esp_netif_get_dns_info(netif, 0, &dns_info);
ESP_LOGI(TAG, "Name Server2: " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4)); ESP_LOGI(TAG, "Name Server1: " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4));
ESP_LOGI(TAG, "~~~~~~~~~~~~~~"); esp_netif_get_dns_info(netif, 1, &dns_info);
xEventGroupSetBits(event_group, CONNECT_BIT); ESP_LOGI(TAG, "Name Server2: " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4));
isPPPConn = true; ESP_LOGI(TAG, "~~~~~~~~~~~~~~");
ESP_LOGI(TAG, "GOT ip event!!!"); xEventGroupSetBits(event_group, CONNECT_BIT);
} isPPPConn = true;
else if (event_id == IP_EVENT_PPP_LOST_IP) ESP_LOGI(TAG, "GOT ip event!!!");
{ }
ESP_LOGI(TAG, "Modem Disconnect from PPP Server"); else if (event_id == IP_EVENT_PPP_LOST_IP)
isPPPConn = false; {
} ESP_LOGI(TAG, "Modem Disconnect from PPP Server");
else if (event_id == IP_EVENT_GOT_IP6) isPPPConn = false;
{ }
ESP_LOGI(TAG, "GOT IPv6 event!"); else if (event_id == IP_EVENT_GOT_IP6)
ip_event_got_ip6_t *event = (ip_event_got_ip6_t*) event_data; {
ESP_LOGI(TAG, "Got IPv6 address " IPV6STR, IPV62STR(event->ip6_info.ip)); ESP_LOGI(TAG, "GOT IPv6 event!");
} ip_event_got_ip6_t *event = (ip_event_got_ip6_t*) event_data;
} ESP_LOGI(TAG, "Got IPv6 address " IPV6STR, IPV62STR(event->ip6_info.ip));
}
void ModemNotReady(void) }
{
void ModemNotReady(void)
} {
static void GSMInitTask(void *pvParameter) }
{
isPPPinitializing = true; static void GSMInitTask(void *pvParameter)
int starttype = *((int*) pvParameter); {
esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event); isPPPinitializing = true;
esp_event_handler_unregister(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed); int starttype = *((int*) pvParameter);
esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event);
if (dce) esp_event_handler_unregister(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed);
{
esp_modem_destroy(dce); if (dce)
} {
esp_modem_destroy(dce);
if (ppp_netif != NULL) }
{
esp_netif_destroy(ppp_netif); if (ppp_netif != NULL)
} {
esp_netif_destroy(ppp_netif);
if (starttype == 0) }
{
//SetGSM_PWR(OFF); if (starttype == 0)
gpio_set_level(GPIO_NUM_2, 0); {
vTaskDelay(pdMS_TO_TICKS(1000)); #if CONFIG_MODEM_DEVICE_POWER_CONTROL_PIN >= 0
//SetGSM_PWR(ON); gpio_set_level(CONFIG_MODEM_DEVICE_POWER_CONTROL_PIN, 0);
gpio_set_level(GPIO_NUM_2, 1); vTaskDelay(pdMS_TO_TICKS(1000));
} gpio_set_level(CONFIG_MODEM_DEVICE_POWER_CONTROL_PIN, 1);
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event, NULL)); #else
ESP_ERROR_CHECK(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, NULL)); if (gsm_reset)
event_group = xEventGroupCreate(); {
/* Configure the DTE */ gsm_reset(0);
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG(); vTaskDelay(pdMS_TO_TICKS(1000));
/* setup UART specific configuration based on kconfig options */ gsm_reset(1);
dte_config.uart_config.tx_io_num = CONFIG_MODEM_UART_TX_PIN; vTaskDelay(pdMS_TO_TICKS(10000));
dte_config.uart_config.rx_io_num = CONFIG_MODEM_UART_RX_PIN; }
dte_config.uart_config.rts_io_num = CONFIG_MODEM_UART_RTS_PIN; else
dte_config.uart_config.cts_io_num = CONFIG_MODEM_UART_CTS_PIN; {
dte_config.uart_config.rx_buffer_size = CONFIG_MODEM_UART_RX_BUFFER_SIZE; ESP_LOGE(TAG, "ethernet chip reset pin not defined");
dte_config.uart_config.tx_buffer_size = CONFIG_MODEM_UART_TX_BUFFER_SIZE; ESP_ERROR_CHECK(1);
dte_config.uart_config.event_queue_size = CONFIG_MODEM_UART_EVENT_QUEUE_SIZE; }
dte_config.task_stack_size = CONFIG_MODEM_UART_EVENT_TASK_STACK_SIZE; #endif
dte_config.task_priority = CONFIG_MODEM_UART_EVENT_TASK_PRIORITY;
dte_config.dte_buffer_size = CONFIG_MODEM_UART_RX_BUFFER_SIZE / 2; }
/* Configure the DCE */
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_MODEM_PPP_APN); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event, NULL));
/* Configure the PPP netif */ ESP_ERROR_CHECK(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, NULL));
esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_PPP() event_group = xEventGroupCreate();
; /* Configure the DTE */
esp_netif_conf.route_prio = PPP_PRIO; esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
esp_netif_config_t netif_ppp_config = ESP_NETIF_DEFAULT_PPP(); /* setup UART specific configuration based on kconfig options */
dte_config.uart_config.tx_io_num = CONFIG_MODEM_UART_TX_PIN;
netif_ppp_config.base = &esp_netif_conf; dte_config.uart_config.rx_io_num = CONFIG_MODEM_UART_RX_PIN;
/* Init netif object */ dte_config.uart_config.rts_io_num = CONFIG_MODEM_UART_RTS_PIN;
ppp_netif = esp_netif_new(&netif_ppp_config); dte_config.uart_config.cts_io_num = CONFIG_MODEM_UART_CTS_PIN;
assert(ppp_netif); dte_config.uart_config.rx_buffer_size = CONFIG_MODEM_UART_RX_BUFFER_SIZE;
dce = esp_modem_new_dev(ESP_MODEM_DCE_SIM800, &dte_config, &dce_config, ppp_netif); dte_config.uart_config.tx_buffer_size = CONFIG_MODEM_UART_TX_BUFFER_SIZE;
assert(dce); dte_config.uart_config.event_queue_size = CONFIG_MODEM_UART_EVENT_QUEUE_SIZE;
dte_config.task_stack_size = CONFIG_MODEM_UART_EVENT_TASK_STACK_SIZE;
mod_info.model[0] = 0x00; dte_config.task_priority = CONFIG_MODEM_UART_EVENT_TASK_PRIORITY;
int GSMConnectTimeout = 0; dte_config.dte_buffer_size = CONFIG_MODEM_UART_RX_BUFFER_SIZE / 2;
while (esp_modem_get_module_name(dce, mod_info.model) != ESP_OK) /* Configure the DCE */
{ esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_MODEM_PPP_APN);
if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT) /* Configure the PPP netif */
goto modem_init_fail; esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_PPP();
vTaskDelay(pdMS_TO_TICKS(1000));
} esp_netif_conf.route_prio = PPP_PRIO;
ESP_LOGI(TAG, "Module type:%s", mod_info.model); esp_netif_config_t netif_ppp_config = ESP_NETIF_DEFAULT_PPP();
mod_info.imsi[0] = 0x00; netif_ppp_config.base = &esp_netif_conf;
while (esp_modem_get_imsi(dce, mod_info.imsi) != ESP_OK) /* Init netif object */
{ ppp_netif = esp_netif_new(&netif_ppp_config);
if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT) assert(ppp_netif);
goto modem_init_fail; dce = esp_modem_new_dev(ESP_MODEM_DCE_SIM800, &dte_config, &dce_config, ppp_netif);
vTaskDelay(pdMS_TO_TICKS(1000)); assert(dce);
}
ESP_LOGI(TAG, "IMSI:%s", mod_info.imsi); mod_info.model[0] = 0x00;
int GSMConnectTimeout = 0;
mod_info.oper[0] = 0x00; int tech = 0; while (esp_modem_get_module_name(dce, mod_info.model) != ESP_OK)
while (esp_modem_get_operator_name(dce, mod_info.oper, &tech) != ESP_OK) {
{ if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT)
if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT) goto modem_init_fail;
goto modem_init_fail; vTaskDelay(pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(1000)); }
} ESP_LOGI(TAG, "Module type:%s", mod_info.model);
ESP_LOGI(TAG, "Operator:%s", mod_info.oper);
mod_info.imsi[0] = 0x00;
mod_info.imei[0] = 0x00; while (esp_modem_get_imsi(dce, mod_info.imsi) != ESP_OK)
while (esp_modem_get_imei(dce, mod_info.imei) != ESP_OK) {
{ if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT)
if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT) goto modem_init_fail;
goto modem_init_fail; vTaskDelay(pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(1000)); }
} ESP_LOGI(TAG, "IMSI:%s", mod_info.imsi);
ESP_LOGI(TAG, "IMEI:%s", mod_info.imei);
mod_info.oper[0] = 0x00;
while (esp_modem_set_mode(dce, ESP_MODEM_MODE_DATA) != ESP_OK) int tech = 0;
{ while (esp_modem_get_operator_name(dce, mod_info.oper, &tech) != ESP_OK)
if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT) {
goto modem_init_fail; if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT)
vTaskDelay(pdMS_TO_TICKS(1000)); goto modem_init_fail;
} vTaskDelay(pdMS_TO_TICKS(1000));
}
ESP_LOGI(TAG, "PPP data mode OK"); ESP_LOGI(TAG, "Operator:%s", mod_info.oper);
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY); mod_info.imei[0] = 0x00;
while (esp_modem_get_imei(dce, mod_info.imei) != ESP_OK)
isPPPinitializing = false; {
vTaskDelete(NULL); if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT)
goto modem_init_fail;
return; vTaskDelay(pdMS_TO_TICKS(1000));
modem_init_fail: }
ESP_LOGE(TAG, "PPP modem init error"); ESP_LOGI(TAG, "IMEI:%s", mod_info.imei);
isPPPinitializing = false;
vTaskDelete(NULL); while (esp_modem_set_mode(dce, ESP_MODEM_MODE_DATA) != ESP_OK)
} {
if (++GSMConnectTimeout >= PPP_MODEM_TIMEOUT)
void PPPModemColdStart(void) goto modem_init_fail;
{ vTaskDelay(pdMS_TO_TICKS(1000));
ResetType = 0; }
xTaskCreate(GSMInitTask, "GSMInitTask", 1024 * 4, &ResetType, 3, initTaskhandle);
} ESP_LOGI(TAG, "PPP data mode OK");
void PPPModemSoftRestart(void) xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
{
ResetType = 1; isPPPinitializing = false;
xTaskCreate(GSMInitTask, "GSMInitTask", 1024 * 4, &ResetType, 3, initTaskhandle); vTaskDelete(NULL);
}
return;
static void GSMRunTask(void *pvParameter) modem_init_fail:
{ ESP_LOGE(TAG, "PPP modem init error");
while (1) isPPPinitializing = false;
{ vTaskDelete(NULL);
if (!isPPPConn && !isPPPinitializing) }
{ //try to reconnect modem
ESP_LOGI(TAG, "PPP modem restart"); void PPPModemColdStart(void)
PPPModemColdStart(); {
} ResetType = 0;
vTaskDelay(pdMS_TO_TICKS(30000)); xTaskCreate(GSMInitTask, "GSMInitTask", 1024 * 4, &ResetType, 3, initTaskhandle);
} }
}
void PPPModemSoftRestart(void)
void PPPModemStart(void) {
{ ResetType = 1;
xTaskCreate(GSMRunTask, "GSMRunTask", 1024 * 4, &ResetType, 3, NULL); xTaskCreate(GSMInitTask, "GSMInitTask", 1024 * 4, &ResetType, 3, initTaskhandle);
} }
#endif static void GSMRunTask(void *pvParameter)
{
while (1)
{
if (!isPPPConn && !isPPPinitializing)
{ //try to reconnect modem
ESP_LOGI(TAG, "PPP modem restart");
PPPModemColdStart();
}
vTaskDelay(pdMS_TO_TICKS(30000));
}
}
void PPPModemStart(void)
{
xTaskCreate(GSMRunTask, "GSMRunTask", 1024 * 4, &ResetType, 3, NULL);
}
#endif