~aleteoryx/muditaos

1521858134d0de81e142a7fe37313748a8b25013 — Maciej-Mudita 5 years ago ff180ed
[EGD-4887] Add PLL2 clock switching

Pll2 will be disabled at the lowest CPU frequency,
which will save power
M changelog.md => changelog.md +1 -0
@@ 5,6 5,7 @@

* Battery Brownout detection
* VoLTE ON/OFF switch in Settings Network window
* Add PLL2 clock switching

### Changed


M module-bsp/board/linux/lpm/LinuxLPM.cpp => module-bsp/board/linux/lpm/LinuxLPM.cpp +5 -0
@@ 29,4 29,9 @@ namespace bsp
    {
        currentOscSource = source;
    }

    void LinuxLPM::SwitchPll2State(bsp::LowPowerMode::Pll2State state)
    {
        currentPll2State = state;
    }
} // namespace bsp

M module-bsp/board/linux/lpm/LinuxLPM.h => module-bsp/board/linux/lpm/LinuxLPM.h +1 -0
@@ 17,6 17,7 @@ namespace bsp
        int32_t Reboot() override final;
        void SetCpuFrequency(CpuFrequency freq) final;
        void SwitchOscillatorSource(OscillatorSource source) final;
        void SwitchPll2State(Pll2State state) final;
    };

} // namespace bsp

M module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp => module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp +24 -0
@@ 7,6 7,7 @@
#include "log/log.hpp"
#include "bsp/BoardDefinitions.hpp"
#include "bsp/watchdog/watchdog.hpp"
#include <clock_config.h>
#include <fsl_clock.h>
#include <fsl_dcdc.h>



@@ 101,6 102,29 @@ 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<uint32_t>(name) >> CCM_TupleShift;

M module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp => module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp +2 -1
@@ 14,7 14,7 @@ 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{3};
    inline constexpr uint32_t ClockNeededRunWaitMode{kCLOCK_ClockNeededRunWait};

    class RT1051LPM : public LowPowerMode
    {


@@ 24,6 24,7 @@ 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;

M module-bsp/bsp/lpm/bsp_lpm.cpp => module-bsp/bsp/lpm/bsp_lpm.cpp +5 -0
@@ 35,4 35,9 @@ namespace bsp{
    {
    	return currentOscSource;
    }

    LowPowerMode::Pll2State LowPowerMode::GetCurrentPll2State() const noexcept
    {
    	return currentPll2State;
    }
}

M module-bsp/bsp/lpm/bsp_lpm.hpp => module-bsp/bsp/lpm/bsp_lpm.hpp +9 -0
@@ 23,6 23,11 @@ namespace bsp {
            External,
            Internal
        };
        enum class Pll2State
        {
            Enable,
            Disable
        };

        LowPowerMode()          = default;
        virtual ~LowPowerMode() = default;


@@ 38,9 43,13 @@ 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


M module-sys/SystemManager/PowerManager.cpp => module-sys/SystemManager/PowerManager.cpp +22 -8
@@ 55,14 55,20 @@ namespace sys
    {
        const auto freq      = lowPowerControl->GetCurrentFrequency();
        const auto oscSource = lowPowerControl->GetCurrentOscillatorSource();
        const auto pll2State = lowPowerControl->GetCurrentPll2State();

        /// switch osc source first
        // switch osc source first
        if (freq == bsp::LowPowerMode::CpuFrequency::Level_1 &&
            oscSource == bsp::LowPowerMode::OscillatorSource::Internal) {
            lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::External);
        }

        /// then increase frequency
        // then turn on pll2
        if (pll2State == bsp::LowPowerMode::Pll2State::Disable) {
            lowPowerControl->SwitchPll2State(bsp::LowPowerMode::Pll2State::Enable);
        }

        // and increase frequency
        if (freq < bsp::LowPowerMode::CpuFrequency::Level_6) {
            lowPowerControl->SetCpuFrequency(bsp::LowPowerMode::CpuFrequency::Level_6);
        }


@@ 71,7 77,6 @@ namespace sys
    void PowerManager::DecreaseCpuFrequency() const
    {
        const auto freq = lowPowerControl->GetCurrentFrequency();
        const auto oscSource = lowPowerControl->GetCurrentOscillatorSource();
        auto level      = bsp::LowPowerMode::CpuFrequency::Level_1;

        switch (freq) {


@@ 94,15 99,24 @@ namespace sys
            break;
        }

        /// decrease frequency first
        // decrease frequency first
        if (level != freq) {
            lowPowerControl->SetCpuFrequency(level);
        }

        /// then switch osc source
        if (level == bsp::LowPowerMode::CpuFrequency::Level_1 &&
            oscSource == bsp::LowPowerMode::OscillatorSource::External) {
            lowPowerControl->SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource::Internal);
        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);
            }
        }
    }