From ec56afc93393af6c84ba69a2cf9c5ca5d0b83cf2 Mon Sep 17 00:00:00 2001 From: Maciej-Mudita Date: Tue, 19 Jan 2021 13:01:21 +0100 Subject: [PATCH] [EGD-5381] Add Low Power Drivers Peripherals clock synchronization dependent on PLL2 --- config/ProjectConfig.cmake | 1 + module-bsp/CMakeLists.txt | 6 ++- module-bsp/board/cross/eMMC/eMMC.cpp | 10 +++- module-bsp/board/cross/eMMC/eMMC.hpp | 2 + module-bsp/board/linux/lpm/LinuxLPM.cpp | 4 -- module-bsp/board/linux/lpm/LinuxLPM.h | 1 - .../board/rt1051/bsp/lpm/ClockState.cpp | 19 +++++++ .../board/rt1051/bsp/lpm/ClockState.hpp | 11 ++++ module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp | 32 +----------- module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp | 7 --- .../rt1051/drivers/RT1051DriverLPSPI.cpp | 51 +++++++++++++++++++ .../rt1051/drivers/RT1051DriverLPSPI.hpp | 25 +++++++++ .../board/rt1051/drivers/RT1051DriverPLL2.cpp | 33 ++++++++++++ .../board/rt1051/drivers/RT1051DriverPLL2.hpp | 23 +++++++++ .../board/rt1051/drivers/RT1051DriverSEMC.cpp | 40 +++++++++++++++ .../board/rt1051/drivers/RT1051DriverSEMC.hpp | 26 ++++++++++ .../rt1051/drivers/RT1051DriverUSDHC.cpp | 45 ++++++++++++++++ .../rt1051/drivers/RT1051DriverUSDHC.hpp | 25 +++++++++ module-bsp/bsp/BoardDefinitions.hpp | 4 ++ module-bsp/bsp/lpm/bsp_lpm.cpp | 5 -- module-bsp/bsp/lpm/bsp_lpm.hpp | 9 ---- module-bsp/devices/Device.cpp | 22 ++++++++ module-bsp/devices/Device.hpp | 27 ++++++++++ module-bsp/drivers/lpspi/DriverLPSPI.cpp | 41 +++++++++++++++ module-bsp/drivers/lpspi/DriverLPSPI.hpp | 36 +++++++++++++ module-bsp/drivers/semc/DriverSEMC.cpp | 42 +++++++++++++++ module-bsp/drivers/semc/DriverSEMC.hpp | 27 ++++++++++ module-bsp/drivers/usdhc/DriverUSDHC.cpp | 42 +++++++++++++++ module-bsp/drivers/usdhc/DriverUSDHC.hpp | 34 +++++++++++++ module-bsp/targets/Target_RT1051.cmake | 5 ++ module-services/service-eink/EinkDisplay.cpp | 19 ++++++- module-services/service-eink/EinkDisplay.hpp | 4 ++ module-services/service-eink/ServiceEink.cpp | 5 ++ module-sys/CMakeLists.txt | 1 + module-sys/SystemManager/DeviceManager.cpp | 47 +++++++++++++++++ module-sys/SystemManager/DeviceManager.hpp | 30 +++++++++++ module-sys/SystemManager/PowerManager.cpp | 32 +++++++----- module-sys/SystemManager/PowerManager.hpp | 4 ++ module-sys/SystemManager/SystemManager.cpp | 12 +++++ module-sys/SystemManager/SystemManager.hpp | 2 + .../messages/DeviceRegistrationMessage.hpp | 28 ++++++++++ module-utils/Utils.hpp | 1 - source/MessageType.hpp | 2 + 43 files changed, 766 insertions(+), 76 deletions(-) create mode 100644 module-bsp/board/rt1051/bsp/lpm/ClockState.cpp create mode 100644 module-bsp/board/rt1051/bsp/lpm/ClockState.hpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverLPSPI.cpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverLPSPI.hpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverPLL2.cpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverPLL2.hpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverSEMC.cpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverSEMC.hpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverUSDHC.cpp create mode 100644 module-bsp/board/rt1051/drivers/RT1051DriverUSDHC.hpp create mode 100644 module-bsp/devices/Device.cpp create mode 100644 module-bsp/devices/Device.hpp create mode 100644 module-bsp/drivers/lpspi/DriverLPSPI.cpp create mode 100644 module-bsp/drivers/lpspi/DriverLPSPI.hpp create mode 100644 module-bsp/drivers/semc/DriverSEMC.cpp create mode 100644 module-bsp/drivers/semc/DriverSEMC.hpp create mode 100644 module-bsp/drivers/usdhc/DriverUSDHC.cpp create mode 100644 module-bsp/drivers/usdhc/DriverUSDHC.hpp create mode 100644 module-sys/SystemManager/DeviceManager.cpp create mode 100644 module-sys/SystemManager/DeviceManager.hpp create mode 100644 module-sys/SystemManager/messages/DeviceRegistrationMessage.hpp diff --git a/config/ProjectConfig.cmake b/config/ProjectConfig.cmake index 3f37a3613ec008ae6947d9566e64642ee6230ae1..2a2212a48af5d6bbddc025a3024a17e562912658 100644 --- a/config/ProjectConfig.cmake +++ b/config/ProjectConfig.cmake @@ -46,6 +46,7 @@ set(PROJECT_CONFIG_DEFINITIONS PROJECT_CONFIG_USER_DYNMEM_SIZE=9*1024*1024 USB_DEVICE_VENDOR_ID=${USB_DEVICE_VENDOR_ID} USB_DEVICE_PRODUCT_ID=${USB_DEVICE_PRODUCT_ID} + MAGIC_ENUM_RANGE_MAX=256 CACHE INTERNAL "" ) diff --git a/module-bsp/CMakeLists.txt b/module-bsp/CMakeLists.txt index 925483ac4ab8b01f7fa537dd38f53e610f56ed7e..cf67f1136bb16110de8aa79d8363c35b8810d843 100644 --- a/module-bsp/CMakeLists.txt +++ b/module-bsp/CMakeLists.txt @@ -12,11 +12,13 @@ set(SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/drivers/dma/DriverDMA.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/drivers/i2c/DriverI2C.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/drivers/pwm/DriverPWM.cpp" - #"${CMAKE_CURRENT_SOURCE_DIR}/drivers/sai/DriverSAI.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/drivers/lpspi/DriverLPSPI.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" - board/linux/lpm/LinuxLPM.cpp board/linux/lpm/LinuxLPM.h "${CMAKE_CURRENT_SOURCE_DIR}/bsp/bluetooth/Bluetooth.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/bsp/common.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/devices/Device.cpp" ) if (NOT ${PROJECT_TARGET} STREQUAL "TARGET_Linux") diff --git a/module-bsp/board/cross/eMMC/eMMC.cpp b/module-bsp/board/cross/eMMC/eMMC.cpp index 77d8e241747ecf59bd90d877f660ea7172df56d7..6bff016e7f57ccb8816b1c5fdc9ea792898ba18e 100644 --- a/module-bsp/board/cross/eMMC/eMMC.cpp +++ b/module-bsp/board/cross/eMMC/eMMC.cpp @@ -20,6 +20,8 @@ namespace bsp RetCode eMMC::Init() { + driverUSDHC = drivers::DriverUSDHC::Create( + "EMMC", static_cast(BoardDefinitions::EMMC_USDHC_INSTANCE)); #if defined(TARGET_RT1051) // pll = DriverPLL::Create(static_cast(BoardDefinitions ::EMMC_PLL),DriverPLLParams{}); @@ -56,7 +58,9 @@ namespace bsp RetCode eMMC::ReadBlocks(uint8_t *buffer, uint32_t startBlock, uint32_t blockCount) { #if defined(TARGET_RT1051) + driverUSDHC->Enable(); auto ret = MMC_ReadBlocks(&mmcCard, buffer, startBlock, blockCount); + driverUSDHC->Disable(); if (ret != kStatus_Success) { return RetCode::Failure; } @@ -71,7 +75,9 @@ namespace bsp RetCode eMMC::WriteBlocks(const uint8_t *buffer, uint32_t startBlock, uint32_t blockCount) { #if defined(TARGET_RT1051) + driverUSDHC->Enable(); auto ret = MMC_WriteBlocks(&mmcCard, buffer, startBlock, blockCount); + driverUSDHC->Disable(); if (ret != kStatus_Success) { return RetCode::Failure; } @@ -86,7 +92,9 @@ namespace bsp RetCode eMMC::SwitchPartition(eMMC::Partition partition) { #if defined(TARGET_RT1051) + driverUSDHC->Enable(); auto ret = MMC_SelectPartition(&mmcCard, static_cast(partition)); + driverUSDHC->Disable(); if (ret != kStatus_Success) { return RetCode::Failure; } @@ -98,4 +106,4 @@ namespace bsp #endif } -} // namespace bsp \ No newline at end of file +} // namespace bsp diff --git a/module-bsp/board/cross/eMMC/eMMC.hpp b/module-bsp/board/cross/eMMC/eMMC.hpp index a0b022b1333311be10581313ebbcfa5c2a2db2fd..e43a5166c96b3be653ea81949c27fb3836f83bd4 100644 --- a/module-bsp/board/cross/eMMC/eMMC.hpp +++ b/module-bsp/board/cross/eMMC/eMMC.hpp @@ -15,6 +15,7 @@ #endif #include "bsp/common.hpp" +#include "drivers/usdhc/DriverUSDHC.hpp" namespace bsp { @@ -55,6 +56,7 @@ namespace bsp #else #error "Unsupported target" #endif + std::shared_ptr driverUSDHC; }; } // namespace bsp diff --git a/module-bsp/board/linux/lpm/LinuxLPM.cpp b/module-bsp/board/linux/lpm/LinuxLPM.cpp index db440c9c1400ebbc0183f2aa6ea7828166e37f99..62db1d869b86da74183c8734131c291e8038c47d 100644 --- a/module-bsp/board/linux/lpm/LinuxLPM.cpp +++ b/module-bsp/board/linux/lpm/LinuxLPM.cpp @@ -30,8 +30,4 @@ namespace bsp currentOscSource = source; } - void LinuxLPM::SwitchPll2State(bsp::LowPowerMode::Pll2State state) - { - currentPll2State = state; - } } // namespace bsp diff --git a/module-bsp/board/linux/lpm/LinuxLPM.h b/module-bsp/board/linux/lpm/LinuxLPM.h index 7bf8f6aae392f11be1606ef47f446136f80e6bd4..d91b18d1faf86ac071d93878a86f40c6200cac3e 100644 --- a/module-bsp/board/linux/lpm/LinuxLPM.h +++ b/module-bsp/board/linux/lpm/LinuxLPM.h @@ -16,7 +16,6 @@ namespace bsp int32_t Reboot() override final; void SetCpuFrequency(CpuFrequency freq) final; void SwitchOscillatorSource(OscillatorSource source) final; - void SwitchPll2State(Pll2State state) final; }; } // namespace bsp diff --git a/module-bsp/board/rt1051/bsp/lpm/ClockState.cpp b/module-bsp/board/rt1051/bsp/lpm/ClockState.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0cde669f4a170de497470bd569e84942fb1362a --- /dev/null +++ b/module-bsp/board/rt1051/bsp/lpm/ClockState.cpp @@ -0,0 +1,19 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "ClockState.hpp" + +namespace bsp +{ + inline constexpr uint8_t CCM_TupleShift{8}; + inline constexpr uint8_t CCM_TupleMask{0x1F}; + inline constexpr uint32_t ClockNeededRunWaitMode{kCLOCK_ClockNeededRunWait}; + + bool IsClockEnabled(clock_ip_name_t name) + { + const auto index = static_cast(name) >> CCM_TupleShift; + const auto shift = static_cast(name) & CCM_TupleMask; + + return ((*reinterpret_cast(&CCM->CCGR0 + index)) & (ClockNeededRunWaitMode << shift)); + } +}; // namespace bsp diff --git a/module-bsp/board/rt1051/bsp/lpm/ClockState.hpp b/module-bsp/board/rt1051/bsp/lpm/ClockState.hpp new file mode 100644 index 0000000000000000000000000000000000000000..aba7a445452a982829061d302e4dfbd319797fcc --- /dev/null +++ b/module-bsp/board/rt1051/bsp/lpm/ClockState.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 + +#include + +namespace bsp +{ + [[nodiscard]] bool IsClockEnabled(clock_ip_name_t name); +} diff --git a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp index e15b9da4a108e06850380e029c01c50345fcd8c3..4a81d8ab56b6d8df1586dfee8541729dc0939304 100644 --- a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp +++ b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp @@ -10,6 +10,7 @@ #include #include #include +#include "ClockState.hpp" namespace bsp { @@ -102,35 +103,4 @@ namespace bsp currentOscSource = source; } - void RT1051LPM::SwitchPll2State(bsp::LowPowerMode::Pll2State state) - { - if (state == bsp::LowPowerMode::Pll2State::Disable) { - if (IsClockEnabled(kCLOCK_Lpspi1) || IsClockEnabled(kCLOCK_Lpspi2) || IsClockEnabled(kCLOCK_Lpspi3) || - IsClockEnabled(kCLOCK_Lpspi4) || IsClockEnabled(kCLOCK_Usdhc1) || IsClockEnabled(kCLOCK_Usdhc2)) { - return; - } - - /// First switch external RAM clock - CLOCK_SetMux(kCLOCK_SemcMux, 0); - /// Then turn off PLL2 - clkPLL2setup(CLK_DISABLE); - } - else { - /// First turn on PLL2 - clkPLL2setup(CLK_ENABLE); - /// Then switch external RAM clock - CLOCK_SetMux(kCLOCK_SemcMux, 1); - } - - currentPll2State = state; - } - - bool RT1051LPM::IsClockEnabled(clock_ip_name_t name) const noexcept - { - const auto index = static_cast(name) >> CCM_TupleShift; - const auto shift = static_cast(name) & CCM_TupleMask; - - return ((*reinterpret_cast(&CCM->CCGR0 + index)) & (ClockNeededRunWaitMode << shift)); - } - } // namespace bsp diff --git a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp index 4e9c7e6242556758e02ea12ef90dea1d6915b692..e6d443c6fb2e0b5603cfcd09991b9e253b38b4e4 100644 --- a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp +++ b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp @@ -7,14 +7,10 @@ #include "bsp/lpm/bsp_lpm.hpp" #include "drivers/gpio/DriverGPIO.hpp" #include "CpuFreqLPM.hpp" -#include namespace bsp { inline constexpr uint8_t OscillatorReadyCounterValue{127}; - inline constexpr uint8_t CCM_TupleShift{8}; - inline constexpr uint8_t CCM_TupleMask{0x1F}; - inline constexpr uint32_t ClockNeededRunWaitMode{kCLOCK_ClockNeededRunWait}; class RT1051LPM : public LowPowerMode { @@ -24,11 +20,8 @@ namespace bsp int32_t Reboot() override final; void SetCpuFrequency(CpuFrequency freq) final; void SwitchOscillatorSource(OscillatorSource source) final; - void SwitchPll2State(Pll2State state) final; private: - [[nodiscard]] bool IsClockEnabled(clock_ip_name_t name) const noexcept; - std::shared_ptr gpio; std::unique_ptr CpuFreq; }; diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverLPSPI.cpp b/module-bsp/board/rt1051/drivers/RT1051DriverLPSPI.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2097ff566a8cef199dd4993d82f5d158f365f293 --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverLPSPI.cpp @@ -0,0 +1,51 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "RT1051DriverLPSPI.hpp" +#include "critical.hpp" +#include "board/rt1051/bsp/lpm/ClockState.hpp" + +namespace drivers +{ + + RT1051DriverLPSPI::RT1051DriverLPSPI(std::string name, LPSPIInstances inst) : DriverLPSPI(std::move(name), inst) + { + switch (instance) { + case LPSPIInstances::LPSPI_1: + lpspiClock = kCLOCK_Lpspi1; + break; + case LPSPIInstances::LPSPI_2: + lpspiClock = kCLOCK_Lpspi2; + break; + case LPSPIInstances::LPSPI_3: + lpspiClock = kCLOCK_Lpspi3; + break; + case LPSPIInstances::LPSPI_4: + lpspiClock = kCLOCK_Lpspi4; + break; + } + } + + void RT1051DriverLPSPI::Enable() + { + cpp_freertos::CriticalSection::Enter(); + if (!pll2Driver) { + pll2Driver = std::make_shared(); + } + if (!bsp::IsClockEnabled(lpspiClock)) { + CLOCK_EnableClock(lpspiClock); + } + cpp_freertos::CriticalSection::Exit(); + } + + void RT1051DriverLPSPI::Disable() + { + cpp_freertos::CriticalSection::Enter(); + if (bsp::IsClockEnabled(lpspiClock)) { + CLOCK_DisableClock(lpspiClock); + } + pll2Driver.reset(); + cpp_freertos::CriticalSection::Exit(); + } + +} // namespace drivers diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverLPSPI.hpp b/module-bsp/board/rt1051/drivers/RT1051DriverLPSPI.hpp new file mode 100644 index 0000000000000000000000000000000000000000..202b267e4a694993ee6d1dac66be9fb3bf248bc5 --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverLPSPI.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/lpspi/DriverLPSPI.hpp" +#include "RT1051DriverPLL2.hpp" +#include "board/rt1051/common/clock_config.h" + +namespace drivers +{ + class RT1051DriverLPSPI : public DriverLPSPI + { + public: + explicit RT1051DriverLPSPI(std::string name, LPSPIInstances inst); + + void Enable() final; + void Disable() final; + + private: + std::shared_ptr pll2Driver; + clock_ip_name_t lpspiClock; + }; + +} // namespace drivers diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverPLL2.cpp b/module-bsp/board/rt1051/drivers/RT1051DriverPLL2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ced3ae9b01147e9da39259b1fa71b35c491201a --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverPLL2.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "RT1051DriverPLL2.hpp" +#include "board/rt1051/bsp/lpm/ClockState.hpp" + +namespace drivers +{ + + RT1051DriverPLL2::RT1051DriverPLL2() noexcept + { + if (!IsPLL2Enabled()) { + clkPLL2setup(CLK_ENABLE); + } + } + + RT1051DriverPLL2::~RT1051DriverPLL2() + { + if ((CLOCK_GetMux(kCLOCK_SemcMux) == SemcMuxPeripheralClock) && !bsp::IsClockEnabled(kCLOCK_Lpspi1) && + !bsp::IsClockEnabled(kCLOCK_Lpspi2) && !bsp::IsClockEnabled(kCLOCK_Lpspi3) && + !bsp::IsClockEnabled(kCLOCK_Lpspi4) && !bsp::IsClockEnabled(kCLOCK_Usdhc1) && + !bsp::IsClockEnabled(kCLOCK_Usdhc2)) { + + clkPLL2setup(CLK_DISABLE); + } + } + + bool RT1051DriverPLL2::IsPLL2Enabled() const noexcept + { + return !(CCM_ANALOG->PLL_SYS & CCM_ANALOG_PLL_SYS_POWERDOWN_MASK); + } + +} // namespace drivers diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverPLL2.hpp b/module-bsp/board/rt1051/drivers/RT1051DriverPLL2.hpp new file mode 100644 index 0000000000000000000000000000000000000000..008674037071528c472029180741d65b4038186e --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverPLL2.hpp @@ -0,0 +1,23 @@ +// 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 +{ + inline constexpr uint32_t SemcMuxPLL2Clock{1}; + inline constexpr uint32_t SemcMuxPeripheralClock{0}; + + class RT1051DriverPLL2 + { + public: + RT1051DriverPLL2() noexcept; + ~RT1051DriverPLL2(); + + private: + [[nodiscard]] bool IsPLL2Enabled() const noexcept; + }; + +} // namespace drivers diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverSEMC.cpp b/module-bsp/board/rt1051/drivers/RT1051DriverSEMC.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94287a0bb3699943becce073b7ed158adb5f4dde --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverSEMC.cpp @@ -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 + +#include "RT1051DriverSEMC.hpp" +#include "critical.hpp" +#include "board/rt1051/common/clock_config.h" + +namespace drivers +{ + + RT1051DriverSEMC::RT1051DriverSEMC(std::string name) : DriverSEMC(std::move(name)) + { + SwitchToPLL2ClockSource(); + } + + void RT1051DriverSEMC::Enable() + {} + + void RT1051DriverSEMC::Disable() + {} + + void RT1051DriverSEMC::SwitchToPLL2ClockSource() + { + cpp_freertos::CriticalSection::Enter(); + if (!pll2Driver) { + pll2Driver = std::make_shared(); + } + CLOCK_SetMux(kCLOCK_SemcMux, SemcMuxPLL2Clock); + cpp_freertos::CriticalSection::Exit(); + } + + void RT1051DriverSEMC::SwitchToPeripheralClockSource() + { + cpp_freertos::CriticalSection::Enter(); + CLOCK_SetMux(kCLOCK_SemcMux, SemcMuxPeripheralClock); + pll2Driver.reset(); + cpp_freertos::CriticalSection::Exit(); + } + +} // namespace drivers diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverSEMC.hpp b/module-bsp/board/rt1051/drivers/RT1051DriverSEMC.hpp new file mode 100644 index 0000000000000000000000000000000000000000..e47c8b84d2b0e83933058cfff310f868cd580927 --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverSEMC.hpp @@ -0,0 +1,26 @@ +// 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/semc/DriverSEMC.hpp" +#include "RT1051DriverPLL2.hpp" + +namespace drivers +{ + class RT1051DriverSEMC : public DriverSEMC + { + public: + explicit RT1051DriverSEMC(std::string name); + + void Enable() final; + void Disable() final; + + void SwitchToPLL2ClockSource() final; + void SwitchToPeripheralClockSource() final; + + private: + std::shared_ptr pll2Driver; + }; + +} // namespace drivers diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverUSDHC.cpp b/module-bsp/board/rt1051/drivers/RT1051DriverUSDHC.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd2cc5c11cb376f6e4852f24f879276ce6b65b3c --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverUSDHC.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "RT1051DriverUSDHC.hpp" +#include "critical.hpp" +#include "board/rt1051/bsp/lpm/ClockState.hpp" + +namespace drivers +{ + + RT1051DriverUSDHC::RT1051DriverUSDHC(std::string name, USDHCInstances inst) : DriverUSDHC(std::move(name), inst) + { + switch (instance) { + case USDHCInstances::USDHC_1: + usdhcClock = kCLOCK_Usdhc1; + break; + case USDHCInstances::USDHC_2: + usdhcClock = kCLOCK_Usdhc2; + break; + } + } + + void RT1051DriverUSDHC::Enable() + { + cpp_freertos::CriticalSection::Enter(); + if (!pll2Driver) { + pll2Driver = std::make_shared(); + } + if (!bsp::IsClockEnabled(usdhcClock)) { + CLOCK_EnableClock(usdhcClock); + } + cpp_freertos::CriticalSection::Exit(); + } + + void RT1051DriverUSDHC::Disable() + { + cpp_freertos::CriticalSection::Enter(); + if (bsp::IsClockEnabled(usdhcClock)) { + CLOCK_DisableClock(usdhcClock); + } + pll2Driver.reset(); + cpp_freertos::CriticalSection::Exit(); + } + +} // namespace drivers diff --git a/module-bsp/board/rt1051/drivers/RT1051DriverUSDHC.hpp b/module-bsp/board/rt1051/drivers/RT1051DriverUSDHC.hpp new file mode 100644 index 0000000000000000000000000000000000000000..6a32ed2330c9e4d1a70be808627116fa59cf1484 --- /dev/null +++ b/module-bsp/board/rt1051/drivers/RT1051DriverUSDHC.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/usdhc/DriverUSDHC.hpp" +#include "RT1051DriverPLL2.hpp" +#include "board/rt1051/common/clock_config.h" + +namespace drivers +{ + class RT1051DriverUSDHC : public DriverUSDHC + { + public: + explicit RT1051DriverUSDHC(std::string name, USDHCInstances inst); + + void Enable() final; + void Disable() final; + + private: + std::shared_ptr pll2Driver; + clock_ip_name_t usdhcClock; + }; + +} // namespace drivers diff --git a/module-bsp/bsp/BoardDefinitions.hpp b/module-bsp/bsp/BoardDefinitions.hpp index 97e4fd04664d28879de47b6d56c203f13855ffb9..11e0731bcad69328639a9fdb97bbb5d52f8221a0 100644 --- a/module-bsp/bsp/BoardDefinitions.hpp +++ b/module-bsp/bsp/BoardDefinitions.hpp @@ -6,6 +6,8 @@ #include "drivers/i2c/DriverI2C.hpp" #include "drivers/pll/DriverPLL.hpp" #include "drivers/gpio/DriverGPIO.hpp" +#include "drivers/lpspi/DriverLPSPI.hpp" +#include "drivers/usdhc/DriverUSDHC.hpp" enum class BoardDefinitions { @@ -84,6 +86,7 @@ enum class BoardDefinitions EINK_RESET_PIN=16, EINK_BUSY_PIN=17, EINK_PLL = static_cast(drivers::PLLInstances::PLL2_PFD2), + EINK_LPSPI_INSTANCE = static_cast(drivers::LPSPIInstances::LPSPI_1), BLUETOOTH_DMA = static_cast(drivers::DMAInstances ::DMA_0), BLUETOOTH_DMAMUX = static_cast(drivers::DMAMuxInstances ::DMAMUX0), @@ -91,6 +94,7 @@ enum class BoardDefinitions BLUETOOTH_RX_DMA_CHANNEL = 8, EMMC_PLL = static_cast(drivers::PLLInstances::PLL2_PFD2), + EMMC_USDHC_INSTANCE = static_cast(drivers::USDHCInstances::USDHC_2), AUDIO_PLL = static_cast(drivers::PLLInstances::PLL4_Audio), diff --git a/module-bsp/bsp/lpm/bsp_lpm.cpp b/module-bsp/bsp/lpm/bsp_lpm.cpp index 39fd726f39c0db3f0124bb50d17204a4b6da112e..da90e0eabc2fc4984df65b135e211cdcdd9c015c 100644 --- a/module-bsp/bsp/lpm/bsp_lpm.cpp +++ b/module-bsp/bsp/lpm/bsp_lpm.cpp @@ -35,9 +35,4 @@ namespace bsp{ { return currentOscSource; } - - LowPowerMode::Pll2State LowPowerMode::GetCurrentPll2State() const noexcept - { - return currentPll2State; - } } diff --git a/module-bsp/bsp/lpm/bsp_lpm.hpp b/module-bsp/bsp/lpm/bsp_lpm.hpp index 03ed25da2188f4824a09cc71973510dfce310a91..0f7705a6820d770ecaff83fe85c5ebf01587cc3d 100644 --- a/module-bsp/bsp/lpm/bsp_lpm.hpp +++ b/module-bsp/bsp/lpm/bsp_lpm.hpp @@ -23,11 +23,6 @@ namespace bsp { External, Internal }; - enum class Pll2State - { - Enable, - Disable - }; LowPowerMode() = default; virtual ~LowPowerMode() = default; @@ -43,13 +38,9 @@ namespace bsp { virtual void SwitchOscillatorSource(OscillatorSource source) = 0; [[nodiscard]] OscillatorSource GetCurrentOscillatorSource() const noexcept; - virtual void SwitchPll2State(Pll2State state) = 0; - [[nodiscard]] Pll2State GetCurrentPll2State() const noexcept; - protected: CpuFrequency currentFrequency = CpuFrequency::Level_6; OscillatorSource currentOscSource = OscillatorSource::External; - Pll2State currentPll2State = Pll2State::Enable; }; } // namespace bsp diff --git a/module-bsp/devices/Device.cpp b/module-bsp/devices/Device.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98bad31f542dd1f72953a68068f7a4c729bc8c94 --- /dev/null +++ b/module-bsp/devices/Device.cpp @@ -0,0 +1,22 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "Device.hpp" + +namespace devices +{ + + Device::Device(std::string name) : name(std::move(name)) + {} + + bool Device::IsEnabled() const noexcept + { + return isEnabled; + } + + [[nodiscard]] auto Device::GetName() const noexcept -> std::string + { + return name; + } + +} // namespace devices diff --git a/module-bsp/devices/Device.hpp b/module-bsp/devices/Device.hpp new file mode 100644 index 0000000000000000000000000000000000000000..fca8a6a17e6ce7c47f565ccd4deb7b9cd825e51f --- /dev/null +++ b/module-bsp/devices/Device.hpp @@ -0,0 +1,27 @@ +// 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 + +namespace devices +{ + class Device + { + public: + explicit Device(std::string name); + virtual ~Device() = default; + + virtual void Enable() = 0; + virtual void Disable() = 0; + [[nodiscard]] bool IsEnabled() const noexcept; + [[nodiscard]] auto GetName() const noexcept -> std::string; + + protected: + bool isEnabled{false}; + + const std::string name; + }; + +} // namespace devices diff --git a/module-bsp/drivers/lpspi/DriverLPSPI.cpp b/module-bsp/drivers/lpspi/DriverLPSPI.cpp new file mode 100644 index 0000000000000000000000000000000000000000..045aee00c18770cac885fe366ca768b39d72df79 --- /dev/null +++ b/module-bsp/drivers/lpspi/DriverLPSPI.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 "DriverLPSPI.hpp" +#include "critical.hpp" + +#if defined(TARGET_RT1051) +#include "board/rt1051/drivers/RT1051DriverLPSPI.hpp" +#elif defined(TARGET_Linux) + +#else +#error "Unsupported target" +#endif + +namespace drivers +{ + + std::weak_ptr DriverLPSPI::singleton[magic_enum::enum_count()]; + + std::shared_ptr DriverLPSPI::Create(std::string name, const drivers::LPSPIInstances 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/lpspi/DriverLPSPI.hpp b/module-bsp/drivers/lpspi/DriverLPSPI.hpp new file mode 100644 index 0000000000000000000000000000000000000000..071eac2b96262ecd4e895aadce77ec04c1fa072d --- /dev/null +++ b/module-bsp/drivers/lpspi/DriverLPSPI.hpp @@ -0,0 +1,36 @@ +// 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 LPSPIInstances + { + LPSPI_1, + LPSPI_2, + LPSPI_3, + LPSPI_4 + }; + + class DriverLPSPI : public devices::Device + { + public: + static std::shared_ptr Create(std::string name, const LPSPIInstances inst); + + explicit DriverLPSPI(std::string name, LPSPIInstances inst) : devices::Device(std::move(name)), instance(inst) + {} + + protected: + LPSPIInstances instance; + + private: + static std::weak_ptr singleton[magic_enum::enum_count()]; + }; + +} // namespace drivers diff --git a/module-bsp/drivers/semc/DriverSEMC.cpp b/module-bsp/drivers/semc/DriverSEMC.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63e568b15354339b20157125f511ca21ae3e441c --- /dev/null +++ b/module-bsp/drivers/semc/DriverSEMC.cpp @@ -0,0 +1,42 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "DriverSEMC.hpp" +#include "critical.hpp" + +#if defined(TARGET_RT1051) +#include "board/rt1051/drivers/RT1051DriverSEMC.hpp" +#elif defined(TARGET_Linux) + +#else +#error "Unsupported target" +#endif + +namespace drivers +{ + + std::weak_ptr DriverSEMC::singleton; + + std::shared_ptr DriverSEMC::Create(std::string name) + { + + cpp_freertos::CriticalSection::Enter(); + std::shared_ptr inst = singleton.lock(); + + if (!inst) { +#if defined(TARGET_RT1051) + inst = std::make_shared(std::move(name)); +#elif defined(TARGET_Linux) +#else +#error "Unsupported target" +#endif + + singleton = std::move(inst); + } + + cpp_freertos::CriticalSection::Exit(); + + return inst; + } + +} // namespace drivers diff --git a/module-bsp/drivers/semc/DriverSEMC.hpp b/module-bsp/drivers/semc/DriverSEMC.hpp new file mode 100644 index 0000000000000000000000000000000000000000..61fc7d857f7dbdf2f8a8afcda52847c187bfff9f --- /dev/null +++ b/module-bsp/drivers/semc/DriverSEMC.hpp @@ -0,0 +1,27 @@ +// 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 + +namespace drivers +{ + + class DriverSEMC : public devices::Device + { + public: + static std::shared_ptr Create(std::string name); + + explicit DriverSEMC(std::string name) : devices::Device(std::move(name)) + {} + + virtual void SwitchToPLL2ClockSource() = 0; + virtual void SwitchToPeripheralClockSource() = 0; + + private: + static std::weak_ptr singleton; + }; + +} // namespace drivers diff --git a/module-bsp/drivers/usdhc/DriverUSDHC.cpp b/module-bsp/drivers/usdhc/DriverUSDHC.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95ede7f1df5eb40f7b24b2fd3d321987b481a229 --- /dev/null +++ b/module-bsp/drivers/usdhc/DriverUSDHC.cpp @@ -0,0 +1,42 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "DriverUSDHC.hpp" +#include "critical.hpp" + +#if defined(TARGET_RT1051) +#include "board/rt1051/drivers/RT1051DriverUSDHC.hpp" +#elif defined(TARGET_Linux) + +#else +#error "Unsupported target" +#endif + +namespace drivers +{ + + std::weak_ptr DriverUSDHC::singleton[magic_enum::enum_count()]; + + std::shared_ptr DriverUSDHC::Create(std::string name, const drivers::USDHCInstances 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/usdhc/DriverUSDHC.hpp b/module-bsp/drivers/usdhc/DriverUSDHC.hpp new file mode 100644 index 0000000000000000000000000000000000000000..4c40e31d292988d156100784be1dd8cb1cb234ad --- /dev/null +++ b/module-bsp/drivers/usdhc/DriverUSDHC.hpp @@ -0,0 +1,34 @@ +// 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 USDHCInstances + { + USDHC_1, + USDHC_2 + }; + + class DriverUSDHC : public devices::Device + { + public: + static std::shared_ptr Create(std::string name, const USDHCInstances inst); + + explicit DriverUSDHC(std::string name, USDHCInstances inst) : devices::Device(std::move(name)), instance(inst) + {} + + protected: + USDHCInstances 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 1fd314675b2bdf000dad4d85c0f5c342dec75d86..6e6e7a1d3674c371f29a4cd4b6d7d8354867afca 100644 --- a/module-bsp/targets/Target_RT1051.cmake +++ b/module-bsp/targets/Target_RT1051.cmake @@ -52,6 +52,7 @@ set(BOARD_SOURCES ${BOARD_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bsp/watchdog/watchdog.cpp" "${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/bluetooth/BluetoothCommon.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bluetooth/BlueKitchen.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bsp/rtc/rtc.cpp" @@ -63,6 +64,10 @@ set(BOARD_SOURCES ${BOARD_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/bsp/audio/qfilter.c" "${USB_SRC}" "${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/RT1051DriverUSDHC.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverSEMC.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverI2C.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverPWM.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/board/rt1051/drivers/RT1051DriverDMAMux.cpp" diff --git a/module-services/service-eink/EinkDisplay.cpp b/module-services/service-eink/EinkDisplay.cpp index ec8a6ec61f87c2edc09efb02b021ad39f601fd75..b003733100b322ebdc61963f6cf162947d53836b 100644 --- a/module-services/service-eink/EinkDisplay.cpp +++ b/module-services/service-eink/EinkDisplay.cpp @@ -5,7 +5,7 @@ #include #include - +#include #include #include @@ -45,7 +45,10 @@ namespace service::eink EinkDisplay::EinkDisplay(::gui::Size screenSize) : size{screenSize}, currentWaveform{createDefaultWaveFormSettings(EinkWaveformGC16)}, displayMode{EinkDisplayColorMode_e::EinkDisplayColorModeStandard} - {} + { + driverLPSPI = drivers::DriverLPSPI::Create( + "EInk", static_cast(BoardDefinitions::EINK_LPSPI_INSTANCE)); + } EinkDisplay::~EinkDisplay() noexcept { @@ -65,12 +68,18 @@ namespace service::eink void EinkDisplay::powerOn() { + if (driverLPSPI) { + driverLPSPI->Enable(); + } EinkPowerOn(); } void EinkDisplay::powerOff() { EinkPowerOff(); + if (driverLPSPI) { + driverLPSPI->Disable(); + } } void EinkDisplay::shutdown() @@ -241,4 +250,10 @@ namespace service::eink { return size; } + + [[nodiscard]] auto EinkDisplay::getDevice() const noexcept -> std::shared_ptr + { + return driverLPSPI; + } + } // namespace service::eink diff --git a/module-services/service-eink/EinkDisplay.hpp b/module-services/service-eink/EinkDisplay.hpp index 4f61bba6eaeba5412501441a2ba652de63d1a3d9..08316bfa2edb544339d6986eec710b654c557e87 100644 --- a/module-services/service-eink/EinkDisplay.hpp +++ b/module-services/service-eink/EinkDisplay.hpp @@ -10,6 +10,7 @@ #include #include +#include "drivers/lpspi/DriverLPSPI.hpp" namespace service::eink { @@ -38,6 +39,8 @@ namespace service::eink std::int32_t getLastTemperature() const noexcept; ::gui::Size getSize() const noexcept; + [[nodiscard]] auto getDevice() const noexcept -> std::shared_ptr; + private: static unsigned int toWaveformTemperatureOffset(std::int32_t temperature) noexcept; static unsigned int toWaveformOffset(unsigned short LUTbank, unsigned int temperatureOffset) noexcept; @@ -50,5 +53,6 @@ namespace service::eink const ::gui::Size size; EinkWaveformSettings_t currentWaveform; EinkDisplayColorMode_e displayMode; + std::shared_ptr driverLPSPI; }; } // namespace service::eink diff --git a/module-services/service-eink/ServiceEink.cpp b/module-services/service-eink/ServiceEink.cpp index 064b6a6add0aa03fbe6c25e247f586290a87d1b7..61220b564cdc38cd116a87bd31ab9fb9a964d620 100644 --- a/module-services/service-eink/ServiceEink.cpp +++ b/module-services/service-eink/ServiceEink.cpp @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include @@ -48,6 +50,9 @@ namespace service::eink return sys::ReturnCodes::Failure; } + auto deviceRegistrationMsg = std::make_shared(display.getDevice()); + bus.sendUnicast(deviceRegistrationMsg, service::name::system_manager); + display.powerOn(); auto msg = std::make_shared(display.getSize()); diff --git a/module-sys/CMakeLists.txt b/module-sys/CMakeLists.txt index 2378ba51c3495e86d5a42b5a627a094db9bf49c5..91e3e903428da50d42890071470be94178409e3e 100644 --- a/module-sys/CMakeLists.txt +++ b/module-sys/CMakeLists.txt @@ -16,6 +16,7 @@ set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/SystemManager/SystemManager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/SystemManager/PowerManager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/SystemManager/CpuStatistics.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/SystemManager/DeviceManager.cpp ) diff --git a/module-sys/SystemManager/DeviceManager.cpp b/module-sys/SystemManager/DeviceManager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1dd940298f37c6f42179996ed8eee7f0ef20c449 --- /dev/null +++ b/module-sys/SystemManager/DeviceManager.cpp @@ -0,0 +1,47 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "DeviceManager.hpp" +#include +#include + +namespace sys +{ + + void DeviceManager::RegisterNewDevice(std::shared_ptr newDevice) + { + if (newDevice) { + devices.push_back(newDevice); + } + } + + void DeviceManager::PrintAllDevices() const noexcept + { + std::for_each(std::begin(devices), std::end(devices), PrintName); + } + + void DeviceManager::DisableAllDevices() const noexcept + { + std::for_each(std::begin(devices), std::end(devices), DisableDevice); + } + + void DeviceManager::PrintName(const DevicePointer &element) + { + if (!element.expired()) { + std::shared_ptr sharedResource = element.lock(); + LOG_INFO("Device %s", sharedResource->GetName().c_str()); + } + } + + void DeviceManager::DisableDevice(const DevicePointer &element) + { + if (!element.expired()) { + std::shared_ptr sharedResource = element.lock(); + if (sharedResource->IsEnabled()) { + sharedResource->Disable(); + LOG_DEBUG("Device %s has been disabled", sharedResource->GetName().c_str()); + } + } + } + +} // namespace sys diff --git a/module-sys/SystemManager/DeviceManager.hpp b/module-sys/SystemManager/DeviceManager.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c042678a684e2bfe26801170f3e0abcf085f2169 --- /dev/null +++ b/module-sys/SystemManager/DeviceManager.hpp @@ -0,0 +1,30 @@ +// 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 "devices/Device.hpp" + +namespace sys +{ + using DevicePointer = std::weak_ptr; + using DeviceVector = std::vector; + + class DeviceManager + { + + public: + void RegisterNewDevice(std::shared_ptr newDevice); + void PrintAllDevices() const noexcept; + void DisableAllDevices() const noexcept; + + private: + static void PrintName(const DevicePointer &element); + static void DisableDevice(const DevicePointer &element); + + DeviceVector devices; + }; + +} // namespace sys diff --git a/module-sys/SystemManager/PowerManager.cpp b/module-sys/SystemManager/PowerManager.cpp index 174e9a838557a13790daf2c04a0a81fd2d697404..874cb1d3799c140a5aee02896aa3f0e2c441573d 100644 --- a/module-sys/SystemManager/PowerManager.cpp +++ b/module-sys/SystemManager/PowerManager.cpp @@ -10,6 +10,7 @@ namespace sys PowerManager::PowerManager() { lowPowerControl = bsp::LowPowerMode::Create().value_or(nullptr); + driverSEMC = drivers::DriverSEMC::Create("ExternalRAM"); } PowerManager::~PowerManager() @@ -55,17 +56,17 @@ namespace sys { const auto freq = lowPowerControl->GetCurrentFrequency(); const auto oscSource = lowPowerControl->GetCurrentOscillatorSource(); - const auto pll2State = lowPowerControl->GetCurrentPll2State(); - // switch osc source first - if (freq == bsp::LowPowerMode::CpuFrequency::Level_1 && - oscSource == bsp::LowPowerMode::OscillatorSource::Internal) { - lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::External); - } + if (freq == bsp::LowPowerMode::CpuFrequency::Level_1) { + // switch osc source first + if (oscSource == bsp::LowPowerMode::OscillatorSource::Internal) { + lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::External); + } - // then turn on pll2 - if (pll2State == bsp::LowPowerMode::Pll2State::Disable) { - lowPowerControl->SwitchPll2State(bsp::LowPowerMode::Pll2State::Enable); + // then switch external RAM clock source + if (driverSEMC) { + driverSEMC->SwitchToPLL2ClockSource(); + } } // and increase frequency @@ -97,16 +98,15 @@ namespace sys if (level == bsp::LowPowerMode::CpuFrequency::Level_1) { const auto oscSource = lowPowerControl->GetCurrentOscillatorSource(); - const auto pll2State = lowPowerControl->GetCurrentPll2State(); // then switch osc source if (oscSource == bsp::LowPowerMode::OscillatorSource::External) { lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::Internal); } - // and turn off pll2 - if (pll2State == bsp::LowPowerMode::Pll2State::Enable) { - lowPowerControl->SwitchPll2State(bsp::LowPowerMode::Pll2State::Disable); + // and switch external RAM clock source + if (driverSEMC) { + driverSEMC->SwitchToPeripheralClockSource(); } } } @@ -116,4 +116,10 @@ namespace sys aboveThresholdCounter = 0; belowThresholdCounter = 0; } + + [[nodiscard]] auto PowerManager::getExternalRamDevice() const noexcept -> std::shared_ptr + { + return driverSEMC; + } + } // namespace sys diff --git a/module-sys/SystemManager/PowerManager.hpp b/module-sys/SystemManager/PowerManager.hpp index da4840cf69b2fa9c3c419d69e2b61805a4257b07..8b0f63fa06c2f063b112a39abf438ba62f214575 100644 --- a/module-sys/SystemManager/PowerManager.hpp +++ b/module-sys/SystemManager/PowerManager.hpp @@ -7,6 +7,7 @@ #include #include "bsp/lpm/bsp_lpm.hpp" +#include "drivers/semc/DriverSEMC.hpp" namespace sys { @@ -42,6 +43,8 @@ namespace sys /// @note the frequency is always reduced by one step void DecreaseCpuFrequency() const; + [[nodiscard]] auto getExternalRamDevice() const noexcept -> std::shared_ptr; + private: void ResetFrequencyShiftCounter(); @@ -49,6 +52,7 @@ namespace sys uint32_t aboveThresholdCounter{0}; std::unique_ptr lowPowerControl; + std::shared_ptr driverSEMC; }; } // namespace sys diff --git a/module-sys/SystemManager/SystemManager.cpp b/module-sys/SystemManager/SystemManager.cpp index ef314e377b7332dc25bc189fb124ee3e4e9628c1..c7cbc0f1a89268d04be7b707480e3f748945f9cd 100644 --- a/module-sys/SystemManager/SystemManager.cpp +++ b/module-sys/SystemManager/SystemManager.cpp @@ -17,6 +17,7 @@ #include #include #include "messages/CpuFrequencyMessage.hpp" +#include "messages/DeviceRegistrationMessage.hpp" const inline size_t systemManagerStack = 4096 * 2; @@ -106,6 +107,7 @@ namespace sys { powerManager = std::make_unique(); cpuStatistics = std::make_unique(); + deviceManager = std::make_unique(); userInit = init; @@ -299,6 +301,15 @@ namespace sys return sys::MessageNone{}; }); + connect(typeid(sys::DeviceRegistrationMessage), [this](sys::Message *message) -> sys::MessagePointer { + auto msg = static_cast(message); + deviceManager->RegisterNewDevice(msg->getDevice()); + + return sys::MessageNone{}; + }); + + deviceManager->RegisterNewDevice(powerManager->getExternalRamDevice()); + return ReturnCodes::Success; } @@ -361,5 +372,6 @@ namespace sys cpp_freertos::MutexStandard SystemManager::destroyMutex; std::unique_ptr SystemManager::powerManager; std::unique_ptr SystemManager::cpuStatistics; + std::unique_ptr SystemManager::deviceManager; } // namespace sys diff --git a/module-sys/SystemManager/SystemManager.hpp b/module-sys/SystemManager/SystemManager.hpp index 4c019ad94e9e6ab77b9d57270e63cab77b644bfc..a6bd15788380e0ef2f3111df2fe53819a6a72ada 100644 --- a/module-sys/SystemManager/SystemManager.hpp +++ b/module-sys/SystemManager/SystemManager.hpp @@ -21,6 +21,7 @@ #include "PowerManager.hpp" #include "Constants.hpp" #include "CpuStatistics.hpp" +#include "DeviceManager.hpp" #include namespace sys @@ -142,6 +143,7 @@ namespace sys static cpp_freertos::MutexStandard destroyMutex; static std::unique_ptr powerManager; static std::unique_ptr cpuStatistics; + static std::unique_ptr deviceManager; }; } // namespace sys diff --git a/module-sys/SystemManager/messages/DeviceRegistrationMessage.hpp b/module-sys/SystemManager/messages/DeviceRegistrationMessage.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c06749a9ec66ee42cc984e926513d7e3c6518551 --- /dev/null +++ b/module-sys/SystemManager/messages/DeviceRegistrationMessage.hpp @@ -0,0 +1,28 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include "devices/Device.hpp" + +namespace sys +{ + class DeviceRegistrationMessage : public sys::DataMessage + { + public: + explicit DeviceRegistrationMessage(std::shared_ptr devicePtr) + : sys::DataMessage(MessageType::DeviceRegistration), device(devicePtr) + {} + + [[nodiscard]] auto getDevice() const noexcept -> std::shared_ptr + { + return device; + }; + + private: + std::shared_ptr device; + }; +} // namespace sys diff --git a/module-utils/Utils.hpp b/module-utils/Utils.hpp index 26da74259daf9a2ebbe788a5320e13fa48914dc7..c79cefd8082b99bca3f765cd85fd871a26d3faa4 100644 --- a/module-utils/Utils.hpp +++ b/module-utils/Utils.hpp @@ -12,7 +12,6 @@ #include #include "time/time_conversion.hpp" -#define MAGIC_ENUM_RANGE_MAX 256 #include namespace utils diff --git a/source/MessageType.hpp b/source/MessageType.hpp index ee7d9d2dea5a0370258779866a1943341d999f20..7a25a68bbce3ba2c0470ce8c9ad5e40cd47e27d1 100644 --- a/source/MessageType.hpp +++ b/source/MessageType.hpp @@ -166,6 +166,8 @@ enum class MessageType // Power manager PMChangePowerMode, + // System manager + DeviceRegistration, // battery charger messages EVMBatteryLevel,