Skip to content

Commit c685651

Browse files
author
Mirela Chirica
committed
Cellular: Added set/get for baud rate
1 parent cbf9f06 commit c685651

File tree

11 files changed

+114
-0
lines changed

11 files changed

+114
-0
lines changed

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,18 @@ 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+
}
274+
275+
nsapi_error_t AT_CellularDevice::get_baud_rate(int &baud_rate)
276+
{
277+
return NSAPI_ERROR_OK;
278+
}

UNITTESTS/target_h/myCellularDevice.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ 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+
}
183+
184+
nsapi_error_t get_baud_rate(int &baud_rate)
185+
{
186+
return NSAPI_ERROR_OK;
187+
}
179188

180189
void verify_timeout_array(const uint16_t timeout[], int array_len)
181190
{

features/cellular/framework/API/CellularDevice.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,20 @@ 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+
453+
/** Gets the current baud rate of the cellular modem
454+
*
455+
* @param baud_rate
456+
* @return NSAPI_ERROR_OK on success, NSAPI_ERROR_DEVICE_ERROR on failure
457+
*/
458+
virtual nsapi_error_t get_baud_rate(int &baud_rate) = 0;
459+
446460
protected:
447461
friend class AT_CellularNetwork;
448462
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 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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,42 @@ 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+
int current_baud_rate = -1;
642+
643+
nsapi_error_t error = get_baud_rate(current_baud_rate);
644+
645+
if (error != NSAPI_ERROR_OK || current_baud_rate == baud_rate) {
646+
return error;
647+
}
648+
649+
error = set_baud_rate_impl(baud_rate);
650+
if (error) {
651+
return error;
652+
}
653+
654+
_at->set_baud(baud_rate);
655+
656+
// Give some time before starting using the UART with the new baud rate
657+
rtos::ThisThread::sleep_for(3000);
658+
659+
error = get_baud_rate(current_baud_rate);
660+
661+
if (current_baud_rate != baud_rate) {
662+
tr_warning("Baudrate was not changed to desired value: %d", baud_rate);
663+
}
664+
665+
return error;
666+
}
667+
668+
nsapi_error_t AT_CellularDevice::set_baud_rate_impl(int baud_rate)
669+
{
670+
return _at->at_cmd_discard("+IPR", "=", "%d", baud_rate);
671+
}
672+
673+
nsapi_error_t AT_CellularDevice::get_baud_rate(int &baud_rate)
674+
{
675+
return _at->at_cmd_int("+IPR", "?", baud_rate);
676+
}

features/cellular/framework/AT/AT_CellularDevice.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ 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+
140+
virtual nsapi_error_t get_baud_rate(int &baud_rate);
141+
138142
AT_CellularNetwork *_network;
139143
AT_CellularSMS *_sms;
140144
AT_CellularInformation *_information;
@@ -153,6 +157,7 @@ class AT_CellularDevice : public CellularDevice {
153157
// Sets up parameters for AT handler, for now only the send delay and URCs.
154158
// This kind of routine is needed for initialisation routines that are virtual and therefore cannot be called from constructor.
155159
void setup_at_handler();
160+
virtual nsapi_error_t set_baud_rate_impl(int baud_rate);
156161

157162
private:
158163
void urc_nw_deact();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ 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, 3, 1, 0, 1);
101+
}
102+
103+
nsapi_error_t QUECTEL_BC95::get_baud_rate(int &baud_rate)
104+
{
105+
return _at->at_cmd_int("+NATSPEED", "?", baud_rate);
106+
}
107+
98108
#if MBED_CONF_QUECTEL_BC95_PROVIDE_DEFAULT
99109
#include "UARTSerial.h"
100110
CellularDevice *CellularDevice::get_default_instance()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ 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);
46+
virtual nsapi_error_t get_baud_rate(int &baud_rate);
4547
virtual nsapi_error_t init();
4648

4749
public: // NetworkInterface

0 commit comments

Comments
 (0)