Skip to content

Commit c908404

Browse files
authored
Merge pull request #11783 from mirelachirica/set_baud_rate
Adding set baud rate routine
2 parents f560e79 + 3fdbe24 commit c908404

File tree

12 files changed

+78
-0
lines changed

12 files changed

+78
-0
lines changed

TESTS/netsocket/tcp/tcpsocket_recv_100k.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "unity/unity.h"
2222
#include "utest.h"
2323
#include "tcp_tests.h"
24+
#include "CellularDevice.h"
2425

2526
using namespace utest::v1;
2627

@@ -119,6 +120,11 @@ void rcv_n_chk_against_rfc864_pattern(TCPSocket &sock)
119120
void TCPSOCKET_RECV_100K()
120121
{
121122
SKIP_IF_TCP_UNSUPPORTED();
123+
124+
#ifdef MBED_CONF_APP_BAUD_RATE
125+
CellularDevice::get_default_instance()->set_baud_rate(MBED_CONF_APP_BAUD_RATE);
126+
#endif
127+
122128
TCPSocket sock;
123129
if (_tcpsocket_connect_to_chargen_srv(sock) != NSAPI_ERROR_OK) {
124130
TEST_FAIL();

UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ set(unittest-test-sources
2525
stubs/EventQueue_stub.cpp
2626
stubs/FileHandle_stub.cpp
2727
stubs/us_ticker_stub.cpp
28+
stubs/UARTSerial_stub.cpp
29+
stubs/SerialBase_stub.cpp
2830
stubs/mbed_assert_stub.c
2931
stubs/mbed_poll_stub.cpp
3032
stubs/Timer_stub.cpp

UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,7 @@ void ATHandler::set_send_delay(uint16_t send_delay)
418418
{
419419
}
420420

421+
void ATHandler::set_baud(int baud_rate)
422+
{
423+
}
424+

UNITTESTS/stubs/AT_CellularDevice_stub.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,13 @@ nsapi_error_t AT_CellularDevice::clear()
261261
{
262262
return NSAPI_ERROR_OK;
263263
}
264+
265+
nsapi_error_t AT_CellularDevice::set_baud_rate(int baud_rate)
266+
{
267+
return NSAPI_ERROR_OK;
268+
}
269+
270+
nsapi_error_t AT_CellularDevice::set_baud_rate_impl(int baud_rate)
271+
{
272+
return NSAPI_ERROR_OK;
273+
}

UNITTESTS/target_h/myCellularDevice.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ class myCellularDevice : public CellularDevice {
176176
{
177177
return NSAPI_ERROR_OK;
178178
}
179+
nsapi_error_t set_baud_rate(int baud_rate)
180+
{
181+
return NSAPI_ERROR_OK;
182+
}
179183

180184
void verify_timeout_array(const uint16_t timeout[], int array_len)
181185
{

features/cellular/framework/API/CellularDevice.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,13 @@ class CellularDevice {
443443
*/
444444
virtual nsapi_error_t release_at_handler(ATHandler *at_handler) = 0;
445445

446+
/** Sets cellular modem to given baud rate
447+
*
448+
* @param baud_rate
449+
* @return NSAPI_ERROR_OK on success, NSAPI_ERROR_DEVICE_ERROR on failure
450+
*/
451+
virtual nsapi_error_t set_baud_rate(int baud_rate) = 0;
452+
446453
protected:
447454
friend class AT_CellularNetwork;
448455
friend class AT_CellularContext;

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,3 +1612,9 @@ void ATHandler::write_hex_string(char *str, size_t size)
16121612
write(hexbuf, 2);
16131613
}
16141614
}
1615+
1616+
void ATHandler::set_baud(int baud_rate)
1617+
{
1618+
static_cast<UARTSerial *>(_fileHandle)->set_baud(baud_rate);
1619+
}
1620+

features/cellular/framework/AT/ATHandler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#include <cstdarg>
2929

30+
#include "UARTSerial.h"
31+
3032
/**
3133
* If application calls associated FileHandle only from single thread context
3234
* then locking between AT command and response is not needed. However,
@@ -219,6 +221,12 @@ class ATHandler {
219221
*/
220222
void set_send_delay(uint16_t send_delay);
221223

224+
/** Sets UARTSerial filehandle to given baud rate
225+
*
226+
* @param baud_rate
227+
*/
228+
void set_baud(int baud_rate);
229+
222230
protected:
223231
void event();
224232
#if defined AT_HANDLER_MUTEX && defined MBED_CONF_RTOS_PRESENT

features/cellular/framework/AT/AT_CellularDevice.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,25 @@ nsapi_error_t AT_CellularDevice::clear()
635635

636636
return err;
637637
}
638+
639+
nsapi_error_t AT_CellularDevice::set_baud_rate(int baud_rate)
640+
{
641+
nsapi_error_t error = set_baud_rate_impl(baud_rate);
642+
643+
if (error) {
644+
tr_warning("Baudrate was not changed to desired value: %d", baud_rate);
645+
return error;
646+
}
647+
648+
_at->set_baud(baud_rate);
649+
650+
// Give some time before starting using the UART with the new baud rate
651+
rtos::ThisThread::sleep_for(3000);
652+
653+
return error;
654+
}
655+
656+
nsapi_error_t AT_CellularDevice::set_baud_rate_impl(int baud_rate)
657+
{
658+
return _at->at_cmd_discard("+IPR", "=", "%d", baud_rate);
659+
}

features/cellular/framework/AT/AT_CellularDevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ class AT_CellularDevice : public CellularDevice {
135135

136136
virtual CellularContext *get_context_list() const;
137137

138+
virtual nsapi_error_t set_baud_rate(int baud_rate);
139+
138140
AT_CellularNetwork *_network;
139141
AT_CellularSMS *_sms;
140142
AT_CellularInformation *_information;
@@ -153,6 +155,7 @@ class AT_CellularDevice : public CellularDevice {
153155
// Sets up parameters for AT handler, for now only the send delay and URCs.
154156
// This kind of routine is needed for initialisation routines that are virtual and therefore cannot be called from constructor.
155157
void setup_at_handler();
158+
virtual nsapi_error_t set_baud_rate_impl(int baud_rate);
156159

157160
private:
158161
void urc_nw_deact();

features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ nsapi_error_t QUECTEL_BC95::init()
9595
return _at->unlock_return_error();
9696
}
9797

98+
nsapi_error_t QUECTEL_BC95::set_baud_rate_impl(int baud_rate)
99+
{
100+
return _at->at_cmd_discard("+NATSPEED", "=", "%d%d%d%d%d%d%d", baud_rate, 30, 0, 2, 1, 0, 0);
101+
}
102+
98103
#if MBED_CONF_QUECTEL_BC95_PROVIDE_DEFAULT
99104
#include "UARTSerial.h"
100105
CellularDevice *CellularDevice::get_default_instance()

features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class QUECTEL_BC95 : public AT_CellularDevice {
4242
virtual AT_CellularNetwork *open_network_impl(ATHandler &at);
4343
virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false);
4444
virtual AT_CellularInformation *open_information_impl(ATHandler &at);
45+
virtual nsapi_error_t set_baud_rate_impl(int baud_rate);
4546
virtual nsapi_error_t init();
4647

4748
public: // NetworkInterface

0 commit comments

Comments
 (0)