diff --git a/UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake index 84e2ce1b4c..6d0419fed5 100644 --- a/UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake @@ -25,6 +25,8 @@ set(unittest-test-sources stubs/EventQueue_stub.cpp stubs/FileHandle_stub.cpp stubs/us_ticker_stub.cpp + stubs/UARTSerial_stub.cpp + stubs/SerialBase_stub.cpp stubs/mbed_assert_stub.c stubs/mbed_poll_stub.cpp stubs/Timer_stub.cpp diff --git a/UNITTESTS/stubs/ATHandler_stub.cpp b/UNITTESTS/stubs/ATHandler_stub.cpp index d03c7a4856..d48e02102a 100644 --- a/UNITTESTS/stubs/ATHandler_stub.cpp +++ b/UNITTESTS/stubs/ATHandler_stub.cpp @@ -418,3 +418,7 @@ void ATHandler::set_send_delay(uint16_t send_delay) { } +void ATHandler::set_baud(int baud_rate) +{ +} + diff --git a/UNITTESTS/stubs/AT_CellularDevice_stub.cpp b/UNITTESTS/stubs/AT_CellularDevice_stub.cpp index a5da7a1f3f..60346ab6ed 100644 --- a/UNITTESTS/stubs/AT_CellularDevice_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularDevice_stub.cpp @@ -261,3 +261,13 @@ nsapi_error_t AT_CellularDevice::clear() { return NSAPI_ERROR_OK; } + +nsapi_error_t AT_CellularDevice::set_baud_rate(int baud_rate) +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t AT_CellularDevice::set_baud_rate_impl(int baud_rate) +{ + return NSAPI_ERROR_OK; +} diff --git a/UNITTESTS/target_h/myCellularDevice.h b/UNITTESTS/target_h/myCellularDevice.h index 6752c0d200..f4a54f2cdf 100644 --- a/UNITTESTS/target_h/myCellularDevice.h +++ b/UNITTESTS/target_h/myCellularDevice.h @@ -176,6 +176,10 @@ public: { return NSAPI_ERROR_OK; } + nsapi_error_t set_baud_rate(int baud_rate) + { + return NSAPI_ERROR_OK; + } void verify_timeout_array(const uint16_t timeout[], int array_len) { diff --git a/features/cellular/framework/API/CellularDevice.h b/features/cellular/framework/API/CellularDevice.h index 5a3e99c046..2b94523024 100644 --- a/features/cellular/framework/API/CellularDevice.h +++ b/features/cellular/framework/API/CellularDevice.h @@ -443,6 +443,13 @@ public: */ virtual nsapi_error_t release_at_handler(ATHandler *at_handler) = 0; + /** Sets cellular modem to given baud rate + * + * @param baud_rate + * @return NSAPI_ERROR_OK on success, NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t set_baud_rate(int baud_rate) = 0; + protected: friend class AT_CellularNetwork; friend class AT_CellularContext; diff --git a/features/cellular/framework/AT/ATHandler.cpp b/features/cellular/framework/AT/ATHandler.cpp index a9591ed866..761fd46fff 100644 --- a/features/cellular/framework/AT/ATHandler.cpp +++ b/features/cellular/framework/AT/ATHandler.cpp @@ -1612,3 +1612,9 @@ void ATHandler::write_hex_string(char *str, size_t size) write(hexbuf, 2); } } + +void ATHandler::set_baud(int baud_rate) +{ + static_cast(_fileHandle)->set_baud(baud_rate); +} + diff --git a/features/cellular/framework/AT/ATHandler.h b/features/cellular/framework/AT/ATHandler.h index 21be72bbe6..2ab937eb6a 100644 --- a/features/cellular/framework/AT/ATHandler.h +++ b/features/cellular/framework/AT/ATHandler.h @@ -27,6 +27,8 @@ #include +#include "UARTSerial.h" + /** * If application calls associated FileHandle only from single thread context * then locking between AT command and response is not needed. However, @@ -219,6 +221,12 @@ public: */ void set_send_delay(uint16_t send_delay); + /** Sets UARTSerial filehandle to given baud rate + * + * @param baud_rate + */ + void set_baud(int baud_rate); + protected: void event(); #if defined AT_HANDLER_MUTEX && defined MBED_CONF_RTOS_PRESENT diff --git a/features/cellular/framework/AT/AT_CellularDevice.cpp b/features/cellular/framework/AT/AT_CellularDevice.cpp index 35cbab43fc..685ef748b7 100644 --- a/features/cellular/framework/AT/AT_CellularDevice.cpp +++ b/features/cellular/framework/AT/AT_CellularDevice.cpp @@ -635,3 +635,25 @@ nsapi_error_t AT_CellularDevice::clear() return err; } + +nsapi_error_t AT_CellularDevice::set_baud_rate(int baud_rate) +{ + nsapi_error_t error = set_baud_rate_impl(baud_rate); + + if (error) { + tr_warning("Baudrate was not changed to desired value: %d", baud_rate); + return error; + } + + _at->set_baud(baud_rate); + + // Give some time before starting using the UART with the new baud rate + rtos::ThisThread::sleep_for(3000); + + return error; +} + +nsapi_error_t AT_CellularDevice::set_baud_rate_impl(int baud_rate) +{ + return _at->at_cmd_discard("+IPR", "=", "%d", baud_rate); +} diff --git a/features/cellular/framework/AT/AT_CellularDevice.h b/features/cellular/framework/AT/AT_CellularDevice.h index 0e984a853b..25f78a87ad 100755 --- a/features/cellular/framework/AT/AT_CellularDevice.h +++ b/features/cellular/framework/AT/AT_CellularDevice.h @@ -135,6 +135,8 @@ public: virtual CellularContext *get_context_list() const; + virtual nsapi_error_t set_baud_rate(int baud_rate); + AT_CellularNetwork *_network; AT_CellularSMS *_sms; AT_CellularInformation *_information; @@ -153,6 +155,7 @@ protected: // Sets up parameters for AT handler, for now only the send delay and URCs. // This kind of routine is needed for initialisation routines that are virtual and therefore cannot be called from constructor. void setup_at_handler(); + virtual nsapi_error_t set_baud_rate_impl(int baud_rate); private: void urc_nw_deact(); diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp index 24c53470ab..50b0abfea0 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp @@ -95,6 +95,11 @@ nsapi_error_t QUECTEL_BC95::init() return _at->unlock_return_error(); } +nsapi_error_t QUECTEL_BC95::set_baud_rate_impl(int baud_rate) +{ + return _at->at_cmd_discard("+NATSPEED", "=", "%d%d%d%d%d%d%d", baud_rate, 30, 0, 2, 1, 0, 0); +} + #if MBED_CONF_QUECTEL_BC95_PROVIDE_DEFAULT #include "UARTSerial.h" CellularDevice *CellularDevice::get_default_instance() diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.h b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.h index 84a017ca6f..5c3d5ddf2a 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.h +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.h @@ -42,6 +42,7 @@ protected: // AT_CellularDevice virtual AT_CellularNetwork *open_network_impl(ATHandler &at); virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false); virtual AT_CellularInformation *open_information_impl(ATHandler &at); + virtual nsapi_error_t set_baud_rate_impl(int baud_rate); virtual nsapi_error_t init(); public: // NetworkInterface