From 9f87dead1c4420f9679ab36856faf581e0129596 Mon Sep 17 00:00:00 2001 From: Maciej Gibowicz Date: Thu, 4 Mar 2021 14:33:56 +0100 Subject: [PATCH] [EGD-4853] Add Wake up GSM modem on demand System wakes up Cellular when there is a need to communicate with the modem, e.g. to handle an outgoing call --- module-bsp/CMakeLists.txt | 1 + module-bsp/board/linux/lpm/LinuxLPM.cpp | 1 - .../rt1051/bsp/cellular/rt1051_cellular.cpp | 15 +++++ .../rt1051/bsp/cellular/rt1051_cellular.hpp | 1 + .../board/rt1051/bsp/lpm/Oscillator.cpp | 50 +++++++++++++++ .../board/rt1051/bsp/lpm/Oscillator.hpp | 11 ++++ module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp | 33 +++------- module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp | 2 - .../rt1051/drivers/RT1051DriverLPUART.cpp | 63 +++++++++++++++++++ .../rt1051/drivers/RT1051DriverLPUART.hpp | 25 ++++++++ .../rt1051/drivers/RT1051DriverOscillator.cpp | 15 +++++ .../rt1051/drivers/RT1051DriverOscillator.hpp | 16 +++++ module-bsp/bsp/BoardDefinitions.hpp | 2 + module-bsp/bsp/cellular/bsp_cellular.cpp | 7 ++- module-bsp/bsp/cellular/bsp_cellular.hpp | 5 +- module-bsp/bsp/lpm/bsp_lpm.cpp | 5 -- module-bsp/bsp/lpm/bsp_lpm.hpp | 2 - module-bsp/drivers/lpuart/DriverLPUART.cpp | 41 ++++++++++++ module-bsp/drivers/lpuart/DriverLPUART.hpp | 40 ++++++++++++ module-bsp/targets/Target_RT1051.cmake | 3 + module-cellular/Modem/TS0710/TS0710.cpp | 9 ++- module-cellular/Modem/TS0710/TS0710.h | 1 + .../service-cellular/CellularCall.cpp | 10 +++ .../service-cellular/ServiceCellular.cpp | 3 + .../service-cellular/CellularCall.hpp | 5 ++ module-sys/SystemManager/PowerManager.cpp | 11 +--- 26 files changed, 330 insertions(+), 47 deletions(-) create mode 100644 module-bsp/board/rt1051/bsp/lpm/Oscillator.cpp create mode 100644 module-bsp/board/rt1051/bsp/lpm/Oscillator.hpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverLPUART.cpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverLPUART.hpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverOscillator.cpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverOscillator.hpp create mode 100644 module-bsp/drivers/lpuart/DriverLPUART.cpp create mode 100644 module-bsp/drivers/lpuart/DriverLPUART.hpp diff --git a/module-bsp/CMakeLists.txt b/module-bsp/CMakeLists.txt index 5b5dcedc3491b1c4d196a48e3c528826ca139c98..f06af5742e57bdfac39c0e03cb575a4f34964baf 100644 --- a/module-bsp/CMakeLists.txt +++ b/module-bsp/CMakeLists.txt @@ -15,6 +15,7 @@ set(SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/drivers/i2c/DriverI2C.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/drivers/pwm/DriverPWM.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/drivers/lpspi/DriverLPSPI.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/drivers/lpuart/DriverLPUART.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/drivers/usdhc/DriverUSDHC.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/drivers/semc/DriverSEMC.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/drivers/gpio/DriverGPIO.cpp" diff --git a/module-bsp/board/linux/lpm/LinuxLPM.cpp b/module-bsp/board/linux/lpm/LinuxLPM.cpp index 38e5688f037a2753bfec25d364a30884dab4870c..a4d86c6ee61f4d65dec9864cd4f58b22748449e6 100644 --- a/module-bsp/board/linux/lpm/LinuxLPM.cpp +++ b/module-bsp/board/linux/lpm/LinuxLPM.cpp @@ -32,7 +32,6 @@ namespace bsp void LinuxLPM::SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource source) { - currentOscSource = source; } } // namespace bsp diff --git a/module-bsp/board/rt1051/bsp/cellular/rt1051_cellular.cpp b/module-bsp/board/rt1051/bsp/cellular/rt1051_cellular.cpp index 4e432015fa09fe9a9b8600f89ea016b1d008b350..0f4e1a0efd6d897ecabdb299b83520d025b4a9e0 100644 --- a/module-bsp/board/rt1051/bsp/cellular/rt1051_cellular.cpp +++ b/module-bsp/board/rt1051/bsp/cellular/rt1051_cellular.cpp @@ -84,6 +84,9 @@ namespace bsp return; } + driverLPUART = drivers::DriverLPUART::Create( + "cellular", static_cast(BoardDefinitions::CELLULAR_LPUART_INSTANCE)); + lpuart_config_t s_cellularConfig; LPUART_GetDefaultConfig(&s_cellularConfig); @@ -210,6 +213,10 @@ namespace bsp sendXfer.data = static_cast(buf); sendXfer.dataSize = nbytes; + if (isInSleepMode) { + ExitSleep(); + } + EnableTx(); uartDmaHandle.userData = xTaskGetCurrentTaskHandle(); @@ -280,12 +287,20 @@ namespace bsp { gpio_3->WritePin(magic_enum::enum_integer(BoardDefinitions::CELLULAR_GPIO_3_DTR_PIN), 1); gpio_2->WritePin(magic_enum::enum_integer(BoardDefinitions::CELLULAR_GPIO_2_WAKEUP_PIN), 1); + if (driverLPUART) { + driverLPUART->Disable(); + } + isInSleepMode = true; } void RT1051Cellular::ExitSleep() { + if (driverLPUART) { + driverLPUART->Enable(); + } gpio_3->WritePin(magic_enum::enum_integer(BoardDefinitions::CELLULAR_GPIO_3_DTR_PIN), 0); gpio_2->WritePin(magic_enum::enum_integer(BoardDefinitions::CELLULAR_GPIO_2_WAKEUP_PIN), 0); + isInSleepMode = false; vTaskDelay(pdMS_TO_TICKS(15)); } diff --git a/module-bsp/board/rt1051/bsp/cellular/rt1051_cellular.hpp b/module-bsp/board/rt1051/bsp/cellular/rt1051_cellular.hpp index fdd13a5ce9daf23e7f58f2c492fe537820d475de..29b549439a91612361cafbd63f5a7cbccbf90d3f 100644 --- a/module-bsp/board/rt1051/bsp/cellular/rt1051_cellular.hpp +++ b/module-bsp/board/rt1051/bsp/cellular/rt1051_cellular.hpp @@ -114,6 +114,7 @@ namespace bsp static TaskHandle_t blockedTaskHandle; private: + bool isInSleepMode{false}; // Constants const static uint32_t baudrate = 115200; const static uint32_t rxStreamBufferLength = 1024; diff --git a/module-bsp/board/rt1051/bsp/lpm/Oscillator.cpp b/module-bsp/board/rt1051/bsp/lpm/Oscillator.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca1c84992cbbbcd9582fc4016815d38330d811ff --- /dev/null +++ b/module-bsp/board/rt1051/bsp/lpm/Oscillator.cpp @@ -0,0 +1,50 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "Oscillator.hpp" +#include "ClockState.hpp" +#include + +namespace bsp +{ + inline constexpr uint8_t OscillatorReadyCounterValue{127}; + + void EnableExternalOscillator() + { + if (!IsExternalOscillatorEnabled()) { + CLOCK_InitExternalClk(0); + /// Switch DCDC to use DCDC external OSC + DCDC_SetClockSource(DCDC, kDCDC_ClockExternalOsc); + /// Switch clock source to external OSC. + CLOCK_SwitchOsc(kCLOCK_XtalOsc); + /// Wait CCM operation finishes + while (CCM->CDHIPR != 0) {} + /// Set Oscillator ready counter value. + CCM->CCR = (CCM->CCR & (~CCM_CCR_OSCNT_MASK)) | CCM_CCR_OSCNT(bsp::OscillatorReadyCounterValue); + } + } + + void DisableExternalOscillator() + { + if (IsExternalOscillatorEnabled()) { + if (IsClockEnabled(kCLOCK_Lpuart1) || IsClockEnabled(kCLOCK_Lpuart2) || IsClockEnabled(kCLOCK_Lpuart3) || + IsClockEnabled(kCLOCK_Lpuart4) || IsClockEnabled(kCLOCK_Lpuart5) || IsClockEnabled(kCLOCK_Lpuart6) || + IsClockEnabled(kCLOCK_Lpuart7) || IsClockEnabled(kCLOCK_Lpuart8)) { + return; + } + + /// Switch DCDC to use DCDC internal OSC + DCDC_SetClockSource(DCDC, kDCDC_ClockInternalOsc); + /// Switch clock source to internal RC + CLOCK_SwitchOsc(kCLOCK_RcOsc); + CLOCK_DeinitExternalClk(); + /// Wait CCM operation finishes + while (CCM->CDHIPR != 0) {} + } + } + + [[nodiscard]] bool IsExternalOscillatorEnabled() + { + return !(XTALOSC24M->LOWPWR_CTRL & XTALOSC24M_LOWPWR_CTRL_OSC_SEL_MASK); + } +}; // namespace bsp diff --git a/module-bsp/board/rt1051/bsp/lpm/Oscillator.hpp b/module-bsp/board/rt1051/bsp/lpm/Oscillator.hpp new file mode 100644 index 0000000000000000000000000000000000000000..69d9e51a852c3181365a8eeb2136a7d46ace0d99 --- /dev/null +++ b/module-bsp/board/rt1051/bsp/lpm/Oscillator.hpp @@ -0,0 +1,11 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +namespace bsp +{ + void EnableExternalOscillator(); + void DisableExternalOscillator(); + [[nodiscard]] bool IsExternalOscillatorEnabled(); +} // namespace bsp diff --git a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp index 7909cede1e3f027a4348629ac099695d285b4245..517f88a193c7eb52f98c015c3d84c978e645033a 100644 --- a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp +++ b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp @@ -9,8 +9,9 @@ #include "bsp/watchdog/watchdog.hpp" #include #include -#include #include "ClockState.hpp" +#include "Oscillator.hpp" +#include "critical.hpp" namespace bsp { @@ -78,33 +79,15 @@ namespace bsp void RT1051LPM::SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource source) { if (source == bsp::LowPowerMode::OscillatorSource::Internal) { - if (IsClockEnabled(kCLOCK_Lpuart1) || IsClockEnabled(kCLOCK_Lpuart2) || IsClockEnabled(kCLOCK_Lpuart3) || - IsClockEnabled(kCLOCK_Lpuart4) || IsClockEnabled(kCLOCK_Lpuart5) || IsClockEnabled(kCLOCK_Lpuart6) || - IsClockEnabled(kCLOCK_Lpuart7) || IsClockEnabled(kCLOCK_Lpuart8)) { - return; - } - - /// Switch DCDC to use DCDC internal OSC - DCDC_SetClockSource(DCDC, kDCDC_ClockInternalOsc); - /// Switch clock source to internal RC - CLOCK_SwitchOsc(kCLOCK_RcOsc); - CLOCK_DeinitExternalClk(); - /// Wait CCM operation finishes - while (CCM->CDHIPR != 0) {} + cpp_freertos::CriticalSection::Enter(); + bsp::DisableExternalOscillator(); + cpp_freertos::CriticalSection::Exit(); } else if (source == bsp::LowPowerMode::OscillatorSource::External) { - CLOCK_InitExternalClk(0); - /// Switch DCDC to use DCDC external OSC - DCDC_SetClockSource(DCDC, kDCDC_ClockExternalOsc); - /// Switch clock source to external OSC. - CLOCK_SwitchOsc(kCLOCK_XtalOsc); - /// Wait CCM operation finishes - while (CCM->CDHIPR != 0) {} - /// Set Oscillator ready counter value. - CCM->CCR = (CCM->CCR & (~CCM_CCR_OSCNT_MASK)) | CCM_CCR_OSCNT(bsp::OscillatorReadyCounterValue); + cpp_freertos::CriticalSection::Enter(); + bsp::EnableExternalOscillator(); + cpp_freertos::CriticalSection::Exit(); } - - currentOscSource = source; } } // namespace bsp diff --git a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp index 6c51e8eb1a56d12b8ae1ed9201efe69fcca67420..dfbaba8904336e15026b6cdf3e3b93d517f5de33 100644 --- a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp +++ b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp @@ -10,8 +10,6 @@ namespace bsp { - inline constexpr uint8_t OscillatorReadyCounterValue{127}; - class RT1051LPM : public LowPowerMode { public: diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverLPUART.cpp b/module-bsp/board/rt1051/drivers/RT1051DriverLPUART.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d4867d76a24ccf333fa5de5a267b362798f38b5 --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverLPUART.cpp @@ -0,0 +1,63 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "RT1051DriverLPUART.hpp" +#include "critical.hpp" +#include "board/rt1051/bsp/lpm/ClockState.hpp" + +namespace drivers +{ + + RT1051DriverLPUART::RT1051DriverLPUART(std::string name, LPUARTInstances inst) : DriverLPUART(std::move(name), inst) + { + switch (instance) { + case LPUARTInstances::LPUART_1: + lpuartClock = kCLOCK_Lpuart1; + break; + case LPUARTInstances::LPUART_2: + lpuartClock = kCLOCK_Lpuart2; + break; + case LPUARTInstances::LPUART_3: + lpuartClock = kCLOCK_Lpuart3; + break; + case LPUARTInstances::LPUART_4: + lpuartClock = kCLOCK_Lpuart4; + break; + case LPUARTInstances::LPUART_5: + lpuartClock = kCLOCK_Lpuart5; + break; + case LPUARTInstances::LPUART_6: + lpuartClock = kCLOCK_Lpuart6; + break; + case LPUARTInstances::LPUART_7: + lpuartClock = kCLOCK_Lpuart7; + break; + case LPUARTInstances::LPUART_8: + lpuartClock = kCLOCK_Lpuart8; + break; + } + } + + void RT1051DriverLPUART::Enable() + { + cpp_freertos::CriticalSection::Enter(); + if (!oscillatorDriver) { + oscillatorDriver = std::make_shared(); + } + if (!bsp::IsClockEnabled(lpuartClock)) { + CLOCK_EnableClock(lpuartClock); + } + cpp_freertos::CriticalSection::Exit(); + } + + void RT1051DriverLPUART::Disable() + { + cpp_freertos::CriticalSection::Enter(); + if (bsp::IsClockEnabled(lpuartClock)) { + CLOCK_DisableClock(lpuartClock); + } + oscillatorDriver.reset(); + cpp_freertos::CriticalSection::Exit(); + } + +} // namespace drivers diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverLPUART.hpp b/module-bsp/board/rt1051/drivers/RT1051DriverLPUART.hpp new file mode 100644 index 0000000000000000000000000000000000000000..3c7e65dfe32db27d725270b73b328d4abc49e892 --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverLPUART.hpp @@ -0,0 +1,25 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include "drivers/lpuart/DriverLPUART.hpp" +#include "RT1051DriverOscillator.hpp" +#include "board/rt1051/common/clock_config.h" + +namespace drivers +{ + class RT1051DriverLPUART : public DriverLPUART + { + public: + explicit RT1051DriverLPUART(std::string name, LPUARTInstances inst); + + void Enable() final; + void Disable() final; + + private: + std::shared_ptr oscillatorDriver; + clock_ip_name_t lpuartClock; + }; + +} // namespace drivers diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverOscillator.cpp b/module-bsp/board/rt1051/drivers/RT1051DriverOscillator.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e41a3ca736d36311587819bb17d9f8ce0efba399 --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverOscillator.cpp @@ -0,0 +1,15 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "RT1051DriverOscillator.hpp" +#include "board/rt1051/bsp/lpm/Oscillator.hpp" + +namespace drivers +{ + + RT1051DriverOscillator::RT1051DriverOscillator() noexcept + { + bsp::EnableExternalOscillator(); + } + +} // namespace drivers diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverOscillator.hpp b/module-bsp/board/rt1051/drivers/RT1051DriverOscillator.hpp new file mode 100644 index 0000000000000000000000000000000000000000..e7cfaa34f856744b472a028a1af420f6c35fe261 --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverOscillator.hpp @@ -0,0 +1,16 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include "board/rt1051/common/clock_config.h" + +namespace drivers +{ + class RT1051DriverOscillator + { + public: + RT1051DriverOscillator() noexcept; + }; + +} // namespace drivers diff --git a/module-bsp/bsp/BoardDefinitions.hpp b/module-bsp/bsp/BoardDefinitions.hpp index 11e0731bcad69328639a9fdb97bbb5d52f8221a0..289685073d875fb42889d8e0ac4161815c90660f 100644 --- a/module-bsp/bsp/BoardDefinitions.hpp +++ b/module-bsp/bsp/BoardDefinitions.hpp @@ -8,6 +8,7 @@ #include "drivers/gpio/DriverGPIO.hpp" #include "drivers/lpspi/DriverLPSPI.hpp" #include "drivers/usdhc/DriverUSDHC.hpp" +#include "drivers/lpuart/DriverLPUART.hpp" enum class BoardDefinitions { @@ -76,6 +77,7 @@ enum class BoardDefinitions CELLULAR_GPIO_2_ANTSEL_PIN = 2, CELLULAR_GPIO_1_STATUS_PIN = 2, // GPIO_AD_B0_02 CELLULAR_GPIO_2_USB_BOOT_PIN = 24, // GPIO_B1_08, output + CELLULAR_LPUART_INSTANCE = static_cast(drivers::LPUARTInstances::LPUART_1), EINK_DMA = static_cast(drivers::DMAInstances ::DMA_0), EINK_DMAMUX = static_cast(drivers::DMAMuxInstances ::DMAMUX0), diff --git a/module-bsp/bsp/cellular/bsp_cellular.cpp b/module-bsp/bsp/cellular/bsp_cellular.cpp index 73cbe1cc6dccc98921c0ab0e5f2d329ae80ce8b0..23bcd7c90048805aff48a6af2409aee5c892f3a2 100644 --- a/module-bsp/bsp/cellular/bsp_cellular.cpp +++ b/module-bsp/bsp/cellular/bsp_cellular.cpp @@ -31,4 +31,9 @@ namespace bsp{ return {}; } -} \ No newline at end of file + [[nodiscard]] auto Cellular::GetCellularDevice() const noexcept -> std::shared_ptr + { + return driverLPUART; + } + +} diff --git a/module-bsp/bsp/cellular/bsp_cellular.hpp b/module-bsp/bsp/cellular/bsp_cellular.hpp index 1e1e10ce5e54764f3b2fbea12f0963754b955e19..8622f7a570c325f3226feb9f8a4fdcbd745df5cb 100644 --- a/module-bsp/bsp/cellular/bsp_cellular.hpp +++ b/module-bsp/bsp/cellular/bsp_cellular.hpp @@ -7,7 +7,7 @@ #include #include #include - +#include "drivers/lpuart/DriverLPUART.hpp" #include namespace bsp { @@ -55,8 +55,11 @@ namespace cellular virtual void SelectAntenna(bsp::cellular::antenna antenna) = 0; virtual bsp::cellular::antenna GetAntenna() = 0; + [[nodiscard]] auto GetCellularDevice() const noexcept -> std::shared_ptr; + protected: bool isInitialized = false; + std::shared_ptr driverLPUART; }; namespace cellular { diff --git a/module-bsp/bsp/lpm/bsp_lpm.cpp b/module-bsp/bsp/lpm/bsp_lpm.cpp index 98e5cff2a4bdc49869ddba9b8d91b3ea7e9c0a3d..cf83c3363e25fbd19726d9cd958031b06146b290 100644 --- a/module-bsp/bsp/lpm/bsp_lpm.cpp +++ b/module-bsp/bsp/lpm/bsp_lpm.cpp @@ -33,9 +33,4 @@ namespace bsp{ { return currentFrequency; } - - LowPowerMode::OscillatorSource LowPowerMode::GetCurrentOscillatorSource() const noexcept - { - return currentOscSource; - } } diff --git a/module-bsp/bsp/lpm/bsp_lpm.hpp b/module-bsp/bsp/lpm/bsp_lpm.hpp index 4700eac0b90ebea523502dc7dd2b2105b43904b4..1b29a2b0c845fd1eebab636b17a51f70c2f2a246 100644 --- a/module-bsp/bsp/lpm/bsp_lpm.hpp +++ b/module-bsp/bsp/lpm/bsp_lpm.hpp @@ -31,11 +31,9 @@ namespace bsp { [[nodiscard]] virtual uint32_t GetCpuFrequency() const noexcept = 0; virtual void SwitchOscillatorSource(OscillatorSource source) = 0; - [[nodiscard]] OscillatorSource GetCurrentOscillatorSource() const noexcept; protected: CpuFrequencyHz currentFrequency = CpuFrequencyHz::Level_6; - OscillatorSource currentOscSource = OscillatorSource::External; }; } // namespace bsp diff --git a/module-bsp/drivers/lpuart/DriverLPUART.cpp b/module-bsp/drivers/lpuart/DriverLPUART.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c7c8c348edcf858afee1a53ee67aea079efdb72 --- /dev/null +++ b/module-bsp/drivers/lpuart/DriverLPUART.cpp @@ -0,0 +1,41 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "DriverLPUART.hpp" +#include "critical.hpp" + +#if defined(TARGET_RT1051) +#include "board/rt1051/drivers/RT1051DriverLPUART.hpp" +#elif defined(TARGET_Linux) + +#else +#error "Unsupported target" +#endif + +namespace drivers +{ + + std::weak_ptr DriverLPUART::singleton[magic_enum::enum_count()]; + + std::shared_ptr DriverLPUART::Create(std::string name, const drivers::LPUARTInstances instance) + { + cpp_freertos::CriticalSection::Enter(); + std::shared_ptr inst = singleton[static_cast(instance)].lock(); + + if (!inst) { +#if defined(TARGET_RT1051) + inst = std::make_shared(std::move(name), instance); +#elif defined(TARGET_Linux) +#else +#error "Unsupported target" +#endif + + singleton[static_cast(instance)] = std::move(inst); + } + + cpp_freertos::CriticalSection::Exit(); + + return inst; + } + +} // namespace drivers diff --git a/module-bsp/drivers/lpuart/DriverLPUART.hpp b/module-bsp/drivers/lpuart/DriverLPUART.hpp new file mode 100644 index 0000000000000000000000000000000000000000..5d9ac3760e16ba0fbe9b5696cf160ea9b3f1d9b3 --- /dev/null +++ b/module-bsp/drivers/lpuart/DriverLPUART.hpp @@ -0,0 +1,40 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include +#include +#include + +namespace drivers +{ + + enum class LPUARTInstances + { + LPUART_1, + LPUART_2, + LPUART_3, + LPUART_4, + LPUART_5, + LPUART_6, + LPUART_7, + LPUART_8 + }; + + class DriverLPUART : public devices::Device + { + public: + static std::shared_ptr Create(std::string name, const LPUARTInstances inst); + + explicit DriverLPUART(std::string name, LPUARTInstances inst) : devices::Device(std::move(name)), instance(inst) + {} + + protected: + LPUARTInstances instance; + + private: + static std::weak_ptr singleton[magic_enum::enum_count()]; + }; + +} // namespace drivers diff --git a/module-bsp/targets/Target_RT1051.cmake b/module-bsp/targets/Target_RT1051.cmake index 1b65e27b8a787d19a9f439db5e7d49b06176a918..d4133259a5213635c5ab4cb05f488fa7d2f6ca96 100644 --- a/module-bsp/targets/Target_RT1051.cmake +++ b/module-bsp/targets/Target_RT1051.cmake @@ -58,6 +58,7 @@ set(BOARD_SOURCES ${BOARD_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bsp/lpm/RT1051LPM.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bsp/lpm/CpuFreqLPM.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bsp/lpm/ClockState.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bsp/lpm/Oscillator.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bluetooth/BluetoothCommon.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bluetooth/BlueKitchen.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bsp/rtc/rtc.cpp" @@ -68,6 +69,8 @@ set(BOARD_SOURCES ${BOARD_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverPLL.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverPLL2.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverLPSPI.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverLPUART.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverOscillator.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverUSDHC.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverSEMC.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverI2C.cpp" diff --git a/module-cellular/Modem/TS0710/TS0710.cpp b/module-cellular/Modem/TS0710/TS0710.cpp index 7c84da788ffe0cb3b8f73e420314b9d3b851a5c4..2c999b8e8784066a4318b4a57925606db876f1bf 100644 --- a/module-cellular/Modem/TS0710/TS0710.cpp +++ b/module-cellular/Modem/TS0710/TS0710.cpp @@ -11,6 +11,7 @@ #include #include #include +#include std::map TypeOfFrame_text = {{TypeOfFrame_e::SABM, "SABM"}, {TypeOfFrame_e::UA, "UA"}, @@ -560,6 +561,12 @@ void TS0710::ExitSleepMode(void) return pv_cellular->ExitSleep(); } +void TS0710::RegisterCellularDevice(void) +{ + auto deviceRegistrationMsg = std::make_shared(pv_cellular->GetCellularDevice()); + pv_parent->bus.sendUnicast(std::move(deviceRegistrationMsg), service::name::system_manager); +} + void TS0710::SetupEchoCalceller(EchoCancellerStrength strength) { switch (strength) { @@ -620,4 +627,4 @@ void TS0710::SetupEchoCalceller(EchoCancellerStrength strength) parser->cmd(at::factory(at::AT::QEEC) + "24,768"); parser->cmd(at::factory(at::AT::QEEC) + "33,896"); }; -} \ No newline at end of file +} diff --git a/module-cellular/Modem/TS0710/TS0710.h b/module-cellular/Modem/TS0710/TS0710.h index 385d1f4958dccd4fe6343f3ac205c369c412396a..317add199a63f1ff72b3257cd1be5aba1fef3fe6 100644 --- a/module-cellular/Modem/TS0710/TS0710.h +++ b/module-cellular/Modem/TS0710/TS0710.h @@ -439,6 +439,7 @@ class TS0710 void TurnOffModem(void); void EnterSleepMode(void); void ExitSleepMode(void); + void RegisterCellularDevice(void); }; #endif //_TS0710_H diff --git a/module-services/service-cellular/CellularCall.cpp b/module-services/service-cellular/CellularCall.cpp index 1c0a65435ec49956a018227a95ca099d3f8be2d0..5cebd3c39e16dca6b004534cf8e926c2d58df875 100644 --- a/module-services/service-cellular/CellularCall.cpp +++ b/module-services/service-cellular/CellularCall.cpp @@ -25,6 +25,10 @@ namespace CellularCall return false; } + if (cpuSentinel) { + cpuSentinel->HoldMinimumFrequency(bsp::CpuFrequencyHz::Level_6); + } + clear(); CalllogRecord callRec; callRec.type = type; @@ -101,4 +105,10 @@ namespace CellularCall call.presentation = number.getFormatted().empty() ? PresentationType::PR_UNKNOWN : PresentationType::PR_ALLOWED; call.phoneNumber = number; } + + void CellularCall::setCpuSentinel(std::shared_ptr sentinel) + { + cpuSentinel = std::move(sentinel); + } + } // namespace CellularCall diff --git a/module-services/service-cellular/ServiceCellular.cpp b/module-services/service-cellular/ServiceCellular.cpp index 258c05ff069b031d9c9664d6da24835adcf782bb..e3f8a18223d1b0ff7b5a0f581d7cf6a4847fe782 100644 --- a/module-services/service-cellular/ServiceCellular.cpp +++ b/module-services/service-cellular/ServiceCellular.cpp @@ -257,10 +257,13 @@ sys::ReturnCodes ServiceCellular::InitHandler() ::settings::SettingsScope::Global); cpuSentinel = std::make_shared(serviceName, this); + ongoingCall.setCpuSentinel(cpuSentinel); auto sentinelRegistrationMsg = std::make_shared(cpuSentinel); bus.sendUnicast(sentinelRegistrationMsg, service::name::system_manager); + cmux->RegisterCellularDevice(); + // temporarily limit the minimum CPU frequency // due to problems with the UART of the GSM modem cpuSentinel->HoldMinimumFrequency(bsp::CpuFrequencyHz::Level_4); diff --git a/module-services/service-cellular/service-cellular/CellularCall.hpp b/module-services/service-cellular/service-cellular/CellularCall.hpp index d6460d237ee05403fc1e7b55b06573a5e77c0d27..3a1d0858e8fa74bbb0c16741683814f8ca479769 100644 --- a/module-services/service-cellular/service-cellular/CellularCall.hpp +++ b/module-services/service-cellular/service-cellular/CellularCall.hpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace CellularCall { @@ -32,6 +33,8 @@ namespace CellularCall std::function endCallAction; utils::time::Timestamp startActiveTime = 0; + std::shared_ptr cpuSentinel; + void setType(const CallType type) { call.type = type; @@ -84,5 +87,7 @@ namespace CellularCall { return call.ID != 0; } + + void setCpuSentinel(std::shared_ptr sentinel); }; } // namespace CellularCall diff --git a/module-sys/SystemManager/PowerManager.cpp b/module-sys/SystemManager/PowerManager.cpp index cf77f621be5678bb5dfdae5b61a316f14c13ce66..ede5f970099a576a20200f70c3c6cabe5e8e8faa 100644 --- a/module-sys/SystemManager/PowerManager.cpp +++ b/module-sys/SystemManager/PowerManager.cpp @@ -59,13 +59,10 @@ namespace sys void PowerManager::IncreaseCpuFrequency() const { const auto freq = lowPowerControl->GetCurrentFrequencyLevel(); - const auto oscSource = lowPowerControl->GetCurrentOscillatorSource(); if (freq == bsp::CpuFrequencyHz::Level_1) { // switch osc source first - if (oscSource == bsp::LowPowerMode::OscillatorSource::Internal) { - lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::External); - } + lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::External); // then switch external RAM clock source if (driverSEMC) { @@ -110,12 +107,8 @@ namespace sys } if (level == bsp::CpuFrequencyHz::Level_1) { - const auto oscSource = lowPowerControl->GetCurrentOscillatorSource(); - // then switch osc source - if (oscSource == bsp::LowPowerMode::OscillatorSource::External) { - lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::Internal); - } + lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::Internal); // and switch external RAM clock source if (driverSEMC) {