Skip to content

Commit b35430a

Browse files
committed
Add sync and update reset and wait logic
1 parent abb0123 commit b35430a

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

libraries/PPP/src/PPP.cpp

+48-30
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ PPPClass::PPPClass()
147147
,_pin_rst_act_low(true)
148148
,_pin(NULL)
149149
,_apn(NULL)
150-
,_rx_buffer_size(1024)
150+
,_rx_buffer_size(4096)
151151
,_tx_buffer_size(512)
152152
,_mode(ESP_MODEM_MODE_COMMAND)
153153
{
@@ -232,18 +232,6 @@ bool PPPClass::begin(ppp_modem_model_t model){
232232
return false;
233233
}
234234

235-
if(_pin_rst >= 0){
236-
if(_pin_rst_act_low){
237-
pinMode(_pin_rst, OUTPUT_OPEN_DRAIN);
238-
} else {
239-
pinMode(_pin_rst, OUTPUT);
240-
}
241-
digitalWrite(_pin_rst, !_pin_rst_act_low);
242-
delay(200);
243-
digitalWrite(_pin_rst, _pin_rst_act_low);
244-
delay(200);
245-
}
246-
247235
Network.begin();
248236
_esp_modem = this;
249237
if(_ppp_ev_instance == NULL && esp_event_handler_instance_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &_ppp_event_cb, NULL, &_ppp_ev_instance)){
@@ -264,21 +252,32 @@ bool PPPClass::begin(ppp_modem_model_t model){
264252

265253
/* Configure the DTE */
266254
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
267-
/* setup UART specific configuration based on kconfig options */
268255
dte_config.uart_config.tx_io_num = _pin_tx;
269256
dte_config.uart_config.rx_io_num = _pin_rx;
270257
dte_config.uart_config.rts_io_num = _pin_rts;
271258
dte_config.uart_config.cts_io_num = _pin_cts;
272259
dte_config.uart_config.flow_control = _flow_ctrl;
273260
dte_config.uart_config.rx_buffer_size = _rx_buffer_size;
274261
dte_config.uart_config.tx_buffer_size = _tx_buffer_size;
275-
dte_config.uart_config.event_queue_size = 20;
276-
dte_config.task_stack_size = 2048;
277-
dte_config.task_priority = 5;
278-
dte_config.dte_buffer_size = _rx_buffer_size / 2;
279262

280263
/* Configure the DCE */
281264
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(_apn);
265+
266+
/* Reset the Modem */
267+
if(_pin_rst >= 0){
268+
log_v("Resetting the modem");
269+
if(_pin_rst_act_low){
270+
pinMode(_pin_rst, OUTPUT_OPEN_DRAIN);
271+
} else {
272+
pinMode(_pin_rst, OUTPUT);
273+
}
274+
digitalWrite(_pin_rst, !_pin_rst_act_low);
275+
delay(200);
276+
digitalWrite(_pin_rst, _pin_rst_act_low);
277+
delay(100);
278+
}
279+
280+
/* Start the DCE */
282281
_dce = esp_modem_new_dev((esp_modem_dce_device_t)model, &dte_config, &dce_config, _esp_netif);
283282
if(_dce == NULL){
284283
log_e("esp_modem_new_dev failed");
@@ -287,27 +286,33 @@ bool PPPClass::begin(ppp_modem_model_t model){
287286

288287
esp_modem_set_error_cb(_dce, _ppp_error_cb);
289288

290-
// wait to be able to talk to the modem
291-
log_v("Waiting for response from the modem");
292-
while(esp_modem_read_pin(_dce, pin_ok) != ESP_OK && trys < 50){
293-
trys++;
294-
delay(500);
295-
}
296-
if(trys >= 50){
297-
log_e("Failed to wait for communication");
298-
goto err;
289+
/* Wait for Modem to respond */
290+
if(_pin_rst >= 0){
291+
// wait to be able to talk to the modem
292+
log_v("Waiting for response from the modem");
293+
while(esp_modem_sync(_dce) != ESP_OK && trys < 50){
294+
trys++;
295+
delay(500);
296+
}
297+
if(trys >= 50){
298+
log_e("Failed to wait for communication");
299+
goto err;
300+
}
301+
} else {
302+
// try to communicate with the modem
303+
299304
}
300305

301-
// enable flow control
306+
/* enable flow control */
302307
if (dte_config.uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) {
303308
ret = esp_modem_set_flow_control(_dce, 2, 2); //2/2 means HW Flow Control.
304309
if (ret != ESP_OK) {
305-
log_e("Failed to set the set_flow_control mode: [%d] %s", ret, esp_err_to_name(ret));
310+
log_e("Failed to set the hardware flow control: [%d] %s", ret, esp_err_to_name(ret));
306311
goto err;
307312
}
308313
}
309314

310-
// check if PIN needed
315+
/* check if PIN needed */
311316
if (esp_modem_read_pin(_dce, pin_ok) == ESP_OK && pin_ok == false) {
312317
if (_pin == NULL || esp_modem_set_pin(_dce, _pin) != ESP_OK) {
313318
log_e("PIN verification failed!");
@@ -382,6 +387,19 @@ void PPPClass::end(void)
382387
_mode = ESP_MODEM_MODE_COMMAND;
383388
}
384389

390+
bool PPPClass::sync() const
391+
{
392+
if(_dce == NULL){
393+
return false;
394+
}
395+
396+
if(_mode == ESP_MODEM_MODE_DATA){
397+
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
398+
return false;
399+
}
400+
return esp_modem_sync(_dce) == ESP_OK;
401+
}
402+
385403
bool PPPClass::attached() const
386404
{
387405
if(_dce == NULL){

libraries/PPP/src/PPP.h

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class PPPClass: public NetworkInterface {
4242
int networkMode() const;
4343
int radioState() const;
4444
bool attached() const;
45+
bool sync() const;
4546

4647
esp_modem_dce_mode_t mode() const { return _mode; }
4748
bool mode(esp_modem_dce_mode_t m);

0 commit comments

Comments
 (0)