Skip to content

Commit d32f7ed

Browse files
committed
Add support for changing baud rate
1 parent 615217d commit d32f7ed

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

libraries/PPP/src/PPP.cpp

+47-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "esp_netif.h"
66
#include "esp_netif_ppp.h"
77
#include <string>
8+
#include "driver/uart.h"
9+
#include "hal/uart_ll.h"
810

911
typedef struct { void * arg; } PdpContext;
1012
#include "esp_modem_api.h"
@@ -150,6 +152,7 @@ PPPClass::PPPClass()
150152
,_rx_buffer_size(4096)
151153
,_tx_buffer_size(512)
152154
,_mode(ESP_MODEM_MODE_COMMAND)
155+
,_uart_num(UART_NUM_1)
153156
{
154157
}
155158

@@ -207,7 +210,7 @@ bool PPPClass::setPins(int8_t tx, int8_t rx, int8_t rts, int8_t cts, esp_modem_f
207210
return true;
208211
}
209212

210-
bool PPPClass::begin(ppp_modem_model_t model){
213+
bool PPPClass::begin(ppp_modem_model_t model, uint8_t uart_num, int baud_rate){
211214
esp_err_t ret = ESP_OK;
212215
bool pin_ok = false;
213216
int trys = 0;
@@ -232,8 +235,12 @@ bool PPPClass::begin(ppp_modem_model_t model){
232235
return false;
233236
}
234237

235-
Network.begin();
238+
_uart_num = uart_num;
236239
_esp_modem = this;
240+
241+
Network.begin();
242+
243+
/* Listen for PPP status events */
237244
if(_ppp_ev_instance == NULL && esp_event_handler_instance_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &_ppp_event_cb, NULL, &_ppp_ev_instance)){
238245
log_e("event_handler_instance_register for NETIF_PPP_STATUS Failed!");
239246
return false;
@@ -247,7 +254,7 @@ bool PPPClass::begin(ppp_modem_model_t model){
247254
return false;
248255
}
249256

250-
/* attach to receive events */
257+
/* Attach to receive IP events */
251258
initNetif(ESP_NETIF_ID_PPP);
252259

253260
/* Configure the DTE */
@@ -259,6 +266,8 @@ bool PPPClass::begin(ppp_modem_model_t model){
259266
dte_config.uart_config.flow_control = _flow_ctrl;
260267
dte_config.uart_config.rx_buffer_size = _rx_buffer_size;
261268
dte_config.uart_config.tx_buffer_size = _tx_buffer_size;
269+
dte_config.uart_config.port_num = _uart_num;
270+
dte_config.uart_config.baud_rate = baud_rate;
262271

263272
/* Configure the DCE */
264273
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(_apn);
@@ -300,7 +309,14 @@ bool PPPClass::begin(ppp_modem_model_t model){
300309
}
301310
} else {
302311
// try to communicate with the modem
303-
312+
if(esp_modem_sync(_dce) != ESP_OK){
313+
log_v("Modem does not respond to AT, maybe in DATA mode? ...exiting network mode");
314+
esp_modem_set_mode(_dce, ESP_MODEM_MODE_COMMAND);
315+
if(esp_modem_sync(_dce) != ESP_OK){
316+
log_e("Modem failed to respond to AT!");
317+
goto err;
318+
}
319+
}
304320
}
305321

306322
/* enable flow control */
@@ -692,6 +708,33 @@ bool PPPClass::storeProfile(){
692708
return true;
693709
}
694710

711+
bool PPPClass::setBaudrate(int baudrate) {
712+
if(_dce == NULL){
713+
return false;
714+
}
715+
716+
if(_mode == ESP_MODEM_MODE_DATA){
717+
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
718+
return false;
719+
}
720+
721+
esp_err_t err = esp_modem_set_baud(_dce, baudrate);
722+
if (err != ESP_OK) {
723+
log_e("esp_modem_set_baud failed with %d %s", err, esp_err_to_name(err));
724+
return false;
725+
}
726+
727+
uint32_t sclk_freq;
728+
err = uart_get_sclk_freq(UART_SCLK_DEFAULT, &sclk_freq);
729+
if(err != ESP_OK){
730+
log_e("uart_get_sclk_freq failed with %d %s", err, esp_err_to_name(err));
731+
return false;
732+
}
733+
uart_ll_set_baudrate(UART_LL_GET_HW(_uart_num), (uint32_t)baudrate, sclk_freq);
734+
735+
return true;
736+
}
737+
695738

696739
bool PPPClass::sms(const char * num, const char * message) {
697740
if(_dce == NULL){

libraries/PPP/src/PPP.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class PPPClass: public NetworkInterface {
2929
bool setPins(int8_t tx, int8_t rx, int8_t rts=-1, int8_t cts=-1, esp_modem_flow_ctrl_t flow_ctrl=ESP_MODEM_FLOW_CONTROL_NONE);
3030
void setResetPin(int8_t rst, bool active_low=true);
3131

32-
bool begin(ppp_modem_model_t model);
32+
bool begin(ppp_modem_model_t model, uint8_t uart_num=1, int baud_rate=115200);
3333
void end();
3434

3535
// Modem DCE APIs
@@ -51,6 +51,7 @@ class PPPClass: public NetworkInterface {
5151
bool reset();
5252
bool storeProfile();
5353

54+
bool setBaudrate(int baudrate);
5455
bool sms(const char * num, const char * message);
5556

5657
esp_modem_dce_t * handle() const;
@@ -76,6 +77,7 @@ class PPPClass: public NetworkInterface {
7677
int _rx_buffer_size;
7778
int _tx_buffer_size;
7879
esp_modem_dce_mode_t _mode;
80+
uint8_t _uart_num;
7981

8082
static bool pppDetachBus(void * bus_pointer);
8183
};

0 commit comments

Comments
 (0)